From 984bc57eca0b17564bc3da7c16c638f0ff817217 Mon Sep 17 00:00:00 2001 From: DotDebian Date: Fri, 2 Oct 2026 19:01:36 +0200 Subject: [PATCH 1/3] feat(shader): implement ds_wrxchg2_rtn_b32 and ds_wrxchg2st64_rtn_b32 Both swap data0 with the dword at addr + offset0 and data1 with the dword at addr + offset1, and return the two previous values in vdst and vdst + 1. The offsets are scaled by 4 bytes, or by 256 bytes for the st64 form. Each location goes through the existing SharedAtomicSwap32. An encoding whose two offsets are equal is rejected at decode time. Co-Authored-By: Claude Opus 5.5 --- .../tests/execution/DsWrxchg2.cpp | 158 ++++++++++++++++++ .../include/RdnaDecoder/RdnaOpcode.hpp | 2 + .../RdnaDecoder/src/RdnaMemoryOpDecoder.cpp | 16 +- .../Translation/TranslationContext.hpp | 1 + .../MemoryInstructionDispatch.cpp | 3 + .../SharedMemoryInstructions.cpp | 18 ++ 6 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/DsWrxchg2.cpp diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/DsWrxchg2.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/DsWrxchg2.cpp new file mode 100644 index 000000000..c1ee2ba6a --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/DsWrxchg2.cpp @@ -0,0 +1,158 @@ +#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "Recompiler.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t Threads = 32; +constexpr std::uint32_t Inputs = 4; +constexpr std::uint32_t Results = 16; +constexpr std::uint32_t ActiveLanes = 16; +constexpr std::uint32_t LdsDwords = 256; +alignas(256) std::array Input{}; +alignas(256) std::array Output{}; + +alignas(256) constexpr auto Code = std::to_array({ + 0x34020082, 0x34060084, 0x343c0082, + 0xe0302000, 0x80000401, 0xe0302004, 0x80000501, 0xe0302008, 0x80000601, 0xe030200c, 0x80000701, + 0xbf8c3f70, + 0xd8340000, 0x0000041e, 0xd8340080, 0x0000051e, 0xd8340100, 0x0000051e, 0xd8340300, 0x0000041e, + 0xd8340180, 0x0000051e, 0xd8340280, 0x0000041e, 0xd8340380, 0x0000041e, 0xd8340200, 0x0000051e, + 0xbf8cc07f, + 0x7e1c0306, 0x7e1e0307, 0x7e200306, 0x7e220307, + 0xd8b82000, 0x0a07061e, + 0xd8bc0103, 0x0c06071e, + 0xd8b8a060, 0x0e0f0e1e, + 0xbefe03ff, 0x0000ffff, + 0xd8b880e0, 0x1007061e, + 0xbefe03c1, + 0xbf8cc07f, + 0xd8d80000, 0x1400001e, 0xd8d80080, 0x1500001e, 0xd8d80300, 0x1600001e, 0xd8d80100, 0x1700001e, + 0xd8d80180, 0x1800001e, 0xd8d80280, 0x1900001e, 0xd8d80380, 0x1a00001e, 0xd8d80200, 0x1b00001e, + 0xbf8cc07f, + 0xe0702000, 0x80010a03, 0xe0702004, 0x80010b03, 0xe0702008, 0x80010c03, 0xe070200c, 0x80010d03, + 0xe0702010, 0x80010e03, 0xe0702014, 0x80010f03, 0xe0702018, 0x80011003, 0xe070201c, 0x80011103, + 0xe0702020, 0x80011403, 0xe0702024, 0x80011503, 0xe0702028, 0x80011603, 0xe070202c, 0x80011703, + 0xe0702030, 0x80011803, 0xe0702034, 0x80011903, 0xe0702038, 0x80011a03, 0xe070203c, 0x80011b03, + 0xbf810000, +}); + +alignas(256) constexpr auto CoincidingOffsets = std::to_array({0xd8b80505, 0x0a07061e, 0xbf810000}); +alignas(256) constexpr auto CoincidingOffsetsSt64 = std::to_array({0xd8bc0000, 0x0a07061e, 0xbf810000}); +alignas(256) constexpr auto DestinationOverflow = std::to_array({0xd8b82000, 0xff07061e, 0xbf810000}); + +std::array BufferDescriptor(const void* data, std::uint32_t count) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((address >> 32u) & 0xffffu) | (4u << 16u), count, 0x01016facu}; +} + +std::string Hex(std::uint32_t value) { + char text[16]; + std::snprintf(text, sizeof(text), "0x%08x", value); + return text; +} + +template +void Translate(AgcDriver::VulkanDevice& device, std::span code, TUse&& use) { + std::vector userData(8, 0u); + const auto input = BufferDescriptor(Input.data(), static_cast(Input.size())); + const auto output = BufferDescriptor(Output.data(), static_cast(Output.size())); + std::copy(input.begin(), input.end(), userData.begin()); + std::copy(output.begin(), output.end(), userData.begin() + 4); + const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, LdsDwords, {false, false, false}, false, 1}; + ShaderRecompiler::RecompileRequest request{ + {ShaderStage::Compute, reinterpret_cast(code.data()), code, 0, {}}, + {32, 0, userData, compute, std::nullopt, std::nullopt, memory}, + device.Target(), + {0, 0, 0, 128} + }; + request.useCache = false; + use(ShaderRecompiler::Recompile(request)); +} + +void Run(AgcDriver::VulkanDevice& device) { + for (std::uint32_t tid = 0; tid < Threads; ++tid) { + for (std::uint32_t word = 0; word < Inputs; ++word) { + Input[tid * Inputs + word] = ((word + 1u) << 28u) | (tid * 0x00010203u + word); + } + } + Output.fill(0xdeadbeefu); + Translate(device, Code, [&](const auto& result) { + device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(Code.data())); + device.WaitIdle(); + }); +} + +void Reject(AgcDriver::VulkanDevice& device, std::span code, const std::string& reason) { + std::string error; + try { + Translate(device, code, [](const auto&) {}); + } catch (const std::exception& exception) { + error = exception.what(); + } + Require(error.find(reason) != std::string::npos, "ds wrxchg2: expected a rejection for \"" + reason + "\", got \"" + error + "\""); +} + +void CheckRejections(AgcDriver::VulkanDevice& device) { + Reject(device, CoincidingOffsets, "DS write exchange of one location through both offsets is not supported"); + Reject(device, CoincidingOffsetsSt64, "DS write exchange of one location through both offsets is not supported"); + Reject(device, DestinationOverflow, "DS write exchange destination register range overflow"); +} + +void Check() { + constexpr std::array names{ + "ds_wrxchg2_rtn_b32 offset0 result", "ds_wrxchg2_rtn_b32 offset1 result", + "ds_wrxchg2st64_rtn_b32 offset0 result", "ds_wrxchg2st64_rtn_b32 offset1 result", + "aliased ds_wrxchg2_rtn_b32 offset0 result", "aliased ds_wrxchg2_rtn_b32 offset1 result", + "masked ds_wrxchg2_rtn_b32 offset0 result", "masked ds_wrxchg2_rtn_b32 offset1 result", + "ds_wrxchg2_rtn_b32 offset0 memory", "ds_wrxchg2_rtn_b32 offset1 memory", + "ds_wrxchg2st64_rtn_b32 offset0 memory", "ds_wrxchg2st64_rtn_b32 offset1 memory", + "aliased ds_wrxchg2_rtn_b32 offset0 memory", "aliased ds_wrxchg2_rtn_b32 offset1 memory", + "masked ds_wrxchg2_rtn_b32 offset0 memory", "masked ds_wrxchg2_rtn_b32 offset1 memory", + }; + for (std::uint32_t tid = 0; tid < Threads; ++tid) { + const std::uint32_t a = Input[tid * Inputs]; + const std::uint32_t b = Input[tid * Inputs + 1]; + const std::uint32_t c = Input[tid * Inputs + 2]; + const std::uint32_t d = Input[tid * Inputs + 3]; + const bool active = tid < ActiveLanes; + const std::array expected{ + a, b, a, b, b, a, active ? a : c, active ? b : d, + c, d, d, c, c, d, active ? c : a, active ? d : b, + }; + for (std::uint32_t j = 0; j < Results; ++j) { + const auto actual = Output[tid * Results + j]; + Require(actual == expected[j], std::string("ds wrxchg2: lane ") + std::to_string(tid) + " " + names[j] + " is " + Hex(actual) + ", expected " + Hex(expected[j])); + } + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + Run(*device); + Check(); + CheckRejections(*device); + std::puts("ds wrxchg2 tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp index ef4daa6c7..0a267265f 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -858,6 +858,8 @@ enum class RdnaOpcode : std::uint16_t { DsXorB32, DsXorRtnB32, DsWrxchgRtnB32, + DsWrxchg2RtnB32, + DsWrxchg2st64RtnB32, DsMinF32, DsMaxF32, DsSwizzleB32, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp index a1a6484f4..f8c180d05 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp @@ -304,6 +304,8 @@ constexpr MemoryOpcodeInfo dsOpcodes[] = { {0x2au, RdnaOpcode::DsOrRtnB32, 1, 32, false, false, false}, {0x2bu, RdnaOpcode::DsXorRtnB32, 1, 32, false, false, false}, {0x2du, RdnaOpcode::DsWrxchgRtnB32, 1, 32, false, false, false}, + {0x2eu, RdnaOpcode::DsWrxchg2RtnB32, 2, 32, false, false, false}, + {0x2fu, RdnaOpcode::DsWrxchg2st64RtnB32, 2, 32, false, false, false}, {0x35u, RdnaOpcode::DsSwizzleB32, 1, 32, false, false, false}, {0x36u, RdnaOpcode::DsReadB32, 1, 32, false, false, false}, {0x37u, RdnaOpcode::DsRead2B32, 2, 32, false, false, false}, @@ -531,6 +533,8 @@ bool isDsAtomicOpcode(RdnaOpcode opcode) { std::uint32_t dsSourceCount(RdnaOpcode opcode) { switch (opcode) { + case RdnaOpcode::DsWrxchg2RtnB32: + case RdnaOpcode::DsWrxchg2st64RtnB32: case RdnaOpcode::DsWrite2B32: case RdnaOpcode::DsWrite2st64B32: case RdnaOpcode::DsWrite2B64: @@ -899,6 +903,14 @@ RdnaInstruction DecodeRdnaDs(std::uint32_t programCounter, std::span Date: Fri, 2 Oct 2026 19:11:03 +0200 Subject: [PATCH 2/3] test(shader): mask the ds_wrxchg2 lanes with a per-lane compare The masked case loaded EXEC from a scalar literal. A scalar mask is read by subgroup lane, so on a host whose subgroups are narrower than the wave (llvmpipe: 8 lanes) lane 16 took bit 0 and stayed active. v_cmpx_gt_u32 on the thread id gives the same low 16 lanes on any subgroup size. Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libSceAgcDriver/tests/execution/DsWrxchg2.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/DsWrxchg2.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/DsWrxchg2.cpp index c1ee2ba6a..9da6c4caf 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/DsWrxchg2.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/DsWrxchg2.cpp @@ -35,7 +35,7 @@ alignas(256) constexpr auto Code = std::to_array({ 0xd8b82000, 0x0a07061e, 0xd8bc0103, 0x0c06071e, 0xd8b8a060, 0x0e0f0e1e, - 0xbefe03ff, 0x0000ffff, + 0x7da80090, 0xd8b880e0, 0x1007061e, 0xbefe03c1, 0xbf8cc07f, From 12008dc2ba01774808f2c479bbe068818cb32a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Franch?= Date: Tue, 6 Oct 2026 22:46:40 +0200 Subject: [PATCH 3/3] docs(technical-debt): note the ds_wrxchg2 per-lane exchange order gap --- docs/dev/TechnicalDebt.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 645d976a4..34022a21f 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -139,6 +139,7 @@ Throughout the project, every function at every stage either **does exactly what - [libSceUlt queues](../../core/libs/prx/libSceUlt/Export.cpp) use preallocated host storage instead of the supplied guest work area; its existing size formula and the 512-byte object layouts remain unverified. Waiting-queue pool thread limits and waiter priority ordering are not implemented. Finalization wakes blocked queue callers with `ULT_ERROR_STATE`. - [MIMG `tfe` and `lwe`](../../core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp) never write the status VGPR after the data. On hardware it is written only when a texel fetch hits an unmapped page of a partially resident texture, or, for `lwe` on sample/gather, when the LOD is below the T# `MIN_LOD_WARN`; texture memory is always resident here, and `lwe` on sample, gather and `image_get_lod` throws. - [Image atomics](../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) on 32_SINT and 32_FLOAT surfaces act on the raw dword through an R32_UINT storage view; their float results are assumed to match those measured on 32_UINT surfaces. Other formats throw. +- [`ds_wrxchg2_rtn_b32` and `ds_wrxchg2st64_rtn_b32`](../../core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp) are translated as two separate 32-bit atomic swaps per invocation. RDNA2 hardware (measured on gfx1035) performs every lane's first exchange before any lane's second one, so when lanes of the same wave or of different waves overlap addresses, the result can differ from hardware. `offset0 == offset1` throws, although on hardware it is two exchanges in order. - The shader recompiler [ignores `s_setreg_b32`](../../core/shader/recompiler/Translation/src/ScalarInstructions.cpp): writes to MODE (float rounding and denormal controls) and the other hardware registers have no effect, and `s_getreg_b32` reads of the MODE round fields report round to nearest even even after `s_setreg_b32` wrote another mode. The initial round mode from the shader's program registers is not read either. - On a host subgroup narrower than 32 lanes, [`threadBit`](../../core/shader/recompiler/Translation/src/TranslationContext/ExecMask.cpp) reads an EXEC written as a value (`s_mov_b32`/`s_mov_b64`) at bit (subgroup invocation id) of EXEC_LO, so lanes at or above the subgroup size read a lower lane's bit. - [Shader recompilation](../../core/shader/recompiler/Recompiler.cpp) currently occurs right before it was transferred to Vulkan with caching, but should be moved to the [relinker](../../core/relinker/main.cpp) stage. For this purpose, [shader/recompiler](../../core/shader/recompiler) was written completely independently from [libs/prx](../../core/libs/prx).