blob: 8cfef9e148ab654062e217ab56bb6e979109f17d [file] [log] [blame]
John Kessenich140f3df2015-06-26 16:58:36 -06001//
John Kessenich927608b2017-01-06 12:34:14 -07002// Copyright (C) 2014 LunarG, Inc.
John Kessenich140f3df2015-06-26 16:58:36 -06003//
John Kessenich927608b2017-01-06 12:34:14 -07004// All rights reserved.
John Kessenich140f3df2015-06-26 16:58:36 -06005//
John Kessenich927608b2017-01-06 12:34:14 -07006// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions
8// are met:
John Kessenich140f3df2015-06-26 16:58:36 -06009//
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//
John Kessenich927608b2017-01-06 12:34:14 -070022// 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.
John Kessenich140f3df2015-06-26 16:58:36 -060034
John Kessenich140f3df2015-06-26 16:58:36 -060035// SPIRV-IR
36//
37// Simple in-memory representation (IR) of SPIRV. Just for holding
38// Each function's CFG of blocks. Has this hierarchy:
John Kessenichecba76f2017-01-06 00:34:48 -070039// - Module, which is a list of
40// - Function, which is a list of
41// - Block, which is a list of
John Kessenich140f3df2015-06-26 16:58:36 -060042// - Instruction
43//
44
45#pragma once
46#ifndef spvIR_H
47#define spvIR_H
48
John Kessenich5e4b1242015-08-06 22:53:06 -060049#include "spirv.hpp"
John Kessenich140f3df2015-06-26 16:58:36 -060050
Dejan Mircevskie537b8b2016-01-10 19:37:00 -050051#include <algorithm>
Dejan Mircevski44bfb0d2016-01-18 16:18:37 -050052#include <cassert>
53#include <functional>
John Kessenich140f3df2015-06-26 16:58:36 -060054#include <iostream>
Andrew Woloszynb7946d12016-01-18 09:23:56 -050055#include <memory>
Dejan Mircevski44bfb0d2016-01-18 16:18:37 -050056#include <vector>
John Kessenich140f3df2015-06-26 16:58:36 -060057
58namespace spv {
59
Dejan Mircevski38d039d2016-01-19 10:01:27 -050060class Block;
John Kessenich140f3df2015-06-26 16:58:36 -060061class Function;
62class Module;
63
64const Id NoResult = 0;
65const Id NoType = 0;
66
John Kessenich4016e382016-07-15 11:53:56 -060067const Decoration NoPrecision = DecorationMax;
John Kessenichecba76f2017-01-06 00:34:48 -070068const MemorySemanticsMask MemorySemanticsAllMemory =
John Kessenichc51287d2016-06-14 19:46:20 -060069 (MemorySemanticsMask)(MemorySemanticsSequentiallyConsistentMask |
John Kessenich48891672016-01-22 10:15:03 -070070 MemorySemanticsUniformMemoryMask |
71 MemorySemanticsSubgroupMemoryMask |
72 MemorySemanticsWorkgroupMemoryMask |
73 MemorySemanticsCrossWorkgroupMemoryMask |
74 MemorySemanticsAtomicCounterMemoryMask |
75 MemorySemanticsImageMemoryMask);
John Kessenich140f3df2015-06-26 16:58:36 -060076
77//
78// SPIR-V IR instruction.
79//
80
81class Instruction {
82public:
Dejan Mircevski34bc6c32016-01-19 14:08:32 -050083 Instruction(Id resultId, Id typeId, Op opCode) : resultId(resultId), typeId(typeId), opCode(opCode), block(nullptr) { }
84 explicit Instruction(Op opCode) : resultId(NoResult), typeId(NoType), opCode(opCode), block(nullptr) { }
John Kessenich55e7d112015-11-15 21:33:39 -070085 virtual ~Instruction() {}
John Kessenich140f3df2015-06-26 16:58:36 -060086 void addIdOperand(Id id) { operands.push_back(id); }
87 void addImmediateOperand(unsigned int immediate) { operands.push_back(immediate); }
88 void addStringOperand(const char* str)
89 {
90 originalString = str;
John Kessenich140f3df2015-06-26 16:58:36 -060091 unsigned int word;
92 char* wordString = (char*)&word;
93 char* wordPtr = wordString;
94 int charCount = 0;
95 char c;
96 do {
97 c = *(str++);
98 *(wordPtr++) = c;
99 ++charCount;
100 if (charCount == 4) {
John Kessenich55e7d112015-11-15 21:33:39 -0700101 addImmediateOperand(word);
John Kessenich140f3df2015-06-26 16:58:36 -0600102 wordPtr = wordString;
103 charCount = 0;
104 }
105 } while (c != 0);
106
107 // deal with partial last word
108 if (charCount > 0) {
109 // pad with 0s
110 for (; charCount < 4; ++charCount)
111 *(wordPtr++) = 0;
John Kessenich55e7d112015-11-15 21:33:39 -0700112 addImmediateOperand(word);
John Kessenich140f3df2015-06-26 16:58:36 -0600113 }
114 }
Dejan Mircevski38d039d2016-01-19 10:01:27 -0500115 void setBlock(Block* b) { block = b; }
Dejan Mircevskifa242902016-01-19 11:31:55 -0500116 Block* getBlock() const { return block; }
John Kessenich140f3df2015-06-26 16:58:36 -0600117 Op getOpCode() const { return opCode; }
118 int getNumOperands() const { return (int)operands.size(); }
119 Id getResultId() const { return resultId; }
120 Id getTypeId() const { return typeId; }
121 Id getIdOperand(int op) const { return operands[op]; }
122 unsigned int getImmediateOperand(int op) const { return operands[op]; }
123 const char* getStringOperand() const { return originalString.c_str(); }
124
125 // Write out the binary form.
126 void dump(std::vector<unsigned int>& out) const
127 {
128 // Compute the wordCount
129 unsigned int wordCount = 1;
130 if (typeId)
131 ++wordCount;
132 if (resultId)
133 ++wordCount;
134 wordCount += (unsigned int)operands.size();
John Kessenich140f3df2015-06-26 16:58:36 -0600135
136 // Write out the beginning of the instruction
137 out.push_back(((wordCount) << WordCountShift) | opCode);
138 if (typeId)
139 out.push_back(typeId);
140 if (resultId)
141 out.push_back(resultId);
142
143 // Write out the operands
144 for (int op = 0; op < (int)operands.size(); ++op)
145 out.push_back(operands[op]);
John Kessenich140f3df2015-06-26 16:58:36 -0600146 }
147
148protected:
149 Instruction(const Instruction&);
150 Id resultId;
151 Id typeId;
152 Op opCode;
153 std::vector<Id> operands;
John Kessenich140f3df2015-06-26 16:58:36 -0600154 std::string originalString; // could be optimized away; convenience for getting string operand
Dejan Mircevski38d039d2016-01-19 10:01:27 -0500155 Block* block;
John Kessenich140f3df2015-06-26 16:58:36 -0600156};
157
158//
159// SPIR-V IR block.
160//
161
162class Block {
163public:
164 Block(Id id, Function& parent);
165 virtual ~Block()
166 {
John Kessenich140f3df2015-06-26 16:58:36 -0600167 }
Dejan Mircevski44bfb0d2016-01-18 16:18:37 -0500168
John Kessenich140f3df2015-06-26 16:58:36 -0600169 Id getId() { return instructions.front()->getResultId(); }
170
171 Function& getParent() const { return parent; }
Andrew Woloszynb7946d12016-01-18 09:23:56 -0500172 void addInstruction(std::unique_ptr<Instruction> inst);
Dejan Mircevski5fe789b2016-01-17 23:27:45 -0500173 void addPredecessor(Block* pred) { predecessors.push_back(pred); pred->successors.push_back(this);}
Andrew Woloszynb7946d12016-01-18 09:23:56 -0500174 void addLocalVariable(std::unique_ptr<Instruction> inst) { localVariables.push_back(std::move(inst)); }
Dejan Mircevski38d039d2016-01-19 10:01:27 -0500175 const std::vector<Block*>& getPredecessors() const { return predecessors; }
176 const std::vector<Block*>& getSuccessors() const { return successors; }
qiningda397332016-03-09 19:54:03 -0500177 const std::vector<std::unique_ptr<Instruction> >& getInstructions() const {
178 return instructions;
179 }
John Kessenich140f3df2015-06-26 16:58:36 -0600180 void setUnreachable() { unreachable = true; }
181 bool isUnreachable() const { return unreachable; }
Dejan Mircevski44bfb0d2016-01-18 16:18:37 -0500182 // Returns the block's merge instruction, if one exists (otherwise null).
183 const Instruction* getMergeInstruction() const {
184 if (instructions.size() < 2) return nullptr;
Dejan Mircevski377f0ca2016-01-19 10:17:33 -0500185 const Instruction* nextToLast = (instructions.cend() - 2)->get();
Dejan Mircevski44bfb0d2016-01-18 16:18:37 -0500186 switch (nextToLast->getOpCode()) {
187 case OpSelectionMerge:
188 case OpLoopMerge:
189 return nextToLast;
190 default:
191 return nullptr;
192 }
193 return nullptr;
194 }
John Kessenich140f3df2015-06-26 16:58:36 -0600195
196 bool isTerminated() const
197 {
198 switch (instructions.back()->getOpCode()) {
199 case OpBranch:
200 case OpBranchConditional:
201 case OpSwitch:
202 case OpKill:
203 case OpReturn:
204 case OpReturnValue:
205 return true;
206 default:
207 return false;
208 }
209 }
210
211 void dump(std::vector<unsigned int>& out) const
212 {
John Kessenich140f3df2015-06-26 16:58:36 -0600213 instructions[0]->dump(out);
214 for (int i = 0; i < (int)localVariables.size(); ++i)
215 localVariables[i]->dump(out);
216 for (int i = 1; i < (int)instructions.size(); ++i)
217 instructions[i]->dump(out);
218 }
219
220protected:
221 Block(const Block&);
222 Block& operator=(Block&);
223
224 // To enforce keeping parent and ownership in sync:
225 friend Function;
226
Andrew Woloszynb7946d12016-01-18 09:23:56 -0500227 std::vector<std::unique_ptr<Instruction> > instructions;
Dejan Mircevski5fe789b2016-01-17 23:27:45 -0500228 std::vector<Block*> predecessors, successors;
Andrew Woloszynb7946d12016-01-18 09:23:56 -0500229 std::vector<std::unique_ptr<Instruction> > localVariables;
John Kessenich140f3df2015-06-26 16:58:36 -0600230 Function& parent;
231
John Kessenichecba76f2017-01-06 00:34:48 -0700232 // track whether this block is known to be uncreachable (not necessarily
John Kessenich140f3df2015-06-26 16:58:36 -0600233 // true for all unreachable blocks, but should be set at least
234 // for the extraneous ones introduced by the builder).
235 bool unreachable;
236};
237
Dejan Mircevski44bfb0d2016-01-18 16:18:37 -0500238// Traverses the control-flow graph rooted at root in an order suited for
239// readable code generation. Invokes callback at every node in the traversal
240// order.
241void inReadableOrder(Block* root, std::function<void(Block*)> callback);
242
John Kessenich140f3df2015-06-26 16:58:36 -0600243//
244// SPIR-V IR Function.
245//
246
247class Function {
248public:
249 Function(Id id, Id resultType, Id functionType, Id firstParam, Module& parent);
250 virtual ~Function()
251 {
252 for (int i = 0; i < (int)parameterInstructions.size(); ++i)
253 delete parameterInstructions[i];
254
255 for (int i = 0; i < (int)blocks.size(); ++i)
256 delete blocks[i];
257 }
258 Id getId() const { return functionInstruction.getResultId(); }
259 Id getParamId(int p) { return parameterInstructions[p]->getResultId(); }
260
261 void addBlock(Block* block) { blocks.push_back(block); }
Dejan Mircevskie537b8b2016-01-10 19:37:00 -0500262 void removeBlock(Block* block)
263 {
264 auto found = find(blocks.begin(), blocks.end(), block);
265 assert(found != blocks.end());
266 blocks.erase(found);
267 delete block;
268 }
John Kessenich140f3df2015-06-26 16:58:36 -0600269
270 Module& getParent() const { return parent; }
271 Block* getEntryBlock() const { return blocks.front(); }
272 Block* getLastBlock() const { return blocks.back(); }
qiningda397332016-03-09 19:54:03 -0500273 const std::vector<Block*>& getBlocks() const { return blocks; }
Andrew Woloszynb7946d12016-01-18 09:23:56 -0500274 void addLocalVariable(std::unique_ptr<Instruction> inst);
John Kessenich140f3df2015-06-26 16:58:36 -0600275 Id getReturnType() const { return functionInstruction.getTypeId(); }
276 void dump(std::vector<unsigned int>& out) const
277 {
278 // OpFunction
279 functionInstruction.dump(out);
280
281 // OpFunctionParameter
282 for (int p = 0; p < (int)parameterInstructions.size(); ++p)
283 parameterInstructions[p]->dump(out);
284
285 // Blocks
Dejan Mircevski44bfb0d2016-01-18 16:18:37 -0500286 inReadableOrder(blocks[0], [&out](const Block* b) { b->dump(out); });
John Kessenich140f3df2015-06-26 16:58:36 -0600287 Instruction end(0, 0, OpFunctionEnd);
288 end.dump(out);
289 }
290
291protected:
292 Function(const Function&);
293 Function& operator=(Function&);
294
295 Module& parent;
296 Instruction functionInstruction;
297 std::vector<Instruction*> parameterInstructions;
298 std::vector<Block*> blocks;
299};
300
301//
302// SPIR-V IR Module.
303//
304
305class Module {
306public:
307 Module() {}
308 virtual ~Module()
309 {
310 // TODO delete things
311 }
312
313 void addFunction(Function *fun) { functions.push_back(fun); }
314
315 void mapInstruction(Instruction *instruction)
316 {
317 spv::Id resultId = instruction->getResultId();
318 // map the instruction's result id
319 if (resultId >= idToInstruction.size())
320 idToInstruction.resize(resultId + 16);
321 idToInstruction[resultId] = instruction;
322 }
323
324 Instruction* getInstruction(Id id) const { return idToInstruction[id]; }
qiningda397332016-03-09 19:54:03 -0500325 const std::vector<Function*>& getFunctions() const { return functions; }
John Kessenich140f3df2015-06-26 16:58:36 -0600326 spv::Id getTypeId(Id resultId) const { return idToInstruction[resultId]->getTypeId(); }
John Kesseniche00e72d2015-12-08 20:48:49 -0700327 StorageClass getStorageClass(Id typeId) const
328 {
329 assert(idToInstruction[typeId]->getOpCode() == spv::OpTypePointer);
330 return (StorageClass)idToInstruction[typeId]->getImmediateOperand(0);
331 }
332
John Kessenich140f3df2015-06-26 16:58:36 -0600333 void dump(std::vector<unsigned int>& out) const
334 {
335 for (int f = 0; f < (int)functions.size(); ++f)
336 functions[f]->dump(out);
337 }
338
339protected:
340 Module(const Module&);
341 std::vector<Function*> functions;
342
343 // map from result id to instruction having that result id
344 std::vector<Instruction*> idToInstruction;
345
346 // map from a result id to its type id
347};
348
349//
350// Implementation (it's here due to circular type definitions).
351//
352
353// Add both
354// - the OpFunction instruction
355// - all the OpFunctionParameter instructions
356__inline Function::Function(Id id, Id resultType, Id functionType, Id firstParamId, Module& parent)
357 : parent(parent), functionInstruction(id, resultType, OpFunction)
358{
359 // OpFunction
360 functionInstruction.addImmediateOperand(FunctionControlMaskNone);
361 functionInstruction.addIdOperand(functionType);
362 parent.mapInstruction(&functionInstruction);
363 parent.addFunction(this);
364
365 // OpFunctionParameter
366 Instruction* typeInst = parent.getInstruction(functionType);
367 int numParams = typeInst->getNumOperands() - 1;
368 for (int p = 0; p < numParams; ++p) {
369 Instruction* param = new Instruction(firstParamId + p, typeInst->getIdOperand(p + 1), OpFunctionParameter);
370 parent.mapInstruction(param);
371 parameterInstructions.push_back(param);
372 }
373}
374
Andrew Woloszynb7946d12016-01-18 09:23:56 -0500375__inline void Function::addLocalVariable(std::unique_ptr<Instruction> inst)
John Kessenich140f3df2015-06-26 16:58:36 -0600376{
Andrew Woloszynb7946d12016-01-18 09:23:56 -0500377 Instruction* raw_instruction = inst.get();
378 blocks[0]->addLocalVariable(std::move(inst));
379 parent.mapInstruction(raw_instruction);
John Kessenich140f3df2015-06-26 16:58:36 -0600380}
381
382__inline Block::Block(Id id, Function& parent) : parent(parent), unreachable(false)
383{
Andrew Woloszynb7946d12016-01-18 09:23:56 -0500384 instructions.push_back(std::unique_ptr<Instruction>(new Instruction(id, NoType, OpLabel)));
Dejan Mircevski38d039d2016-01-19 10:01:27 -0500385 instructions.back()->setBlock(this);
Dejan Mircevski377f0ca2016-01-19 10:17:33 -0500386 parent.getParent().mapInstruction(instructions.back().get());
John Kessenich140f3df2015-06-26 16:58:36 -0600387}
388
Andrew Woloszynb7946d12016-01-18 09:23:56 -0500389__inline void Block::addInstruction(std::unique_ptr<Instruction> inst)
John Kessenich140f3df2015-06-26 16:58:36 -0600390{
Dejan Mircevski38d039d2016-01-19 10:01:27 -0500391 Instruction* raw_instruction = inst.get();
Andrew Woloszynb7946d12016-01-18 09:23:56 -0500392 instructions.push_back(std::move(inst));
Dejan Mircevski38d039d2016-01-19 10:01:27 -0500393 raw_instruction->setBlock(this);
Andrew Woloszynb7946d12016-01-18 09:23:56 -0500394 if (raw_instruction->getResultId())
395 parent.getParent().mapInstruction(raw_instruction);
John Kessenich140f3df2015-06-26 16:58:36 -0600396}
397
398}; // end spv namespace
399
400#endif // spvIR_H