2 //Copyright (C) 2014 LunarG, Inc.
\r
4 //All rights reserved.
\r
6 //Redistribution and use in source and binary forms, with or without
\r
7 //modification, are permitted provided that the following conditions
\r
10 // Redistributions of source code must retain the above copyright
\r
11 // notice, this list of conditions and the following disclaimer.
\r
13 // Redistributions in binary form must reproduce the above
\r
14 // copyright notice, this list of conditions and the following
\r
15 // disclaimer in the documentation and/or other materials provided
\r
16 // with the distribution.
\r
18 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
\r
19 // contributors may be used to endorse or promote products derived
\r
20 // from this software without specific prior written permission.
\r
22 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
\r
23 //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
\r
24 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
\r
25 //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
\r
26 //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
27 //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
\r
28 //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
\r
29 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
\r
30 //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
\r
31 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
\r
32 //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
33 //POSSIBILITY OF SUCH DAMAGE.
\r
36 // Author: John Kessenich, LunarG
\r
41 // Simple in-memory representation (IR) of SPIRV. Just for holding
\r
42 // Each function's CFG of blocks. Has this hierarchy:
\r
43 // - Module, which is a list of
\r
44 // - Function, which is a list of
\r
45 // - Block, which is a list of
\r
64 const Id NoResult = 0;
\r
65 const Id NoType = 0;
\r
67 const unsigned int BadValue = 0xFFFFFFFF;
\r
68 const Decoration NoPrecision = (Decoration)BadValue;
\r
69 const MemorySemanticsMask MemorySemanticsAllMemory = (MemorySemanticsMask)0x3FF;
\r
72 // SPIR-V IR instruction.
\r
77 Instruction(Id resultId, Id typeId, Op opCode) : resultId(resultId), typeId(typeId), opCode(opCode), string(0) { }
\r
78 explicit Instruction(Op opCode) : resultId(NoResult), typeId(NoType), opCode(opCode), string(0) { }
\r
79 virtual ~Instruction()
\r
83 void addIdOperand(Id id) { operands.push_back(id); }
\r
84 void addImmediateOperand(unsigned int immediate) { operands.push_back(immediate); }
\r
85 void addStringOperand(const char* str)
\r
87 originalString = str;
\r
88 string = new std::vector<unsigned int>;
\r
90 char* wordString = (char*)&word;
\r
91 char* wordPtr = wordString;
\r
98 if (charCount == 4) {
\r
99 string->push_back(word);
\r
100 wordPtr = wordString;
\r
105 // deal with partial last word
\r
106 if (charCount > 0) {
\r
108 for (; charCount < 4; ++charCount)
\r
110 string->push_back(word);
\r
113 Op getOpCode() const { return opCode; }
\r
114 int getNumOperands() const { return (int)operands.size(); }
\r
115 Id getResultId() const { return resultId; }
\r
116 Id getTypeId() const { return typeId; }
\r
117 Id getIdOperand(int op) const { return operands[op]; }
\r
118 unsigned int getImmediateOperand(int op) const { return operands[op]; }
\r
119 const char* getStringOperand() const { return originalString.c_str(); }
\r
121 // Write out the binary form.
\r
122 void dump(std::vector<unsigned int>& out) const
\r
124 // Compute the wordCount
\r
125 unsigned int wordCount = 1;
\r
130 wordCount += (unsigned int)operands.size();
\r
132 wordCount += (unsigned int)string->size();
\r
134 // Write out the beginning of the instruction
\r
135 out.push_back(((wordCount) << WordCountShift) | opCode);
\r
137 out.push_back(typeId);
\r
139 out.push_back(resultId);
\r
141 // Write out the operands
\r
142 for (int op = 0; op < (int)operands.size(); ++op)
\r
143 out.push_back(operands[op]);
\r
145 for (int op = 0; op < (int)string->size(); ++op)
\r
146 out.push_back((*string)[op]);
\r
150 Instruction(const Instruction&);
\r
154 std::vector<Id> operands;
\r
155 std::vector<unsigned int>* string; // usually non-existent
\r
156 std::string originalString; // could be optimized away; convenience for getting string operand
\r
160 // SPIR-V IR block.
\r
165 Block(Id id, Function& parent);
\r
168 // TODO: free instructions
\r
171 Id getId() { return instructions.front()->getResultId(); }
\r
173 Function& getParent() const { return parent; }
\r
174 void addInstruction(Instruction* inst);
\r
175 void addPredecessor(Block* pred) { predecessors.push_back(pred); }
\r
176 void addLocalVariable(Instruction* inst) { localVariables.push_back(inst); }
\r
177 int getNumPredecessors() const { return (int)predecessors.size(); }
\r
178 void setUnreachable() { unreachable = true; }
\r
179 bool isUnreachable() const { return unreachable; }
\r
181 bool isTerminated() const
\r
183 switch (instructions.back()->getOpCode()) {
\r
185 case OpBranchConditional:
\r
189 case OpReturnValue:
\r
196 void dump(std::vector<unsigned int>& out) const
\r
198 // skip the degenerate unreachable blocks
\r
199 // TODO: code gen: skip all unreachable blocks (transitive closure)
\r
200 // (but, until that's done safer to keep non-degenerate unreachable blocks, in case others depend on something)
\r
201 if (unreachable && instructions.size() <= 2)
\r
204 instructions[0]->dump(out);
\r
205 for (int i = 0; i < (int)localVariables.size(); ++i)
\r
206 localVariables[i]->dump(out);
\r
207 for (int i = 1; i < (int)instructions.size(); ++i)
\r
208 instructions[i]->dump(out);
\r
212 Block(const Block&);
\r
213 Block& operator=(Block&);
\r
215 // To enforce keeping parent and ownership in sync:
\r
218 std::vector<Instruction*> instructions;
\r
219 std::vector<Block*> predecessors;
\r
220 std::vector<Instruction*> localVariables;
\r
223 // track whether this block is known to be uncreachable (not necessarily
\r
224 // true for all unreachable blocks, but should be set at least
\r
225 // for the extraneous ones introduced by the builder).
\r
230 // SPIR-V IR Function.
\r
235 Function(Id id, Id resultType, Id functionType, Id firstParam, Module& parent);
\r
236 virtual ~Function()
\r
238 for (int i = 0; i < (int)parameterInstructions.size(); ++i)
\r
239 delete parameterInstructions[i];
\r
241 for (int i = 0; i < (int)blocks.size(); ++i)
\r
244 Id getId() const { return functionInstruction.getResultId(); }
\r
245 Id getParamId(int p) { return parameterInstructions[p]->getResultId(); }
\r
247 void addBlock(Block* block) { blocks.push_back(block); }
\r
248 void popBlock(Block*) { blocks.pop_back(); }
\r
250 Module& getParent() const { return parent; }
\r
251 Block* getEntryBlock() const { return blocks.front(); }
\r
252 Block* getLastBlock() const { return blocks.back(); }
\r
253 void addLocalVariable(Instruction* inst);
\r
254 Id getReturnType() const { return functionInstruction.getTypeId(); }
\r
255 void dump(std::vector<unsigned int>& out) const
\r
258 functionInstruction.dump(out);
\r
260 // OpFunctionParameter
\r
261 for (int p = 0; p < (int)parameterInstructions.size(); ++p)
\r
262 parameterInstructions[p]->dump(out);
\r
265 for (int b = 0; b < (int)blocks.size(); ++b)
\r
266 blocks[b]->dump(out);
\r
267 Instruction end(0, 0, OpFunctionEnd);
\r
272 Function(const Function&);
\r
273 Function& operator=(Function&);
\r
276 Instruction functionInstruction;
\r
277 std::vector<Instruction*> parameterInstructions;
\r
278 std::vector<Block*> blocks;
\r
282 // SPIR-V IR Module.
\r
290 // TODO delete things
\r
293 void addFunction(Function *fun) { functions.push_back(fun); }
\r
295 void mapInstruction(Instruction *instruction)
\r
297 spv::Id resultId = instruction->getResultId();
\r
298 // map the instruction's result id
\r
299 if (resultId >= idToInstruction.size())
\r
300 idToInstruction.resize(resultId + 16);
\r
301 idToInstruction[resultId] = instruction;
\r
304 Instruction* getInstruction(Id id) const { return idToInstruction[id]; }
\r
305 spv::Id getTypeId(Id resultId) const { return idToInstruction[resultId]->getTypeId(); }
\r
306 StorageClass getStorageClass(Id typeId) const { return (StorageClass)idToInstruction[typeId]->getImmediateOperand(0); }
\r
307 void dump(std::vector<unsigned int>& out) const
\r
309 for (int f = 0; f < (int)functions.size(); ++f)
\r
310 functions[f]->dump(out);
\r
314 Module(const Module&);
\r
315 std::vector<Function*> functions;
\r
317 // map from result id to instruction having that result id
\r
318 std::vector<Instruction*> idToInstruction;
\r
320 // map from a result id to its type id
\r
324 // Implementation (it's here due to circular type definitions).
\r
328 // - the OpFunction instruction
\r
329 // - all the OpFunctionParameter instructions
\r
330 __inline Function::Function(Id id, Id resultType, Id functionType, Id firstParamId, Module& parent)
\r
331 : parent(parent), functionInstruction(id, resultType, OpFunction)
\r
334 functionInstruction.addImmediateOperand(FunctionControlMaskNone);
\r
335 functionInstruction.addIdOperand(functionType);
\r
336 parent.mapInstruction(&functionInstruction);
\r
337 parent.addFunction(this);
\r
339 // OpFunctionParameter
\r
340 Instruction* typeInst = parent.getInstruction(functionType);
\r
341 int numParams = typeInst->getNumOperands() - 1;
\r
342 for (int p = 0; p < numParams; ++p) {
\r
343 Instruction* param = new Instruction(firstParamId + p, typeInst->getIdOperand(p + 1), OpFunctionParameter);
\r
344 parent.mapInstruction(param);
\r
345 parameterInstructions.push_back(param);
\r
349 __inline void Function::addLocalVariable(Instruction* inst)
\r
351 blocks[0]->addLocalVariable(inst);
\r
352 parent.mapInstruction(inst);
\r
355 __inline Block::Block(Id id, Function& parent) : parent(parent), unreachable(false)
\r
357 instructions.push_back(new Instruction(id, NoType, OpLabel));
\r
360 __inline void Block::addInstruction(Instruction* inst)
\r
362 instructions.push_back(inst);
\r
363 if (inst->getResultId())
\r
364 parent.getParent().mapInstruction(inst);
\r
367 }; // end spv namespace
\r