feat(shader): add itermediate primitives impl

This commit is contained in:
boykopovar
2026-09-15 10:24:34 +03:00
parent af2ff821c4
commit af5a8c6235
4 changed files with 311 additions and 76 deletions
@@ -4,18 +4,30 @@
namespace ShaderRecompiler {
bool operator==(const GuestRegister& lhs, const GuestRegister& rhs) {
throw std::runtime_error("operator== not implemented");
return lhs.bank == rhs.bank && lhs.index == rhs.index;
}
bool operator<(const GuestRegister& lhs, const GuestRegister& rhs) {
throw std::runtime_error("operator< not implemented");
if (lhs.bank != rhs.bank) {
return lhs.bank < rhs.bank;
}
return lhs.index < rhs.index;
}
std::uint32_t RegIndex(ScalarReg reg) {
throw std::runtime_error("RegIndex not implemented");
const auto index = static_cast<std::uint32_t>(reg);
if (index >= NumScalarRegs) {
throw std::out_of_range("ScalarReg index is out of range");
}
return index;
}
std::uint32_t RegIndex(VectorReg reg) {
throw std::runtime_error("RegIndex not implemented");
const auto index = static_cast<std::uint32_t>(reg);
if (index >= NumVectorRegs) {
throw std::out_of_range("VectorReg index is out of range");
}
return index;
}
}
@@ -1,64 +1,118 @@
#include "IntermediateRepresentation/IrBlock.hpp"
#include <algorithm>
#include <stdexcept>
namespace ShaderRecompiler {
IrBlock::IrBlock(std::uint32_t id) : id(id) {
throw std::runtime_error("IrBlock::IrBlock not implemented");
}
std::uint32_t IrBlock::Id() const {
throw std::runtime_error("IrBlock::Id not implemented");
return id;
}
std::list<IrValue*>& IrBlock::Instructions() {
throw std::runtime_error("IrBlock::Instructions not implemented");
return instructions;
}
const std::list<IrValue*>& IrBlock::Instructions() const {
throw std::runtime_error("IrBlock::Instructions not implemented");
return instructions;
}
std::vector<IrBlock*>& IrBlock::Predecessors() {
throw std::runtime_error("IrBlock::Predecessors not implemented");
return predecessors;
}
std::vector<IrBlock*>& IrBlock::Successors() {
throw std::runtime_error("IrBlock::Successors not implemented");
return successors;
}
void IrBlock::AppendInstruction(IrValue* value) {
throw std::runtime_error("IrBlock::AppendInstruction not implemented");
if (value == nullptr) {
throw std::invalid_argument("IrBlock::AppendInstruction value cannot be null");
}
if (value->Parent() != nullptr) {
throw std::runtime_error("IrBlock::AppendInstruction value already belongs to a block");
}
instructions.push_back(value);
value->SetParent(this);
}
void IrBlock::InsertInstructionBefore(IrValue* position, IrValue* value) {
throw std::runtime_error("IrBlock::InsertInstructionBefore not implemented");
if (value == nullptr) {
throw std::invalid_argument("IrBlock::InsertInstructionBefore value cannot be null");
}
if (value->Parent() != nullptr) {
throw std::runtime_error("IrBlock::InsertInstructionBefore value already belongs to a block");
}
if (position == nullptr) {
instructions.push_front(value);
value->SetParent(this);
return;
}
const auto it = std::find(instructions.begin(), instructions.end(), position);
if (it == instructions.end()) {
throw std::runtime_error("IrBlock::InsertInstructionBefore position is not in this block");
}
instructions.insert(it, value);
value->SetParent(this);
}
void IrBlock::RemoveInstruction(IrValue* value) {
throw std::runtime_error("IrBlock::RemoveInstruction not implemented");
if (value == nullptr) {
throw std::invalid_argument("IrBlock::RemoveInstruction value cannot be null");
}
const auto it = std::find(instructions.begin(), instructions.end(), value);
if (it == instructions.end()) {
throw std::runtime_error("IrBlock::RemoveInstruction value is not in this block");
}
instructions.erase(it);
value->SetParent(nullptr);
}
void IrBlock::AddPredecessor(IrBlock* block) {
throw std::runtime_error("IrBlock::AddPredecessor not implemented");
if (block == nullptr) {
throw std::invalid_argument("IrBlock::AddPredecessor block cannot be null");
}
predecessors.push_back(block);
}
void IrBlock::AddSuccessor(IrBlock* block) {
throw std::runtime_error("IrBlock::AddSuccessor not implemented");
if (block == nullptr) {
throw std::invalid_argument("IrBlock::AddSuccessor block cannot be null");
}
successors.push_back(block);
}
const std::vector<IrBlock*>& IrBlock::Predecessors() const {
throw std::runtime_error("IrBlock::Predecessors not implemented");
return predecessors;
}
const std::vector<IrBlock*>& IrBlock::Successors() const {
throw std::runtime_error("IrBlock::Successors not implemented");
return successors;
}
void IrBlock::SsaSeal() {
throw std::runtime_error("IrBlock::SsaSeal not implemented");
if (ssaSealed) {
throw std::runtime_error("IrBlock::SsaSeal called on an already sealed block");
}
ssaSealed = true;
}
bool IrBlock::IsSsaSealed() const {
throw std::runtime_error("IrBlock::IsSsaSealed not implemented");
return ssaSealed;
}
void IrBlock::AddBranch(IrBlock* block) {
throw std::runtime_error("IrBlock::AddBranch not implemented");
if (block == nullptr) {
throw std::invalid_argument("IrBlock::AddBranch block cannot be null");
}
AddSuccessor(block);
block->AddPredecessor(this);
}
bool IrBlock::Empty() const {
throw std::runtime_error("IrBlock::Empty not implemented");
return instructions.empty();
}
}
@@ -1,38 +1,96 @@
#include "IntermediateRepresentation/IrType.hpp"
#include <array>
#include <stdexcept>
namespace ShaderRecompiler {
namespace {
constexpr std::uint32_t FloatTypeMask = static_cast<std::uint32_t>(IrType::F16) | static_cast<std::uint32_t>(IrType::F32) | static_cast<std::uint32_t>(IrType::F64) | static_cast<std::uint32_t>(IrType::Vec2F32) | static_cast<std::uint32_t>(IrType::Vec3F32) | static_cast<std::uint32_t>(IrType::Vec4F32);
constexpr std::uint32_t IntegerTypeMask = static_cast<std::uint32_t>(IrType::Bool) | static_cast<std::uint32_t>(IrType::U8) | static_cast<std::uint32_t>(IrType::U16) | static_cast<std::uint32_t>(IrType::U32) | static_cast<std::uint32_t>(IrType::S32) | static_cast<std::uint32_t>(IrType::U64) | static_cast<std::uint32_t>(IrType::S64) | static_cast<std::uint32_t>(IrType::Vec2U32) | static_cast<std::uint32_t>(IrType::Vec3U32) | static_cast<std::uint32_t>(IrType::Vec4U32);
constexpr std::uint32_t VectorTypeMask = static_cast<std::uint32_t>(IrType::Vec2U32) | static_cast<std::uint32_t>(IrType::Vec3U32) | static_cast<std::uint32_t>(IrType::Vec4U32) | static_cast<std::uint32_t>(IrType::Vec2F32) | static_cast<std::uint32_t>(IrType::Vec3F32) | static_cast<std::uint32_t>(IrType::Vec4F32);
}
bool IsFloatType(IrType type) {
throw std::runtime_error("IsFloatType not implemented");
return (static_cast<std::uint32_t>(type) & FloatTypeMask) != 0u;
}
bool IsIntegerType(IrType type) {
throw std::runtime_error("IsIntegerType not implemented");
return (static_cast<std::uint32_t>(type) & IntegerTypeMask) != 0u;
}
bool IsVectorType(IrType type) {
throw std::runtime_error("IsVectorType not implemented");
return (static_cast<std::uint32_t>(type) & VectorTypeMask) != 0u;
}
std::uint32_t ComponentCount(IrType type) {
throw std::runtime_error("ComponentCount not implemented");
switch (type) {
case IrType::Bool:
case IrType::U8:
case IrType::U16:
case IrType::U32:
case IrType::S32:
case IrType::F16:
case IrType::F32:
case IrType::U64:
case IrType::S64:
case IrType::F64:
return 1u;
case IrType::Vec2U32:
case IrType::Vec2F32:
return 2u;
case IrType::Vec3U32:
case IrType::Vec3F32:
return 3u;
case IrType::Vec4U32:
case IrType::Vec4F32:
return 4u;
default:
throw std::runtime_error("ComponentCount does not accept this type");
}
}
std::string TypeToString(IrType type) {
throw std::runtime_error("TypeToString not implemented");
static constexpr std::array<const char*, 26> names = {
"Bool", "U32", "S32", "F32", "U64", "S64", "F64", "Vec2U32", "Vec3U32", "Vec4U32",
"Vec2F32", "Vec3F32", "Vec4F32", "Label", "Opaque", "ScalarReg", "VectorReg", "U8",
"U16", "F16", "SrtResource", "BufferResource", "AddressResource", "ImageResource",
"SamplerResource", "ImageAddress"
};
const auto bits = static_cast<std::uint32_t>(type);
if (bits == 0u) {
return "Void";
}
std::string result;
for (std::uint32_t index = 0; index < names.size(); index++) {
if ((bits & (1u << index)) == 0u) {
continue;
}
if (!result.empty()) {
result += '|';
}
result += names[index];
}
return result;
}
IrType operator|(IrType lhs, IrType rhs) {
throw std::runtime_error("operator| not implemented");
return static_cast<IrType>(static_cast<std::uint32_t>(lhs) | static_cast<std::uint32_t>(rhs));
}
IrType operator&(IrType lhs, IrType rhs) {
throw std::runtime_error("operator& not implemented");
return static_cast<IrType>(static_cast<std::uint32_t>(lhs) & static_cast<std::uint32_t>(rhs));
}
bool TypesOverlap(IrType lhs, IrType rhs) {
throw std::runtime_error("TypesOverlap not implemented");
return (lhs & rhs) != IrType::Void;
}
bool AreTypesCompatible(IrType lhs, IrType rhs) {
throw std::runtime_error("AreTypesCompatible not implemented");
return lhs == rhs || lhs == IrType::Opaque || rhs == IrType::Opaque || TypesOverlap(lhs, rhs);
}
}
@@ -1,177 +1,288 @@
#include "IntermediateRepresentation/IrValue.hpp"
#include <algorithm>
#include <bit>
#include <stdexcept>
namespace ShaderRecompiler {
IrValue::IrValue(IrOpcode opcode, IrType type, std::uint32_t id) : opcode(opcode), type(type), id(id), hasImmediate(false), parent(nullptr) {
throw std::runtime_error("IrValue::IrValue not implemented");
}
IrOpcode IrValue::Opcode() const {
throw std::runtime_error("IrValue::Opcode not implemented");
return opcode;
}
IrType IrValue::Type() const {
throw std::runtime_error("IrValue::Type not implemented");
return type;
}
std::uint32_t IrValue::Id() const {
throw std::runtime_error("IrValue::Id not implemented");
return id;
}
const std::vector<IrValue*>& IrValue::Arguments() const {
throw std::runtime_error("IrValue::Arguments not implemented");
return arguments;
}
const std::vector<IrValue*>& IrValue::Uses() const {
throw std::runtime_error("IrValue::Uses not implemented");
return uses;
}
IrBlock* IrValue::Parent() const {
throw std::runtime_error("IrValue::Parent not implemented");
return parent;
}
bool IrValue::HasImmediate() const {
throw std::runtime_error("IrValue::HasImmediate not implemented");
return hasImmediate;
}
std::uint32_t IrValue::ImmediateU32() const {
throw std::runtime_error("IrValue::ImmediateU32 not implemented");
return static_cast<std::uint32_t>(immediateBits);
}
void IrValue::AddArgument(IrValue* argument) {
throw std::runtime_error("IrValue::AddArgument not implemented");
arguments.push_back(argument);
if (argument == nullptr) {
return;
}
argument->uses.push_back(this);
argument->operandUses.push_back(IrUse{this, arguments.size() - 1});
}
void IrValue::ReplaceArgument(std::size_t index, IrValue* argument) {
throw std::runtime_error("IrValue::ReplaceArgument not implemented");
if (index >= arguments.size()) {
throw std::out_of_range("IrValue::ReplaceArgument index is out of range");
}
IrValue* previous = arguments[index];
if (previous != nullptr) {
const auto useIt = std::find(previous->uses.begin(), previous->uses.end(), this);
if (useIt != previous->uses.end()) {
previous->uses.erase(useIt);
}
const auto operandIt = std::find_if(previous->operandUses.begin(), previous->operandUses.end(), [this, index](const IrUse& use) {
return use.user == this && use.operand == index;
});
if (operandIt != previous->operandUses.end()) {
previous->operandUses.erase(operandIt);
}
}
arguments[index] = argument;
if (argument != nullptr) {
argument->uses.push_back(this);
argument->operandUses.push_back(IrUse{this, index});
}
}
void IrValue::ReplaceAllUsesWith(IrValue* replacement) {
throw std::runtime_error("IrValue::ReplaceAllUsesWith not implemented");
if (replacement == this) {
return;
}
while (!operandUses.empty()) {
const IrUse use = operandUses.back();
use.user->ReplaceArgument(use.operand, replacement);
}
}
void IrValue::SetImmediateU32(std::uint32_t value) {
throw std::runtime_error("IrValue::SetImmediateU32 not implemented");
immediateBits = value;
hasImmediate = true;
}
void IrValue::SetParent(IrBlock* parent) {
throw std::runtime_error("IrValue::SetParent not implemented");
this->parent = parent;
}
bool IrValue::IsEmpty() const {
throw std::runtime_error("IrValue::IsEmpty not implemented");
return opcode == IrOpcode::Void;
}
bool IrValue::IsIdentity() const {
throw std::runtime_error("IrValue::IsIdentity not implemented");
return opcode == IrOpcode::Identity;
}
bool IrValue::IsPhi() const {
throw std::runtime_error("IrValue::IsPhi not implemented");
return opcode == IrOpcode::Phi;
}
IrValue* IrValue::Resolve() const {
throw std::runtime_error("IrValue::Resolve not implemented");
const IrValue* current = this;
while (current->IsIdentity()) {
if (current->arguments.empty() || current->arguments.front() == nullptr) {
throw std::runtime_error("IrValue::Resolve encountered a malformed Identity value");
}
current = current->arguments.front();
}
return const_cast<IrValue*>(current);
}
bool IrValue::MayHaveSideEffects() const {
throw std::runtime_error("IrValue::MayHaveSideEffects not implemented");
return IrOpcodeHasSideEffects(opcode);
}
bool IrValue::HasUses() const {
throw std::runtime_error("IrValue::HasUses not implemented");
return !uses.empty();
}
std::size_t IrValue::UseCount() const {
throw std::runtime_error("IrValue::UseCount not implemented");
return uses.size();
}
std::size_t IrValue::ArgumentCount() const {
throw std::runtime_error("IrValue::ArgumentCount not implemented");
return arguments.size();
}
std::size_t IrValue::PhiBlockCount() const {
throw std::runtime_error("IrValue::PhiBlockCount not implemented");
return phiBlocks.size();
}
IrValue* IrValue::Argument(std::size_t index) const {
throw std::runtime_error("IrValue::Argument not implemented");
if (index >= arguments.size()) {
throw std::out_of_range("IrValue::Argument index is out of range");
}
return arguments[index];
}
IrBlock* IrValue::PhiBlock(std::size_t index) const {
throw std::runtime_error("IrValue::PhiBlock not implemented");
if (index >= phiBlocks.size()) {
throw std::out_of_range("IrValue::PhiBlock index is out of range");
}
return phiBlocks[index];
}
const std::vector<IrUse>& IrValue::OperandUses() const {
throw std::runtime_error("IrValue::OperandUses not implemented");
return operandUses;
}
std::uint64_t IrValue::ImmediateU64() const {
throw std::runtime_error("IrValue::ImmediateU64 not implemented");
return immediateBits;
}
std::uint16_t IrValue::ImmediateF16Bits() const {
throw std::runtime_error("IrValue::ImmediateF16Bits not implemented");
return static_cast<std::uint16_t>(immediateBits);
}
float IrValue::ImmediateF32() const {
throw std::runtime_error("IrValue::ImmediateF32 not implemented");
return std::bit_cast<float>(static_cast<std::uint32_t>(immediateBits));
}
bool IrValue::ImmediateBool() const {
throw std::runtime_error("IrValue::ImmediateBool not implemented");
return immediateBits != 0u;
}
std::uint8_t IrValue::ImmediateU8() const {
throw std::runtime_error("IrValue::ImmediateU8 not implemented");
return static_cast<std::uint8_t>(immediateBits);
}
std::uint16_t IrValue::ImmediateU16() const {
throw std::runtime_error("IrValue::ImmediateU16 not implemented");
return static_cast<std::uint16_t>(immediateBits);
}
GuestRegister IrValue::Register() const {
throw std::runtime_error("IrValue::Register not implemented");
return registerValue;
}
void IrValue::SetImmediateU64(std::uint64_t value) {
throw std::runtime_error("IrValue::SetImmediateU64 not implemented");
immediateBits = value;
hasImmediate = true;
}
void IrValue::SetImmediateF16Bits(std::uint16_t bits) {
throw std::runtime_error("IrValue::SetImmediateF16Bits not implemented");
immediateBits = bits;
hasImmediate = true;
}
void IrValue::SetImmediateF32(float value) {
throw std::runtime_error("IrValue::SetImmediateF32 not implemented");
immediateBits = std::bit_cast<std::uint32_t>(value);
hasImmediate = true;
}
void IrValue::SetImmediateBool(bool value) {
throw std::runtime_error("IrValue::SetImmediateBool not implemented");
immediateBits = value ? 1u : 0u;
hasImmediate = true;
}
void IrValue::SetImmediateU8(std::uint8_t value) {
throw std::runtime_error("IrValue::SetImmediateU8 not implemented");
immediateBits = value;
hasImmediate = true;
}
void IrValue::SetImmediateU16(std::uint16_t value) {
throw std::runtime_error("IrValue::SetImmediateU16 not implemented");
immediateBits = value;
hasImmediate = true;
}
void IrValue::SetRegister(const GuestRegister& reg) {
throw std::runtime_error("IrValue::SetRegister not implemented");
registerValue = reg;
}
void IrValue::AddPhiOperand(IrBlock* predecessor, IrValue* value) {
throw std::runtime_error("IrValue::AddPhiOperand not implemented");
if (!IsPhi()) {
throw std::runtime_error("IrValue::AddPhiOperand called on a non-Phi value");
}
if (predecessor == nullptr) {
throw std::invalid_argument("IrValue::AddPhiOperand predecessor cannot be null");
}
phiBlocks.push_back(predecessor);
AddArgument(value);
}
void IrValue::ReplaceOpcode(IrOpcode opcode) {
throw std::runtime_error("IrValue::ReplaceOpcode not implemented");
this->opcode = opcode;
}
void IrValue::Invalidate() {
throw std::runtime_error("IrValue::Invalidate not implemented");
if (!uses.empty()) {
throw std::runtime_error("IrValue::Invalidate called on a value that still has uses");
}
for (std::size_t index = 0; index < arguments.size(); index++) {
ReplaceArgument(index, nullptr);
}
opcode = IrOpcode::Unreachable;
}
void IrValue::ReplaceUsesWith(IrValue* replacement, bool preserve) {
throw std::runtime_error("IrValue::ReplaceUsesWith not implemented");
if (replacement == this) {
return;
}
if (preserve) {
for (std::size_t index = 0; index < arguments.size(); index++) {
ReplaceArgument(index, nullptr);
}
arguments.clear();
opcode = IrOpcode::Identity;
AddArgument(replacement);
return;
}
ReplaceAllUsesWith(replacement);
Invalidate();
}
bool IrValue::operator==(const IrValue& other) const {
throw std::runtime_error("operator== not implemented");
if (this == &other) {
return true;
}
if (opcode != other.opcode || type != other.type || hasImmediate != other.hasImmediate) {
return false;
}
if (hasImmediate && immediateBits != other.immediateBits) {
return false;
}
if (!(registerValue == other.registerValue)) {
return false;
}
if (arguments.size() != other.arguments.size()) {
return false;
}
for (std::size_t index = 0; index < arguments.size(); index++) {
if (arguments[index] != other.arguments[index]) {
return false;
}
}
return true;
}
bool IrUse::operator==(const IrUse& other) const {
throw std::runtime_error("IrUse::operator== not implemented");
return user == other.user && operand == other.operand;
}
}