From 039b88b08e97636511b71b3d906ea42aedb4d1f2 Mon Sep 17 00:00:00 2001 From: boykopovar Date: Tue, 15 Sep 2026 11:36:41 +0300 Subject: [PATCH] refactor(shader): refactor ir builder to separated module + som getters impl --- core/shader/recompiler/CMakeLists.txt | 10 +- .../IrBuilderInternal.hpp | 14 + .../src/IrBuilder.cpp | 263 ------------------ .../src/IrBuilder/IrBuilderArithmetic.cpp | 46 +++ .../src/IrBuilder/IrBuilderConstants.cpp | 48 ++++ .../src/IrBuilder/IrBuilderControlFlow.cpp | 14 + .../src/IrBuilder/IrBuilderConversions.cpp | 30 ++ .../src/IrBuilder/IrBuilderCore.cpp | 60 ++++ .../IrBuilder/IrBuilderExtendedRegisters.cpp | 78 ++++++ .../src/IrBuilder/IrBuilderInternal.cpp | 15 + .../src/IrBuilder/IrBuilderLogic.cpp | 38 +++ .../src/IrBuilder/IrBuilderRegisters.cpp | 119 ++++++++ 12 files changed, 471 insertions(+), 264 deletions(-) create mode 100644 core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrBuilderInternal.hpp delete mode 100644 core/shader/recompiler/IntermediateRepresentation/src/IrBuilder.cpp create mode 100644 core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderArithmetic.cpp create mode 100644 core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderConstants.cpp create mode 100644 core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderControlFlow.cpp create mode 100644 core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderConversions.cpp create mode 100644 core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderCore.cpp create mode 100644 core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderExtendedRegisters.cpp create mode 100644 core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderInternal.cpp create mode 100644 core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderLogic.cpp create mode 100644 core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderRegisters.cpp diff --git a/core/shader/recompiler/CMakeLists.txt b/core/shader/recompiler/CMakeLists.txt index 81192e889..23ec073de 100644 --- a/core/shader/recompiler/CMakeLists.txt +++ b/core/shader/recompiler/CMakeLists.txt @@ -21,7 +21,15 @@ function(add_shader_recompiler target runtimeTarget) ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/IntermediateRepresentation/src/IrValue.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/IntermediateRepresentation/src/IrBlock.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/IntermediateRepresentation/src/IrProgram.cpp - ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/IntermediateRepresentation/src/IrBuilder.cpp + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/IntermediateRepresentation/src/IrBuilder/IrBuilderInternal.cpp + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/IntermediateRepresentation/src/IrBuilder/IrBuilderCore.cpp + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/IntermediateRepresentation/src/IrBuilder/IrBuilderConstants.cpp + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/IntermediateRepresentation/src/IrBuilder/IrBuilderRegisters.cpp + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/IntermediateRepresentation/src/IrBuilder/IrBuilderControlFlow.cpp + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/IntermediateRepresentation/src/IrBuilder/IrBuilderExtendedRegisters.cpp + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/IntermediateRepresentation/src/IrBuilder/IrBuilderConversions.cpp + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/IntermediateRepresentation/src/IrBuilder/IrBuilderArithmetic.cpp + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/IntermediateRepresentation/src/IrBuilder/IrBuilderLogic.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Translation/src/EmbeddedVertexFetch.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Translation/src/InstructionTranslator.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Translation/src/ScalarInstructions.cpp diff --git a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrBuilderInternal.hpp b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrBuilderInternal.hpp new file mode 100644 index 000000000..34c578d7b --- /dev/null +++ b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrBuilderInternal.hpp @@ -0,0 +1,14 @@ +#ifndef CORE_SHADER_RECOMPILIER_INTERMEDIATEREPRESENTATION_INCLUDE_INTERMEDIATEREPRESENTATION_IRBUILDERINTERNAL_HPP +#define CORE_SHADER_RECOMPILIER_INTERMEDIATEREPRESENTATION_INCLUDE_INTERMEDIATEREPRESENTATION_IRBUILDERINTERNAL_HPP + +#include "IntermediateRepresentation/IrProgram.hpp" +#include "IntermediateRepresentation/GuestRegister.hpp" + +namespace ShaderRecompiler { + +[[nodiscard]] IrValue& createLeafValue(IrProgram& program, IrType type); +[[nodiscard]] IrValue& createRegisterOperand(IrProgram& program, RegisterBank bank, std::uint32_t index, IrType type); + +} + +#endif diff --git a/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder.cpp b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder.cpp deleted file mode 100644 index 3aeaaa6e4..000000000 --- a/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder.cpp +++ /dev/null @@ -1,263 +0,0 @@ -#include "IntermediateRepresentation/IrBuilder.hpp" -#include - -namespace ShaderRecompiler { - -IrBuilder::IrBuilder(IrProgram& program) : program(program), insertionPoint(nullptr) { - throw std::runtime_error("IrBuilder::IrBuilder not implemented"); -} -void IrBuilder::SetInsertionPoint(IrBlock& block) { - throw std::runtime_error("IrBuilder::SetInsertionPoint not implemented"); -} -IrValue& IrBuilder::Constant(std::uint32_t value) { - throw std::runtime_error("IrBuilder::Constant not implemented"); -} -IrValue& IrBuilder::ReadRegister(const GuestRegister& reg) { - throw std::runtime_error("IrBuilder::ReadRegister not implemented"); -} -void IrBuilder::WriteRegister(const GuestRegister& reg, IrValue& value) { - throw std::runtime_error("IrBuilder::WriteRegister not implemented"); -} -IrValue& IrBuilder::Emit(IrOpcode opcode, IrType type, std::initializer_list arguments) { - throw std::runtime_error("IrBuilder::Emit not implemented"); -} -void IrBuilder::Branch(IrBlock& target) { - throw std::runtime_error("IrBuilder::Branch not implemented"); -} -void IrBuilder::BranchConditional(IrValue& condition, IrBlock& trueTarget, IrBlock& falseTarget) { - throw std::runtime_error("IrBuilder::BranchConditional not implemented"); -} - -IrValue& IrBuilder::ConstantU64(std::uint64_t value) { - throw std::runtime_error("IrBuilder::ConstantU64 not implemented"); -} - -IrValue& IrBuilder::ConstantF16(std::uint16_t bits) { - throw std::runtime_error("IrBuilder::ConstantF16 not implemented"); -} - -IrValue& IrBuilder::ConstantF32(float value) { - throw std::runtime_error("IrBuilder::ConstantF32 not implemented"); -} - -IrValue& IrBuilder::ConstantBool(bool value) { - throw std::runtime_error("IrBuilder::ConstantBool not implemented"); -} - -IrValue& IrBuilder::ConstantU8(std::uint8_t value) { - throw std::runtime_error("IrBuilder::ConstantU8 not implemented"); -} - -IrValue& IrBuilder::ConstantU16(std::uint16_t value) { - throw std::runtime_error("IrBuilder::ConstantU16 not implemented"); -} - -IrValue& IrBuilder::Emit(IrOpcode opcode, IrType type, std::initializer_list arguments, std::uint64_t flags) { - throw std::runtime_error("IrBuilder::Emit not implemented"); -} - -IrValue& IrBuilder::GetUserData(ScalarReg reg) { - throw std::runtime_error("IrBuilder::GetUserData not implemented"); -} - -IrValue& IrBuilder::GetScalarReg(ScalarReg reg) { - throw std::runtime_error("IrBuilder::GetScalarReg not implemented"); -} - -void IrBuilder::SetScalarReg(ScalarReg reg, IrValue& value) { - throw std::runtime_error("IrBuilder::SetScalarReg not implemented"); -} - -IrValue& IrBuilder::GetThreadBitScalarReg(ScalarReg reg) { - throw std::runtime_error("IrBuilder::GetThreadBitScalarReg not implemented"); -} - -void IrBuilder::SetThreadBitScalarReg(ScalarReg reg, IrValue& value) { - throw std::runtime_error("IrBuilder::SetThreadBitScalarReg not implemented"); -} - -IrValue& IrBuilder::GetScalarMaskTag(ScalarReg reg) { - throw std::runtime_error("IrBuilder::GetScalarMaskTag not implemented"); -} - -void IrBuilder::SetScalarMaskTag(ScalarReg reg, IrValue& value) { - throw std::runtime_error("IrBuilder::SetScalarMaskTag not implemented"); -} - -IrValue& IrBuilder::GetVectorReg(VectorReg reg) { - throw std::runtime_error("IrBuilder::GetVectorReg not implemented"); -} - -void IrBuilder::SetVectorReg(VectorReg reg, IrValue& value) { - throw std::runtime_error("IrBuilder::SetVectorReg not implemented"); -} - -IrValue& IrBuilder::GetGotoVariable(std::uint32_t id) { - throw std::runtime_error("IrBuilder::GetGotoVariable not implemented"); -} - -void IrBuilder::SetGotoVariable(std::uint32_t id, IrValue& value) { - throw std::runtime_error("IrBuilder::SetGotoVariable not implemented"); -} - -IrValue& IrBuilder::GetScc() { - throw std::runtime_error("IrBuilder::GetScc not implemented"); -} - -void IrBuilder::SetScc(IrValue& value) { - throw std::runtime_error("IrBuilder::SetScc not implemented"); -} - -IrValue& IrBuilder::GetExec() { - throw std::runtime_error("IrBuilder::GetExec not implemented"); -} - -void IrBuilder::SetExec(IrValue& value) { - throw std::runtime_error("IrBuilder::SetExec not implemented"); -} - -IrValue& IrBuilder::GetExecLo() { - throw std::runtime_error("IrBuilder::GetExecLo not implemented"); -} - -void IrBuilder::SetExecLo(IrValue& value) { - throw std::runtime_error("IrBuilder::SetExecLo not implemented"); -} - -IrValue& IrBuilder::GetExecHi() { - throw std::runtime_error("IrBuilder::GetExecHi not implemented"); -} - -void IrBuilder::SetExecHi(IrValue& value) { - throw std::runtime_error("IrBuilder::SetExecHi not implemented"); -} - -IrValue& IrBuilder::GetVcc() { - throw std::runtime_error("IrBuilder::GetVcc not implemented"); -} - -void IrBuilder::SetVcc(IrValue& value) { - throw std::runtime_error("IrBuilder::SetVcc not implemented"); -} - -IrValue& IrBuilder::GetVccLo() { - throw std::runtime_error("IrBuilder::GetVccLo not implemented"); -} - -void IrBuilder::SetVccLo(IrValue& value) { - throw std::runtime_error("IrBuilder::SetVccLo not implemented"); -} - -IrValue& IrBuilder::GetVccHi() { - throw std::runtime_error("IrBuilder::GetVccHi not implemented"); -} - -void IrBuilder::SetVccHi(IrValue& value) { - throw std::runtime_error("IrBuilder::SetVccHi not implemented"); -} - -IrValue& IrBuilder::GetM0() { - throw std::runtime_error("IrBuilder::GetM0 not implemented"); -} - -void IrBuilder::SetM0(IrValue& value) { - throw std::runtime_error("IrBuilder::SetM0 not implemented"); -} - -IrValue& IrBuilder::BitCastF32(IrValue& value) { - throw std::runtime_error("IrBuilder::BitCastF32 not implemented"); -} - -IrValue& IrBuilder::BitCastU32(IrValue& value) { - throw std::runtime_error("IrBuilder::BitCastU32 not implemented"); -} - -IrValue& IrBuilder::BitCastF16(IrValue& value) { - throw std::runtime_error("IrBuilder::BitCastF16 not implemented"); -} - -IrValue& IrBuilder::BitCastU32FromF16(IrValue& value) { - throw std::runtime_error("IrBuilder::BitCastU32FromF16 not implemented"); -} - -IrValue& IrBuilder::ConstructU64(IrValue& low, IrValue& high) { - throw std::runtime_error("IrBuilder::ConstructU64 not implemented"); -} - -IrValue& IrBuilder::CompositeExtract(IrValue& composite, std::uint32_t index) { - throw std::runtime_error("IrBuilder::CompositeExtract not implemented"); -} - -IrValue& IrBuilder::IAdd(IrValue& lhs, IrValue& rhs) { - throw std::runtime_error("IrBuilder::IAdd not implemented"); -} - -IrValue& IrBuilder::ISub(IrValue& lhs, IrValue& rhs) { - throw std::runtime_error("IrBuilder::ISub not implemented"); -} - -IrValue& IrBuilder::IMul(IrValue& lhs, IrValue& rhs) { - throw std::runtime_error("IrBuilder::IMul not implemented"); -} - -IrValue& IrBuilder::ShiftLeftLogical(IrValue& value, IrValue& shift) { - throw std::runtime_error("IrBuilder::ShiftLeftLogical not implemented"); -} - -IrValue& IrBuilder::ShiftRightLogical(IrValue& value, IrValue& shift) { - throw std::runtime_error("IrBuilder::ShiftRightLogical not implemented"); -} - -IrValue& IrBuilder::ShiftRightArithmetic(IrValue& value, IrValue& shift) { - throw std::runtime_error("IrBuilder::ShiftRightArithmetic not implemented"); -} - -IrValue& IrBuilder::BitwiseAnd(IrValue& lhs, IrValue& rhs) { - throw std::runtime_error("IrBuilder::BitwiseAnd not implemented"); -} - -IrValue& IrBuilder::BitwiseOr(IrValue& lhs, IrValue& rhs) { - throw std::runtime_error("IrBuilder::BitwiseOr not implemented"); -} - -IrValue& IrBuilder::BitwiseXor(IrValue& lhs, IrValue& rhs) { - throw std::runtime_error("IrBuilder::BitwiseXor not implemented"); -} - -IrValue& IrBuilder::BitwiseNot(IrValue& value) { - throw std::runtime_error("IrBuilder::BitwiseNot not implemented"); -} - -IrValue& IrBuilder::Select(IrValue& condition, IrValue& trueValue, IrValue& falseValue) { - throw std::runtime_error("IrBuilder::Select not implemented"); -} - -IrValue& IrBuilder::IEqual(IrValue& lhs, IrValue& rhs) { - throw std::runtime_error("IrBuilder::IEqual not implemented"); -} - -IrValue& IrBuilder::INotEqual(IrValue& lhs, IrValue& rhs) { - throw std::runtime_error("IrBuilder::INotEqual not implemented"); -} - -IrValue& IrBuilder::ULessThan(IrValue& lhs, IrValue& rhs) { - throw std::runtime_error("IrBuilder::ULessThan not implemented"); -} - -IrValue& IrBuilder::UGreaterThan(IrValue& lhs, IrValue& rhs) { - throw std::runtime_error("IrBuilder::UGreaterThan not implemented"); -} - -IrValue& IrBuilder::LogicalAnd(IrValue& lhs, IrValue& rhs) { - throw std::runtime_error("IrBuilder::LogicalAnd not implemented"); -} - -IrValue& IrBuilder::LogicalOr(IrValue& lhs, IrValue& rhs) { - throw std::runtime_error("IrBuilder::LogicalOr not implemented"); -} - -IrValue& IrBuilder::LogicalNot(IrValue& value) { - throw std::runtime_error("IrBuilder::LogicalNot not implemented"); -} - -} diff --git a/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderArithmetic.cpp b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderArithmetic.cpp new file mode 100644 index 000000000..250ed9b53 --- /dev/null +++ b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderArithmetic.cpp @@ -0,0 +1,46 @@ +#include "IntermediateRepresentation/IrBuilder.hpp" +#include + +namespace ShaderRecompiler { + +IrValue& IrBuilder::IAdd(IrValue& lhs, IrValue& rhs) { + throw std::runtime_error("IrBuilder::IAdd not implemented"); +} + +IrValue& IrBuilder::ISub(IrValue& lhs, IrValue& rhs) { + throw std::runtime_error("IrBuilder::ISub not implemented"); +} + +IrValue& IrBuilder::IMul(IrValue& lhs, IrValue& rhs) { + throw std::runtime_error("IrBuilder::IMul not implemented"); +} + +IrValue& IrBuilder::ShiftLeftLogical(IrValue& value, IrValue& shift) { + throw std::runtime_error("IrBuilder::ShiftLeftLogical not implemented"); +} + +IrValue& IrBuilder::ShiftRightLogical(IrValue& value, IrValue& shift) { + throw std::runtime_error("IrBuilder::ShiftRightLogical not implemented"); +} + +IrValue& IrBuilder::ShiftRightArithmetic(IrValue& value, IrValue& shift) { + throw std::runtime_error("IrBuilder::ShiftRightArithmetic not implemented"); +} + +IrValue& IrBuilder::BitwiseAnd(IrValue& lhs, IrValue& rhs) { + throw std::runtime_error("IrBuilder::BitwiseAnd not implemented"); +} + +IrValue& IrBuilder::BitwiseOr(IrValue& lhs, IrValue& rhs) { + throw std::runtime_error("IrBuilder::BitwiseOr not implemented"); +} + +IrValue& IrBuilder::BitwiseXor(IrValue& lhs, IrValue& rhs) { + throw std::runtime_error("IrBuilder::BitwiseXor not implemented"); +} + +IrValue& IrBuilder::BitwiseNot(IrValue& value) { + throw std::runtime_error("IrBuilder::BitwiseNot not implemented"); +} + +} diff --git a/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderConstants.cpp b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderConstants.cpp new file mode 100644 index 000000000..30a6447d5 --- /dev/null +++ b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderConstants.cpp @@ -0,0 +1,48 @@ +#include "IntermediateRepresentation/IrBuilder.hpp" +#include "IntermediateRepresentation/IrBuilderInternal.hpp" + +namespace ShaderRecompiler { + +IrValue& IrBuilder::Constant(std::uint32_t value) { + IrValue& constant = createLeafValue(program, IrType::U32); + constant.SetImmediateU32(value); + return constant; +} + +IrValue& IrBuilder::ConstantU64(std::uint64_t value) { + IrValue& constant = createLeafValue(program, IrType::U64); + constant.SetImmediateU64(value); + return constant; +} + +IrValue& IrBuilder::ConstantF16(std::uint16_t bits) { + IrValue& constant = createLeafValue(program, IrType::F16); + constant.SetImmediateF16Bits(bits); + return constant; +} + +IrValue& IrBuilder::ConstantF32(float value) { + IrValue& constant = createLeafValue(program, IrType::F32); + constant.SetImmediateF32(value); + return constant; +} + +IrValue& IrBuilder::ConstantBool(bool value) { + IrValue& constant = createLeafValue(program, IrType::Bool); + constant.SetImmediateBool(value); + return constant; +} + +IrValue& IrBuilder::ConstantU8(std::uint8_t value) { + IrValue& constant = createLeafValue(program, IrType::U8); + constant.SetImmediateU8(value); + return constant; +} + +IrValue& IrBuilder::ConstantU16(std::uint16_t value) { + IrValue& constant = createLeafValue(program, IrType::U16); + constant.SetImmediateU16(value); + return constant; +} + +} diff --git a/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderControlFlow.cpp b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderControlFlow.cpp new file mode 100644 index 000000000..8e0702129 --- /dev/null +++ b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderControlFlow.cpp @@ -0,0 +1,14 @@ +#include "IntermediateRepresentation/IrBuilder.hpp" +#include + +namespace ShaderRecompiler { + +void IrBuilder::Branch(IrBlock& target) { + throw std::runtime_error("IrBuilder::Branch not implemented"); +} + +void IrBuilder::BranchConditional(IrValue& condition, IrBlock& trueTarget, IrBlock& falseTarget) { + throw std::runtime_error("IrBuilder::BranchConditional not implemented"); +} + +} diff --git a/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderConversions.cpp b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderConversions.cpp new file mode 100644 index 000000000..1449520b4 --- /dev/null +++ b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderConversions.cpp @@ -0,0 +1,30 @@ +#include "IntermediateRepresentation/IrBuilder.hpp" +#include + +namespace ShaderRecompiler { + +IrValue& IrBuilder::BitCastF32(IrValue& value) { + throw std::runtime_error("IrBuilder::BitCastF32 not implemented"); +} + +IrValue& IrBuilder::BitCastU32(IrValue& value) { + throw std::runtime_error("IrBuilder::BitCastU32 not implemented"); +} + +IrValue& IrBuilder::BitCastF16(IrValue& value) { + throw std::runtime_error("IrBuilder::BitCastF16 not implemented"); +} + +IrValue& IrBuilder::BitCastU32FromF16(IrValue& value) { + throw std::runtime_error("IrBuilder::BitCastU32FromF16 not implemented"); +} + +IrValue& IrBuilder::ConstructU64(IrValue& low, IrValue& high) { + throw std::runtime_error("IrBuilder::ConstructU64 not implemented"); +} + +IrValue& IrBuilder::CompositeExtract(IrValue& composite, std::uint32_t index) { + throw std::runtime_error("IrBuilder::CompositeExtract not implemented"); +} + +} diff --git a/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderCore.cpp b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderCore.cpp new file mode 100644 index 000000000..db7a8e94c --- /dev/null +++ b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderCore.cpp @@ -0,0 +1,60 @@ +#include "IntermediateRepresentation/IrBuilder.hpp" +#include +#include + +namespace ShaderRecompiler { + +IrBuilder::IrBuilder(IrProgram& program) : program(program), insertionPoint(nullptr) { +} + +void IrBuilder::SetInsertionPoint(IrBlock& block) { + insertionPoint = █ +} + +IrValue& IrBuilder::Emit(IrOpcode opcode, IrType type, std::initializer_list arguments) { + if (insertionPoint == nullptr) { + throw std::runtime_error("IrBuilder::Emit no insertion point set"); + } + if (arguments.size() != IrOpcodeOperandCount(opcode)) { + throw std::invalid_argument("IrBuilder::Emit argument count mismatch for " + std::string(IrOpcodeName(opcode))); + } + IrValue& value = program.CreateValue(opcode, type); + std::size_t index = 0; + for (IrValue* argument : arguments) { + if (argument == nullptr) { + throw std::invalid_argument("IrBuilder::Emit argument cannot be null"); + } + if (argument->Type() != IrOpcodeArgumentType(opcode, index)) { + throw std::invalid_argument("IrBuilder::Emit argument type mismatch at index " + std::to_string(index)); + } + value.AddArgument(argument); + ++index; + } + insertionPoint->AppendInstruction(&value); + return value; +} + +IrValue& IrBuilder::Emit(IrOpcode opcode, IrType type, std::initializer_list arguments, std::uint64_t flags) { + if (insertionPoint == nullptr) { + throw std::runtime_error("IrBuilder::Emit no insertion point set"); + } + if (arguments.size() != IrOpcodeOperandCount(opcode)) { + throw std::invalid_argument("IrBuilder::Emit argument count mismatch for " + std::string(IrOpcodeName(opcode))); + } + IrValue& value = program.CreateValue(opcode, type, flags); + std::size_t index = 0; + for (IrValue* argument : arguments) { + if (argument == nullptr) { + throw std::invalid_argument("IrBuilder::Emit argument cannot be null"); + } + if (argument->Type() != IrOpcodeArgumentType(opcode, index)) { + throw std::invalid_argument("IrBuilder::Emit argument type mismatch at index " + std::to_string(index)); + } + value.AddArgument(argument); + ++index; + } + insertionPoint->AppendInstruction(&value); + return value; +} + +} diff --git a/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderExtendedRegisters.cpp b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderExtendedRegisters.cpp new file mode 100644 index 000000000..1e6113ed2 --- /dev/null +++ b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderExtendedRegisters.cpp @@ -0,0 +1,78 @@ +#include "IntermediateRepresentation/IrBuilder.hpp" +#include + +namespace ShaderRecompiler { + +IrValue& IrBuilder::GetGotoVariable(std::uint32_t id) { + throw std::runtime_error("IrBuilder::GetGotoVariable not implemented"); +} + +void IrBuilder::SetGotoVariable(std::uint32_t id, IrValue& value) { + throw std::runtime_error("IrBuilder::SetGotoVariable not implemented"); +} + +IrValue& IrBuilder::GetScc() { + throw std::runtime_error("IrBuilder::GetScc not implemented"); +} + +void IrBuilder::SetScc(IrValue& value) { + throw std::runtime_error("IrBuilder::SetScc not implemented"); +} + +IrValue& IrBuilder::GetExec() { + throw std::runtime_error("IrBuilder::GetExec not implemented"); +} + +void IrBuilder::SetExec(IrValue& value) { + throw std::runtime_error("IrBuilder::SetExec not implemented"); +} + +IrValue& IrBuilder::GetExecLo() { + throw std::runtime_error("IrBuilder::GetExecLo not implemented"); +} + +void IrBuilder::SetExecLo(IrValue& value) { + throw std::runtime_error("IrBuilder::SetExecLo not implemented"); +} + +IrValue& IrBuilder::GetExecHi() { + throw std::runtime_error("IrBuilder::GetExecHi not implemented"); +} + +void IrBuilder::SetExecHi(IrValue& value) { + throw std::runtime_error("IrBuilder::SetExecHi not implemented"); +} + +IrValue& IrBuilder::GetVcc() { + throw std::runtime_error("IrBuilder::GetVcc not implemented"); +} + +void IrBuilder::SetVcc(IrValue& value) { + throw std::runtime_error("IrBuilder::SetVcc not implemented"); +} + +IrValue& IrBuilder::GetVccLo() { + throw std::runtime_error("IrBuilder::GetVccLo not implemented"); +} + +void IrBuilder::SetVccLo(IrValue& value) { + throw std::runtime_error("IrBuilder::SetVccLo not implemented"); +} + +IrValue& IrBuilder::GetVccHi() { + throw std::runtime_error("IrBuilder::GetVccHi not implemented"); +} + +void IrBuilder::SetVccHi(IrValue& value) { + throw std::runtime_error("IrBuilder::SetVccHi not implemented"); +} + +IrValue& IrBuilder::GetM0() { + throw std::runtime_error("IrBuilder::GetM0 not implemented"); +} + +void IrBuilder::SetM0(IrValue& value) { + throw std::runtime_error("IrBuilder::SetM0 not implemented"); +} + +} diff --git a/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderInternal.cpp b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderInternal.cpp new file mode 100644 index 000000000..26161bbec --- /dev/null +++ b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderInternal.cpp @@ -0,0 +1,15 @@ +#include "IntermediateRepresentation/IrBuilderInternal.hpp" + +namespace ShaderRecompiler { + +IrValue& createLeafValue(IrProgram& program, IrType type) { + return program.CreateValue(IrOpcode::Void, type); +} + +IrValue& createRegisterOperand(IrProgram& program, RegisterBank bank, std::uint32_t index, IrType type) { + IrValue& operand = createLeafValue(program, type); + operand.SetRegister(GuestRegister{bank, index}); + return operand; +} + +} diff --git a/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderLogic.cpp b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderLogic.cpp new file mode 100644 index 000000000..7b6e5fc4b --- /dev/null +++ b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderLogic.cpp @@ -0,0 +1,38 @@ +#include "IntermediateRepresentation/IrBuilder.hpp" +#include + +namespace ShaderRecompiler { + +IrValue& IrBuilder::Select(IrValue& condition, IrValue& trueValue, IrValue& falseValue) { + throw std::runtime_error("IrBuilder::Select not implemented"); +} + +IrValue& IrBuilder::IEqual(IrValue& lhs, IrValue& rhs) { + throw std::runtime_error("IrBuilder::IEqual not implemented"); +} + +IrValue& IrBuilder::INotEqual(IrValue& lhs, IrValue& rhs) { + throw std::runtime_error("IrBuilder::INotEqual not implemented"); +} + +IrValue& IrBuilder::ULessThan(IrValue& lhs, IrValue& rhs) { + throw std::runtime_error("IrBuilder::ULessThan not implemented"); +} + +IrValue& IrBuilder::UGreaterThan(IrValue& lhs, IrValue& rhs) { + throw std::runtime_error("IrBuilder::UGreaterThan not implemented"); +} + +IrValue& IrBuilder::LogicalAnd(IrValue& lhs, IrValue& rhs) { + throw std::runtime_error("IrBuilder::LogicalAnd not implemented"); +} + +IrValue& IrBuilder::LogicalOr(IrValue& lhs, IrValue& rhs) { + throw std::runtime_error("IrBuilder::LogicalOr not implemented"); +} + +IrValue& IrBuilder::LogicalNot(IrValue& value) { + throw std::runtime_error("IrBuilder::LogicalNot not implemented"); +} + +} diff --git a/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderRegisters.cpp b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderRegisters.cpp new file mode 100644 index 000000000..2a81dd39b --- /dev/null +++ b/core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderRegisters.cpp @@ -0,0 +1,119 @@ +#include "IntermediateRepresentation/IrBuilder.hpp" +#include "IntermediateRepresentation/IrBuilderInternal.hpp" +#include + +namespace ShaderRecompiler { + +IrValue& IrBuilder::ReadRegister(const GuestRegister& reg) { + switch (reg.bank) { + case RegisterBank::Scalar: + return GetScalarReg(static_cast(reg.index)); + case RegisterBank::Vector: + return GetVectorReg(static_cast(reg.index)); + case RegisterBank::VectorConditionCode: + return GetVcc(); + case RegisterBank::ScalarConditionCode: + return GetScc(); + case RegisterBank::ExecutionMask: + return GetExec(); + case RegisterBank::Memory: + throw std::invalid_argument("IrBuilder::ReadRegister memory bank has no register representation"); + case RegisterBank::ThreadBitScalar: + return GetThreadBitScalarReg(static_cast(reg.index)); + case RegisterBank::ScalarMaskTag: + return GetScalarMaskTag(static_cast(reg.index)); + case RegisterBank::GotoVariable: + return GetGotoVariable(reg.index); + case RegisterBank::M0: + return GetM0(); + case RegisterBank::UserData: + return GetUserData(static_cast(reg.index)); + default: + throw std::invalid_argument("IrBuilder::ReadRegister unknown register bank"); + } +} + +void IrBuilder::WriteRegister(const GuestRegister& reg, IrValue& value) { + switch (reg.bank) { + case RegisterBank::Scalar: + SetScalarReg(static_cast(reg.index), value); + return; + case RegisterBank::Vector: + SetVectorReg(static_cast(reg.index), value); + return; + case RegisterBank::VectorConditionCode: + SetVcc(value); + return; + case RegisterBank::ScalarConditionCode: + SetScc(value); + return; + case RegisterBank::ExecutionMask: + SetExec(value); + return; + case RegisterBank::Memory: + throw std::invalid_argument("IrBuilder::WriteRegister memory bank has no register representation"); + case RegisterBank::ThreadBitScalar: + SetThreadBitScalarReg(static_cast(reg.index), value); + return; + case RegisterBank::ScalarMaskTag: + SetScalarMaskTag(static_cast(reg.index), value); + return; + case RegisterBank::GotoVariable: + SetGotoVariable(reg.index, value); + return; + case RegisterBank::M0: + SetM0(value); + return; + case RegisterBank::UserData: + throw std::invalid_argument("IrBuilder::WriteRegister user data has no setter"); + default: + throw std::invalid_argument("IrBuilder::WriteRegister unknown register bank"); + } +} + +IrValue& IrBuilder::GetUserData(ScalarReg reg) { + IrValue& operand = createRegisterOperand(program, RegisterBank::UserData, RegIndex(reg), IrType::ScalarReg); + return Emit(IrOpcode::GetUserData, IrType::U32, {&operand}); +} + +IrValue& IrBuilder::GetScalarReg(ScalarReg reg) { + IrValue& operand = createRegisterOperand(program, RegisterBank::Scalar, RegIndex(reg), IrType::ScalarReg); + return Emit(IrOpcode::GetScalarRegister, IrType::U32, {&operand}); +} + +void IrBuilder::SetScalarReg(ScalarReg reg, IrValue& value) { + IrValue& operand = createRegisterOperand(program, RegisterBank::Scalar, RegIndex(reg), IrType::ScalarReg); + (void)Emit(IrOpcode::SetScalarRegister, IrType::Void, {&operand, &value}); +} + +IrValue& IrBuilder::GetThreadBitScalarReg(ScalarReg reg) { + IrValue& operand = createRegisterOperand(program, RegisterBank::ThreadBitScalar, RegIndex(reg), IrType::ScalarReg); + return Emit(IrOpcode::GetThreadBitScalarRegister, IrType::Bool, {&operand}); +} + +void IrBuilder::SetThreadBitScalarReg(ScalarReg reg, IrValue& value) { + IrValue& operand = createRegisterOperand(program, RegisterBank::ThreadBitScalar, RegIndex(reg), IrType::ScalarReg); + (void)Emit(IrOpcode::SetThreadBitScalarRegister, IrType::Void, {&operand, &value}); +} + +IrValue& IrBuilder::GetScalarMaskTag(ScalarReg reg) { + IrValue& operand = createRegisterOperand(program, RegisterBank::ScalarMaskTag, RegIndex(reg), IrType::ScalarReg); + return Emit(IrOpcode::GetScalarMaskTag, IrType::Bool, {&operand}); +} + +void IrBuilder::SetScalarMaskTag(ScalarReg reg, IrValue& value) { + IrValue& operand = createRegisterOperand(program, RegisterBank::ScalarMaskTag, RegIndex(reg), IrType::ScalarReg); + (void)Emit(IrOpcode::SetScalarMaskTag, IrType::Void, {&operand, &value}); +} + +IrValue& IrBuilder::GetVectorReg(VectorReg reg) { + IrValue& operand = createRegisterOperand(program, RegisterBank::Vector, RegIndex(reg), IrType::VectorReg); + return Emit(IrOpcode::GetVectorRegister, IrType::U32, {&operand}); +} + +void IrBuilder::SetVectorReg(VectorReg reg, IrValue& value) { + IrValue& operand = createRegisterOperand(program, RegisterBank::Vector, RegIndex(reg), IrType::VectorReg); + (void)Emit(IrOpcode::SetVectorRegister, IrType::Void, {&operand, &value}); +} + +}