glasm: Rework control flow introducing a syntax list
This commit regresses VertexA shaders, their transformation pass has to be adapted to the new control flow.
This commit is contained in:
parent
8a556b49cf
commit
03f6d424ce
33 changed files with 437 additions and 505 deletions
|
|
@ -51,6 +51,7 @@ add_library(shader_recompiler STATIC
|
|||
backend/spirv/emit_spirv_warp.cpp
|
||||
environment.h
|
||||
exception.h
|
||||
frontend/ir/abstract_syntax_list.h
|
||||
frontend/ir/attribute.cpp
|
||||
frontend/ir/attribute.h
|
||||
frontend/ir/basic_block.cpp
|
||||
|
|
|
|||
|
|
@ -117,8 +117,6 @@ auto Arg(EmitContext& ctx, const IR::Value& arg) {
|
|||
return Identity<const IR::Value&>{arg};
|
||||
} else if constexpr (std::is_same_v<ArgType, u32>) {
|
||||
return Identity{arg.U32()};
|
||||
} else if constexpr (std::is_same_v<ArgType, IR::Block*>) {
|
||||
return Identity{arg.Label()};
|
||||
} else if constexpr (std::is_same_v<ArgType, IR::Attribute>) {
|
||||
return Identity{arg.Attribute()};
|
||||
} else if constexpr (std::is_same_v<ArgType, IR::Patch>) {
|
||||
|
|
@ -177,6 +175,39 @@ void EmitInst(EmitContext& ctx, IR::Inst* inst) {
|
|||
throw LogicError("Invalid opcode {}", inst->GetOpcode());
|
||||
}
|
||||
|
||||
void EmitCode(EmitContext& ctx, const IR::Program& program) {
|
||||
const auto eval{
|
||||
[&](const IR::U1& cond) { return ScalarS32{ctx.reg_alloc.Consume(IR::Value{cond})}; }};
|
||||
for (const IR::AbstractSyntaxNode& node : program.syntax_list) {
|
||||
switch (node.type) {
|
||||
case IR::AbstractSyntaxNode::Type::Block:
|
||||
for (IR::Inst& inst : node.block->Instructions()) {
|
||||
EmitInst(ctx, &inst);
|
||||
}
|
||||
break;
|
||||
case IR::AbstractSyntaxNode::Type::If:
|
||||
ctx.Add("MOV.S.CC RC,{};IF NE.x;", eval(node.if_node.cond));
|
||||
break;
|
||||
case IR::AbstractSyntaxNode::Type::EndIf:
|
||||
ctx.Add("ENDIF;");
|
||||
break;
|
||||
case IR::AbstractSyntaxNode::Type::Loop:
|
||||
ctx.Add("REP;");
|
||||
break;
|
||||
case IR::AbstractSyntaxNode::Type::Repeat:
|
||||
ctx.Add("MOV.S.CC RC,{};BRK NE.x;ENDREP;", eval(node.repeat.cond));
|
||||
break;
|
||||
case IR::AbstractSyntaxNode::Type::Break:
|
||||
ctx.Add("MOV.S.CC RC,{};BRK NE.x;", eval(node.repeat.cond));
|
||||
break;
|
||||
case IR::AbstractSyntaxNode::Type::Return:
|
||||
case IR::AbstractSyntaxNode::Type::Unreachable:
|
||||
ctx.Add("RET;");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SetupOptions(std::string& header, Info info) {
|
||||
if (info.uses_int64_bit_atomics) {
|
||||
header += "OPTION NV_shader_atomic_int64;";
|
||||
|
|
@ -201,11 +232,7 @@ void SetupOptions(std::string& header, Info info) {
|
|||
|
||||
std::string EmitGLASM(const Profile&, IR::Program& program, Bindings&) {
|
||||
EmitContext ctx{program};
|
||||
for (IR::Block* const block : program.blocks) {
|
||||
for (IR::Inst& inst : block->Instructions()) {
|
||||
EmitInst(ctx, &inst);
|
||||
}
|
||||
}
|
||||
EmitCode(ctx, program);
|
||||
std::string header = "!!NVcp5.0\n"
|
||||
"OPTION NV_internal;";
|
||||
SetupOptions(header, program.info);
|
||||
|
|
|
|||
|
|
@ -22,13 +22,8 @@ class EmitContext;
|
|||
void EmitPhi(EmitContext& ctx, IR::Inst& inst);
|
||||
void EmitVoid(EmitContext& ctx);
|
||||
void EmitIdentity(EmitContext& ctx, IR::Inst& inst, const IR::Value& value);
|
||||
void EmitBranch(EmitContext& ctx);
|
||||
void EmitBranchConditional(EmitContext& ctx);
|
||||
void EmitLoopMerge(EmitContext& ctx);
|
||||
void EmitSelectionMerge(EmitContext& ctx);
|
||||
void EmitReturn(EmitContext& ctx);
|
||||
void EmitBranchConditionRef(EmitContext&);
|
||||
void EmitJoin(EmitContext& ctx);
|
||||
void EmitUnreachable(EmitContext& ctx);
|
||||
void EmitDemoteToHelperInvocation(EmitContext& ctx);
|
||||
void EmitBarrier(EmitContext& ctx);
|
||||
void EmitWorkgroupMemoryBarrier(EmitContext& ctx);
|
||||
|
|
|
|||
|
|
@ -91,7 +91,8 @@ void EmitBitFieldInsert(EmitContext& ctx, IR::Inst& inst, ScalarS32 base, Scalar
|
|||
if (count.type != Type::Register && offset.type != Type::Register) {
|
||||
ctx.Add("BFI.S {},{{{},{},0,0}},{},{};", ret, count, offset, insert, base);
|
||||
} else {
|
||||
ctx.Add("MOV.S RC.x,{};MOV.U RC.y,{};"
|
||||
ctx.Add("MOV.S RC.x,{};"
|
||||
"MOV.S RC.y,{};"
|
||||
"BFI.S {},RC,{},{};",
|
||||
count, offset, ret, insert, base);
|
||||
}
|
||||
|
|
@ -103,7 +104,8 @@ void EmitBitFieldSExtract(EmitContext& ctx, IR::Inst& inst, ScalarS32 base, Scal
|
|||
if (count.type != Type::Register && offset.type != Type::Register) {
|
||||
ctx.Add("BFE.S {},{{{},{},0,0}},{};", ret, count, offset, base);
|
||||
} else {
|
||||
ctx.Add("MOV.S RC.x,{};MOV.U RC.y,{};"
|
||||
ctx.Add("MOV.S RC.x,{};"
|
||||
"MOV.S RC.y,{};"
|
||||
"BFE.S {},RC,{};",
|
||||
count, offset, ret, base);
|
||||
}
|
||||
|
|
@ -115,7 +117,8 @@ void EmitBitFieldUExtract(EmitContext& ctx, IR::Inst& inst, ScalarU32 base, Scal
|
|||
if (count.type != Type::Register && offset.type != Type::Register) {
|
||||
ctx.Add("BFE.U {},{{{},{},0,0}},{};", ret, count, offset, base);
|
||||
} else {
|
||||
ctx.Add("MOV.U RC.x,{};MOV.U RC.y,{};"
|
||||
ctx.Add("MOV.U RC.x,{};"
|
||||
"MOV.U RC.y,{};"
|
||||
"BFE.U {},RC,{};",
|
||||
count, offset, ret, base);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,34 +23,12 @@ void EmitPhi(EmitContext& ctx, IR::Inst& inst) {
|
|||
|
||||
void EmitVoid(EmitContext&) {}
|
||||
|
||||
void EmitBranch(EmitContext& ctx) {
|
||||
NotImplemented();
|
||||
}
|
||||
|
||||
void EmitBranchConditional(EmitContext& ctx) {
|
||||
NotImplemented();
|
||||
}
|
||||
|
||||
void EmitLoopMerge(EmitContext& ctx) {
|
||||
NotImplemented();
|
||||
}
|
||||
|
||||
void EmitSelectionMerge(EmitContext& ctx) {
|
||||
NotImplemented();
|
||||
}
|
||||
|
||||
void EmitReturn(EmitContext& ctx) {
|
||||
ctx.Add("RET;");
|
||||
}
|
||||
void EmitBranchConditionRef(EmitContext&) {}
|
||||
|
||||
void EmitJoin(EmitContext& ctx) {
|
||||
NotImplemented();
|
||||
}
|
||||
|
||||
void EmitUnreachable(EmitContext& ctx) {
|
||||
NotImplemented();
|
||||
}
|
||||
|
||||
void EmitDemoteToHelperInvocation(EmitContext& ctx) {
|
||||
NotImplemented();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -463,7 +463,6 @@ EmitContext::EmitContext(const Profile& profile_, IR::Program& program, Bindings
|
|||
DefineImages(program.info, image_binding);
|
||||
DefineAttributeMemAccess(program.info);
|
||||
DefineGlobalMemoryFunctions(program.info);
|
||||
DefineLabels(program);
|
||||
}
|
||||
|
||||
EmitContext::~EmitContext() = default;
|
||||
|
|
@ -487,8 +486,6 @@ Id EmitContext::Def(const IR::Value& value) {
|
|||
return Const(value.F32());
|
||||
case IR::Type::F64:
|
||||
return Constant(F64[1], value.F64());
|
||||
case IR::Type::Label:
|
||||
return value.Label()->Definition<Id>();
|
||||
default:
|
||||
throw NotImplementedException("Immediate type {}", value.Type());
|
||||
}
|
||||
|
|
@ -1139,12 +1136,6 @@ void EmitContext::DefineImages(const Info& info, u32& binding) {
|
|||
}
|
||||
}
|
||||
|
||||
void EmitContext::DefineLabels(IR::Program& program) {
|
||||
for (IR::Block* const block : program.blocks) {
|
||||
block->SetDefinition(OpLabel());
|
||||
}
|
||||
}
|
||||
|
||||
void EmitContext::DefineInputs(const Info& info) {
|
||||
if (info.uses_workgroup_id) {
|
||||
workgroup_id = DefineInput(*this, U32[3], false, spv::BuiltIn::WorkgroupId);
|
||||
|
|
|
|||
|
|
@ -296,7 +296,6 @@ private:
|
|||
void DefineImages(const Info& info, u32& binding);
|
||||
void DefineAttributeMemAccess(const Info& info);
|
||||
void DefineGlobalMemoryFunctions(const Info& info);
|
||||
void DefineLabels(IR::Program& program);
|
||||
|
||||
void DefineInputs(const Info& info);
|
||||
void DefineOutputs(const IR::Program& program);
|
||||
|
|
|
|||
|
|
@ -41,8 +41,6 @@ ArgType Arg(EmitContext& ctx, const IR::Value& arg) {
|
|||
return arg;
|
||||
} else if constexpr (std::is_same_v<ArgType, u32>) {
|
||||
return arg.U32();
|
||||
} else if constexpr (std::is_same_v<ArgType, IR::Block*>) {
|
||||
return arg.Label();
|
||||
} else if constexpr (std::is_same_v<ArgType, IR::Attribute>) {
|
||||
return arg.Attribute();
|
||||
} else if constexpr (std::is_same_v<ArgType, IR::Patch>) {
|
||||
|
|
@ -109,15 +107,74 @@ Id TypeId(const EmitContext& ctx, IR::Type type) {
|
|||
}
|
||||
}
|
||||
|
||||
void Traverse(EmitContext& ctx, IR::Program& program) {
|
||||
IR::Block* current_block{};
|
||||
for (const IR::AbstractSyntaxNode& node : program.syntax_list) {
|
||||
switch (node.type) {
|
||||
case IR::AbstractSyntaxNode::Type::Block:
|
||||
const Id label{node.block->Definition<Id>()};
|
||||
if (current_block) {
|
||||
ctx.OpBranch(label);
|
||||
}
|
||||
current_block = node.block;
|
||||
ctx.AddLabel(label);
|
||||
for (IR::Inst& inst : node.block->Instructions()) {
|
||||
EmitInst(ctx, &inst);
|
||||
}
|
||||
break;
|
||||
case IR::AbstractSyntaxNode::Type::If: {
|
||||
const Id if_label{node.if_node.body->Definition<Id>()};
|
||||
const Id endif_label{node.if_node.merge->Definition<Id>()};
|
||||
ctx.OpSelectionMerge(endif_label, spv::SelectionControlMask::MaskNone);
|
||||
ctx.OpBranchConditional(ctx.Def(node.if_node.cond), if_label, endif_label);
|
||||
break;
|
||||
}
|
||||
case IR::AbstractSyntaxNode::Type::Loop: {
|
||||
const Id body_label{node.loop.body->Definition<Id>()};
|
||||
const Id continue_label{node.loop.continue_block->Definition<Id>()};
|
||||
const Id endloop_label{node.loop.merge->Definition<Id>()};
|
||||
|
||||
ctx.OpLoopMerge(endloop_label, continue_label, spv::LoopControlMask::MaskNone);
|
||||
ctx.OpBranch(node.loop.body->Definition<Id>());
|
||||
break;
|
||||
}
|
||||
case IR::AbstractSyntaxNode::Type::Break: {
|
||||
const Id break_label{node.break_node.merge->Definition<Id>()};
|
||||
const Id skip_label{node.break_node.skip->Definition<Id>()};
|
||||
ctx.OpBranchConditional(ctx.Def(node.break_node.cond), break_label, skip_label);
|
||||
break;
|
||||
}
|
||||
case IR::AbstractSyntaxNode::Type::EndIf:
|
||||
if (current_block) {
|
||||
ctx.OpBranch(node.end_if.merge->Definition<Id>());
|
||||
}
|
||||
break;
|
||||
case IR::AbstractSyntaxNode::Type::Repeat: {
|
||||
const Id loop_header_label{node.repeat.loop_header->Definition<Id>()};
|
||||
const Id merge_label{node.repeat.merge->Definition<Id>()};
|
||||
ctx.OpBranchConditional(ctx.Def(node.repeat.cond), loop_header_label, merge_label);
|
||||
break;
|
||||
}
|
||||
case IR::AbstractSyntaxNode::Type::Return:
|
||||
ctx.OpReturn();
|
||||
break;
|
||||
case IR::AbstractSyntaxNode::Type::Unreachable:
|
||||
ctx.OpUnreachable();
|
||||
break;
|
||||
}
|
||||
if (node.type != IR::AbstractSyntaxNode::Type::Block) {
|
||||
current_block = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Id DefineMain(EmitContext& ctx, IR::Program& program) {
|
||||
const Id void_function{ctx.TypeFunction(ctx.void_id)};
|
||||
const Id main{ctx.OpFunction(ctx.void_id, spv::FunctionControlMask::MaskNone, void_function)};
|
||||
for (IR::Block* const block : program.blocks) {
|
||||
ctx.AddLabel(block->Definition<Id>());
|
||||
for (IR::Inst& inst : block->Instructions()) {
|
||||
EmitInst(ctx, &inst);
|
||||
}
|
||||
block->SetDefinition(ctx.OpLabel());
|
||||
}
|
||||
Traverse(ctx, program);
|
||||
ctx.OpFunctionEnd();
|
||||
return main;
|
||||
}
|
||||
|
|
@ -411,6 +468,8 @@ Id EmitIdentity(EmitContext& ctx, const IR::Value& value) {
|
|||
return id;
|
||||
}
|
||||
|
||||
void EmitBranchConditionRef(EmitContext&) {}
|
||||
|
||||
void EmitGetZeroFromOp(EmitContext&) {
|
||||
throw LogicError("Unreachable instruction");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,40 +7,21 @@
|
|||
|
||||
namespace Shader::Backend::SPIRV {
|
||||
|
||||
void EmitBranch(EmitContext& ctx, Id label) {
|
||||
ctx.OpBranch(label);
|
||||
}
|
||||
|
||||
void EmitBranchConditional(EmitContext& ctx, Id condition, Id true_label, Id false_label) {
|
||||
ctx.OpBranchConditional(condition, true_label, false_label);
|
||||
}
|
||||
|
||||
void EmitLoopMerge(EmitContext& ctx, Id merge_label, Id continue_label) {
|
||||
ctx.OpLoopMerge(merge_label, continue_label, spv::LoopControlMask::MaskNone);
|
||||
}
|
||||
|
||||
void EmitSelectionMerge(EmitContext& ctx, Id merge_label) {
|
||||
ctx.OpSelectionMerge(merge_label, spv::SelectionControlMask::MaskNone);
|
||||
}
|
||||
|
||||
void EmitReturn(EmitContext& ctx) {
|
||||
ctx.OpReturn();
|
||||
}
|
||||
|
||||
void EmitJoin(EmitContext&) {
|
||||
throw NotImplementedException("Join shouldn't be emitted");
|
||||
}
|
||||
|
||||
void EmitUnreachable(EmitContext& ctx) {
|
||||
ctx.OpUnreachable();
|
||||
}
|
||||
|
||||
void EmitDemoteToHelperInvocation(EmitContext& ctx, Id continue_label) {
|
||||
void EmitDemoteToHelperInvocation(EmitContext& ctx) {
|
||||
if (ctx.profile.support_demote_to_helper_invocation) {
|
||||
ctx.OpDemoteToHelperInvocationEXT();
|
||||
ctx.OpBranch(continue_label);
|
||||
} else {
|
||||
const Id kill_label{ctx.OpLabel()};
|
||||
const Id impossible_label{ctx.OpLabel()};
|
||||
ctx.OpSelectionMerge(impossible_label, spv::SelectionControlMask::MaskNone);
|
||||
ctx.OpBranchConditional(ctx.true_value, kill_label, impossible_label);
|
||||
ctx.AddLabel(kill_label);
|
||||
ctx.OpKill();
|
||||
ctx.AddLabel(impossible_label);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,14 +23,9 @@ class EmitContext;
|
|||
Id EmitPhi(EmitContext& ctx, IR::Inst* inst);
|
||||
void EmitVoid(EmitContext& ctx);
|
||||
Id EmitIdentity(EmitContext& ctx, const IR::Value& value);
|
||||
void EmitBranch(EmitContext& ctx, Id label);
|
||||
void EmitBranchConditional(EmitContext& ctx, Id condition, Id true_label, Id false_label);
|
||||
void EmitLoopMerge(EmitContext& ctx, Id merge_label, Id continue_label);
|
||||
void EmitSelectionMerge(EmitContext& ctx, Id merge_label);
|
||||
void EmitReturn(EmitContext& ctx);
|
||||
void EmitBranchConditionRef(EmitContext&);
|
||||
void EmitJoin(EmitContext& ctx);
|
||||
void EmitUnreachable(EmitContext& ctx);
|
||||
void EmitDemoteToHelperInvocation(EmitContext& ctx, Id continue_label);
|
||||
void EmitDemoteToHelperInvocation(EmitContext& ctx);
|
||||
void EmitBarrier(EmitContext& ctx);
|
||||
void EmitWorkgroupMemoryBarrier(EmitContext& ctx);
|
||||
void EmitDeviceMemoryBarrier(EmitContext& ctx);
|
||||
|
|
|
|||
56
src/shader_recompiler/frontend/ir/abstract_syntax_list.h
Normal file
56
src/shader_recompiler/frontend/ir/abstract_syntax_list.h
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
// Copyright 2021 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "shader_recompiler/frontend/ir/value.h"
|
||||
|
||||
namespace Shader::IR {
|
||||
|
||||
class Block;
|
||||
|
||||
struct AbstractSyntaxNode {
|
||||
enum class Type {
|
||||
Block,
|
||||
If,
|
||||
EndIf,
|
||||
Loop,
|
||||
Repeat,
|
||||
Break,
|
||||
Return,
|
||||
Unreachable,
|
||||
};
|
||||
Type type{};
|
||||
union {
|
||||
Block* block{};
|
||||
struct {
|
||||
U1 cond;
|
||||
Block* body;
|
||||
Block* merge;
|
||||
} if_node;
|
||||
struct {
|
||||
Block* merge;
|
||||
} end_if;
|
||||
struct {
|
||||
Block* body;
|
||||
Block* continue_block;
|
||||
Block* merge;
|
||||
} loop;
|
||||
struct {
|
||||
U1 cond;
|
||||
Block* loop_header;
|
||||
Block* merge;
|
||||
} repeat;
|
||||
struct {
|
||||
U1 cond;
|
||||
Block* merge;
|
||||
Block* skip;
|
||||
} break_node;
|
||||
};
|
||||
};
|
||||
using AbstractSyntaxList = std::vector<AbstractSyntaxNode>;
|
||||
|
||||
} // namespace Shader::IR
|
||||
|
|
@ -14,10 +14,7 @@
|
|||
|
||||
namespace Shader::IR {
|
||||
|
||||
Block::Block(ObjectPool<Inst>& inst_pool_, u32 begin, u32 end)
|
||||
: inst_pool{&inst_pool_}, location_begin{begin}, location_end{end} {}
|
||||
|
||||
Block::Block(ObjectPool<Inst>& inst_pool_) : Block{inst_pool_, 0, 0} {}
|
||||
Block::Block(ObjectPool<Inst>& inst_pool_) : inst_pool{&inst_pool_} {}
|
||||
|
||||
Block::~Block() = default;
|
||||
|
||||
|
|
@ -40,39 +37,15 @@ Block::iterator Block::PrependNewInst(iterator insertion_point, Opcode op,
|
|||
return result_it;
|
||||
}
|
||||
|
||||
void Block::SetBranches(Condition cond, Block* branch_true_, Block* branch_false_) {
|
||||
branch_cond = cond;
|
||||
branch_true = branch_true_;
|
||||
branch_false = branch_false_;
|
||||
}
|
||||
|
||||
void Block::SetBranch(Block* branch) {
|
||||
branch_cond = Condition{true};
|
||||
branch_true = branch;
|
||||
}
|
||||
|
||||
void Block::SetReturn() {
|
||||
branch_cond = Condition{true};
|
||||
branch_true = nullptr;
|
||||
branch_false = nullptr;
|
||||
}
|
||||
|
||||
bool Block::IsVirtual() const noexcept {
|
||||
return location_begin == location_end;
|
||||
}
|
||||
|
||||
u32 Block::LocationBegin() const noexcept {
|
||||
return location_begin;
|
||||
}
|
||||
|
||||
u32 Block::LocationEnd() const noexcept {
|
||||
return location_end;
|
||||
}
|
||||
|
||||
void Block::AddImmediatePredecessor(Block* block) {
|
||||
if (std::ranges::find(imm_predecessors, block) == imm_predecessors.end()) {
|
||||
imm_predecessors.push_back(block);
|
||||
void Block::AddBranch(Block* block) {
|
||||
if (std::ranges::find(imm_successors, block) != imm_successors.end()) {
|
||||
throw LogicError("Successor already inserted");
|
||||
}
|
||||
if (std::ranges::find(block->imm_predecessors, this) != block->imm_predecessors.end()) {
|
||||
throw LogicError("Predecessor already inserted");
|
||||
}
|
||||
imm_successors.push_back(block);
|
||||
block->imm_predecessors.push_back(this);
|
||||
}
|
||||
|
||||
static std::string BlockToIndex(const std::map<const Block*, size_t>& block_to_index,
|
||||
|
|
@ -92,15 +65,11 @@ static size_t InstIndex(std::map<const Inst*, size_t>& inst_to_index, size_t& in
|
|||
return it->second;
|
||||
}
|
||||
|
||||
static std::string ArgToIndex(const std::map<const Block*, size_t>& block_to_index,
|
||||
std::map<const Inst*, size_t>& inst_to_index, size_t& inst_index,
|
||||
static std::string ArgToIndex(std::map<const Inst*, size_t>& inst_to_index, size_t& inst_index,
|
||||
const Value& arg) {
|
||||
if (arg.IsEmpty()) {
|
||||
return "<null>";
|
||||
}
|
||||
if (arg.IsLabel()) {
|
||||
return BlockToIndex(block_to_index, arg.Label());
|
||||
}
|
||||
if (!arg.IsImmediate() || arg.IsIdentity()) {
|
||||
return fmt::format("%{}", InstIndex(inst_to_index, inst_index, arg.Inst()));
|
||||
}
|
||||
|
|
@ -140,8 +109,7 @@ std::string DumpBlock(const Block& block, const std::map<const Block*, size_t>&
|
|||
if (const auto it{block_to_index.find(&block)}; it != block_to_index.end()) {
|
||||
ret += fmt::format(" ${}", it->second);
|
||||
}
|
||||
ret += fmt::format(": begin={:04x} end={:04x}\n", block.LocationBegin(), block.LocationEnd());
|
||||
|
||||
ret += '\n';
|
||||
for (const Inst& inst : block) {
|
||||
const Opcode op{inst.GetOpcode()};
|
||||
ret += fmt::format("[{:016x}] ", reinterpret_cast<u64>(&inst));
|
||||
|
|
@ -153,7 +121,7 @@ std::string DumpBlock(const Block& block, const std::map<const Block*, size_t>&
|
|||
const size_t arg_count{inst.NumArgs()};
|
||||
for (size_t arg_index = 0; arg_index < arg_count; ++arg_index) {
|
||||
const Value arg{inst.Arg(arg_index)};
|
||||
const std::string arg_str{ArgToIndex(block_to_index, inst_to_index, inst_index, arg)};
|
||||
const std::string arg_str{ArgToIndex(inst_to_index, inst_index, arg)};
|
||||
ret += arg_index != 0 ? ", " : " ";
|
||||
if (op == Opcode::Phi) {
|
||||
ret += fmt::format("[ {}, {} ]", arg_str,
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@
|
|||
#include <boost/intrusive/list.hpp>
|
||||
|
||||
#include "common/bit_cast.h"
|
||||
#include "common/common_types.h"
|
||||
#include "shader_recompiler/frontend/ir/condition.h"
|
||||
#include "shader_recompiler/frontend/ir/value.h"
|
||||
#include "shader_recompiler/object_pool.h"
|
||||
|
|
@ -27,7 +28,6 @@ public:
|
|||
using reverse_iterator = InstructionList::reverse_iterator;
|
||||
using const_reverse_iterator = InstructionList::const_reverse_iterator;
|
||||
|
||||
explicit Block(ObjectPool<Inst>& inst_pool_, u32 begin, u32 end);
|
||||
explicit Block(ObjectPool<Inst>& inst_pool_);
|
||||
~Block();
|
||||
|
||||
|
|
@ -44,22 +44,8 @@ public:
|
|||
iterator PrependNewInst(iterator insertion_point, Opcode op,
|
||||
std::initializer_list<Value> args = {}, u32 flags = 0);
|
||||
|
||||
/// Set the branches to jump to when all instructions have executed.
|
||||
void SetBranches(Condition cond, Block* branch_true, Block* branch_false);
|
||||
/// Set the branch to unconditionally jump to when all instructions have executed.
|
||||
void SetBranch(Block* branch);
|
||||
/// Mark the block as a return block.
|
||||
void SetReturn();
|
||||
|
||||
/// Returns true when the block does not implement any guest instructions directly.
|
||||
[[nodiscard]] bool IsVirtual() const noexcept;
|
||||
/// Gets the starting location of this basic block.
|
||||
[[nodiscard]] u32 LocationBegin() const noexcept;
|
||||
/// Gets the end location for this basic block.
|
||||
[[nodiscard]] u32 LocationEnd() const noexcept;
|
||||
|
||||
/// Adds a new immediate predecessor to this basic block.
|
||||
void AddImmediatePredecessor(Block* block);
|
||||
/// Adds a new branch to this basic block.
|
||||
void AddBranch(Block* block);
|
||||
|
||||
/// Gets a mutable reference to the instruction list for this basic block.
|
||||
[[nodiscard]] InstructionList& Instructions() noexcept {
|
||||
|
|
@ -71,9 +57,13 @@ public:
|
|||
}
|
||||
|
||||
/// Gets an immutable span to the immediate predecessors.
|
||||
[[nodiscard]] std::span<Block* const> ImmediatePredecessors() const noexcept {
|
||||
[[nodiscard]] std::span<Block* const> ImmPredecessors() const noexcept {
|
||||
return imm_predecessors;
|
||||
}
|
||||
/// Gets an immutable span to the immediate successors.
|
||||
[[nodiscard]] std::span<Block* const> ImmSuccessors() const noexcept {
|
||||
return imm_successors;
|
||||
}
|
||||
|
||||
/// Intrusively store the host definition of this instruction.
|
||||
template <typename DefinitionType>
|
||||
|
|
@ -87,19 +77,6 @@ public:
|
|||
return Common::BitCast<DefinitionType>(definition);
|
||||
}
|
||||
|
||||
[[nodiscard]] Condition BranchCondition() const noexcept {
|
||||
return branch_cond;
|
||||
}
|
||||
[[nodiscard]] bool IsTerminationBlock() const noexcept {
|
||||
return !branch_true && !branch_false;
|
||||
}
|
||||
[[nodiscard]] Block* TrueBranch() const noexcept {
|
||||
return branch_true;
|
||||
}
|
||||
[[nodiscard]] Block* FalseBranch() const noexcept {
|
||||
return branch_false;
|
||||
}
|
||||
|
||||
void SetSsaRegValue(IR::Reg reg, const Value& value) noexcept {
|
||||
ssa_reg_values[RegIndex(reg)] = value;
|
||||
}
|
||||
|
|
@ -178,22 +155,14 @@ public:
|
|||
private:
|
||||
/// Memory pool for instruction list
|
||||
ObjectPool<Inst>* inst_pool;
|
||||
/// Starting location of this block
|
||||
u32 location_begin;
|
||||
/// End location of this block
|
||||
u32 location_end;
|
||||
|
||||
/// List of instructions in this block
|
||||
InstructionList instructions;
|
||||
|
||||
/// Condition to choose the branch to take
|
||||
Condition branch_cond{true};
|
||||
/// Block to jump into when the branch condition evaluates as true
|
||||
Block* branch_true{nullptr};
|
||||
/// Block to jump into when the branch condition evaluates as false
|
||||
Block* branch_false{nullptr};
|
||||
/// Block immediate predecessors
|
||||
std::vector<Block*> imm_predecessors;
|
||||
/// Block immediate successors
|
||||
std::vector<Block*> imm_successors;
|
||||
|
||||
/// Intrusively store the value of a register in the block.
|
||||
std::array<Value, NUM_REGS> ssa_reg_values;
|
||||
|
|
|
|||
|
|
@ -61,25 +61,28 @@ F64 IREmitter::Imm64(f64 value) const {
|
|||
return F64{Value{value}};
|
||||
}
|
||||
|
||||
void IREmitter::Branch(Block* label) {
|
||||
label->AddImmediatePredecessor(block);
|
||||
block->SetBranch(label);
|
||||
Inst(Opcode::Branch, label);
|
||||
void IREmitter::Prologue() {
|
||||
Inst(Opcode::Prologue);
|
||||
}
|
||||
|
||||
void IREmitter::BranchConditional(const U1& condition, Block* true_label, Block* false_label) {
|
||||
block->SetBranches(IR::Condition{true}, true_label, false_label);
|
||||
true_label->AddImmediatePredecessor(block);
|
||||
false_label->AddImmediatePredecessor(block);
|
||||
Inst(Opcode::BranchConditional, condition, true_label, false_label);
|
||||
void IREmitter::Epilogue() {
|
||||
Inst(Opcode::Epilogue);
|
||||
}
|
||||
|
||||
void IREmitter::LoopMerge(Block* merge_block, Block* continue_target) {
|
||||
Inst(Opcode::LoopMerge, merge_block, continue_target);
|
||||
void IREmitter::BranchConditionRef(const U1& cond) {
|
||||
Inst(Opcode::BranchConditionRef, cond);
|
||||
}
|
||||
|
||||
void IREmitter::SelectionMerge(Block* merge_block) {
|
||||
Inst(Opcode::SelectionMerge, merge_block);
|
||||
void IREmitter::DemoteToHelperInvocation() {
|
||||
Inst(Opcode::DemoteToHelperInvocation);
|
||||
}
|
||||
|
||||
void IREmitter::EmitVertex(const U32& stream) {
|
||||
Inst(Opcode::EmitVertex, stream);
|
||||
}
|
||||
|
||||
void IREmitter::EndPrimitive(const U32& stream) {
|
||||
Inst(Opcode::EndPrimitive, stream);
|
||||
}
|
||||
|
||||
void IREmitter::Barrier() {
|
||||
|
|
@ -94,37 +97,6 @@ void IREmitter::DeviceMemoryBarrier() {
|
|||
Inst(Opcode::DeviceMemoryBarrier);
|
||||
}
|
||||
|
||||
void IREmitter::Return() {
|
||||
block->SetReturn();
|
||||
Inst(Opcode::Return);
|
||||
}
|
||||
|
||||
void IREmitter::Unreachable() {
|
||||
Inst(Opcode::Unreachable);
|
||||
}
|
||||
|
||||
void IREmitter::DemoteToHelperInvocation(Block* continue_label) {
|
||||
block->SetBranch(continue_label);
|
||||
continue_label->AddImmediatePredecessor(block);
|
||||
Inst(Opcode::DemoteToHelperInvocation, continue_label);
|
||||
}
|
||||
|
||||
void IREmitter::Prologue() {
|
||||
Inst(Opcode::Prologue);
|
||||
}
|
||||
|
||||
void IREmitter::Epilogue() {
|
||||
Inst(Opcode::Epilogue);
|
||||
}
|
||||
|
||||
void IREmitter::EmitVertex(const U32& stream) {
|
||||
Inst(Opcode::EmitVertex, stream);
|
||||
}
|
||||
|
||||
void IREmitter::EndPrimitive(const U32& stream) {
|
||||
Inst(Opcode::EndPrimitive, stream);
|
||||
}
|
||||
|
||||
U32 IREmitter::GetReg(IR::Reg reg) {
|
||||
return Inst<U32>(Opcode::GetRegister, reg);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,17 +32,10 @@ public:
|
|||
[[nodiscard]] U64 Imm64(s64 value) const;
|
||||
[[nodiscard]] F64 Imm64(f64 value) const;
|
||||
|
||||
void Branch(Block* label);
|
||||
void BranchConditional(const U1& condition, Block* true_label, Block* false_label);
|
||||
void LoopMerge(Block* merge_block, Block* continue_target);
|
||||
void SelectionMerge(Block* merge_block);
|
||||
void Return();
|
||||
void Unreachable();
|
||||
void DemoteToHelperInvocation(Block* continue_label);
|
||||
|
||||
void Prologue();
|
||||
void Epilogue();
|
||||
|
||||
void BranchConditionRef(const U1& cond);
|
||||
void DemoteToHelperInvocation();
|
||||
void EmitVertex(const U32& stream);
|
||||
void EndPrimitive(const U32& stream);
|
||||
|
||||
|
|
|
|||
|
|
@ -56,19 +56,14 @@ Inst::~Inst() {
|
|||
|
||||
bool Inst::MayHaveSideEffects() const noexcept {
|
||||
switch (op) {
|
||||
case Opcode::Branch:
|
||||
case Opcode::BranchConditional:
|
||||
case Opcode::LoopMerge:
|
||||
case Opcode::SelectionMerge:
|
||||
case Opcode::Return:
|
||||
case Opcode::Prologue:
|
||||
case Opcode::Epilogue:
|
||||
case Opcode::BranchConditionRef:
|
||||
case Opcode::Join:
|
||||
case Opcode::Unreachable:
|
||||
case Opcode::DemoteToHelperInvocation:
|
||||
case Opcode::Barrier:
|
||||
case Opcode::WorkgroupMemoryBarrier:
|
||||
case Opcode::DeviceMemoryBarrier:
|
||||
case Opcode::Prologue:
|
||||
case Opcode::Epilogue:
|
||||
case Opcode::EmitVertex:
|
||||
case Opcode::EndPrimitive:
|
||||
case Opcode::SetAttribute:
|
||||
|
|
|
|||
|
|
@ -30,7 +30,6 @@ struct OpcodeMeta {
|
|||
// using enum Type;
|
||||
constexpr Type Void{Type::Void};
|
||||
constexpr Type Opaque{Type::Opaque};
|
||||
constexpr Type Label{Type::Label};
|
||||
constexpr Type Reg{Type::Reg};
|
||||
constexpr Type Pred{Type::Pred};
|
||||
constexpr Type Attribute{Type::Attribute};
|
||||
|
|
|
|||
|
|
@ -7,27 +7,20 @@ OPCODE(Phi, Opaque,
|
|||
OPCODE(Identity, Opaque, Opaque, )
|
||||
OPCODE(Void, Void, )
|
||||
|
||||
// Control flow
|
||||
OPCODE(Branch, Void, Label, )
|
||||
OPCODE(BranchConditional, Void, U1, Label, Label, )
|
||||
OPCODE(LoopMerge, Void, Label, Label, )
|
||||
OPCODE(SelectionMerge, Void, Label, )
|
||||
OPCODE(Return, Void, )
|
||||
// Special operations
|
||||
OPCODE(Prologue, Void, )
|
||||
OPCODE(Epilogue, Void, )
|
||||
OPCODE(BranchConditionRef, Void, U1, )
|
||||
OPCODE(Join, Void, )
|
||||
OPCODE(Unreachable, Void, )
|
||||
OPCODE(DemoteToHelperInvocation, Void, Label, )
|
||||
OPCODE(DemoteToHelperInvocation, Void, )
|
||||
OPCODE(EmitVertex, Void, U32, )
|
||||
OPCODE(EndPrimitive, Void, U32, )
|
||||
|
||||
// Barriers
|
||||
OPCODE(Barrier, Void, )
|
||||
OPCODE(WorkgroupMemoryBarrier, Void, )
|
||||
OPCODE(DeviceMemoryBarrier, Void, )
|
||||
|
||||
// Special operations
|
||||
OPCODE(Prologue, Void, )
|
||||
OPCODE(Epilogue, Void, )
|
||||
OPCODE(EmitVertex, Void, U32, )
|
||||
OPCODE(EndPrimitive, Void, U32, )
|
||||
|
||||
// Context getters/setters
|
||||
OPCODE(GetRegister, U32, Reg, )
|
||||
OPCODE(SetRegister, Void, Reg, U32, )
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <boost/container/flat_set.hpp>
|
||||
#include <boost/container/small_vector.hpp>
|
||||
|
||||
|
|
@ -10,35 +12,31 @@
|
|||
|
||||
namespace Shader::IR {
|
||||
|
||||
BlockList PostOrder(const BlockList& blocks) {
|
||||
BlockList PostOrder(const AbstractSyntaxNode& root) {
|
||||
boost::container::small_vector<Block*, 16> block_stack;
|
||||
boost::container::flat_set<Block*> visited;
|
||||
|
||||
BlockList post_order_blocks;
|
||||
post_order_blocks.reserve(blocks.size());
|
||||
|
||||
Block* const first_block{blocks.front()};
|
||||
if (root.type != AbstractSyntaxNode::Type::Block) {
|
||||
throw LogicError("First node in abstract syntax list root is not a block");
|
||||
}
|
||||
Block* const first_block{root.block};
|
||||
visited.insert(first_block);
|
||||
block_stack.push_back(first_block);
|
||||
|
||||
const auto visit_branch = [&](Block* block, Block* branch) {
|
||||
if (!branch) {
|
||||
return false;
|
||||
}
|
||||
if (!visited.insert(branch).second) {
|
||||
return false;
|
||||
}
|
||||
// Calling push_back twice is faster than insert on MSVC
|
||||
block_stack.push_back(block);
|
||||
block_stack.push_back(branch);
|
||||
return true;
|
||||
};
|
||||
while (!block_stack.empty()) {
|
||||
Block* const block{block_stack.back()};
|
||||
const auto visit{[&](Block* branch) {
|
||||
if (!visited.insert(branch).second) {
|
||||
return false;
|
||||
}
|
||||
// Calling push_back twice is faster than insert on MSVC
|
||||
block_stack.push_back(block);
|
||||
block_stack.push_back(branch);
|
||||
return true;
|
||||
}};
|
||||
block_stack.pop_back();
|
||||
|
||||
if (!visit_branch(block, block->TrueBranch()) &&
|
||||
!visit_branch(block, block->FalseBranch())) {
|
||||
if (std::ranges::none_of(block->ImmSuccessors(), visit)) {
|
||||
post_order_blocks.push_back(block);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,9 +5,10 @@
|
|||
#pragma once
|
||||
|
||||
#include "shader_recompiler/frontend/ir/basic_block.h"
|
||||
#include "shader_recompiler/frontend/ir/abstract_syntax_list.h"
|
||||
|
||||
namespace Shader::IR {
|
||||
|
||||
BlockList PostOrder(const BlockList& blocks);
|
||||
BlockList PostOrder(const AbstractSyntaxNode& root);
|
||||
|
||||
} // namespace Shader::IR
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue