From d76ecfac06a832fcad61a1df9ccc7fb21b129769 Mon Sep 17 00:00:00 2001 From: STL1te Date: Tue, 29 Sep 2026 14:57:38 +0300 Subject: [PATCH] fix(shader): DPP row and bank masks gate the instruction's VGPR write The decoder puts an instruction's DPP fields on its source, and only a destination carrying them has writeRawU32 keep the lanes the masks leave out (and, without bound_ctrl, the lanes with no source lane). destinationOperand copied them over, but no write used it, so every row was written: a wave64 scan adding the other row's total into the odd rows only (row_mask:0xa) added it into every row, the prefix sums a key pass indexes primitives by came out wrong, and the tree built from the keys held cycles its walkers looped on. As in the Kyty recompiler (writes through DestinationOperand): TranslateInstruction gives the destination its source's DPP fields once, and a mac accumulator reads the destination without them (accumulatorOperand). agc_dpp_write_tests checks a masked DPP add writes through DppUpdateU32. Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libSceAgcDriver/CMakeLists.txt | 7 +++- .../prx/libSceAgcDriver/tests/DppWrite.cpp | 40 +++++++++++++++++++ .../Translation/TranslationContext.hpp | 1 + .../Translation/src/DispatchInstructions.cpp | 7 +++- .../Translation/src/FloatInstructions.cpp | 4 +- .../src/TranslationContext/OperandAccess.cpp | 7 ++++ 6 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/DppWrite.cpp diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index e8a37c6c6..9c712a10c 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -163,6 +163,11 @@ target_link_libraries(agc_structurizer_tests PRIVATE shader_recompiler_agc_drive set_target_properties(agc_structurizer_tests PROPERTIES CXX_EXTENSIONS OFF) configure_windows_unwind(agc_structurizer_tests) +add_executable(agc_dpp_write_tests EXCLUDE_FROM_ALL tests/DppWrite.cpp) +target_link_libraries(agc_dpp_write_tests PRIVATE shader_recompiler_agc_driver libc) +set_target_properties(agc_dpp_write_tests PROPERTIES CXX_EXTENSIONS OFF) +configure_windows_unwind(agc_dpp_write_tests) + add_executable(agc_driver_flip_tests EXCLUDE_FROM_ALL tests/Flip.cpp ) @@ -292,7 +297,7 @@ set_target_properties(agc_driver_bda_device_tests PROPERTIES CXX_EXTENSIONS OFF) configure_windows_unwind(agc_driver_bda_device_tests) if(BUILD_TESTING) - foreach(agcTest IN ITEMS agc_driver_tests agc_shader_memory_tests agc_structurizer_tests agc_driver_flip_tests agc_driver_pm4_tests) + foreach(agcTest IN ITEMS agc_driver_tests agc_shader_memory_tests agc_structurizer_tests agc_dpp_write_tests agc_driver_flip_tests agc_driver_pm4_tests) string(REGEX REPLACE "_tests$" "" agcTestName "${agcTest}") set_target_properties(${agcTest} PROPERTIES EXCLUDE_FROM_ALL OFF) add_test(NAME ${agcTestName} COMMAND ${agcTest}) diff --git a/core/libs/prx/libSceAgcDriver/tests/DppWrite.cpp b/core/libs/prx/libSceAgcDriver/tests/DppWrite.cpp new file mode 100644 index 000000000..7955b83f1 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/DppWrite.cpp @@ -0,0 +1,40 @@ +// A DPP instruction's row and bank masks gate its VGPR write, not only its source read: the rows a +// mask leaves out keep their value (a scan adding the other row's total into the odd rows only). +#include "ControlFlow/GraphBuilder.hpp" +#include "ControlFlow/Structurizer.hpp" +#include "RdnaDecoder/RdnaInstructionDecoder.hpp" +#include "Translation/InstructionTranslator.hpp" +#include "Translation/ShaderInputInfoBuilder.hpp" +#include +#include +#include + +using namespace ShaderRecompiler; + +int main() { + // v_add_nc_u32_dpp v1, v0, v1 quad_perm:[0,1,2,3] row_mask:0xa bank_mask:0xf; s_endpgm + const std::vector code{0x4a0202fau, 0xaf00e400u, 0xbf810000u}; + try { + const auto decoded = RdnaInstructionDecoder{}.Decode(code); + auto cfg = GraphBuilder{}.Build(decoded); + Structurizer{}.Structurize(cfg); + GuestContext context{}; + context.waveSize = 32; + context.compute = ShaderComputeStageInfo{{32u, 1u, 1u}, 0u, {false, false, false}, false, 1u}; + TranslateOptions options{}; + options.stage = ShaderStageKind::Compute; + options.waveSize = 32; + options.userDataCount = 0; + options.inputInfo = BuildShaderStageInputInfo(ShaderStageKind::Compute, context, 32); + const auto program = InstructionTranslator{}.Translate(decoded, cfg, options); + for (const auto& block : program.Blocks()) { + for (const auto* inst : block->Instructions()) { + if (inst->Opcode() == IrOpcode::DppUpdateU32 && inst->Flags().rowMask == 0xa) return 0; + } + } + std::fprintf(stderr, "the DPP add writes v1 without its row mask\n"); + } catch (const std::exception& error) { + std::fprintf(stderr, "%s\n", error.what()); + } + return 1; +} diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index eb9c775ac..b69cded08 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -30,6 +30,7 @@ private: const RdnaOperand& sourceAt(const RdnaInstruction& inst, std::uint32_t index); RdnaOperand destinationOperand(const RdnaInstruction& inst); + RdnaOperand accumulatorOperand(const RdnaInstruction& inst); RdnaOperand offsetOperand(const RdnaOperand& operand, std::uint32_t offset); RdnaOperand scalarDestinationOperand(const RdnaOperand& operand, std::uint32_t offset); RdnaOperand plainOperand(const RdnaOperand& operand); diff --git a/core/shader/recompiler/Translation/src/DispatchInstructions.cpp b/core/shader/recompiler/Translation/src/DispatchInstructions.cpp index 66a4ecbb0..f43a2bb10 100644 --- a/core/shader/recompiler/Translation/src/DispatchInstructions.cpp +++ b/core/shader/recompiler/Translation/src/DispatchInstructions.cpp @@ -12,7 +12,12 @@ void DispatchInstruction(IrBuilder& builder, const RdnaInstruction& instruction, throw std::runtime_error("DispatchInstruction not implemented"); } -void TranslationContext::TranslateInstruction(const RdnaInstruction& instruction) { +void TranslationContext::TranslateInstruction(const RdnaInstruction& decoded) { + // DPP masks the instruction's VGPR write as well as its source read (row and bank masks, lanes + // with no source lane without bound_ctrl keep their value): the destination takes the source's + // DPP fields for writeRawU32. + RdnaInstruction instruction = decoded; + instruction.destination = destinationOperand(decoded); currentOpcode = instruction.op; currentProgramCounter = instruction.programCounter; if (instruction.op == RdnaOpcode::Unknown || instruction.op == RdnaOpcode::Count) { diff --git a/core/shader/recompiler/Translation/src/FloatInstructions.cpp b/core/shader/recompiler/Translation/src/FloatInstructions.cpp index b3a9b4677..64ca4ff4e 100644 --- a/core/shader/recompiler/Translation/src/FloatInstructions.cpp +++ b/core/shader/recompiler/Translation/src/FloatInstructions.cpp @@ -100,7 +100,7 @@ bool TranslationContext::float16Binary(const RdnaInstruction& inst, IrOpcode opc bool TranslationContext::float16Ternary(const RdnaInstruction& inst, IrOpcode opcode, bool accumulator, bool mix) { std::array args{}; for (std::uint32_t index = 0u; index < args.size(); ++index) { - const RdnaOperand& operand = accumulator && index == 2u ? inst.destination : sourceAt(inst, index); + const RdnaOperand& operand = accumulator && index == 2u ? accumulatorOperand(inst) : sourceAt(inst, index); args[index] = mix ? &readMixF32(operand).Value() : &readF16AsF32(operand).Value(); } writeF16(inst.destination, IrF32(ir.Emit(opcode, IrType::F32, {args[0], args[1], args[2]}))); @@ -128,7 +128,7 @@ bool TranslationContext::floatBinary(const RdnaInstruction& inst, IrOpcode opcod bool TranslationContext::floatTernary(const RdnaInstruction& inst, IrOpcode opcode, bool accumulator, bool mix) { std::array args{}; for (std::uint32_t index = 0u; index < args.size(); ++index) { - const RdnaOperand& operand = accumulator && index == 2u ? inst.destination : sourceAt(inst, index); + const RdnaOperand& operand = accumulator && index == 2u ? accumulatorOperand(inst) : sourceAt(inst, index); const IrType type = IrOpcodeArgumentType(opcode, index); args[index] = type == IrType::F32 && mix ? &readMixF32(operand).Value() : readOperand(operand, type); } diff --git a/core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp b/core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp index bd1d89b8c..1bda28419 100644 --- a/core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp +++ b/core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp @@ -36,6 +36,13 @@ RdnaOperand TranslationContext::destinationOperand(const RdnaInstruction& inst) return destination; } +// The destination read as a mac accumulator: its own value, which the instruction's DPP does not move. +RdnaOperand TranslationContext::accumulatorOperand(const RdnaInstruction& inst) { + RdnaOperand accumulator = inst.destination; + accumulator.dpp = false; + return accumulator; +} + RdnaOperand TranslationContext::offsetOperand(const RdnaOperand& operand, std::uint32_t offset) { if (offset == 0u) { return operand;