diff options
| author | 2021-05-05 02:19:08 -0300 | |
|---|---|---|
| committer | 2021-07-22 21:51:30 -0400 | |
| commit | b10cf64c486d8730fcfeb53a333814915b3b5fbe (patch) | |
| tree | 59cb4f62ac806071e0f0a341589c164017520f3a /src/shader_recompiler/backend/glasm/reg_alloc.cpp | |
| parent | shader: ISET.X implementation (diff) | |
| download | yuzu-b10cf64c486d8730fcfeb53a333814915b3b5fbe.tar.gz yuzu-b10cf64c486d8730fcfeb53a333814915b3b5fbe.tar.xz yuzu-b10cf64c486d8730fcfeb53a333814915b3b5fbe.zip | |
glasm: Add GLASM backend infrastructure
Diffstat (limited to 'src/shader_recompiler/backend/glasm/reg_alloc.cpp')
| -rw-r--r-- | src/shader_recompiler/backend/glasm/reg_alloc.cpp | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/shader_recompiler/backend/glasm/reg_alloc.cpp b/src/shader_recompiler/backend/glasm/reg_alloc.cpp new file mode 100644 index 000000000..0460a394b --- /dev/null +++ b/src/shader_recompiler/backend/glasm/reg_alloc.cpp | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | // Copyright 2021 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <string> | ||
| 6 | #include <string_view> | ||
| 7 | |||
| 8 | #include <fmt/format.h> | ||
| 9 | |||
| 10 | #include "shader_recompiler/backend/glasm/reg_alloc.h" | ||
| 11 | #include "shader_recompiler/exception.h" | ||
| 12 | #include "shader_recompiler/frontend/ir/value.h" | ||
| 13 | |||
| 14 | namespace Shader::Backend::GLASM { | ||
| 15 | namespace { | ||
| 16 | constexpr std::string_view SWIZZLE = "xyzw"; | ||
| 17 | |||
| 18 | std::string Representation(Id id) { | ||
| 19 | if (id.is_condition_code != 0) { | ||
| 20 | throw NotImplementedException("Condition code"); | ||
| 21 | } | ||
| 22 | if (id.is_spill != 0) { | ||
| 23 | throw NotImplementedException("Spilling"); | ||
| 24 | } | ||
| 25 | const u32 num_elements{id.num_elements_minus_one + 1}; | ||
| 26 | const u32 index{static_cast<u32>(id.index)}; | ||
| 27 | if (num_elements == 4) { | ||
| 28 | return fmt::format("R{}", index); | ||
| 29 | } else { | ||
| 30 | return fmt::format("R{}.{}", index, SWIZZLE.substr(id.base_element, num_elements)); | ||
| 31 | } | ||
| 32 | } | ||
| 33 | } // Anonymous namespace | ||
| 34 | |||
| 35 | std::string RegAlloc::Define(IR::Inst& inst, u32 num_elements, u32 alignment) { | ||
| 36 | const Id id{Alloc(num_elements, alignment)}; | ||
| 37 | inst.SetDefinition<Id>(id); | ||
| 38 | return Representation(id); | ||
| 39 | } | ||
| 40 | |||
| 41 | std::string RegAlloc::Consume(const IR::Value& value) { | ||
| 42 | if (!value.IsImmediate()) { | ||
| 43 | return Consume(*value.Inst()); | ||
| 44 | } | ||
| 45 | throw NotImplementedException("Immediate loading"); | ||
| 46 | } | ||
| 47 | |||
| 48 | std::string RegAlloc::Consume(IR::Inst& inst) { | ||
| 49 | const Id id{inst.Definition<Id>()}; | ||
| 50 | inst.DestructiveRemoveUsage(); | ||
| 51 | if (!inst.HasUses()) { | ||
| 52 | Free(id); | ||
| 53 | } | ||
| 54 | return Representation(inst.Definition<Id>()); | ||
| 55 | } | ||
| 56 | |||
| 57 | Id RegAlloc::Alloc(u32 num_elements, [[maybe_unused]] u32 alignment) { | ||
| 58 | for (size_t reg = 0; reg < NUM_REGS; ++reg) { | ||
| 59 | if (register_use[reg]) { | ||
| 60 | continue; | ||
| 61 | } | ||
| 62 | num_used_registers = std::max(num_used_registers, reg + 1); | ||
| 63 | register_use[reg] = true; | ||
| 64 | return Id{ | ||
| 65 | .base_element = 0, | ||
| 66 | .num_elements_minus_one = num_elements - 1, | ||
| 67 | .index = static_cast<u32>(reg), | ||
| 68 | .is_spill = 0, | ||
| 69 | .is_condition_code = 0, | ||
| 70 | }; | ||
| 71 | } | ||
| 72 | throw NotImplementedException("Register spilling"); | ||
| 73 | } | ||
| 74 | |||
| 75 | void RegAlloc::Free(Id id) { | ||
| 76 | if (id.is_spill != 0) { | ||
| 77 | throw NotImplementedException("Free spill"); | ||
| 78 | } | ||
| 79 | register_use[id.index] = false; | ||
| 80 | } | ||
| 81 | |||
| 82 | } // namespace Shader::Backend::GLASM | ||