#include <iostream>
#include <memory>
#include <vector>
+#include <set>
namespace spv {
struct IdImmediate {
bool isId; // true if word is an Id, false if word is an immediate
unsigned word;
+ IdImmediate(bool i, unsigned w) : isId(i), word(w) {}
};
//
operands.push_back(immediate);
idOperand.push_back(false);
}
+ void setImmediateOperand(unsigned idx, unsigned int immediate) {
+ assert(!idOperand[idx]);
+ operands[idx] = immediate;
+ }
+
void addStringOperand(const char* str)
{
- unsigned int word;
- char* wordString = (char*)&word;
- char* wordPtr = wordString;
- int charCount = 0;
+ unsigned int word = 0;
+ unsigned int shiftAmount = 0;
char c;
+
do {
c = *(str++);
- *(wordPtr++) = c;
- ++charCount;
- if (charCount == 4) {
+ word |= ((unsigned int)c) << shiftAmount;
+ shiftAmount += 8;
+ if (shiftAmount == 32) {
addImmediateOperand(word);
- wordPtr = wordString;
- charCount = 0;
+ word = 0;
+ shiftAmount = 0;
}
} while (c != 0);
// deal with partial last word
- if (charCount > 0) {
- // pad with 0s
- for (; charCount < 4; ++charCount)
- *(wordPtr++) = 0;
+ if (shiftAmount > 0) {
addImmediateOperand(word);
}
}
const std::vector<std::unique_ptr<Instruction> >& getInstructions() const {
return instructions;
}
+ const std::vector<std::unique_ptr<Instruction> >& getLocalVariables() const { return localVariables; }
void setUnreachable() { unreachable = true; }
bool isUnreachable() const { return unreachable; }
// Returns the block's merge instruction, if one exists (otherwise null).
return nullptr;
}
+ // Change this block into a canonical dead merge block. Delete instructions
+ // as necessary. A canonical dead merge block has only an OpLabel and an
+ // OpUnreachable.
+ void rewriteAsCanonicalUnreachableMerge() {
+ assert(localVariables.empty());
+ // Delete all instructions except for the label.
+ assert(instructions.size() > 0);
+ instructions.resize(1);
+ successors.clear();
+ addInstruction(std::unique_ptr<Instruction>(new Instruction(OpUnreachable)));
+ }
+ // Change this block into a canonical dead continue target branching to the
+ // given header ID. Delete instructions as necessary. A canonical dead continue
+ // target has only an OpLabel and an unconditional branch back to the corresponding
+ // header.
+ void rewriteAsCanonicalUnreachableContinue(Block* header) {
+ assert(localVariables.empty());
+ // Delete all instructions except for the label.
+ assert(instructions.size() > 0);
+ instructions.resize(1);
+ successors.clear();
+ // Add OpBranch back to the header.
+ assert(header != nullptr);
+ Instruction* branch = new Instruction(OpBranch);
+ branch->addIdOperand(header->getId());
+ addInstruction(std::unique_ptr<Instruction>(branch));
+ successors.push_back(header);
+ }
+
bool isTerminated() const
{
switch (instructions.back()->getOpCode()) {
case OpBranchConditional:
case OpSwitch:
case OpKill:
+ case OpTerminateInvocation:
case OpReturn:
case OpReturnValue:
+ case OpUnreachable:
return true;
default:
return false;
bool unreachable;
};
+// The different reasons for reaching a block in the inReadableOrder traversal.
+enum ReachReason {
+ // Reachable from the entry block via transfers of control, i.e. branches.
+ ReachViaControlFlow = 0,
+ // A continue target that is not reachable via control flow.
+ ReachDeadContinue,
+ // A merge block that is not reachable via control flow.
+ ReachDeadMerge
+};
+
// Traverses the control-flow graph rooted at root in an order suited for
// readable code generation. Invokes callback at every node in the traversal
-// order.
-void inReadableOrder(Block* root, std::function<void(Block*)> callback);
+// order. The callback arguments are:
+// - the block,
+// - the reason we reached the block,
+// - if the reason was that block is an unreachable continue or unreachable merge block
+// then the last parameter is the corresponding header block.
+void inReadableOrder(Block* root, std::function<void(Block*, ReachReason, Block* header)> callback);
//
// SPIR-V IR Function.
const std::vector<Block*>& getBlocks() const { return blocks; }
void addLocalVariable(std::unique_ptr<Instruction> inst);
Id getReturnType() const { return functionInstruction.getTypeId(); }
+ Id getFuncId() const { return functionInstruction.getResultId(); }
+ void setReturnPrecision(Decoration precision)
+ {
+ if (precision == DecorationRelaxedPrecision)
+ reducedPrecisionReturn = true;
+ }
+ Decoration getReturnPrecision() const
+ { return reducedPrecisionReturn ? DecorationRelaxedPrecision : NoPrecision; }
+
+ void setDebugLineInfo(Id fileName, int line, int column) {
+ lineInstruction = std::unique_ptr<Instruction>{new Instruction(OpLine)};
+ lineInstruction->addIdOperand(fileName);
+ lineInstruction->addImmediateOperand(line);
+ lineInstruction->addImmediateOperand(column);
+ }
+ bool hasDebugLineInfo() const { return lineInstruction != nullptr; }
void setImplicitThis() { implicitThis = true; }
bool hasImplicitThis() const { return implicitThis; }
+ void addParamPrecision(unsigned param, Decoration precision)
+ {
+ if (precision == DecorationRelaxedPrecision)
+ reducedPrecisionParams.insert(param);
+ }
+ Decoration getParamPrecision(unsigned param) const
+ {
+ return reducedPrecisionParams.find(param) != reducedPrecisionParams.end() ?
+ DecorationRelaxedPrecision : NoPrecision;
+ }
+
void dump(std::vector<unsigned int>& out) const
{
+ // OpLine
+ if (lineInstruction != nullptr) {
+ lineInstruction->dump(out);
+ }
+
// OpFunction
functionInstruction.dump(out);
parameterInstructions[p]->dump(out);
// Blocks
- inReadableOrder(blocks[0], [&out](const Block* b) { b->dump(out); });
+ inReadableOrder(blocks[0], [&out](const Block* b, ReachReason, Block*) { b->dump(out); });
Instruction end(0, 0, OpFunctionEnd);
end.dump(out);
}
Function& operator=(Function&);
Module& parent;
+ std::unique_ptr<Instruction> lineInstruction;
Instruction functionInstruction;
std::vector<Instruction*> parameterInstructions;
std::vector<Block*> blocks;
bool implicitThis; // true if this is a member function expecting to be passed a 'this' as the first argument
+ bool reducedPrecisionReturn;
+ std::set<int> reducedPrecisionParams; // list of parameter indexes that need a relaxed precision arg
};
//
// - the OpFunction instruction
// - all the OpFunctionParameter instructions
__inline Function::Function(Id id, Id resultType, Id functionType, Id firstParamId, Module& parent)
- : parent(parent), functionInstruction(id, resultType, OpFunction), implicitThis(false)
+ : parent(parent), lineInstruction(nullptr),
+ functionInstruction(id, resultType, OpFunction), implicitThis(false),
+ reducedPrecisionReturn(false)
{
// OpFunction
functionInstruction.addImmediateOperand(FunctionControlMaskNone);
parent.getParent().mapInstruction(raw_instruction);
}
-}; // end spv namespace
+} // end spv namespace
#endif // spvIR_H