diff options
Diffstat (limited to 'src/shader_recompiler/backend')
4 files changed, 53 insertions, 21 deletions
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl.cpp b/src/shader_recompiler/backend/glsl/emit_glsl.cpp index e5aaf81a7..a8e53cf66 100644 --- a/src/shader_recompiler/backend/glsl/emit_glsl.cpp +++ b/src/shader_recompiler/backend/glsl/emit_glsl.cpp | |||
| @@ -98,18 +98,33 @@ void EmitInst(EmitContext& ctx, IR::Inst* inst) { | |||
| 98 | throw LogicError("Invalid opcode {}", inst->GetOpcode()); | 98 | throw LogicError("Invalid opcode {}", inst->GetOpcode()); |
| 99 | } | 99 | } |
| 100 | 100 | ||
| 101 | void Precolor(EmitContext& ctx, const IR::Program& program) { | 101 | bool IsReference(IR::Inst& inst) { |
| 102 | return inst.GetOpcode() == IR::Opcode::Reference; | ||
| 103 | } | ||
| 104 | |||
| 105 | void PrecolorInst(IR::Inst& phi) { | ||
| 106 | // Insert phi moves before references to avoid overwritting other phis | ||
| 107 | const size_t num_args{phi.NumArgs()}; | ||
| 108 | for (size_t i = 0; i < num_args; ++i) { | ||
| 109 | IR::Block& phi_block{*phi.PhiBlock(i)}; | ||
| 110 | auto it{std::find_if_not(phi_block.rbegin(), phi_block.rend(), IsReference).base()}; | ||
| 111 | IR::IREmitter ir{phi_block, it}; | ||
| 112 | const IR::Value arg{phi.Arg(i)}; | ||
| 113 | if (arg.IsImmediate()) { | ||
| 114 | ir.PhiMove(phi, arg); | ||
| 115 | } else { | ||
| 116 | ir.PhiMove(phi, IR::Value{&RegAlloc::AliasInst(*arg.Inst())}); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | for (size_t i = 0; i < num_args; ++i) { | ||
| 120 | IR::IREmitter{*phi.PhiBlock(i)}.Reference(IR::Value{&phi}); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | void Precolor(const IR::Program& program) { | ||
| 102 | for (IR::Block* const block : program.blocks) { | 125 | for (IR::Block* const block : program.blocks) { |
| 103 | for (IR::Inst& phi : block->Instructions() | std::views::take_while(IR::IsPhi)) { | 126 | for (IR::Inst& phi : block->Instructions() | std::views::take_while(IR::IsPhi)) { |
| 104 | ctx.Add("{};", ctx.reg_alloc.Define(phi, phi.Arg(0).Type())); | 127 | PrecolorInst(phi); |
| 105 | const size_t num_args{phi.NumArgs()}; | ||
| 106 | for (size_t i = 0; i < num_args; ++i) { | ||
| 107 | IR::IREmitter{*phi.PhiBlock(i)}.PhiMove(phi, phi.Arg(i)); | ||
| 108 | } | ||
| 109 | // Add reference to the phi node on the phi predecessor to avoid overwritting it | ||
| 110 | for (size_t i = 0; i < num_args; ++i) { | ||
| 111 | IR::IREmitter{*phi.PhiBlock(i)}.Reference(IR::Value{&phi}); | ||
| 112 | } | ||
| 113 | } | 128 | } |
| 114 | } | 129 | } |
| 115 | } | 130 | } |
| @@ -158,7 +173,7 @@ void EmitCode(EmitContext& ctx, const IR::Program& program) { | |||
| 158 | std::string EmitGLSL(const Profile& profile, const RuntimeInfo&, IR::Program& program, | 173 | std::string EmitGLSL(const Profile& profile, const RuntimeInfo&, IR::Program& program, |
| 159 | Bindings& bindings) { | 174 | Bindings& bindings) { |
| 160 | EmitContext ctx{program, bindings, profile}; | 175 | EmitContext ctx{program, bindings, profile}; |
| 161 | Precolor(ctx, program); | 176 | Precolor(program); |
| 162 | EmitCode(ctx, program); | 177 | EmitCode(ctx, program); |
| 163 | return ctx.code; | 178 | return ctx.code; |
| 164 | } | 179 | } |
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_not_implemented.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_not_implemented.cpp index d67a1d81f..b37b3c76d 100644 --- a/src/shader_recompiler/backend/glsl/emit_glsl_not_implemented.cpp +++ b/src/shader_recompiler/backend/glsl/emit_glsl_not_implemented.cpp | |||
| @@ -19,8 +19,15 @@ static void NotImplemented() { | |||
| 19 | throw NotImplementedException("GLSL instruction"); | 19 | throw NotImplementedException("GLSL instruction"); |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | void EmitPhi(EmitContext& ctx, IR::Inst& inst) { | 22 | void EmitPhi(EmitContext& ctx, IR::Inst& phi) { |
| 23 | // NotImplemented(); | 23 | const size_t num_args{phi.NumArgs()}; |
| 24 | for (size_t i = 0; i < num_args; ++i) { | ||
| 25 | ctx.reg_alloc.Consume(phi.Arg(i)); | ||
| 26 | } | ||
| 27 | if (!phi.Definition<Id>().is_valid) { | ||
| 28 | // The phi node wasn't forward defined | ||
| 29 | ctx.Add("{};", ctx.reg_alloc.Define(phi, phi.Arg(0).Type())); | ||
| 30 | } | ||
| 24 | } | 31 | } |
| 25 | 32 | ||
| 26 | void EmitVoid(EmitContext& ctx) { | 33 | void EmitVoid(EmitContext& ctx) { |
| @@ -31,11 +38,18 @@ void EmitReference(EmitContext&) { | |||
| 31 | // NotImplemented(); | 38 | // NotImplemented(); |
| 32 | } | 39 | } |
| 33 | 40 | ||
| 34 | void EmitPhiMove(EmitContext& ctx, const IR::Value& phi, const IR::Value& value) { | 41 | void EmitPhiMove(EmitContext& ctx, const IR::Value& phi_value, const IR::Value& value) { |
| 35 | if (phi == value) { | 42 | IR::Inst& phi{RegAlloc::AliasInst(*phi_value.Inst())}; |
| 43 | if (!phi.Definition<Id>().is_valid) { | ||
| 44 | // The phi node wasn't forward defined | ||
| 45 | ctx.Add("{};", ctx.reg_alloc.Define(phi, phi.Arg(0).Type())); | ||
| 46 | } | ||
| 47 | const auto phi_reg{ctx.reg_alloc.Consume(IR::Value{&phi})}; | ||
| 48 | const auto val_reg{ctx.reg_alloc.Consume(value)}; | ||
| 49 | if (phi_reg == val_reg) { | ||
| 36 | return; | 50 | return; |
| 37 | } | 51 | } |
| 38 | ctx.Add("{}={};", ctx.reg_alloc.Consume(phi), ctx.reg_alloc.Consume(value)); | 52 | ctx.Add("{}={};", phi_reg, val_reg); |
| 39 | } | 53 | } |
| 40 | 54 | ||
| 41 | void EmitBranch(EmitContext& ctx, std::string_view label) { | 55 | void EmitBranch(EmitContext& ctx, std::string_view label) { |
diff --git a/src/shader_recompiler/backend/glsl/reg_alloc.cpp b/src/shader_recompiler/backend/glsl/reg_alloc.cpp index 3de4c1172..c60a87d91 100644 --- a/src/shader_recompiler/backend/glsl/reg_alloc.cpp +++ b/src/shader_recompiler/backend/glsl/reg_alloc.cpp | |||
| @@ -146,10 +146,11 @@ Id RegAlloc::Alloc() { | |||
| 146 | } | 146 | } |
| 147 | register_use[reg] = true; | 147 | register_use[reg] = true; |
| 148 | Id ret{}; | 148 | Id ret{}; |
| 149 | ret.index.Assign(static_cast<u32>(reg)); | 149 | ret.is_valid.Assign(1); |
| 150 | ret.is_long.Assign(0); | 150 | ret.is_long.Assign(0); |
| 151 | ret.is_spill.Assign(0); | 151 | ret.is_spill.Assign(0); |
| 152 | ret.is_condition_code.Assign(0); | 152 | ret.is_condition_code.Assign(0); |
| 153 | ret.index.Assign(static_cast<u32>(reg)); | ||
| 153 | return ret; | 154 | return ret; |
| 154 | } | 155 | } |
| 155 | } | 156 | } |
diff --git a/src/shader_recompiler/backend/glsl/reg_alloc.h b/src/shader_recompiler/backend/glsl/reg_alloc.h index 9f2ff8718..419e1e761 100644 --- a/src/shader_recompiler/backend/glsl/reg_alloc.h +++ b/src/shader_recompiler/backend/glsl/reg_alloc.h | |||
| @@ -33,10 +33,12 @@ enum class Type : u32 { | |||
| 33 | struct Id { | 33 | struct Id { |
| 34 | union { | 34 | union { |
| 35 | u32 raw; | 35 | u32 raw; |
| 36 | BitField<0, 29, u32> index; | 36 | BitField<0, 1, u32> is_valid; |
| 37 | BitField<29, 1, u32> is_long; | 37 | BitField<1, 1, u32> is_long; |
| 38 | BitField<30, 1, u32> is_spill; | 38 | BitField<2, 1, u32> is_spill; |
| 39 | BitField<31, 1, u32> is_condition_code; | 39 | BitField<3, 1, u32> is_condition_code; |
| 40 | BitField<4, 1, u32> is_null; | ||
| 41 | BitField<5, 27, u32> index; | ||
| 40 | }; | 42 | }; |
| 41 | 43 | ||
| 42 | bool operator==(Id rhs) const noexcept { | 44 | bool operator==(Id rhs) const noexcept { |