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 | b23d232 | 2018-12-14 10:47:35 -0700 | [diff] [blame] | 3 | // Copyright (C) 2015-2018 Google, Inc. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 5 | // All rights reserved. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 7 | // Redistribution and use in source and binary forms, with or without |
| 8 | // modification, are permitted provided that the following conditions |
| 9 | // are met: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 10 | // |
| 11 | // Redistributions of source code must retain the above copyright |
| 12 | // notice, this list of conditions and the following disclaimer. |
| 13 | // |
| 14 | // Redistributions in binary form must reproduce the above |
| 15 | // copyright notice, this list of conditions and the following |
| 16 | // disclaimer in the documentation and/or other materials provided |
| 17 | // with the distribution. |
| 18 | // |
| 19 | // Neither the name of 3Dlabs Inc. Ltd. nor the names of its |
| 20 | // contributors may be used to endorse or promote products derived |
| 21 | // from this software without specific prior written permission. |
| 22 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 23 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 24 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 25 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 26 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 27 | // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 28 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 29 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 31 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 33 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 34 | // POSSIBILITY OF SUCH DAMAGE. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 35 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 36 | // SPIRV-IR |
| 37 | // |
| 38 | // Simple in-memory representation (IR) of SPIRV. Just for holding |
| 39 | // Each function's CFG of blocks. Has this hierarchy: |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 40 | // - Module, which is a list of |
| 41 | // - Function, which is a list of |
| 42 | // - Block, which is a list of |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 43 | // - Instruction |
| 44 | // |
| 45 | |
| 46 | #pragma once |
| 47 | #ifndef spvIR_H |
| 48 | #define spvIR_H |
| 49 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 50 | #include "spirv.hpp" |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 51 | |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 52 | #include <algorithm> |
Dejan Mircevski | 44bfb0d | 2016-01-18 16:18:37 -0500 | [diff] [blame] | 53 | #include <cassert> |
| 54 | #include <functional> |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 55 | #include <iostream> |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 56 | #include <memory> |
Dejan Mircevski | 44bfb0d | 2016-01-18 16:18:37 -0500 | [diff] [blame] | 57 | #include <vector> |
John Kessenich | 4df1033 | 2020-06-26 08:37:06 -0600 | [diff] [blame] | 58 | #include <set> |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 59 | |
| 60 | namespace spv { |
| 61 | |
Dejan Mircevski | 38d039d | 2016-01-19 10:01:27 -0500 | [diff] [blame] | 62 | class Block; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 63 | class Function; |
| 64 | class Module; |
| 65 | |
| 66 | const Id NoResult = 0; |
| 67 | const Id NoType = 0; |
| 68 | |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 69 | const Decoration NoPrecision = DecorationMax; |
LoopDawg | 2baa774 | 2017-08-31 13:44:34 -0600 | [diff] [blame] | 70 | |
| 71 | #ifdef __GNUC__ |
| 72 | # define POTENTIALLY_UNUSED __attribute__((unused)) |
| 73 | #else |
| 74 | # define POTENTIALLY_UNUSED |
| 75 | #endif |
| 76 | |
| 77 | POTENTIALLY_UNUSED |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 78 | const MemorySemanticsMask MemorySemanticsAllMemory = |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 79 | (MemorySemanticsMask)(MemorySemanticsUniformMemoryMask | |
John Kessenich | 4889167 | 2016-01-22 10:15:03 -0700 | [diff] [blame] | 80 | MemorySemanticsWorkgroupMemoryMask | |
John Kessenich | 4889167 | 2016-01-22 10:15:03 -0700 | [diff] [blame] | 81 | MemorySemanticsAtomicCounterMemoryMask | |
| 82 | MemorySemanticsImageMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 83 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 84 | struct IdImmediate { |
| 85 | bool isId; // true if word is an Id, false if word is an immediate |
| 86 | unsigned word; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 87 | IdImmediate(bool i, unsigned w) : isId(i), word(w) {} |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 88 | }; |
| 89 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 90 | // |
| 91 | // SPIR-V IR instruction. |
| 92 | // |
| 93 | |
| 94 | class Instruction { |
| 95 | public: |
Dejan Mircevski | 34bc6c3 | 2016-01-19 14:08:32 -0500 | [diff] [blame] | 96 | Instruction(Id resultId, Id typeId, Op opCode) : resultId(resultId), typeId(typeId), opCode(opCode), block(nullptr) { } |
| 97 | explicit Instruction(Op opCode) : resultId(NoResult), typeId(NoType), opCode(opCode), block(nullptr) { } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 98 | virtual ~Instruction() {} |
John Kessenich | 228e964 | 2018-08-13 21:37:59 -0600 | [diff] [blame] | 99 | void addIdOperand(Id id) { |
| 100 | operands.push_back(id); |
| 101 | idOperand.push_back(true); |
| 102 | } |
| 103 | void addImmediateOperand(unsigned int immediate) { |
| 104 | operands.push_back(immediate); |
| 105 | idOperand.push_back(false); |
| 106 | } |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 107 | void setImmediateOperand(unsigned idx, unsigned int immediate) { |
| 108 | assert(!idOperand[idx]); |
| 109 | operands[idx] = immediate; |
| 110 | } |
| 111 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 112 | void addStringOperand(const char* str) |
| 113 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 114 | unsigned int word; |
| 115 | char* wordString = (char*)&word; |
| 116 | char* wordPtr = wordString; |
| 117 | int charCount = 0; |
| 118 | char c; |
| 119 | do { |
| 120 | c = *(str++); |
| 121 | *(wordPtr++) = c; |
| 122 | ++charCount; |
| 123 | if (charCount == 4) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 124 | addImmediateOperand(word); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 125 | wordPtr = wordString; |
| 126 | charCount = 0; |
| 127 | } |
| 128 | } while (c != 0); |
| 129 | |
| 130 | // deal with partial last word |
| 131 | if (charCount > 0) { |
| 132 | // pad with 0s |
| 133 | for (; charCount < 4; ++charCount) |
| 134 | *(wordPtr++) = 0; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 135 | addImmediateOperand(word); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 136 | } |
| 137 | } |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 138 | bool isIdOperand(int op) const { return idOperand[op]; } |
Dejan Mircevski | 38d039d | 2016-01-19 10:01:27 -0500 | [diff] [blame] | 139 | void setBlock(Block* b) { block = b; } |
Dejan Mircevski | fa24290 | 2016-01-19 11:31:55 -0500 | [diff] [blame] | 140 | Block* getBlock() const { return block; } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 141 | Op getOpCode() const { return opCode; } |
John Kessenich | 228e964 | 2018-08-13 21:37:59 -0600 | [diff] [blame] | 142 | int getNumOperands() const |
| 143 | { |
| 144 | assert(operands.size() == idOperand.size()); |
| 145 | return (int)operands.size(); |
| 146 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 147 | Id getResultId() const { return resultId; } |
| 148 | Id getTypeId() const { return typeId; } |
John Kessenich | 228e964 | 2018-08-13 21:37:59 -0600 | [diff] [blame] | 149 | Id getIdOperand(int op) const { |
| 150 | assert(idOperand[op]); |
| 151 | return operands[op]; |
| 152 | } |
| 153 | unsigned int getImmediateOperand(int op) const { |
| 154 | assert(!idOperand[op]); |
| 155 | return operands[op]; |
| 156 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 157 | |
| 158 | // Write out the binary form. |
| 159 | void dump(std::vector<unsigned int>& out) const |
| 160 | { |
| 161 | // Compute the wordCount |
| 162 | unsigned int wordCount = 1; |
| 163 | if (typeId) |
| 164 | ++wordCount; |
| 165 | if (resultId) |
| 166 | ++wordCount; |
| 167 | wordCount += (unsigned int)operands.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 168 | |
| 169 | // Write out the beginning of the instruction |
| 170 | out.push_back(((wordCount) << WordCountShift) | opCode); |
| 171 | if (typeId) |
| 172 | out.push_back(typeId); |
| 173 | if (resultId) |
| 174 | out.push_back(resultId); |
| 175 | |
| 176 | // Write out the operands |
| 177 | for (int op = 0; op < (int)operands.size(); ++op) |
| 178 | out.push_back(operands[op]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | protected: |
| 182 | Instruction(const Instruction&); |
| 183 | Id resultId; |
| 184 | Id typeId; |
| 185 | Op opCode; |
John Kessenich | 228e964 | 2018-08-13 21:37:59 -0600 | [diff] [blame] | 186 | std::vector<Id> operands; // operands, both <id> and immediates (both are unsigned int) |
| 187 | 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] | 188 | Block* block; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | // |
| 192 | // SPIR-V IR block. |
| 193 | // |
| 194 | |
| 195 | class Block { |
| 196 | public: |
| 197 | Block(Id id, Function& parent); |
| 198 | virtual ~Block() |
| 199 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 200 | } |
Dejan Mircevski | 44bfb0d | 2016-01-18 16:18:37 -0500 | [diff] [blame] | 201 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 202 | Id getId() { return instructions.front()->getResultId(); } |
| 203 | |
| 204 | Function& getParent() const { return parent; } |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 205 | void addInstruction(std::unique_ptr<Instruction> inst); |
Dejan Mircevski | 5fe789b | 2016-01-17 23:27:45 -0500 | [diff] [blame] | 206 | 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] | 207 | 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] | 208 | const std::vector<Block*>& getPredecessors() const { return predecessors; } |
| 209 | const std::vector<Block*>& getSuccessors() const { return successors; } |
qining | da39733 | 2016-03-09 19:54:03 -0500 | [diff] [blame] | 210 | const std::vector<std::unique_ptr<Instruction> >& getInstructions() const { |
| 211 | return instructions; |
| 212 | } |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 213 | const std::vector<std::unique_ptr<Instruction> >& getLocalVariables() const { return localVariables; } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 214 | void setUnreachable() { unreachable = true; } |
| 215 | bool isUnreachable() const { return unreachable; } |
Dejan Mircevski | 44bfb0d | 2016-01-18 16:18:37 -0500 | [diff] [blame] | 216 | // Returns the block's merge instruction, if one exists (otherwise null). |
| 217 | const Instruction* getMergeInstruction() const { |
| 218 | if (instructions.size() < 2) return nullptr; |
Dejan Mircevski | 377f0ca | 2016-01-19 10:17:33 -0500 | [diff] [blame] | 219 | const Instruction* nextToLast = (instructions.cend() - 2)->get(); |
Dejan Mircevski | 44bfb0d | 2016-01-18 16:18:37 -0500 | [diff] [blame] | 220 | switch (nextToLast->getOpCode()) { |
| 221 | case OpSelectionMerge: |
| 222 | case OpLoopMerge: |
| 223 | return nextToLast; |
| 224 | default: |
| 225 | return nullptr; |
| 226 | } |
| 227 | return nullptr; |
| 228 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 229 | |
David Neto | 8c3d5b4 | 2019-10-21 14:50:31 -0400 | [diff] [blame] | 230 | // Change this block into a canonical dead merge block. Delete instructions |
| 231 | // as necessary. A canonical dead merge block has only an OpLabel and an |
| 232 | // OpUnreachable. |
| 233 | void rewriteAsCanonicalUnreachableMerge() { |
| 234 | assert(localVariables.empty()); |
| 235 | // Delete all instructions except for the label. |
| 236 | assert(instructions.size() > 0); |
| 237 | instructions.resize(1); |
| 238 | successors.clear(); |
John Kessenich | 1fff362 | 2020-04-01 00:46:57 -0600 | [diff] [blame] | 239 | addInstruction(std::unique_ptr<Instruction>(new Instruction(OpUnreachable))); |
David Neto | 8c3d5b4 | 2019-10-21 14:50:31 -0400 | [diff] [blame] | 240 | } |
| 241 | // Change this block into a canonical dead continue target branching to the |
| 242 | // given header ID. Delete instructions as necessary. A canonical dead continue |
| 243 | // target has only an OpLabel and an unconditional branch back to the corresponding |
| 244 | // header. |
| 245 | void rewriteAsCanonicalUnreachableContinue(Block* header) { |
| 246 | assert(localVariables.empty()); |
| 247 | // Delete all instructions except for the label. |
| 248 | assert(instructions.size() > 0); |
| 249 | instructions.resize(1); |
| 250 | successors.clear(); |
| 251 | // Add OpBranch back to the header. |
| 252 | assert(header != nullptr); |
| 253 | Instruction* branch = new Instruction(OpBranch); |
| 254 | branch->addIdOperand(header->getId()); |
Ryan Harrison | 0552c0a | 2019-11-04 16:23:11 -0500 | [diff] [blame] | 255 | addInstruction(std::unique_ptr<Instruction>(branch)); |
David Neto | 8c3d5b4 | 2019-10-21 14:50:31 -0400 | [diff] [blame] | 256 | successors.push_back(header); |
| 257 | } |
| 258 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 259 | bool isTerminated() const |
| 260 | { |
| 261 | switch (instructions.back()->getOpCode()) { |
| 262 | case OpBranch: |
| 263 | case OpBranchConditional: |
| 264 | case OpSwitch: |
| 265 | case OpKill: |
Jesse Hall | 74e8f05 | 2020-11-09 08:30:01 -0800 | [diff] [blame] | 266 | case OpTerminateInvocation: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 267 | case OpReturn: |
| 268 | case OpReturnValue: |
David Neto | 8c3d5b4 | 2019-10-21 14:50:31 -0400 | [diff] [blame] | 269 | case OpUnreachable: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 270 | return true; |
| 271 | default: |
| 272 | return false; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | void dump(std::vector<unsigned int>& out) const |
| 277 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 278 | instructions[0]->dump(out); |
| 279 | for (int i = 0; i < (int)localVariables.size(); ++i) |
| 280 | localVariables[i]->dump(out); |
| 281 | for (int i = 1; i < (int)instructions.size(); ++i) |
| 282 | instructions[i]->dump(out); |
| 283 | } |
| 284 | |
| 285 | protected: |
| 286 | Block(const Block&); |
| 287 | Block& operator=(Block&); |
| 288 | |
| 289 | // To enforce keeping parent and ownership in sync: |
| 290 | friend Function; |
| 291 | |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 292 | std::vector<std::unique_ptr<Instruction> > instructions; |
Dejan Mircevski | 5fe789b | 2016-01-17 23:27:45 -0500 | [diff] [blame] | 293 | std::vector<Block*> predecessors, successors; |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 294 | std::vector<std::unique_ptr<Instruction> > localVariables; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 295 | Function& parent; |
| 296 | |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 297 | // track whether this block is known to be uncreachable (not necessarily |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 298 | // true for all unreachable blocks, but should be set at least |
| 299 | // for the extraneous ones introduced by the builder). |
| 300 | bool unreachable; |
| 301 | }; |
| 302 | |
David Neto | 8c3d5b4 | 2019-10-21 14:50:31 -0400 | [diff] [blame] | 303 | // The different reasons for reaching a block in the inReadableOrder traversal. |
John Kessenich | 31c3370 | 2019-11-02 21:26:40 -0600 | [diff] [blame] | 304 | enum ReachReason { |
David Neto | 8c3d5b4 | 2019-10-21 14:50:31 -0400 | [diff] [blame] | 305 | // Reachable from the entry block via transfers of control, i.e. branches. |
| 306 | ReachViaControlFlow = 0, |
| 307 | // A continue target that is not reachable via control flow. |
| 308 | ReachDeadContinue, |
| 309 | // A merge block that is not reachable via control flow. |
| 310 | ReachDeadMerge |
| 311 | }; |
| 312 | |
Dejan Mircevski | 44bfb0d | 2016-01-18 16:18:37 -0500 | [diff] [blame] | 313 | // Traverses the control-flow graph rooted at root in an order suited for |
| 314 | // readable code generation. Invokes callback at every node in the traversal |
David Neto | 8c3d5b4 | 2019-10-21 14:50:31 -0400 | [diff] [blame] | 315 | // order. The callback arguments are: |
| 316 | // - the block, |
| 317 | // - the reason we reached the block, |
| 318 | // - if the reason was that block is an unreachable continue or unreachable merge block |
| 319 | // then the last parameter is the corresponding header block. |
| 320 | void inReadableOrder(Block* root, std::function<void(Block*, ReachReason, Block* header)> callback); |
Dejan Mircevski | 44bfb0d | 2016-01-18 16:18:37 -0500 | [diff] [blame] | 321 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 322 | // |
| 323 | // SPIR-V IR Function. |
| 324 | // |
| 325 | |
| 326 | class Function { |
| 327 | public: |
| 328 | Function(Id id, Id resultType, Id functionType, Id firstParam, Module& parent); |
| 329 | virtual ~Function() |
| 330 | { |
| 331 | for (int i = 0; i < (int)parameterInstructions.size(); ++i) |
| 332 | delete parameterInstructions[i]; |
| 333 | |
| 334 | for (int i = 0; i < (int)blocks.size(); ++i) |
| 335 | delete blocks[i]; |
| 336 | } |
| 337 | Id getId() const { return functionInstruction.getResultId(); } |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 338 | Id getParamId(int p) const { return parameterInstructions[p]->getResultId(); } |
| 339 | Id getParamType(int p) const { return parameterInstructions[p]->getTypeId(); } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 340 | |
| 341 | void addBlock(Block* block) { blocks.push_back(block); } |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 342 | void removeBlock(Block* block) |
| 343 | { |
| 344 | auto found = find(blocks.begin(), blocks.end(), block); |
| 345 | assert(found != blocks.end()); |
| 346 | blocks.erase(found); |
| 347 | delete block; |
| 348 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 349 | |
| 350 | Module& getParent() const { return parent; } |
| 351 | Block* getEntryBlock() const { return blocks.front(); } |
| 352 | Block* getLastBlock() const { return blocks.back(); } |
qining | da39733 | 2016-03-09 19:54:03 -0500 | [diff] [blame] | 353 | const std::vector<Block*>& getBlocks() const { return blocks; } |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 354 | void addLocalVariable(std::unique_ptr<Instruction> inst); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 355 | Id getReturnType() const { return functionInstruction.getTypeId(); } |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 356 | void setReturnPrecision(Decoration precision) |
| 357 | { |
| 358 | if (precision == DecorationRelaxedPrecision) |
| 359 | reducedPrecisionReturn = true; |
| 360 | } |
| 361 | Decoration getReturnPrecision() const |
| 362 | { return reducedPrecisionReturn ? DecorationRelaxedPrecision : NoPrecision; } |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 363 | |
| 364 | void setImplicitThis() { implicitThis = true; } |
| 365 | bool hasImplicitThis() const { return implicitThis; } |
| 366 | |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 367 | void addParamPrecision(unsigned param, Decoration precision) |
| 368 | { |
| 369 | if (precision == DecorationRelaxedPrecision) |
| 370 | reducedPrecisionParams.insert(param); |
| 371 | } |
| 372 | Decoration getParamPrecision(unsigned param) const |
| 373 | { |
| 374 | return reducedPrecisionParams.find(param) != reducedPrecisionParams.end() ? |
| 375 | DecorationRelaxedPrecision : NoPrecision; |
| 376 | } |
John Kessenich | 4df1033 | 2020-06-26 08:37:06 -0600 | [diff] [blame] | 377 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 378 | void dump(std::vector<unsigned int>& out) const |
| 379 | { |
| 380 | // OpFunction |
| 381 | functionInstruction.dump(out); |
| 382 | |
| 383 | // OpFunctionParameter |
| 384 | for (int p = 0; p < (int)parameterInstructions.size(); ++p) |
| 385 | parameterInstructions[p]->dump(out); |
| 386 | |
| 387 | // Blocks |
David Neto | 8c3d5b4 | 2019-10-21 14:50:31 -0400 | [diff] [blame] | 388 | inReadableOrder(blocks[0], [&out](const Block* b, ReachReason, Block*) { b->dump(out); }); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 389 | Instruction end(0, 0, OpFunctionEnd); |
| 390 | end.dump(out); |
| 391 | } |
| 392 | |
| 393 | protected: |
| 394 | Function(const Function&); |
| 395 | Function& operator=(Function&); |
| 396 | |
| 397 | Module& parent; |
| 398 | Instruction functionInstruction; |
| 399 | std::vector<Instruction*> parameterInstructions; |
| 400 | std::vector<Block*> blocks; |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 401 | bool implicitThis; // true if this is a member function expecting to be passed a 'this' as the first argument |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 402 | bool reducedPrecisionReturn; |
John Kessenich | 4df1033 | 2020-06-26 08:37:06 -0600 | [diff] [blame] | 403 | std::set<int> reducedPrecisionParams; // list of parameter indexes that need a relaxed precision arg |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 404 | }; |
| 405 | |
| 406 | // |
| 407 | // SPIR-V IR Module. |
| 408 | // |
| 409 | |
| 410 | class Module { |
| 411 | public: |
| 412 | Module() {} |
| 413 | virtual ~Module() |
| 414 | { |
| 415 | // TODO delete things |
| 416 | } |
| 417 | |
| 418 | void addFunction(Function *fun) { functions.push_back(fun); } |
| 419 | |
| 420 | void mapInstruction(Instruction *instruction) |
| 421 | { |
| 422 | spv::Id resultId = instruction->getResultId(); |
| 423 | // map the instruction's result id |
| 424 | if (resultId >= idToInstruction.size()) |
| 425 | idToInstruction.resize(resultId + 16); |
| 426 | idToInstruction[resultId] = instruction; |
| 427 | } |
| 428 | |
| 429 | Instruction* getInstruction(Id id) const { return idToInstruction[id]; } |
qining | da39733 | 2016-03-09 19:54:03 -0500 | [diff] [blame] | 430 | const std::vector<Function*>& getFunctions() const { return functions; } |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 431 | spv::Id getTypeId(Id resultId) const { |
| 432 | return idToInstruction[resultId] == nullptr ? NoType : idToInstruction[resultId]->getTypeId(); |
| 433 | } |
John Kessenich | e00e72d | 2015-12-08 20:48:49 -0700 | [diff] [blame] | 434 | StorageClass getStorageClass(Id typeId) const |
| 435 | { |
| 436 | assert(idToInstruction[typeId]->getOpCode() == spv::OpTypePointer); |
| 437 | return (StorageClass)idToInstruction[typeId]->getImmediateOperand(0); |
| 438 | } |
| 439 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 440 | void dump(std::vector<unsigned int>& out) const |
| 441 | { |
| 442 | for (int f = 0; f < (int)functions.size(); ++f) |
| 443 | functions[f]->dump(out); |
| 444 | } |
| 445 | |
| 446 | protected: |
| 447 | Module(const Module&); |
| 448 | std::vector<Function*> functions; |
| 449 | |
| 450 | // map from result id to instruction having that result id |
| 451 | std::vector<Instruction*> idToInstruction; |
| 452 | |
| 453 | // map from a result id to its type id |
| 454 | }; |
| 455 | |
| 456 | // |
| 457 | // Implementation (it's here due to circular type definitions). |
| 458 | // |
| 459 | |
| 460 | // Add both |
| 461 | // - the OpFunction instruction |
| 462 | // - all the OpFunctionParameter instructions |
| 463 | __inline Function::Function(Id id, Id resultType, Id functionType, Id firstParamId, Module& parent) |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 464 | : parent(parent), functionInstruction(id, resultType, OpFunction), implicitThis(false), |
| 465 | reducedPrecisionReturn(false) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 466 | { |
| 467 | // OpFunction |
| 468 | functionInstruction.addImmediateOperand(FunctionControlMaskNone); |
| 469 | functionInstruction.addIdOperand(functionType); |
| 470 | parent.mapInstruction(&functionInstruction); |
| 471 | parent.addFunction(this); |
| 472 | |
| 473 | // OpFunctionParameter |
| 474 | Instruction* typeInst = parent.getInstruction(functionType); |
| 475 | int numParams = typeInst->getNumOperands() - 1; |
| 476 | for (int p = 0; p < numParams; ++p) { |
| 477 | Instruction* param = new Instruction(firstParamId + p, typeInst->getIdOperand(p + 1), OpFunctionParameter); |
| 478 | parent.mapInstruction(param); |
| 479 | parameterInstructions.push_back(param); |
| 480 | } |
| 481 | } |
| 482 | |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 483 | __inline void Function::addLocalVariable(std::unique_ptr<Instruction> inst) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 484 | { |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 485 | Instruction* raw_instruction = inst.get(); |
| 486 | blocks[0]->addLocalVariable(std::move(inst)); |
| 487 | parent.mapInstruction(raw_instruction); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | __inline Block::Block(Id id, Function& parent) : parent(parent), unreachable(false) |
| 491 | { |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 492 | instructions.push_back(std::unique_ptr<Instruction>(new Instruction(id, NoType, OpLabel))); |
Dejan Mircevski | 38d039d | 2016-01-19 10:01:27 -0500 | [diff] [blame] | 493 | instructions.back()->setBlock(this); |
Dejan Mircevski | 377f0ca | 2016-01-19 10:17:33 -0500 | [diff] [blame] | 494 | parent.getParent().mapInstruction(instructions.back().get()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 495 | } |
| 496 | |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 497 | __inline void Block::addInstruction(std::unique_ptr<Instruction> inst) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 498 | { |
Dejan Mircevski | 38d039d | 2016-01-19 10:01:27 -0500 | [diff] [blame] | 499 | Instruction* raw_instruction = inst.get(); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 500 | instructions.push_back(std::move(inst)); |
Dejan Mircevski | 38d039d | 2016-01-19 10:01:27 -0500 | [diff] [blame] | 501 | raw_instruction->setBlock(this); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 502 | if (raw_instruction->getResultId()) |
| 503 | parent.getParent().mapInstruction(raw_instruction); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 504 | } |
| 505 | |
Shahbaz Youssefi | 6cca0e9 | 2019-06-25 12:08:10 -0400 | [diff] [blame] | 506 | } // end spv namespace |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 507 | |
| 508 | #endif // spvIR_H |