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 // SPIR-V IR instruction.
\r
69 Instruction(Id resultId, Id typeId, OpCode opCode) : resultId(resultId), typeId(typeId), opCode(opCode), string(0) { }
\r
70 explicit Instruction(OpCode opCode) : resultId(NoResult), typeId(NoType), opCode(opCode), string(0) { }
\r
71 virtual ~Instruction()
\r
75 void addIdOperand(Id id) { operands.push_back(id); }
\r
76 void addImmediateOperand(unsigned int immediate) { operands.push_back(immediate); }
\r
77 void addStringOperand(const char* str)
\r
79 string = new std::vector<unsigned int>;
\r
81 char* wordString = (char*)&word;
\r
82 char* wordPtr = wordString;
\r
89 if (charCount == 4) {
\r
90 string->push_back(word);
\r
91 wordPtr = wordString;
\r
96 // deal with partial last word
\r
97 if (charCount > 0) {
\r
99 for (; charCount < 4; ++charCount)
\r
101 string->push_back(word);
\r
104 originalString = str;
\r
106 OpCode getOpCode() const { return opCode; }
\r
107 int getNumOperands() const { return operands.size(); }
\r
108 Id getResultId() const { return resultId; }
\r
109 Id getTypeId() const { return typeId; }
\r
110 Id getIdOperand(int op) const { return operands[op]; }
\r
111 unsigned int getImmediateOperand(int op) const { return operands[op]; }
\r
112 const char* getStringOperand() const { return originalString.c_str(); }
\r
114 // Write out the binary form.
\r
115 void dump(std::vector<unsigned int>& out) const
\r
117 // Compute the wordCount
\r
118 unsigned int wordCount = 1;
\r
123 wordCount += operands.size();
\r
125 wordCount += string->size();
\r
127 // Write out the beginning of the instruction
\r
128 out.push_back(((wordCount) << WordCountShift) | opCode);
\r
130 out.push_back(typeId);
\r
132 out.push_back(resultId);
\r
134 // Write out the operands
\r
135 for (int op = 0; op < (int)operands.size(); ++op)
\r
136 out.push_back(operands[op]);
\r
138 for (int op = 0; op < (int)string->size(); ++op)
\r
139 out.push_back((*string)[op]);
\r
143 Instruction(const Instruction&);
\r
147 std::vector<Id> operands;
\r
148 std::vector<unsigned int>* string; // usually non-existent
\r
149 std::string originalString; // could be optimized away; convenience for getting string operand
\r
153 // SPIR-V IR block.
\r
158 // Setting insert to true indicates to add this new block
\r
159 // to the end of the parent function.
\r
160 Block(Id id, Function& parent);
\r
163 // TODO: free instructions
\r
166 Id getId() { return instructions.front()->getResultId(); }
\r
168 Function& getParent() const { return parent; }
\r
169 void addInstruction(Instruction* inst);
\r
170 void addPredecessor(Block* pred) { predecessors.push_back(pred); }
\r
171 void addLocalVariable(Instruction* inst) { localVariables.push_back(inst); }
\r
172 int getNumPredecessors() const { return (int)predecessors.size(); }
\r
174 bool isTerminated() const
\r
176 switch (instructions.back()->getOpCode()) {
\r
178 case OpBranchConditional:
\r
182 case OpReturnValue:
\r
189 void dump(std::vector<unsigned int>& out) const
\r
191 instructions[0]->dump(out);
\r
192 for (int i = 0; i < (int)localVariables.size(); ++i)
\r
193 localVariables[i]->dump(out);
\r
194 for (int i = 1; i < (int)instructions.size(); ++i)
\r
195 instructions[i]->dump(out);
\r
199 Block(const Block&);
\r
201 // To enforce keeping parent and ownership in sync:
\r
204 std::vector<Instruction*> instructions;
\r
205 std::vector<Block*> predecessors;
\r
206 std::vector<Instruction*> localVariables;
\r
211 // SPIR-V IR Function.
\r
216 Function(Id id, Id resultType, Id functionType, Id firstParam, Module& parent);
\r
217 virtual ~Function()
\r
219 for (int i = 0; i < (int)parameterInstructions.size(); ++i)
\r
220 delete parameterInstructions[i];
\r
222 for (int i = 0; i < (int)blocks.size(); ++i)
\r
225 Id getId() const { return functionInstruction.getResultId(); }
\r
226 Id getParamId(int p) { return parameterInstructions[p]->getResultId(); }
\r
228 void addBlock(Block* block) { blocks.push_back(block); }
\r
229 void popBlock(Block* block) { assert(blocks.back() == block); blocks.pop_back(); }
\r
231 Module& getParent() const { return parent; }
\r
232 Block* getEntryBlock() const { return blocks.front(); }
\r
233 Block* getLastBlock() const { return blocks.back(); }
\r
234 void addLocalVariable(Instruction* inst);
\r
235 Id getReturnType() const { return functionInstruction.getTypeId(); }
\r
236 void dump(std::vector<unsigned int>& out) const
\r
239 functionInstruction.dump(out);
\r
241 // OpFunctionParameter
\r
242 for (int p = 0; p < (int)parameterInstructions.size(); ++p)
\r
243 parameterInstructions[p]->dump(out);
\r
246 for (int b = 0; b < (int)blocks.size(); ++b)
\r
247 blocks[b]->dump(out);
\r
248 Instruction end(0, 0, OpFunctionEnd);
\r
253 Function(const Function&);
\r
255 Instruction functionInstruction;
\r
256 std::vector<Instruction*> parameterInstructions;
\r
257 std::vector<Block*> blocks;
\r
261 // SPIR-V IR Module.
\r
269 // TODO delete things
\r
272 void addFunction(Function *fun) { functions.push_back(fun); }
\r
274 void mapInstruction(Instruction *instruction)
\r
276 spv::Id resultId = instruction->getResultId();
\r
277 // map the instruction's result id
\r
278 if (resultId >= idToInstruction.size())
\r
279 idToInstruction.resize(resultId + 16);
\r
280 idToInstruction[resultId] = instruction;
\r
283 Instruction* getInstruction(Id id) const { return idToInstruction[id]; }
\r
284 spv::Id getTypeId(Id resultId) const { return idToInstruction[resultId]->getTypeId(); }
\r
285 StorageClass getStorageClass(Id typeId) const { return (StorageClass)idToInstruction[typeId]->getImmediateOperand(0); }
\r
286 void dump(std::vector<unsigned int>& out) const
\r
288 for (int f = 0; f < (int)functions.size(); ++f)
\r
289 functions[f]->dump(out);
\r
293 Module(const Module&);
\r
294 std::vector<Function*> functions;
\r
296 // map from result id to instruction having that result id
\r
297 std::vector<Instruction*> idToInstruction;
\r
299 // map from a result id to its type id
\r
303 // Implementation (it's here due to circular type definitions).
\r
307 // - the OpFunction instruction
\r
308 // - all the OpFunctionParameter instructions
\r
309 __inline Function::Function(Id id, Id resultType, Id functionType, Id firstParamId, Module& parent)
\r
310 : parent(parent), functionInstruction(id, resultType, OpFunction)
\r
313 functionInstruction.addImmediateOperand(FunctionControlNone);
\r
314 functionInstruction.addIdOperand(functionType);
\r
315 parent.mapInstruction(&functionInstruction);
\r
316 parent.addFunction(this);
\r
318 // OpFunctionParameter
\r
319 Instruction* typeInst = parent.getInstruction(functionType);
\r
320 int numParams = typeInst->getNumOperands() - 1;
\r
321 for (int p = 0; p < numParams; ++p) {
\r
322 Instruction* param = new Instruction(firstParamId + p, typeInst->getIdOperand(p + 1), OpFunctionParameter);
\r
323 parent.mapInstruction(param);
\r
324 parameterInstructions.push_back(param);
\r
328 __inline void Function::addLocalVariable(Instruction* inst)
\r
330 blocks[0]->addLocalVariable(inst);
\r
331 parent.mapInstruction(inst);
\r
334 __inline Block::Block(Id id, Function& parent) : parent(parent)
\r
336 instructions.push_back(new Instruction(id, NoType, OpLabel));
\r
339 __inline void Block::addInstruction(Instruction* inst)
\r
341 instructions.push_back(inst);
\r
342 if (inst->getResultId())
\r
343 parent.getParent().mapInstruction(inst);
\r
346 }; // end spv namespace
\r