blob: 5bea5967407ef9873bc54de626fbbb940cc98326 [file] [log] [blame]
John Kessenich0df0cde2015-03-03 17:09:43 +00001//
2//Copyright (C) 2014 LunarG, Inc.
3//
4//All rights reserved.
5//
6//Redistribution and use in source and binary forms, with or without
7//modification, are permitted provided that the following conditions
8//are met:
9//
10// Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12//
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.
17//
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.
21//
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.
34
35//
36// Author: John Kessenich, LunarG
37//
38
39// SPIRV-IR
40//
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
46// - Instruction
47//
48
49#pragma once
50#ifndef spvIR_H
51#define spvIR_H
52
53#include "spirv.h"
54
55#include <vector>
56#include <iostream>
57
58namespace spv {
59
60class Function;
61class Module;
62
63//
64// SPIR-V IR instruction.
65//
66
67class Instruction {
68public:
69 Instruction(Id resultId, Id typeId, OpCode opCode) : resultId(resultId), typeId(typeId), opCode(opCode), string(0) { }
70 explicit Instruction(OpCode opCode) : resultId(NoResult), typeId(NoType), opCode(opCode), string(0) { }
71 virtual ~Instruction()
72 {
73 delete string;
74 }
75 void addIdOperand(Id id) { operands.push_back(id); }
76 void addImmediateOperand(unsigned int immediate) { operands.push_back(immediate); }
77 void addStringOperand(const char* str)
78 {
79 string = new std::vector<unsigned int>;
80 unsigned int word;
81 char* wordString = (char*)&word;
82 char* wordPtr = wordString;
83 int charCount = 0;
84 char c;
85 do {
86 c = *(str++);
87 *(wordPtr++) = c;
88 ++charCount;
89 if (charCount == 4) {
90 string->push_back(word);
91 wordPtr = wordString;
92 charCount = 0;
93 }
94 } while (c != 0);
95
96 // deal with partial last word
97 if (charCount > 0) {
98 // pad with 0s
99 for (; charCount < 4; ++charCount)
100 *(wordPtr++) = 0;
101 string->push_back(word);
102 }
103
104 originalString = str;
105 }
106 OpCode getOpCode() const { return opCode; }
107 int getNumOperands() const { return operands.size(); }
108 Id getResultId() const { return resultId; }
109 Id getTypeId() const { return typeId; }
110 Id getIdOperand(int op) const { return operands[op]; }
111 unsigned int getImmediateOperand(int op) const { return operands[op]; }
112 const char* getStringOperand() const { return originalString.c_str(); }
113
114 // Write out the binary form.
115 void dump(std::vector<unsigned int>& out) const
116 {
117 // Compute the wordCount
118 unsigned int wordCount = 1;
119 if (typeId)
120 ++wordCount;
121 if (resultId)
122 ++wordCount;
123 wordCount += operands.size();
124 if (string)
125 wordCount += string->size();
126
127 // Write out the beginning of the instruction
128 out.push_back(((wordCount) << WordCountShift) | opCode);
129 if (typeId)
130 out.push_back(typeId);
131 if (resultId)
132 out.push_back(resultId);
133
134 // Write out the operands
135 for (int op = 0; op < (int)operands.size(); ++op)
136 out.push_back(operands[op]);
137 if (string)
138 for (int op = 0; op < (int)string->size(); ++op)
139 out.push_back((*string)[op]);
140 }
141
142protected:
143 Instruction(const Instruction&);
144 Id resultId;
145 Id typeId;
146 OpCode opCode;
147 std::vector<Id> operands;
148 std::vector<unsigned int>* string; // usually non-existent
149 std::string originalString; // could be optimized away; convenience for getting string operand
150};
151
152//
153// SPIR-V IR block.
154//
155
156class Block {
157public:
158 // Setting insert to true indicates to add this new block
159 // to the end of the parent function.
160 Block(Id id, Function& parent);
161 virtual ~Block()
162 {
163 // TODO: free instructions
164 }
165
166 Id getId() { return instructions.front()->getResultId(); }
167
168 Function& getParent() const { return parent; }
169 void addInstruction(Instruction* inst);
170 void addPredecessor(Block* pred) { predecessors.push_back(pred); }
171 void addLocalVariable(Instruction* inst) { localVariables.push_back(inst); }
172 int getNumPredecessors() const { return (int)predecessors.size(); }
173
174 bool isTerminated() const
175 {
176 switch (instructions.back()->getOpCode()) {
177 case OpBranch:
178 case OpBranchConditional:
179 case OpSwitch:
180 case OpKill:
181 case OpReturn:
182 case OpReturnValue:
183 return true;
184 default:
185 return false;
186 }
187 }
188
189 void dump(std::vector<unsigned int>& out) const
190 {
191 instructions[0]->dump(out);
192 for (int i = 0; i < (int)localVariables.size(); ++i)
193 localVariables[i]->dump(out);
194 for (int i = 1; i < (int)instructions.size(); ++i)
195 instructions[i]->dump(out);
196 }
197
198protected:
199 Block(const Block&);
200
201 // To enforce keeping parent and ownership in sync:
202 friend Function;
203
204 std::vector<Instruction*> instructions;
205 std::vector<Block*> predecessors;
206 std::vector<Instruction*> localVariables;
207 Function& parent;
208};
209
210//
211// SPIR-V IR Function.
212//
213
214class Function {
215public:
216 Function(Id id, Id resultType, Id functionType, Id firstParam, Module& parent);
217 virtual ~Function()
218 {
219 for (int i = 0; i < (int)parameterInstructions.size(); ++i)
220 delete parameterInstructions[i];
221
222 for (int i = 0; i < (int)blocks.size(); ++i)
223 delete blocks[i];
224 }
225 Id getId() const { return functionInstruction.getResultId(); }
226 Id getParamId(int p) { return parameterInstructions[p]->getResultId(); }
227
228 void addBlock(Block* block) { blocks.push_back(block); }
229 void popBlock(Block* block) { assert(blocks.back() == block); blocks.pop_back(); }
230
231 Module& getParent() const { return parent; }
232 Block* getEntryBlock() const { return blocks.front(); }
233 Block* getLastBlock() const { return blocks.back(); }
234 void addLocalVariable(Instruction* inst);
235 Id getReturnType() const { return functionInstruction.getTypeId(); }
236 void dump(std::vector<unsigned int>& out) const
237 {
238 // OpFunction
239 functionInstruction.dump(out);
240
241 // OpFunctionParameter
242 for (int p = 0; p < (int)parameterInstructions.size(); ++p)
243 parameterInstructions[p]->dump(out);
244
245 // Blocks
246 for (int b = 0; b < (int)blocks.size(); ++b)
247 blocks[b]->dump(out);
248 Instruction end(0, 0, OpFunctionEnd);
249 end.dump(out);
250 }
251
252protected:
253 Function(const Function&);
254 Module& parent;
255 Instruction functionInstruction;
256 std::vector<Instruction*> parameterInstructions;
257 std::vector<Block*> blocks;
258};
259
260//
261// SPIR-V IR Module.
262//
263
264class Module {
265public:
266 Module() {}
267 virtual ~Module()
268 {
269 // TODO delete things
270 }
271
272 void addFunction(Function *fun) { functions.push_back(fun); }
273
274 void mapInstruction(Instruction *instruction)
275 {
276 spv::Id resultId = instruction->getResultId();
277 // map the instruction's result id
278 if (resultId >= idToInstruction.size())
279 idToInstruction.resize(resultId + 16);
280 idToInstruction[resultId] = instruction;
281 }
282
283 Instruction* getInstruction(Id id) const { return idToInstruction[id]; }
284 spv::Id getTypeId(Id resultId) const { return idToInstruction[resultId]->getTypeId(); }
285 StorageClass getStorageClass(Id typeId) const { return (StorageClass)idToInstruction[typeId]->getImmediateOperand(0); }
286 void dump(std::vector<unsigned int>& out) const
287 {
288 for (int f = 0; f < (int)functions.size(); ++f)
289 functions[f]->dump(out);
290 }
291
292protected:
293 Module(const Module&);
294 std::vector<Function*> functions;
295
296 // map from result id to instruction having that result id
297 std::vector<Instruction*> idToInstruction;
298
299 // map from a result id to its type id
300};
301
302//
303// Implementation (it's here due to circular type definitions).
304//
305
306// Add both
307// - the OpFunction instruction
308// - all the OpFunctionParameter instructions
309__inline Function::Function(Id id, Id resultType, Id functionType, Id firstParamId, Module& parent)
310 : parent(parent), functionInstruction(id, resultType, OpFunction)
311{
312 // OpFunction
313 functionInstruction.addImmediateOperand(FunctionControlNone);
314 functionInstruction.addIdOperand(functionType);
315 parent.mapInstruction(&functionInstruction);
316 parent.addFunction(this);
317
318 // OpFunctionParameter
319 Instruction* typeInst = parent.getInstruction(functionType);
320 int numParams = typeInst->getNumOperands() - 1;
321 for (int p = 0; p < numParams; ++p) {
322 Instruction* param = new Instruction(firstParamId + p, typeInst->getIdOperand(p + 1), OpFunctionParameter);
323 parent.mapInstruction(param);
324 parameterInstructions.push_back(param);
325 }
326}
327
328__inline void Function::addLocalVariable(Instruction* inst)
329{
330 blocks[0]->addLocalVariable(inst);
331 parent.mapInstruction(inst);
332}
333
334__inline Block::Block(Id id, Function& parent) : parent(parent)
335{
336 instructions.push_back(new Instruction(id, NoType, OpLabel));
337}
338
339__inline void Block::addInstruction(Instruction* inst)
340{
341 instructions.push_back(inst);
342 if (inst->getResultId())
343 parent.getParent().mapInstruction(inst);
344}
345
346}; // end spv namespace
347
348#endif // spvIR_H