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
41 // Simple in-memory representation (IR) of SPIRV. Just for holding
42 // Each function's CFG of blocks. Has this hierarchy:
43 // - Module, which is a list of
44 // - Function, which is a list of
45 // - Block, which is a list of
65 const Id NoResult = 0;
68 const unsigned int BadValue = 0xFFFFFFFF;
69 const Decoration NoPrecision = (Decoration)BadValue;
70 const MemorySemanticsMask MemorySemanticsAllMemory = (MemorySemanticsMask)0x3FF;
73 // SPIR-V IR instruction.
78 Instruction(Id resultId, Id typeId, Op opCode) : resultId(resultId), typeId(typeId), opCode(opCode) { }
79 explicit Instruction(Op opCode) : resultId(NoResult), typeId(NoType), opCode(opCode) { }
80 virtual ~Instruction() {}
81 void addIdOperand(Id id) { operands.push_back(id); }
82 void addImmediateOperand(unsigned int immediate) { operands.push_back(immediate); }
83 void addStringOperand(const char* str)
87 char* wordString = (char*)&word;
88 char* wordPtr = wordString;
96 addImmediateOperand(word);
102 // deal with partial last word
105 for (; charCount < 4; ++charCount)
107 addImmediateOperand(word);
110 Op getOpCode() const { return opCode; }
111 int getNumOperands() const { return (int)operands.size(); }
112 Id getResultId() const { return resultId; }
113 Id getTypeId() const { return typeId; }
114 Id getIdOperand(int op) const { return operands[op]; }
115 unsigned int getImmediateOperand(int op) const { return operands[op]; }
116 const char* getStringOperand() const { return originalString.c_str(); }
118 // Write out the binary form.
119 void dump(std::vector<unsigned int>& out) const
121 // Compute the wordCount
122 unsigned int wordCount = 1;
127 wordCount += (unsigned int)operands.size();
129 // Write out the beginning of the instruction
130 out.push_back(((wordCount) << WordCountShift) | opCode);
132 out.push_back(typeId);
134 out.push_back(resultId);
136 // Write out the operands
137 for (int op = 0; op < (int)operands.size(); ++op)
138 out.push_back(operands[op]);
142 Instruction(const Instruction&);
146 std::vector<Id> operands;
147 std::string originalString; // could be optimized away; convenience for getting string operand
156 Block(Id id, Function& parent);
161 Id getId() { return instructions.front()->getResultId(); }
163 Function& getParent() const { return parent; }
164 void addInstruction(std::unique_ptr<Instruction> inst);
165 void addPredecessor(Block* pred) { predecessors.push_back(pred); pred->successors.push_back(this);}
166 void addLocalVariable(std::unique_ptr<Instruction> inst) { localVariables.push_back(std::move(inst)); }
167 const std::vector<Block*> getPredecessors() const { return predecessors; }
168 const std::vector<Block*> getSuccessors() const { return successors; }
169 void setUnreachable() { unreachable = true; }
170 bool isUnreachable() const { return unreachable; }
172 bool isTerminated() const
174 switch (instructions.back()->getOpCode()) {
176 case OpBranchConditional:
187 void dump(std::vector<unsigned int>& out) const
189 // skip the degenerate unreachable blocks
190 // TODO: code gen: skip all unreachable blocks (transitive closure)
191 // (but, until that's done safer to keep non-degenerate unreachable blocks, in case others depend on something)
192 if (unreachable && instructions.size() <= 2)
195 instructions[0]->dump(out);
196 for (int i = 0; i < (int)localVariables.size(); ++i)
197 localVariables[i]->dump(out);
198 for (int i = 1; i < (int)instructions.size(); ++i)
199 instructions[i]->dump(out);
204 Block& operator=(Block&);
206 // To enforce keeping parent and ownership in sync:
209 std::vector<std::unique_ptr<Instruction> > instructions;
210 std::vector<Block*> predecessors, successors;
211 std::vector<std::unique_ptr<Instruction> > localVariables;
214 // track whether this block is known to be uncreachable (not necessarily
215 // true for all unreachable blocks, but should be set at least
216 // for the extraneous ones introduced by the builder).
221 // SPIR-V IR Function.
226 Function(Id id, Id resultType, Id functionType, Id firstParam, Module& parent);
229 for (int i = 0; i < (int)parameterInstructions.size(); ++i)
230 delete parameterInstructions[i];
232 for (int i = 0; i < (int)blocks.size(); ++i)
235 Id getId() const { return functionInstruction.getResultId(); }
236 Id getParamId(int p) { return parameterInstructions[p]->getResultId(); }
238 void addBlock(Block* block) { blocks.push_back(block); }
239 void popBlock(Block*) { blocks.pop_back(); }
241 Module& getParent() const { return parent; }
242 Block* getEntryBlock() const { return blocks.front(); }
243 Block* getLastBlock() const { return blocks.back(); }
244 void addLocalVariable(std::unique_ptr<Instruction> inst);
245 Id getReturnType() const { return functionInstruction.getTypeId(); }
246 void dump(std::vector<unsigned int>& out) const
249 functionInstruction.dump(out);
251 // OpFunctionParameter
252 for (int p = 0; p < (int)parameterInstructions.size(); ++p)
253 parameterInstructions[p]->dump(out);
256 for (int b = 0; b < (int)blocks.size(); ++b)
257 blocks[b]->dump(out);
258 Instruction end(0, 0, OpFunctionEnd);
263 Function(const Function&);
264 Function& operator=(Function&);
267 Instruction functionInstruction;
268 std::vector<Instruction*> parameterInstructions;
269 std::vector<Block*> blocks;
281 // TODO delete things
284 void addFunction(Function *fun) { functions.push_back(fun); }
286 void mapInstruction(Instruction *instruction)
288 spv::Id resultId = instruction->getResultId();
289 // map the instruction's result id
290 if (resultId >= idToInstruction.size())
291 idToInstruction.resize(resultId + 16);
292 idToInstruction[resultId] = instruction;
295 Instruction* getInstruction(Id id) const { return idToInstruction[id]; }
296 spv::Id getTypeId(Id resultId) const { return idToInstruction[resultId]->getTypeId(); }
297 StorageClass getStorageClass(Id typeId) const
299 assert(idToInstruction[typeId]->getOpCode() == spv::OpTypePointer);
300 return (StorageClass)idToInstruction[typeId]->getImmediateOperand(0);
303 void dump(std::vector<unsigned int>& out) const
305 for (int f = 0; f < (int)functions.size(); ++f)
306 functions[f]->dump(out);
310 Module(const Module&);
311 std::vector<Function*> functions;
313 // map from result id to instruction having that result id
314 std::vector<Instruction*> idToInstruction;
316 // map from a result id to its type id
320 // Implementation (it's here due to circular type definitions).
324 // - the OpFunction instruction
325 // - all the OpFunctionParameter instructions
326 __inline Function::Function(Id id, Id resultType, Id functionType, Id firstParamId, Module& parent)
327 : parent(parent), functionInstruction(id, resultType, OpFunction)
330 functionInstruction.addImmediateOperand(FunctionControlMaskNone);
331 functionInstruction.addIdOperand(functionType);
332 parent.mapInstruction(&functionInstruction);
333 parent.addFunction(this);
335 // OpFunctionParameter
336 Instruction* typeInst = parent.getInstruction(functionType);
337 int numParams = typeInst->getNumOperands() - 1;
338 for (int p = 0; p < numParams; ++p) {
339 Instruction* param = new Instruction(firstParamId + p, typeInst->getIdOperand(p + 1), OpFunctionParameter);
340 parent.mapInstruction(param);
341 parameterInstructions.push_back(param);
345 __inline void Function::addLocalVariable(std::unique_ptr<Instruction> inst)
347 Instruction* raw_instruction = inst.get();
348 blocks[0]->addLocalVariable(std::move(inst));
349 parent.mapInstruction(raw_instruction);
352 __inline Block::Block(Id id, Function& parent) : parent(parent), unreachable(false)
354 instructions.push_back(std::unique_ptr<Instruction>(new Instruction(id, NoType, OpLabel)));
357 __inline void Block::addInstruction(std::unique_ptr<Instruction> inst)
359 Instruction* raw_instruction = inst.get();
360 instructions.push_back(std::move(inst));
361 if (raw_instruction->getResultId())
362 parent.getParent().mapInstruction(raw_instruction);
365 }; // end spv namespace