John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 2 | // Copyright (C) 2014 LunarG, Inc. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 4 | // All rights reserved. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions |
| 8 | // are met: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 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 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 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. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 34 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 35 | // 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 Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 39 | // - Module, which is a list of |
| 40 | // - Function, which is a list of |
| 41 | // - Block, which is a list of |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 42 | // - Instruction |
| 43 | // |
| 44 | |
| 45 | #pragma once |
| 46 | #ifndef spvIR_H |
| 47 | #define spvIR_H |
| 48 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 49 | #include "spirv.hpp" |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 50 | |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 51 | #include <algorithm> |
Dejan Mircevski | 44bfb0d | 2016-01-18 16:18:37 -0500 | [diff] [blame] | 52 | #include <cassert> |
| 53 | #include <functional> |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 54 | #include <iostream> |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 55 | #include <memory> |
Dejan Mircevski | 44bfb0d | 2016-01-18 16:18:37 -0500 | [diff] [blame] | 56 | #include <vector> |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 57 | |
| 58 | namespace spv { |
| 59 | |
Dejan Mircevski | 38d039d | 2016-01-19 10:01:27 -0500 | [diff] [blame] | 60 | class Block; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 61 | class Function; |
| 62 | class Module; |
| 63 | |
| 64 | const Id NoResult = 0; |
| 65 | const Id NoType = 0; |
| 66 | |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 67 | const Decoration NoPrecision = DecorationMax; |
LoopDawg | 2baa774 | 2017-08-31 13:44:34 -0600 | [diff] [blame] | 68 | |
| 69 | #ifdef __GNUC__ |
| 70 | # define POTENTIALLY_UNUSED __attribute__((unused)) |
| 71 | #else |
| 72 | # define POTENTIALLY_UNUSED |
| 73 | #endif |
| 74 | |
| 75 | POTENTIALLY_UNUSED |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 76 | const MemorySemanticsMask MemorySemanticsAllMemory = |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 77 | (MemorySemanticsMask)(MemorySemanticsUniformMemoryMask | |
John Kessenich | 4889167 | 2016-01-22 10:15:03 -0700 | [diff] [blame] | 78 | MemorySemanticsWorkgroupMemoryMask | |
John Kessenich | 4889167 | 2016-01-22 10:15:03 -0700 | [diff] [blame] | 79 | MemorySemanticsAtomicCounterMemoryMask | |
| 80 | MemorySemanticsImageMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 81 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame^] | 82 | struct IdImmediate { |
| 83 | bool isId; // true if word is an Id, false if word is an immediate |
| 84 | unsigned word; |
| 85 | }; |
| 86 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 87 | // |
| 88 | // SPIR-V IR instruction. |
| 89 | // |
| 90 | |
| 91 | class Instruction { |
| 92 | public: |
Dejan Mircevski | 34bc6c3 | 2016-01-19 14:08:32 -0500 | [diff] [blame] | 93 | Instruction(Id resultId, Id typeId, Op opCode) : resultId(resultId), typeId(typeId), opCode(opCode), block(nullptr) { } |
| 94 | explicit Instruction(Op opCode) : resultId(NoResult), typeId(NoType), opCode(opCode), block(nullptr) { } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 95 | virtual ~Instruction() {} |
John Kessenich | 228e964 | 2018-08-13 21:37:59 -0600 | [diff] [blame] | 96 | void addIdOperand(Id id) { |
| 97 | operands.push_back(id); |
| 98 | idOperand.push_back(true); |
| 99 | } |
| 100 | void addImmediateOperand(unsigned int immediate) { |
| 101 | operands.push_back(immediate); |
| 102 | idOperand.push_back(false); |
| 103 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 104 | void addStringOperand(const char* str) |
| 105 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 106 | unsigned int word; |
| 107 | char* wordString = (char*)&word; |
| 108 | char* wordPtr = wordString; |
| 109 | int charCount = 0; |
| 110 | char c; |
| 111 | do { |
| 112 | c = *(str++); |
| 113 | *(wordPtr++) = c; |
| 114 | ++charCount; |
| 115 | if (charCount == 4) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 116 | addImmediateOperand(word); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 117 | wordPtr = wordString; |
| 118 | charCount = 0; |
| 119 | } |
| 120 | } while (c != 0); |
| 121 | |
| 122 | // deal with partial last word |
| 123 | if (charCount > 0) { |
| 124 | // pad with 0s |
| 125 | for (; charCount < 4; ++charCount) |
| 126 | *(wordPtr++) = 0; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 127 | addImmediateOperand(word); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 128 | } |
| 129 | } |
John Kessenich | 228e964 | 2018-08-13 21:37:59 -0600 | [diff] [blame] | 130 | bool isIdOperand(int op) { return idOperand[op]; } |
Dejan Mircevski | 38d039d | 2016-01-19 10:01:27 -0500 | [diff] [blame] | 131 | void setBlock(Block* b) { block = b; } |
Dejan Mircevski | fa24290 | 2016-01-19 11:31:55 -0500 | [diff] [blame] | 132 | Block* getBlock() const { return block; } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 133 | Op getOpCode() const { return opCode; } |
John Kessenich | 228e964 | 2018-08-13 21:37:59 -0600 | [diff] [blame] | 134 | int getNumOperands() const |
| 135 | { |
| 136 | assert(operands.size() == idOperand.size()); |
| 137 | return (int)operands.size(); |
| 138 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 139 | Id getResultId() const { return resultId; } |
| 140 | Id getTypeId() const { return typeId; } |
John Kessenich | 228e964 | 2018-08-13 21:37:59 -0600 | [diff] [blame] | 141 | Id getIdOperand(int op) const { |
| 142 | assert(idOperand[op]); |
| 143 | return operands[op]; |
| 144 | } |
| 145 | unsigned int getImmediateOperand(int op) const { |
| 146 | assert(!idOperand[op]); |
| 147 | return operands[op]; |
| 148 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 149 | |
| 150 | // Write out the binary form. |
| 151 | void dump(std::vector<unsigned int>& out) const |
| 152 | { |
| 153 | // Compute the wordCount |
| 154 | unsigned int wordCount = 1; |
| 155 | if (typeId) |
| 156 | ++wordCount; |
| 157 | if (resultId) |
| 158 | ++wordCount; |
| 159 | wordCount += (unsigned int)operands.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 160 | |
| 161 | // Write out the beginning of the instruction |
| 162 | out.push_back(((wordCount) << WordCountShift) | opCode); |
| 163 | if (typeId) |
| 164 | out.push_back(typeId); |
| 165 | if (resultId) |
| 166 | out.push_back(resultId); |
| 167 | |
| 168 | // Write out the operands |
| 169 | for (int op = 0; op < (int)operands.size(); ++op) |
| 170 | out.push_back(operands[op]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | protected: |
| 174 | Instruction(const Instruction&); |
| 175 | Id resultId; |
| 176 | Id typeId; |
| 177 | Op opCode; |
John Kessenich | 228e964 | 2018-08-13 21:37:59 -0600 | [diff] [blame] | 178 | std::vector<Id> operands; // operands, both <id> and immediates (both are unsigned int) |
| 179 | std::vector<bool> idOperand; // true for operands that are <id>, false for immediates |
Dejan Mircevski | 38d039d | 2016-01-19 10:01:27 -0500 | [diff] [blame] | 180 | Block* block; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | // |
| 184 | // SPIR-V IR block. |
| 185 | // |
| 186 | |
| 187 | class Block { |
| 188 | public: |
| 189 | Block(Id id, Function& parent); |
| 190 | virtual ~Block() |
| 191 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 192 | } |
Dejan Mircevski | 44bfb0d | 2016-01-18 16:18:37 -0500 | [diff] [blame] | 193 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 194 | Id getId() { return instructions.front()->getResultId(); } |
| 195 | |
| 196 | Function& getParent() const { return parent; } |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 197 | void addInstruction(std::unique_ptr<Instruction> inst); |
Dejan Mircevski | 5fe789b | 2016-01-17 23:27:45 -0500 | [diff] [blame] | 198 | void addPredecessor(Block* pred) { predecessors.push_back(pred); pred->successors.push_back(this);} |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 199 | void addLocalVariable(std::unique_ptr<Instruction> inst) { localVariables.push_back(std::move(inst)); } |
Dejan Mircevski | 38d039d | 2016-01-19 10:01:27 -0500 | [diff] [blame] | 200 | const std::vector<Block*>& getPredecessors() const { return predecessors; } |
| 201 | const std::vector<Block*>& getSuccessors() const { return successors; } |
qining | da39733 | 2016-03-09 19:54:03 -0500 | [diff] [blame] | 202 | const std::vector<std::unique_ptr<Instruction> >& getInstructions() const { |
| 203 | return instructions; |
| 204 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 205 | void setUnreachable() { unreachable = true; } |
| 206 | bool isUnreachable() const { return unreachable; } |
Dejan Mircevski | 44bfb0d | 2016-01-18 16:18:37 -0500 | [diff] [blame] | 207 | // Returns the block's merge instruction, if one exists (otherwise null). |
| 208 | const Instruction* getMergeInstruction() const { |
| 209 | if (instructions.size() < 2) return nullptr; |
Dejan Mircevski | 377f0ca | 2016-01-19 10:17:33 -0500 | [diff] [blame] | 210 | const Instruction* nextToLast = (instructions.cend() - 2)->get(); |
Dejan Mircevski | 44bfb0d | 2016-01-18 16:18:37 -0500 | [diff] [blame] | 211 | switch (nextToLast->getOpCode()) { |
| 212 | case OpSelectionMerge: |
| 213 | case OpLoopMerge: |
| 214 | return nextToLast; |
| 215 | default: |
| 216 | return nullptr; |
| 217 | } |
| 218 | return nullptr; |
| 219 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 220 | |
| 221 | bool isTerminated() const |
| 222 | { |
| 223 | switch (instructions.back()->getOpCode()) { |
| 224 | case OpBranch: |
| 225 | case OpBranchConditional: |
| 226 | case OpSwitch: |
| 227 | case OpKill: |
| 228 | case OpReturn: |
| 229 | case OpReturnValue: |
| 230 | return true; |
| 231 | default: |
| 232 | return false; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | void dump(std::vector<unsigned int>& out) const |
| 237 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 238 | instructions[0]->dump(out); |
| 239 | for (int i = 0; i < (int)localVariables.size(); ++i) |
| 240 | localVariables[i]->dump(out); |
| 241 | for (int i = 1; i < (int)instructions.size(); ++i) |
| 242 | instructions[i]->dump(out); |
| 243 | } |
| 244 | |
| 245 | protected: |
| 246 | Block(const Block&); |
| 247 | Block& operator=(Block&); |
| 248 | |
| 249 | // To enforce keeping parent and ownership in sync: |
| 250 | friend Function; |
| 251 | |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 252 | std::vector<std::unique_ptr<Instruction> > instructions; |
Dejan Mircevski | 5fe789b | 2016-01-17 23:27:45 -0500 | [diff] [blame] | 253 | std::vector<Block*> predecessors, successors; |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 254 | std::vector<std::unique_ptr<Instruction> > localVariables; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 255 | Function& parent; |
| 256 | |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 257 | // track whether this block is known to be uncreachable (not necessarily |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 258 | // true for all unreachable blocks, but should be set at least |
| 259 | // for the extraneous ones introduced by the builder). |
| 260 | bool unreachable; |
| 261 | }; |
| 262 | |
Dejan Mircevski | 44bfb0d | 2016-01-18 16:18:37 -0500 | [diff] [blame] | 263 | // Traverses the control-flow graph rooted at root in an order suited for |
| 264 | // readable code generation. Invokes callback at every node in the traversal |
| 265 | // order. |
| 266 | void inReadableOrder(Block* root, std::function<void(Block*)> callback); |
| 267 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 268 | // |
| 269 | // SPIR-V IR Function. |
| 270 | // |
| 271 | |
| 272 | class Function { |
| 273 | public: |
| 274 | Function(Id id, Id resultType, Id functionType, Id firstParam, Module& parent); |
| 275 | virtual ~Function() |
| 276 | { |
| 277 | for (int i = 0; i < (int)parameterInstructions.size(); ++i) |
| 278 | delete parameterInstructions[i]; |
| 279 | |
| 280 | for (int i = 0; i < (int)blocks.size(); ++i) |
| 281 | delete blocks[i]; |
| 282 | } |
| 283 | Id getId() const { return functionInstruction.getResultId(); } |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 284 | Id getParamId(int p) const { return parameterInstructions[p]->getResultId(); } |
| 285 | Id getParamType(int p) const { return parameterInstructions[p]->getTypeId(); } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 286 | |
| 287 | void addBlock(Block* block) { blocks.push_back(block); } |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 288 | void removeBlock(Block* block) |
| 289 | { |
| 290 | auto found = find(blocks.begin(), blocks.end(), block); |
| 291 | assert(found != blocks.end()); |
| 292 | blocks.erase(found); |
| 293 | delete block; |
| 294 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 295 | |
| 296 | Module& getParent() const { return parent; } |
| 297 | Block* getEntryBlock() const { return blocks.front(); } |
| 298 | Block* getLastBlock() const { return blocks.back(); } |
qining | da39733 | 2016-03-09 19:54:03 -0500 | [diff] [blame] | 299 | const std::vector<Block*>& getBlocks() const { return blocks; } |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 300 | void addLocalVariable(std::unique_ptr<Instruction> inst); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 301 | Id getReturnType() const { return functionInstruction.getTypeId(); } |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 302 | |
| 303 | void setImplicitThis() { implicitThis = true; } |
| 304 | bool hasImplicitThis() const { return implicitThis; } |
| 305 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 306 | void dump(std::vector<unsigned int>& out) const |
| 307 | { |
| 308 | // OpFunction |
| 309 | functionInstruction.dump(out); |
| 310 | |
| 311 | // OpFunctionParameter |
| 312 | for (int p = 0; p < (int)parameterInstructions.size(); ++p) |
| 313 | parameterInstructions[p]->dump(out); |
| 314 | |
| 315 | // Blocks |
Dejan Mircevski | 44bfb0d | 2016-01-18 16:18:37 -0500 | [diff] [blame] | 316 | inReadableOrder(blocks[0], [&out](const Block* b) { b->dump(out); }); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 317 | Instruction end(0, 0, OpFunctionEnd); |
| 318 | end.dump(out); |
| 319 | } |
| 320 | |
| 321 | protected: |
| 322 | Function(const Function&); |
| 323 | Function& operator=(Function&); |
| 324 | |
| 325 | Module& parent; |
| 326 | Instruction functionInstruction; |
| 327 | std::vector<Instruction*> parameterInstructions; |
| 328 | std::vector<Block*> blocks; |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 329 | bool implicitThis; // true if this is a member function expecting to be passed a 'this' as the first argument |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 330 | }; |
| 331 | |
| 332 | // |
| 333 | // SPIR-V IR Module. |
| 334 | // |
| 335 | |
| 336 | class Module { |
| 337 | public: |
| 338 | Module() {} |
| 339 | virtual ~Module() |
| 340 | { |
| 341 | // TODO delete things |
| 342 | } |
| 343 | |
| 344 | void addFunction(Function *fun) { functions.push_back(fun); } |
| 345 | |
| 346 | void mapInstruction(Instruction *instruction) |
| 347 | { |
| 348 | spv::Id resultId = instruction->getResultId(); |
| 349 | // map the instruction's result id |
| 350 | if (resultId >= idToInstruction.size()) |
| 351 | idToInstruction.resize(resultId + 16); |
| 352 | idToInstruction[resultId] = instruction; |
| 353 | } |
| 354 | |
| 355 | Instruction* getInstruction(Id id) const { return idToInstruction[id]; } |
qining | da39733 | 2016-03-09 19:54:03 -0500 | [diff] [blame] | 356 | const std::vector<Function*>& getFunctions() const { return functions; } |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame^] | 357 | spv::Id getTypeId(Id resultId) const { |
| 358 | return idToInstruction[resultId] == nullptr ? NoType : idToInstruction[resultId]->getTypeId(); |
| 359 | } |
John Kessenich | e00e72d | 2015-12-08 20:48:49 -0700 | [diff] [blame] | 360 | StorageClass getStorageClass(Id typeId) const |
| 361 | { |
| 362 | assert(idToInstruction[typeId]->getOpCode() == spv::OpTypePointer); |
| 363 | return (StorageClass)idToInstruction[typeId]->getImmediateOperand(0); |
| 364 | } |
| 365 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 366 | void dump(std::vector<unsigned int>& out) const |
| 367 | { |
| 368 | for (int f = 0; f < (int)functions.size(); ++f) |
| 369 | functions[f]->dump(out); |
| 370 | } |
| 371 | |
| 372 | protected: |
| 373 | Module(const Module&); |
| 374 | std::vector<Function*> functions; |
| 375 | |
| 376 | // map from result id to instruction having that result id |
| 377 | std::vector<Instruction*> idToInstruction; |
| 378 | |
| 379 | // map from a result id to its type id |
| 380 | }; |
| 381 | |
| 382 | // |
| 383 | // Implementation (it's here due to circular type definitions). |
| 384 | // |
| 385 | |
| 386 | // Add both |
| 387 | // - the OpFunction instruction |
| 388 | // - all the OpFunctionParameter instructions |
| 389 | __inline Function::Function(Id id, Id resultType, Id functionType, Id firstParamId, Module& parent) |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 390 | : parent(parent), functionInstruction(id, resultType, OpFunction), implicitThis(false) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 391 | { |
| 392 | // OpFunction |
| 393 | functionInstruction.addImmediateOperand(FunctionControlMaskNone); |
| 394 | functionInstruction.addIdOperand(functionType); |
| 395 | parent.mapInstruction(&functionInstruction); |
| 396 | parent.addFunction(this); |
| 397 | |
| 398 | // OpFunctionParameter |
| 399 | Instruction* typeInst = parent.getInstruction(functionType); |
| 400 | int numParams = typeInst->getNumOperands() - 1; |
| 401 | for (int p = 0; p < numParams; ++p) { |
| 402 | Instruction* param = new Instruction(firstParamId + p, typeInst->getIdOperand(p + 1), OpFunctionParameter); |
| 403 | parent.mapInstruction(param); |
| 404 | parameterInstructions.push_back(param); |
| 405 | } |
| 406 | } |
| 407 | |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 408 | __inline void Function::addLocalVariable(std::unique_ptr<Instruction> inst) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 409 | { |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 410 | Instruction* raw_instruction = inst.get(); |
| 411 | blocks[0]->addLocalVariable(std::move(inst)); |
| 412 | parent.mapInstruction(raw_instruction); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | __inline Block::Block(Id id, Function& parent) : parent(parent), unreachable(false) |
| 416 | { |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 417 | instructions.push_back(std::unique_ptr<Instruction>(new Instruction(id, NoType, OpLabel))); |
Dejan Mircevski | 38d039d | 2016-01-19 10:01:27 -0500 | [diff] [blame] | 418 | instructions.back()->setBlock(this); |
Dejan Mircevski | 377f0ca | 2016-01-19 10:17:33 -0500 | [diff] [blame] | 419 | parent.getParent().mapInstruction(instructions.back().get()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 420 | } |
| 421 | |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 422 | __inline void Block::addInstruction(std::unique_ptr<Instruction> inst) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 423 | { |
Dejan Mircevski | 38d039d | 2016-01-19 10:01:27 -0500 | [diff] [blame] | 424 | Instruction* raw_instruction = inst.get(); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 425 | instructions.push_back(std::move(inst)); |
Dejan Mircevski | 38d039d | 2016-01-19 10:01:27 -0500 | [diff] [blame] | 426 | raw_instruction->setBlock(this); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 427 | if (raw_instruction->getResultId()) |
| 428 | parent.getParent().mapInstruction(raw_instruction); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | }; // end spv namespace |
| 432 | |
| 433 | #endif // spvIR_H |