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 <noreply@anthropic.com>
This commit is contained in:
STL1te
2026-09-29 14:57:38 +03:00
parent f98b28d0d7
commit d76ecfac06
6 changed files with 62 additions and 4 deletions
+6 -1
View File
@@ -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})
@@ -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 <cstdio>
#include <exception>
#include <vector>
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<std::uint32_t> 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<DppMoveFlags>().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;
}
@@ -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);
@@ -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) {
@@ -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<IrValue*, 3> 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<IrValue*, 3> 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);
}
@@ -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;