blob: 718b3d507a51e976f43dfbd2e7c717bb1f952b0c [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>
John Kessenich11f5fc02015-05-07 01:04:29 +000057#include <assert.h>
John Kessenich0df0cde2015-03-03 17:09:43 +000058
59namespace spv {
60
61class Function;
62class Module;
63
John Kessenichb40d6ac2015-03-30 17:41:16 +000064const Id NoResult = 0;
65const Id NoType = 0;
66
67const unsigned int BadValue = 0xFFFFFFFF;
68const Decoration NoPrecision = (Decoration)BadValue;
69const MemorySemanticsMask MemorySemanticsAllMemory = (MemorySemanticsMask)0x3FF;
70
John Kessenich0df0cde2015-03-03 17:09:43 +000071//
72// SPIR-V IR instruction.
73//
74
75class Instruction {
76public:
John Kessenichb40d6ac2015-03-30 17:41:16 +000077 Instruction(Id resultId, Id typeId, Op opCode) : resultId(resultId), typeId(typeId), opCode(opCode), string(0) { }
78 explicit Instruction(Op opCode) : resultId(NoResult), typeId(NoType), opCode(opCode), string(0) { }
John Kessenich0df0cde2015-03-03 17:09:43 +000079 virtual ~Instruction()
80 {
81 delete string;
82 }
83 void addIdOperand(Id id) { operands.push_back(id); }
84 void addImmediateOperand(unsigned int immediate) { operands.push_back(immediate); }
85 void addStringOperand(const char* str)
86 {
John Kessenich1cfc71b2015-05-08 00:45:31 +000087 originalString = str;
John Kessenich0df0cde2015-03-03 17:09:43 +000088 string = new std::vector<unsigned int>;
89 unsigned int word;
90 char* wordString = (char*)&word;
91 char* wordPtr = wordString;
92 int charCount = 0;
93 char c;
94 do {
95 c = *(str++);
96 *(wordPtr++) = c;
97 ++charCount;
98 if (charCount == 4) {
99 string->push_back(word);
100 wordPtr = wordString;
101 charCount = 0;
102 }
103 } while (c != 0);
104
105 // deal with partial last word
106 if (charCount > 0) {
107 // pad with 0s
108 for (; charCount < 4; ++charCount)
109 *(wordPtr++) = 0;
110 string->push_back(word);
111 }
John Kessenich0df0cde2015-03-03 17:09:43 +0000112 }
John Kessenichb40d6ac2015-03-30 17:41:16 +0000113 Op getOpCode() const { return opCode; }
John Kessenich3a44d7f2015-05-11 18:50:01 +0000114 int getNumOperands() const { return (int)operands.size(); }
John Kessenich0df0cde2015-03-03 17:09:43 +0000115 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;
John Kessenich3a44d7f2015-05-11 18:50:01 +0000130 wordCount += (unsigned int)operands.size();
John Kessenich0df0cde2015-03-03 17:09:43 +0000131 if (string)
John Kessenich3a44d7f2015-05-11 18:50:01 +0000132 wordCount += (unsigned int)string->size();
John Kessenich0df0cde2015-03-03 17:09:43 +0000133
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:
John Kessenich0df0cde2015-03-03 17:09:43 +0000165 Block(Id id, Function& parent);
166 virtual ~Block()
167 {
168 // TODO: free instructions
169 }
170
171 Id getId() { return instructions.front()->getResultId(); }
172
173 Function& getParent() const { return parent; }
174 void addInstruction(Instruction* inst);
175 void addPredecessor(Block* pred) { predecessors.push_back(pred); }
176 void addLocalVariable(Instruction* inst) { localVariables.push_back(inst); }
177 int getNumPredecessors() const { return (int)predecessors.size(); }
John Kessenich735a2ef2015-05-03 22:38:16 +0000178 void setUnreachable() { unreachable = true; }
179 bool isUnreachable() const { return unreachable; }
John Kessenich0df0cde2015-03-03 17:09:43 +0000180
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 {
John Kessenich735a2ef2015-05-03 22:38:16 +0000198 // skip the degenerate unreachable blocks
199 // TODO: code gen: skip all unreachable blocks (transitive closure)
200 // (but, until that's done safer to keep non-degenerate unreachable blocks, in case others depend on something)
201 if (unreachable && instructions.size() <= 2)
202 return;
203
John Kessenich0df0cde2015-03-03 17:09:43 +0000204 instructions[0]->dump(out);
205 for (int i = 0; i < (int)localVariables.size(); ++i)
206 localVariables[i]->dump(out);
207 for (int i = 1; i < (int)instructions.size(); ++i)
208 instructions[i]->dump(out);
209 }
210
211protected:
212 Block(const Block&);
213
214 // To enforce keeping parent and ownership in sync:
215 friend Function;
216
217 std::vector<Instruction*> instructions;
218 std::vector<Block*> predecessors;
219 std::vector<Instruction*> localVariables;
220 Function& parent;
John Kessenich735a2ef2015-05-03 22:38:16 +0000221
222 // track whether this block is known to be uncreachable (not necessarily
223 // true for all unreachable blocks, but should be set at least
224 // for the extraneous ones introduced by the builder).
225 bool unreachable;
John Kessenich0df0cde2015-03-03 17:09:43 +0000226};
227
228//
229// SPIR-V IR Function.
230//
231
232class Function {
233public:
234 Function(Id id, Id resultType, Id functionType, Id firstParam, Module& parent);
235 virtual ~Function()
236 {
237 for (int i = 0; i < (int)parameterInstructions.size(); ++i)
238 delete parameterInstructions[i];
239
240 for (int i = 0; i < (int)blocks.size(); ++i)
241 delete blocks[i];
242 }
243 Id getId() const { return functionInstruction.getResultId(); }
244 Id getParamId(int p) { return parameterInstructions[p]->getResultId(); }
245
246 void addBlock(Block* block) { blocks.push_back(block); }
247 void popBlock(Block* block) { assert(blocks.back() == block); blocks.pop_back(); }
248
249 Module& getParent() const { return parent; }
250 Block* getEntryBlock() const { return blocks.front(); }
251 Block* getLastBlock() const { return blocks.back(); }
252 void addLocalVariable(Instruction* inst);
253 Id getReturnType() const { return functionInstruction.getTypeId(); }
254 void dump(std::vector<unsigned int>& out) const
255 {
256 // OpFunction
257 functionInstruction.dump(out);
258
259 // OpFunctionParameter
260 for (int p = 0; p < (int)parameterInstructions.size(); ++p)
261 parameterInstructions[p]->dump(out);
262
263 // Blocks
264 for (int b = 0; b < (int)blocks.size(); ++b)
265 blocks[b]->dump(out);
266 Instruction end(0, 0, OpFunctionEnd);
267 end.dump(out);
268 }
269
270protected:
271 Function(const Function&);
272 Module& parent;
273 Instruction functionInstruction;
274 std::vector<Instruction*> parameterInstructions;
275 std::vector<Block*> blocks;
276};
277
278//
279// SPIR-V IR Module.
280//
281
282class Module {
283public:
284 Module() {}
285 virtual ~Module()
286 {
287 // TODO delete things
288 }
289
290 void addFunction(Function *fun) { functions.push_back(fun); }
291
292 void mapInstruction(Instruction *instruction)
293 {
294 spv::Id resultId = instruction->getResultId();
295 // map the instruction's result id
296 if (resultId >= idToInstruction.size())
297 idToInstruction.resize(resultId + 16);
298 idToInstruction[resultId] = instruction;
299 }
300
301 Instruction* getInstruction(Id id) const { return idToInstruction[id]; }
302 spv::Id getTypeId(Id resultId) const { return idToInstruction[resultId]->getTypeId(); }
303 StorageClass getStorageClass(Id typeId) const { return (StorageClass)idToInstruction[typeId]->getImmediateOperand(0); }
304 void dump(std::vector<unsigned int>& out) const
305 {
306 for (int f = 0; f < (int)functions.size(); ++f)
307 functions[f]->dump(out);
308 }
309
310protected:
311 Module(const Module&);
312 std::vector<Function*> functions;
313
314 // map from result id to instruction having that result id
315 std::vector<Instruction*> idToInstruction;
316
317 // map from a result id to its type id
318};
319
320//
321// Implementation (it's here due to circular type definitions).
322//
323
324// Add both
325// - the OpFunction instruction
326// - all the OpFunctionParameter instructions
327__inline Function::Function(Id id, Id resultType, Id functionType, Id firstParamId, Module& parent)
328 : parent(parent), functionInstruction(id, resultType, OpFunction)
329{
330 // OpFunction
John Kessenichb40d6ac2015-03-30 17:41:16 +0000331 functionInstruction.addImmediateOperand(FunctionControlMaskNone);
John Kessenich0df0cde2015-03-03 17:09:43 +0000332 functionInstruction.addIdOperand(functionType);
333 parent.mapInstruction(&functionInstruction);
334 parent.addFunction(this);
335
336 // OpFunctionParameter
337 Instruction* typeInst = parent.getInstruction(functionType);
338 int numParams = typeInst->getNumOperands() - 1;
339 for (int p = 0; p < numParams; ++p) {
340 Instruction* param = new Instruction(firstParamId + p, typeInst->getIdOperand(p + 1), OpFunctionParameter);
341 parent.mapInstruction(param);
342 parameterInstructions.push_back(param);
343 }
344}
345
346__inline void Function::addLocalVariable(Instruction* inst)
347{
348 blocks[0]->addLocalVariable(inst);
349 parent.mapInstruction(inst);
350}
351
John Kessenich735a2ef2015-05-03 22:38:16 +0000352__inline Block::Block(Id id, Function& parent) : parent(parent), unreachable(false)
John Kessenich0df0cde2015-03-03 17:09:43 +0000353{
354 instructions.push_back(new Instruction(id, NoType, OpLabel));
355}
356
357__inline void Block::addInstruction(Instruction* inst)
358{
359 instructions.push_back(inst);
360 if (inst->getResultId())
361 parent.getParent().mapInstruction(inst);
362}
363
364}; // end spv namespace
365
366#endif // spvIR_H