mirror of
https://github.com/yuzu-mirror/yuzu.git
synced 2024-11-05 10:49:58 +00:00
glasm: Changes to GLASM register allocator and emit context
This commit is contained in:
parent
36f1586267
commit
c1ba685d9c
4 changed files with 64 additions and 26 deletions
|
@ -2,6 +2,10 @@
|
||||||
// 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.
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "shader_recompiler/backend/glasm/emit_context.h"
|
#include "shader_recompiler/backend/glasm/emit_context.h"
|
||||||
|
|
||||||
|
namespace Shader::Backend::GLASM {
|
||||||
|
|
||||||
|
EmitContext::EmitContext() = default;
|
||||||
|
|
||||||
|
} // namespace Shader::Backend::GLASM
|
||||||
|
|
|
@ -5,17 +5,38 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include <fmt/format.h>
|
||||||
|
|
||||||
#include "shader_recompiler/backend/glasm/reg_alloc.h"
|
#include "shader_recompiler/backend/glasm/reg_alloc.h"
|
||||||
|
|
||||||
|
namespace Shader::IR {
|
||||||
|
class Inst;
|
||||||
|
}
|
||||||
|
|
||||||
namespace Shader::Backend::GLASM {
|
namespace Shader::Backend::GLASM {
|
||||||
|
|
||||||
class EmitContext {
|
class EmitContext {
|
||||||
public:
|
public:
|
||||||
std::string code;
|
explicit EmitContext();
|
||||||
RegAlloc reg_alloc;
|
|
||||||
|
|
||||||
private:
|
template <typename... Args>
|
||||||
|
void Add(const char* fmt, IR::Inst& inst, Args&&... args) {
|
||||||
|
code += fmt::format(fmt, reg_alloc.Define(inst), std::forward<Args>(args)...);
|
||||||
|
// TODO: Remove this
|
||||||
|
code += '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
void Add(const char* fmt, Args&&... args) {
|
||||||
|
code += fmt::format(fmt, std::forward<Args>(args)...);
|
||||||
|
// TODO: Remove this
|
||||||
|
code += '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string code;
|
||||||
|
RegAlloc reg_alloc{*this};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Shader::Backend::GLASM
|
} // namespace Shader::Backend::GLASM
|
||||||
|
|
|
@ -3,18 +3,16 @@
|
||||||
// Refer to the license.txt file included.
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
|
||||||
|
|
||||||
#include <fmt/format.h>
|
#include <fmt/format.h>
|
||||||
|
|
||||||
|
#include "shader_recompiler/backend/glasm/emit_context.h"
|
||||||
#include "shader_recompiler/backend/glasm/reg_alloc.h"
|
#include "shader_recompiler/backend/glasm/reg_alloc.h"
|
||||||
#include "shader_recompiler/exception.h"
|
#include "shader_recompiler/exception.h"
|
||||||
#include "shader_recompiler/frontend/ir/value.h"
|
#include "shader_recompiler/frontend/ir/value.h"
|
||||||
|
|
||||||
namespace Shader::Backend::GLASM {
|
namespace Shader::Backend::GLASM {
|
||||||
namespace {
|
namespace {
|
||||||
constexpr std::string_view SWIZZLE = "xyzw";
|
|
||||||
|
|
||||||
std::string Representation(Id id) {
|
std::string Representation(Id id) {
|
||||||
if (id.is_condition_code != 0) {
|
if (id.is_condition_code != 0) {
|
||||||
throw NotImplementedException("Condition code");
|
throw NotImplementedException("Condition code");
|
||||||
|
@ -22,27 +20,36 @@ std::string Representation(Id id) {
|
||||||
if (id.is_spill != 0) {
|
if (id.is_spill != 0) {
|
||||||
throw NotImplementedException("Spilling");
|
throw NotImplementedException("Spilling");
|
||||||
}
|
}
|
||||||
const u32 num_elements{id.num_elements_minus_one + 1};
|
|
||||||
const u32 index{static_cast<u32>(id.index)};
|
const u32 index{static_cast<u32>(id.index)};
|
||||||
if (num_elements == 4) {
|
return fmt::format("R{}.x", index);
|
||||||
return fmt::format("R{}", index);
|
}
|
||||||
} else {
|
|
||||||
return fmt::format("R{}.{}", index, SWIZZLE.substr(id.base_element, num_elements));
|
std::string ImmValue(const IR::Value& value) {
|
||||||
|
switch (value.Type()) {
|
||||||
|
case IR::Type::U1:
|
||||||
|
return value.U1() ? "-1" : "0";
|
||||||
|
case IR::Type::U32:
|
||||||
|
return fmt::format("{}", value.U32());
|
||||||
|
case IR::Type::F32:
|
||||||
|
return fmt::format("{}", value.F32());
|
||||||
|
default:
|
||||||
|
throw NotImplementedException("Immediate type", value.Type());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // Anonymous namespace
|
} // Anonymous namespace
|
||||||
|
|
||||||
std::string RegAlloc::Define(IR::Inst& inst, u32 num_elements, u32 alignment) {
|
std::string RegAlloc::Define(IR::Inst& inst) {
|
||||||
const Id id{Alloc(num_elements, alignment)};
|
const Id id{Alloc()};
|
||||||
inst.SetDefinition<Id>(id);
|
inst.SetDefinition<Id>(id);
|
||||||
return Representation(id);
|
return Representation(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string RegAlloc::Consume(const IR::Value& value) {
|
std::string RegAlloc::Consume(const IR::Value& value) {
|
||||||
if (!value.IsImmediate()) {
|
if (value.IsImmediate()) {
|
||||||
return Consume(*value.Inst());
|
return ImmValue(value);
|
||||||
|
} else {
|
||||||
|
return Consume(*value.InstRecursive());
|
||||||
}
|
}
|
||||||
throw NotImplementedException("Immediate loading");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string RegAlloc::Consume(IR::Inst& inst) {
|
std::string RegAlloc::Consume(IR::Inst& inst) {
|
||||||
|
@ -54,7 +61,7 @@ std::string RegAlloc::Consume(IR::Inst& inst) {
|
||||||
return Representation(inst.Definition<Id>());
|
return Representation(inst.Definition<Id>());
|
||||||
}
|
}
|
||||||
|
|
||||||
Id RegAlloc::Alloc(u32 num_elements, [[maybe_unused]] u32 alignment) {
|
Id RegAlloc::Alloc() {
|
||||||
for (size_t reg = 0; reg < NUM_REGS; ++reg) {
|
for (size_t reg = 0; reg < NUM_REGS; ++reg) {
|
||||||
if (register_use[reg]) {
|
if (register_use[reg]) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -62,8 +69,6 @@ Id RegAlloc::Alloc(u32 num_elements, [[maybe_unused]] u32 alignment) {
|
||||||
num_used_registers = std::max(num_used_registers, reg + 1);
|
num_used_registers = std::max(num_used_registers, reg + 1);
|
||||||
register_use[reg] = true;
|
register_use[reg] = true;
|
||||||
return Id{
|
return Id{
|
||||||
.base_element = 0,
|
|
||||||
.num_elements_minus_one = num_elements - 1,
|
|
||||||
.index = static_cast<u32>(reg),
|
.index = static_cast<u32>(reg),
|
||||||
.is_spill = 0,
|
.is_spill = 0,
|
||||||
.is_condition_code = 0,
|
.is_condition_code = 0,
|
||||||
|
|
|
@ -15,27 +15,35 @@ class Value;
|
||||||
|
|
||||||
namespace Shader::Backend::GLASM {
|
namespace Shader::Backend::GLASM {
|
||||||
|
|
||||||
|
class EmitContext;
|
||||||
|
|
||||||
struct Id {
|
struct Id {
|
||||||
u32 base_element : 2;
|
u32 index : 30;
|
||||||
u32 num_elements_minus_one : 2;
|
|
||||||
u32 index : 26;
|
|
||||||
u32 is_spill : 1;
|
u32 is_spill : 1;
|
||||||
u32 is_condition_code : 1;
|
u32 is_condition_code : 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
class RegAlloc {
|
class RegAlloc {
|
||||||
public:
|
public:
|
||||||
std::string Define(IR::Inst& inst, u32 num_elements = 1, u32 alignment = 1);
|
RegAlloc(EmitContext& ctx_) : ctx{ctx_} {}
|
||||||
|
|
||||||
|
std::string Define(IR::Inst& inst);
|
||||||
|
|
||||||
std::string Consume(const IR::Value& value);
|
std::string Consume(const IR::Value& value);
|
||||||
|
|
||||||
|
[[nodiscard]] size_t NumUsedRegisters() const noexcept {
|
||||||
|
return num_used_registers;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static constexpr size_t NUM_REGS = 4096;
|
static constexpr size_t NUM_REGS = 4096;
|
||||||
static constexpr size_t NUM_ELEMENTS = 4;
|
static constexpr size_t NUM_ELEMENTS = 4;
|
||||||
|
|
||||||
|
EmitContext& ctx;
|
||||||
|
|
||||||
std::string Consume(IR::Inst& inst);
|
std::string Consume(IR::Inst& inst);
|
||||||
|
|
||||||
Id Alloc(u32 num_elements, u32 alignment);
|
Id Alloc();
|
||||||
|
|
||||||
void Free(Id id);
|
void Free(Id id);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue