1 //===-- SPIRVBaseInfo.h - Top level SPIRV definitions -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains TableGen generated enum definitions, mnemonic lookup
10 // functions, versioning/capabilities/extensions getters for symbolic/named
11 // operands for various SPIR-V instructions.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_LIB_TARGET_SPIRV_SPIRVSYMBOLICOPERANDS_H
16 #define LLVM_LIB_TARGET_SPIRV_SPIRVSYMBOLICOPERANDS_H
17 
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include <string>
21 
22 namespace llvm {
23 namespace SPIRV {
24 namespace OperandCategory {
25 #define GET_OperandCategory_DECL
26 #include "SPIRVGenTables.inc"
27 } // namespace OperandCategory
28 
29 namespace Extension {
30 #define GET_Extension_DECL
31 #include "SPIRVGenTables.inc"
32 } // namespace Extension
33 
34 namespace Capability {
35 #define GET_Capability_DECL
36 #include "SPIRVGenTables.inc"
37 } // namespace Capability
38 
39 namespace SourceLanguage {
40 #define GET_SourceLanguage_DECL
41 #include "SPIRVGenTables.inc"
42 } // namespace SourceLanguage
43 
44 namespace AddressingModel {
45 #define GET_AddressingModel_DECL
46 #include "SPIRVGenTables.inc"
47 } // namespace AddressingModel
48 
49 namespace ExecutionModel {
50 #define GET_ExecutionModel_DECL
51 #include "SPIRVGenTables.inc"
52 } // namespace ExecutionModel
53 
54 namespace MemoryModel {
55 #define GET_MemoryModel_DECL
56 #include "SPIRVGenTables.inc"
57 } // namespace MemoryModel
58 
59 namespace ExecutionMode {
60 #define GET_ExecutionMode_DECL
61 #include "SPIRVGenTables.inc"
62 } // namespace ExecutionMode
63 
64 namespace StorageClass {
65 #define GET_StorageClass_DECL
66 #include "SPIRVGenTables.inc"
67 } // namespace StorageClass
68 
69 namespace Dim {
70 #define GET_Dim_DECL
71 #include "SPIRVGenTables.inc"
72 } // namespace Dim
73 
74 namespace SamplerAddressingMode {
75 #define GET_SamplerAddressingMode_DECL
76 #include "SPIRVGenTables.inc"
77 } // namespace SamplerAddressingMode
78 
79 namespace SamplerFilterMode {
80 #define GET_SamplerFilterMode_DECL
81 #include "SPIRVGenTables.inc"
82 } // namespace SamplerFilterMode
83 
84 namespace ImageFormat {
85 #define GET_ImageFormat_DECL
86 #include "SPIRVGenTables.inc"
87 } // namespace ImageFormat
88 
89 namespace ImageChannelOrder {
90 #define GET_ImageChannelOrder_DECL
91 #include "SPIRVGenTables.inc"
92 } // namespace ImageChannelOrder
93 
94 namespace ImageChannelDataType {
95 #define GET_ImageChannelDataType_DECL
96 #include "SPIRVGenTables.inc"
97 } // namespace ImageChannelDataType
98 
99 namespace ImageOperand {
100 #define GET_ImageOperand_DECL
101 #include "SPIRVGenTables.inc"
102 } // namespace ImageOperand
103 
104 namespace FPFastMathMode {
105 #define GET_FPFastMathMode_DECL
106 #include "SPIRVGenTables.inc"
107 } // namespace FPFastMathMode
108 
109 namespace FPRoundingMode {
110 #define GET_FPRoundingMode_DECL
111 #include "SPIRVGenTables.inc"
112 } // namespace FPRoundingMode
113 
114 namespace LinkageType {
115 #define GET_LinkageType_DECL
116 #include "SPIRVGenTables.inc"
117 } // namespace LinkageType
118 
119 namespace AccessQualifier {
120 #define GET_AccessQualifier_DECL
121 #include "SPIRVGenTables.inc"
122 } // namespace AccessQualifier
123 
124 namespace FunctionParameterAttribute {
125 #define GET_FunctionParameterAttribute_DECL
126 #include "SPIRVGenTables.inc"
127 } // namespace FunctionParameterAttribute
128 
129 namespace Decoration {
130 #define GET_Decoration_DECL
131 #include "SPIRVGenTables.inc"
132 } // namespace Decoration
133 
134 namespace BuiltIn {
135 #define GET_BuiltIn_DECL
136 #include "SPIRVGenTables.inc"
137 } // namespace BuiltIn
138 
139 namespace SelectionControl {
140 #define GET_SelectionControl_DECL
141 #include "SPIRVGenTables.inc"
142 } // namespace SelectionControl
143 
144 namespace LoopControl {
145 #define GET_LoopControl_DECL
146 #include "SPIRVGenTables.inc"
147 } // namespace LoopControl
148 
149 namespace FunctionControl {
150 #define GET_FunctionControl_DECL
151 #include "SPIRVGenTables.inc"
152 } // namespace FunctionControl
153 
154 namespace MemorySemantics {
155 #define GET_MemorySemantics_DECL
156 #include "SPIRVGenTables.inc"
157 } // namespace MemorySemantics
158 
159 namespace MemoryOperand {
160 #define GET_MemoryOperand_DECL
161 #include "SPIRVGenTables.inc"
162 } // namespace MemoryOperand
163 
164 namespace Scope {
165 #define GET_Scope_DECL
166 #include "SPIRVGenTables.inc"
167 } // namespace Scope
168 
169 namespace GroupOperation {
170 #define GET_GroupOperation_DECL
171 #include "SPIRVGenTables.inc"
172 } // namespace GroupOperation
173 
174 namespace KernelEnqueueFlags {
175 #define GET_KernelEnqueueFlags_DECL
176 #include "SPIRVGenTables.inc"
177 } // namespace KernelEnqueueFlags
178 
179 namespace KernelProfilingInfo {
180 #define GET_KernelProfilingInfo_DECL
181 #include "SPIRVGenTables.inc"
182 } // namespace KernelProfilingInfo
183 
184 namespace InstructionSet {
185 #define GET_InstructionSet_DECL
186 #include "SPIRVGenTables.inc"
187 } // namespace InstructionSet
188 
189 namespace OpenCLExtInst {
190 #define GET_OpenCLExtInst_DECL
191 #include "SPIRVGenTables.inc"
192 } // namespace OpenCLExtInst
193 
194 namespace GLSLExtInst {
195 #define GET_GLSLExtInst_DECL
196 #include "SPIRVGenTables.inc"
197 } // namespace GLSLExtInst
198 
199 namespace Opcode {
200 #define GET_Opcode_DECL
201 #include "SPIRVGenTables.inc"
202 } // namespace Opcode
203 
204 struct ExtendedBuiltin {
205   StringRef Name;
206   InstructionSet::InstructionSet Set;
207   uint32_t Number;
208 };
209 } // namespace SPIRV
210 
211 using CapabilityList = SmallVector<SPIRV::Capability::Capability, 8>;
212 using ExtensionList = SmallVector<SPIRV::Extension::Extension, 8>;
213 
214 std::string
215 getSymbolicOperandMnemonic(SPIRV::OperandCategory::OperandCategory Category,
216                            int32_t Value);
217 uint32_t
218 getSymbolicOperandMinVersion(SPIRV::OperandCategory::OperandCategory Category,
219                              uint32_t Value);
220 uint32_t
221 getSymbolicOperandMaxVersion(SPIRV::OperandCategory::OperandCategory Category,
222                              uint32_t Value);
223 CapabilityList
224 getSymbolicOperandCapabilities(SPIRV::OperandCategory::OperandCategory Category,
225                                uint32_t Value);
226 CapabilityList
227 getCapabilitiesEnabledByExtension(SPIRV::Extension::Extension Extension);
228 ExtensionList
229 getSymbolicOperandExtensions(SPIRV::OperandCategory::OperandCategory Category,
230                              uint32_t Value);
231 std::string getLinkStringForBuiltIn(SPIRV::BuiltIn::BuiltIn BuiltInValue);
232 
233 bool getSpirvBuiltInIdByName(StringRef Name, SPIRV::BuiltIn::BuiltIn &BI);
234 
235 std::string getExtInstSetName(SPIRV::InstructionSet::InstructionSet Set);
236 SPIRV::InstructionSet::InstructionSet
237 getExtInstSetFromString(std::string SetName);
238 std::string getExtInstName(SPIRV::InstructionSet::InstructionSet Set,
239                            uint32_t InstructionNumber);
240 
241 // Return a string representation of the operands from startIndex onwards.
242 // Templated to allow both MachineInstr and MCInst to use the same logic.
243 template <class InstType>
getSPIRVStringOperand(const InstType & MI,unsigned StartIndex)244 std::string getSPIRVStringOperand(const InstType &MI, unsigned StartIndex) {
245   std::string s; // Iteratively append to this string.
246 
247   const unsigned NumOps = MI.getNumOperands();
248   bool IsFinished = false;
249   for (unsigned i = StartIndex; i < NumOps && !IsFinished; ++i) {
250     const auto &Op = MI.getOperand(i);
251     if (!Op.isImm()) // Stop if we hit a register operand.
252       break;
253     assert((Op.getImm() >> 32) == 0 && "Imm operand should be i32 word");
254     const uint32_t Imm = Op.getImm(); // Each i32 word is up to 4 characters.
255     for (unsigned ShiftAmount = 0; ShiftAmount < 32; ShiftAmount += 8) {
256       char c = (Imm >> ShiftAmount) & 0xff;
257       if (c == 0) { // Stop if we hit a null-terminator character.
258         IsFinished = true;
259         break;
260       }
261       s += c; // Otherwise, append the character to the result string.
262     }
263   }
264   return s;
265 }
266 } // namespace llvm
267 #endif // LLVM_LIB_TARGET_SPIRV_SPIRVSYMBOLICOPERANDS_H
268