Skip to main content

rspirv/grammar/
reflect.rs

1//! Reflect functions for SPIR-V instructions.
2
3use crate::spirv;
4
5/// Returns [`true`] if the given opcode is for a location debug instruction.
6pub fn is_location_debug(opcode: spirv::Op) -> bool {
7    matches!(opcode, spirv::Op::Line | spirv::Op::NoLine)
8}
9
10/// Returns [`true`] if the given opcode is for a variable-defining instruction.
11pub fn is_variable(opcode: spirv::Op) -> bool {
12    opcode == spirv::Op::Variable
13}
14
15/// Returns [`true`] if the given opcode is a return instruction.
16pub fn is_return(opcode: spirv::Op) -> bool {
17    matches!(opcode, spirv::Op::Return | spirv::Op::ReturnValue)
18}
19
20/// Returns [`true`] if the given opcode aborts execution.
21pub fn is_abort(opcode: spirv::Op) -> bool {
22    matches!(
23        opcode,
24        spirv::Op::Kill
25            | spirv::Op::TerminateInvocation
26            | spirv::Op::TerminateRayKHR
27            | spirv::Op::IgnoreIntersectionKHR
28            | spirv::Op::EmitMeshTasksEXT
29            | spirv::Op::Unreachable
30    )
31}
32
33/// Returns [`true`] if the given opcode is a return instruction or it aborts execution.
34///
35/// <https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#FunctionTermination>
36pub fn is_return_or_abort(opcode: spirv::Op) -> bool {
37    is_return(opcode) || is_abort(opcode)
38}
39
40/// Returns [`true`] if the given opcode is a branch instruction.
41///
42/// <https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Branch> and <https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#ConditionalBranch>
43pub fn is_branch(opcode: spirv::Op) -> bool {
44    matches!(
45        opcode,
46        spirv::Op::Branch | spirv::Op::BranchConditional | spirv::Op::Switch
47    )
48}
49
50/// Returns [`true`] if the given opcode is for a terminator instruction.
51///
52/// <https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Termination>
53pub fn is_block_terminator(opcode: spirv::Op) -> bool {
54    is_branch(opcode) || is_return_or_abort(opcode)
55}