Debian Codegraph @

third_party/glslang/SPIRV/SpvBuilder.h

raw
1 //
2 // Copyright (C) 2014-2015 LunarG, Inc.
3 // Copyright (C) 2015-2020 Google, Inc.
4 // Copyright (C) 2017 ARM Limited.
5 // Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
6 //
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions
11 // are met:
12 //
13 // Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // Redistributions in binary form must reproduce the above
17 // copyright notice, this list of conditions and the following
18 // disclaimer in the documentation and/or other materials provided
19 // with the distribution.
20 //
21 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
22 // contributors may be used to endorse or promote products derived
23 // from this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 // POSSIBILITY OF SUCH DAMAGE.
37
38 //
39 // "Builder" is an interface to fully build SPIR-V IR. Allocate one of
40 // these to build (a thread safe) internal SPIR-V representation (IR),
41 // and then dump it as a binary stream according to the SPIR-V specification.
42 //
43 // A Builder has a 1:1 relationship with a SPIR-V module.
44 //
45
46 #pragma once
47 #ifndef SpvBuilder_H
48 #define SpvBuilder_H
49
50 #include "Logger.h"
51 #include "spirv.hpp"
52 #include "spvIR.h"
53
54 #include <algorithm>
55 #include <map>
56 #include <memory>
57 #include <set>
58 #include <sstream>
59 #include <stack>
60 #include <unordered_map>
61 #include <map>
62
63 namespace spv {
64
65 typedef enum {
66 Spv_1_0 = (1 << 16),
67 Spv_1_1 = (1 << 16) | (1 << 8),
68 Spv_1_2 = (1 << 16) | (2 << 8),
69 Spv_1_3 = (1 << 16) | (3 << 8),
70 Spv_1_4 = (1 << 16) | (4 << 8),
71 Spv_1_5 = (1 << 16) | (5 << 8),
72 } SpvVersion;
73
74 class Builder {
75 public:
76 Builder(unsigned int spvVersion, unsigned int userNumber, SpvBuildLogger* logger);
77 virtual ~Builder();
78
79 static const int maxMatrixSize = 4;
80
81 unsigned int getSpvVersion() const { return spvVersion; }
82
83 void setSource(spv::SourceLanguage lang, int version)
84 {
85 source = lang;
86 sourceVersion = version;
87 }
88 spv::Id getStringId(const std::string& str)
89 {
90 auto sItr = stringIds.find(str);
91 if (sItr != stringIds.end())
92 return sItr->second;
93 spv::Id strId = getUniqueId();
94 Instruction* fileString = new Instruction(strId, NoType, OpString);
95 const char* file_c_str = str.c_str();
96 fileString->addStringOperand(file_c_str);
97 strings.push_back(std::unique_ptr<Instruction>(fileString));
98 module.mapInstruction(fileString);
99 stringIds[file_c_str] = strId;
100 return strId;
101 }
102 void setSourceFile(const std::string& file)
103 {
104 sourceFileStringId = getStringId(file);
105 }
106 void setSourceText(const std::string& text) { sourceText = text; }
107 void addSourceExtension(const char* ext) { sourceExtensions.push_back(ext); }
108 void addModuleProcessed(const std::string& p) { moduleProcesses.push_back(p.c_str()); }
109 void setEmitOpLines() { emitOpLines = true; }
110 void addExtension(const char* ext) { extensions.insert(ext); }
111 void removeExtension(const char* ext)
112 {
113 extensions.erase(ext);
114 }
115 void addIncorporatedExtension(const char* ext, SpvVersion incorporatedVersion)
116 {
117 if (getSpvVersion() < static_cast<unsigned>(incorporatedVersion))
118 addExtension(ext);
119 }
120 void promoteIncorporatedExtension(const char* baseExt, const char* promoExt, SpvVersion incorporatedVersion)
121 {
122 removeExtension(baseExt);
123 addIncorporatedExtension(promoExt, incorporatedVersion);
124 }
125 void addInclude(const std::string& name, const std::string& text)
126 {
127 spv::Id incId = getStringId(name);
128 includeFiles[incId] = &text;
129 }
130 Id import(const char*);
131 void setMemoryModel(spv::AddressingModel addr, spv::MemoryModel mem)
132 {
133 addressModel = addr;
134 memoryModel = mem;
135 }
136
137 void addCapability(spv::Capability cap) { capabilities.insert(cap); }
138
139 // To get a new <id> for anything needing a new one.
140 Id getUniqueId() { return ++uniqueId; }
141
142 // To get a set of new <id>s, e.g., for a set of function parameters
143 Id getUniqueIds(int numIds)
144 {
145 Id id = uniqueId + 1;
146 uniqueId += numIds;
147 return id;
148 }
149
150 // Generate OpLine for non-filename-based #line directives (ie no filename
151 // seen yet): Log the current line, and if different than the last one,
152 // issue a new OpLine using the new line and current source file name.
153 void setLine(int line);
154
155 // If filename null, generate OpLine for non-filename-based line directives,
156 // else do filename-based: Log the current line and file, and if different
157 // than the last one, issue a new OpLine using the new line and file
158 // name.
159 void setLine(int line, const char* filename);
160 // Low-level OpLine. See setLine() for a layered helper.
161 void addLine(Id fileName, int line, int column);
162
163 // For creating new types (will return old type if the requested one was already made).
164 Id makeVoidType();
165 Id makeBoolType();
166 Id makePointer(StorageClass, Id pointee);
167 Id makeForwardPointer(StorageClass);
168 Id makePointerFromForwardPointer(StorageClass, Id forwardPointerType, Id pointee);
169 Id makeIntegerType(int width, bool hasSign); // generic
170 Id makeIntType(int width) { return makeIntegerType(width, true); }
171 Id makeUintType(int width) { return makeIntegerType(width, false); }
172 Id makeFloatType(int width);
173 Id makeStructType(const std::vector<Id>& members, const char*);
174 Id makeStructResultType(Id type0, Id type1);
175 Id makeVectorType(Id component, int size);
176 Id makeMatrixType(Id component, int cols, int rows);
177 Id makeArrayType(Id element, Id sizeId, int stride); // 0 stride means no stride decoration
178 Id makeRuntimeArray(Id element);
179 Id makeFunctionType(Id returnType, const std::vector<Id>& paramTypes);
180 Id makeImageType(Id sampledType, Dim, bool depth, bool arrayed, bool ms, unsigned sampled, ImageFormat format);
181 Id makeSamplerType();
182 Id makeSampledImageType(Id imageType);
183 Id makeCooperativeMatrixType(Id component, Id scope, Id rows, Id cols);
184
185 // accelerationStructureNV type
186 Id makeAccelerationStructureType();
187 // rayQueryEXT type
188 Id makeRayQueryType();
189
190 // For querying about types.
191 Id getTypeId(Id resultId) const { return module.getTypeId(resultId); }
192 Id getDerefTypeId(Id resultId) const;
193 Op getOpCode(Id id) const { return module.getInstruction(id)->getOpCode(); }
194 Op getTypeClass(Id typeId) const { return getOpCode(typeId); }
195 Op getMostBasicTypeClass(Id typeId) const;
196 int getNumComponents(Id resultId) const { return getNumTypeComponents(getTypeId(resultId)); }
197 int getNumTypeConstituents(Id typeId) const;
198 int getNumTypeComponents(Id typeId) const { return getNumTypeConstituents(typeId); }
199 Id getScalarTypeId(Id typeId) const;
200 Id getContainedTypeId(Id typeId) const;
201 Id getContainedTypeId(Id typeId, int) const;
202 StorageClass getTypeStorageClass(Id typeId) const { return module.getStorageClass(typeId); }
203 ImageFormat getImageTypeFormat(Id typeId) const
204 { return (ImageFormat)module.getInstruction(typeId)->getImmediateOperand(6); }
205
206 bool isPointer(Id resultId) const { return isPointerType(getTypeId(resultId)); }
207 bool isScalar(Id resultId) const { return isScalarType(getTypeId(resultId)); }
208 bool isVector(Id resultId) const { return isVectorType(getTypeId(resultId)); }
209 bool isMatrix(Id resultId) const { return isMatrixType(getTypeId(resultId)); }
210 bool isCooperativeMatrix(Id resultId)const { return isCooperativeMatrixType(getTypeId(resultId)); }
211 bool isAggregate(Id resultId) const { return isAggregateType(getTypeId(resultId)); }
212 bool isSampledImage(Id resultId) const { return isSampledImageType(getTypeId(resultId)); }
213
214 bool isBoolType(Id typeId)
215 { return groupedTypes[OpTypeBool].size() > 0 && typeId == groupedTypes[OpTypeBool].back()->getResultId(); }
216 bool isIntType(Id typeId) const
217 { return getTypeClass(typeId) == OpTypeInt && module.getInstruction(typeId)->getImmediateOperand(1) != 0; }
218 bool isUintType(Id typeId) const
219 { return getTypeClass(typeId) == OpTypeInt && module.getInstruction(typeId)->getImmediateOperand(1) == 0; }
220 bool isFloatType(Id typeId) const { return getTypeClass(typeId) == OpTypeFloat; }
221 bool isPointerType(Id typeId) const { return getTypeClass(typeId) == OpTypePointer; }
222 bool isScalarType(Id typeId) const
223 { return getTypeClass(typeId) == OpTypeFloat || getTypeClass(typeId) == OpTypeInt ||
224 getTypeClass(typeId) == OpTypeBool; }
225 bool isVectorType(Id typeId) const { return getTypeClass(typeId) == OpTypeVector; }
226 bool isMatrixType(Id typeId) const { return getTypeClass(typeId) == OpTypeMatrix; }
227 bool isStructType(Id typeId) const { return getTypeClass(typeId) == OpTypeStruct; }
228 bool isArrayType(Id typeId) const { return getTypeClass(typeId) == OpTypeArray; }
229 #ifdef GLSLANG_WEB
230 bool isCooperativeMatrixType(Id typeId)const { return false; }
231 #else
232 bool isCooperativeMatrixType(Id typeId)const { return getTypeClass(typeId) == OpTypeCooperativeMatrixNV; }
233 #endif
234 bool isAggregateType(Id typeId) const
235 { return isArrayType(typeId) || isStructType(typeId) || isCooperativeMatrixType(typeId); }
236 bool isImageType(Id typeId) const { return getTypeClass(typeId) == OpTypeImage; }
237 bool isSamplerType(Id typeId) const { return getTypeClass(typeId) == OpTypeSampler; }
238 bool isSampledImageType(Id typeId) const { return getTypeClass(typeId) == OpTypeSampledImage; }
239 bool containsType(Id typeId, Op typeOp, unsigned int width) const;
240 bool containsPhysicalStorageBufferOrArray(Id typeId) const;
241
242 bool isConstantOpCode(Op opcode) const;
243 bool isSpecConstantOpCode(Op opcode) const;
244 bool isConstant(Id resultId) const { return isConstantOpCode(getOpCode(resultId)); }
245 bool isConstantScalar(Id resultId) const { return getOpCode(resultId) == OpConstant; }
246 bool isSpecConstant(Id resultId) const { return isSpecConstantOpCode(getOpCode(resultId)); }
247 unsigned int getConstantScalar(Id resultId) const
248 { return module.getInstruction(resultId)->getImmediateOperand(0); }
249 StorageClass getStorageClass(Id resultId) const { return getTypeStorageClass(getTypeId(resultId)); }
250
251 bool isVariableOpCode(Op opcode) const { return opcode == OpVariable; }
252 bool isVariable(Id resultId) const { return isVariableOpCode(getOpCode(resultId)); }
253 bool isGlobalStorage(Id resultId) const { return getStorageClass(resultId) != StorageClassFunction; }
254 bool isGlobalVariable(Id resultId) const { return isVariable(resultId) && isGlobalStorage(resultId); }
255 // See if a resultId is valid for use as an initializer.
256 bool isValidInitializer(Id resultId) const { return isConstant(resultId) || isGlobalVariable(resultId); }
257
258 int getScalarTypeWidth(Id typeId) const
259 {
260 Id scalarTypeId = getScalarTypeId(typeId);
261 assert(getTypeClass(scalarTypeId) == OpTypeInt || getTypeClass(scalarTypeId) == OpTypeFloat);
262 return module.getInstruction(scalarTypeId)->getImmediateOperand(0);
263 }
264
265 int getTypeNumColumns(Id typeId) const
266 {
267 assert(isMatrixType(typeId));
268 return getNumTypeConstituents(typeId);
269 }
270 int getNumColumns(Id resultId) const { return getTypeNumColumns(getTypeId(resultId)); }
271 int getTypeNumRows(Id typeId) const
272 {
273 assert(isMatrixType(typeId));
274 return getNumTypeComponents(getContainedTypeId(typeId));
275 }
276 int getNumRows(Id resultId) const { return getTypeNumRows(getTypeId(resultId)); }
277
278 Dim getTypeDimensionality(Id typeId) const
279 {
280 assert(isImageType(typeId));
281 return (Dim)module.getInstruction(typeId)->getImmediateOperand(1);
282 }
283 Id getImageType(Id resultId) const
284 {
285 Id typeId = getTypeId(resultId);
286 assert(isImageType(typeId) || isSampledImageType(typeId));
287 return isSampledImageType(typeId) ? module.getInstruction(typeId)->getIdOperand(0) : typeId;
288 }
289 bool isArrayedImageType(Id typeId) const
290 {
291 assert(isImageType(typeId));
292 return module.getInstruction(typeId)->getImmediateOperand(3) != 0;
293 }
294
295 // For making new constants (will return old constant if the requested one was already made).
296 Id makeBoolConstant(bool b, bool specConstant = false);
297 Id makeInt8Constant(int i, bool specConstant = false)
298 { return makeIntConstant(makeIntType(8), (unsigned)i, specConstant); }
299 Id makeUint8Constant(unsigned u, bool specConstant = false)
300 { return makeIntConstant(makeUintType(8), u, specConstant); }
301 Id makeInt16Constant(int i, bool specConstant = false)
302 { return makeIntConstant(makeIntType(16), (unsigned)i, specConstant); }
303 Id makeUint16Constant(unsigned u, bool specConstant = false)
304 { return makeIntConstant(makeUintType(16), u, specConstant); }
305 Id makeIntConstant(int i, bool specConstant = false)
306 { return makeIntConstant(makeIntType(32), (unsigned)i, specConstant); }
307 Id makeUintConstant(unsigned u, bool specConstant = false)
308 { return makeIntConstant(makeUintType(32), u, specConstant); }
309 Id makeInt64Constant(long long i, bool specConstant = false)
310 { return makeInt64Constant(makeIntType(64), (unsigned long long)i, specConstant); }
311 Id makeUint64Constant(unsigned long long u, bool specConstant = false)
312 { return makeInt64Constant(makeUintType(64), u, specConstant); }
313 Id makeFloatConstant(float f, bool specConstant = false);
314 Id makeDoubleConstant(double d, bool specConstant = false);
315 Id makeFloat16Constant(float f16, bool specConstant = false);
316 Id makeFpConstant(Id type, double d, bool specConstant = false);
317
318 // Turn the array of constants into a proper spv constant of the requested type.
319 Id makeCompositeConstant(Id type, const std::vector<Id>& comps, bool specConst = false);
320
321 // Methods for adding information outside the CFG.
322 Instruction* addEntryPoint(ExecutionModel, Function*, const char* name);
323 void addExecutionMode(Function*, ExecutionMode mode, int value1 = -1, int value2 = -1, int value3 = -1);
324 void addExecutionMode(Function*, ExecutionMode mode, const std::vector<unsigned>& literals);
325 void addExecutionModeId(Function*, ExecutionMode mode, const std::vector<Id>& operandIds);
326 void addName(Id, const char* name);
327 void addMemberName(Id, int member, const char* name);
328 void addDecoration(Id, Decoration, int num = -1);
329 void addDecoration(Id, Decoration, const char*);
330 void addDecoration(Id, Decoration, const std::vector<unsigned>& literals);
331 void addDecoration(Id, Decoration, const std::vector<const char*>& strings);
332 void addDecorationId(Id id, Decoration, Id idDecoration);
333 void addDecorationId(Id id, Decoration, const std::vector<Id>& operandIds);
334 void addMemberDecoration(Id, unsigned int member, Decoration, int num = -1);
335 void addMemberDecoration(Id, unsigned int member, Decoration, const char*);
336 void addMemberDecoration(Id, unsigned int member, Decoration, const std::vector<unsigned>& literals);
337 void addMemberDecoration(Id, unsigned int member, Decoration, const std::vector<const char*>& strings);
338
339 // At the end of what block do the next create*() instructions go?
340 void setBuildPoint(Block* bp) { buildPoint = bp; }
341 Block* getBuildPoint() const { return buildPoint; }
342
343 // Make the entry-point function. The returned pointer is only valid
344 // for the lifetime of this builder.
345 Function* makeEntryPoint(const char*);
346
347 // Make a shader-style function, and create its entry block if entry is non-zero.
348 // Return the function, pass back the entry.
349 // The returned pointer is only valid for the lifetime of this builder.
350 Function* makeFunctionEntry(Decoration precision, Id returnType, const char* name,
351 const std::vector<Id>& paramTypes, const std::vector<std::vector<Decoration>>& precisions, Block **entry = 0);
352
353 // Create a return. An 'implicit' return is one not appearing in the source
354 // code. In the case of an implicit return, no post-return block is inserted.
355 void makeReturn(bool implicit, Id retVal = 0);
356
357 // Generate all the code needed to finish up a function.
358 void leaveFunction();
359
360 // Create a discard or terminate-invocation.
361 void makeDiscard();
362 void makeTerminateInvocation();
363
364 // Create a global or function local or IO variable.
365 Id createVariable(Decoration precision, StorageClass, Id type, const char* name = nullptr,
366 Id initializer = NoResult);
367
368 // Create an intermediate with an undefined value.
369 Id createUndefined(Id type);
370
371 // Store into an Id and return the l-value
372 void createStore(Id rValue, Id lValue, spv::MemoryAccessMask memoryAccess = spv::MemoryAccessMaskNone,
373 spv::Scope scope = spv::ScopeMax, unsigned int alignment = 0);
374
375 // Load from an Id and return it
376 Id createLoad(Id lValue, spv::Decoration precision,
377 spv::MemoryAccessMask memoryAccess = spv::MemoryAccessMaskNone,
378 spv::Scope scope = spv::ScopeMax, unsigned int alignment = 0);
379
380 // Create an OpAccessChain instruction
381 Id createAccessChain(StorageClass, Id base, const std::vector<Id>& offsets);
382
383 // Create an OpArrayLength instruction
384 Id createArrayLength(Id base, unsigned int member);
385
386 // Create an OpCooperativeMatrixLengthNV instruction
387 Id createCooperativeMatrixLength(Id type);
388
389 // Create an OpCompositeExtract instruction
390 Id createCompositeExtract(Id composite, Id typeId, unsigned index);
391 Id createCompositeExtract(Id composite, Id typeId, const std::vector<unsigned>& indexes);
392 Id createCompositeInsert(Id object, Id composite, Id typeId, unsigned index);
393 Id createCompositeInsert(Id object, Id composite, Id typeId, const std::vector<unsigned>& indexes);
394
395 Id createVectorExtractDynamic(Id vector, Id typeId, Id componentIndex);
396 Id createVectorInsertDynamic(Id vector, Id typeId, Id component, Id componentIndex);
397
398 void createNoResultOp(Op);
399 void createNoResultOp(Op, Id operand);
400 void createNoResultOp(Op, const std::vector<Id>& operands);
401 void createNoResultOp(Op, const std::vector<IdImmediate>& operands);
402 void createControlBarrier(Scope execution, Scope memory, MemorySemanticsMask);
403 void createMemoryBarrier(unsigned executionScope, unsigned memorySemantics);
404 Id createUnaryOp(Op, Id typeId, Id operand);
405 Id createBinOp(Op, Id typeId, Id operand1, Id operand2);
406 Id createTriOp(Op, Id typeId, Id operand1, Id operand2, Id operand3);
407 Id createOp(Op, Id typeId, const std::vector<Id>& operands);
408 Id createOp(Op, Id typeId, const std::vector<IdImmediate>& operands);
409 Id createFunctionCall(spv::Function*, const std::vector<spv::Id>&);
410 Id createSpecConstantOp(Op, Id typeId, const std::vector<spv::Id>& operands, const std::vector<unsigned>& literals);
411
412 // Take an rvalue (source) and a set of channels to extract from it to
413 // make a new rvalue, which is returned.
414 Id createRvalueSwizzle(Decoration precision, Id typeId, Id source, const std::vector<unsigned>& channels);
415
416 // Take a copy of an lvalue (target) and a source of components, and set the
417 // source components into the lvalue where the 'channels' say to put them.
418 // An updated version of the target is returned.
419 // (No true lvalue or stores are used.)
420 Id createLvalueSwizzle(Id typeId, Id target, Id source, const std::vector<unsigned>& channels);
421
422 // If both the id and precision are valid, the id
423 // gets tagged with the requested precision.
424 // The passed in id is always the returned id, to simplify use patterns.
425 Id setPrecision(Id id, Decoration precision)
426 {
427 if (precision != NoPrecision && id != NoResult)
428 addDecoration(id, precision);
429
430 return id;
431 }
432
433 // Can smear a scalar to a vector for the following forms:
434 // - promoteScalar(scalar, vector) // smear scalar to width of vector
435 // - promoteScalar(vector, scalar) // smear scalar to width of vector
436 // - promoteScalar(pointer, scalar) // smear scalar to width of what pointer points to
437 // - promoteScalar(scalar, scalar) // do nothing
438 // Other forms are not allowed.
439 //
440 // Generally, the type of 'scalar' does not need to be the same type as the components in 'vector'.
441 // The type of the created vector is a vector of components of the same type as the scalar.
442 //
443 // Note: One of the arguments will change, with the result coming back that way rather than
444 // through the return value.
445 void promoteScalar(Decoration precision, Id& left, Id& right);
446
447 // Make a value by smearing the scalar to fill the type.
448 // vectorType should be the correct type for making a vector of scalarVal.
449 // (No conversions are done.)
450 Id smearScalar(Decoration precision, Id scalarVal, Id vectorType);
451
452 // Create a call to a built-in function.
453 Id createBuiltinCall(Id resultType, Id builtins, int entryPoint, const std::vector<Id>& args);
454
455 // List of parameters used to create a texture operation
456 struct TextureParameters {
457 Id sampler;
458 Id coords;
459 Id bias;
460 Id lod;
461 Id Dref;
462 Id offset;
463 Id offsets;
464 Id gradX;
465 Id gradY;
466 Id sample;
467 Id component;
468 Id texelOut;
469 Id lodClamp;
470 Id granularity;
471 Id coarse;
472 bool nonprivate;
473 bool volatil;
474 };
475
476 // Select the correct texture operation based on all inputs, and emit the correct instruction
477 Id createTextureCall(Decoration precision, Id resultType, bool sparse, bool fetch, bool proj, bool gather,
478 bool noImplicit, const TextureParameters&, ImageOperandsMask);
479
480 // Emit the OpTextureQuery* instruction that was passed in.
481 // Figure out the right return value and type, and return it.
482 Id createTextureQueryCall(Op, const TextureParameters&, bool isUnsignedResult);
483
484 Id createSamplePositionCall(Decoration precision, Id, Id);
485
486 Id createBitFieldExtractCall(Decoration precision, Id, Id, Id, bool isSigned);
487 Id createBitFieldInsertCall(Decoration precision, Id, Id, Id, Id);
488
489 // Reduction comparison for composites: For equal and not-equal resulting in a scalar.
490 Id createCompositeCompare(Decoration precision, Id, Id, bool /* true if for equal, false if for not-equal */);
491
492 // OpCompositeConstruct
493 Id createCompositeConstruct(Id typeId, const std::vector<Id>& constituents);
494
495 // vector or scalar constructor
496 Id createConstructor(Decoration precision, const std::vector<Id>& sources, Id resultTypeId);
497
498 // matrix constructor
499 Id createMatrixConstructor(Decoration precision, const std::vector<Id>& sources, Id constructee);
500
501 // Helper to use for building nested control flow with if-then-else.
502 class If {
503 public:
504 If(Id condition, unsigned int ctrl, Builder& builder);
505 ~If() {}
506
507 void makeBeginElse();
508 void makeEndIf();
509
510 private:
511 If(const If&);
512 If& operator=(If&);
513
514 Builder& builder;
515 Id condition;
516 unsigned int control;
517 Function* function;
518 Block* headerBlock;
519 Block* thenBlock;
520 Block* elseBlock;
521 Block* mergeBlock;
522 };
523
524 // Make a switch statement. A switch has 'numSegments' of pieces of code, not containing
525 // any case/default labels, all separated by one or more case/default labels. Each possible
526 // case value v is a jump to the caseValues[v] segment. The defaultSegment is also in this
527 // number space. How to compute the value is given by 'condition', as in switch(condition).
528 //
529 // The SPIR-V Builder will maintain the stack of post-switch merge blocks for nested switches.
530 //
531 // Use a defaultSegment < 0 if there is no default segment (to branch to post switch).
532 //
533 // Returns the right set of basic blocks to start each code segment with, so that the caller's
534 // recursion stack can hold the memory for it.
535 //
536 void makeSwitch(Id condition, unsigned int control, int numSegments, const std::vector<int>& caseValues,
537 const std::vector<int>& valueToSegment, int defaultSegment, std::vector<Block*>& segmentBB);
538
539 // Add a branch to the innermost switch's merge block.
540 void addSwitchBreak();
541
542 // Move to the next code segment, passing in the return argument in makeSwitch()
543 void nextSwitchSegment(std::vector<Block*>& segmentBB, int segment);
544
545 // Finish off the innermost switch.
546 void endSwitch(std::vector<Block*>& segmentBB);
547
548 struct LoopBlocks {
549 LoopBlocks(Block& head, Block& body, Block& merge, Block& continue_target) :
550 head(head), body(body), merge(merge), continue_target(continue_target) { }
551 Block &head, &body, &merge, &continue_target;
552 private:
553 LoopBlocks();
554 LoopBlocks& operator=(const LoopBlocks&) = delete;
555 };
556
557 // Start a new loop and prepare the builder to generate code for it. Until
558 // closeLoop() is called for this loop, createLoopContinue() and
559 // createLoopExit() will target its corresponding blocks.
560 LoopBlocks& makeNewLoop();
561
562 // Create a new block in the function containing the build point. Memory is
563 // owned by the function object.
564 Block& makeNewBlock();
565
566 // Add a branch to the continue_target of the current (innermost) loop.
567 void createLoopContinue();
568
569 // Add an exit (e.g. "break") from the innermost loop that we're currently
570 // in.
571 void createLoopExit();
572
573 // Close the innermost loop that you're in
574 void closeLoop();
575
576 //
577 // Access chain design for an R-Value vs. L-Value:
578 //
579 // There is a single access chain the builder is building at
580 // any particular time. Such a chain can be used to either to a load or
581 // a store, when desired.
582 //
583 // Expressions can be r-values, l-values, or both, or only r-values:
584 // a[b.c].d = .... // l-value
585 // ... = a[b.c].d; // r-value, that also looks like an l-value
586 // ++a[b.c].d; // r-value and l-value
587 // (x + y)[2]; // r-value only, can't possibly be l-value
588 //
589 // Computing an r-value means generating code. Hence,
590 // r-values should only be computed when they are needed, not speculatively.
591 //
592 // Computing an l-value means saving away information for later use in the compiler,
593 // no code is generated until the l-value is later dereferenced. It is okay
594 // to speculatively generate an l-value, just not okay to speculatively dereference it.
595 //
596 // The base of the access chain (the left-most variable or expression
597 // from which everything is based) can be set either as an l-value
598 // or as an r-value. Most efficient would be to set an l-value if one
599 // is available. If an expression was evaluated, the resulting r-value
600 // can be set as the chain base.
601 //
602 // The users of this single access chain can save and restore if they
603 // want to nest or manage multiple chains.
604 //
605
606 struct AccessChain {
607 Id base; // for l-values, pointer to the base object, for r-values, the base object
608 std::vector<Id> indexChain;
609 Id instr; // cache the instruction that generates this access chain
610 std::vector<unsigned> swizzle; // each std::vector element selects the next GLSL component number
611 Id component; // a dynamic component index, can coexist with a swizzle,
612 // done after the swizzle, NoResult if not present
613 Id preSwizzleBaseType; // dereferenced type, before swizzle or component is applied;
614 // NoType unless a swizzle or component is present
615 bool isRValue; // true if 'base' is an r-value, otherwise, base is an l-value
616 unsigned int alignment; // bitwise OR of alignment values passed in. Accumulates worst alignment.
617 // Only tracks base and (optional) component selection alignment.
618
619 // Accumulate whether anything in the chain of structures has coherent decorations.
620 struct CoherentFlags {
621 CoherentFlags() { clear(); }
622 #ifdef GLSLANG_WEB
623 void clear() { }
624 bool isVolatile() const { return false; }
625 CoherentFlags operator |=(const CoherentFlags &other) { return *this; }
626 #else
627 bool isVolatile() const { return volatil; }
628 bool anyCoherent() const {
629 return coherent || devicecoherent || queuefamilycoherent || workgroupcoherent ||
630 subgroupcoherent || shadercallcoherent;
631 }
632
633 unsigned coherent : 1;
634 unsigned devicecoherent : 1;
635 unsigned queuefamilycoherent : 1;
636 unsigned workgroupcoherent : 1;
637 unsigned subgroupcoherent : 1;
638 unsigned shadercallcoherent : 1;
639 unsigned nonprivate : 1;
640 unsigned volatil : 1;
641 unsigned isImage : 1;
642
643 void clear() {
644 coherent = 0;
645 devicecoherent = 0;
646 queuefamilycoherent = 0;
647 workgroupcoherent = 0;
648 subgroupcoherent = 0;
649 shadercallcoherent = 0;
650 nonprivate = 0;
651 volatil = 0;
652 isImage = 0;
653 }
654
655 CoherentFlags operator |=(const CoherentFlags &other) {
656 coherent |= other.coherent;
657 devicecoherent |= other.devicecoherent;
658 queuefamilycoherent |= other.queuefamilycoherent;
659 workgroupcoherent |= other.workgroupcoherent;
660 subgroupcoherent |= other.subgroupcoherent;
661 shadercallcoherent |= other.shadercallcoherent;
662 nonprivate |= other.nonprivate;
663 volatil |= other.volatil;
664 isImage |= other.isImage;
665 return *this;
666 }
667 #endif
668 };
669 CoherentFlags coherentFlags;
670 };
671
672 //
673 // the SPIR-V builder maintains a single active chain that
674 // the following methods operate on
675 //
676
677 // for external save and restore
678 AccessChain getAccessChain() { return accessChain; }
679 void setAccessChain(AccessChain newChain) { accessChain = newChain; }
680
681 // clear accessChain
682 void clearAccessChain();
683
684 // set new base as an l-value base
685 void setAccessChainLValue(Id lValue)
686 {
687 assert(isPointer(lValue));
688 accessChain.base = lValue;
689 }
690
691 // set new base value as an r-value
692 void setAccessChainRValue(Id rValue)
693 {
694 accessChain.isRValue = true;
695 accessChain.base = rValue;
696 }
697
698 // push offset onto the end of the chain
699 void accessChainPush(Id offset, AccessChain::CoherentFlags coherentFlags, unsigned int alignment)
700 {
701 accessChain.indexChain.push_back(offset);
702 accessChain.coherentFlags |= coherentFlags;
703 accessChain.alignment |= alignment;
704 }
705
706 // push new swizzle onto the end of any existing swizzle, merging into a single swizzle
707 void accessChainPushSwizzle(std::vector<unsigned>& swizzle, Id preSwizzleBaseType,
708 AccessChain::CoherentFlags coherentFlags, unsigned int alignment);
709
710 // push a dynamic component selection onto the access chain, only applicable with a
711 // non-trivial swizzle or no swizzle
712 void accessChainPushComponent(Id component, Id preSwizzleBaseType, AccessChain::CoherentFlags coherentFlags,
713 unsigned int alignment)
714 {
715 if (accessChain.swizzle.size() != 1) {
716 accessChain.component = component;
717 if (accessChain.preSwizzleBaseType == NoType)
718 accessChain.preSwizzleBaseType = preSwizzleBaseType;
719 }
720 accessChain.coherentFlags |= coherentFlags;
721 accessChain.alignment |= alignment;
722 }
723
724 // use accessChain and swizzle to store value
725 void accessChainStore(Id rvalue, Decoration nonUniform,
726 spv::MemoryAccessMask memoryAccess = spv::MemoryAccessMaskNone,
727 spv::Scope scope = spv::ScopeMax, unsigned int alignment = 0);
728
729 // use accessChain and swizzle to load an r-value
730 Id accessChainLoad(Decoration precision, Decoration nonUniform, Id ResultType,
731 spv::MemoryAccessMask memoryAccess = spv::MemoryAccessMaskNone, spv::Scope scope = spv::ScopeMax,
732 unsigned int alignment = 0);
733
734 // Return whether or not the access chain can be represented in SPIR-V
735 // as an l-value.
736 // E.g., a[3].yx cannot be, while a[3].y and a[3].y[x] can be.
737 bool isSpvLvalue() const { return accessChain.swizzle.size() <= 1; }
738
739 // get the direct pointer for an l-value
740 Id accessChainGetLValue();
741
742 // Get the inferred SPIR-V type of the result of the current access chain,
743 // based on the type of the base and the chain of dereferences.
744 Id accessChainGetInferredType();
745
746 // Add capabilities, extensions, remove unneeded decorations, etc.,
747 // based on the resulting SPIR-V.
748 void postProcess();
749
750 // Prune unreachable blocks in the CFG and remove unneeded decorations.
751 void postProcessCFG();
752
753 #ifndef GLSLANG_WEB
754 // Add capabilities, extensions based on instructions in the module.
755 void postProcessFeatures();
756 // Hook to visit each instruction in a block in a function
757 void postProcess(Instruction&);
758 // Hook to visit each non-32-bit sized float/int operation in a block.
759 void postProcessType(const Instruction&, spv::Id typeId);
760 #endif
761
762 void dump(std::vector<unsigned int>&) const;
763
764 void createBranch(Block* block);
765 void createConditionalBranch(Id condition, Block* thenBlock, Block* elseBlock);
766 void createLoopMerge(Block* mergeBlock, Block* continueBlock, unsigned int control,
767 const std::vector<unsigned int>& operands);
768
769 // Sets to generate opcode for specialization constants.
770 void setToSpecConstCodeGenMode() { generatingOpCodeForSpecConst = true; }
771 // Sets to generate opcode for non-specialization constants (normal mode).
772 void setToNormalCodeGenMode() { generatingOpCodeForSpecConst = false; }
773 // Check if the builder is generating code for spec constants.
774 bool isInSpecConstCodeGenMode() { return generatingOpCodeForSpecConst; }
775
776 protected:
777 Id makeIntConstant(Id typeId, unsigned value, bool specConstant);
778 Id makeInt64Constant(Id typeId, unsigned long long value, bool specConstant);
779 Id findScalarConstant(Op typeClass, Op opcode, Id typeId, unsigned value);
780 Id findScalarConstant(Op typeClass, Op opcode, Id typeId, unsigned v1, unsigned v2);
781 Id findCompositeConstant(Op typeClass, Id typeId, const std::vector<Id>& comps);
782 Id findStructConstant(Id typeId, const std::vector<Id>& comps);
783 Id collapseAccessChain();
784 void remapDynamicSwizzle();
785 void transferAccessChainSwizzle(bool dynamic);
786 void simplifyAccessChainSwizzle();
787 void createAndSetNoPredecessorBlock(const char*);
788 void createSelectionMerge(Block* mergeBlock, unsigned int control);
789 void dumpSourceInstructions(std::vector<unsigned int>&) const;
790 void dumpSourceInstructions(const spv::Id fileId, const std::string& text, std::vector<unsigned int>&) const;
791 void dumpInstructions(std::vector<unsigned int>&, const std::vector<std::unique_ptr<Instruction> >&) const;
792 void dumpModuleProcesses(std::vector<unsigned int>&) const;
793 spv::MemoryAccessMask sanitizeMemoryAccessForStorageClass(spv::MemoryAccessMask memoryAccess, StorageClass sc)
794 const;
795
796 unsigned int spvVersion; // the version of SPIR-V to emit in the header
797 SourceLanguage source;
798 int sourceVersion;
799 spv::Id sourceFileStringId;
800 std::string sourceText;
801 int currentLine;
802 const char* currentFile;
803 bool emitOpLines;
804 std::set<std::string> extensions;
805 std::vector<const char*> sourceExtensions;
806 std::vector<const char*> moduleProcesses;
807 AddressingModel addressModel;
808 MemoryModel memoryModel;
809 std::set<spv::Capability> capabilities;
810 int builderNumber;
811 Module module;
812 Block* buildPoint;
813 Id uniqueId;
814 Function* entryPointFunction;
815 bool generatingOpCodeForSpecConst;
816 AccessChain accessChain;
817
818 // special blocks of instructions for output
819 std::vector<std::unique_ptr<Instruction> > strings;
820 std::vector<std::unique_ptr<Instruction> > imports;
821 std::vector<std::unique_ptr<Instruction> > entryPoints;
822 std::vector<std::unique_ptr<Instruction> > executionModes;
823 std::vector<std::unique_ptr<Instruction> > names;
824 std::vector<std::unique_ptr<Instruction> > decorations;
825 std::vector<std::unique_ptr<Instruction> > constantsTypesGlobals;
826 std::vector<std::unique_ptr<Instruction> > externals;
827 std::vector<std::unique_ptr<Function> > functions;
828
829 // not output, internally used for quick & dirty canonical (unique) creation
830
831 // map type opcodes to constant inst.
832 std::unordered_map<unsigned int, std::vector<Instruction*>> groupedConstants;
833 // map struct-id to constant instructions
834 std::unordered_map<unsigned int, std::vector<Instruction*>> groupedStructConstants;
835 // map type opcodes to type instructions
836 std::unordered_map<unsigned int, std::vector<Instruction*>> groupedTypes;
837
838 // stack of switches
839 std::stack<Block*> switchMerges;
840
841 // Our loop stack.
842 std::stack<LoopBlocks> loops;
843
844 // map from strings to their string ids
845 std::unordered_map<std::string, spv::Id> stringIds;
846
847 // map from include file name ids to their contents
848 std::map<spv::Id, const std::string*> includeFiles;
849
850 // The stream for outputting warnings and errors.
851 SpvBuildLogger* logger;
852 }; // end Builder class
853
854 }; // end spv namespace
855
856 #endif // SpvBuilder_H