1 // Copyright (c) 2015-2016 The Khronos Group Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef SOURCE_OPCODE_H_ 16 #define SOURCE_OPCODE_H_ 17 18 #include "source/instruction.h" 19 #include "source/latest_version_spirv_header.h" 20 #include "source/table.h" 21 #include "spirv-tools/libspirv.h" 22 23 // Returns the name of a registered SPIR-V generator as a null-terminated 24 // string. If the generator is not known, then returns the string "Unknown". 25 // The generator parameter should be most significant 16-bits of the generator 26 // word in the SPIR-V module header. 27 // 28 // See the registry at https://www.khronos.org/registry/spir-v/api/spir-v.xml. 29 const char* spvGeneratorStr(uint32_t generator); 30 31 // Combines word_count and opcode enumerant in single word. 32 uint32_t spvOpcodeMake(uint16_t word_count, SpvOp opcode); 33 34 // Splits word into into two constituent parts: word_count and opcode. 35 void spvOpcodeSplit(const uint32_t word, uint16_t* word_count, 36 uint16_t* opcode); 37 38 // Finds the named opcode in the given opcode table. On success, returns 39 // SPV_SUCCESS and writes a handle of the table entry into *entry. 40 spv_result_t spvOpcodeTableNameLookup(spv_target_env, 41 const spv_opcode_table table, 42 const char* name, spv_opcode_desc* entry); 43 44 // Finds the opcode by enumerant in the given opcode table. On success, returns 45 // SPV_SUCCESS and writes a handle of the table entry into *entry. 46 spv_result_t spvOpcodeTableValueLookup(spv_target_env, 47 const spv_opcode_table table, 48 const SpvOp opcode, 49 spv_opcode_desc* entry); 50 51 // Copies an instruction's word and fixes the endianness to host native. The 52 // source instruction's stream/opcode/endianness is in the words/opcode/endian 53 // parameter. The word_count parameter specifies the number of words to copy. 54 // Writes copied instruction into *inst. 55 void spvInstructionCopy(const uint32_t* words, const SpvOp opcode, 56 const uint16_t word_count, 57 const spv_endianness_t endian, spv_instruction_t* inst); 58 59 // Determine if the given opcode is a scalar type. Returns zero if false, 60 // non-zero otherwise. 61 int32_t spvOpcodeIsScalarType(const SpvOp opcode); 62 63 // Determines if the given opcode is a specialization constant. Returns zero if 64 // false, non-zero otherwise. 65 int32_t spvOpcodeIsSpecConstant(const SpvOp opcode); 66 67 // Determines if the given opcode is a constant. Returns zero if false, non-zero 68 // otherwise. 69 int32_t spvOpcodeIsConstant(const SpvOp opcode); 70 71 // Returns true if the given opcode is a constant or undef. 72 bool spvOpcodeIsConstantOrUndef(const SpvOp opcode); 73 74 // Returns true if the given opcode is a scalar specialization constant. 75 bool spvOpcodeIsScalarSpecConstant(const SpvOp opcode); 76 77 // Determines if the given opcode is a composite type. Returns zero if false, 78 // non-zero otherwise. 79 int32_t spvOpcodeIsComposite(const SpvOp opcode); 80 81 // Determines if the given opcode results in a pointer when using the logical 82 // addressing model. Returns zero if false, non-zero otherwise. 83 int32_t spvOpcodeReturnsLogicalPointer(const SpvOp opcode); 84 85 // Returns whether the given opcode could result in a pointer or a variable 86 // pointer when using the logical addressing model. 87 bool spvOpcodeReturnsLogicalVariablePointer(const SpvOp opcode); 88 89 // Determines if the given opcode generates a type. Returns zero if false, 90 // non-zero otherwise. 91 int32_t spvOpcodeGeneratesType(SpvOp opcode); 92 93 // Returns true if the opcode adds a decoration to an id. 94 bool spvOpcodeIsDecoration(const SpvOp opcode); 95 96 // Returns true if the opcode is a load from memory into a result id. This 97 // function only considers core instructions. 98 bool spvOpcodeIsLoad(const SpvOp opcode); 99 100 // Returns true if the opcode is an atomic operation that uses the original 101 // value. 102 bool spvOpcodeIsAtomicWithLoad(const SpvOp opcode); 103 104 // Returns true if the opcode is an atomic operation. 105 bool spvOpcodeIsAtomicOp(const SpvOp opcode); 106 107 // Returns true if the given opcode is a branch instruction. 108 bool spvOpcodeIsBranch(SpvOp opcode); 109 110 // Returns true if the given opcode is a return instruction. 111 bool spvOpcodeIsReturn(SpvOp opcode); 112 113 // Returns true if the given opcode aborts execution. 114 bool spvOpcodeIsAbort(SpvOp opcode); 115 116 // Returns true if the given opcode is a return instruction or it aborts 117 // execution. 118 bool spvOpcodeIsReturnOrAbort(SpvOp opcode); 119 120 // Returns true if the given opcode is a kill instruction or it terminates 121 // execution. Note that branches, returns, and unreachables do not terminate 122 // execution. 123 bool spvOpcodeTerminatesExecution(SpvOp opcode); 124 125 // Returns true if the given opcode is a basic block terminator. 126 bool spvOpcodeIsBlockTerminator(SpvOp opcode); 127 128 // Returns true if the given opcode always defines an opaque type. 129 bool spvOpcodeIsBaseOpaqueType(SpvOp opcode); 130 131 // Returns true if the given opcode is a non-uniform group operation. 132 bool spvOpcodeIsNonUniformGroupOperation(SpvOp opcode); 133 134 // Returns true if the opcode with vector inputs could be divided into a series 135 // of independent scalar operations that would give the same result. 136 bool spvOpcodeIsScalarizable(SpvOp opcode); 137 138 // Returns true if the given opcode is a debug instruction. 139 bool spvOpcodeIsDebug(SpvOp opcode); 140 141 // Returns true for opcodes that are binary operators, 142 // where the order of the operands is irrelevant. 143 bool spvOpcodeIsCommutativeBinaryOperator(SpvOp opcode); 144 145 // Returns true for opcodes that represent linear algebra instructions. 146 bool spvOpcodeIsLinearAlgebra(SpvOp opcode); 147 148 // Returns true for opcodes that represent image sample instructions. 149 bool spvOpcodeIsImageSample(SpvOp opcode); 150 151 // Returns a vector containing the indices of the memory semantics <id> 152 // operands for |opcode|. 153 std::vector<uint32_t> spvOpcodeMemorySemanticsOperandIndices(SpvOp opcode); 154 155 // Returns true for opcodes that represent access chain instructions. 156 bool spvOpcodeIsAccessChain(SpvOp opcode); 157 158 // Returns true for opcodes that represent bit instructions. 159 bool spvOpcodeIsBit(SpvOp opcode); 160 161 #endif // SOURCE_OPCODE_H_ 162