Debian Codegraph @

llvm-project/llvm/projects/llvm-spirv/lib/SPIRV/libSPIRV/SPIRVValue.h

raw
1 //===- SPIRVValue.h - Class to represent a SPIR-V Value ---------*- 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 defines the values defined in SPIR-V spec with op codes.
37 ///
38 /// The name of the SPIR-V values follow the op code name in the spec.
39 /// This is for readability and ease of using macro to handle types.
40 //
41 //===----------------------------------------------------------------------===//
42
43 #ifndef SPIRV_LIBSPIRV_SPIRVVALUE_H
44 #define SPIRV_LIBSPIRV_SPIRVVALUE_H
45
46 #include "SPIRVDecorate.h"
47 #include "SPIRVEntry.h"
48 #include "SPIRVType.h"
49
50 namespace llvm {
51 class APInt;
52 } // namespace llvm
53
54 #include <iostream>
55
56 namespace SPIRV {
57
58 class SPIRVValue : public SPIRVEntry {
59 public:
60 // Complete constructor for value with id and type
61 SPIRVValue(SPIRVModule *M, unsigned TheWordCount, Op TheOpCode,
62 SPIRVType *TheType, SPIRVId TheId)
63 : SPIRVEntry(M, TheWordCount, TheOpCode, TheId), Type(TheType) {
64 validate();
65 }
66 // Complete constructor for value with type but without id
67 SPIRVValue(SPIRVModule *M, unsigned TheWordCount, Op TheOpCode,
68 SPIRVType *TheType)
69 : SPIRVEntry(M, TheWordCount, TheOpCode), Type(TheType) {
70 setHasNoId();
71 SPIRVValue::validate();
72 }
73 // Complete constructor for value with id but without type
74 SPIRVValue(SPIRVModule *M, unsigned TheWordCount, Op TheOpCode, SPIRVId TheId)
75 : SPIRVEntry(M, TheWordCount, TheOpCode, TheId), Type(NULL) {
76 setHasNoType();
77 SPIRVValue::validate();
78 }
79 // Complete constructor for value without id and type
80 SPIRVValue(SPIRVModule *M, unsigned TheWordCount, Op TheOpCode)
81 : SPIRVEntry(M, TheWordCount, TheOpCode), Type(NULL) {
82 setHasNoId();
83 setHasNoType();
84 SPIRVValue::validate();
85 }
86 // Incomplete constructor
87 SPIRVValue(Op TheOpCode) : SPIRVEntry(TheOpCode), Type(NULL) {}
88
89 bool hasType() const { return !(Attrib & SPIRVEA_NOTYPE); }
90 SPIRVType *getType() const {
91 assert(hasType() && "value has no type");
92 return Type;
93 }
94 bool isVolatile() const;
95 bool hasAlignment(SPIRVWord *Result = 0) const;
96 bool hasNoSignedWrap() const;
97 bool hasNoUnsignedWrap() const;
98
99 void setAlignment(SPIRVWord);
100 void setVolatile(bool IsVolatile);
101
102 template <spv::Decoration NoIntegerWrapDecoration>
103 void setNoIntegerDecorationWrap(bool HasNoIntegerWrap);
104
105 void setFPFastMathMode(SPIRVWord FPFastMathMode);
106
107 void validate() const override {
108 SPIRVEntry::validate();
109 assert((!hasType() || Type) && "Invalid type");
110 }
111
112 void setType(SPIRVType *Ty) {
113 Type = Ty;
114 assert(!Ty || !Ty->isTypeVoid() || OpCode == OpFunction);
115 if (Ty && (!Ty->isTypeVoid() || OpCode == OpFunction))
116 setHasType();
117 else
118 setHasNoType();
119 }
120
121 SPIRVCapVec getRequiredCapability() const override {
122 SPIRVCapVec CV;
123 if (!hasType())
124 return CV;
125 return Type->getRequiredCapability();
126 }
127
128 std::optional<ExtensionID> getRequiredExtension() const override {
129 std::optional<ExtensionID> EV;
130 if (!hasType())
131 return EV;
132 EV = Type->getRequiredExtension();
133 assert(Module &&
134 (!EV.has_value() || Module->isAllowedToUseExtension(EV.value())));
135 return EV;
136 }
137
138 protected:
139 void setHasNoType() { Attrib |= SPIRVEA_NOTYPE; }
140 void setHasType() { Attrib &= ~SPIRVEA_NOTYPE; }
141
142 SPIRVType *Type; // Value Type
143 };
144
145 template <spv::Op OC> class SPIRVConstantBase : public SPIRVValue {
146 public:
147 // Complete constructor for integer constant
148 SPIRVConstantBase(SPIRVModule *M, SPIRVType *TheType, SPIRVId TheId,
149 uint64_t TheValue)
150 : SPIRVValue(M, 0, OC, TheType, TheId) {
151 setWords(&TheValue);
152 }
153 // Incomplete constructor for AP integer constant
154 SPIRVConstantBase(SPIRVModule *M, SPIRVType *TheType, SPIRVId TheId,
155 const llvm::APInt &TheValue);
156 // Complete constructor for float constant
157 SPIRVConstantBase(SPIRVModule *M, SPIRVType *TheType, SPIRVId TheId,
158 float TheValue)
159 : SPIRVValue(M, 0, OC, TheType, TheId) {
160 setWords(reinterpret_cast<uint64_t *>(&TheValue));
161 }
162 // Complete constructor for double constant
163 SPIRVConstantBase(SPIRVModule *M, SPIRVType *TheType, SPIRVId TheId,
164 double TheValue)
165 : SPIRVValue(M, 0, OC, TheType, TheId) {
166 setWords(reinterpret_cast<uint64_t *>(&TheValue));
167 }
168 // Incomplete constructor
169 SPIRVConstantBase() : SPIRVValue(OC), NumWords(0) {}
170 uint64_t getZExtIntValue() const { return getValue<uint64_t>(); }
171 float getFloatValue() const { return getValue<float>(); }
172 double getDoubleValue() const { return getValue<double>(); }
173 unsigned getNumWords() const { return NumWords; }
174 const std::vector<SPIRVWord> &getSPIRVWords() { return Words; }
175
176 protected:
177 constexpr static SPIRVWord FixedWC = 3;
178
179 // Common method for getting values of size less or equal to 64 bits.
180 template <typename T> T getValue() const {
181 constexpr auto ValueSize = static_cast<unsigned>(sizeof(T));
182 assert((ValueSize <= 8) && "Incorrect result type of requested value");
183 T TheValue{};
184 unsigned CopyBytes = std::min(ValueSize, NumWords * SpirvWordSize);
185 std::memcpy(&TheValue, Words.data(), CopyBytes);
186 return TheValue;
187 }
188
189 void setWords(const uint64_t *TheValue);
190 void recalculateWordCount() {
191 NumWords =
192 (Type->getBitWidth() + SpirvWordBitWidth - 1) / SpirvWordBitWidth;
193 WordCount = FixedWC + NumWords;
194 }
195 void validate() const override {
196 SPIRVValue::validate();
197 assert(NumWords >= 1 && "Invalid constant size");
198 }
199 void encode(spv_ostream &O) const override {
200 getEncoder(O) << Type << Id;
201 for (const auto &Word : Words)
202 getEncoder(O) << Word;
203 }
204 void setWordCount(SPIRVWord WordCount) override {
205 SPIRVValue::setWordCount(WordCount);
206 NumWords = WordCount - FixedWC;
207 }
208 void decode(std::istream &I) override {
209 getDecoder(I) >> Type >> Id;
210 Words.resize(NumWords);
211 for (auto &Word : Words)
212 getDecoder(I) >> Word;
213 }
214
215 unsigned NumWords;
216
217 private:
218 std::vector<SPIRVWord> Words;
219 };
220
221 using SPIRVConstant = SPIRVConstantBase<OpConstant>;
222 using SPIRVSpecConstant = SPIRVConstantBase<OpSpecConstant>;
223
224 template <Op OC> class SPIRVConstantEmpty : public SPIRVValue {
225 public:
226 // Complete constructor
227 SPIRVConstantEmpty(SPIRVModule *M, SPIRVType *TheType, SPIRVId TheId)
228 : SPIRVValue(M, 3, OC, TheType, TheId) {
229 validate();
230 }
231 // Incomplete constructor
232 SPIRVConstantEmpty() : SPIRVValue(OC) {}
233
234 protected:
235 void validate() const override { SPIRVValue::validate(); }
236 _SPIRV_DEF_ENCDEC2(Type, Id)
237 };
238
239 template <Op OC> class SPIRVConstantBool : public SPIRVConstantEmpty<OC> {
240 public:
241 // Complete constructor
242 SPIRVConstantBool(SPIRVModule *M, SPIRVType *TheType, SPIRVId TheId)
243 : SPIRVConstantEmpty<OC>(M, TheType, TheId) {}
244 // Incomplete constructor
245 SPIRVConstantBool() {}
246
247 protected:
248 void validate() const override {
249 SPIRVConstantEmpty<OC>::validate();
250 assert(this->Type->isTypeBool() && "Invalid type");
251 }
252 };
253
254 typedef SPIRVConstantBool<OpConstantTrue> SPIRVConstantTrue;
255 typedef SPIRVConstantBool<OpConstantFalse> SPIRVConstantFalse;
256 typedef SPIRVConstantBool<OpSpecConstantTrue> SPIRVSpecConstantTrue;
257 typedef SPIRVConstantBool<OpSpecConstantFalse> SPIRVSpecConstantFalse;
258
259 class SPIRVConstantNull : public SPIRVConstantEmpty<OpConstantNull> {
260 public:
261 // Complete constructor
262 SPIRVConstantNull(SPIRVModule *M, SPIRVType *TheType, SPIRVId TheId)
263 : SPIRVConstantEmpty(M, TheType, TheId) {
264 validate();
265 }
266 // Incomplete constructor
267 SPIRVConstantNull() {}
268
269 protected:
270 void validate() const override {
271 SPIRVConstantEmpty::validate();
272 assert((Type->isTypeBool() || Type->isTypeInt() || Type->isTypeFloat() ||
273 Type->isTypeComposite() || Type->isTypeOpaque() ||
274 Type->isTypeEvent() || Type->isTypePointer() ||
275 Type->isTypeReserveId() || Type->isTypeDeviceEvent() ||
276 (Type->isTypeSubgroupAvcINTEL() &&
277 !Type->isTypeSubgroupAvcMceINTEL())) &&
278 "Invalid type");
279 }
280 };
281
282 class SPIRVUndef : public SPIRVConstantEmpty<OpUndef> {
283 public:
284 // Complete constructor
285 SPIRVUndef(SPIRVModule *M, SPIRVType *TheType, SPIRVId TheId)
286 : SPIRVConstantEmpty(M, TheType, TheId) {
287 validate();
288 }
289 // Incomplete constructor
290 SPIRVUndef() {}
291
292 protected:
293 void validate() const override { SPIRVConstantEmpty::validate(); }
294 };
295
296 template <spv::Op OC> class SPIRVConstantCompositeBase : public SPIRVValue {
297 public:
298 // There are always 3 words in this instruction except constituents:
299 // 1) WordCount + OpCode
300 // 2) Result type
301 // 3) Result Id
302 constexpr static SPIRVWord FixedWC = 3;
303 using ContinuedInstType = typename InstToContinued<OC>::Type;
304 // Complete constructor for composite constant
305 SPIRVConstantCompositeBase(SPIRVModule *M, SPIRVType *TheType, SPIRVId TheId,
306 const std::vector<SPIRVValue *> TheElements)
307 : SPIRVValue(M, TheElements.size() + FixedWC, OC, TheType, TheId) {
308 Elements = getIds(TheElements);
309 validate();
310 }
311 // Incomplete constructor
312 SPIRVConstantCompositeBase() : SPIRVValue(OC) {}
313 std::vector<SPIRVValue *> getElements() const { return getValues(Elements); }
314
315 // TODO: Should we attach operands of continued instructions as well?
316 std::vector<SPIRVEntry *> getNonLiteralOperands() const override {
317 std::vector<SPIRVValue *> Elements = getElements();
318 return std::vector<SPIRVEntry *>(Elements.begin(), Elements.end());
319 }
320
321 std::vector<ContinuedInstType> getContinuedInstructions() {
322 return ContinuedInstructions;
323 }
324
325 void addContinuedInstruction(ContinuedInstType Inst) {
326 ContinuedInstructions.push_back(Inst);
327 }
328
329 void encodeChildren(spv_ostream &O) const override {
330 O << SPIRVNL();
331 for (auto &I : ContinuedInstructions)
332 O << *I;
333 }
334
335 protected:
336 void validate() const override {
337 SPIRVValue::validate();
338 for (auto &I : Elements)
339 getValue(I)->validate();
340 }
341
342 void setWordCount(SPIRVWord WordCount) override {
343 SPIRVEntry::setWordCount(WordCount);
344 Elements.resize(WordCount - FixedWC);
345 }
346
347 void encode(spv_ostream &O) const override {
348 getEncoder(O) << Type << Id << Elements;
349 }
350
351 void decode(std::istream &I) override {
352 SPIRVDecoder Decoder = getDecoder(I);
353 Decoder >> Type >> Id >> Elements;
354
355 for (SPIRVEntry *E : Decoder.getContinuedInstructions(ContinuedOpCode)) {
356 addContinuedInstruction(static_cast<ContinuedInstType>(E));
357 }
358 }
359
360 std::vector<SPIRVId> Elements;
361 std::vector<ContinuedInstType> ContinuedInstructions;
362 const spv::Op ContinuedOpCode = InstToContinued<OC>::OpCode;
363 };
364
365 using SPIRVConstantComposite = SPIRVConstantCompositeBase<OpConstantComposite>;
366 using SPIRVSpecConstantComposite =
367 SPIRVConstantCompositeBase<OpSpecConstantComposite>;
368
369 class SPIRVConstantSampler : public SPIRVValue {
370 public:
371 const static Op OC = OpConstantSampler;
372 const static SPIRVWord WC = 6;
373 // Complete constructor
374 SPIRVConstantSampler(SPIRVModule *M, SPIRVType *TheType, SPIRVId TheId,
375 SPIRVWord TheAddrMode, SPIRVWord TheNormalized,
376 SPIRVWord TheFilterMode)
377 : SPIRVValue(M, WC, OC, TheType, TheId), AddrMode(TheAddrMode),
378 Normalized(TheNormalized), FilterMode(TheFilterMode) {
379 validate();
380 }
381 // Incomplete constructor
382 SPIRVConstantSampler()
383 : SPIRVValue(OC), AddrMode(SPIRVSAM_Invalid), Normalized(SPIRVWORD_MAX),
384 FilterMode(SPIRVSFM_Invalid) {}
385
386 SPIRVWord getAddrMode() const { return AddrMode; }
387
388 SPIRVWord getFilterMode() const { return FilterMode; }
389
390 SPIRVWord getNormalized() const { return Normalized; }
391 SPIRVCapVec getRequiredCapability() const override {
392 return getVec(CapabilityLiteralSampler);
393 }
394
395 protected:
396 SPIRVWord AddrMode;
397 SPIRVWord Normalized;
398 SPIRVWord FilterMode;
399 void validate() const override {
400 SPIRVValue::validate();
401 assert(OpCode == OC);
402 assert(WordCount == WC);
403 assert(Type->isTypeSampler());
404 }
405 _SPIRV_DEF_ENCDEC5(Type, Id, AddrMode, Normalized, FilterMode)
406 };
407
408 class SPIRVConstantPipeStorage : public SPIRVValue {
409 public:
410 const static Op OC = OpConstantPipeStorage;
411 const static SPIRVWord WC = 6;
412 // Complete constructor
413 SPIRVConstantPipeStorage(SPIRVModule *M, SPIRVType *TheType, SPIRVId TheId,
414 SPIRVWord ThePacketSize, SPIRVWord ThePacketAlign,
415 SPIRVWord TheCapacity)
416 : SPIRVValue(M, WC, OC, TheType, TheId), PacketSize(ThePacketSize),
417 PacketAlign(ThePacketAlign), Capacity(TheCapacity) {
418 validate();
419 }
420 // Incomplete constructor
421 SPIRVConstantPipeStorage()
422 : SPIRVValue(OC), PacketSize(0), PacketAlign(0), Capacity(0) {}
423
424 SPIRVWord getPacketSize() const { return PacketSize; }
425
426 SPIRVWord getPacketAlign() const { return PacketAlign; }
427
428 SPIRVWord getCapacity() const { return Capacity; }
429 SPIRVCapVec getRequiredCapability() const override {
430 return getVec(CapabilityPipes, CapabilityPipeStorage);
431 }
432
433 protected:
434 SPIRVWord PacketSize;
435 SPIRVWord PacketAlign;
436 SPIRVWord Capacity;
437 void validate() const override {
438 SPIRVValue::validate();
439 assert(OpCode == OC);
440 assert(WordCount == WC);
441 assert(Type->isTypePipeStorage());
442 }
443 _SPIRV_DEF_ENCDEC5(Type, Id, PacketSize, PacketAlign, Capacity)
444 };
445
446 class SPIRVForward : public SPIRVValue, public SPIRVComponentExecutionModes {
447 public:
448 const static Op OC = internal::OpForward;
449 // Complete constructor
450 SPIRVForward(SPIRVModule *TheModule, SPIRVType *TheTy, SPIRVId TheId)
451 : SPIRVValue(TheModule, 0, OC, TheId) {
452 if (TheTy)
453 setType(TheTy);
454 }
455 SPIRVForward() : SPIRVValue(OC) { assert(0 && "should never be called"); }
456 _SPIRV_DEF_ENCDEC1(Id)
457 friend class SPIRVFunction;
458
459 protected:
460 void validate() const override {}
461 };
462
463 } // namespace SPIRV
464
465 #endif // SPIRV_LIBSPIRV_SPIRVVALUE_H