/src/shaderc/third_party/glslang/SPIRV/spvIR.h
Line | Count | Source |
1 | | // |
2 | | // Copyright (C) 2014 LunarG, Inc. |
3 | | // Copyright (C) 2015-2018 Google, Inc. |
4 | | // |
5 | | // All rights reserved. |
6 | | // |
7 | | // Redistribution and use in source and binary forms, with or without |
8 | | // modification, are permitted provided that the following conditions |
9 | | // are met: |
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 | | // |
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. |
35 | | |
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: |
40 | | // - Module, which is a list of |
41 | | // - Function, which is a list of |
42 | | // - Block, which is a list of |
43 | | // - Instruction |
44 | | // |
45 | | |
46 | | #pragma once |
47 | | #ifndef spvIR_H |
48 | | #define spvIR_H |
49 | | |
50 | | #include "spirv.hpp11" |
51 | | |
52 | | #include <algorithm> |
53 | | #include <cassert> |
54 | | #include <functional> |
55 | | #include <iostream> |
56 | | #include <memory> |
57 | | #include <vector> |
58 | | #include <set> |
59 | | #include <optional> |
60 | | |
61 | | namespace spv { |
62 | | |
63 | | class Block; |
64 | | class Function; |
65 | | class Module; |
66 | | |
67 | | const Id NoResult = 0; |
68 | | const Id NoType = 0; |
69 | | |
70 | | const Decoration NoPrecision = Decoration::Max; |
71 | | |
72 | | #ifdef __GNUC__ |
73 | | # define POTENTIALLY_UNUSED __attribute__((unused)) |
74 | | #else |
75 | | # define POTENTIALLY_UNUSED |
76 | | #endif |
77 | | |
78 | | POTENTIALLY_UNUSED |
79 | | const MemorySemanticsMask MemorySemanticsAllMemory = |
80 | | (MemorySemanticsMask)(MemorySemanticsMask::UniformMemory | |
81 | | MemorySemanticsMask::WorkgroupMemory | |
82 | | MemorySemanticsMask::AtomicCounterMemory | |
83 | | MemorySemanticsMask::ImageMemory); |
84 | | |
85 | | struct IdImmediate { |
86 | | bool isId; // true if word is an Id, false if word is an immediate |
87 | | unsigned word; |
88 | 12.0k | IdImmediate(bool i, unsigned w) : isId(i), word(w) {} |
89 | 0 | IdImmediate(bool i, spv::MemoryAccessMask w) : isId(i), word((unsigned)w) {} |
90 | 0 | IdImmediate(bool i, spv::TensorAddressingOperandsMask w) : isId(i), word((unsigned)w) {} |
91 | 6 | IdImmediate(bool i, spv::ImageOperandsMask w) : isId(i), word((unsigned)w) {} |
92 | 0 | IdImmediate(bool i, spv::CooperativeMatrixOperandsMask w) : isId(i), word((unsigned)w) {} |
93 | | }; |
94 | | |
95 | | // |
96 | | // SPIR-V IR instruction. |
97 | | // |
98 | | |
99 | | class Instruction { |
100 | | public: |
101 | 164k | Instruction(Id resultId, Id typeId, Op opCode) : resultId(resultId), typeId(typeId), opCode(opCode), block(nullptr) { } |
102 | 72.0k | explicit Instruction(Op opCode) : resultId(NoResult), typeId(NoType), opCode(opCode), block(nullptr) { } |
103 | 237k | virtual ~Instruction() {} |
104 | 140k | void reserveOperands(size_t count) { |
105 | 140k | operands.reserve(count); |
106 | 140k | idOperand.reserve(count); |
107 | 140k | } |
108 | 293k | void addIdOperand(Id id) { |
109 | | // ids can't be 0 |
110 | 293k | assert(id); |
111 | 293k | operands.push_back(id); |
112 | 293k | idOperand.push_back(true); |
113 | 293k | } |
114 | | // This method is potentially dangerous as it can break assumptions |
115 | | // about SSA and lack of forward references. |
116 | 0 | void setIdOperand(unsigned idx, Id id) { |
117 | 0 | assert(id); |
118 | 0 | assert(idOperand[idx]); |
119 | 0 | operands[idx] = id; |
120 | 0 | } |
121 | | |
122 | 187k | void addImmediateOperand(unsigned int immediate) { |
123 | 187k | operands.push_back(immediate); |
124 | 187k | idOperand.push_back(false); |
125 | 187k | } |
126 | | |
127 | 20.5k | void addImmediateOperand(spv::StorageClass immediate) { |
128 | 20.5k | addImmediateOperand((unsigned)immediate); |
129 | 20.5k | } |
130 | | |
131 | 31 | void addImmediateOperand(spv::ExecutionMode immediate) { |
132 | 31 | addImmediateOperand((unsigned)immediate); |
133 | 31 | } |
134 | | |
135 | 628 | void addImmediateOperand(spv::ExecutionModel immediate) { |
136 | 628 | addImmediateOperand((unsigned)immediate); |
137 | 628 | } |
138 | | |
139 | 13.4k | void addImmediateOperand(spv::Decoration immediate) { |
140 | 13.4k | addImmediateOperand((unsigned)immediate); |
141 | 13.4k | } |
142 | | |
143 | 0 | void addImmediateOperand(spv::LinkageType immediate) { |
144 | 0 | addImmediateOperand((unsigned)immediate); |
145 | 0 | } |
146 | | |
147 | 81 | void addImmediateOperand(spv::MemoryAccessMask immediate) { |
148 | 81 | addImmediateOperand((unsigned)immediate); |
149 | 81 | } |
150 | | |
151 | 1.86k | void addImmediateOperand(spv::Capability immediate) { |
152 | 1.86k | addImmediateOperand((unsigned)immediate); |
153 | 1.86k | } |
154 | | |
155 | 628 | void addImmediateOperand(spv::AddressingModel immediate) { |
156 | 628 | addImmediateOperand((unsigned)immediate); |
157 | 628 | } |
158 | | |
159 | 628 | void addImmediateOperand(spv::MemoryModel immediate) { |
160 | 628 | addImmediateOperand((unsigned)immediate); |
161 | 628 | } |
162 | | |
163 | 0 | void addImmediateOperand(spv::FPEncoding immediate) { |
164 | 0 | addImmediateOperand((unsigned)immediate); |
165 | 0 | } |
166 | | |
167 | 628 | void addImmediateOperand(spv::SourceLanguage immediate) { |
168 | 628 | addImmediateOperand((unsigned)immediate); |
169 | 628 | } |
170 | | |
171 | 1.24k | void addImmediateOperand(spv::Dim immediate) { |
172 | 1.24k | addImmediateOperand((unsigned)immediate); |
173 | 1.24k | } |
174 | | |
175 | 1.49k | void addImmediateOperand(spv::FunctionControlMask immediate){ |
176 | 1.49k | addImmediateOperand((unsigned)immediate); |
177 | 1.49k | } |
178 | | |
179 | 2.41k | void addImmediateOperand(spv::SelectionControlMask immediate) { |
180 | 2.41k | addImmediateOperand((unsigned)immediate); |
181 | 2.41k | } |
182 | | |
183 | 858 | void addImmediateOperand(spv::LoopControlMask immediate) { |
184 | 858 | addImmediateOperand((unsigned)immediate); |
185 | 858 | } |
186 | | |
187 | 1.57k | void setImmediateOperand(unsigned idx, unsigned int immediate) { |
188 | 1.57k | assert(!idOperand[idx]); |
189 | 1.57k | operands[idx] = immediate; |
190 | 1.57k | } |
191 | | |
192 | 0 | void clearOperands() { |
193 | 0 | operands.clear(); |
194 | 0 | idOperand.clear(); |
195 | 0 | } |
196 | | |
197 | | void addStringOperand(const char* str) |
198 | 26.3k | { |
199 | 26.3k | unsigned int word = 0; |
200 | 26.3k | unsigned int shiftAmount = 0; |
201 | 26.3k | unsigned char c; |
202 | | |
203 | 280k | do { |
204 | 280k | c = *(str++); |
205 | 280k | word |= ((unsigned int)c) << shiftAmount; |
206 | 280k | shiftAmount += 8; |
207 | 280k | if (shiftAmount == 32) { |
208 | 59.3k | addImmediateOperand(word); |
209 | 59.3k | word = 0; |
210 | 59.3k | shiftAmount = 0; |
211 | 59.3k | } |
212 | 280k | } while (c != 0); |
213 | | |
214 | | // deal with partial last word |
215 | 26.3k | if (shiftAmount > 0) { |
216 | 21.5k | addImmediateOperand(word); |
217 | 21.5k | } |
218 | 26.3k | } |
219 | 578k | bool isIdOperand(int op) const { return idOperand[op]; } |
220 | 148k | void setBlock(Block* b) { block = b; } |
221 | 8.77k | Block* getBlock() const { return block; } |
222 | 2.90M | Op getOpCode() const { return opCode; } |
223 | | int getNumOperands() const |
224 | 944k | { |
225 | 944k | assert(operands.size() == idOperand.size()); |
226 | 944k | return (int)operands.size(); |
227 | 944k | } |
228 | 1.03M | Id getResultId() const { return resultId; } |
229 | 1.51M | Id getTypeId() const { return typeId; } |
230 | 0 | void setTypeId(Id tId) { typeId = tId; } |
231 | 2.33M | Id getIdOperand(int op) const { |
232 | 2.33M | assert(idOperand[op]); |
233 | 2.33M | return operands[op]; |
234 | 2.33M | } |
235 | 1.36M | unsigned int getImmediateOperand(int op) const { |
236 | 1.36M | assert(!idOperand[op]); |
237 | 1.36M | return operands[op]; |
238 | 1.36M | } |
239 | | |
240 | | // Write out the binary form. |
241 | | void dump(std::vector<unsigned int>& out) const |
242 | 231k | { |
243 | | // Compute the wordCount |
244 | 231k | unsigned int wordCount = 1; |
245 | 231k | if (typeId) |
246 | 121k | ++wordCount; |
247 | 231k | if (resultId) |
248 | 151k | ++wordCount; |
249 | 231k | wordCount += (unsigned int)operands.size(); |
250 | | |
251 | | // Write out the beginning of the instruction |
252 | 231k | out.push_back(((wordCount) << WordCountShift) | (unsigned)opCode); |
253 | 231k | if (typeId) |
254 | 121k | out.push_back(typeId); |
255 | 231k | if (resultId) |
256 | 151k | out.push_back(resultId); |
257 | | |
258 | | // Write out the operands |
259 | 709k | for (int op = 0; op < (int)operands.size(); ++op) |
260 | 477k | out.push_back(operands[op]); |
261 | 231k | } |
262 | | |
263 | 0 | const char *getNameString() const { |
264 | 0 | if (opCode == Op::OpString) { |
265 | 0 | return (const char *)&operands[0]; |
266 | 0 | } else { |
267 | 0 | assert(opCode == Op::OpName); |
268 | 0 | return (const char *)&operands[1]; |
269 | 0 | } |
270 | 0 | } |
271 | | |
272 | | protected: |
273 | | Instruction(const Instruction&); |
274 | | Id resultId; |
275 | | Id typeId; |
276 | | Op opCode; |
277 | | std::vector<Id> operands; // operands, both <id> and immediates (both are unsigned int) |
278 | | std::vector<bool> idOperand; // true for operands that are <id>, false for immediates |
279 | | Block* block; |
280 | | }; |
281 | | |
282 | | // |
283 | | // SPIR-V IR block. |
284 | | // |
285 | | |
286 | | struct DebugSourceLocation { |
287 | | int line; |
288 | | int column; |
289 | | spv::Id fileId; |
290 | | }; |
291 | | |
292 | | class Block { |
293 | | public: |
294 | | Block(Id id, Function& parent); |
295 | | virtual ~Block() |
296 | 13.6k | { |
297 | 13.6k | } |
298 | | |
299 | 19.7k | Id getId() { return instructions.front()->getResultId(); } |
300 | | |
301 | 31.8k | Function& getParent() const { return parent; } |
302 | | // Returns true if the source location is actually updated. |
303 | | // Note we still need the builder to insert the line marker instruction. This is just a tracker. |
304 | 0 | bool updateDebugSourceLocation(int line, int column, spv::Id fileId) { |
305 | 0 | if (currentSourceLoc && currentSourceLoc->line == line && currentSourceLoc->column == column && |
306 | 0 | currentSourceLoc->fileId == fileId) { |
307 | 0 | return false; |
308 | 0 | } |
309 | | |
310 | 0 | currentSourceLoc = DebugSourceLocation{line, column, fileId}; |
311 | 0 | return true; |
312 | 0 | } |
313 | | // Returns true if the scope is actually updated. |
314 | | // Note we still need the builder to insert the debug scope instruction. This is just a tracker. |
315 | 0 | bool updateDebugScope(spv::Id scopeId) { |
316 | 0 | assert(scopeId); |
317 | 0 | if (currentDebugScope && *currentDebugScope == scopeId) { |
318 | 0 | return false; |
319 | 0 | } |
320 | | |
321 | 0 | currentDebugScope = scopeId; |
322 | 0 | return true; |
323 | 0 | } |
324 | | void addInstruction(std::unique_ptr<Instruction> inst); |
325 | 14.9k | void addPredecessor(Block* pred) { predecessors.push_back(pred); pred->successors.push_back(this);} |
326 | 9.51k | void addLocalVariable(std::unique_ptr<Instruction> inst) { localVariables.push_back(std::move(inst)); } |
327 | 0 | const std::vector<Block*>& getPredecessors() const { return predecessors; } |
328 | 22.6k | const std::vector<Block*>& getSuccessors() const { return successors; } |
329 | 212k | std::vector<std::unique_ptr<Instruction> >& getInstructions() { |
330 | 212k | return instructions; |
331 | 212k | } |
332 | 36.7k | const std::vector<std::unique_ptr<Instruction> >& getLocalVariables() const { return localVariables; } |
333 | 2.27k | void setUnreachable() { unreachable = true; } |
334 | 0 | bool isUnreachable() const { return unreachable; } |
335 | | // Returns the block's merge instruction, if one exists (otherwise null). |
336 | 22.6k | const Instruction* getMergeInstruction() const { |
337 | 22.6k | if (instructions.size() < 2) return nullptr; |
338 | 22.6k | const Instruction* nextToLast = (instructions.cend() - 2)->get(); |
339 | 22.6k | switch (nextToLast->getOpCode()) { |
340 | 4.82k | case Op::OpSelectionMerge: |
341 | 6.54k | case Op::OpLoopMerge: |
342 | 6.54k | return nextToLast; |
343 | 16.1k | default: |
344 | 16.1k | return nullptr; |
345 | 22.6k | } |
346 | 0 | return nullptr; |
347 | 22.6k | } |
348 | | |
349 | | // Change this block into a canonical dead merge block. Delete instructions |
350 | | // as necessary. A canonical dead merge block has only an OpLabel and an |
351 | | // OpUnreachable. |
352 | 18 | void rewriteAsCanonicalUnreachableMerge() { |
353 | 18 | assert(localVariables.empty()); |
354 | | // Delete all instructions except for the label. |
355 | 18 | assert(instructions.size() > 0); |
356 | 18 | instructions.resize(1); |
357 | 18 | successors.clear(); |
358 | 18 | addInstruction(std::unique_ptr<Instruction>(new Instruction(Op::OpUnreachable))); |
359 | 18 | } |
360 | | // Change this block into a canonical dead continue target branching to the |
361 | | // given header ID. Delete instructions as necessary. A canonical dead continue |
362 | | // target has only an OpLabel and an unconditional branch back to the corresponding |
363 | | // header. |
364 | 0 | void rewriteAsCanonicalUnreachableContinue(Block* header) { |
365 | 0 | assert(localVariables.empty()); |
366 | | // Delete all instructions except for the label. |
367 | 0 | assert(instructions.size() > 0); |
368 | 0 | instructions.resize(1); |
369 | 0 | successors.clear(); |
370 | | // Add OpBranch back to the header. |
371 | 0 | assert(header != nullptr); |
372 | 0 | Instruction* branch = new Instruction(Op::OpBranch); |
373 | 0 | branch->addIdOperand(header->getId()); |
374 | 0 | addInstruction(std::unique_ptr<Instruction>(branch)); |
375 | 0 | successors.push_back(header); |
376 | 0 | } |
377 | | |
378 | | bool isTerminated() const |
379 | 2.57k | { |
380 | 2.57k | switch (instructions.back()->getOpCode()) { |
381 | 0 | case Op::OpBranch: |
382 | 0 | case Op::OpBranchConditional: |
383 | 0 | case Op::OpSwitch: |
384 | 0 | case Op::OpKill: |
385 | 0 | case Op::OpTerminateInvocation: |
386 | 0 | case Op::OpReturn: |
387 | 0 | case Op::OpReturnValue: |
388 | 0 | case Op::OpUnreachable: |
389 | 0 | case Op::OpAbortKHR: |
390 | 0 | return true; |
391 | 2.57k | default: |
392 | 2.57k | return false; |
393 | 2.57k | } |
394 | 2.57k | } |
395 | | |
396 | | void dump(std::vector<unsigned int>& out) const |
397 | 11.3k | { |
398 | 11.3k | instructions[0]->dump(out); |
399 | 20.8k | for (int i = 0; i < (int)localVariables.size(); ++i) |
400 | 9.51k | localVariables[i]->dump(out); |
401 | 143k | for (int i = 1; i < (int)instructions.size(); ++i) |
402 | 132k | instructions[i]->dump(out); |
403 | 11.3k | } |
404 | | |
405 | | protected: |
406 | | Block(const Block&); |
407 | | Block& operator=(Block&); |
408 | | |
409 | | // To enforce keeping parent and ownership in sync: |
410 | | friend Function; |
411 | | |
412 | | std::vector<std::unique_ptr<Instruction> > instructions; |
413 | | std::vector<Block*> predecessors, successors; |
414 | | std::vector<std::unique_ptr<Instruction> > localVariables; |
415 | | Function& parent; |
416 | | |
417 | | // Track source location of the last source location marker instruction. |
418 | | std::optional<DebugSourceLocation> currentSourceLoc; |
419 | | |
420 | | // Track scope of the last debug scope instruction. |
421 | | std::optional<spv::Id> currentDebugScope; |
422 | | |
423 | | // track whether this block is known to be uncreachable (not necessarily |
424 | | // true for all unreachable blocks, but should be set at least |
425 | | // for the extraneous ones introduced by the builder). |
426 | | bool unreachable; |
427 | | }; |
428 | | |
429 | | // The different reasons for reaching a block in the inReadableOrder traversal. |
430 | | enum ReachReason { |
431 | | // Reachable from the entry block via transfers of control, i.e. branches. |
432 | | ReachViaControlFlow = 0, |
433 | | // A continue target that is not reachable via control flow. |
434 | | ReachDeadContinue, |
435 | | // A merge block that is not reachable via control flow. |
436 | | ReachDeadMerge |
437 | | }; |
438 | | |
439 | | // Traverses the control-flow graph rooted at root in an order suited for |
440 | | // readable code generation. Invokes callback at every node in the traversal |
441 | | // order. The callback arguments are: |
442 | | // - the block, |
443 | | // - the reason we reached the block, |
444 | | // - if the reason was that block is an unreachable continue or unreachable merge block |
445 | | // then the last parameter is the corresponding header block. |
446 | | void inReadableOrder(Block* root, std::function<void(Block*, ReachReason, Block* header)> callback); |
447 | | |
448 | | // |
449 | | // SPIR-V IR Function. |
450 | | // |
451 | | |
452 | | class Function { |
453 | | public: |
454 | | Function(Id id, Id resultType, Id functionType, Id firstParam, LinkageType linkage, const std::string& name, Module& parent); |
455 | | virtual ~Function() |
456 | 1.49k | { |
457 | 2.85k | for (int i = 0; i < (int)parameterInstructions.size(); ++i) |
458 | 1.36k | delete parameterInstructions[i]; |
459 | | |
460 | 15.1k | for (int i = 0; i < (int)blocks.size(); ++i) |
461 | 13.6k | delete blocks[i]; |
462 | 1.49k | } |
463 | 5.64k | Id getId() const { return functionInstruction.getResultId(); } |
464 | 2.72k | Id getParamId(int p) const { return parameterInstructions[p]->getResultId(); } |
465 | 1.76k | Id getParamType(int p) const { return parameterInstructions[p]->getTypeId(); } |
466 | | |
467 | 13.6k | void addBlock(Block* block) { blocks.push_back(block); } |
468 | | void removeBlock(Block* block) |
469 | 0 | { |
470 | 0 | auto found = find(blocks.begin(), blocks.end(), block); |
471 | 0 | assert(found != blocks.end()); |
472 | 0 | blocks.erase(found); |
473 | 0 | delete block; |
474 | 0 | } |
475 | | |
476 | 118k | Module& getParent() const { return parent; } |
477 | 2.36k | Block* getEntryBlock() const { return blocks.front(); } |
478 | 1.25k | Block* getLastBlock() const { return blocks.back(); } |
479 | 36.1k | const std::vector<Block*>& getBlocks() const { return blocks; } |
480 | | void addLocalVariable(std::unique_ptr<Instruction> inst); |
481 | 4.92k | Id getReturnType() const { return functionInstruction.getTypeId(); } |
482 | 0 | Id getFuncId() const { return functionInstruction.getResultId(); } |
483 | 0 | Id getFuncTypeId() const { return functionInstruction.getIdOperand(1); } |
484 | | void setFunctionControl(FunctionControlMask functionControl) |
485 | 1.49k | { |
486 | 1.49k | functionInstruction.setImmediateOperand(0, static_cast<unsigned>(functionControl)); |
487 | 1.49k | } |
488 | | void setReturnPrecision(Decoration precision) |
489 | 1.49k | { |
490 | 1.49k | if (precision == Decoration::RelaxedPrecision) |
491 | 0 | reducedPrecisionReturn = true; |
492 | 1.49k | } |
493 | | Decoration getReturnPrecision() const |
494 | 717 | { return reducedPrecisionReturn ? Decoration::RelaxedPrecision : NoPrecision; } |
495 | | |
496 | 0 | void setDebugLineInfo(Id fileName, int line, int column) { |
497 | 0 | lineInstruction = std::unique_ptr<Instruction>{new Instruction(Op::OpLine)}; |
498 | 0 | lineInstruction->reserveOperands(3); |
499 | 0 | lineInstruction->addIdOperand(fileName); |
500 | 0 | lineInstruction->addImmediateOperand(line); |
501 | 0 | lineInstruction->addImmediateOperand(column); |
502 | 0 | } |
503 | 0 | bool hasDebugLineInfo() const { return lineInstruction != nullptr; } |
504 | | |
505 | 0 | void setImplicitThis() { implicitThis = true; } |
506 | 5.28k | bool hasImplicitThis() const { return implicitThis; } |
507 | | |
508 | | void addParamPrecision(unsigned param, Decoration precision) |
509 | 0 | { |
510 | 0 | if (precision == Decoration::RelaxedPrecision) |
511 | 0 | reducedPrecisionParams.insert(param); |
512 | 0 | } |
513 | | Decoration getParamPrecision(unsigned param) const |
514 | 1.76k | { |
515 | 1.76k | return reducedPrecisionParams.find(param) != reducedPrecisionParams.end() ? |
516 | 1.76k | Decoration::RelaxedPrecision : NoPrecision; |
517 | 1.76k | } |
518 | | |
519 | | void dump(std::vector<unsigned int>& out) const |
520 | 1.49k | { |
521 | | // OpLine |
522 | 1.49k | if (lineInstruction != nullptr) { |
523 | 0 | lineInstruction->dump(out); |
524 | 0 | } |
525 | | |
526 | | // OpFunction |
527 | 1.49k | functionInstruction.dump(out); |
528 | | |
529 | | // OpFunctionParameter |
530 | 2.85k | for (int p = 0; p < (int)parameterInstructions.size(); ++p) |
531 | 1.36k | parameterInstructions[p]->dump(out); |
532 | | |
533 | | // Blocks |
534 | 11.3k | inReadableOrder(blocks[0], [&out](const Block* b, ReachReason, Block*) { b->dump(out); }); |
535 | 1.49k | Instruction end(0, 0, Op::OpFunctionEnd); |
536 | 1.49k | end.dump(out); |
537 | 1.49k | } |
538 | | |
539 | 1.49k | LinkageType getLinkType() const { return linkType; } |
540 | 0 | const char* getExportName() const { return exportName.c_str(); } |
541 | | |
542 | | protected: |
543 | | Function(const Function&); |
544 | | Function& operator=(Function&); |
545 | | |
546 | | Module& parent; |
547 | | std::unique_ptr<Instruction> lineInstruction; |
548 | | Instruction functionInstruction; |
549 | | std::vector<Instruction*> parameterInstructions; |
550 | | std::vector<Block*> blocks; |
551 | | bool implicitThis; // true if this is a member function expecting to be passed a 'this' as the first argument |
552 | | bool reducedPrecisionReturn; |
553 | | std::set<int> reducedPrecisionParams; // list of parameter indexes that need a relaxed precision arg |
554 | | LinkageType linkType; |
555 | | std::string exportName; |
556 | | }; |
557 | | |
558 | | // |
559 | | // SPIR-V IR Module. |
560 | | // |
561 | | |
562 | | class Module { |
563 | | public: |
564 | 628 | Module() {} |
565 | | virtual ~Module() |
566 | 628 | { |
567 | | // TODO delete things |
568 | 628 | } |
569 | | |
570 | 1.49k | void addFunction(Function *fun) { functions.push_back(fun); } |
571 | | |
572 | | void mapInstruction(Instruction *instruction) |
573 | 154k | { |
574 | 154k | spv::Id resultId = instruction->getResultId(); |
575 | | // map the instruction's result id |
576 | 154k | if (resultId >= idToInstruction.size()) |
577 | 9.97k | idToInstruction.resize(resultId + 16); |
578 | 154k | idToInstruction[resultId] = instruction; |
579 | 154k | } |
580 | | |
581 | 2.46M | Instruction* getInstruction(Id id) const { return idToInstruction[id]; } |
582 | 6.76k | const std::vector<Function*>& getFunctions() const { return functions; } |
583 | 1.23M | spv::Id getTypeId(Id resultId) const { |
584 | 1.23M | return idToInstruction[resultId] == nullptr ? NoType : idToInstruction[resultId]->getTypeId(); |
585 | 1.23M | } |
586 | | StorageClass getStorageClass(Id typeId) const |
587 | 312k | { |
588 | 312k | assert(idToInstruction[typeId]->getOpCode() == spv::Op::OpTypePointer || |
589 | 312k | idToInstruction[typeId]->getOpCode() == spv::Op::OpTypeUntypedPointerKHR); |
590 | 312k | return (StorageClass)idToInstruction[typeId]->getImmediateOperand(0); |
591 | 312k | } |
592 | | |
593 | | void dump(std::vector<unsigned int>& out) const |
594 | 628 | { |
595 | 2.12k | for (int f = 0; f < (int)functions.size(); ++f) |
596 | 1.49k | functions[f]->dump(out); |
597 | 628 | } |
598 | | |
599 | | protected: |
600 | | Module(const Module&); |
601 | | std::vector<Function*> functions; |
602 | | |
603 | | // map from result id to instruction having that result id |
604 | | std::vector<Instruction*> idToInstruction; |
605 | | |
606 | | // map from a result id to its type id |
607 | | }; |
608 | | |
609 | | // |
610 | | // Implementation (it's here due to circular type definitions). |
611 | | // |
612 | | |
613 | | // Add both |
614 | | // - the OpFunction instruction |
615 | | // - all the OpFunctionParameter instructions |
616 | | __inline Function::Function(Id id, Id resultType, Id functionType, Id firstParamId, LinkageType linkage, const std::string& name, Module& parent) |
617 | 1.49k | : parent(parent), lineInstruction(nullptr), |
618 | 1.49k | functionInstruction(id, resultType, Op::OpFunction), implicitThis(false), |
619 | 1.49k | reducedPrecisionReturn(false), |
620 | 1.49k | linkType(linkage) |
621 | 1.49k | { |
622 | | // OpFunction |
623 | 1.49k | functionInstruction.reserveOperands(2); |
624 | 1.49k | functionInstruction.addImmediateOperand(FunctionControlMask::MaskNone); |
625 | 1.49k | functionInstruction.addIdOperand(functionType); |
626 | 1.49k | parent.mapInstruction(&functionInstruction); |
627 | 1.49k | parent.addFunction(this); |
628 | | |
629 | | // OpFunctionParameter |
630 | 1.49k | Instruction* typeInst = parent.getInstruction(functionType); |
631 | 1.49k | int numParams = typeInst->getNumOperands() - 1; |
632 | 2.85k | for (int p = 0; p < numParams; ++p) { |
633 | 1.36k | Instruction* param = new Instruction(firstParamId + p, typeInst->getIdOperand(p + 1), Op::OpFunctionParameter); |
634 | 1.36k | parent.mapInstruction(param); |
635 | 1.36k | parameterInstructions.push_back(param); |
636 | 1.36k | } |
637 | | |
638 | | // If importing/exporting, save the function name (without the mangled parameters) for the linkage decoration |
639 | 1.49k | if (linkType != LinkageType::Max) { |
640 | 0 | exportName = name.substr(0, name.find_first_of('(')); |
641 | 0 | } |
642 | 1.49k | } |
643 | | |
644 | | __inline void Function::addLocalVariable(std::unique_ptr<Instruction> inst) |
645 | 9.51k | { |
646 | 9.51k | Instruction* raw_instruction = inst.get(); |
647 | 9.51k | blocks[0]->addLocalVariable(std::move(inst)); |
648 | 9.51k | parent.mapInstruction(raw_instruction); |
649 | 9.51k | } |
650 | | |
651 | 13.6k | __inline Block::Block(Id id, Function& parent) : parent(parent), unreachable(false) |
652 | 13.6k | { |
653 | 13.6k | instructions.push_back(std::unique_ptr<Instruction>(new Instruction(id, NoType, Op::OpLabel))); |
654 | 13.6k | instructions.back()->setBlock(this); |
655 | 13.6k | parent.getParent().mapInstruction(instructions.back().get()); |
656 | 13.6k | } |
657 | | |
658 | | __inline void Block::addInstruction(std::unique_ptr<Instruction> inst) |
659 | 134k | { |
660 | 134k | Instruction* raw_instruction = inst.get(); |
661 | 134k | instructions.push_back(std::move(inst)); |
662 | 134k | raw_instruction->setBlock(this); |
663 | 134k | if (raw_instruction->getResultId()) |
664 | 96.4k | parent.getParent().mapInstruction(raw_instruction); |
665 | 134k | } |
666 | | |
667 | | } // end spv namespace |
668 | | |
669 | | #endif // spvIR_H |