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 // Gets the name of an instruction, without the "Op" prefix.
60 const char* spvOpcodeString(const SpvOp opcode);
61 
62 // Determine if the given opcode is a scalar type. Returns zero if false,
63 // non-zero otherwise.
64 int32_t spvOpcodeIsScalarType(const SpvOp opcode);
65 
66 // Determines if the given opcode is a specialization constant. Returns zero if
67 // false, non-zero otherwise.
68 int32_t spvOpcodeIsSpecConstant(const SpvOp opcode);
69 
70 // Determines if the given opcode is a constant. Returns zero if false, non-zero
71 // otherwise.
72 int32_t spvOpcodeIsConstant(const SpvOp opcode);
73 
74 // Returns true if the given opcode is a constant or undef.
75 bool spvOpcodeIsConstantOrUndef(const SpvOp opcode);
76 
77 // Returns true if the given opcode is a scalar specialization constant.
78 bool spvOpcodeIsScalarSpecConstant(const SpvOp opcode);
79 
80 // Determines if the given opcode is a composite type. Returns zero if false,
81 // non-zero otherwise.
82 int32_t spvOpcodeIsComposite(const SpvOp opcode);
83 
84 // Determines if the given opcode results in a pointer when using the logical
85 // addressing model. Returns zero if false, non-zero otherwise.
86 int32_t spvOpcodeReturnsLogicalPointer(const SpvOp opcode);
87 
88 // Returns whether the given opcode could result in a pointer or a variable
89 // pointer when using the logical addressing model.
90 bool spvOpcodeReturnsLogicalVariablePointer(const SpvOp opcode);
91 
92 // Determines if the given opcode generates a type. Returns zero if false,
93 // non-zero otherwise.
94 int32_t spvOpcodeGeneratesType(SpvOp opcode);
95 
96 // Returns true if the opcode adds a decoration to an id.
97 bool spvOpcodeIsDecoration(const SpvOp opcode);
98 
99 // Returns true if the opcode is a load from memory into a result id.  This
100 // function only considers core instructions.
101 bool spvOpcodeIsLoad(const SpvOp opcode);
102 
103 // Returns true if the opcode is an atomic operation that uses the original
104 // value.
105 bool spvOpcodeIsAtomicWithLoad(const SpvOp opcode);
106 
107 // Returns true if the opcode is an atomic operation.
108 bool spvOpcodeIsAtomicOp(const SpvOp opcode);
109 
110 // Returns true if the given opcode is a branch instruction.
111 bool spvOpcodeIsBranch(SpvOp opcode);
112 
113 // Returns true if the given opcode is a return instruction.
114 bool spvOpcodeIsReturn(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 basic block terminator.
121 bool spvOpcodeIsBlockTerminator(SpvOp opcode);
122 
123 // Returns true if the given opcode always defines an opaque type.
124 bool spvOpcodeIsBaseOpaqueType(SpvOp opcode);
125 
126 // Returns true if the given opcode is a non-uniform group operation.
127 bool spvOpcodeIsNonUniformGroupOperation(SpvOp opcode);
128 
129 // Returns true if the opcode with vector inputs could be divided into a series
130 // of independent scalar operations that would give the same result.
131 bool spvOpcodeIsScalarizable(SpvOp opcode);
132 
133 // Returns true if the given opcode is a debug instruction.
134 bool spvOpcodeIsDebug(SpvOp opcode);
135 
136 // Returns a vector containing the indices of the memory semantics <id>
137 // operands for |opcode|.
138 std::vector<uint32_t> spvOpcodeMemorySemanticsOperandIndices(SpvOp opcode);
139 
140 #endif  // SOURCE_OPCODE_H_
141