2 // Copyright (C) 2014 LunarG, Inc.
4 // All rights reserved.
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.
37 // Simple in-memory representation (IR) of SPIRV. Just for holding
38 // Each function's CFG of blocks. Has this hierarchy:
39 // - Module, which is a list of
40 // - Function, which is a list of
41 // - Block, which is a list of
64 const Id NoResult = 0;
67 const Decoration NoPrecision = DecorationMax;
68 const MemorySemanticsMask MemorySemanticsAllMemory =
69 (MemorySemanticsMask)(MemorySemanticsSequentiallyConsistentMask |
70 MemorySemanticsUniformMemoryMask |
71 MemorySemanticsSubgroupMemoryMask |
72 MemorySemanticsWorkgroupMemoryMask |
73 MemorySemanticsCrossWorkgroupMemoryMask |
74 MemorySemanticsAtomicCounterMemoryMask |
75 MemorySemanticsImageMemoryMask);
78 // SPIR-V IR instruction.
83 Instruction(Id resultId, Id typeId, Op opCode) : resultId(resultId), typeId(typeId), opCode(opCode), block(nullptr) { }
84 explicit Instruction(Op opCode) : resultId(NoResult), typeId(NoType), opCode(opCode), block(nullptr) { }
85 virtual ~Instruction() {}
86 void addIdOperand(Id id) { operands.push_back(id); }
87 void addImmediateOperand(unsigned int immediate) { operands.push_back(immediate); }
88 void addStringOperand(const char* str)
92 char* wordString = (char*)&word;
93 char* wordPtr = wordString;
100 if (charCount == 4) {
101 addImmediateOperand(word);
102 wordPtr = wordString;
107 // deal with partial last word
110 for (; charCount < 4; ++charCount)
112 addImmediateOperand(word);
115 void setBlock(Block* b) { block = b; }
116 Block* getBlock() const { return block; }
117 Op getOpCode() const { return opCode; }
118 int getNumOperands() const { return (int)operands.size(); }
119 Id getResultId() const { return resultId; }
120 Id getTypeId() const { return typeId; }
121 Id getIdOperand(int op) const { return operands[op]; }
122 unsigned int getImmediateOperand(int op) const { return operands[op]; }
123 const char* getStringOperand() const { return originalString.c_str(); }
125 // Write out the binary form.
126 void dump(std::vector<unsigned int>& out) const
128 // Compute the wordCount
129 unsigned int wordCount = 1;
134 wordCount += (unsigned int)operands.size();
136 // Write out the beginning of the instruction
137 out.push_back(((wordCount) << WordCountShift) | opCode);
139 out.push_back(typeId);
141 out.push_back(resultId);
143 // Write out the operands
144 for (int op = 0; op < (int)operands.size(); ++op)
145 out.push_back(operands[op]);
149 Instruction(const Instruction&);
153 std::vector<Id> operands;
154 std::string originalString; // could be optimized away; convenience for getting string operand
164 Block(Id id, Function& parent);
169 Id getId() { return instructions.front()->getResultId(); }
171 Function& getParent() const { return parent; }
172 void addInstruction(std::unique_ptr<Instruction> inst);
173 void addPredecessor(Block* pred) { predecessors.push_back(pred); pred->successors.push_back(this);}
174 void addLocalVariable(std::unique_ptr<Instruction> inst) { localVariables.push_back(std::move(inst)); }
175 const std::vector<Block*>& getPredecessors() const { return predecessors; }
176 const std::vector<Block*>& getSuccessors() const { return successors; }
177 const std::vector<std::unique_ptr<Instruction> >& getInstructions() const {
180 void setUnreachable() { unreachable = true; }
181 bool isUnreachable() const { return unreachable; }
182 // Returns the block's merge instruction, if one exists (otherwise null).
183 const Instruction* getMergeInstruction() const {
184 if (instructions.size() < 2) return nullptr;
185 const Instruction* nextToLast = (instructions.cend() - 2)->get();
186 switch (nextToLast->getOpCode()) {
187 case OpSelectionMerge:
196 bool isTerminated() const
198 switch (instructions.back()->getOpCode()) {
200 case OpBranchConditional:
211 void dump(std::vector<unsigned int>& out) const
213 instructions[0]->dump(out);
214 for (int i = 0; i < (int)localVariables.size(); ++i)
215 localVariables[i]->dump(out);
216 for (int i = 1; i < (int)instructions.size(); ++i)
217 instructions[i]->dump(out);
222 Block& operator=(Block&);
224 // To enforce keeping parent and ownership in sync:
227 std::vector<std::unique_ptr<Instruction> > instructions;
228 std::vector<Block*> predecessors, successors;
229 std::vector<std::unique_ptr<Instruction> > localVariables;
232 // track whether this block is known to be uncreachable (not necessarily
233 // true for all unreachable blocks, but should be set at least
234 // for the extraneous ones introduced by the builder).
238 // Traverses the control-flow graph rooted at root in an order suited for
239 // readable code generation. Invokes callback at every node in the traversal
241 void inReadableOrder(Block* root, std::function<void(Block*)> callback);
244 // SPIR-V IR Function.
249 Function(Id id, Id resultType, Id functionType, Id firstParam, Module& parent);
252 for (int i = 0; i < (int)parameterInstructions.size(); ++i)
253 delete parameterInstructions[i];
255 for (int i = 0; i < (int)blocks.size(); ++i)
258 Id getId() const { return functionInstruction.getResultId(); }
259 Id getParamId(int p) { return parameterInstructions[p]->getResultId(); }
261 void addBlock(Block* block) { blocks.push_back(block); }
262 void removeBlock(Block* block)
264 auto found = find(blocks.begin(), blocks.end(), block);
265 assert(found != blocks.end());
270 Module& getParent() const { return parent; }
271 Block* getEntryBlock() const { return blocks.front(); }
272 Block* getLastBlock() const { return blocks.back(); }
273 const std::vector<Block*>& getBlocks() const { return blocks; }
274 void addLocalVariable(std::unique_ptr<Instruction> inst);
275 Id getReturnType() const { return functionInstruction.getTypeId(); }
276 void dump(std::vector<unsigned int>& out) const
279 functionInstruction.dump(out);
281 // OpFunctionParameter
282 for (int p = 0; p < (int)parameterInstructions.size(); ++p)
283 parameterInstructions[p]->dump(out);
286 inReadableOrder(blocks[0], [&out](const Block* b) { b->dump(out); });
287 Instruction end(0, 0, OpFunctionEnd);
292 Function(const Function&);
293 Function& operator=(Function&);
296 Instruction functionInstruction;
297 std::vector<Instruction*> parameterInstructions;
298 std::vector<Block*> blocks;
310 // TODO delete things
313 void addFunction(Function *fun) { functions.push_back(fun); }
315 void mapInstruction(Instruction *instruction)
317 spv::Id resultId = instruction->getResultId();
318 // map the instruction's result id
319 if (resultId >= idToInstruction.size())
320 idToInstruction.resize(resultId + 16);
321 idToInstruction[resultId] = instruction;
324 Instruction* getInstruction(Id id) const { return idToInstruction[id]; }
325 const std::vector<Function*>& getFunctions() const { return functions; }
326 spv::Id getTypeId(Id resultId) const { return idToInstruction[resultId]->getTypeId(); }
327 StorageClass getStorageClass(Id typeId) const
329 assert(idToInstruction[typeId]->getOpCode() == spv::OpTypePointer);
330 return (StorageClass)idToInstruction[typeId]->getImmediateOperand(0);
333 void dump(std::vector<unsigned int>& out) const
335 for (int f = 0; f < (int)functions.size(); ++f)
336 functions[f]->dump(out);
340 Module(const Module&);
341 std::vector<Function*> functions;
343 // map from result id to instruction having that result id
344 std::vector<Instruction*> idToInstruction;
346 // map from a result id to its type id
350 // Implementation (it's here due to circular type definitions).
354 // - the OpFunction instruction
355 // - all the OpFunctionParameter instructions
356 __inline Function::Function(Id id, Id resultType, Id functionType, Id firstParamId, Module& parent)
357 : parent(parent), functionInstruction(id, resultType, OpFunction)
360 functionInstruction.addImmediateOperand(FunctionControlMaskNone);
361 functionInstruction.addIdOperand(functionType);
362 parent.mapInstruction(&functionInstruction);
363 parent.addFunction(this);
365 // OpFunctionParameter
366 Instruction* typeInst = parent.getInstruction(functionType);
367 int numParams = typeInst->getNumOperands() - 1;
368 for (int p = 0; p < numParams; ++p) {
369 Instruction* param = new Instruction(firstParamId + p, typeInst->getIdOperand(p + 1), OpFunctionParameter);
370 parent.mapInstruction(param);
371 parameterInstructions.push_back(param);
375 __inline void Function::addLocalVariable(std::unique_ptr<Instruction> inst)
377 Instruction* raw_instruction = inst.get();
378 blocks[0]->addLocalVariable(std::move(inst));
379 parent.mapInstruction(raw_instruction);
382 __inline Block::Block(Id id, Function& parent) : parent(parent), unreachable(false)
384 instructions.push_back(std::unique_ptr<Instruction>(new Instruction(id, NoType, OpLabel)));
385 instructions.back()->setBlock(this);
386 parent.getParent().mapInstruction(instructions.back().get());
389 __inline void Block::addInstruction(std::unique_ptr<Instruction> inst)
391 Instruction* raw_instruction = inst.get();
392 instructions.push_back(std::move(inst));
393 raw_instruction->setBlock(this);
394 if (raw_instruction->getResultId())
395 parent.getParent().mapInstruction(raw_instruction);
398 }; // end spv namespace