Triforce v0.2.0

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ruckus7382sfgsgdf
2026-11-10 19:32:48 -06:00
commit 34b3683441
1730 changed files with 554853 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,44 @@
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "shader_recompiler/frontend/ir/program.h"
#include "shader_recompiler/ir_opt/passes.h"
namespace Shader::Optimization {
void ConditionalBarrierPass(IR::Program& program) {
s32 conditional_control_flow_count{0};
s32 conditional_return_count{0};
for (IR::AbstractSyntaxNode& node : program.syntax_list) {
switch (node.type) {
case IR::AbstractSyntaxNode::Type::If:
case IR::AbstractSyntaxNode::Type::Loop:
conditional_control_flow_count++;
break;
case IR::AbstractSyntaxNode::Type::EndIf:
case IR::AbstractSyntaxNode::Type::Repeat:
conditional_control_flow_count--;
break;
case IR::AbstractSyntaxNode::Type::Unreachable:
case IR::AbstractSyntaxNode::Type::Return:
if (conditional_control_flow_count > 0) {
conditional_return_count++;
}
break;
case IR::AbstractSyntaxNode::Type::Block:
for (IR::Inst& inst : node.data.block->Instructions()) {
if ((conditional_control_flow_count > 0 || conditional_return_count > 0) &&
inst.GetOpcode() == IR::Opcode::Barrier) {
LOG_WARNING(Shader, "Barrier within conditional control flow");
inst.ReplaceOpcode(IR::Opcode::Identity);
}
}
break;
default:
break;
}
}
ASSERT(conditional_control_flow_count == 0);
}
} // namespace Shader::Optimization

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,25 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "shader_recompiler/frontend/ir/basic_block.h"
#include "shader_recompiler/frontend/ir/value.h"
#include "shader_recompiler/ir_opt/passes.h"
namespace Shader::Optimization {
void DeadCodeEliminationPass(IR::Program& program) {
// We iterate over the instructions in reverse order.
// This is because removing an instruction reduces the number of uses for earlier instructions.
for (IR::Block* const block : program.post_order_blocks) {
auto it{block->end()};
while (it != block->begin()) {
--it;
if (!it->HasUses() && !it->MayHaveSideEffects()) {
it->Invalidate();
it = block->Instructions().erase(it);
}
}
}
}
} // namespace Shader::Optimization

View File

@@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "shader_recompiler/frontend/ir/ir_emitter.h"
#include "shader_recompiler/ir_opt/passes.h"
namespace Shader::Optimization {
void VertexATransformPass(IR::Program& program) {
for (IR::Block* const block : program.blocks) {
for (IR::Inst& inst : block->Instructions()) {
if (inst.GetOpcode() == IR::Opcode::Epilogue) {
return inst.Invalidate();
}
}
}
}
void VertexBTransformPass(IR::Program& program) {
for (IR::Block* const block : program.blocks) {
for (IR::Inst& inst : block->Instructions()) {
if (inst.GetOpcode() == IR::Opcode::Prologue) {
return inst.Invalidate();
}
}
}
}
} // namespace Shader::Optimization

View File

@@ -0,0 +1,129 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include <boost/container/small_vector.hpp>
#include "shader_recompiler/frontend/ir/ir_emitter.h"
#include "shader_recompiler/host_translate_info.h"
#include "shader_recompiler/ir_opt/passes.h"
namespace Shader::Optimization {
namespace {
constexpr int MAX_BALLOT_DEPTH = 8;
struct SlotAtomic {
IR::Block* block;
IR::Inst* atomic;
IR::Inst* ballot;
};
IR::Inst* FindBallot(const IR::Value& value, int depth) {
if (depth > MAX_BALLOT_DEPTH || value.IsImmediate()) {
return nullptr;
}
IR::Inst* const inst{value.InstRecursive()};
if (inst->GetOpcode() == IR::Opcode::SubgroupBallot) {
return inst;
}
if (inst->GetOpcode() == IR::Opcode::Phi) {
return nullptr;
}
for (size_t index = 0; index < inst->NumArgs(); ++index) {
if (IR::Inst* const ballot{FindBallot(inst->Arg(index), depth + 1)}) {
return ballot;
}
}
return nullptr;
}
bool IsShuffle(IR::Opcode opcode) {
switch (opcode) {
case IR::Opcode::ShuffleIndex:
case IR::Opcode::ShuffleUp:
case IR::Opcode::ShuffleDown:
case IR::Opcode::ShuffleButterfly:
return true;
default:
return false;
}
}
IR::U32 PrimitiveSlot(IR::IREmitter& ir, u32 invocations, const IR::U32& amount) {
IR::U32 key{ir.GetAttributeU32(IR::Attribute::PrimitiveId)};
if (invocations > 1) {
key = ir.IAdd(ir.IMul(key, ir.Imm32(invocations)), ir.InvocationId());
}
return ir.IMul(key, amount);
}
bool MergesAtomic(const IR::Inst& phi, const IR::Block& block, const IR::Inst& atomic) {
for (size_t index = 0; index < phi.NumArgs(); ++index) {
const IR::Value arg{phi.Arg(index)};
if (phi.PhiBlock(index) == &block && !arg.IsImmediate() &&
arg.InstRecursive() == &atomic) {
return true;
}
}
return false;
}
void RewriteMergedSlots(IR::Block& block, IR::Inst& atomic, u32 invocations) {
for (IR::Block* const successor : block.ImmSuccessors()) {
for (IR::Inst& phi : successor->Instructions()) {
if (phi.GetOpcode() != IR::Opcode::Phi || !MergesAtomic(phi, block, atomic)) {
continue;
}
for (size_t index = 0; index < phi.NumArgs(); ++index) {
const IR::Value arg{phi.Arg(index)};
if (arg.IsImmediate() || !IsShuffle(arg.InstRecursive()->GetOpcode())) {
continue;
}
IR::IREmitter ir{*phi.PhiBlock(index)};
const IR::U32 amount{arg.InstRecursive()->Arg(0)};
phi.SetArg(index, PrimitiveSlot(ir, invocations, amount));
}
}
}
}
void RewriteAtomic(IR::Block& block, IR::Inst& atomic, u32 invocations) {
const auto insert_point{IR::Block::InstructionList::s_iterator_to(atomic)};
IR::IREmitter ir{block, insert_point};
const IR::U32 amount{atomic.Arg(2)};
const IR::U32 slot{PrimitiveSlot(ir, invocations, amount)};
block.PrependNewInst(insert_point, IR::Opcode::StorageAtomicUMax32,
{atomic.Arg(0), atomic.Arg(1), ir.IAdd(slot, amount)});
atomic.ReplaceUsesWith(slot);
}
void NeutralizePredicate(IR::Inst& ballot) {
const IR::Value pred{ballot.Arg(0)};
if (!pred.IsImmediate()) {
pred.InstRecursive()->ReplaceUsesWith(IR::Value{true});
}
}
}
void GeometryCompactionPass(IR::Program& program, const HostTranslateInfo& host_info) {
if (program.stage != Stage::Geometry || !host_info.single_lane_geometry_subgroups) {
return;
}
boost::container::small_vector<SlotAtomic, 4> slot_atomics;
for (IR::Block* const block : program.post_order_blocks) {
for (IR::Inst& inst : block->Instructions()) {
if (inst.GetOpcode() != IR::Opcode::StorageAtomicIAdd32) {
continue;
}
if (IR::Inst* const ballot{FindBallot(inst.Arg(2), 0)}) {
slot_atomics.push_back({block, &inst, ballot});
}
}
}
for (const SlotAtomic& slot_atomic : slot_atomics) {
RewriteMergedSlots(*slot_atomic.block, *slot_atomic.atomic, program.invocations);
RewriteAtomic(*slot_atomic.block, *slot_atomic.atomic, program.invocations);
NeutralizePredicate(*slot_atomic.ballot);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,43 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <vector>
#include <boost/container/small_vector.hpp>
#include "shader_recompiler/frontend/ir/basic_block.h"
#include "shader_recompiler/frontend/ir/value.h"
#include "shader_recompiler/ir_opt/passes.h"
namespace Shader::Optimization {
void IdentityRemovalPass(IR::Program& program) {
boost::container::small_vector<IR::Inst*, 16> to_invalidate;
for (IR::Block* const block : program.blocks) {
for (auto it = block->begin(); it != block->end();) {
const size_t num_args{it->NumArgs()};
for (size_t i = 0; i < num_args; ++i) {
IR::Value arg = it->Arg(i);
if (arg.IsIdentity()) {
do { // Pointer chasing (3-derefs)
arg = arg.Inst()->Arg(0);
} while (arg.IsIdentity());
it->SetArg(i, arg);
}
}
if (it->GetOpcode() == IR::Opcode::Identity || it->GetOpcode() == IR::Opcode::Void) {
to_invalidate.push_back(&*it);
it = block->Instructions().erase(it);
} else {
++it;
}
}
}
for (IR::Inst* const inst : to_invalidate)
inst->Invalidate();
}
} // namespace Shader::Optimization

View File

@@ -0,0 +1,68 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <algorithm>
#include <bit>
#include <optional>
#include <boost/container/small_vector.hpp>
#include "shader_recompiler/environment.h"
#include "shader_recompiler/frontend/ir/basic_block.h"
#include "shader_recompiler/frontend/ir/breadth_first_search.h"
#include "shader_recompiler/frontend/ir/ir_emitter.h"
#include "shader_recompiler/host_translate_info.h"
#include "shader_recompiler/ir_opt/passes.h"
#include "shader_recompiler/shader_info.h"
namespace Shader::Optimization {
static IR::Attribute EmulatedLayerAttribute(VaryingState& stores) {
for (u32 i = 0; i < 32; i++) {
if (!stores.Generic(i)) {
return IR::Attribute::Generic0X + (i * 4);
}
}
return IR::Attribute::Layer;
}
static bool PermittedProgramStage(Stage stage) {
switch (stage) {
case Stage::VertexA:
case Stage::VertexB:
case Stage::TessellationControl:
case Stage::TessellationEval:
return true;
default:
return false;
}
}
void LayerPass(IR::Program& program, const HostTranslateInfo& host_info) {
if (host_info.support_viewport_index_layer || !PermittedProgramStage(program.stage)) {
return;
}
const auto end{program.post_order_blocks.end()};
const auto layer_attribute = EmulatedLayerAttribute(program.info.stores);
bool requires_layer_emulation = false;
for (auto block = program.post_order_blocks.begin(); block != end; ++block) {
for (IR::Inst& inst : (*block)->Instructions()) {
if (inst.GetOpcode() == IR::Opcode::SetAttribute &&
inst.Arg(0).Attribute() == IR::Attribute::Layer) {
requires_layer_emulation = true;
inst.SetArg(0, IR::Value{layer_attribute});
}
}
}
if (requires_layer_emulation) {
program.info.requires_layer_emulation = true;
program.info.emulated_layer = layer_attribute;
program.info.stores.Set(IR::Attribute::Layer, false);
program.info.stores.Set(layer_attribute, true);
}
}
} // namespace Shader::Optimization

View File

@@ -0,0 +1,158 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "shader_recompiler/frontend/ir/ir_emitter.h"
#include "shader_recompiler/frontend/ir/value.h"
#include "shader_recompiler/ir_opt/passes.h"
namespace Shader::Optimization {
namespace {
IR::Opcode Replace(IR::Opcode op) {
switch (op) {
case IR::Opcode::SelectF16: // (hookshot: left as F16, it selected between values now 32 bits wide)
return IR::Opcode::SelectF32;
case IR::Opcode::FPAbs16:
return IR::Opcode::FPAbs32;
case IR::Opcode::FPAdd16:
return IR::Opcode::FPAdd32;
case IR::Opcode::FPCeil16:
return IR::Opcode::FPCeil32;
case IR::Opcode::FPFloor16:
return IR::Opcode::FPFloor32;
case IR::Opcode::FPFma16:
return IR::Opcode::FPFma32;
case IR::Opcode::FPMul16:
return IR::Opcode::FPMul32;
case IR::Opcode::FPNeg16:
return IR::Opcode::FPNeg32;
case IR::Opcode::FPRoundEven16:
return IR::Opcode::FPRoundEven32;
case IR::Opcode::FPSaturate16:
return IR::Opcode::FPSaturate32;
case IR::Opcode::FPClamp16:
return IR::Opcode::FPClamp32;
case IR::Opcode::FPTrunc16:
return IR::Opcode::FPTrunc32;
case IR::Opcode::CompositeConstructF16x2:
return IR::Opcode::CompositeConstructF32x2;
case IR::Opcode::CompositeConstructF16x3:
return IR::Opcode::CompositeConstructF32x3;
case IR::Opcode::CompositeConstructF16x4:
return IR::Opcode::CompositeConstructF32x4;
case IR::Opcode::CompositeExtractF16x2:
return IR::Opcode::CompositeExtractF32x2;
case IR::Opcode::CompositeExtractF16x3:
return IR::Opcode::CompositeExtractF32x3;
case IR::Opcode::CompositeExtractF16x4:
return IR::Opcode::CompositeExtractF32x4;
case IR::Opcode::CompositeInsertF16x2:
return IR::Opcode::CompositeInsertF32x2;
case IR::Opcode::CompositeInsertF16x3:
return IR::Opcode::CompositeInsertF32x3;
case IR::Opcode::CompositeInsertF16x4:
return IR::Opcode::CompositeInsertF32x4;
case IR::Opcode::FPOrdEqual16:
return IR::Opcode::FPOrdEqual32;
case IR::Opcode::FPUnordEqual16:
return IR::Opcode::FPUnordEqual32;
case IR::Opcode::FPOrdNotEqual16:
return IR::Opcode::FPOrdNotEqual32;
case IR::Opcode::FPUnordNotEqual16:
return IR::Opcode::FPUnordNotEqual32;
case IR::Opcode::FPOrdLessThan16:
return IR::Opcode::FPOrdLessThan32;
case IR::Opcode::FPUnordLessThan16:
return IR::Opcode::FPUnordLessThan32;
case IR::Opcode::FPOrdGreaterThan16:
return IR::Opcode::FPOrdGreaterThan32;
case IR::Opcode::FPUnordGreaterThan16:
return IR::Opcode::FPUnordGreaterThan32;
case IR::Opcode::FPOrdLessThanEqual16:
return IR::Opcode::FPOrdLessThanEqual32;
case IR::Opcode::FPUnordLessThanEqual16:
return IR::Opcode::FPUnordLessThanEqual32;
case IR::Opcode::FPOrdGreaterThanEqual16:
return IR::Opcode::FPOrdGreaterThanEqual32;
case IR::Opcode::FPUnordGreaterThanEqual16:
return IR::Opcode::FPUnordGreaterThanEqual32;
case IR::Opcode::FPIsNan16:
return IR::Opcode::FPIsNan32;
case IR::Opcode::ConvertS16F16:
return IR::Opcode::ConvertS16F32;
case IR::Opcode::ConvertS32F16:
return IR::Opcode::ConvertS32F32;
case IR::Opcode::ConvertS64F16:
return IR::Opcode::ConvertS64F32;
case IR::Opcode::ConvertU16F16:
return IR::Opcode::ConvertU16F32;
case IR::Opcode::ConvertU32F16:
return IR::Opcode::ConvertU32F32;
case IR::Opcode::ConvertU64F16:
return IR::Opcode::ConvertU64F32;
case IR::Opcode::PackFloat2x16:
return IR::Opcode::PackHalf2x16;
case IR::Opcode::UnpackFloat2x16:
return IR::Opcode::UnpackHalf2x16;
case IR::Opcode::ConvertF32F16:
return IR::Opcode::Identity;
case IR::Opcode::ConvertF16F32:
return IR::Opcode::Identity;
case IR::Opcode::ConvertF16S8:
return IR::Opcode::ConvertF32S8;
case IR::Opcode::ConvertF16S16:
return IR::Opcode::ConvertF32S16;
case IR::Opcode::ConvertF16S32:
return IR::Opcode::ConvertF32S32;
case IR::Opcode::ConvertF16S64:
return IR::Opcode::ConvertF32S64;
case IR::Opcode::ConvertF16U8:
return IR::Opcode::ConvertF32U8;
case IR::Opcode::ConvertF16U16:
return IR::Opcode::ConvertF32U16;
case IR::Opcode::ConvertF16U32:
return IR::Opcode::ConvertF32U32;
case IR::Opcode::ConvertF16U64:
return IR::Opcode::ConvertF32U64;
case IR::Opcode::GlobalAtomicAddF16x2:
return IR::Opcode::GlobalAtomicAddF32x2;
case IR::Opcode::StorageAtomicAddF16x2:
return IR::Opcode::StorageAtomicAddF32x2;
case IR::Opcode::GlobalAtomicMinF16x2:
return IR::Opcode::GlobalAtomicMinF32x2;
case IR::Opcode::StorageAtomicMinF16x2:
return IR::Opcode::StorageAtomicMinF32x2;
case IR::Opcode::GlobalAtomicMaxF16x2:
return IR::Opcode::GlobalAtomicMaxF32x2;
case IR::Opcode::StorageAtomicMaxF16x2:
return IR::Opcode::StorageAtomicMaxF32x2;
default:
return op;
}
}
} // Anonymous namespace
void LowerFp16ToFp32(IR::Program& program) {
for (IR::Block* const block : program.blocks) {
for (IR::Inst& inst : block->Instructions()) {
inst.ReplaceOpcode(Replace(inst.GetOpcode()));
}
}
// (hookshot) A half's bits and a half, now held in 32-bit floats: converted, not reinterpreted. Left as bitcasts they took a
// 32-bit float's bits for a half's, and Switch 2 shaders that move halves through integers drew noise on cards without f16
// shaders (GTX 10xx, Hookshot issue 98). After the loop above, so every float they meet is 32 bits already
for (IR::Block* const block : program.blocks) {
for (IR::Inst& inst : block->Instructions()) {
if (inst.GetOpcode() == IR::Opcode::BitCastU16F16) {
IR::IREmitter ir{*block, IR::Block::InstructionList::s_iterator_to(inst)};
const IR::U32 bits{ir.PackHalf2x16(ir.CompositeConstruct(inst.Arg(0), ir.Imm32(0.0f)))};
inst.ReplaceUsesWith(IR::Value{ir.UConvert(16, bits)});
} else if (inst.GetOpcode() == IR::Opcode::BitCastF16U16) {
IR::IREmitter ir{*block, IR::Block::InstructionList::s_iterator_to(inst)};
const IR::U32 wide{ir.UConvert(32, IR::U16{inst.Arg(0)})};
inst.ReplaceUsesWith(ir.CompositeExtract(ir.UnpackHalf2x16(wide), 0));
}
}
}
}
} // namespace Shader::Optimization

View File

@@ -0,0 +1,188 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "shader_recompiler/frontend/ir/ir_emitter.h"
#include "shader_recompiler/frontend/ir/opcodes.h"
#include "shader_recompiler/frontend/ir/value.h"
#include "shader_recompiler/ir_opt/passes.h"
namespace Shader::Optimization {
namespace {
constexpr s32 F64ToF32Exp = +1023 - 127;
constexpr s32 F32ToF64Exp = +127 - 1023;
IR::F32 PackedF64ToF32(IR::IREmitter& ir, const IR::Value& packed) {
const IR::U32 lo{ir.CompositeExtract(packed, 0)};
const IR::U32 hi{ir.CompositeExtract(packed, 1)};
const IR::U32 sign{ir.BitFieldExtract(hi, ir.Imm32(31), ir.Imm32(1))};
const IR::U32 exp{ir.BitFieldExtract(hi, ir.Imm32(20), ir.Imm32(11))};
const IR::U32 mantissa_hi{ir.BitFieldExtract(hi, ir.Imm32(0), ir.Imm32(20))};
const IR::U32 mantissa_lo{ir.BitFieldExtract(lo, ir.Imm32(29), ir.Imm32(3))};
const IR::U32 mantissa{
ir.BitwiseOr(ir.ShiftLeftLogical(mantissa_hi, ir.Imm32(3)), mantissa_lo)};
const IR::U32 exp_if_subnorm{
ir.Select(ir.IEqual(exp, ir.Imm32(0)), ir.Imm32(0), ir.IAdd(exp, ir.Imm32(F64ToF32Exp)))};
const IR::U32 exp_if_infnan{
ir.Select(ir.IEqual(exp, ir.Imm32(0x7ff)), ir.Imm32(0xff), exp_if_subnorm)};
const IR::U32 result{
ir.BitwiseOr(ir.ShiftLeftLogical(sign, ir.Imm32(31)),
ir.BitwiseOr(ir.ShiftLeftLogical(exp_if_infnan, ir.Imm32(23)), mantissa))};
return ir.BitCast<IR::F32>(result);
}
IR::Value F32ToPackedF64(IR::IREmitter& ir, const IR::Value& raw) {
const IR::U32 value{ir.BitCast<IR::U32>(IR::F32(raw))};
const IR::U32 sign{ir.BitFieldExtract(value, ir.Imm32(31), ir.Imm32(1))};
const IR::U32 exp{ir.BitFieldExtract(value, ir.Imm32(23), ir.Imm32(8))};
const IR::U32 mantissa{ir.BitFieldExtract(value, ir.Imm32(0), ir.Imm32(23))};
const IR::U32 mantissa_hi{ir.BitFieldExtract(mantissa, ir.Imm32(3), ir.Imm32(20))};
const IR::U32 mantissa_lo{ir.BitFieldExtract(mantissa, ir.Imm32(0), ir.Imm32(3))};
const IR::U32 exp_if_subnorm{
ir.Select(ir.IEqual(exp, ir.Imm32(0)), ir.Imm32(0), ir.IAdd(exp, ir.Imm32(F32ToF64Exp)))};
const IR::U32 exp_if_infnan{
ir.Select(ir.IEqual(exp, ir.Imm32(0xff)), ir.Imm32(0x7ff), exp_if_subnorm)};
const IR::U32 lo{ir.ShiftLeftLogical(mantissa_lo, ir.Imm32(29))};
const IR::U32 hi{
ir.BitwiseOr(ir.ShiftLeftLogical(sign, ir.Imm32(31)),
ir.BitwiseOr(ir.ShiftLeftLogical(exp_if_infnan, ir.Imm32(20)), mantissa_hi))};
return ir.CompositeConstruct(lo, hi);
}
IR::Opcode Replace(IR::Opcode op) {
switch (op) {
case IR::Opcode::FPAbs64:
return IR::Opcode::FPAbs32;
case IR::Opcode::FPAdd64:
return IR::Opcode::FPAdd32;
case IR::Opcode::FPCeil64:
return IR::Opcode::FPCeil32;
case IR::Opcode::FPFloor64:
return IR::Opcode::FPFloor32;
case IR::Opcode::FPFma64:
return IR::Opcode::FPFma32;
case IR::Opcode::FPMul64:
return IR::Opcode::FPMul32;
case IR::Opcode::FPNeg64:
return IR::Opcode::FPNeg32;
case IR::Opcode::FPRoundEven64:
return IR::Opcode::FPRoundEven32;
case IR::Opcode::FPSaturate64:
return IR::Opcode::FPSaturate32;
case IR::Opcode::FPClamp64:
return IR::Opcode::FPClamp32;
case IR::Opcode::FPTrunc64:
return IR::Opcode::FPTrunc32;
case IR::Opcode::CompositeConstructF64x2:
return IR::Opcode::CompositeConstructF32x2;
case IR::Opcode::CompositeConstructF64x3:
return IR::Opcode::CompositeConstructF32x3;
case IR::Opcode::CompositeConstructF64x4:
return IR::Opcode::CompositeConstructF32x4;
case IR::Opcode::CompositeExtractF64x2:
return IR::Opcode::CompositeExtractF32x2;
case IR::Opcode::CompositeExtractF64x3:
return IR::Opcode::CompositeExtractF32x3;
case IR::Opcode::CompositeExtractF64x4:
return IR::Opcode::CompositeExtractF32x4;
case IR::Opcode::CompositeInsertF64x2:
return IR::Opcode::CompositeInsertF32x2;
case IR::Opcode::CompositeInsertF64x3:
return IR::Opcode::CompositeInsertF32x3;
case IR::Opcode::CompositeInsertF64x4:
return IR::Opcode::CompositeInsertF32x4;
case IR::Opcode::FPOrdEqual64:
return IR::Opcode::FPOrdEqual32;
case IR::Opcode::FPUnordEqual64:
return IR::Opcode::FPUnordEqual32;
case IR::Opcode::FPOrdNotEqual64:
return IR::Opcode::FPOrdNotEqual32;
case IR::Opcode::FPUnordNotEqual64:
return IR::Opcode::FPUnordNotEqual32;
case IR::Opcode::FPOrdLessThan64:
return IR::Opcode::FPOrdLessThan32;
case IR::Opcode::FPUnordLessThan64:
return IR::Opcode::FPUnordLessThan32;
case IR::Opcode::FPOrdGreaterThan64:
return IR::Opcode::FPOrdGreaterThan32;
case IR::Opcode::FPUnordGreaterThan64:
return IR::Opcode::FPUnordGreaterThan32;
case IR::Opcode::FPOrdLessThanEqual64:
return IR::Opcode::FPOrdLessThanEqual32;
case IR::Opcode::FPUnordLessThanEqual64:
return IR::Opcode::FPUnordLessThanEqual32;
case IR::Opcode::FPOrdGreaterThanEqual64:
return IR::Opcode::FPOrdGreaterThanEqual32;
case IR::Opcode::FPUnordGreaterThanEqual64:
return IR::Opcode::FPUnordGreaterThanEqual32;
case IR::Opcode::FPIsNan64:
return IR::Opcode::FPIsNan32;
case IR::Opcode::ConvertS16F64:
return IR::Opcode::ConvertS16F32;
case IR::Opcode::ConvertS32F64:
return IR::Opcode::ConvertS32F32;
case IR::Opcode::ConvertS64F64:
return IR::Opcode::ConvertS64F32;
case IR::Opcode::ConvertU16F64:
return IR::Opcode::ConvertU16F32;
case IR::Opcode::ConvertU32F64:
return IR::Opcode::ConvertU32F32;
case IR::Opcode::ConvertU64F64:
return IR::Opcode::ConvertU64F32;
case IR::Opcode::ConvertF32F64:
return IR::Opcode::Identity;
case IR::Opcode::ConvertF64F32:
return IR::Opcode::Identity;
case IR::Opcode::ConvertF64S8:
return IR::Opcode::ConvertF32S8;
case IR::Opcode::ConvertF64S16:
return IR::Opcode::ConvertF32S16;
case IR::Opcode::ConvertF64S32:
return IR::Opcode::ConvertF32S32;
case IR::Opcode::ConvertF64S64:
return IR::Opcode::ConvertF32S64;
case IR::Opcode::ConvertF64U8:
return IR::Opcode::ConvertF32U8;
case IR::Opcode::ConvertF64U16:
return IR::Opcode::ConvertF32U16;
case IR::Opcode::ConvertF64U32:
return IR::Opcode::ConvertF32U32;
case IR::Opcode::ConvertF64U64:
return IR::Opcode::ConvertF32U64;
default:
return op;
}
}
void Lower(IR::Block& block, IR::Inst& inst) {
switch (inst.GetOpcode()) {
case IR::Opcode::PackDouble2x32: {
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
inst.ReplaceUsesWith(PackedF64ToF32(ir, inst.Arg(0)));
break;
}
case IR::Opcode::UnpackDouble2x32: {
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
inst.ReplaceUsesWith(F32ToPackedF64(ir, inst.Arg(0)));
break;
}
default:
inst.ReplaceOpcode(Replace(inst.GetOpcode()));
break;
}
}
} // Anonymous namespace
void LowerFp64ToFp32(IR::Program& program) {
for (IR::Block* const block : program.blocks) {
for (IR::Inst& inst : block->Instructions()) {
Lower(*block, inst);
}
}
}
} // namespace Shader::Optimization

View File

@@ -0,0 +1,362 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <utility>
#include "shader_recompiler/exception.h"
#include "shader_recompiler/frontend/ir/basic_block.h"
#include "shader_recompiler/frontend/ir/ir_emitter.h"
#include "shader_recompiler/frontend/ir/program.h"
#include "shader_recompiler/frontend/ir/value.h"
#include "shader_recompiler/ir_opt/passes.h"
namespace Shader::Optimization {
namespace {
std::pair<IR::U32, IR::U32> Unpack(IR::IREmitter& ir, const IR::Value& packed) {
if (packed.IsImmediate()) {
const u64 value{packed.U64()};
return {
ir.Imm32(static_cast<u32>(value)),
ir.Imm32(static_cast<u32>(value >> 32)),
};
} else {
return std::pair<IR::U32, IR::U32>{
ir.CompositeExtract(packed, 0u),
ir.CompositeExtract(packed, 1u),
};
}
}
void IAdd64To32(IR::Block& block, IR::Inst& inst) {
if (inst.HasAssociatedPseudoOperation()) {
throw NotImplementedException("IAdd64 emulation with pseudo instructions");
}
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
const auto [a_lo, a_hi]{Unpack(ir, inst.Arg(0))};
const auto [b_lo, b_hi]{Unpack(ir, inst.Arg(1))};
const IR::U32 ret_lo{ir.IAdd(a_lo, b_lo)};
const IR::U32 carry{ir.Select(ir.GetCarryFromOp(ret_lo), ir.Imm32(1u), ir.Imm32(0u))};
const IR::U32 ret_hi{ir.IAdd(ir.IAdd(a_hi, b_hi), carry)};
inst.ReplaceUsesWith(ir.CompositeConstruct(ret_lo, ret_hi));
}
void ISub64To32(IR::Block& block, IR::Inst& inst) {
if (inst.HasAssociatedPseudoOperation()) {
throw NotImplementedException("ISub64 emulation with pseudo instructions");
}
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
const auto [a_lo, a_hi]{Unpack(ir, inst.Arg(0))};
const auto [b_lo, b_hi]{Unpack(ir, inst.Arg(1))};
const IR::U32 ret_lo{ir.ISub(a_lo, b_lo)};
const IR::U1 underflow{ir.IGreaterThan(ret_lo, a_lo, false)};
const IR::U32 underflow_bit{ir.Select(underflow, ir.Imm32(1u), ir.Imm32(0u))};
const IR::U32 ret_hi{ir.ISub(ir.ISub(a_hi, b_hi), underflow_bit)};
inst.ReplaceUsesWith(ir.CompositeConstruct(ret_lo, ret_hi));
}
void INeg64To32(IR::Block& block, IR::Inst& inst) {
if (inst.HasAssociatedPseudoOperation()) {
throw NotImplementedException("INeg64 emulation with pseudo instructions");
}
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
auto [lo, hi]{Unpack(ir, inst.Arg(0))};
lo = ir.BitwiseNot(lo);
hi = ir.BitwiseNot(hi);
lo = ir.IAdd(lo, ir.Imm32(1));
const IR::U32 carry{ir.Select(ir.GetCarryFromOp(lo), ir.Imm32(1u), ir.Imm32(0u))};
hi = ir.IAdd(hi, carry);
inst.ReplaceUsesWith(ir.CompositeConstruct(lo, hi));
}
void ShiftLeftLogical64To32(IR::Block& block, IR::Inst& inst) {
if (inst.HasAssociatedPseudoOperation()) {
throw NotImplementedException("ShiftLeftLogical64 emulation with pseudo instructions");
}
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
const auto [lo, hi]{Unpack(ir, inst.Arg(0))};
const IR::U32 shift{inst.Arg(1)};
const IR::U32 shifted_lo{ir.ShiftLeftLogical(lo, shift)};
const IR::U32 shifted_hi{ir.ShiftLeftLogical(hi, shift)};
const IR::U32 inv_shift{ir.ISub(shift, ir.Imm32(32))};
const IR::U1 is_long{ir.IGreaterThanEqual(inv_shift, ir.Imm32(0), true)};
const IR::U1 is_zero{ir.IEqual(shift, ir.Imm32(0))};
const IR::U32 long_ret_lo{ir.Imm32(0)};
const IR::U32 long_ret_hi{ir.ShiftLeftLogical(lo, inv_shift)};
const IR::U32 shift_complement{ir.ISub(ir.Imm32(32), shift)};
const IR::U32 lo_extract{ir.BitFieldExtract(lo, shift_complement, shift, false)};
const IR::U32 short_ret_lo{shifted_lo};
const IR::U32 short_ret_hi{ir.BitwiseOr(shifted_hi, lo_extract)};
const IR::U32 zero_ret_lo{lo};
const IR::U32 zero_ret_hi{hi};
const IR::U32 non_zero_lo{ir.Select(is_long, long_ret_lo, short_ret_lo)};
const IR::U32 non_zero_hi{ir.Select(is_long, long_ret_hi, short_ret_hi)};
const IR::U32 ret_lo{ir.Select(is_zero, zero_ret_lo, non_zero_lo)};
const IR::U32 ret_hi{ir.Select(is_zero, zero_ret_hi, non_zero_hi)};
inst.ReplaceUsesWith(ir.CompositeConstruct(ret_lo, ret_hi));
}
void ShiftRightLogical64To32(IR::Block& block, IR::Inst& inst) {
if (inst.HasAssociatedPseudoOperation()) {
throw NotImplementedException("ShiftRightLogical64 emulation with pseudo instructions");
}
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
const auto [lo, hi]{Unpack(ir, inst.Arg(0))};
const IR::U32 shift{inst.Arg(1)};
const IR::U32 shifted_lo{ir.ShiftRightLogical(lo, shift)};
const IR::U32 shifted_hi{ir.ShiftRightLogical(hi, shift)};
const IR::U32 inv_shift{ir.ISub(shift, ir.Imm32(32))};
const IR::U1 is_long{ir.IGreaterThanEqual(inv_shift, ir.Imm32(0), true)};
const IR::U1 is_zero{ir.IEqual(shift, ir.Imm32(0))};
const IR::U32 long_ret_hi{ir.Imm32(0)};
const IR::U32 long_ret_lo{ir.ShiftRightLogical(hi, inv_shift)};
const IR::U32 shift_complement{ir.ISub(ir.Imm32(32), shift)};
const IR::U32 short_hi_extract{ir.BitFieldExtract(hi, ir.Imm32(0), shift)};
const IR::U32 short_ret_hi{shifted_hi};
const IR::U32 short_ret_lo{
ir.BitFieldInsert(shifted_lo, short_hi_extract, shift_complement, shift)};
const IR::U32 zero_ret_lo{lo};
const IR::U32 zero_ret_hi{hi};
const IR::U32 non_zero_lo{ir.Select(is_long, long_ret_lo, short_ret_lo)};
const IR::U32 non_zero_hi{ir.Select(is_long, long_ret_hi, short_ret_hi)};
const IR::U32 ret_lo{ir.Select(is_zero, zero_ret_lo, non_zero_lo)};
const IR::U32 ret_hi{ir.Select(is_zero, zero_ret_hi, non_zero_hi)};
inst.ReplaceUsesWith(ir.CompositeConstruct(ret_lo, ret_hi));
}
void ShiftRightArithmetic64To32(IR::Block& block, IR::Inst& inst) {
if (inst.HasAssociatedPseudoOperation()) {
throw NotImplementedException("ShiftRightArithmetic64 emulation with pseudo instructions");
}
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
const auto [lo, hi]{Unpack(ir, inst.Arg(0))};
const IR::U32 shift{inst.Arg(1)};
const IR::U32 shifted_lo{ir.ShiftRightLogical(lo, shift)};
const IR::U32 shifted_hi{ir.ShiftRightArithmetic(hi, shift)};
const IR::U32 sign_extension{ir.ShiftRightArithmetic(hi, ir.Imm32(31))};
const IR::U32 inv_shift{ir.ISub(shift, ir.Imm32(32))};
const IR::U1 is_long{ir.IGreaterThanEqual(inv_shift, ir.Imm32(0), true)};
const IR::U1 is_zero{ir.IEqual(shift, ir.Imm32(0))};
const IR::U32 long_ret_hi{sign_extension};
const IR::U32 long_ret_lo{ir.ShiftRightArithmetic(hi, inv_shift)};
const IR::U32 shift_complement{ir.ISub(ir.Imm32(32), shift)};
const IR::U32 short_hi_extract(ir.BitFieldExtract(hi, ir.Imm32(0), shift));
const IR::U32 short_ret_hi{shifted_hi};
const IR::U32 short_ret_lo{
ir.BitFieldInsert(shifted_lo, short_hi_extract, shift_complement, shift)};
const IR::U32 zero_ret_lo{lo};
const IR::U32 zero_ret_hi{hi};
const IR::U32 non_zero_lo{ir.Select(is_long, long_ret_lo, short_ret_lo)};
const IR::U32 non_zero_hi{ir.Select(is_long, long_ret_hi, short_ret_hi)};
const IR::U32 ret_lo{ir.Select(is_zero, zero_ret_lo, non_zero_lo)};
const IR::U32 ret_hi{ir.Select(is_zero, zero_ret_hi, non_zero_hi)};
inst.ReplaceUsesWith(ir.CompositeConstruct(ret_lo, ret_hi));
}
void IAbs64To32(IR::Block& block, IR::Inst& inst) {
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
const auto [lo, hi]{Unpack(ir, inst.Arg(0))};
const IR::U32 neg_lo{ir.IAdd(ir.BitwiseNot(lo), ir.Imm32(1))};
const IR::U32 carry{IR::U32{ir.Select(ir.GetCarryFromOp(neg_lo), ir.Imm32(1u), ir.Imm32(0u))}};
const IR::U32 neg_hi{ir.IAdd(ir.BitwiseNot(hi), carry)};
const IR::U1 is_negative{ir.INotEqual(ir.BitwiseAnd(hi, ir.Imm32(0x80000000u)), ir.Imm32(0u))};
const IR::U32 ret_lo{IR::U32{ir.Select(is_negative, neg_lo, lo)}};
const IR::U32 ret_hi{IR::U32{ir.Select(is_negative, neg_hi, hi)}};
inst.ReplaceUsesWith(ir.CompositeConstruct(ret_lo, ret_hi));
}
void SelectU64To32(IR::Block& block, IR::Inst& inst) {
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
const IR::U1 condition{inst.Arg(0)};
const auto [true_lo, true_hi]{Unpack(ir, inst.Arg(1))};
const auto [false_lo, false_hi]{Unpack(ir, inst.Arg(2))};
const IR::U32 ret_lo{IR::U32{ir.Select(condition, true_lo, false_lo)}};
const IR::U32 ret_hi{IR::U32{ir.Select(condition, true_hi, false_hi)}};
inst.ReplaceUsesWith(ir.CompositeConstruct(ret_lo, ret_hi));
}
void UndefU64To32(IR::Block& block, IR::Inst& inst) {
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
inst.ReplaceUsesWith(ir.CompositeConstruct(ir.Imm32(0u), ir.Imm32(0u)));
}
void ConvertU64U32To32(IR::Block& block, IR::Inst& inst) {
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
inst.ReplaceUsesWith(ir.CompositeConstruct(IR::U32{inst.Arg(0)}, ir.Imm32(0u)));
}
void ConvertU32U64To32(IR::Block& block, IR::Inst& inst) {
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
inst.ReplaceUsesWith(Unpack(ir, inst.Arg(0)).first);
}
void IntToFloat64To32(IR::Block& block, IR::Inst& inst, bool is_signed, size_t dest_bitsize) {
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
const auto [lo, hi]{Unpack(ir, inst.Arg(0))};
const IR::F32 low{ir.ConvertUToF(32, 32, lo)};
const IR::F32 high{is_signed ? IR::F32{ir.ConvertSToF(32, 32, hi)}
: IR::F32{ir.ConvertUToF(32, 32, hi)}};
const IR::F32 combined{ir.FPFma(high, ir.Imm32(4294967296.0f), low)};
if (dest_bitsize == 32) {
inst.ReplaceUsesWith(combined);
} else {
inst.ReplaceUsesWith(ir.FPConvert(dest_bitsize, combined));
}
}
void FloatToInt64To32(IR::Block& block, IR::Inst& inst, bool is_signed, size_t src_bitsize) {
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
const IR::F32 value{src_bitsize == 32 ? IR::F32{inst.Arg(0)}
: IR::F32{ir.FPConvert(32, IR::F16F32F64{inst.Arg(0)})}};
const IR::F32 high_f{ir.FPFloor(ir.FPMul(value, ir.Imm32(1.0f / 4294967296.0f)))};
const IR::U32 hi{is_signed ? IR::U32{ir.ConvertFToS(32, high_f)}
: IR::U32{ir.ConvertFToU(32, high_f)}};
const IR::F32 low_f{ir.FPFma(high_f, ir.FPNeg(ir.Imm32(4294967296.0f)), value)};
const IR::U32 lo{IR::U32{ir.ConvertFToU(32, low_f)}};
inst.ReplaceUsesWith(ir.CompositeConstruct(lo, hi));
}
void Lower(IR::Block& block, IR::Inst& inst) {
switch (inst.GetOpcode()) {
case IR::Opcode::PackUint2x32:
case IR::Opcode::UnpackUint2x32:
return inst.ReplaceOpcode(IR::Opcode::Identity);
case IR::Opcode::IAdd64:
return IAdd64To32(block, inst);
case IR::Opcode::ISub64:
return ISub64To32(block, inst);
case IR::Opcode::INeg64:
return INeg64To32(block, inst);
case IR::Opcode::ShiftLeftLogical64:
return ShiftLeftLogical64To32(block, inst);
case IR::Opcode::ShiftRightLogical64:
return ShiftRightLogical64To32(block, inst);
case IR::Opcode::ShiftRightArithmetic64:
return ShiftRightArithmetic64To32(block, inst);
case IR::Opcode::SharedAtomicExchange64:
return inst.ReplaceOpcode(IR::Opcode::SharedAtomicExchange32x2);
case IR::Opcode::GlobalAtomicIAdd64:
return inst.ReplaceOpcode(IR::Opcode::GlobalAtomicIAdd32x2);
case IR::Opcode::GlobalAtomicSMin64:
return inst.ReplaceOpcode(IR::Opcode::GlobalAtomicSMin32x2);
case IR::Opcode::GlobalAtomicUMin64:
return inst.ReplaceOpcode(IR::Opcode::GlobalAtomicUMin32x2);
case IR::Opcode::GlobalAtomicSMax64:
return inst.ReplaceOpcode(IR::Opcode::GlobalAtomicSMax32x2);
case IR::Opcode::GlobalAtomicUMax64:
return inst.ReplaceOpcode(IR::Opcode::GlobalAtomicUMax32x2);
case IR::Opcode::GlobalAtomicAnd64:
return inst.ReplaceOpcode(IR::Opcode::GlobalAtomicAnd32x2);
case IR::Opcode::GlobalAtomicOr64:
return inst.ReplaceOpcode(IR::Opcode::GlobalAtomicOr32x2);
case IR::Opcode::GlobalAtomicXor64:
return inst.ReplaceOpcode(IR::Opcode::GlobalAtomicXor32x2);
case IR::Opcode::GlobalAtomicExchange64:
return inst.ReplaceOpcode(IR::Opcode::GlobalAtomicExchange32x2);
case IR::Opcode::StorageAtomicIAdd64:
return inst.ReplaceOpcode(IR::Opcode::StorageAtomicIAdd32x2);
case IR::Opcode::StorageAtomicSMin64:
return inst.ReplaceOpcode(IR::Opcode::StorageAtomicSMin32x2);
case IR::Opcode::StorageAtomicUMin64:
return inst.ReplaceOpcode(IR::Opcode::StorageAtomicUMin32x2);
case IR::Opcode::StorageAtomicSMax64:
return inst.ReplaceOpcode(IR::Opcode::StorageAtomicSMax32x2);
case IR::Opcode::StorageAtomicUMax64:
return inst.ReplaceOpcode(IR::Opcode::StorageAtomicUMax32x2);
case IR::Opcode::StorageAtomicAnd64:
return inst.ReplaceOpcode(IR::Opcode::StorageAtomicAnd32x2);
case IR::Opcode::StorageAtomicOr64:
return inst.ReplaceOpcode(IR::Opcode::StorageAtomicOr32x2);
case IR::Opcode::StorageAtomicXor64:
return inst.ReplaceOpcode(IR::Opcode::StorageAtomicXor32x2);
case IR::Opcode::StorageAtomicExchange64:
return inst.ReplaceOpcode(IR::Opcode::StorageAtomicExchange32x2);
case IR::Opcode::BitCastU64F64:
return inst.ReplaceOpcode(IR::Opcode::UnpackDouble2x32);
case IR::Opcode::BitCastF64U64:
return inst.ReplaceOpcode(IR::Opcode::PackDouble2x32);
case IR::Opcode::UndefU64:
return UndefU64To32(block, inst);
case IR::Opcode::SelectU64:
return SelectU64To32(block, inst);
case IR::Opcode::IAbs64:
return IAbs64To32(block, inst);
case IR::Opcode::ConvertU64U32:
return ConvertU64U32To32(block, inst);
case IR::Opcode::ConvertU32U64:
return ConvertU32U64To32(block, inst);
case IR::Opcode::ConvertS64F16:
return FloatToInt64To32(block, inst, true, 16);
case IR::Opcode::ConvertS64F32:
return FloatToInt64To32(block, inst, true, 32);
case IR::Opcode::ConvertS64F64:
return FloatToInt64To32(block, inst, true, 64);
case IR::Opcode::ConvertU64F16:
return FloatToInt64To32(block, inst, false, 16);
case IR::Opcode::ConvertU64F32:
return FloatToInt64To32(block, inst, false, 32);
case IR::Opcode::ConvertU64F64:
return FloatToInt64To32(block, inst, false, 64);
case IR::Opcode::ConvertF16S64:
return IntToFloat64To32(block, inst, true, 16);
case IR::Opcode::ConvertF32S64:
return IntToFloat64To32(block, inst, true, 32);
case IR::Opcode::ConvertF64S64:
return IntToFloat64To32(block, inst, true, 64);
case IR::Opcode::ConvertF16U64:
return IntToFloat64To32(block, inst, false, 16);
case IR::Opcode::ConvertF32U64:
return IntToFloat64To32(block, inst, false, 32);
case IR::Opcode::ConvertF64U64:
return IntToFloat64To32(block, inst, false, 64);
default:
break;
}
}
} // Anonymous namespace
void LowerInt64ToInt32(IR::Program& program) {
const auto end{program.post_order_blocks.rend()};
for (auto it = program.post_order_blocks.rbegin(); it != end; ++it) {
IR::Block* const block{*it};
for (IR::Inst& inst : block->Instructions()) {
Lower(*block, inst);
}
}
}
} // namespace Shader::Optimization

View File

@@ -0,0 +1,90 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// (hookshot) From Orchid (MythosOfs/Orchid, src/shader_recompiler/ir_opt/oot_visual_workaround.h, GPL-3.0), matched by
// the IR's shape here instead of Orchid's shader hashes: a visual workaround for Ocarina of Time, not a fix. The root
// cause of the washed-out outdoors is open there and here.
// - fog: the one compute shader with an add of 2^x and -2^y, the fog integration: its slice distance scaled before
// the extinction and scattering accumulation (Orchid: 0.25; 0.08 here; it thins interiors' fog too);
// - exposure: a fragment shader with three loads of c3+0x38, the final pass's exposure before its filmic curve (0.65).
// RX_VK_OOT_VISUAL=0: off. RX_VK_OOT_FOG, RX_VK_OOT_EXPOSURE: the factors.
#pragma once
#include <utility>
#include <vector>
#include "shader_recompiler/frontend/ir/ir_emitter.h"
#include "shader_recompiler/frontend/ir/program.h"
namespace Shader::Optimization {
/// The edits made (0: not such a shader)
inline size_t ApplyOotVisualWorkaround(IR::Program& program, f32 fog_scale, f32 exposure_scale) {
const bool fog = program.stage == Stage::Compute;
const bool exposure = program.stage == Stage::Fragment;
if (!fog && !exposure) return 0;
const auto has_opcode = [](const IR::Value& value, IR::Opcode opcode) {
return !value.IsImmediate() && value.InstRecursive()->GetOpcode() == opcode;
};
const auto negative_exp = [&](const IR::Value& value) {
if (has_opcode(value, IR::Opcode::FPNeg32)) {
return has_opcode(value.InstRecursive()->Arg(0), IR::Opcode::FPExp2);
}
if (!has_opcode(value, IR::Opcode::BitCastF32U32)) return false;
const auto bits = value.InstRecursive()->Arg(0);
if (!has_opcode(bits, IR::Opcode::BitwiseXor32)) return false;
const auto* flip = bits.InstRecursive();
const auto source = flip->Arg(0);
return flip->Arg(1).IsImmediate() && flip->Arg(1).U32() == 0x80000000U &&
has_opcode(source, IR::Opcode::BitCastU32F32) &&
has_opcode(source.InstRecursive()->Arg(0), IR::Opcode::FPExp2);
};
std::vector<std::pair<IR::Block*, IR::Inst*>> matches;
for (IR::Block* block : program.blocks) {
for (IR::Inst& inst : block->Instructions()) {
if (fog && inst.GetOpcode() == IR::Opcode::FPAdd32 &&
((negative_exp(inst.Arg(0)) && has_opcode(inst.Arg(1), IR::Opcode::FPExp2)) ||
(negative_exp(inst.Arg(1)) && has_opcode(inst.Arg(0), IR::Opcode::FPExp2)))) {
matches.emplace_back(block, &inst);
}
if (exposure && inst.GetOpcode() == IR::Opcode::GetCbufF32 && inst.Arg(0).IsImmediate() &&
inst.Arg(0).U32() == 3 && inst.Arg(1).IsImmediate() && inst.Arg(1).U32() == 0x38) {
matches.emplace_back(block, &inst);
}
}
}
if (matches.size() != (fog ? 1u : 3u)) return 0;
for (const auto& [block, inst] : matches) {
const auto insertion = block->Instructions().iterator_to(*inst);
const auto original = block->PrependNewInst(insertion, *inst);
IR::IREmitter ir{*block, insertion};
const auto scaled = ir.FPMul(IR::F32{IR::Value{&*original}}, ir.Imm32(fog ? fog_scale : exposure_scale));
inst->ReplaceUsesWith(scaled);
}
return matches.size();
}
/// (hookshot) Not a NaN or an infinity into a float colour target. One such pixel in the scene went through the bloom's chain of
/// averages to the whole picture: the one-frame white flash, the HUD over it (RX_VK_FRAMELOG, RX_VK_PROBE=2 caught it: a NaN in the
/// scene target, the blur after it all at its top). What the console's blend does with one is unknown; nothing here wants it.
/// float_targets: bit i, target i is a float one (an integer target's bits go as they are)
inline size_t GuardFragmentOutputs(IR::Program& program, u32 float_targets) {
if (program.stage != Stage::Fragment) return 0;
std::vector<std::pair<IR::Block*, IR::Inst*>> writes;
for (IR::Block* block : program.blocks) {
for (IR::Inst& inst : block->Instructions()) {
if (inst.GetOpcode() == IR::Opcode::SetFragColor && inst.Arg(0).IsImmediate() &&
(float_targets >> inst.Arg(0).U32() & 1) != 0 && !inst.Arg(2).IsImmediate()) {
writes.emplace_back(block, &inst);
}
}
}
for (const auto& [block, inst] : writes) {
IR::IREmitter ir{*block, block->Instructions().iterator_to(*inst)};
const IR::F32 value{inst->Arg(2)};
const IR::F32 limited{ir.FPClamp(value, ir.Imm32(-1e30f), ir.Imm32(1e30f))};
const IR::F32 safe{ir.Select(ir.FPIsNan(value), ir.Imm32(0.0f), limited)};
inst->SetArg(2, safe);
}
return writes.size();
}
} // namespace Shader::Optimization

View File

@@ -0,0 +1,45 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "shader_recompiler/environment.h"
#include "shader_recompiler/frontend/ir/program.h"
namespace Shader {
struct HostTranslateInfo;
}
namespace Shader::Optimization {
void CollectShaderInfoPass(Environment& env, IR::Program& program);
void ConditionalBarrierPass(IR::Program& program);
void ConstantPropagationPass(Environment& env, IR::Program& program);
void DeadCodeEliminationPass(IR::Program& program);
void GeometryCompactionPass(IR::Program& program, const HostTranslateInfo& host_info);
void GlobalMemoryToStorageBufferPass(Environment& env, IR::Program& program,
const HostTranslateInfo& host_info);
void IdentityRemovalPass(IR::Program& program);
void LowerFp64ToFp32(IR::Program& program);
void LowerFp16ToFp32(IR::Program& program);
void LowerInt64ToInt32(IR::Program& program);
void RescalingPass(IR::Program& program);
void SsaRewritePass(IR::Program& program);
void PositionPass(Environment& env, IR::Program& program);
void BlitConstantsPass(Environment& env, IR::Program& program);
void TexturePass(Environment& env, IR::Program& program, const HostTranslateInfo& host_info);
void LayerPass(IR::Program& program, const HostTranslateInfo& host_info);
void VendorWorkaroundPass(IR::Program& program);
void VerificationPass(const IR::Program& program);
bool HasBrokenPattern(const IR::Program& program);
// Dual Vertex
void VertexATransformPass(IR::Program& program);
void VertexBTransformPass(IR::Program& program);
void JoinTextureInfo(Info& base, Info& source);
void JoinStorageInfo(Info& base, Info& source);
} // namespace Shader::Optimization

View File

@@ -0,0 +1,228 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <boost/container/small_vector.hpp>
#include "shader_recompiler/frontend/ir/basic_block.h"
#include "shader_recompiler/frontend/ir/ir_emitter.h"
#include "shader_recompiler/frontend/ir/value.h"
#include "shader_recompiler/ir_opt/passes.h"
namespace Shader::Optimization {
namespace {
struct PositionInst {
IR::Inst* inst;
IR::Block* block;
IR::Attribute attr;
};
using PositionInstVector = boost::container::small_vector<PositionInst, 24>;
/// True when every value reaching this one, through phis, is the bit pattern of a float.
bool IsFloatBits(const IR::Value& value, int depth) {
const IR::Inst* const inst = value.TryInstRecursive();
if (!inst) {
return false;
}
if (inst->GetOpcode() == IR::Opcode::BitCastU32F32) {
return true;
}
if (inst->GetOpcode() != IR::Opcode::Phi || depth == 4) {
return false;
}
for (size_t arg = 0; arg < inst->NumArgs(); ++arg) {
if (!IsFloatBits(inst->Arg(arg), depth + 1)) {
return false;
}
}
return true;
}
} // Anonymous namespace
void PositionPass(Environment& env, IR::Program& program) {
if (env.ShaderStage() != Stage::VertexB || env.ReadViewportTransformState()) {
return;
}
Info& info{program.info};
info.uses_render_area = true;
PositionInstVector to_replace;
for (IR::Block* const block : program.post_order_blocks) {
for (IR::Inst& inst : block->Instructions()) {
switch (inst.GetOpcode()) {
case IR::Opcode::SetAttribute: {
const IR::Attribute attr{inst.Arg(0).Attribute()};
switch (attr) {
case IR::Attribute::PositionX:
case IR::Attribute::PositionY: {
to_replace.push_back(PositionInst{.inst = &inst, .block = block, .attr = attr});
break;
}
default:
break;
}
break;
}
default:
break;
}
}
}
for (PositionInst& position_inst : to_replace) {
IR::IREmitter ir{*position_inst.block,
IR::Block::InstructionList::s_iterator_to(*position_inst.inst)};
const IR::F32 value(position_inst.inst->Arg(1));
const IR::F32F64 scale(ir.Imm32(2.f));
const IR::F32 negative_one{ir.Imm32(-1.f)};
switch (position_inst.attr) {
case IR::Attribute::PositionX: {
position_inst.inst->SetArg(
1,
ir.FPFma(value, ir.FPMul(ir.FPRecip(ir.RenderAreaWidth()), scale), negative_one));
break;
}
case IR::Attribute::PositionY: {
position_inst.inst->SetArg(
1,
ir.FPFma(value, ir.FPMul(ir.FPRecip(ir.RenderAreaHeight()), scale), negative_one));
break;
}
default:
break;
}
}
}
// Switch 2 fullscreen blit vertex shaders read a parameter block from constant bank 24 (lowered to
// bank 17, which games never bind) that the console provides itself. For a blit covering the whole
// render target it holds: texel size (0x0/0x4, again at 0x10/0x14), size (0x8/0xc), a UV scale of
// 1 (0x18/0x1c), the UV clamp rectangle 0,0..1,1 (0x30-0x3c, read by the fragment shader), a zero
// dither bias (0x40-0x48) and the integer source rectangle 0,0..W-1,H-1 (0x50-0x5c).
void BlitConstantsPass(Environment& env, IR::Program& program) {
if (env.ShaderStage() != Stage::VertexB && env.ShaderStage() != Stage::Fragment) {
return;
}
// Bank 24 is root table 0, which games may load themselves (3D methods 0x504/0x508; the buffer
// cache binds it as bank 17): Fire Emblem: Fortune's Weave keeps its material push constants
// there. A shader reading bank 17 outside the blit block reads such a table and keeps its reads,
// as does one reading only words the blit block leaves zero (an unbound bank reads zero anyway;
// the game's enemy health gauges read only 0x40 of their table).
const auto in_blit_block = [](u32 offset) {
return offset < 0x20 || (offset >= 0x30 && offset < 0x4c) || (offset >= 0x50 && offset < 0x60);
};
const auto is_nonzero_word = [](u32 offset) {
return offset < 0x20 || offset == 0x38 || offset == 0x3c || offset == 0x58 || offset == 0x5c;
};
bool reads_nonzero_word{};
for (IR::Block* const block : program.post_order_blocks) {
for (IR::Inst& inst : block->Instructions()) {
const IR::Opcode opcode{inst.GetOpcode()};
if ((opcode != IR::Opcode::GetCbufU32 && opcode != IR::Opcode::GetCbufF32 &&
opcode != IR::Opcode::GetCbufU32x2) ||
!inst.Arg(0).IsImmediate() || inst.Arg(0).U32() != 17 || !inst.Arg(1).IsImmediate()) {
continue;
}
const u32 offset{inst.Arg(1).U32()};
const bool pair{opcode == IR::Opcode::GetCbufU32x2};
if (!in_blit_block(offset) || (pair && !in_blit_block(offset + 4))) {
return;
}
reads_nonzero_word |= is_nonzero_word(offset) || (pair && is_nonzero_word(offset + 4));
}
}
if (!reads_nonzero_word) {
return;
}
// Native fragment shaders add an integer dither bias from 0x10-0x18 to their output bits; the
// console leaves it zero there, while the blit block holds texel size and UV scale. Kirby Air
// Riders' SMAA resolve adds it to a phi of sRGB-encoded colours, which turned the world white.
if (env.ShaderStage() == Stage::Fragment && env.HasNativeInstructions()) {
for (IR::Block* const block : program.post_order_blocks) {
for (IR::Inst& inst : block->Instructions()) {
if (inst.GetOpcode() != IR::Opcode::BitCastF32U32) {
continue;
}
const IR::Inst* const arithmetic = inst.Arg(0).TryInstRecursive();
if (!arithmetic || (arithmetic->GetOpcode() != IR::Opcode::IAdd32 &&
arithmetic->GetOpcode() != IR::Opcode::ISub32)) {
continue;
}
for (size_t bias_arg = 0; bias_arg < 2; ++bias_arg) {
if (bias_arg == 0 && arithmetic->GetOpcode() == IR::Opcode::ISub32) {
continue;
}
const IR::Inst* const bias = arithmetic->Arg(bias_arg).TryInstRecursive();
if (!bias || bias->GetOpcode() != IR::Opcode::GetCbufU32 ||
!IsFloatBits(arithmetic->Arg(1 - bias_arg), 0) ||
!bias->Arg(0).IsImmediate() || bias->Arg(0).U32() != 17 ||
!bias->Arg(1).IsImmediate()) {
continue;
}
const u32 offset = bias->Arg(1).U32();
if (offset == 0x10 || offset == 0x14 || offset == 0x18) {
inst.SetArg(0, arithmetic->Arg(1 - bias_arg));
break;
}
}
}
}
}
bool used{};
for (IR::Block* const block : program.post_order_blocks) {
for (IR::Inst& inst : block->Instructions()) {
const IR::Opcode opcode{inst.GetOpcode()};
if (opcode != IR::Opcode::GetCbufU32 && opcode != IR::Opcode::GetCbufF32 &&
opcode != IR::Opcode::GetCbufU32x2) {
continue;
}
if (!inst.Arg(0).IsImmediate() || inst.Arg(0).U32() != 17 || !inst.Arg(1).IsImmediate()) {
continue;
}
IR::IREmitter ir{*block, IR::Block::InstructionList::s_iterator_to(inst)};
const IR::F32 width{ir.RenderAreaWidth()};
const IR::F32 height{ir.RenderAreaHeight()};
const auto word = [&](u32 offset) -> IR::U32 {
switch (offset) {
case 0x0:
case 0x10:
return ir.BitCast<IR::U32>(IR::F32{ir.FPRecip(width)});
case 0x4:
case 0x14:
return ir.BitCast<IR::U32>(IR::F32{ir.FPRecip(height)});
case 0x8:
return ir.BitCast<IR::U32>(width);
case 0xc:
return ir.BitCast<IR::U32>(height);
case 0x18:
case 0x1c:
case 0x38:
case 0x3c:
return ir.Imm32(0x3f800000U);
case 0x58:
return IR::U32{ir.ConvertFToU(32, ir.FPAdd(width, ir.Imm32(-1.0f)))};
case 0x5c:
return IR::U32{ir.ConvertFToU(32, ir.FPAdd(height, ir.Imm32(-1.0f)))};
default:
return ir.Imm32(0U);
}
};
const u32 offset{inst.Arg(1).U32()};
IR::Value value;
if (opcode == IR::Opcode::GetCbufU32x2) {
value = ir.CompositeConstruct(word(offset), word(offset + 4));
} else if (opcode == IR::Opcode::GetCbufF32) {
value = ir.BitCast<IR::F32>(word(offset));
} else {
value = word(offset);
}
inst.ReplaceUsesWith(value);
used = true;
}
}
if (used) {
program.info.uses_render_area = true;
}
}
} // namespace Shader::Optimization

View File

@@ -0,0 +1,111 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <cstddef>
#include "common/common_types.h"
#include "shader_recompiler/frontend/ir/value.h"
namespace Shader::Optimization {
constexpr u32 MaskCanBeMinusOne{1u << 0};
constexpr u32 MaskCanBeZero{1u << 1};
constexpr u32 MaskCanBeOne{1u << 2};
constexpr u32 MaskUnknown{0};
constexpr u32 MaxMaskAnalysisDepth{8};
// negation maps -1 -> 1, 0 -> 0 and 1 -> -1: swap the -1 and 1 bits, keep 0
inline u32 NegateMask(u32 values) {
return (values & MaskCanBeZero) | ((values & MaskCanBeMinusOne) << 2) |
((values & MaskCanBeOne) >> 2);
}
// x + 1 with x != 1: -1 becomes 0, 0 becomes 1
inline u32 AddOneToMask(u32 values) {
return values << 1;
}
// x - 1 with x != -1: 0 becomes -1, 1 becomes 0
inline u32 SubtractOneFromMask(u32 values) {
return values >> 1;
}
// mask constant: guest code encodes booleans as 0, 1 and -1 (all bits set)
inline bool IsMaskConstant(const IR::Value& candidate) {
return candidate.IsImmediate() &&
(candidate.U32() == 0 || candidate.U32() == 1 || candidate.U32() == 0xffffffffU);
}
/// "predicate mask" can take: -1, 0 and 1.
/// guest compilers build masks from selects, negations, +/-1 adjustments and phis.
/// everything unproven comes back as MaskUnknown, so callers only fold
/// with a proven mask and never fold on a wrong one.
inline u32 MaskValues(const IR::Value& value, u32 depth = 0) {
const IR::Value resolved{value.Resolve()};
if (resolved.IsImmediate()) {
if (resolved.Type() != IR::Type::U32) {
return MaskUnknown;
}
switch (resolved.U32()) {
case 0xffffffffU:
return MaskCanBeMinusOne;
case 0:
return MaskCanBeZero;
case 1:
return MaskCanBeOne;
default:
return MaskUnknown;
}
}
if (depth >= MaxMaskAnalysisDepth) {
return MaskUnknown;
}
const IR::Inst* const inst{resolved.InstRecursive()};
switch (inst->GetOpcode()) {
case IR::Opcode::SelectU32: {
const u32 true_values{MaskValues(inst->Arg(1), depth + 1)};
const u32 false_values{MaskValues(inst->Arg(2), depth + 1)};
return true_values != MaskUnknown && false_values != MaskUnknown
? true_values | false_values
: MaskUnknown;
}
case IR::Opcode::INeg32:
return NegateMask(MaskValues(inst->Arg(0), depth + 1));
case IR::Opcode::IAdd32:
for (size_t index = 0; index < 2; ++index) {
const IR::Value step{inst->Arg(1 - index).Resolve()};
if (!step.IsImmediate()) {
continue;
}
const u32 values{MaskValues(inst->Arg(index), depth + 1)};
// +/- 1 stays in the domain only if the sum cannot escape it
// 1 + 1 = 2 and -1 - 1 = -2 are not mask values
if (step.U32() == 1 && (values & MaskCanBeOne) == 0) {
return AddOneToMask(values);
}
if (step.U32() == 0xffffffffU && (values & MaskCanBeMinusOne) == 0) {
return SubtractOneFromMask(values);
}
return MaskUnknown;
}
return MaskUnknown;
case IR::Opcode::Phi: {
u32 values{};
for (size_t index = 0; index < inst->NumArgs(); ++index) {
const u32 arg_values{MaskValues(inst->Arg(index), depth + 1)};
if (arg_values == MaskUnknown) {
return MaskUnknown;
}
values |= arg_values;
}
return values;
}
default:
return MaskUnknown;
}
}
} // namespace Shader::Optimization

View File

@@ -0,0 +1,359 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "common/settings.h"
#include "shader_recompiler/environment.h"
#include "shader_recompiler/frontend/ir/ir_emitter.h"
#include "shader_recompiler/frontend/ir/modifiers.h"
#include "shader_recompiler/frontend/ir/program.h"
#include "shader_recompiler/frontend/ir/value.h"
#include "shader_recompiler/ir_opt/passes.h"
#include "shader_recompiler/shader_info.h"
namespace Shader::Optimization {
namespace {
[[nodiscard]] bool IsTextureTypeRescalable(TextureType type) {
switch (type) {
case TextureType::Color2D:
case TextureType::ColorArray2D:
case TextureType::Color2DRect:
return true;
case TextureType::Color1D:
case TextureType::ColorArray1D:
case TextureType::Color3D:
case TextureType::ColorCube:
case TextureType::ColorArrayCube:
case TextureType::Buffer:
break;
}
return false;
}
void VisitMark(IR::Block& block, IR::Inst& inst) {
switch (inst.GetOpcode()) {
case IR::Opcode::ShuffleIndex:
case IR::Opcode::ShuffleUp:
case IR::Opcode::ShuffleDown:
case IR::Opcode::ShuffleButterfly: {
const IR::Value shfl_arg{inst.Arg(0)};
if (shfl_arg.IsImmediate()) {
break;
}
const IR::Inst* const arg_inst{shfl_arg.InstRecursive()};
if (arg_inst->GetOpcode() != IR::Opcode::BitCastU32F32) {
break;
}
const IR::Value bitcast_arg{arg_inst->Arg(0)};
if (bitcast_arg.IsImmediate()) {
break;
}
IR::Inst* const bitcast_inst{bitcast_arg.InstRecursive()};
bool must_patch_outside = false;
if (bitcast_inst->GetOpcode() == IR::Opcode::GetAttribute) {
const IR::Attribute attr{bitcast_inst->Arg(0).Attribute()};
switch (attr) {
case IR::Attribute::PositionX:
case IR::Attribute::PositionY:
bitcast_inst->SetFlags<u32>(0xDEADBEEF);
must_patch_outside = true;
break;
default:
break;
}
}
if (must_patch_outside) {
const auto it{IR::Block::InstructionList::s_iterator_to(inst)};
IR::IREmitter ir{block, it};
IR::Inst* const new_inst{&*block.PrependNewInst(it, inst)};
const IR::F32 new_bitcast{ir.ConvertUToF(32, 32, IR::Value{new_inst})};
const IR::F32 up_factor{ir.FPRecip(ir.ResolutionDownFactor())};
const IR::Value converted{ir.FPMul(new_bitcast, up_factor)};
inst.ReplaceUsesWith(converted);
}
break;
}
default:
break;
}
}
void PatchFragCoord(IR::Block& block, IR::Inst& inst) {
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
const IR::F32 down_factor{ir.ResolutionDownFactor()};
const IR::F32 frag_coord{ir.GetAttribute(inst.Arg(0).Attribute())};
const IR::F32 downscaled_frag_coord{ir.FPMul(frag_coord, down_factor)};
inst.ReplaceUsesWith(downscaled_frag_coord);
}
void PatchPointSize(IR::Block& block, IR::Inst& inst) {
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
const IR::F32 point_value{inst.Arg(1)};
const IR::F32 up_factor{ir.FPRecip(ir.ResolutionDownFactor())};
const IR::F32 upscaled_point_value{ir.FPMul(point_value, up_factor)};
inst.SetArg(1, upscaled_point_value);
}
[[nodiscard]] IR::U32 Scale(IR::IREmitter& ir, const IR::U1& is_scaled, const IR::U32& value) {
IR::U32 scaled_value{value};
if (const u32 up_scale = Settings::values.resolution_info.up_scale; up_scale != 1) {
scaled_value = ir.IMul(scaled_value, ir.Imm32(up_scale));
}
if (const u32 down_shift = Settings::values.resolution_info.down_shift; down_shift != 0) {
scaled_value = ir.ShiftRightArithmetic(scaled_value, ir.Imm32(down_shift));
}
return IR::U32{ir.Select(is_scaled, scaled_value, value)};
}
[[nodiscard]] IR::U32 SubScale(IR::IREmitter& ir, const IR::U1& is_scaled, const IR::U32& value,
const IR::Attribute attrib) {
const IR::F32 up_factor{ir.Imm32(Settings::values.resolution_info.up_factor)};
const IR::F32 base{ir.FPMul(ir.ConvertUToF(32, 32, value), up_factor)};
const IR::F32 frag_coord{ir.GetAttribute(attrib)};
const IR::F32 down_factor{ir.Imm32(Settings::values.resolution_info.down_factor)};
const IR::F32 floor{ir.FPMul(up_factor, ir.FPFloor(ir.FPMul(frag_coord, down_factor)))};
const IR::F16F32F64 deviation{ir.FPAdd(base, ir.FPAdd(frag_coord, ir.FPNeg(floor)))};
return IR::U32{ir.Select(is_scaled, ir.ConvertFToU(32, deviation), value)};
}
[[nodiscard]] IR::U32 DownScale(IR::IREmitter& ir, const IR::U1& is_scaled, const IR::U32& value) {
IR::U32 scaled_value{value};
if (const u32 down_shift = Settings::values.resolution_info.down_shift; down_shift != 0) {
scaled_value = ir.ShiftLeftLogical(scaled_value, ir.Imm32(down_shift));
}
if (const u32 up_scale = Settings::values.resolution_info.up_scale; up_scale != 1) {
scaled_value = ir.IDiv(scaled_value, ir.Imm32(up_scale));
}
return IR::U32{ir.Select(is_scaled, scaled_value, value)};
}
void PatchImageQueryDimensions(IR::Block& block, IR::Inst& inst) {
const auto it{IR::Block::InstructionList::s_iterator_to(inst)};
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
const auto info{inst.Flags<IR::TextureInstInfo>()};
const IR::U1 is_scaled{ir.IsTextureScaled(ir.Imm32(info.descriptor_index))};
switch (info.type) {
case TextureType::Color2D:
case TextureType::ColorArray2D:
case TextureType::Color2DRect: {
const IR::Value new_inst{&*block.PrependNewInst(it, inst)};
const IR::U32 width{DownScale(ir, is_scaled, IR::U32{ir.CompositeExtract(new_inst, 0)})};
const IR::U32 height{DownScale(ir, is_scaled, IR::U32{ir.CompositeExtract(new_inst, 1)})};
const IR::Value replacement{ir.CompositeConstruct(
width, height, ir.CompositeExtract(new_inst, 2), ir.CompositeExtract(new_inst, 3))};
inst.ReplaceUsesWith(replacement);
break;
}
case TextureType::Color1D:
case TextureType::ColorArray1D:
case TextureType::Color3D:
case TextureType::ColorCube:
case TextureType::ColorArrayCube:
case TextureType::Buffer:
// Nothing to patch here
break;
}
}
void ScaleIntegerComposite(IR::IREmitter& ir, IR::Inst& inst, const IR::U1& is_scaled,
size_t index) {
const IR::Value composite{inst.Arg(index)};
if (composite.IsEmpty()) {
return;
}
const auto info{inst.Flags<IR::TextureInstInfo>()};
const IR::U32 x{Scale(ir, is_scaled, IR::U32{ir.CompositeExtract(composite, 0)})};
const IR::U32 y{Scale(ir, is_scaled, IR::U32{ir.CompositeExtract(composite, 1)})};
switch (info.type) {
case TextureType::Color2D:
case TextureType::Color2DRect:
inst.SetArg(index, ir.CompositeConstruct(x, y));
break;
case TextureType::ColorArray2D: {
const IR::U32 z{ir.CompositeExtract(composite, 2)};
inst.SetArg(index, ir.CompositeConstruct(x, y, z));
break;
}
case TextureType::Color1D:
case TextureType::ColorArray1D:
case TextureType::Color3D:
case TextureType::ColorCube:
case TextureType::ColorArrayCube:
case TextureType::Buffer:
// Nothing to patch here
break;
}
}
void ScaleIntegerOffsetComposite(IR::IREmitter& ir, IR::Inst& inst, const IR::U1& is_scaled,
size_t index) {
const IR::Value composite{inst.Arg(index)};
if (composite.IsEmpty()) {
return;
}
const auto info{inst.Flags<IR::TextureInstInfo>()};
const IR::U32 x{Scale(ir, is_scaled, IR::U32{ir.CompositeExtract(composite, 0)})};
const IR::U32 y{Scale(ir, is_scaled, IR::U32{ir.CompositeExtract(composite, 1)})};
switch (info.type) {
case TextureType::ColorArray2D:
case TextureType::Color2D:
case TextureType::Color2DRect:
inst.SetArg(index, ir.CompositeConstruct(x, y));
break;
case TextureType::Color1D:
case TextureType::ColorArray1D:
case TextureType::Color3D:
case TextureType::ColorCube:
case TextureType::ColorArrayCube:
case TextureType::Buffer:
// Nothing to patch here
break;
}
}
void SubScaleCoord(IR::IREmitter& ir, IR::Inst& inst, const IR::U1& is_scaled) {
const auto info{inst.Flags<IR::TextureInstInfo>()};
const IR::Value coord{inst.Arg(1)};
const IR::U32 coord_x{ir.CompositeExtract(coord, 0)};
const IR::U32 coord_y{ir.CompositeExtract(coord, 1)};
const IR::U32 scaled_x{SubScale(ir, is_scaled, coord_x, IR::Attribute::PositionX)};
const IR::U32 scaled_y{SubScale(ir, is_scaled, coord_y, IR::Attribute::PositionY)};
switch (info.type) {
case TextureType::Color2D:
case TextureType::Color2DRect:
inst.SetArg(1, ir.CompositeConstruct(scaled_x, scaled_y));
break;
case TextureType::ColorArray2D: {
const IR::U32 z{ir.CompositeExtract(coord, 2)};
inst.SetArg(1, ir.CompositeConstruct(scaled_x, scaled_y, z));
break;
}
case TextureType::Color1D:
case TextureType::ColorArray1D:
case TextureType::Color3D:
case TextureType::ColorCube:
case TextureType::ColorArrayCube:
case TextureType::Buffer:
// Nothing to patch here
break;
}
}
void SubScaleImageFetch(IR::Block& block, IR::Inst& inst) {
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
const auto info{inst.Flags<IR::TextureInstInfo>()};
if (!IsTextureTypeRescalable(info.type)) {
return;
}
const IR::U1 is_scaled{ir.IsTextureScaled(ir.Imm32(info.descriptor_index))};
SubScaleCoord(ir, inst, is_scaled);
// Scale ImageFetch offset
ScaleIntegerOffsetComposite(ir, inst, is_scaled, 2);
}
void SubScaleImageRead(IR::Block& block, IR::Inst& inst) {
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
const auto info{inst.Flags<IR::TextureInstInfo>()};
if (!IsTextureTypeRescalable(info.type)) {
return;
}
const IR::U1 is_scaled{ir.IsImageScaled(ir.Imm32(info.descriptor_index))};
SubScaleCoord(ir, inst, is_scaled);
}
void PatchImageFetch(IR::Block& block, IR::Inst& inst) {
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
const auto info{inst.Flags<IR::TextureInstInfo>()};
if (!IsTextureTypeRescalable(info.type)) {
return;
}
const IR::U1 is_scaled{ir.IsTextureScaled(ir.Imm32(info.descriptor_index))};
ScaleIntegerComposite(ir, inst, is_scaled, 1);
// Scale ImageFetch offset
ScaleIntegerOffsetComposite(ir, inst, is_scaled, 2);
}
void PatchImageRead(IR::Block& block, IR::Inst& inst) {
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
const auto info{inst.Flags<IR::TextureInstInfo>()};
if (!IsTextureTypeRescalable(info.type)) {
return;
}
const IR::U1 is_scaled{ir.IsImageScaled(ir.Imm32(info.descriptor_index))};
ScaleIntegerComposite(ir, inst, is_scaled, 1);
}
void Visit(const IR::Program& program, IR::Block& block, IR::Inst& inst) {
const bool is_fragment_shader{program.stage == Stage::Fragment};
switch (inst.GetOpcode()) {
case IR::Opcode::GetAttribute: {
const IR::Attribute attr{inst.Arg(0).Attribute()};
switch (attr) {
case IR::Attribute::PositionX:
case IR::Attribute::PositionY:
if (is_fragment_shader && inst.Flags<u32>() != 0xDEADBEEF) {
PatchFragCoord(block, inst);
}
break;
default:
break;
}
break;
}
case IR::Opcode::SetAttribute: {
const IR::Attribute attr{inst.Arg(0).Attribute()};
switch (attr) {
case IR::Attribute::PointSize:
if (inst.Flags<u32>() != 0xDEADBEEF) {
PatchPointSize(block, inst);
}
break;
default:
break;
}
break;
}
case IR::Opcode::ImageQueryDimensions:
PatchImageQueryDimensions(block, inst);
break;
case IR::Opcode::ImageFetch:
if (is_fragment_shader) {
SubScaleImageFetch(block, inst);
} else {
PatchImageFetch(block, inst);
}
break;
case IR::Opcode::ImageRead:
if (is_fragment_shader) {
SubScaleImageRead(block, inst);
} else {
PatchImageRead(block, inst);
}
break;
default:
break;
}
}
} // Anonymous namespace
void RescalingPass(IR::Program& program) {
const bool is_fragment_shader{program.stage == Stage::Fragment};
if (is_fragment_shader) {
for (IR::Block* const block : program.post_order_blocks) {
for (IR::Inst& inst : block->Instructions()) {
VisitMark(*block, inst);
}
}
}
for (IR::Block* const block : program.post_order_blocks) {
for (IR::Inst& inst : block->Instructions()) {
Visit(program, *block, inst);
}
}
}
} // namespace Shader::Optimization

View File

@@ -0,0 +1,417 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
// This file implements the SSA rewriting algorithm proposed in
//
// Simple and Efficient Construction of Static Single Assignment Form.
// Braun M., Buchwald S., Hack S., Leiba R., Mallon C., Zwinkau A. (2013)
// In: Jhala R., De Bosschere K. (eds)
// Compiler Construction. CC 2013.
// Lecture Notes in Computer Science, vol 7791.
// Springer, Berlin, Heidelberg
//
// https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
//
#include <deque>
#include <map>
#include <span>
#include "common/container/unordered_map.h"
#include <variant>
#include <vector>
#include "shader_recompiler/frontend/ir/basic_block.h"
#include "shader_recompiler/frontend/ir/opcodes.h"
#include "shader_recompiler/frontend/ir/pred.h"
#include "shader_recompiler/frontend/ir/reg.h"
#include "shader_recompiler/frontend/ir/value.h"
#include "shader_recompiler/ir_opt/passes.h"
namespace Shader::Optimization {
namespace {
struct FlagTag {
auto operator<=>(const FlagTag&) const noexcept = default;
};
struct ZeroFlagTag : FlagTag {};
struct SignFlagTag : FlagTag {};
struct CarryFlagTag : FlagTag {};
struct OverflowFlagTag : FlagTag {};
struct GotoVariable : FlagTag {
GotoVariable() = default;
explicit GotoVariable(u32 index_) : index{index_} {}
auto operator<=>(const GotoVariable&) const noexcept = default;
u32 index;
};
struct IndirectBranchVariable {
auto operator<=>(const IndirectBranchVariable&) const noexcept = default;
};
using Variant = std::variant<IR::Reg, IR::Pred, ZeroFlagTag, SignFlagTag, CarryFlagTag, OverflowFlagTag, GotoVariable, IndirectBranchVariable>;
// TODO: majority of these require stable iterators, test with XC beforehand
using ValueMap = std::unordered_map<IR::Block*, IR::Value>;
struct DefTable {
const IR::Value& Def(IR::Block* block, IR::Reg variable) {
return block->SsaRegValue(variable);
}
void SetDef(IR::Block* block, IR::Reg variable, const IR::Value& value) {
block->SetSsaRegValue(variable, value);
}
const IR::Value& Def(IR::Block* block, IR::Pred variable) {
return preds[IR::PredIndex(variable)][block];
}
void SetDef(IR::Block* block, IR::Pred variable, const IR::Value& value) {
preds[IR::PredIndex(variable)].insert_or_assign(block, value);
}
const IR::Value& Def(IR::Block* block, GotoVariable variable) {
return goto_vars[variable.index][block];
}
void SetDef(IR::Block* block, GotoVariable variable, const IR::Value& value) {
goto_vars[variable.index].insert_or_assign(block, value);
}
const IR::Value& Def(IR::Block* block, IndirectBranchVariable) {
return indirect_branch_var[block];
}
void SetDef(IR::Block* block, IndirectBranchVariable, const IR::Value& value) {
indirect_branch_var.insert_or_assign(block, value);
}
const IR::Value& Def(IR::Block* block, ZeroFlagTag) {
return zero_flag[block];
}
void SetDef(IR::Block* block, ZeroFlagTag, const IR::Value& value) {
zero_flag.insert_or_assign(block, value);
}
const IR::Value& Def(IR::Block* block, SignFlagTag) {
return sign_flag[block];
}
void SetDef(IR::Block* block, SignFlagTag, const IR::Value& value) {
sign_flag.insert_or_assign(block, value);
}
const IR::Value& Def(IR::Block* block, CarryFlagTag) {
return carry_flag[block];
}
void SetDef(IR::Block* block, CarryFlagTag, const IR::Value& value) {
carry_flag.insert_or_assign(block, value);
}
const IR::Value& Def(IR::Block* block, OverflowFlagTag) {
return overflow_flag[block];
}
void SetDef(IR::Block* block, OverflowFlagTag, const IR::Value& value) {
overflow_flag.insert_or_assign(block, value);
}
std::array<ValueMap, IR::NUM_USER_PREDS> preds;
// TODO: Requires stable iterators
std::unordered_map<u32, ValueMap> goto_vars;
ValueMap indirect_branch_var;
ValueMap zero_flag;
ValueMap sign_flag;
ValueMap carry_flag;
ValueMap overflow_flag;
};
IR::Opcode UndefOpcode(IR::Reg) noexcept {
return IR::Opcode::UndefU32;
}
IR::Opcode UndefOpcode(IR::Pred) noexcept {
return IR::Opcode::UndefU1;
}
IR::Opcode UndefOpcode(const FlagTag&) noexcept {
return IR::Opcode::UndefU1;
}
IR::Opcode UndefOpcode(IndirectBranchVariable) noexcept {
return IR::Opcode::UndefU32;
}
enum class Status {
Start,
SetValue,
PreparePhiArgument,
PushPhiArgument,
};
template <typename Type>
struct ReadState {
ReadState(IR::Block* block_) : block{block_} {}
ReadState() = default;
IR::Block* block{};
IR::Value result{};
IR::Inst* phi{};
IR::Block* const* pred_it{};
IR::Block* const* pred_end{};
Status pc{Status::Start};
};
class Pass {
public:
template <typename Type>
void WriteVariable(Type variable, IR::Block* block, const IR::Value& value) {
current_def.SetDef(block, variable, value);
}
template <typename Type>
IR::Value ReadVariable(Type variable, IR::Block* root_block) {
// TODO: Windows commits sodoku if you use small_vector
std::vector<ReadState<Type>> stack{
ReadState<Type>(nullptr),
ReadState<Type>(root_block),
};
const auto prepare_phi_operand{[&] {
if (stack.back().pred_it == stack.back().pred_end) {
IR::Inst* const phi{stack.back().phi};
IR::Block* const block{stack.back().block};
const IR::Value result{TryRemoveTrivialPhi(*phi, block, UndefOpcode(variable))};
stack.pop_back();
stack.back().result = result;
WriteVariable(variable, block, result);
} else {
IR::Block* const imm_pred{*stack.back().pred_it};
stack.back().pc = Status::PushPhiArgument;
stack.emplace_back(imm_pred);
}
}};
do {
IR::Block* const block{stack.back().block};
switch (stack.back().pc) {
case Status::Start: {
if (const IR::Value& def = current_def.Def(block, variable); !def.IsEmpty()) {
stack.back().result = def;
} else if (!block->IsSsaSealed()) {
// Incomplete CFG
IR::Inst* phi{&*block->PrependNewInst(block->begin(), IR::Opcode::Phi)};
phi->SetFlags(IR::TypeOf(UndefOpcode(variable)));
incomplete_phis[block].insert_or_assign(variable, phi);
stack.back().result = IR::Value{&*phi};
} else if (const std::span imm_preds = block->ImmPredecessors();
imm_preds.size() == 1) {
// Optimize the common case of one predecessor: no phi needed
stack.back().pc = Status::SetValue;
stack.emplace_back(imm_preds.front());
break;
} else {
// Break potential cycles with operandless phi
IR::Inst* const phi{&*block->PrependNewInst(block->begin(), IR::Opcode::Phi)};
phi->SetFlags(IR::TypeOf(UndefOpcode(variable)));
WriteVariable(variable, block, IR::Value{phi});
stack.back().phi = phi;
stack.back().pred_it = imm_preds.data();
stack.back().pred_end = imm_preds.data() + imm_preds.size();
prepare_phi_operand();
break;
}
}
[[fallthrough]];
case Status::SetValue: {
const IR::Value result{stack.back().result};
WriteVariable(variable, block, result);
stack.pop_back();
stack.back().result = result;
break;
}
case Status::PushPhiArgument: {
IR::Inst* const phi{stack.back().phi};
phi->AddPhiOperand(*stack.back().pred_it, stack.back().result);
++stack.back().pred_it;
}
[[fallthrough]];
case Status::PreparePhiArgument:
prepare_phi_operand();
break;
}
} while (stack.size() > 1);
return stack.back().result;
}
void SealBlock(IR::Block* block) {
const auto it{incomplete_phis.find(block)};
if (it != incomplete_phis.end()) {
for (auto& pair : it->second) {
auto& variant{pair.first};
auto& phi{pair.second};
std::visit([&](auto& variable) { AddPhiOperands(variable, *phi, block); }, variant);
}
}
block->SsaSeal();
}
private:
template <typename Type>
IR::Value AddPhiOperands(Type variable, IR::Inst& phi, IR::Block* block) {
for (IR::Block* const imm_pred : block->ImmPredecessors()) {
phi.AddPhiOperand(imm_pred, ReadVariable(variable, imm_pred));
}
return TryRemoveTrivialPhi(phi, block, UndefOpcode(variable));
}
IR::Value TryRemoveTrivialPhi(IR::Inst& phi, IR::Block* block, IR::Opcode undef_opcode) {
IR::Value same;
const size_t num_args{phi.NumArgs()};
for (size_t arg_index = 0; arg_index < num_args; ++arg_index) {
const IR::Value& op{phi.Arg(arg_index)};
if (op.Resolve() == same.Resolve() || op == IR::Value{&phi}) {
// Unique value or self-reference
continue;
}
if (!same.IsEmpty()) {
// The phi merges at least two values: not trivial
return IR::Value{&phi};
}
same = op;
}
// Remove the phi node from the block, it will be reinserted
IR::Block::InstructionList& list{block->Instructions()};
list.erase(IR::Block::InstructionList::s_iterator_to(phi));
// Find the first non-phi instruction and use it as an insertion point
IR::Block::iterator reinsert_point{std::ranges::find_if_not(list, IR::IsPhi)};
if (same.IsEmpty()) {
// The phi is unreachable or in the start block
// Insert an undefined instruction and make it the phi node replacement
// The "phi" node reinsertion point is specified after this instruction
reinsert_point = block->PrependNewInst(reinsert_point, undef_opcode);
same = IR::Value{&*reinsert_point};
++reinsert_point;
}
// Reinsert the phi node and reroute all its uses to the "same" value
list.insert(reinsert_point, phi);
phi.ReplaceUsesWith(same);
// TODO: Try to recursively remove all phi users, which might have become trivial
return same;
}
// TODO: Windows dies with stack exhaustion?
std::unordered_map<IR::Block*, std::map<Variant, IR::Inst*>> incomplete_phis;
DefTable current_def;
};
void VisitInst(Pass& pass, IR::Block* block, IR::Inst& inst) {
switch (inst.GetOpcode()) {
case IR::Opcode::SetRegister:
if (const IR::Reg reg{inst.Arg(0).Reg()}; reg != IR::Reg::RZ) {
pass.WriteVariable(reg, block, inst.Arg(1));
}
break;
case IR::Opcode::SetPred:
if (const IR::Pred pred{inst.Arg(0).Pred()}; pred != IR::Pred::PT) {
pass.WriteVariable(pred, block, inst.Arg(1));
}
break;
case IR::Opcode::SetGotoVariable:
pass.WriteVariable(GotoVariable{inst.Arg(0).U32()}, block, inst.Arg(1));
break;
case IR::Opcode::SetIndirectBranchVariable:
pass.WriteVariable(IndirectBranchVariable{}, block, inst.Arg(0));
break;
case IR::Opcode::SetZFlag:
pass.WriteVariable(ZeroFlagTag{}, block, inst.Arg(0));
break;
case IR::Opcode::SetSFlag:
pass.WriteVariable(SignFlagTag{}, block, inst.Arg(0));
break;
case IR::Opcode::SetCFlag:
pass.WriteVariable(CarryFlagTag{}, block, inst.Arg(0));
break;
case IR::Opcode::SetOFlag:
pass.WriteVariable(OverflowFlagTag{}, block, inst.Arg(0));
break;
case IR::Opcode::GetRegister:
if (const IR::Reg reg{inst.Arg(0).Reg()}; reg != IR::Reg::RZ) {
inst.ReplaceUsesWith(pass.ReadVariable(reg, block));
}
break;
case IR::Opcode::GetPred:
if (const IR::Pred pred{inst.Arg(0).Pred()}; pred != IR::Pred::PT) {
inst.ReplaceUsesWith(pass.ReadVariable(pred, block));
}
break;
case IR::Opcode::GetGotoVariable:
inst.ReplaceUsesWith(pass.ReadVariable(GotoVariable{inst.Arg(0).U32()}, block));
break;
case IR::Opcode::GetIndirectBranchVariable:
inst.ReplaceUsesWith(pass.ReadVariable(IndirectBranchVariable{}, block));
break;
case IR::Opcode::GetZFlag:
inst.ReplaceUsesWith(pass.ReadVariable(ZeroFlagTag{}, block));
break;
case IR::Opcode::GetSFlag:
inst.ReplaceUsesWith(pass.ReadVariable(SignFlagTag{}, block));
break;
case IR::Opcode::GetCFlag:
inst.ReplaceUsesWith(pass.ReadVariable(CarryFlagTag{}, block));
break;
case IR::Opcode::GetOFlag:
inst.ReplaceUsesWith(pass.ReadVariable(OverflowFlagTag{}, block));
break;
default:
break;
}
}
void VisitBlock(Pass& pass, IR::Block* block) {
for (IR::Inst& inst : block->Instructions()) {
VisitInst(pass, block, inst);
}
pass.SealBlock(block);
}
IR::Type GetConcreteType(IR::Inst* inst) {
std::deque<IR::Inst*> queue;
queue.push_back(inst);
while (!queue.empty()) {
IR::Inst* current = queue.front();
queue.pop_front();
const size_t num_args{current->NumArgs()};
for (size_t i = 0; i < num_args; ++i) {
const auto set_type = current->Arg(i).Type();
if (set_type != IR::Type::Opaque) {
return set_type;
}
if (!current->Arg(i).IsImmediate()) {
queue.push_back(current->Arg(i).Inst());
}
}
}
return IR::Type::Opaque;
}
} // Anonymous namespace
void SsaRewritePass(IR::Program& program) {
Pass pass;
const auto end{program.post_order_blocks.rend()};
for (auto block = program.post_order_blocks.rbegin(); block != end; ++block) {
VisitBlock(pass, *block);
}
for (auto block = program.post_order_blocks.rbegin(); block != end; ++block) {
for (IR::Inst& inst : (*block)->Instructions()) {
if (inst.GetOpcode() == IR::Opcode::Phi) {
if (inst.Type() == IR::Type::Opaque) {
inst.SetFlags(GetConcreteType(&inst));
}
inst.OrderPhiArgs();
}
}
}
}
} // namespace Shader::Optimization

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,122 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "shader_recompiler/frontend/ir/basic_block.h"
#include "shader_recompiler/frontend/ir/ir_emitter.h"
#include "shader_recompiler/frontend/ir/value.h"
#include "shader_recompiler/ir_opt/passes.h"
namespace Shader::Optimization {
namespace {
void AddingByteSwapsWorkaround(IR::Block& block, IR::Inst& inst) {
/*
* Workaround for an NVIDIA bug seen in Super Mario RPG
*
* We are looking for this pattern:
* %lhs_bfe = BitFieldUExtract %factor_a, #0, #16
* %lhs_mul = IMul32 %lhs_bfe, %factor_b // potentially optional?
* %lhs_shl = ShiftLeftLogical32 %lhs_mul, #16
* %rhs_bfe = BitFieldUExtract %factor_a, #16, #16
* %result = IAdd32 %lhs_shl, %rhs_bfe
*
* And replacing the IAdd32 with a BitwiseOr32
* %result = BitwiseOr32 %lhs_shl, %rhs_bfe
*
*/
IR::Inst* const lhs_shl{inst.Arg(0).TryInstRecursive()};
IR::Inst* const rhs_bfe{inst.Arg(1).TryInstRecursive()};
if (!lhs_shl || !rhs_bfe) {
return;
}
if (lhs_shl->GetOpcode() != IR::Opcode::ShiftLeftLogical32 ||
lhs_shl->Arg(1) != IR::Value{16U}) {
return;
}
if (rhs_bfe->GetOpcode() != IR::Opcode::BitFieldUExtract || rhs_bfe->Arg(1) != IR::Value{16U} ||
rhs_bfe->Arg(2) != IR::Value{16U}) {
return;
}
IR::Inst* const lhs_mul{lhs_shl->Arg(0).TryInstRecursive()};
if (!lhs_mul) {
return;
}
const bool lhs_mul_optional{lhs_mul->GetOpcode() == IR::Opcode::BitFieldUExtract};
if (lhs_mul->GetOpcode() != IR::Opcode::IMul32 &&
lhs_mul->GetOpcode() != IR::Opcode::BitFieldUExtract) {
return;
}
IR::Inst* const lhs_bfe{lhs_mul_optional ? lhs_mul : lhs_mul->Arg(0).TryInstRecursive()};
if (!lhs_bfe) {
return;
}
if (lhs_bfe->GetOpcode() != IR::Opcode::BitFieldUExtract) {
return;
}
if (lhs_bfe->Arg(1) != IR::Value{0U} || lhs_bfe->Arg(2) != IR::Value{16U}) {
return;
}
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
inst.ReplaceUsesWith(ir.BitwiseOr(IR::U32{inst.Arg(0)}, IR::U32{inst.Arg(1)}));
}
} // Anonymous namespace
void VendorWorkaroundPass(IR::Program& program) {
for (IR::Block* const block : program.post_order_blocks) {
for (IR::Inst& inst : block->Instructions()) {
switch (inst.GetOpcode()) {
case IR::Opcode::IAdd32:
AddingByteSwapsWorkaround(*block, inst);
break;
default:
break;
}
}
}
}
bool HasBrokenPattern(const IR::Program& program) {
if (program.stage != Stage::Fragment) {
return false;
}
for (const IR::Block* block : program.post_order_blocks) {
for (const IR::Inst& inst : block->Instructions()) {
switch (inst.GetOpcode()) {
case IR::Opcode::ShuffleIndex:
case IR::Opcode::ShuffleUp:
case IR::Opcode::ShuffleDown:
case IR::Opcode::ShuffleButterfly:
break;
default:
continue;
}
const IR::Value shuffle_arg{inst.Arg(0)};
if (shuffle_arg.IsImmediate()) {
continue;
}
const IR::Inst* bitcast{shuffle_arg.InstRecursive()};
if (!bitcast || bitcast->GetOpcode() != IR::Opcode::BitCastU32F32) {
continue;
}
const IR::Value bitcast_arg{bitcast->Arg(0)};
if (bitcast_arg.IsImmediate()) {
continue;
}
const IR::Inst* attribute{bitcast_arg.InstRecursive()};
if (!attribute || attribute->GetOpcode() != IR::Opcode::GetAttribute) {
continue;
}
const IR::Attribute attr{attribute->Arg(0).Attribute()};
if (attr == IR::Attribute::PositionX || attr == IR::Attribute::PositionY) {
return true;
}
}
}
return false;
}
} // namespace Shader::Optimization

View File

@@ -0,0 +1,97 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <map>
#include <set>
#include "shader_recompiler/exception.h"
#include "shader_recompiler/frontend/ir/basic_block.h"
#include "shader_recompiler/frontend/ir/value.h"
#include "shader_recompiler/ir_opt/passes.h"
namespace Shader::Optimization {
static void ValidateTypes(const IR::Program& program) {
for (const auto& block : program.blocks) {
for (const IR::Inst& inst : *block) {
if (inst.GetOpcode() == IR::Opcode::Phi) {
// Skip validation on phi nodes
continue;
}
const size_t num_args{inst.NumArgs()};
for (size_t i = 0; i < num_args; ++i) {
const IR::Type t1{inst.Arg(i).Type()};
const IR::Type t2{IR::ArgTypeOf(inst.GetOpcode(), i)};
if (!IR::AreTypesCompatible(t1, t2)) {
throw LogicError("Invalid types in block:\n{}", IR::DumpBlock(*block));
}
}
}
}
}
static void ValidateUses(const IR::Program& program) {
std::map<IR::Inst*, int> actual_uses;
for (const auto& block : program.blocks) {
for (const IR::Inst& inst : *block) {
const size_t num_args{inst.NumArgs()};
for (size_t i = 0; i < num_args; ++i) {
const IR::Value arg{inst.Arg(i)};
if (!arg.IsImmediate()) {
++actual_uses[arg.Inst()];
}
}
}
}
for (const auto& [inst, uses] : actual_uses) {
if (inst->UseCount() != uses) {
throw LogicError("Invalid uses in block: {}", IR::DumpProgram(program));
}
}
}
static void ValidateForwardDeclarations(const IR::Program& program) {
std::set<const IR::Inst*> definitions;
for (const IR::Block* const block : program.blocks) {
for (const IR::Inst& inst : *block) {
definitions.emplace(&inst);
if (inst.GetOpcode() == IR::Opcode::Phi) {
// Phi nodes can have forward declarations
continue;
}
const size_t num_args{inst.NumArgs()};
for (size_t arg = 0; arg < num_args; ++arg) {
if (inst.Arg(arg).IsImmediate()) {
continue;
}
if (!definitions.contains(inst.Arg(arg).Inst())) {
throw LogicError("Forward declaration in block: {}", IR::DumpBlock(*block));
}
}
}
}
}
static void ValidatePhiNodes(const IR::Program& program) {
for (const IR::Block* const block : program.blocks) {
bool no_more_phis{false};
for (const IR::Inst& inst : *block) {
if (inst.GetOpcode() == IR::Opcode::Phi) {
if (no_more_phis) {
throw LogicError("Interleaved phi nodes: {}", IR::DumpBlock(*block));
}
} else {
no_more_phis = true;
}
}
}
}
void VerificationPass(const IR::Program& program) {
ValidateTypes(program);
ValidateUses(program);
ValidateForwardDeclarations(program);
ValidatePhiNodes(program);
}
} // namespace Shader::Optimization