2015-06-26 22:58:36 +00:00
|
|
|
//
|
2017-01-06 19:34:14 +00:00
|
|
|
// Copyright (C) 2014 LunarG, Inc.
|
2018-12-14 17:47:35 +00:00
|
|
|
// Copyright (C) 2015-2018 Google, Inc.
|
2015-06-26 22:58:36 +00:00
|
|
|
//
|
2017-01-06 19:34:14 +00:00
|
|
|
// All rights reserved.
|
2015-06-26 22:58:36 +00:00
|
|
|
//
|
2017-01-06 19:34:14 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions
|
|
|
|
// are met:
|
2015-06-26 22:58:36 +00:00
|
|
|
//
|
|
|
|
// Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
//
|
|
|
|
// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
2017-01-06 19:34:14 +00:00
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
// SPIRV-IR
|
|
|
|
//
|
|
|
|
// Simple in-memory representation (IR) of SPIRV. Just for holding
|
|
|
|
// Each function's CFG of blocks. Has this hierarchy:
|
2017-01-06 07:34:48 +00:00
|
|
|
// - Module, which is a list of
|
|
|
|
// - Function, which is a list of
|
|
|
|
// - Block, which is a list of
|
2015-06-26 22:58:36 +00:00
|
|
|
// - Instruction
|
|
|
|
//
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#ifndef spvIR_H
|
|
|
|
#define spvIR_H
|
|
|
|
|
2015-08-07 04:53:06 +00:00
|
|
|
#include "spirv.hpp"
|
2015-06-26 22:58:36 +00:00
|
|
|
|
2016-01-11 00:37:00 +00:00
|
|
|
#include <algorithm>
|
2016-01-18 21:18:37 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <functional>
|
2015-06-26 22:58:36 +00:00
|
|
|
#include <iostream>
|
2016-01-18 14:23:56 +00:00
|
|
|
#include <memory>
|
2016-01-18 21:18:37 +00:00
|
|
|
#include <vector>
|
2020-06-26 14:37:06 +00:00
|
|
|
#include <set>
|
2024-02-09 18:27:40 +00:00
|
|
|
#include <optional>
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
namespace spv {
|
|
|
|
|
2016-01-19 15:01:27 +00:00
|
|
|
class Block;
|
2015-06-26 22:58:36 +00:00
|
|
|
class Function;
|
|
|
|
class Module;
|
|
|
|
|
|
|
|
const Id NoResult = 0;
|
|
|
|
const Id NoType = 0;
|
|
|
|
|
2016-07-15 17:53:56 +00:00
|
|
|
const Decoration NoPrecision = DecorationMax;
|
2017-08-31 19:44:34 +00:00
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
# define POTENTIALLY_UNUSED __attribute__((unused))
|
|
|
|
#else
|
|
|
|
# define POTENTIALLY_UNUSED
|
|
|
|
#endif
|
|
|
|
|
|
|
|
POTENTIALLY_UNUSED
|
2017-01-06 07:34:48 +00:00
|
|
|
const MemorySemanticsMask MemorySemanticsAllMemory =
|
2017-12-11 11:02:24 +00:00
|
|
|
(MemorySemanticsMask)(MemorySemanticsUniformMemoryMask |
|
2016-01-22 17:15:03 +00:00
|
|
|
MemorySemanticsWorkgroupMemoryMask |
|
|
|
|
MemorySemanticsAtomicCounterMemoryMask |
|
|
|
|
MemorySemanticsImageMemoryMask);
|
2015-06-26 22:58:36 +00:00
|
|
|
|
2018-08-14 19:31:43 +00:00
|
|
|
struct IdImmediate {
|
|
|
|
bool isId; // true if word is an Id, false if word is an immediate
|
|
|
|
unsigned word;
|
2019-02-19 19:10:32 +00:00
|
|
|
IdImmediate(bool i, unsigned w) : isId(i), word(w) {}
|
2018-08-14 19:31:43 +00:00
|
|
|
};
|
|
|
|
|
2015-06-26 22:58:36 +00:00
|
|
|
//
|
|
|
|
// SPIR-V IR instruction.
|
|
|
|
//
|
|
|
|
|
|
|
|
class Instruction {
|
|
|
|
public:
|
2016-01-19 19:08:32 +00:00
|
|
|
Instruction(Id resultId, Id typeId, Op opCode) : resultId(resultId), typeId(typeId), opCode(opCode), block(nullptr) { }
|
|
|
|
explicit Instruction(Op opCode) : resultId(NoResult), typeId(NoType), opCode(opCode), block(nullptr) { }
|
2015-11-16 04:33:39 +00:00
|
|
|
virtual ~Instruction() {}
|
2024-04-01 16:51:29 +00:00
|
|
|
void reserveOperands(size_t count) {
|
|
|
|
operands.reserve(count);
|
|
|
|
idOperand.reserve(count);
|
|
|
|
}
|
2018-08-14 03:37:59 +00:00
|
|
|
void addIdOperand(Id id) {
|
2023-06-22 20:11:29 +00:00
|
|
|
// ids can't be 0
|
|
|
|
assert(id);
|
2018-08-14 03:37:59 +00:00
|
|
|
operands.push_back(id);
|
|
|
|
idOperand.push_back(true);
|
|
|
|
}
|
2024-05-20 17:25:55 +00:00
|
|
|
// This method is potentially dangerous as it can break assumptions
|
|
|
|
// about SSA and lack of forward references.
|
|
|
|
void setIdOperand(unsigned idx, Id id) {
|
|
|
|
assert(id);
|
|
|
|
assert(idOperand[idx]);
|
|
|
|
operands[idx] = id;
|
|
|
|
}
|
|
|
|
|
2018-08-14 03:37:59 +00:00
|
|
|
void addImmediateOperand(unsigned int immediate) {
|
|
|
|
operands.push_back(immediate);
|
|
|
|
idOperand.push_back(false);
|
|
|
|
}
|
2019-01-06 23:58:04 +00:00
|
|
|
void setImmediateOperand(unsigned idx, unsigned int immediate) {
|
|
|
|
assert(!idOperand[idx]);
|
|
|
|
operands[idx] = immediate;
|
|
|
|
}
|
|
|
|
|
2015-06-26 22:58:36 +00:00
|
|
|
void addStringOperand(const char* str)
|
|
|
|
{
|
2021-10-19 16:09:52 +00:00
|
|
|
unsigned int word = 0;
|
|
|
|
unsigned int shiftAmount = 0;
|
2015-06-26 22:58:36 +00:00
|
|
|
char c;
|
2021-10-19 16:09:52 +00:00
|
|
|
|
2015-06-26 22:58:36 +00:00
|
|
|
do {
|
|
|
|
c = *(str++);
|
2021-10-19 16:09:52 +00:00
|
|
|
word |= ((unsigned int)c) << shiftAmount;
|
|
|
|
shiftAmount += 8;
|
|
|
|
if (shiftAmount == 32) {
|
2015-11-16 04:33:39 +00:00
|
|
|
addImmediateOperand(word);
|
2021-10-19 16:09:52 +00:00
|
|
|
word = 0;
|
|
|
|
shiftAmount = 0;
|
2015-06-26 22:58:36 +00:00
|
|
|
}
|
|
|
|
} while (c != 0);
|
|
|
|
|
|
|
|
// deal with partial last word
|
2021-10-19 16:09:52 +00:00
|
|
|
if (shiftAmount > 0) {
|
2015-11-16 04:33:39 +00:00
|
|
|
addImmediateOperand(word);
|
2015-06-26 22:58:36 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-15 19:54:09 +00:00
|
|
|
bool isIdOperand(int op) const { return idOperand[op]; }
|
2016-01-19 15:01:27 +00:00
|
|
|
void setBlock(Block* b) { block = b; }
|
2016-01-19 16:31:55 +00:00
|
|
|
Block* getBlock() const { return block; }
|
2015-06-26 22:58:36 +00:00
|
|
|
Op getOpCode() const { return opCode; }
|
2018-08-14 03:37:59 +00:00
|
|
|
int getNumOperands() const
|
|
|
|
{
|
|
|
|
assert(operands.size() == idOperand.size());
|
|
|
|
return (int)operands.size();
|
|
|
|
}
|
2015-06-26 22:58:36 +00:00
|
|
|
Id getResultId() const { return resultId; }
|
|
|
|
Id getTypeId() const { return typeId; }
|
2018-08-14 03:37:59 +00:00
|
|
|
Id getIdOperand(int op) const {
|
|
|
|
assert(idOperand[op]);
|
|
|
|
return operands[op];
|
|
|
|
}
|
|
|
|
unsigned int getImmediateOperand(int op) const {
|
|
|
|
assert(!idOperand[op]);
|
|
|
|
return operands[op];
|
|
|
|
}
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
// Write out the binary form.
|
|
|
|
void dump(std::vector<unsigned int>& out) const
|
|
|
|
{
|
|
|
|
// Compute the wordCount
|
|
|
|
unsigned int wordCount = 1;
|
|
|
|
if (typeId)
|
|
|
|
++wordCount;
|
|
|
|
if (resultId)
|
|
|
|
++wordCount;
|
|
|
|
wordCount += (unsigned int)operands.size();
|
|
|
|
|
|
|
|
// Write out the beginning of the instruction
|
|
|
|
out.push_back(((wordCount) << WordCountShift) | opCode);
|
|
|
|
if (typeId)
|
|
|
|
out.push_back(typeId);
|
|
|
|
if (resultId)
|
|
|
|
out.push_back(resultId);
|
|
|
|
|
|
|
|
// Write out the operands
|
|
|
|
for (int op = 0; op < (int)operands.size(); ++op)
|
|
|
|
out.push_back(operands[op]);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Instruction(const Instruction&);
|
|
|
|
Id resultId;
|
|
|
|
Id typeId;
|
|
|
|
Op opCode;
|
2018-08-14 03:37:59 +00:00
|
|
|
std::vector<Id> operands; // operands, both <id> and immediates (both are unsigned int)
|
|
|
|
std::vector<bool> idOperand; // true for operands that are <id>, false for immediates
|
2016-01-19 15:01:27 +00:00
|
|
|
Block* block;
|
2015-06-26 22:58:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// SPIR-V IR block.
|
|
|
|
//
|
|
|
|
|
2024-02-09 18:27:40 +00:00
|
|
|
struct DebugSourceLocation {
|
|
|
|
int line;
|
|
|
|
int column;
|
|
|
|
spv::Id fileId;
|
|
|
|
};
|
|
|
|
|
2015-06-26 22:58:36 +00:00
|
|
|
class Block {
|
|
|
|
public:
|
|
|
|
Block(Id id, Function& parent);
|
|
|
|
virtual ~Block()
|
|
|
|
{
|
|
|
|
}
|
2016-01-18 21:18:37 +00:00
|
|
|
|
2015-06-26 22:58:36 +00:00
|
|
|
Id getId() { return instructions.front()->getResultId(); }
|
|
|
|
|
|
|
|
Function& getParent() const { return parent; }
|
2024-02-09 18:27:40 +00:00
|
|
|
// Returns true if the source location is actually updated.
|
|
|
|
// Note we still need the builder to insert the line marker instruction. This is just a tracker.
|
|
|
|
bool updateDebugSourceLocation(int line, int column, spv::Id fileId) {
|
|
|
|
if (currentSourceLoc && currentSourceLoc->line == line && currentSourceLoc->column == column &&
|
|
|
|
currentSourceLoc->fileId == fileId) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentSourceLoc = DebugSourceLocation{line, column, fileId};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Returns true if the scope is actually updated.
|
|
|
|
// Note we still need the builder to insert the debug scope instruction. This is just a tracker.
|
|
|
|
bool updateDebugScope(spv::Id scopeId) {
|
|
|
|
assert(scopeId);
|
|
|
|
if (currentDebugScope && *currentDebugScope == scopeId) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentDebugScope = scopeId;
|
|
|
|
return true;
|
|
|
|
}
|
2016-01-18 14:23:56 +00:00
|
|
|
void addInstruction(std::unique_ptr<Instruction> inst);
|
2016-01-18 04:27:45 +00:00
|
|
|
void addPredecessor(Block* pred) { predecessors.push_back(pred); pred->successors.push_back(this);}
|
2016-01-18 14:23:56 +00:00
|
|
|
void addLocalVariable(std::unique_ptr<Instruction> inst) { localVariables.push_back(std::move(inst)); }
|
2016-01-19 15:01:27 +00:00
|
|
|
const std::vector<Block*>& getPredecessors() const { return predecessors; }
|
|
|
|
const std::vector<Block*>& getSuccessors() const { return successors; }
|
2024-05-20 17:25:55 +00:00
|
|
|
std::vector<std::unique_ptr<Instruction> >& getInstructions() {
|
2016-03-10 00:54:03 +00:00
|
|
|
return instructions;
|
|
|
|
}
|
2019-01-06 23:58:04 +00:00
|
|
|
const std::vector<std::unique_ptr<Instruction> >& getLocalVariables() const { return localVariables; }
|
2015-06-26 22:58:36 +00:00
|
|
|
void setUnreachable() { unreachable = true; }
|
|
|
|
bool isUnreachable() const { return unreachable; }
|
2016-01-18 21:18:37 +00:00
|
|
|
// Returns the block's merge instruction, if one exists (otherwise null).
|
|
|
|
const Instruction* getMergeInstruction() const {
|
|
|
|
if (instructions.size() < 2) return nullptr;
|
2016-01-19 15:17:33 +00:00
|
|
|
const Instruction* nextToLast = (instructions.cend() - 2)->get();
|
2016-01-18 21:18:37 +00:00
|
|
|
switch (nextToLast->getOpCode()) {
|
|
|
|
case OpSelectionMerge:
|
|
|
|
case OpLoopMerge:
|
|
|
|
return nextToLast;
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-06-26 22:58:36 +00:00
|
|
|
|
2019-10-21 18:50:31 +00:00
|
|
|
// 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();
|
2020-04-01 06:46:57 +00:00
|
|
|
addInstruction(std::unique_ptr<Instruction>(new Instruction(OpUnreachable)));
|
2019-10-21 18:50:31 +00:00
|
|
|
}
|
|
|
|
// 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());
|
2019-11-04 21:23:11 +00:00
|
|
|
addInstruction(std::unique_ptr<Instruction>(branch));
|
2019-10-21 18:50:31 +00:00
|
|
|
successors.push_back(header);
|
|
|
|
}
|
|
|
|
|
2015-06-26 22:58:36 +00:00
|
|
|
bool isTerminated() const
|
|
|
|
{
|
|
|
|
switch (instructions.back()->getOpCode()) {
|
|
|
|
case OpBranch:
|
|
|
|
case OpBranchConditional:
|
|
|
|
case OpSwitch:
|
|
|
|
case OpKill:
|
2020-11-09 16:30:01 +00:00
|
|
|
case OpTerminateInvocation:
|
2015-06-26 22:58:36 +00:00
|
|
|
case OpReturn:
|
|
|
|
case OpReturnValue:
|
2019-10-21 18:50:31 +00:00
|
|
|
case OpUnreachable:
|
2015-06-26 22:58:36 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void dump(std::vector<unsigned int>& out) const
|
|
|
|
{
|
|
|
|
instructions[0]->dump(out);
|
|
|
|
for (int i = 0; i < (int)localVariables.size(); ++i)
|
|
|
|
localVariables[i]->dump(out);
|
|
|
|
for (int i = 1; i < (int)instructions.size(); ++i)
|
|
|
|
instructions[i]->dump(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Block(const Block&);
|
|
|
|
Block& operator=(Block&);
|
|
|
|
|
|
|
|
// To enforce keeping parent and ownership in sync:
|
|
|
|
friend Function;
|
|
|
|
|
2016-01-18 14:23:56 +00:00
|
|
|
std::vector<std::unique_ptr<Instruction> > instructions;
|
2016-01-18 04:27:45 +00:00
|
|
|
std::vector<Block*> predecessors, successors;
|
2016-01-18 14:23:56 +00:00
|
|
|
std::vector<std::unique_ptr<Instruction> > localVariables;
|
2015-06-26 22:58:36 +00:00
|
|
|
Function& parent;
|
|
|
|
|
2024-02-09 18:27:40 +00:00
|
|
|
// Track source location of the last source location marker instruction.
|
|
|
|
std::optional<DebugSourceLocation> currentSourceLoc;
|
|
|
|
|
|
|
|
// Track scope of the last debug scope instruction.
|
|
|
|
std::optional<spv::Id> currentDebugScope;
|
|
|
|
|
2017-01-06 07:34:48 +00:00
|
|
|
// track whether this block is known to be uncreachable (not necessarily
|
2015-06-26 22:58:36 +00:00
|
|
|
// true for all unreachable blocks, but should be set at least
|
|
|
|
// for the extraneous ones introduced by the builder).
|
|
|
|
bool unreachable;
|
|
|
|
};
|
|
|
|
|
2019-10-21 18:50:31 +00:00
|
|
|
// The different reasons for reaching a block in the inReadableOrder traversal.
|
2019-11-03 03:26:40 +00:00
|
|
|
enum ReachReason {
|
2019-10-21 18:50:31 +00:00
|
|
|
// 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
|
|
|
|
};
|
|
|
|
|
2016-01-18 21:18:37 +00:00
|
|
|
// Traverses the control-flow graph rooted at root in an order suited for
|
|
|
|
// readable code generation. Invokes callback at every node in the traversal
|
2019-10-21 18:50:31 +00:00
|
|
|
// 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);
|
2016-01-18 21:18:37 +00:00
|
|
|
|
2015-06-26 22:58:36 +00:00
|
|
|
//
|
|
|
|
// SPIR-V IR Function.
|
|
|
|
//
|
|
|
|
|
|
|
|
class Function {
|
|
|
|
public:
|
2023-08-17 19:49:18 +00:00
|
|
|
Function(Id id, Id resultType, Id functionType, Id firstParam, LinkageType linkage, const std::string& name, Module& parent);
|
2015-06-26 22:58:36 +00:00
|
|
|
virtual ~Function()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < (int)parameterInstructions.size(); ++i)
|
|
|
|
delete parameterInstructions[i];
|
|
|
|
|
|
|
|
for (int i = 0; i < (int)blocks.size(); ++i)
|
|
|
|
delete blocks[i];
|
|
|
|
}
|
|
|
|
Id getId() const { return functionInstruction.getResultId(); }
|
2018-05-04 17:43:03 +00:00
|
|
|
Id getParamId(int p) const { return parameterInstructions[p]->getResultId(); }
|
|
|
|
Id getParamType(int p) const { return parameterInstructions[p]->getTypeId(); }
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
void addBlock(Block* block) { blocks.push_back(block); }
|
2016-01-11 00:37:00 +00:00
|
|
|
void removeBlock(Block* block)
|
|
|
|
{
|
|
|
|
auto found = find(blocks.begin(), blocks.end(), block);
|
|
|
|
assert(found != blocks.end());
|
|
|
|
blocks.erase(found);
|
|
|
|
delete block;
|
|
|
|
}
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
Module& getParent() const { return parent; }
|
|
|
|
Block* getEntryBlock() const { return blocks.front(); }
|
|
|
|
Block* getLastBlock() const { return blocks.back(); }
|
2016-03-10 00:54:03 +00:00
|
|
|
const std::vector<Block*>& getBlocks() const { return blocks; }
|
2016-01-18 14:23:56 +00:00
|
|
|
void addLocalVariable(std::unique_ptr<Instruction> inst);
|
2015-06-26 22:58:36 +00:00
|
|
|
Id getReturnType() const { return functionInstruction.getTypeId(); }
|
2021-12-09 23:26:48 +00:00
|
|
|
Id getFuncId() const { return functionInstruction.getResultId(); }
|
2023-10-12 04:42:55 +00:00
|
|
|
Id getFuncTypeId() const { return functionInstruction.getIdOperand(1); }
|
2020-06-30 07:27:08 +00:00
|
|
|
void setReturnPrecision(Decoration precision)
|
|
|
|
{
|
|
|
|
if (precision == DecorationRelaxedPrecision)
|
|
|
|
reducedPrecisionReturn = true;
|
|
|
|
}
|
|
|
|
Decoration getReturnPrecision() const
|
|
|
|
{ return reducedPrecisionReturn ? DecorationRelaxedPrecision : NoPrecision; }
|
2017-03-22 05:56:40 +00:00
|
|
|
|
2022-05-24 06:05:43 +00:00
|
|
|
void setDebugLineInfo(Id fileName, int line, int column) {
|
2022-05-31 17:40:25 +00:00
|
|
|
lineInstruction = std::unique_ptr<Instruction>{new Instruction(OpLine)};
|
2024-04-01 16:51:29 +00:00
|
|
|
lineInstruction->reserveOperands(3);
|
2022-05-31 06:08:50 +00:00
|
|
|
lineInstruction->addIdOperand(fileName);
|
|
|
|
lineInstruction->addImmediateOperand(line);
|
|
|
|
lineInstruction->addImmediateOperand(column);
|
2022-05-24 06:05:43 +00:00
|
|
|
}
|
2022-05-31 06:08:50 +00:00
|
|
|
bool hasDebugLineInfo() const { return lineInstruction != nullptr; }
|
2022-05-24 06:05:43 +00:00
|
|
|
|
2017-03-22 05:56:40 +00:00
|
|
|
void setImplicitThis() { implicitThis = true; }
|
|
|
|
bool hasImplicitThis() const { return implicitThis; }
|
|
|
|
|
2020-06-30 07:27:08 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-06-26 14:37:06 +00:00
|
|
|
|
2015-06-26 22:58:36 +00:00
|
|
|
void dump(std::vector<unsigned int>& out) const
|
|
|
|
{
|
2022-05-24 06:05:43 +00:00
|
|
|
// OpLine
|
2022-05-31 06:08:50 +00:00
|
|
|
if (lineInstruction != nullptr) {
|
|
|
|
lineInstruction->dump(out);
|
2022-05-24 06:05:43 +00:00
|
|
|
}
|
2023-05-22 15:44:18 +00:00
|
|
|
|
2015-06-26 22:58:36 +00:00
|
|
|
// OpFunction
|
|
|
|
functionInstruction.dump(out);
|
|
|
|
|
|
|
|
// OpFunctionParameter
|
|
|
|
for (int p = 0; p < (int)parameterInstructions.size(); ++p)
|
|
|
|
parameterInstructions[p]->dump(out);
|
|
|
|
|
|
|
|
// Blocks
|
2019-10-21 18:50:31 +00:00
|
|
|
inReadableOrder(blocks[0], [&out](const Block* b, ReachReason, Block*) { b->dump(out); });
|
2015-06-26 22:58:36 +00:00
|
|
|
Instruction end(0, 0, OpFunctionEnd);
|
|
|
|
end.dump(out);
|
|
|
|
}
|
|
|
|
|
2023-08-17 19:49:18 +00:00
|
|
|
LinkageType getLinkType() const { return linkType; }
|
|
|
|
const char* getExportName() const { return exportName.c_str(); }
|
|
|
|
|
2015-06-26 22:58:36 +00:00
|
|
|
protected:
|
|
|
|
Function(const Function&);
|
|
|
|
Function& operator=(Function&);
|
|
|
|
|
|
|
|
Module& parent;
|
2022-05-31 06:08:50 +00:00
|
|
|
std::unique_ptr<Instruction> lineInstruction;
|
2015-06-26 22:58:36 +00:00
|
|
|
Instruction functionInstruction;
|
|
|
|
std::vector<Instruction*> parameterInstructions;
|
|
|
|
std::vector<Block*> blocks;
|
2017-03-22 05:56:40 +00:00
|
|
|
bool implicitThis; // true if this is a member function expecting to be passed a 'this' as the first argument
|
2020-06-30 07:27:08 +00:00
|
|
|
bool reducedPrecisionReturn;
|
2020-06-26 14:37:06 +00:00
|
|
|
std::set<int> reducedPrecisionParams; // list of parameter indexes that need a relaxed precision arg
|
2023-08-17 19:49:18 +00:00
|
|
|
LinkageType linkType;
|
|
|
|
std::string exportName;
|
2015-06-26 22:58:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// SPIR-V IR Module.
|
|
|
|
//
|
|
|
|
|
|
|
|
class Module {
|
|
|
|
public:
|
|
|
|
Module() {}
|
|
|
|
virtual ~Module()
|
|
|
|
{
|
|
|
|
// TODO delete things
|
|
|
|
}
|
|
|
|
|
|
|
|
void addFunction(Function *fun) { functions.push_back(fun); }
|
|
|
|
|
|
|
|
void mapInstruction(Instruction *instruction)
|
|
|
|
{
|
|
|
|
spv::Id resultId = instruction->getResultId();
|
|
|
|
// map the instruction's result id
|
|
|
|
if (resultId >= idToInstruction.size())
|
|
|
|
idToInstruction.resize(resultId + 16);
|
|
|
|
idToInstruction[resultId] = instruction;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction* getInstruction(Id id) const { return idToInstruction[id]; }
|
2016-03-10 00:54:03 +00:00
|
|
|
const std::vector<Function*>& getFunctions() const { return functions; }
|
2018-08-14 19:31:43 +00:00
|
|
|
spv::Id getTypeId(Id resultId) const {
|
|
|
|
return idToInstruction[resultId] == nullptr ? NoType : idToInstruction[resultId]->getTypeId();
|
|
|
|
}
|
2015-12-09 03:48:49 +00:00
|
|
|
StorageClass getStorageClass(Id typeId) const
|
|
|
|
{
|
|
|
|
assert(idToInstruction[typeId]->getOpCode() == spv::OpTypePointer);
|
|
|
|
return (StorageClass)idToInstruction[typeId]->getImmediateOperand(0);
|
|
|
|
}
|
|
|
|
|
2015-06-26 22:58:36 +00:00
|
|
|
void dump(std::vector<unsigned int>& out) const
|
|
|
|
{
|
|
|
|
for (int f = 0; f < (int)functions.size(); ++f)
|
|
|
|
functions[f]->dump(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Module(const Module&);
|
|
|
|
std::vector<Function*> functions;
|
|
|
|
|
|
|
|
// map from result id to instruction having that result id
|
|
|
|
std::vector<Instruction*> idToInstruction;
|
|
|
|
|
|
|
|
// map from a result id to its type id
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Implementation (it's here due to circular type definitions).
|
|
|
|
//
|
|
|
|
|
|
|
|
// Add both
|
|
|
|
// - the OpFunction instruction
|
|
|
|
// - all the OpFunctionParameter instructions
|
2023-08-17 19:49:18 +00:00
|
|
|
__inline Function::Function(Id id, Id resultType, Id functionType, Id firstParamId, LinkageType linkage, const std::string& name, Module& parent)
|
2022-05-31 06:08:50 +00:00
|
|
|
: parent(parent), lineInstruction(nullptr),
|
2022-05-24 06:05:43 +00:00
|
|
|
functionInstruction(id, resultType, OpFunction), implicitThis(false),
|
2023-08-17 19:49:18 +00:00
|
|
|
reducedPrecisionReturn(false),
|
|
|
|
linkType(linkage)
|
2015-06-26 22:58:36 +00:00
|
|
|
{
|
|
|
|
// OpFunction
|
2024-04-01 16:51:29 +00:00
|
|
|
functionInstruction.reserveOperands(2);
|
2015-06-26 22:58:36 +00:00
|
|
|
functionInstruction.addImmediateOperand(FunctionControlMaskNone);
|
|
|
|
functionInstruction.addIdOperand(functionType);
|
|
|
|
parent.mapInstruction(&functionInstruction);
|
|
|
|
parent.addFunction(this);
|
|
|
|
|
|
|
|
// OpFunctionParameter
|
|
|
|
Instruction* typeInst = parent.getInstruction(functionType);
|
|
|
|
int numParams = typeInst->getNumOperands() - 1;
|
|
|
|
for (int p = 0; p < numParams; ++p) {
|
|
|
|
Instruction* param = new Instruction(firstParamId + p, typeInst->getIdOperand(p + 1), OpFunctionParameter);
|
|
|
|
parent.mapInstruction(param);
|
|
|
|
parameterInstructions.push_back(param);
|
|
|
|
}
|
2023-08-17 19:49:18 +00:00
|
|
|
|
|
|
|
// If importing/exporting, save the function name (without the mangled parameters) for the linkage decoration
|
|
|
|
if (linkType != LinkageTypeMax) {
|
|
|
|
exportName = name.substr(0, name.find_first_of('('));
|
|
|
|
}
|
2015-06-26 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
2016-01-18 14:23:56 +00:00
|
|
|
__inline void Function::addLocalVariable(std::unique_ptr<Instruction> inst)
|
2015-06-26 22:58:36 +00:00
|
|
|
{
|
2016-01-18 14:23:56 +00:00
|
|
|
Instruction* raw_instruction = inst.get();
|
|
|
|
blocks[0]->addLocalVariable(std::move(inst));
|
|
|
|
parent.mapInstruction(raw_instruction);
|
2015-06-26 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__inline Block::Block(Id id, Function& parent) : parent(parent), unreachable(false)
|
|
|
|
{
|
2016-01-18 14:23:56 +00:00
|
|
|
instructions.push_back(std::unique_ptr<Instruction>(new Instruction(id, NoType, OpLabel)));
|
2016-01-19 15:01:27 +00:00
|
|
|
instructions.back()->setBlock(this);
|
2016-01-19 15:17:33 +00:00
|
|
|
parent.getParent().mapInstruction(instructions.back().get());
|
2015-06-26 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
2016-01-18 14:23:56 +00:00
|
|
|
__inline void Block::addInstruction(std::unique_ptr<Instruction> inst)
|
2015-06-26 22:58:36 +00:00
|
|
|
{
|
2016-01-19 15:01:27 +00:00
|
|
|
Instruction* raw_instruction = inst.get();
|
2016-01-18 14:23:56 +00:00
|
|
|
instructions.push_back(std::move(inst));
|
2016-01-19 15:01:27 +00:00
|
|
|
raw_instruction->setBlock(this);
|
2016-01-18 14:23:56 +00:00
|
|
|
if (raw_instruction->getResultId())
|
|
|
|
parent.getParent().mapInstruction(raw_instruction);
|
2015-06-26 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 16:08:10 +00:00
|
|
|
} // end spv namespace
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
#endif // spvIR_H
|