• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.hpp"
51 
52 #include <algorithm>
53 #include <cassert>
54 #include <functional>
55 #include <iostream>
56 #include <memory>
57 #include <vector>
58 #include <set>
59 
60 namespace spv {
61 
62 class Block;
63 class Function;
64 class Module;
65 
66 const Id NoResult = 0;
67 const Id NoType = 0;
68 
69 const Decoration NoPrecision = DecorationMax;
70 
71 #ifdef __GNUC__
72 #   define POTENTIALLY_UNUSED __attribute__((unused))
73 #else
74 #   define POTENTIALLY_UNUSED
75 #endif
76 
77 POTENTIALLY_UNUSED
78 const MemorySemanticsMask MemorySemanticsAllMemory =
79                 (MemorySemanticsMask)(MemorySemanticsUniformMemoryMask |
80                                       MemorySemanticsWorkgroupMemoryMask |
81                                       MemorySemanticsAtomicCounterMemoryMask |
82                                       MemorySemanticsImageMemoryMask);
83 
84 struct IdImmediate {
85     bool isId;      // true if word is an Id, false if word is an immediate
86     unsigned word;
IdImmediateIdImmediate87     IdImmediate(bool i, unsigned w) : isId(i), word(w) {}
88 };
89 
90 //
91 // SPIR-V IR instruction.
92 //
93 
94 class Instruction {
95 public:
Instruction(Id resultId,Id typeId,Op opCode)96     Instruction(Id resultId, Id typeId, Op opCode) : resultId(resultId), typeId(typeId), opCode(opCode), block(nullptr) { }
Instruction(Op opCode)97     explicit Instruction(Op opCode) : resultId(NoResult), typeId(NoType), opCode(opCode), block(nullptr) { }
~Instruction()98     virtual ~Instruction() {}
addIdOperand(Id id)99     void addIdOperand(Id id) {
100         // ids can't be 0
101         assert(id);
102         operands.push_back(id);
103         idOperand.push_back(true);
104     }
addImmediateOperand(unsigned int immediate)105     void addImmediateOperand(unsigned int immediate) {
106         operands.push_back(immediate);
107         idOperand.push_back(false);
108     }
setImmediateOperand(unsigned idx,unsigned int immediate)109     void setImmediateOperand(unsigned idx, unsigned int immediate) {
110         assert(!idOperand[idx]);
111         operands[idx] = immediate;
112     }
113 
addStringOperand(const char * str)114     void addStringOperand(const char* str)
115     {
116         unsigned int word = 0;
117         unsigned int shiftAmount = 0;
118         char c;
119 
120         do {
121             c = *(str++);
122             word |= ((unsigned int)c) << shiftAmount;
123             shiftAmount += 8;
124             if (shiftAmount == 32) {
125                 addImmediateOperand(word);
126                 word = 0;
127                 shiftAmount = 0;
128             }
129         } while (c != 0);
130 
131         // deal with partial last word
132         if (shiftAmount > 0) {
133             addImmediateOperand(word);
134         }
135     }
isIdOperand(int op)136     bool isIdOperand(int op) const { return idOperand[op]; }
setBlock(Block * b)137     void setBlock(Block* b) { block = b; }
getBlock()138     Block* getBlock() const { return block; }
getOpCode()139     Op getOpCode() const { return opCode; }
getNumOperands()140     int getNumOperands() const
141     {
142         assert(operands.size() == idOperand.size());
143         return (int)operands.size();
144     }
getResultId()145     Id getResultId() const { return resultId; }
getTypeId()146     Id getTypeId() const { return typeId; }
getIdOperand(int op)147     Id getIdOperand(int op) const {
148         assert(idOperand[op]);
149         return operands[op];
150     }
getImmediateOperand(int op)151     unsigned int getImmediateOperand(int op) const {
152         assert(!idOperand[op]);
153         return operands[op];
154     }
155 
156     // Write out the binary form.
dump(std::vector<unsigned int> & out)157     void dump(std::vector<unsigned int>& out) const
158     {
159         // Compute the wordCount
160         unsigned int wordCount = 1;
161         if (typeId)
162             ++wordCount;
163         if (resultId)
164             ++wordCount;
165         wordCount += (unsigned int)operands.size();
166 
167         // Write out the beginning of the instruction
168         out.push_back(((wordCount) << WordCountShift) | opCode);
169         if (typeId)
170             out.push_back(typeId);
171         if (resultId)
172             out.push_back(resultId);
173 
174         // Write out the operands
175         for (int op = 0; op < (int)operands.size(); ++op)
176             out.push_back(operands[op]);
177     }
178 
179 protected:
180     Instruction(const Instruction&);
181     Id resultId;
182     Id typeId;
183     Op opCode;
184     std::vector<Id> operands;     // operands, both <id> and immediates (both are unsigned int)
185     std::vector<bool> idOperand;  // true for operands that are <id>, false for immediates
186     Block* block;
187 };
188 
189 //
190 // SPIR-V IR block.
191 //
192 
193 class Block {
194 public:
195     Block(Id id, Function& parent);
~Block()196     virtual ~Block()
197     {
198     }
199 
getId()200     Id getId() { return instructions.front()->getResultId(); }
201 
getParent()202     Function& getParent() const { return parent; }
203     void addInstruction(std::unique_ptr<Instruction> inst);
addPredecessor(Block * pred)204     void addPredecessor(Block* pred) { predecessors.push_back(pred); pred->successors.push_back(this);}
addLocalVariable(std::unique_ptr<Instruction> inst)205     void addLocalVariable(std::unique_ptr<Instruction> inst) { localVariables.push_back(std::move(inst)); }
getPredecessors()206     const std::vector<Block*>& getPredecessors() const { return predecessors; }
getSuccessors()207     const std::vector<Block*>& getSuccessors() const { return successors; }
getInstructions()208     const std::vector<std::unique_ptr<Instruction> >& getInstructions() const {
209         return instructions;
210     }
getLocalVariables()211     const std::vector<std::unique_ptr<Instruction> >& getLocalVariables() const { return localVariables; }
setUnreachable()212     void setUnreachable() { unreachable = true; }
isUnreachable()213     bool isUnreachable() const { return unreachable; }
214     // Returns the block's merge instruction, if one exists (otherwise null).
getMergeInstruction()215     const Instruction* getMergeInstruction() const {
216         if (instructions.size() < 2) return nullptr;
217         const Instruction* nextToLast = (instructions.cend() - 2)->get();
218         switch (nextToLast->getOpCode()) {
219             case OpSelectionMerge:
220             case OpLoopMerge:
221                 return nextToLast;
222             default:
223                 return nullptr;
224         }
225         return nullptr;
226     }
227 
228     // Change this block into a canonical dead merge block.  Delete instructions
229     // as necessary.  A canonical dead merge block has only an OpLabel and an
230     // OpUnreachable.
rewriteAsCanonicalUnreachableMerge()231     void rewriteAsCanonicalUnreachableMerge() {
232         assert(localVariables.empty());
233         // Delete all instructions except for the label.
234         assert(instructions.size() > 0);
235         instructions.resize(1);
236         successors.clear();
237         addInstruction(std::unique_ptr<Instruction>(new Instruction(OpUnreachable)));
238     }
239     // Change this block into a canonical dead continue target branching to the
240     // given header ID.  Delete instructions as necessary.  A canonical dead continue
241     // target has only an OpLabel and an unconditional branch back to the corresponding
242     // header.
rewriteAsCanonicalUnreachableContinue(Block * header)243     void rewriteAsCanonicalUnreachableContinue(Block* header) {
244         assert(localVariables.empty());
245         // Delete all instructions except for the label.
246         assert(instructions.size() > 0);
247         instructions.resize(1);
248         successors.clear();
249         // Add OpBranch back to the header.
250         assert(header != nullptr);
251         Instruction* branch = new Instruction(OpBranch);
252         branch->addIdOperand(header->getId());
253         addInstruction(std::unique_ptr<Instruction>(branch));
254         successors.push_back(header);
255     }
256 
isTerminated()257     bool isTerminated() const
258     {
259         switch (instructions.back()->getOpCode()) {
260         case OpBranch:
261         case OpBranchConditional:
262         case OpSwitch:
263         case OpKill:
264         case OpTerminateInvocation:
265         case OpReturn:
266         case OpReturnValue:
267         case OpUnreachable:
268             return true;
269         default:
270             return false;
271         }
272     }
273 
dump(std::vector<unsigned int> & out)274     void dump(std::vector<unsigned int>& out) const
275     {
276         instructions[0]->dump(out);
277         for (int i = 0; i < (int)localVariables.size(); ++i)
278             localVariables[i]->dump(out);
279         for (int i = 1; i < (int)instructions.size(); ++i)
280             instructions[i]->dump(out);
281     }
282 
283 protected:
284     Block(const Block&);
285     Block& operator=(Block&);
286 
287     // To enforce keeping parent and ownership in sync:
288     friend Function;
289 
290     std::vector<std::unique_ptr<Instruction> > instructions;
291     std::vector<Block*> predecessors, successors;
292     std::vector<std::unique_ptr<Instruction> > localVariables;
293     Function& parent;
294 
295     // track whether this block is known to be uncreachable (not necessarily
296     // true for all unreachable blocks, but should be set at least
297     // for the extraneous ones introduced by the builder).
298     bool unreachable;
299 };
300 
301 // The different reasons for reaching a block in the inReadableOrder traversal.
302 enum ReachReason {
303     // Reachable from the entry block via transfers of control, i.e. branches.
304     ReachViaControlFlow = 0,
305     // A continue target that is not reachable via control flow.
306     ReachDeadContinue,
307     // A merge block that is not reachable via control flow.
308     ReachDeadMerge
309 };
310 
311 // Traverses the control-flow graph rooted at root in an order suited for
312 // readable code generation.  Invokes callback at every node in the traversal
313 // order.  The callback arguments are:
314 // - the block,
315 // - the reason we reached the block,
316 // - if the reason was that block is an unreachable continue or unreachable merge block
317 //   then the last parameter is the corresponding header block.
318 void inReadableOrder(Block* root, std::function<void(Block*, ReachReason, Block* header)> callback);
319 
320 //
321 // SPIR-V IR Function.
322 //
323 
324 class Function {
325 public:
326     Function(Id id, Id resultType, Id functionType, Id firstParam, LinkageType linkage, const std::string& name, Module& parent);
~Function()327     virtual ~Function()
328     {
329         for (int i = 0; i < (int)parameterInstructions.size(); ++i)
330             delete parameterInstructions[i];
331 
332         for (int i = 0; i < (int)blocks.size(); ++i)
333             delete blocks[i];
334     }
getId()335     Id getId() const { return functionInstruction.getResultId(); }
getParamId(int p)336     Id getParamId(int p) const { return parameterInstructions[p]->getResultId(); }
getParamType(int p)337     Id getParamType(int p) const { return parameterInstructions[p]->getTypeId(); }
338 
addBlock(Block * block)339     void addBlock(Block* block) { blocks.push_back(block); }
removeBlock(Block * block)340     void removeBlock(Block* block)
341     {
342         auto found = find(blocks.begin(), blocks.end(), block);
343         assert(found != blocks.end());
344         blocks.erase(found);
345         delete block;
346     }
347 
getParent()348     Module& getParent() const { return parent; }
getEntryBlock()349     Block* getEntryBlock() const { return blocks.front(); }
getLastBlock()350     Block* getLastBlock() const { return blocks.back(); }
getBlocks()351     const std::vector<Block*>& getBlocks() const { return blocks; }
352     void addLocalVariable(std::unique_ptr<Instruction> inst);
getReturnType()353     Id getReturnType() const { return functionInstruction.getTypeId(); }
getFuncId()354     Id getFuncId() const { return functionInstruction.getResultId(); }
getFuncTypeId()355     Id getFuncTypeId() const { return functionInstruction.getIdOperand(1); }
setReturnPrecision(Decoration precision)356     void setReturnPrecision(Decoration precision)
357     {
358         if (precision == DecorationRelaxedPrecision)
359             reducedPrecisionReturn = true;
360     }
getReturnPrecision()361     Decoration getReturnPrecision() const
362         { return reducedPrecisionReturn ? DecorationRelaxedPrecision : NoPrecision; }
363 
setDebugLineInfo(Id fileName,int line,int column)364     void setDebugLineInfo(Id fileName, int line, int column) {
365         lineInstruction = std::unique_ptr<Instruction>{new Instruction(OpLine)};
366         lineInstruction->addIdOperand(fileName);
367         lineInstruction->addImmediateOperand(line);
368         lineInstruction->addImmediateOperand(column);
369     }
hasDebugLineInfo()370     bool hasDebugLineInfo() const { return lineInstruction != nullptr; }
371 
setImplicitThis()372     void setImplicitThis() { implicitThis = true; }
hasImplicitThis()373     bool hasImplicitThis() const { return implicitThis; }
374 
addParamPrecision(unsigned param,Decoration precision)375     void addParamPrecision(unsigned param, Decoration precision)
376     {
377         if (precision == DecorationRelaxedPrecision)
378             reducedPrecisionParams.insert(param);
379     }
getParamPrecision(unsigned param)380     Decoration getParamPrecision(unsigned param) const
381     {
382         return reducedPrecisionParams.find(param) != reducedPrecisionParams.end() ?
383             DecorationRelaxedPrecision : NoPrecision;
384     }
385 
dump(std::vector<unsigned int> & out)386     void dump(std::vector<unsigned int>& out) const
387     {
388         // OpLine
389         if (lineInstruction != nullptr) {
390             lineInstruction->dump(out);
391         }
392 
393         // OpFunction
394         functionInstruction.dump(out);
395 
396         // OpFunctionParameter
397         for (int p = 0; p < (int)parameterInstructions.size(); ++p)
398             parameterInstructions[p]->dump(out);
399 
400         // Blocks
401         inReadableOrder(blocks[0], [&out](const Block* b, ReachReason, Block*) { b->dump(out); });
402         Instruction end(0, 0, OpFunctionEnd);
403         end.dump(out);
404     }
405 
getLinkType()406     LinkageType getLinkType() const { return linkType; }
getExportName()407     const char* getExportName() const { return exportName.c_str(); }
408 
409 protected:
410     Function(const Function&);
411     Function& operator=(Function&);
412 
413     Module& parent;
414     std::unique_ptr<Instruction> lineInstruction;
415     Instruction functionInstruction;
416     std::vector<Instruction*> parameterInstructions;
417     std::vector<Block*> blocks;
418     bool implicitThis;  // true if this is a member function expecting to be passed a 'this' as the first argument
419     bool reducedPrecisionReturn;
420     std::set<int> reducedPrecisionParams;  // list of parameter indexes that need a relaxed precision arg
421     LinkageType linkType;
422     std::string exportName;
423 };
424 
425 //
426 // SPIR-V IR Module.
427 //
428 
429 class Module {
430 public:
Module()431     Module() {}
~Module()432     virtual ~Module()
433     {
434         // TODO delete things
435     }
436 
addFunction(Function * fun)437     void addFunction(Function *fun) { functions.push_back(fun); }
438 
mapInstruction(Instruction * instruction)439     void mapInstruction(Instruction *instruction)
440     {
441         spv::Id resultId = instruction->getResultId();
442         // map the instruction's result id
443         if (resultId >= idToInstruction.size())
444             idToInstruction.resize(resultId + 16);
445         idToInstruction[resultId] = instruction;
446     }
447 
getInstruction(Id id)448     Instruction* getInstruction(Id id) const { return idToInstruction[id]; }
getFunctions()449     const std::vector<Function*>& getFunctions() const { return functions; }
getTypeId(Id resultId)450     spv::Id getTypeId(Id resultId) const {
451         return idToInstruction[resultId] == nullptr ? NoType : idToInstruction[resultId]->getTypeId();
452     }
getStorageClass(Id typeId)453     StorageClass getStorageClass(Id typeId) const
454     {
455         assert(idToInstruction[typeId]->getOpCode() == spv::OpTypePointer);
456         return (StorageClass)idToInstruction[typeId]->getImmediateOperand(0);
457     }
458 
dump(std::vector<unsigned int> & out)459     void dump(std::vector<unsigned int>& out) const
460     {
461         for (int f = 0; f < (int)functions.size(); ++f)
462             functions[f]->dump(out);
463     }
464 
465 protected:
466     Module(const Module&);
467     std::vector<Function*> functions;
468 
469     // map from result id to instruction having that result id
470     std::vector<Instruction*> idToInstruction;
471 
472     // map from a result id to its type id
473 };
474 
475 //
476 // Implementation (it's here due to circular type definitions).
477 //
478 
479 // Add both
480 // - the OpFunction instruction
481 // - all the OpFunctionParameter instructions
Function(Id id,Id resultType,Id functionType,Id firstParamId,LinkageType linkage,const std::string & name,Module & parent)482 __inline Function::Function(Id id, Id resultType, Id functionType, Id firstParamId, LinkageType linkage, const std::string& name, Module& parent)
483     : parent(parent), lineInstruction(nullptr),
484       functionInstruction(id, resultType, OpFunction), implicitThis(false),
485       reducedPrecisionReturn(false),
486       linkType(linkage)
487 {
488     // OpFunction
489     functionInstruction.addImmediateOperand(FunctionControlMaskNone);
490     functionInstruction.addIdOperand(functionType);
491     parent.mapInstruction(&functionInstruction);
492     parent.addFunction(this);
493 
494     // OpFunctionParameter
495     Instruction* typeInst = parent.getInstruction(functionType);
496     int numParams = typeInst->getNumOperands() - 1;
497     for (int p = 0; p < numParams; ++p) {
498         Instruction* param = new Instruction(firstParamId + p, typeInst->getIdOperand(p + 1), OpFunctionParameter);
499         parent.mapInstruction(param);
500         parameterInstructions.push_back(param);
501     }
502 
503     // If importing/exporting, save the function name (without the mangled parameters) for the linkage decoration
504     if (linkType != LinkageTypeMax) {
505         exportName = name.substr(0, name.find_first_of('('));
506     }
507 }
508 
addLocalVariable(std::unique_ptr<Instruction> inst)509 __inline void Function::addLocalVariable(std::unique_ptr<Instruction> inst)
510 {
511     Instruction* raw_instruction = inst.get();
512     blocks[0]->addLocalVariable(std::move(inst));
513     parent.mapInstruction(raw_instruction);
514 }
515 
Block(Id id,Function & parent)516 __inline Block::Block(Id id, Function& parent) : parent(parent), unreachable(false)
517 {
518     instructions.push_back(std::unique_ptr<Instruction>(new Instruction(id, NoType, OpLabel)));
519     instructions.back()->setBlock(this);
520     parent.getParent().mapInstruction(instructions.back().get());
521 }
522 
addInstruction(std::unique_ptr<Instruction> inst)523 __inline void Block::addInstruction(std::unique_ptr<Instruction> inst)
524 {
525     Instruction* raw_instruction = inst.get();
526     instructions.push_back(std::move(inst));
527     raw_instruction->setBlock(this);
528     if (raw_instruction->getResultId())
529         parent.getParent().mapInstruction(raw_instruction);
530 }
531 
532 }  // end spv namespace
533 
534 #endif // spvIR_H
535