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