yuzu-emu
/
yuzu-android
Archived
1
0
Fork 0

glsl: implement phi nodes

This commit is contained in:
ameerj 2021-05-24 19:33:11 -04:00
parent 3d9ecbe998
commit e99d01ff53
4 changed files with 54 additions and 20 deletions

View File

@ -2,6 +2,7 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <ranges>
#include <string> #include <string>
#include <tuple> #include <tuple>
@ -9,6 +10,7 @@
#include "shader_recompiler/backend/glsl/emit_context.h" #include "shader_recompiler/backend/glsl/emit_context.h"
#include "shader_recompiler/backend/glsl/emit_glsl.h" #include "shader_recompiler/backend/glsl/emit_glsl.h"
#include "shader_recompiler/backend/glsl/emit_glsl_instructions.h" #include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"
#include "shader_recompiler/frontend/ir/ir_emitter.h"
#include "shader_recompiler/frontend/ir/program.h" #include "shader_recompiler/frontend/ir/program.h"
#include "shader_recompiler/profile.h" #include "shader_recompiler/profile.h"
@ -96,6 +98,22 @@ void EmitInst(EmitContext& ctx, IR::Inst* inst) {
throw LogicError("Invalid opcode {}", inst->GetOpcode()); throw LogicError("Invalid opcode {}", inst->GetOpcode());
} }
void Precolor(EmitContext& ctx, const IR::Program& program) {
for (IR::Block* const block : program.blocks) {
for (IR::Inst& phi : block->Instructions() | std::views::take_while(IR::IsPhi)) {
ctx.Add("{};", ctx.reg_alloc.Define(phi, phi.Arg(0).Type()));
const size_t num_args{phi.NumArgs()};
for (size_t i = 0; i < num_args; ++i) {
IR::IREmitter{*phi.PhiBlock(i)}.PhiMove(phi, phi.Arg(i));
}
// Add reference to the phi node on the phi predecessor to avoid overwritting it
for (size_t i = 0; i < num_args; ++i) {
IR::IREmitter{*phi.PhiBlock(i)}.Reference(IR::Value{&phi});
}
}
}
}
void EmitCode(EmitContext& ctx, const IR::Program& program) { void EmitCode(EmitContext& ctx, const IR::Program& program) {
for (const IR::AbstractSyntaxNode& node : program.syntax_list) { for (const IR::AbstractSyntaxNode& node : program.syntax_list) {
switch (node.type) { switch (node.type) {
@ -105,37 +123,31 @@ void EmitCode(EmitContext& ctx, const IR::Program& program) {
} }
break; break;
case IR::AbstractSyntaxNode::Type::If: case IR::AbstractSyntaxNode::Type::If:
ctx.Add("if ("); ctx.Add("if ({}){{", ctx.reg_alloc.Consume(node.data.if_node.cond));
break; break;
case IR::AbstractSyntaxNode::Type::EndIf: case IR::AbstractSyntaxNode::Type::EndIf:
ctx.Add("){{"); ctx.Add("}}");
break;
case IR::AbstractSyntaxNode::Type::Loop:
ctx.Add("while (");
break;
case IR::AbstractSyntaxNode::Type::Repeat:
if (node.data.repeat.cond.IsImmediate()) {
if (node.data.repeat.cond.U1()) {
ctx.Add("ENDREP;");
} else {
ctx.Add("BRK;"
"ENDREP;");
}
}
break; break;
case IR::AbstractSyntaxNode::Type::Break: case IR::AbstractSyntaxNode::Type::Break:
if (node.data.break_node.cond.IsImmediate()) { if (node.data.break_node.cond.IsImmediate()) {
if (node.data.break_node.cond.U1()) { if (node.data.break_node.cond.U1()) {
ctx.Add("break;"); ctx.Add("break;");
} }
} else {
// TODO: implement this
ctx.Add("MOV.S.CC RC,{};"
"BRK (NE.x);",
0);
} }
break; break;
case IR::AbstractSyntaxNode::Type::Return: case IR::AbstractSyntaxNode::Type::Return:
case IR::AbstractSyntaxNode::Type::Unreachable: case IR::AbstractSyntaxNode::Type::Unreachable:
ctx.Add("return;"); ctx.Add("return;\n}}");
break; break;
case IR::AbstractSyntaxNode::Type::Loop:
case IR::AbstractSyntaxNode::Type::Repeat:
default: default:
ctx.Add("UNAHNDLED {}", node.type); throw NotImplementedException("{}", node.type);
break; break;
} }
} }
@ -146,8 +158,8 @@ void EmitCode(EmitContext& ctx, const IR::Program& program) {
std::string EmitGLSL(const Profile& profile, const RuntimeInfo&, IR::Program& program, std::string EmitGLSL(const Profile& profile, const RuntimeInfo&, IR::Program& program,
Bindings& bindings) { Bindings& bindings) {
EmitContext ctx{program, bindings, profile}; EmitContext ctx{program, bindings, profile};
Precolor(ctx, program);
EmitCode(ctx, program); EmitCode(ctx, program);
ctx.code += "}";
return ctx.code; return ctx.code;
} }

View File

@ -28,11 +28,14 @@ void EmitVoid(EmitContext& ctx) {
} }
void EmitReference(EmitContext&) { void EmitReference(EmitContext&) {
NotImplemented(); // NotImplemented();
} }
void EmitPhiMove(EmitContext& ctx, const IR::Value& phi, const IR::Value& value) { void EmitPhiMove(EmitContext& ctx, const IR::Value& phi, const IR::Value& value) {
NotImplemented(); if (phi == value) {
return;
}
ctx.Add("{}={};", ctx.reg_alloc.Consume(phi), ctx.reg_alloc.Consume(value));
} }
void EmitBranch(EmitContext& ctx, std::string_view label) { void EmitBranch(EmitContext& ctx, std::string_view label) {

View File

@ -74,6 +74,23 @@ std::string RegAlloc::Define(IR::Inst& inst, Type type) {
return type_str + Representation(id); return type_str + Representation(id);
} }
std::string RegAlloc::Define(IR::Inst& inst, IR::Type type) {
switch (type) {
case IR::Type::U1:
return Define(inst, Type::U1);
case IR::Type::U32:
return Define(inst, Type::U32);
case IR::Type::F32:
return Define(inst, Type::F32);
case IR::Type::U64:
return Define(inst, Type::U64);
case IR::Type::F64:
return Define(inst, Type::F64);
default:
throw NotImplementedException("IR type {}", type);
}
}
std::string RegAlloc::Consume(const IR::Value& value) { std::string RegAlloc::Consume(const IR::Value& value) {
return value.IsImmediate() ? MakeImm(value) : Consume(*value.InstRecursive()); return value.IsImmediate() ? MakeImm(value) : Consume(*value.InstRecursive());
} }

View File

@ -12,6 +12,7 @@
namespace Shader::IR { namespace Shader::IR {
class Inst; class Inst;
class Value; class Value;
enum class Type;
} // namespace Shader::IR } // namespace Shader::IR
namespace Shader::Backend::GLSL { namespace Shader::Backend::GLSL {
@ -50,6 +51,7 @@ class RegAlloc {
public: public:
std::string Define(IR::Inst& inst); std::string Define(IR::Inst& inst);
std::string Define(IR::Inst& inst, Type type); std::string Define(IR::Inst& inst, Type type);
std::string Define(IR::Inst& inst, IR::Type type);
std::string Consume(const IR::Value& value); std::string Consume(const IR::Value& value);