Debian Codegraph @

llvm-project/llvm/projects/llvm-spirv/lib/SPIRV/libSPIRV/SPIRVInstruction.cpp

raw
1 //===- SPIRVInstruction.cpp -Class to represent SPIR-V instruction - C++ --===//
2 //
3 // The LLVM/SPIRV Translator
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 // Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal with the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
16 //
17 // Redistributions of source code must retain the above copyright notice,
18 // this list of conditions and the following disclaimers.
19 // Redistributions in binary form must reproduce the above copyright notice,
20 // this list of conditions and the following disclaimers in the documentation
21 // and/or other materials provided with the distribution.
22 // Neither the names of Advanced Micro Devices, Inc., nor the names of its
23 // contributors may be used to endorse or promote products derived from this
24 // Software without specific prior written permission.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
31 // THE SOFTWARE.
32 //
33 //===----------------------------------------------------------------------===//
34 /// \file
35 ///
36 /// This file implements SPIR-V instructions.
37 ///
38 //===----------------------------------------------------------------------===//
39
40 #include "SPIRVInstruction.h"
41 #include "SPIRVBasicBlock.h"
42 #include "SPIRVFunction.h"
43 #include "SPIRVInternal.h"
44
45 #include <unordered_set>
46
47 namespace SPIRV {
48
49 // Complete constructor for instruction with type and id
50 SPIRVInstruction::SPIRVInstruction(unsigned TheWordCount, Op TheOC,
51 SPIRVType *TheType, SPIRVId TheId,
52 SPIRVBasicBlock *TheBB)
53 : SPIRVValue(TheBB->getModule(), TheWordCount, TheOC, TheType, TheId),
54 BB(TheBB), DebugScope(nullptr) {
55 SPIRVInstruction::validate();
56 }
57
58 SPIRVInstruction::SPIRVInstruction(unsigned TheWordCount, Op TheOC,
59 SPIRVType *TheType, SPIRVId TheId,
60 SPIRVBasicBlock *TheBB, SPIRVModule *TheBM)
61 : SPIRVValue(TheBM, TheWordCount, TheOC, TheType, TheId), BB(TheBB),
62 DebugScope(nullptr) {
63 SPIRVInstruction::validate();
64 }
65
66 // Complete constructor for instruction with id but no type
67 SPIRVInstruction::SPIRVInstruction(unsigned TheWordCount, Op TheOC,
68 SPIRVId TheId, SPIRVBasicBlock *TheBB)
69 : SPIRVValue(TheBB->getModule(), TheWordCount, TheOC, TheId), BB(TheBB),
70 DebugScope(nullptr) {
71 SPIRVInstruction::validate();
72 }
73 // Complete constructor for instruction without type and id
74 SPIRVInstruction::SPIRVInstruction(unsigned TheWordCount, Op TheOC,
75 SPIRVBasicBlock *TheBB)
76 : SPIRVValue(TheBB->getModule(), TheWordCount, TheOC), BB(TheBB),
77 DebugScope(nullptr) {
78 SPIRVInstruction::validate();
79 }
80 // Complete constructor for instruction with type but no id
81 SPIRVInstruction::SPIRVInstruction(unsigned TheWordCount, Op TheOC,
82 SPIRVType *TheType, SPIRVBasicBlock *TheBB)
83 : SPIRVValue(TheBB->getModule(), TheWordCount, TheOC, TheType), BB(TheBB),
84 DebugScope(nullptr) {
85 SPIRVInstruction::validate();
86 }
87
88 // Special constructor for debug instruction
89 SPIRVInstruction::SPIRVInstruction(unsigned TheWordCount, Op TheOC,
90 SPIRVType *TheType, SPIRVId TheId,
91 SPIRVModule *TheBM, SPIRVBasicBlock *TheBB)
92 : SPIRVValue(TheBM, TheWordCount, TheOC, TheType, TheId), BB(TheBB),
93 DebugScope(nullptr) {
94 SPIRVInstruction::validate();
95 }
96
97 void SPIRVInstruction::setParent(SPIRVBasicBlock *TheBB) {
98 assert(TheBB && "Invalid BB");
99 if (BB == TheBB)
100 return;
101 assert(BB == NULL && "BB cannot change parent");
102 BB = TheBB;
103 }
104
105 void SPIRVInstruction::setScope(SPIRVEntry *Scope) {
106 assert(Scope && Scope->getOpCode() == OpLabel && "Invalid scope");
107 setParent(static_cast<SPIRVBasicBlock *>(Scope));
108 }
109
110 SPIRVFunctionCall::SPIRVFunctionCall(SPIRVId TheId, SPIRVFunction *TheFunction,
111 const std::vector<SPIRVWord> &TheArgs,
112 SPIRVBasicBlock *BB)
113 : SPIRVFunctionCallGeneric(TheFunction->getFunctionType()->getReturnType(),
114 TheId, TheArgs, BB),
115 FunctionId(TheFunction->getId()) {
116 validate();
117 }
118
119 void SPIRVFunctionCall::validate() const {
120 SPIRVFunctionCallGeneric::validate();
121 }
122
123 SPIRVFunctionPointerCallINTEL::SPIRVFunctionPointerCallINTEL(
124 SPIRVId TheId, SPIRVValue *TheCalledValue, SPIRVType *TheReturnType,
125 const std::vector<SPIRVWord> &TheArgs, SPIRVBasicBlock *BB)
126 : SPIRVFunctionCallGeneric(TheReturnType, TheId, TheArgs, BB),
127 CalledValueId(TheCalledValue->getId()) {
128 validate();
129 }
130
131 void SPIRVFunctionPointerCallINTEL::validate() const {
132 SPIRVFunctionCallGeneric::validate();
133 }
134
135 // ToDo: Each instruction should implement this function
136 std::vector<SPIRVValue *> SPIRVInstruction::getOperands() {
137 std::vector<SPIRVValue *> Empty;
138 assert(0 && "not supported");
139 return Empty;
140 }
141
142 std::vector<SPIRVType *>
143 SPIRVInstruction::getOperandTypes(const std::vector<SPIRVValue *> &Ops) {
144 std::vector<SPIRVType *> Tys;
145 for (auto &I : Ops) {
146 SPIRVType *Ty = nullptr;
147 if (I->getOpCode() == OpFunction)
148 Ty = reinterpret_cast<SPIRVFunction *>(I)->getFunctionType();
149 else if (I->getOpCode() == OpTypeCooperativeMatrixKHR)
150 Ty = reinterpret_cast<SPIRVType *>(I);
151 else
152 Ty = I->getType();
153
154 Tys.push_back(Ty);
155 }
156 return Tys;
157 }
158
159 std::vector<SPIRVType *> SPIRVInstruction::getOperandTypes() {
160 return getOperandTypes(getOperands());
161 }
162
163 void SPIRVImageInstBase::setOpWords(const std::vector<SPIRVWord> &OpsArg) {
164 std::vector<SPIRVWord> Ops = OpsArg;
165
166 // If the Image Operands field has the SignExtend or ZeroExtend bit set,
167 // either raise the minimum SPIR-V version to 1.4, or drop the operand
168 // if SPIR-V 1.4 cannot be emitted.
169 size_t ImgOpsIndex = getImageOperandsIndex(OpCode);
170 if (ImgOpsIndex != ~0U && ImgOpsIndex < Ops.size()) {
171 SPIRVWord ImgOps = Ops[ImgOpsIndex];
172 unsigned SignZeroExtMasks = ImageOperandsMask::ImageOperandsSignExtendMask |
173 ImageOperandsMask::ImageOperandsZeroExtendMask;
174 if (ImgOps & SignZeroExtMasks) {
175 SPIRVModule *M = getModule();
176 if (M->isAllowedToUseVersion(VersionNumber::SPIRV_1_4)) {
177 M->setMinSPIRVVersion(VersionNumber::SPIRV_1_4);
178 } else {
179 // Drop SignExtend/ZeroExtend if we cannot use SPIR-V 1.4.
180 Ops[ImgOpsIndex] &= ~SignZeroExtMasks;
181 if (Ops[ImgOpsIndex] == 0) {
182 // Drop the Image Operands if SignExtend/ZeroExtend was the only
183 // bit set.
184 Ops.pop_back();
185 }
186 }
187 }
188 }
189 SPIRVInstTemplateBase::setOpWords(Ops);
190 }
191
192 bool isSpecConstantOpAllowedOp(Op OC) {
193 static SPIRVWord Table[] = {
194 OpSConvert,
195 OpFConvert,
196 OpConvertFToS,
197 OpConvertSToF,
198 OpConvertFToU,
199 OpConvertUToF,
200 OpUConvert,
201 OpConvertPtrToU,
202 OpConvertUToPtr,
203 OpGenericCastToPtr,
204 OpPtrCastToGeneric,
205 OpCrossWorkgroupCastToPtrINTEL,
206 OpPtrCastToCrossWorkgroupINTEL,
207 OpBitcast,
208 OpQuantizeToF16,
209 OpSNegate,
210 OpNot,
211 OpIAdd,
212 OpISub,
213 OpIMul,
214 OpUDiv,
215 OpSDiv,
216 OpUMod,
217 OpSRem,
218 OpSMod,
219 OpShiftRightLogical,
220 OpShiftRightArithmetic,
221 OpShiftLeftLogical,
222 OpBitwiseOr,
223 OpBitwiseXor,
224 OpBitwiseAnd,
225 OpFNegate,
226 OpFAdd,
227 OpFSub,
228 OpFMul,
229 OpFDiv,
230 OpFRem,
231 OpFMod,
232 OpVectorShuffle,
233 OpCompositeExtract,
234 OpCompositeInsert,
235 OpLogicalOr,
236 OpLogicalAnd,
237 OpLogicalNot,
238 OpLogicalEqual,
239 OpLogicalNotEqual,
240 OpSelect,
241 OpIEqual,
242 OpINotEqual,
243 OpULessThan,
244 OpSLessThan,
245 OpUGreaterThan,
246 OpSGreaterThan,
247 OpULessThanEqual,
248 OpSLessThanEqual,
249 OpUGreaterThanEqual,
250 OpSGreaterThanEqual,
251 OpAccessChain,
252 OpInBoundsAccessChain,
253 OpPtrAccessChain,
254 OpInBoundsPtrAccessChain,
255 };
256 static std::unordered_set<SPIRVWord> Allow(std::begin(Table),
257 std::end(Table));
258 return Allow.count(OC);
259 }
260
261 SPIRVSpecConstantOp *createSpecConstantOpInst(SPIRVInstruction *Inst) {
262 auto OC = Inst->getOpCode();
263 assert(isSpecConstantOpAllowedOp(OC) &&
264 "Op code not allowed for OpSpecConstantOp");
265 std::vector<SPIRVWord> Ops;
266
267 // CompositeExtract/Insert operations use zero-based numbering for their
268 // indexes (containted in instruction operands). All their operands are
269 // Literals, so we can pass them as is for further handling.
270 if (OC == OpCompositeExtract || OC == OpCompositeInsert) {
271 auto *SPIRVInst = static_cast<SPIRVInstTemplateBase *>(Inst);
272 Ops = SPIRVInst->getOpWords();
273 } else {
274 Ops = Inst->getIds(Inst->getOperands());
275 }
276
277 Ops.insert(Ops.begin(), OC);
278 return static_cast<SPIRVSpecConstantOp *>(SPIRVSpecConstantOp::create(
279 OpSpecConstantOp, Inst->getType(), Inst->getId(), Ops, nullptr,
280 Inst->getModule()));
281 }
282
283 SPIRVInstruction *createInstFromSpecConstantOp(SPIRVSpecConstantOp *Inst) {
284 assert(Inst->getOpCode() == OpSpecConstantOp && "Not OpSpecConstantOp");
285 auto Ops = Inst->getOpWords();
286 auto OC = static_cast<Op>(Ops[0]);
287 assert(isSpecConstantOpAllowedOp(OC) &&
288 "Op code not allowed for OpSpecConstantOp");
289 auto *Const = Inst->getOperand(1);
290 // LLVM would eliminate a bitcast from a function pointer in a constexpr
291 // context. Cut this short here to avoid necessity to align address spaces
292 if (OC == OpBitcast && Const->getOpCode() == OpConstantFunctionPointerINTEL)
293 return static_cast<SPIRVInstruction *>(Const);
294 Ops.erase(Ops.begin(), Ops.begin() + 1);
295 auto *BM = Inst->getModule();
296 auto *RetInst = SPIRVInstTemplateBase::create(
297 OC, Inst->getType(), Inst->getId(), Ops, nullptr, BM);
298 // Instruction that creates from OpSpecConstantOp has the same Id
299 BM->insertEntryNoId(RetInst);
300 return RetInst;
301 }
302
303 } // namespace SPIRV