summaryrefslogtreecommitdiff
path: root/src/shader_recompiler/backend
diff options
context:
space:
mode:
authorGravatar ReinUsesLisp2021-05-14 04:48:46 -0300
committerGravatar ameerj2021-07-22 21:51:31 -0400
commitbf5e48ffe4bd48ea681f2a01c8919c97125e88df (patch)
tree2127c2f01aa19b98672f1ac9f34395b9b0240b3e /src/shader_recompiler/backend
parentglasm: Write result to scalar on integer comparison instructions (diff)
downloadyuzu-bf5e48ffe4bd48ea681f2a01c8919c97125e88df.tar.gz
yuzu-bf5e48ffe4bd48ea681f2a01c8919c97125e88df.tar.xz
yuzu-bf5e48ffe4bd48ea681f2a01c8919c97125e88df.zip
glasm: Initial implementation of phi nodes on GLASM
Diffstat (limited to 'src/shader_recompiler/backend')
-rw-r--r--src/shader_recompiler/backend/glasm/emit_glasm.cpp59
-rw-r--r--src/shader_recompiler/backend/glasm/emit_glasm_instructions.h3
-rw-r--r--src/shader_recompiler/backend/glasm/emit_glasm_not_implemented.cpp27
-rw-r--r--src/shader_recompiler/backend/spirv/emit_spirv.cpp6
-rw-r--r--src/shader_recompiler/backend/spirv/emit_spirv_instructions.h3
5 files changed, 88 insertions, 10 deletions
diff --git a/src/shader_recompiler/backend/glasm/emit_glasm.cpp b/src/shader_recompiler/backend/glasm/emit_glasm.cpp
index fa48ba25c..775dd9e7e 100644
--- a/src/shader_recompiler/backend/glasm/emit_glasm.cpp
+++ b/src/shader_recompiler/backend/glasm/emit_glasm.cpp
@@ -2,6 +2,7 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <ranges>
5#include <string> 6#include <string>
6#include <tuple> 7#include <tuple>
7 8
@@ -9,6 +10,7 @@
9#include "shader_recompiler/backend/glasm/emit_context.h" 10#include "shader_recompiler/backend/glasm/emit_context.h"
10#include "shader_recompiler/backend/glasm/emit_glasm.h" 11#include "shader_recompiler/backend/glasm/emit_glasm.h"
11#include "shader_recompiler/backend/glasm/emit_glasm_instructions.h" 12#include "shader_recompiler/backend/glasm/emit_glasm_instructions.h"
13#include "shader_recompiler/frontend/ir/ir_emitter.h"
12#include "shader_recompiler/frontend/ir/program.h" 14#include "shader_recompiler/frontend/ir/program.h"
13#include "shader_recompiler/profile.h" 15#include "shader_recompiler/profile.h"
14 16
@@ -175,6 +177,34 @@ void EmitInst(EmitContext& ctx, IR::Inst* inst) {
175 throw LogicError("Invalid opcode {}", inst->GetOpcode()); 177 throw LogicError("Invalid opcode {}", inst->GetOpcode());
176} 178}
177 179
180void Precolor(EmitContext& ctx, const IR::Program& program) {
181 for (IR::Block* const block : program.blocks) {
182 for (IR::Inst& phi : block->Instructions() | std::views::take_while(IR::IsPhi)) {
183 switch (phi.Arg(0).Type()) {
184 case IR::Type::U1:
185 case IR::Type::U32:
186 case IR::Type::F32:
187 ctx.reg_alloc.Define(phi);
188 break;
189 case IR::Type::U64:
190 case IR::Type::F64:
191 ctx.reg_alloc.LongDefine(phi);
192 break;
193 default:
194 throw NotImplementedException("Phi node type {}", phi.Type());
195 }
196 const size_t num_args{phi.NumArgs()};
197 for (size_t i = 0; i < num_args; ++i) {
198 IR::IREmitter{*phi.PhiBlock(i)}.PhiMove(phi, phi.Arg(i));
199 }
200 // Add reference to the phi node on the phi predecessor to avoid overwritting it
201 for (size_t i = 0; i < num_args; ++i) {
202 IR::IREmitter{*phi.PhiBlock(i)}.DummyReference(IR::Value{&phi});
203 }
204 }
205 }
206}
207
178void EmitCode(EmitContext& ctx, const IR::Program& program) { 208void EmitCode(EmitContext& ctx, const IR::Program& program) {
179 const auto eval{ 209 const auto eval{
180 [&](const IR::U1& cond) { return ScalarS32{ctx.reg_alloc.Consume(IR::Value{cond})}; }}; 210 [&](const IR::U1& cond) { return ScalarS32{ctx.reg_alloc.Consume(IR::Value{cond})}; }};
@@ -186,7 +216,9 @@ void EmitCode(EmitContext& ctx, const IR::Program& program) {
186 } 216 }
187 break; 217 break;
188 case IR::AbstractSyntaxNode::Type::If: 218 case IR::AbstractSyntaxNode::Type::If:
189 ctx.Add("MOV.S.CC RC,{};IF NE.x;", eval(node.if_node.cond)); 219 ctx.Add("MOV.S.CC RC,{};"
220 "IF NE.x;",
221 eval(node.if_node.cond));
190 break; 222 break;
191 case IR::AbstractSyntaxNode::Type::EndIf: 223 case IR::AbstractSyntaxNode::Type::EndIf:
192 ctx.Add("ENDIF;"); 224 ctx.Add("ENDIF;");
@@ -195,10 +227,30 @@ void EmitCode(EmitContext& ctx, const IR::Program& program) {
195 ctx.Add("REP;"); 227 ctx.Add("REP;");
196 break; 228 break;
197 case IR::AbstractSyntaxNode::Type::Repeat: 229 case IR::AbstractSyntaxNode::Type::Repeat:
198 ctx.Add("MOV.S.CC RC,{};BRK NE.x;ENDREP;", eval(node.repeat.cond)); 230 if (node.repeat.cond.IsImmediate()) {
231 if (node.repeat.cond.U1()) {
232 ctx.Add("ENDREP;");
233 } else {
234 ctx.Add("BRK;"
235 "ENDREP;");
236 }
237 } else {
238 ctx.Add("MOV.S.CC RC,{};"
239 "BRK (EQ.x);"
240 "ENDREP;",
241 eval(node.repeat.cond));
242 }
199 break; 243 break;
200 case IR::AbstractSyntaxNode::Type::Break: 244 case IR::AbstractSyntaxNode::Type::Break:
201 ctx.Add("MOV.S.CC RC,{};BRK NE.x;", eval(node.repeat.cond)); 245 if (node.break_node.cond.IsImmediate()) {
246 if (node.break_node.cond.U1()) {
247 ctx.Add("BRK;");
248 }
249 } else {
250 ctx.Add("MOV.S.CC RC,{};"
251 "BRK (NE.x);",
252 eval(node.break_node.cond));
253 }
202 break; 254 break;
203 case IR::AbstractSyntaxNode::Type::Return: 255 case IR::AbstractSyntaxNode::Type::Return:
204 case IR::AbstractSyntaxNode::Type::Unreachable: 256 case IR::AbstractSyntaxNode::Type::Unreachable:
@@ -233,6 +285,7 @@ void SetupOptions(std::string& header, Info info) {
233 285
234std::string EmitGLASM(const Profile&, IR::Program& program, Bindings&) { 286std::string EmitGLASM(const Profile&, IR::Program& program, Bindings&) {
235 EmitContext ctx{program}; 287 EmitContext ctx{program};
288 Precolor(ctx, program);
236 EmitCode(ctx, program); 289 EmitCode(ctx, program);
237 std::string header = "!!NVcp5.0\n" 290 std::string header = "!!NVcp5.0\n"
238 "OPTION NV_internal;"; 291 "OPTION NV_internal;";
diff --git a/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h b/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h
index 0f7f16e6e..a74e422d6 100644
--- a/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h
+++ b/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h
@@ -22,7 +22,8 @@ class EmitContext;
22void EmitPhi(EmitContext& ctx, IR::Inst& inst); 22void EmitPhi(EmitContext& ctx, IR::Inst& inst);
23void EmitVoid(EmitContext& ctx); 23void EmitVoid(EmitContext& ctx);
24void EmitIdentity(EmitContext& ctx, IR::Inst& inst, const IR::Value& value); 24void EmitIdentity(EmitContext& ctx, IR::Inst& inst, const IR::Value& value);
25void EmitBranchConditionRef(EmitContext&); 25void EmitDummyReference(EmitContext&);
26void EmitPhiMove(EmitContext& ctx, const IR::Value& phi, const IR::Value& value);
26void EmitJoin(EmitContext& ctx); 27void EmitJoin(EmitContext& ctx);
27void EmitDemoteToHelperInvocation(EmitContext& ctx); 28void EmitDemoteToHelperInvocation(EmitContext& ctx);
28void EmitBarrier(EmitContext& ctx); 29void EmitBarrier(EmitContext& ctx);
diff --git a/src/shader_recompiler/backend/glasm/emit_glasm_not_implemented.cpp b/src/shader_recompiler/backend/glasm/emit_glasm_not_implemented.cpp
index f37ad5587..969b91a81 100644
--- a/src/shader_recompiler/backend/glasm/emit_glasm_not_implemented.cpp
+++ b/src/shader_recompiler/backend/glasm/emit_glasm_not_implemented.cpp
@@ -17,13 +17,32 @@ namespace Shader::Backend::GLASM {
17 17
18#define NotImplemented() throw NotImplementedException("GLASM instruction {}", __LINE__) 18#define NotImplemented() throw NotImplementedException("GLASM instruction {}", __LINE__)
19 19
20void EmitPhi(EmitContext& ctx, IR::Inst& inst) { 20void EmitPhi(EmitContext&, IR::Inst&) {}
21 NotImplemented();
22}
23 21
24void EmitVoid(EmitContext&) {} 22void EmitVoid(EmitContext&) {}
25 23
26void EmitBranchConditionRef(EmitContext&) {} 24void EmitDummyReference(EmitContext&) {}
25
26void EmitPhiMove(EmitContext& ctx, const IR::Value& phi, const IR::Value& value) {
27 if (phi == value) {
28 return;
29 }
30 const Register phi_reg{ctx.reg_alloc.Consume(phi)};
31 const Value eval_value{ctx.reg_alloc.Consume(value)};
32 switch (phi.InstRecursive()->Arg(0).Type()) {
33 case IR::Type::U1:
34 case IR::Type::U32:
35 case IR::Type::F32:
36 ctx.Add("MOV.S {}.x,{};", phi_reg, ScalarS32{eval_value});
37 break;
38 case IR::Type::U64:
39 case IR::Type::F64:
40 ctx.Add("MOV.U64 {}.x,{};", phi_reg, ScalarRegister{eval_value});
41 break;
42 default:
43 throw NotImplementedException("Phi node type {}", phi.Type());
44 }
45}
27 46
28void EmitJoin(EmitContext& ctx) { 47void EmitJoin(EmitContext& ctx) {
29 NotImplemented(); 48 NotImplemented();
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv.cpp b/src/shader_recompiler/backend/spirv/emit_spirv.cpp
index c22edfec2..7bf8c78de 100644
--- a/src/shader_recompiler/backend/spirv/emit_spirv.cpp
+++ b/src/shader_recompiler/backend/spirv/emit_spirv.cpp
@@ -468,7 +468,11 @@ Id EmitIdentity(EmitContext& ctx, const IR::Value& value) {
468 return id; 468 return id;
469} 469}
470 470
471void EmitBranchConditionRef(EmitContext&) {} 471void EmitDummyReference(EmitContext&) {}
472
473void EmitPhiMove(EmitContext&) {
474 throw LogicError("Unreachable instruction");
475}
472 476
473void EmitGetZeroFromOp(EmitContext&) { 477void EmitGetZeroFromOp(EmitContext&) {
474 throw LogicError("Unreachable instruction"); 478 throw LogicError("Unreachable instruction");
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h b/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h
index 2f4f6e59e..0a2b31772 100644
--- a/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h
+++ b/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h
@@ -23,7 +23,8 @@ class EmitContext;
23Id EmitPhi(EmitContext& ctx, IR::Inst* inst); 23Id EmitPhi(EmitContext& ctx, IR::Inst* inst);
24void EmitVoid(EmitContext& ctx); 24void EmitVoid(EmitContext& ctx);
25Id EmitIdentity(EmitContext& ctx, const IR::Value& value); 25Id EmitIdentity(EmitContext& ctx, const IR::Value& value);
26void EmitBranchConditionRef(EmitContext&); 26void EmitDummyReference(EmitContext&);
27void EmitPhiMove(EmitContext&);
27void EmitJoin(EmitContext& ctx); 28void EmitJoin(EmitContext& ctx);
28void EmitDemoteToHelperInvocation(EmitContext& ctx); 29void EmitDemoteToHelperInvocation(EmitContext& ctx);
29void EmitBarrier(EmitContext& ctx); 30void EmitBarrier(EmitContext& ctx);