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