summaryrefslogtreecommitdiff
path: root/src/shader_recompiler/backend/glasm/emit_glasm.cpp
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/glasm/emit_glasm.cpp
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/glasm/emit_glasm.cpp')
-rw-r--r--src/shader_recompiler/backend/glasm/emit_glasm.cpp59
1 files changed, 56 insertions, 3 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;";