2021-05-05 05:19:08 +00:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <bitset>
|
|
|
|
|
2021-05-09 06:11:34 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
|
|
|
#include "common/bit_cast.h"
|
2021-05-08 19:46:32 +00:00
|
|
|
#include "common/bit_field.h"
|
2021-05-05 05:19:08 +00:00
|
|
|
#include "common/common_types.h"
|
2021-05-09 06:11:34 +00:00
|
|
|
#include "shader_recompiler/exception.h"
|
2021-05-05 05:19:08 +00:00
|
|
|
|
|
|
|
namespace Shader::IR {
|
|
|
|
class Inst;
|
|
|
|
class Value;
|
|
|
|
} // namespace Shader::IR
|
|
|
|
|
|
|
|
namespace Shader::Backend::GLASM {
|
|
|
|
|
2021-05-07 09:31:30 +00:00
|
|
|
class EmitContext;
|
|
|
|
|
2021-05-09 06:11:34 +00:00
|
|
|
enum class Type : u32 {
|
2021-05-15 21:15:13 +00:00
|
|
|
Void,
|
2021-05-09 06:11:34 +00:00
|
|
|
Register,
|
|
|
|
U32,
|
|
|
|
S32,
|
|
|
|
F32,
|
2021-05-10 01:43:29 +00:00
|
|
|
U64,
|
2021-05-09 21:03:01 +00:00
|
|
|
F64,
|
2021-05-09 06:11:34 +00:00
|
|
|
};
|
|
|
|
|
2021-05-05 05:19:08 +00:00
|
|
|
struct Id {
|
2021-05-08 19:46:32 +00:00
|
|
|
union {
|
|
|
|
u32 raw;
|
2021-05-09 21:03:01 +00:00
|
|
|
BitField<0, 29, u32> index;
|
|
|
|
BitField<29, 1, u32> is_long;
|
2021-05-08 19:46:32 +00:00
|
|
|
BitField<30, 1, u32> is_spill;
|
|
|
|
BitField<31, 1, u32> is_condition_code;
|
|
|
|
};
|
2021-05-09 06:11:34 +00:00
|
|
|
|
|
|
|
bool operator==(Id rhs) const noexcept {
|
|
|
|
return raw == rhs.raw;
|
|
|
|
}
|
|
|
|
bool operator!=(Id rhs) const noexcept {
|
|
|
|
return !operator==(rhs);
|
|
|
|
}
|
2021-05-05 05:19:08 +00:00
|
|
|
};
|
2021-05-09 06:11:34 +00:00
|
|
|
static_assert(sizeof(Id) == sizeof(u32));
|
|
|
|
|
|
|
|
struct Value {
|
|
|
|
Type type;
|
|
|
|
union {
|
|
|
|
Id id;
|
|
|
|
u32 imm_u32;
|
|
|
|
s32 imm_s32;
|
|
|
|
f32 imm_f32;
|
2021-05-10 01:43:29 +00:00
|
|
|
u64 imm_u64;
|
2021-05-09 21:03:01 +00:00
|
|
|
f64 imm_f64;
|
2021-05-09 06:11:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool operator==(const Value& rhs) const noexcept {
|
|
|
|
if (type != rhs.type) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch (type) {
|
2021-05-15 21:15:13 +00:00
|
|
|
case Type::Void:
|
|
|
|
return true;
|
2021-05-09 06:11:34 +00:00
|
|
|
case Type::Register:
|
|
|
|
return id == rhs.id;
|
|
|
|
case Type::U32:
|
|
|
|
return imm_u32 == rhs.imm_u32;
|
|
|
|
case Type::S32:
|
|
|
|
return imm_s32 == rhs.imm_s32;
|
|
|
|
case Type::F32:
|
|
|
|
return Common::BitCast<u32>(imm_f32) == Common::BitCast<u32>(rhs.imm_f32);
|
2021-05-10 01:43:29 +00:00
|
|
|
case Type::U64:
|
|
|
|
return imm_u64 == rhs.imm_u64;
|
2021-05-09 21:03:01 +00:00
|
|
|
case Type::F64:
|
|
|
|
return Common::BitCast<u64>(imm_f64) == Common::BitCast<u64>(rhs.imm_f64);
|
2021-05-09 06:11:34 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool operator!=(const Value& rhs) const noexcept {
|
|
|
|
return !operator==(rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
struct Register : Value {};
|
|
|
|
struct ScalarRegister : Value {};
|
|
|
|
struct ScalarU32 : Value {};
|
|
|
|
struct ScalarS32 : Value {};
|
|
|
|
struct ScalarF32 : Value {};
|
2021-05-09 21:03:01 +00:00
|
|
|
struct ScalarF64 : Value {};
|
2021-05-05 05:19:08 +00:00
|
|
|
|
|
|
|
class RegAlloc {
|
|
|
|
public:
|
2021-05-07 09:31:30 +00:00
|
|
|
RegAlloc(EmitContext& ctx_) : ctx{ctx_} {}
|
|
|
|
|
2021-05-09 06:11:34 +00:00
|
|
|
Register Define(IR::Inst& inst);
|
|
|
|
|
2021-05-09 21:03:01 +00:00
|
|
|
Register LongDefine(IR::Inst& inst);
|
|
|
|
|
2021-05-10 06:47:31 +00:00
|
|
|
[[nodiscard]] Value Peek(const IR::Value& value);
|
|
|
|
|
2021-05-09 06:11:34 +00:00
|
|
|
Value Consume(const IR::Value& value);
|
|
|
|
|
2021-05-10 06:47:31 +00:00
|
|
|
void Unref(IR::Inst& inst);
|
|
|
|
|
2021-05-09 21:03:01 +00:00
|
|
|
[[nodiscard]] Register AllocReg();
|
|
|
|
|
|
|
|
[[nodiscard]] Register AllocLongReg();
|
2021-05-05 05:19:08 +00:00
|
|
|
|
2021-05-09 06:11:34 +00:00
|
|
|
void FreeReg(Register reg);
|
2021-05-05 05:19:08 +00:00
|
|
|
|
2021-05-10 01:43:29 +00:00
|
|
|
void InvalidateConditionCodes() {
|
|
|
|
// This does nothing for now
|
|
|
|
}
|
|
|
|
|
2021-05-07 09:31:30 +00:00
|
|
|
[[nodiscard]] size_t NumUsedRegisters() const noexcept {
|
|
|
|
return num_used_registers;
|
|
|
|
}
|
|
|
|
|
2021-05-09 21:03:01 +00:00
|
|
|
[[nodiscard]] size_t NumUsedLongRegisters() const noexcept {
|
|
|
|
return num_used_long_registers;
|
|
|
|
}
|
|
|
|
|
2021-05-05 05:19:08 +00:00
|
|
|
private:
|
|
|
|
static constexpr size_t NUM_REGS = 4096;
|
|
|
|
static constexpr size_t NUM_ELEMENTS = 4;
|
|
|
|
|
2021-05-10 06:47:31 +00:00
|
|
|
Value MakeImm(const IR::Value& value);
|
|
|
|
|
2021-05-09 21:03:01 +00:00
|
|
|
Register Define(IR::Inst& inst, bool is_long);
|
|
|
|
|
2021-05-10 06:47:31 +00:00
|
|
|
Value PeekInst(IR::Inst& inst);
|
|
|
|
|
|
|
|
Value ConsumeInst(IR::Inst& inst);
|
2021-05-05 05:19:08 +00:00
|
|
|
|
2021-05-09 21:03:01 +00:00
|
|
|
Id Alloc(bool is_long);
|
2021-05-05 05:19:08 +00:00
|
|
|
|
|
|
|
void Free(Id id);
|
|
|
|
|
2021-05-09 06:11:34 +00:00
|
|
|
EmitContext& ctx;
|
2021-05-05 05:19:08 +00:00
|
|
|
size_t num_used_registers{};
|
2021-05-09 21:03:01 +00:00
|
|
|
size_t num_used_long_registers{};
|
2021-05-05 05:19:08 +00:00
|
|
|
std::bitset<NUM_REGS> register_use{};
|
2021-05-09 21:03:01 +00:00
|
|
|
std::bitset<NUM_REGS> long_register_use{};
|
2021-05-05 05:19:08 +00:00
|
|
|
};
|
|
|
|
|
2021-05-09 06:11:34 +00:00
|
|
|
template <bool scalar, typename FormatContext>
|
|
|
|
auto FormatTo(FormatContext& ctx, Id id) {
|
|
|
|
if (id.is_condition_code != 0) {
|
|
|
|
throw NotImplementedException("Condition code emission");
|
|
|
|
}
|
|
|
|
if (id.is_spill != 0) {
|
|
|
|
throw NotImplementedException("Spill emission");
|
|
|
|
}
|
|
|
|
if constexpr (scalar) {
|
2021-05-09 21:03:01 +00:00
|
|
|
if (id.is_long != 0) {
|
|
|
|
return fmt::format_to(ctx.out(), "D{}.x", id.index.Value());
|
|
|
|
} else {
|
|
|
|
return fmt::format_to(ctx.out(), "R{}.x", id.index.Value());
|
|
|
|
}
|
2021-05-09 06:11:34 +00:00
|
|
|
} else {
|
2021-05-09 21:03:01 +00:00
|
|
|
if (id.is_long != 0) {
|
|
|
|
return fmt::format_to(ctx.out(), "D{}", id.index.Value());
|
|
|
|
} else {
|
|
|
|
return fmt::format_to(ctx.out(), "R{}", id.index.Value());
|
|
|
|
}
|
2021-05-09 06:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 05:19:08 +00:00
|
|
|
} // namespace Shader::Backend::GLASM
|
2021-05-09 06:11:34 +00:00
|
|
|
|
|
|
|
template <>
|
|
|
|
struct fmt::formatter<Shader::Backend::GLASM::Id> {
|
|
|
|
constexpr auto parse(format_parse_context& ctx) {
|
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(Shader::Backend::GLASM::Id id, FormatContext& ctx) {
|
2021-05-09 07:08:19 +00:00
|
|
|
return Shader::Backend::GLASM::FormatTo<true>(ctx, id);
|
2021-05-09 06:11:34 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct fmt::formatter<Shader::Backend::GLASM::Register> {
|
|
|
|
constexpr auto parse(format_parse_context& ctx) {
|
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const Shader::Backend::GLASM::Register& value, FormatContext& ctx) {
|
|
|
|
if (value.type != Shader::Backend::GLASM::Type::Register) {
|
|
|
|
throw Shader::InvalidArgument("Register value type is not register");
|
|
|
|
}
|
2021-05-09 07:08:19 +00:00
|
|
|
return Shader::Backend::GLASM::FormatTo<false>(ctx, value.id);
|
2021-05-09 06:11:34 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct fmt::formatter<Shader::Backend::GLASM::ScalarRegister> {
|
|
|
|
constexpr auto parse(format_parse_context& ctx) {
|
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const Shader::Backend::GLASM::ScalarRegister& value, FormatContext& ctx) {
|
|
|
|
if (value.type != Shader::Backend::GLASM::Type::Register) {
|
|
|
|
throw Shader::InvalidArgument("Register value type is not register");
|
|
|
|
}
|
2021-05-09 07:08:19 +00:00
|
|
|
return Shader::Backend::GLASM::FormatTo<true>(ctx, value.id);
|
2021-05-09 06:11:34 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct fmt::formatter<Shader::Backend::GLASM::ScalarU32> {
|
|
|
|
constexpr auto parse(format_parse_context& ctx) {
|
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const Shader::Backend::GLASM::ScalarU32& value, FormatContext& ctx) {
|
|
|
|
switch (value.type) {
|
2021-05-15 21:15:13 +00:00
|
|
|
case Shader::Backend::GLASM::Type::Void:
|
|
|
|
break;
|
2021-05-09 06:11:34 +00:00
|
|
|
case Shader::Backend::GLASM::Type::Register:
|
2021-05-09 07:08:19 +00:00
|
|
|
return Shader::Backend::GLASM::FormatTo<true>(ctx, value.id);
|
2021-05-09 06:11:34 +00:00
|
|
|
case Shader::Backend::GLASM::Type::U32:
|
|
|
|
return fmt::format_to(ctx.out(), "{}", value.imm_u32);
|
|
|
|
case Shader::Backend::GLASM::Type::S32:
|
|
|
|
return fmt::format_to(ctx.out(), "{}", static_cast<u32>(value.imm_s32));
|
|
|
|
case Shader::Backend::GLASM::Type::F32:
|
|
|
|
return fmt::format_to(ctx.out(), "{}", Common::BitCast<u32>(value.imm_f32));
|
2021-05-10 01:43:29 +00:00
|
|
|
case Shader::Backend::GLASM::Type::U64:
|
2021-05-09 21:03:01 +00:00
|
|
|
case Shader::Backend::GLASM::Type::F64:
|
|
|
|
break;
|
2021-05-09 06:11:34 +00:00
|
|
|
}
|
|
|
|
throw Shader::InvalidArgument("Invalid value type {}", value.type);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct fmt::formatter<Shader::Backend::GLASM::ScalarS32> {
|
|
|
|
constexpr auto parse(format_parse_context& ctx) {
|
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const Shader::Backend::GLASM::ScalarS32& value, FormatContext& ctx) {
|
|
|
|
switch (value.type) {
|
2021-05-15 21:15:13 +00:00
|
|
|
case Shader::Backend::GLASM::Type::Void:
|
|
|
|
break;
|
2021-05-09 06:11:34 +00:00
|
|
|
case Shader::Backend::GLASM::Type::Register:
|
2021-05-09 07:08:19 +00:00
|
|
|
return Shader::Backend::GLASM::FormatTo<true>(ctx, value.id);
|
2021-05-09 06:11:34 +00:00
|
|
|
case Shader::Backend::GLASM::Type::U32:
|
|
|
|
return fmt::format_to(ctx.out(), "{}", static_cast<s32>(value.imm_u32));
|
|
|
|
case Shader::Backend::GLASM::Type::S32:
|
|
|
|
return fmt::format_to(ctx.out(), "{}", value.imm_s32);
|
|
|
|
case Shader::Backend::GLASM::Type::F32:
|
|
|
|
return fmt::format_to(ctx.out(), "{}", Common::BitCast<s32>(value.imm_f32));
|
2021-05-10 01:43:29 +00:00
|
|
|
case Shader::Backend::GLASM::Type::U64:
|
2021-05-09 21:03:01 +00:00
|
|
|
case Shader::Backend::GLASM::Type::F64:
|
|
|
|
break;
|
2021-05-09 06:11:34 +00:00
|
|
|
}
|
|
|
|
throw Shader::InvalidArgument("Invalid value type {}", value.type);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct fmt::formatter<Shader::Backend::GLASM::ScalarF32> {
|
|
|
|
constexpr auto parse(format_parse_context& ctx) {
|
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const Shader::Backend::GLASM::ScalarF32& value, FormatContext& ctx) {
|
|
|
|
switch (value.type) {
|
2021-05-15 21:15:13 +00:00
|
|
|
case Shader::Backend::GLASM::Type::Void:
|
|
|
|
break;
|
2021-05-09 06:11:34 +00:00
|
|
|
case Shader::Backend::GLASM::Type::Register:
|
2021-05-09 07:08:19 +00:00
|
|
|
return Shader::Backend::GLASM::FormatTo<true>(ctx, value.id);
|
2021-05-09 06:11:34 +00:00
|
|
|
case Shader::Backend::GLASM::Type::U32:
|
|
|
|
return fmt::format_to(ctx.out(), "{}", Common::BitCast<u32>(value.imm_u32));
|
|
|
|
case Shader::Backend::GLASM::Type::S32:
|
|
|
|
return fmt::format_to(ctx.out(), "{}", Common::BitCast<s32>(value.imm_s32));
|
|
|
|
case Shader::Backend::GLASM::Type::F32:
|
|
|
|
return fmt::format_to(ctx.out(), "{}", value.imm_f32);
|
2021-05-10 01:43:29 +00:00
|
|
|
case Shader::Backend::GLASM::Type::U64:
|
2021-05-09 21:03:01 +00:00
|
|
|
case Shader::Backend::GLASM::Type::F64:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
throw Shader::InvalidArgument("Invalid value type {}", value.type);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct fmt::formatter<Shader::Backend::GLASM::ScalarF64> {
|
|
|
|
constexpr auto parse(format_parse_context& ctx) {
|
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const Shader::Backend::GLASM::ScalarF64& value, FormatContext& ctx) {
|
|
|
|
switch (value.type) {
|
2021-05-15 21:15:13 +00:00
|
|
|
case Shader::Backend::GLASM::Type::Void:
|
|
|
|
break;
|
2021-05-09 21:03:01 +00:00
|
|
|
case Shader::Backend::GLASM::Type::Register:
|
|
|
|
return Shader::Backend::GLASM::FormatTo<true>(ctx, value.id);
|
|
|
|
case Shader::Backend::GLASM::Type::U32:
|
|
|
|
case Shader::Backend::GLASM::Type::S32:
|
|
|
|
case Shader::Backend::GLASM::Type::F32:
|
|
|
|
break;
|
2021-05-10 01:43:29 +00:00
|
|
|
case Shader::Backend::GLASM::Type::U64:
|
2021-05-10 04:33:24 +00:00
|
|
|
return fmt::format_to(ctx.out(), "{}", Common::BitCast<f64>(value.imm_u64));
|
2021-05-09 21:03:01 +00:00
|
|
|
case Shader::Backend::GLASM::Type::F64:
|
2021-05-10 04:33:24 +00:00
|
|
|
return fmt::format_to(ctx.out(), "{}", value.imm_f64);
|
2021-05-09 06:11:34 +00:00
|
|
|
}
|
|
|
|
throw Shader::InvalidArgument("Invalid value type {}", value.type);
|
|
|
|
}
|
|
|
|
};
|