2 //Copyright (C) 2014 LunarG, Inc.
6 //Redistribution and use in source and binary forms, with or without
7 //modification, are permitted provided that the following conditions
10 // Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
13 // Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials provided
16 // with the distribution.
18 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
19 // contributors may be used to endorse or promote products derived
20 // from this software without specific prior written permission.
22 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 //POSSIBILITY OF SUCH DAMAGE.
36 // Author: John Kessenich, LunarG
40 // "Builder" is an interface to fully build SPIR-V IR. Allocate one of
41 // these to build (a thread safe) internal SPIR-V representation (IR),
42 // and then dump it as a binary stream according to the SPIR-V specification.
44 // A Builder has a 1:1 relationship with a SPIR-V module.
63 Builder(unsigned int userNumber);
66 static const int maxMatrixSize = 4;
68 void setSource(spv::SourceLanguage lang, int version)
71 sourceVersion = version;
73 void addSourceExtension(const char* ext) { extensions.push_back(ext); }
74 Id import(const char*);
75 void setMemoryModel(spv::AddressingModel addr, spv::MemoryModel mem)
81 void addCapability(spv::Capability cap) { capabilities.push_back(cap); }
83 // To get a new <id> for anything needing a new one.
84 Id getUniqueId() { return ++uniqueId; }
86 // To get a set of new <id>s, e.g., for a set of function parameters
87 Id getUniqueIds(int numIds)
94 // For creating new types (will return old type if the requested one was already made).
97 Id makePointer(StorageClass, Id type);
98 Id makeIntegerType(int width, bool hasSign); // generic
99 Id makeIntType(int width) { return makeIntegerType(width, true); }
100 Id makeUintType(int width) { return makeIntegerType(width, false); }
101 Id makeFloatType(int width);
102 Id makeStructType(std::vector<Id>& members, const char*);
103 Id makeStructResultType(Id type0, Id type1);
104 Id makeVectorType(Id component, int size);
105 Id makeMatrixType(Id component, int cols, int rows);
106 Id makeArrayType(Id element, unsigned size, int stride); // 0 means no stride decoration
107 Id makeRuntimeArray(Id element);
108 Id makeFunctionType(Id returnType, std::vector<Id>& paramTypes);
109 Id makeImageType(Id sampledType, Dim, bool depth, bool arrayed, bool ms, unsigned sampled, ImageFormat format);
110 Id makeSamplerType();
111 Id makeSampledImageType(Id imageType);
113 // For querying about types.
114 Id getTypeId(Id resultId) const { return module.getTypeId(resultId); }
115 Id getDerefTypeId(Id resultId) const;
116 Op getOpCode(Id id) const { return module.getInstruction(id)->getOpCode(); }
117 Op getTypeClass(Id typeId) const { return getOpCode(typeId); }
118 Op getMostBasicTypeClass(Id typeId) const;
119 int getNumComponents(Id resultId) const { return getNumTypeComponents(getTypeId(resultId)); }
120 int getNumTypeConstituents(Id typeId) const;
121 int getNumTypeComponents(Id typeId) const { return getNumTypeConstituents(typeId); }
122 Id getScalarTypeId(Id typeId) const;
123 Id getContainedTypeId(Id typeId) const;
124 Id getContainedTypeId(Id typeId, int) const;
125 StorageClass getTypeStorageClass(Id typeId) const { return module.getStorageClass(typeId); }
127 bool isPointer(Id resultId) const { return isPointerType(getTypeId(resultId)); }
128 bool isScalar(Id resultId) const { return isScalarType(getTypeId(resultId)); }
129 bool isVector(Id resultId) const { return isVectorType(getTypeId(resultId)); }
130 bool isMatrix(Id resultId) const { return isMatrixType(getTypeId(resultId)); }
131 bool isAggregate(Id resultId) const { return isAggregateType(getTypeId(resultId)); }
132 bool isSampledImage(Id resultId) const { return isSampledImageType(getTypeId(resultId)); }
134 bool isBoolType(Id typeId) const { return groupedTypes[OpTypeBool].size() > 0 && typeId == groupedTypes[OpTypeBool].back()->getResultId(); }
135 bool isPointerType(Id typeId) const { return getTypeClass(typeId) == OpTypePointer; }
136 bool isScalarType(Id typeId) const { return getTypeClass(typeId) == OpTypeFloat || getTypeClass(typeId) == OpTypeInt || getTypeClass(typeId) == OpTypeBool; }
137 bool isVectorType(Id typeId) const { return getTypeClass(typeId) == OpTypeVector; }
138 bool isMatrixType(Id typeId) const { return getTypeClass(typeId) == OpTypeMatrix; }
139 bool isStructType(Id typeId) const { return getTypeClass(typeId) == OpTypeStruct; }
140 bool isArrayType(Id typeId) const { return getTypeClass(typeId) == OpTypeArray; }
141 bool isAggregateType(Id typeId) const { return isArrayType(typeId) || isStructType(typeId); }
142 bool isImageType(Id typeId) const { return getTypeClass(typeId) == OpTypeImage; }
143 bool isSamplerType(Id typeId) const { return getTypeClass(typeId) == OpTypeSampler; }
144 bool isSampledImageType(Id typeId) const { return getTypeClass(typeId) == OpTypeSampledImage; }
146 bool isConstantOpCode(Op opcode) const;
147 bool isConstant(Id resultId) const { return isConstantOpCode(getOpCode(resultId)); }
148 bool isConstantScalar(Id resultId) const { return getOpCode(resultId) == OpConstant; }
149 unsigned int getConstantScalar(Id resultId) const { return module.getInstruction(resultId)->getImmediateOperand(0); }
150 StorageClass getStorageClass(Id resultId) const { return getTypeStorageClass(getTypeId(resultId)); }
152 int getTypeNumColumns(Id typeId) const
154 assert(isMatrixType(typeId));
155 return getNumTypeConstituents(typeId);
157 int getNumColumns(Id resultId) const { return getTypeNumColumns(getTypeId(resultId)); }
158 int getTypeNumRows(Id typeId) const
160 assert(isMatrixType(typeId));
161 return getNumTypeComponents(getContainedTypeId(typeId));
163 int getNumRows(Id resultId) const { return getTypeNumRows(getTypeId(resultId)); }
165 Dim getTypeDimensionality(Id typeId) const
167 assert(isImageType(typeId));
168 return (Dim)module.getInstruction(typeId)->getImmediateOperand(1);
170 Id getImageType(Id resultId) const
172 Id typeId = getTypeId(resultId);
173 assert(isImageType(typeId) || isSampledImageType(typeId));
174 return isSampledImageType(typeId) ? module.getInstruction(typeId)->getIdOperand(0) : typeId;
176 bool isArrayedImageType(Id typeId) const
178 assert(isImageType(typeId));
179 return module.getInstruction(typeId)->getImmediateOperand(3) != 0;
182 // For making new constants (will return old constant if the requested one was already made).
183 Id makeBoolConstant(bool b, bool specConstant = false);
184 Id makeIntConstant(int i, bool specConstant = false) { return makeIntConstant(makeIntType(32), (unsigned)i, specConstant); }
185 Id makeUintConstant(unsigned u, bool specConstant = false) { return makeIntConstant(makeUintType(32), u, specConstant); }
186 Id makeFloatConstant(float f, bool specConstant = false);
187 Id makeDoubleConstant(double d, bool specConstant = false);
189 // Turn the array of constants into a proper spv constant of the requested type.
190 Id makeCompositeConstant(Id type, std::vector<Id>& comps);
192 // Methods for adding information outside the CFG.
193 Instruction* addEntryPoint(ExecutionModel, Function*, const char* name);
194 void addExecutionMode(Function*, ExecutionMode mode, int value1 = -1, int value2 = -1, int value3 = -1);
195 void addName(Id, const char* name);
196 void addMemberName(Id, int member, const char* name);
197 void addLine(Id target, Id fileName, int line, int column);
198 void addDecoration(Id, Decoration, int num = -1);
199 void addMemberDecoration(Id, unsigned int member, Decoration, int num = -1);
201 // At the end of what block do the next create*() instructions go?
202 void setBuildPoint(Block* bp) { buildPoint = bp; }
203 Block* getBuildPoint() const { return buildPoint; }
205 // Make the main function. The returned pointer is only valid
206 // for the lifetime of this builder.
207 Function* makeMain();
209 // Make a shader-style function, and create its entry block if entry is non-zero.
210 // Return the function, pass back the entry.
211 // The returned pointer is only valid for the lifetime of this builder.
212 Function* makeFunctionEntry(Id returnType, const char* name, std::vector<Id>& paramTypes, Block **entry = 0);
214 // Create a return. An 'implicit' return is one not appearing in the source
215 // code. In the case of an implicit return, no post-return block is inserted.
216 void makeReturn(bool implicit, Id retVal = 0);
218 // Generate all the code needed to finish up a function.
219 void leaveFunction();
224 // Create a global or function local or IO variable.
225 Id createVariable(StorageClass, Id type, const char* name = 0);
227 // Create an imtermediate with an undefined value.
228 Id createUndefined(Id type);
230 // Store into an Id and return the l-value
231 void createStore(Id rValue, Id lValue);
233 // Load from an Id and return it
234 Id createLoad(Id lValue);
236 // Create an OpAccessChain instruction
237 Id createAccessChain(StorageClass, Id base, std::vector<Id>& offsets);
239 // Create an OpArrayLength instruction
240 Id createArrayLength(Id base, unsigned int member);
242 // Create an OpCompositeExtract instruction
243 Id createCompositeExtract(Id composite, Id typeId, unsigned index);
244 Id createCompositeExtract(Id composite, Id typeId, std::vector<unsigned>& indexes);
245 Id createCompositeInsert(Id object, Id composite, Id typeId, unsigned index);
246 Id createCompositeInsert(Id object, Id composite, Id typeId, std::vector<unsigned>& indexes);
248 Id createVectorExtractDynamic(Id vector, Id typeId, Id componentIndex);
249 Id createVectorInsertDynamic(Id vector, Id typeId, Id component, Id componentIndex);
251 void createNoResultOp(Op);
252 void createNoResultOp(Op, Id operand);
253 void createNoResultOp(Op, const std::vector<Id>& operands);
254 void createControlBarrier(Scope execution, Scope memory, MemorySemanticsMask);
255 void createMemoryBarrier(unsigned executionScope, unsigned memorySemantics);
256 Id createUnaryOp(Op, Id typeId, Id operand);
257 Id createBinOp(Op, Id typeId, Id operand1, Id operand2);
258 Id createTriOp(Op, Id typeId, Id operand1, Id operand2, Id operand3);
259 Id createOp(Op, Id typeId, const std::vector<Id>& operands);
260 Id createFunctionCall(spv::Function*, std::vector<spv::Id>&);
262 // Take an rvalue (source) and a set of channels to extract from it to
263 // make a new rvalue, which is returned.
264 Id createRvalueSwizzle(Id typeId, Id source, std::vector<unsigned>& channels);
266 // Take a copy of an lvalue (target) and a source of components, and set the
267 // source components into the lvalue where the 'channels' say to put them.
268 // An updated version of the target is returned.
269 // (No true lvalue or stores are used.)
270 Id createLvalueSwizzle(Id typeId, Id target, Id source, std::vector<unsigned>& channels);
272 // If the value passed in is an instruction and the precision is not NoPrecision,
273 // it gets tagged with the requested precision.
274 void setPrecision(Id /* value */, Decoration precision)
276 if (precision != NoPrecision) {
281 // Can smear a scalar to a vector for the following forms:
282 // - promoteScalar(scalar, vector) // smear scalar to width of vector
283 // - promoteScalar(vector, scalar) // smear scalar to width of vector
284 // - promoteScalar(pointer, scalar) // smear scalar to width of what pointer points to
285 // - promoteScalar(scalar, scalar) // do nothing
286 // Other forms are not allowed.
288 // Generally, the type of 'scalar' does not need to be the same type as the components in 'vector'.
289 // The type of the created vector is a vector of components of the same type as the scalar.
291 // Note: One of the arguments will change, with the result coming back that way rather than
292 // through the return value.
293 void promoteScalar(Decoration precision, Id& left, Id& right);
295 // Make a value by smearing the scalar to fill the type.
296 // vectorType should be the correct type for making a vector of scalarVal.
297 // (No conversions are done.)
298 Id smearScalar(Decoration precision, Id scalarVal, Id vectorType);
300 // Create a call to a built-in function.
301 Id createBuiltinCall(Decoration precision, Id resultType, Id builtins, int entryPoint, std::vector<Id>& args);
303 // List of parameters used to create a texture operation
304 struct TextureParameters {
318 // Select the correct texture operation based on all inputs, and emit the correct instruction
319 Id createTextureCall(Decoration precision, Id resultType, bool fetch, bool proj, bool gather, const TextureParameters&);
321 // Emit the OpTextureQuery* instruction that was passed in.
322 // Figure out the right return value and type, and return it.
323 Id createTextureQueryCall(Op, const TextureParameters&);
325 Id createSamplePositionCall(Decoration precision, Id, Id);
327 Id createBitFieldExtractCall(Decoration precision, Id, Id, Id, bool isSigned);
328 Id createBitFieldInsertCall(Decoration precision, Id, Id, Id, Id);
330 // Reduction comparision for composites: For equal and not-equal resulting in a scalar.
331 Id createCompositeCompare(Decoration precision, Id, Id, bool /* true if for equal, false if for not-equal */);
333 // OpCompositeConstruct
334 Id createCompositeConstruct(Id typeId, std::vector<Id>& constituents);
336 // vector or scalar constructor
337 Id createConstructor(Decoration precision, const std::vector<Id>& sources, Id resultTypeId);
339 // matrix constructor
340 Id createMatrixConstructor(Decoration precision, const std::vector<Id>& sources, Id constructee);
342 // Helper to use for building nested control flow with if-then-else.
345 If(Id condition, Builder& builder);
348 void makeBeginElse();
364 // Make a switch statement. A switch has 'numSegments' of pieces of code, not containing
365 // any case/default labels, all separated by one or more case/default labels. Each possible
366 // case value v is a jump to the caseValues[v] segment. The defaultSegment is also in this
367 // number space. How to compute the value is given by 'condition', as in switch(condition).
369 // The SPIR-V Builder will maintain the stack of post-switch merge blocks for nested switches.
371 // Use a defaultSegment < 0 if there is no default segment (to branch to post switch).
373 // Returns the right set of basic blocks to start each code segment with, so that the caller's
374 // recursion stack can hold the memory for it.
376 void makeSwitch(Id condition, int numSegments, std::vector<int>& caseValues, std::vector<int>& valueToSegment, int defaultSegment,
377 std::vector<Block*>& segmentBB); // return argument
379 // Add a branch to the innermost switch's merge block.
380 void addSwitchBreak();
382 // Move to the next code segment, passing in the return argument in makeSwitch()
383 void nextSwitchSegment(std::vector<Block*>& segmentBB, int segment);
385 // Finish off the innermost switch.
386 void endSwitch(std::vector<Block*>& segmentBB);
388 // Start the beginning of a new loop, and prepare the builder to
389 // generate code for the loop test.
390 // The loopTestFirst parameter is true when the loop test executes before
391 // the body. (It is false for do-while loops.)
392 void makeNewLoop(bool loopTestFirst);
394 // Add the branch for the loop test, based on the given condition.
395 // The true branch goes to the first block in the loop body, and
396 // the false branch goes to the loop's merge block. The builder insertion
397 // point will be placed at the start of the body.
398 void createLoopTestBranch(Id condition);
400 // Generate an unconditional branch to the loop body. The builder insertion
401 // point will be placed at the start of the body. Use this when there is
403 void createBranchToBody();
405 // Add a branch to the test of the current (innermost) loop.
406 // The way we generate code, that's also the loop header.
407 void createLoopContinue();
409 // Add an exit (e.g. "break") for the innermost loop that you're in
410 void createLoopExit();
412 // Close the innermost loop that you're in
416 // Access chain design for an R-Value vs. L-Value:
418 // There is a single access chain the builder is building at
419 // any particular time. Such a chain can be used to either to a load or
420 // a store, when desired.
422 // Expressions can be r-values, l-values, or both, or only r-values:
423 // a[b.c].d = .... // l-value
424 // ... = a[b.c].d; // r-value, that also looks like an l-value
425 // ++a[b.c].d; // r-value and l-value
426 // (x + y)[2]; // r-value only, can't possibly be l-value
428 // Computing an r-value means generating code. Hence,
429 // r-values should only be computed when they are needed, not speculatively.
431 // Computing an l-value means saving away information for later use in the compiler,
432 // no code is generated until the l-value is later dereferenced. It is okay
433 // to speculatively generate an l-value, just not okay to speculatively dereference it.
435 // The base of the access chain (the left-most variable or expression
436 // from which everything is based) can be set either as an l-value
437 // or as an r-value. Most efficient would be to set an l-value if one
438 // is available. If an expression was evaluated, the resulting r-value
439 // can be set as the chain base.
441 // The users of this single access chain can save and restore if they
442 // want to nest or manage multiple chains.
446 Id base; // for l-values, pointer to the base object, for r-values, the base object
447 std::vector<Id> indexChain;
448 Id instr; // cache the instruction that generates this access chain
449 std::vector<unsigned> swizzle; // each std::vector element selects the next GLSL component number
450 Id component; // a dynamic component index, can coexist with a swizzle, done after the swizzle, NoResult if not present
451 Id preSwizzleBaseType; // dereferenced type, before swizzle or component is applied; NoType unless a swizzle or component is present
452 bool isRValue; // true if 'base' is an r-value, otherwise, base is an l-value
456 // the SPIR-V builder maintains a single active chain that
457 // the following methods operated on
460 // for external save and restore
461 AccessChain getAccessChain() { return accessChain; }
462 void setAccessChain(AccessChain newChain) { accessChain = newChain; }
465 void clearAccessChain();
467 // set new base as an l-value base
468 void setAccessChainLValue(Id lValue)
470 assert(isPointer(lValue));
471 accessChain.base = lValue;
474 // set new base value as an r-value
475 void setAccessChainRValue(Id rValue)
477 accessChain.isRValue = true;
478 accessChain.base = rValue;
481 // push offset onto the end of the chain
482 void accessChainPush(Id offset)
484 accessChain.indexChain.push_back(offset);
487 // push new swizzle onto the end of any existing swizzle, merging into a single swizzle
488 void accessChainPushSwizzle(std::vector<unsigned>& swizzle, Id preSwizzleBaseType);
490 // push a variable component selection onto the access chain; supporting only one, so unsided
491 void accessChainPushComponent(Id component, Id preSwizzleBaseType)
493 accessChain.component = component;
494 if (accessChain.preSwizzleBaseType == NoType)
495 accessChain.preSwizzleBaseType = preSwizzleBaseType;
498 // use accessChain and swizzle to store value
499 void accessChainStore(Id rvalue);
501 // use accessChain and swizzle to load an r-value
502 Id accessChainLoad(Id ResultType);
504 // get the direct pointer for an l-value
505 Id accessChainGetLValue();
507 void dump(std::vector<unsigned int>&) const;
510 Id makeIntConstant(Id typeId, unsigned value, bool specConstant);
511 Id findScalarConstant(Op typeClass, Op opcode, Id typeId, unsigned value) const;
512 Id findScalarConstant(Op typeClass, Op opcode, Id typeId, unsigned v1, unsigned v2) const;
513 Id findCompositeConstant(Op typeClass, std::vector<Id>& comps) const;
514 Id collapseAccessChain();
515 void transferAccessChainSwizzle(bool dynamic);
516 void simplifyAccessChainSwizzle();
517 void createAndSetNoPredecessorBlock(const char*);
518 void createBranch(Block* block);
519 void createSelectionMerge(Block* mergeBlock, unsigned int control);
520 void createLoopMerge(Block* mergeBlock, Block* continueBlock, unsigned int control);
521 void createConditionalBranch(Id condition, Block* thenBlock, Block* elseBlock);
522 void dumpInstructions(std::vector<unsigned int>&, const std::vector<std::unique_ptr<Instruction> >&) const;
524 struct Loop; // Defined below.
525 void createBranchToLoopHeaderFromInside(const Loop& loop);
527 SourceLanguage source;
529 std::vector<const char*> extensions;
530 AddressingModel addressModel;
531 MemoryModel memoryModel;
532 std::vector<spv::Capability> capabilities;
537 Function* mainFunction;
538 AccessChain accessChain;
540 // special blocks of instructions for output
541 std::vector<std::unique_ptr<Instruction> > imports;
542 std::vector<std::unique_ptr<Instruction> > entryPoints;
543 std::vector<std::unique_ptr<Instruction> > executionModes;
544 std::vector<std::unique_ptr<Instruction> > names;
545 std::vector<std::unique_ptr<Instruction> > lines;
546 std::vector<std::unique_ptr<Instruction> > decorations;
547 std::vector<std::unique_ptr<Instruction> > constantsTypesGlobals;
548 std::vector<std::unique_ptr<Instruction> > externals;
549 std::vector<std::unique_ptr<Function> > functions;
551 // not output, internally used for quick & dirty canonical (unique) creation
552 std::vector<Instruction*> groupedConstants[OpConstant]; // all types appear before OpConstant
553 std::vector<Instruction*> groupedTypes[OpConstant];
556 std::stack<Block*> switchMerges;
558 // Data that needs to be kept in order to properly handle loops.
560 // Constructs a default Loop structure containing new header, merge, and
561 // body blocks for the current function.
562 // The testFirst argument indicates whether the loop test executes at
563 // the top of the loop rather than at the bottom. In the latter case,
564 // also create a phi instruction whose value indicates whether we're on
565 // the first iteration of the loop. The phi instruction is initialized
566 // with no values or predecessor operands.
567 Loop(Builder& builder, bool testFirst);
569 // The function containing the loop.
570 Function* const function;
571 // The header is the first block generated for the loop.
572 // It dominates all the blocks in the loop, i.e. it is always
573 // executed before any others.
574 // If the loop test is executed before the body (as in "while" and
575 // "for" loops), then the header begins with the test code.
576 // Otherwise, the loop is a "do-while" loop and the header contains the
577 // start of the body of the loop (if the body exists).
579 // The merge block marks the end of the loop. Control is transferred
580 // to the merge block when either the loop test fails, or when a
581 // nested "break" is encountered.
583 // The body block is the first basic block in the body of the loop, i.e.
584 // the code that is to be repeatedly executed, aside from loop control.
585 // This member is null until we generate code that references the loop
588 // True when the loop test executes before the body.
589 const bool testFirst;
590 // When the test executes after the body, this is defined as the phi
591 // instruction that tells us whether we are on the first iteration of
592 // the loop. Otherwise this is null. This is non-const because
593 // it has to be initialized outside of the initializer-list.
594 Instruction* isFirstIteration;
598 std::stack<Loop> loops;
599 }; // end Builder class
601 // Use for non-fatal notes about what's not complete
602 void TbdFunctionality(const char*);
604 // Use for fatal missing functionality
605 void MissingFunctionality(const char*);
607 }; // end spv namespace
609 #endif // SpvBuilder_H