From 36b17177e0112e0f11009d6996fd61931fdd5ebe Mon Sep 17 00:00:00 2001 From: Sanjaysrinivas Date: Wed, 7 Oct 2026 18:35:29 +0200 Subject: [PATCH 1/3] feat(shader): add LDS conditional exchange --- .../tests/execution/LdsCondxchg.cpp | 174 ++++++++++++++++++ .../include/RdnaDecoder/RdnaOpcode.hpp | 1 + .../RdnaDecoder/src/RdnaMemoryOpDecoder.cpp | 10 + .../Translation/TranslationContext.hpp | 1 + .../MemoryInstructionDispatch.cpp | 2 + .../SharedMemoryInstructions.cpp | 23 +++ docs/dev/TechnicalDebt.md | 1 + 7 files changed, 212 insertions(+) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/LdsCondxchg.cpp diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/LdsCondxchg.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/LdsCondxchg.cpp new file mode 100644 index 000000000..407bf68ff --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/LdsCondxchg.cpp @@ -0,0 +1,174 @@ +#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 MaxThreads = 64; +constexpr std::uint32_t Words = 8; +constexpr std::uint32_t LdsDwords = 4096; +constexpr std::uint32_t Sign = 0x80000000u; +constexpr std::uint32_t Sentinel = 0xdeadbeefu; +alignas(256) std::array Input{}; +alignas(256) std::array Output{}; + +auto Code(std::uint32_t waveSize, std::uint32_t offset, std::uint32_t destination, bool inactive, bool contended) { + const std::uint32_t saveExec = waveSize == 32u ? 0xbe943c6au : 0xbe94246au; + const std::uint32_t restoreExec = waveSize == 32u ? 0xbefe03c1u : 0xbefe04c1u; + return std::to_array({ + 0x34020085u, 0xe0381000u, 0x80000201u, 0xe0381010u, 0x80000601u, 0xbf8c3f70u, + 0x7e160304u, 0x7e180305u, 0x7e1a02ffu, Sentinel, 0x7e140280u, + contended ? 0x7d840080u : 0x7d841480u, saveExec, + 0xd9340000u, 0x00000206u, 0xd9340008u, 0x00000806u, 0xd9340010u, 0x00000806u, + 0xbf8cc07fu, restoreExec, 0xbf8a0000u, + inactive ? 0x36140081u : 0x36140080u, 0x7d841480u, saveExec, + 0xd9f80000u | offset, (destination << 24u) | 0x00000b07u, + 0xbf8cc07fu, restoreExec, 0xbf8a0000u, + 0x7e280300u | destination, 0x7e2a0300u | (destination + 1u), + 0xd9d80000u, 0x16000006u, 0xd9d80008u, 0x18000006u, 0xd9d80010u, 0x1a000006u, + 0xbf8cc07fu, 0xe0781000u, 0x80011401u, 0xe0781010u, 0x80011801u, 0xbf810000u, + }); +} + +std::array BufferDescriptor(const void* data, std::uint32_t bytes) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((address >> 32u) & 0xffffu), bytes, 0x01016facu}; +} + +template +void Translate(std::span code, std::uint32_t waveSize, const ShaderRecompiler::SpirvTarget& target, TUse&& use) { + std::vector userData(8, 0u); + const auto input = BufferDescriptor(Input.data(), static_cast(Input.size() * 4u)); + const auto output = BufferDescriptor(Output.data(), static_cast(Output.size() * 4u)); + 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{{waveSize, 1, 1}, LdsDwords, {false, false, false}, false, 1}; + ShaderRecompiler::RecompileRequest request{ + {ShaderStage::Compute, reinterpret_cast(code.data()), code, 0, {}}, + {waveSize, 0, userData, compute, std::nullopt, std::nullopt, memory}, + target, + {0, 0, 0, 128} + }; + request.useCache = false; + use(ShaderRecompiler::Recompile(request)); +} + +void Expect(std::uint32_t tid, std::uint32_t index, std::uint32_t expected) { + const auto actual = Output[tid * Words + index]; + Require(actual == expected, "lds condxchg: lane " + std::to_string(tid) + " word " + std::to_string(index) + " is " + std::to_string(actual) + ", expected " + std::to_string(expected)); +} + +template +void ExpectError(TCheck&& check, const std::string& reason) { + std::string error; + try { + check(); + } catch (const std::exception& exception) { + error = exception.what(); + } + Require(error.find(reason) != std::string::npos, "lds condxchg: expected \"" + reason + "\", got \"" + error + "\""); +} + +void CheckRejections(AgcDriver::VulkanDevice& device) { + const auto translate = [&](std::span code) { Translate(code, 32u, device.Target(), [](const auto&) {}); }; + constexpr std::array boundary{0xd9f80000u, 0xfe00fe00u, 0xbf810000u}; + translate(boundary); + ExpectError([&] { translate(std::span(boundary).first(1u)); }, "truncated DS instruction"); + constexpr std::array sourceOverflow{0xd9f80000u, 0x0200ff00u, 0xbf810000u}; + constexpr std::array destinationOverflow{0xd9f80000u, 0xff000400u, 0xbf810000u}; + constexpr std::array gds{0xd9fa0000u, 0x02000400u, 0xbf810000u}; + ExpectError([&] { translate(sourceOverflow); }, "DS conditional exchange source register range overflow"); + ExpectError([&] { translate(destinationOverflow); }, "DS conditional exchange destination register range overflow"); + ExpectError([&] { translate(gds); }, "DS conditional exchange GDS mode is not supported"); +} + +void Fill(std::uint32_t threads, std::uint32_t offset, bool contended) { + for (std::uint32_t tid = 0u; tid < threads; ++tid) { + auto* in = &Input[tid * Words]; + const auto seed = (tid + 1u) * 0x9e3779b97f4a7c15ull; + in[0] = contended ? 0xffffffffu : static_cast(seed); + in[1] = contended ? 0xfffffffeu : static_cast(seed >> 32u); + in[2] = contended ? Sign | (tid + 1u) : ((tid & 1u) != 0u ? Sign : 0u) | ((tid / 4u) % 2u != 0u ? 0x7fffffffu : 0u); + in[3] = contended ? Sign | (tid + 1u) : ((tid & 2u) != 0u ? Sign : 0u) | ((tid / 8u) % 2u != 0u ? 0x13579bdfu : 0u); + in[4] = contended ? 0u : LdsDwords * 4u - 24u - tid * 32u; + in[5] = in[4] - offset + (tid & 7u) + ((tid & 8u) != 0u ? 0u : 0x10000u); + in[6] = 0xa5a5a5a5u; + in[7] = 0x5a5a5a5au; + } +} + +void CheckContention(std::uint32_t threads) { + for (std::uint32_t word = 0u; word < 2u; ++word) { + const auto finalValue = Output[2u + word]; + Require(finalValue >= 1u && finalValue <= threads, "lds condxchg: invalid final contended dword"); + std::array seen{}; + for (std::uint32_t tid = 0u; tid < threads; ++tid) { + Expect(tid, 2u + word, finalValue); + const auto old = Output[tid * Words + word]; + const auto index = old == Input[word] ? 0u : old; + Require(index <= threads && index != tid + 1u && !seen[index], "lds condxchg: invalid or duplicate returned contended dword"); + seen[index] = true; + } + Require(seen[0] && !seen[finalValue], "lds condxchg: contended dword returns lost an exchange"); + } +} + +void Run(AgcDriver::VulkanDevice& device, std::uint32_t waveSize, const ShaderRecompiler::SpirvTarget& target, std::uint32_t offset, std::uint32_t destination, bool inactive, bool contended) { + Fill(waveSize, offset, contended); + Output.fill(Sentinel); + alignas(256) const auto code = Code(waveSize, offset, destination, inactive, contended); + Translate(code, waveSize, target, [&](const auto& result) { + device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device.WaitIdle(); + }); + for (std::uint32_t tid = 0u; tid < waveSize; ++tid) { + const auto* in = &Input[tid * Words]; + if (!contended) { + const bool active = !inactive || tid % 2u == 0u; + Expect(tid, 0u, active ? in[0] : destination == 12u ? in[3] : in[5]); + Expect(tid, 1u, active ? in[1] : destination == 12u ? Sentinel : in[6]); + Expect(tid, 2u, active && (in[2] & Sign) != 0u ? in[2] & ~Sign : in[0]); + Expect(tid, 3u, active && (in[3] & Sign) != 0u ? in[3] & ~Sign : in[1]); + } + for (std::uint32_t index = 4u; index < Words; ++index) Expect(tid, index, in[6u + index % 2u]); + } + if (contended) CheckContention(waveSize); +} + +void CheckExecution(AgcDriver::VulkanDevice& device, std::uint32_t waveSize, const ShaderRecompiler::SpirvTarget& target) { + for (const auto offset : {0u, 0x123u, 0xffffu}) Run(device, waveSize, target, offset, 12u, false, false); + Run(device, waveSize, target, 0x123u, 12u, true, false); + Run(device, waveSize, target, 0xffffu, 7u, true, false); + Run(device, waveSize, target, 0x123u, 12u, false, true); +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + CheckRejections(*device); + CheckExecution(*device, 32u, device->Target()); + CheckExecution(*device, 64u, device->Target()); + CheckExecution(*device, 64u, device->ComputeTarget(32u)); + std::puts("lds condxchg 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 9cc00be2f..9e9fba1df 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -932,6 +932,7 @@ enum class RdnaOpcode : std::uint16_t { DsXorRtnB64, DsMskorRtnB64, DsWrxchgRtnB64, + DsCondxchg32RtnB64, DsCmpstRtnB64, DsCmpstRtnF64, DsMinRtnF64, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp index 3f155040a..17c93986a 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp @@ -324,6 +324,7 @@ constexpr MemoryOpcodeInfo dsOpcodes[] = { {0x76u, RdnaOpcode::DsReadB64, 2, 32, false, false, false}, {0x77u, RdnaOpcode::DsRead2B64, 4, 32, false, false, false}, {0x78u, RdnaOpcode::DsRead2st64B64, 4, 32, false, false, false}, + {0x7eu, RdnaOpcode::DsCondxchg32RtnB64, 2, 32, false, false, false}, {0xa0u, RdnaOpcode::DsWriteB8D16Hi, 1, 8, false, false, false}, {0xa1u, RdnaOpcode::DsWriteB16D16Hi, 1, 16, false, false, false}, {0xa2u, RdnaOpcode::DsReadU8D16, 1, 8, false, false, false}, @@ -526,6 +527,7 @@ bool isDsAtomicOpcode(RdnaOpcode opcode) { case RdnaOpcode::DsOrRtnB64: case RdnaOpcode::DsXorRtnB64: case RdnaOpcode::DsWrxchgRtnB64: + case RdnaOpcode::DsCondxchg32RtnB64: case RdnaOpcode::DsMinRtnF64: case RdnaOpcode::DsMaxRtnF64: return true; @@ -918,6 +920,14 @@ RdnaInstruction DecodeRdnaDs(std::uint32_t programCounter, std::span 256u) { + throw std::runtime_error("DS conditional exchange source register range overflow"); + } + if (vdst + 2u > 256u) { + throw std::runtime_error("DS conditional exchange destination register range overflow"); + } + } if (info.opcode == RdnaOpcode::DsReadAddtidB32 && (data0 != 0u || data1 != 0u)) { throw std::runtime_error("DS read addtid data operands are not supported"); } diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index d87a7e86e..90f9ca252 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -116,6 +116,7 @@ private: bool dsWrite2(const RdnaInstruction& inst); bool dsAtomic2(const RdnaInstruction& inst, IrOpcode opcode, bool returnsValue); bool dsAtomic64(const RdnaInstruction& inst, IrOpcode opcode, bool returnsValue); + bool dsCondxchg32(const RdnaInstruction& inst); bool dsAppendConsume(const RdnaInstruction& inst, IrOpcode opcode); bool dsAddtid(const RdnaInstruction& inst, bool write); bool globalAddtid(const RdnaInstruction& inst, bool write); diff --git a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp index 593cf1c43..c7092fa05 100644 --- a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp +++ b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp @@ -430,6 +430,8 @@ bool TranslationContext::emitMemory(const RdnaInstruction& inst) { return dsAtomic64(inst, IrOpcode::SharedAtomicFMax64, true); case RdnaOpcode::DsWrxchgRtnB64: return dsAtomic64(inst, IrOpcode::SharedAtomicSwap64, true); + case RdnaOpcode::DsCondxchg32RtnB64: + return dsCondxchg32(inst); case RdnaOpcode::DsNop: emitControlNop(); return true; diff --git a/core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp b/core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp index 47327d850..3c00ca123 100644 --- a/core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp +++ b/core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp @@ -224,6 +224,29 @@ bool TranslationContext::dsAtomic64(const RdnaInstruction& inst, IrOpcode opcode return true; } +bool TranslationContext::dsCondxchg32(const RdnaInstruction& inst) { + if (inst.gds) { + throw std::runtime_error("DS conditional exchange GDS mode is not supported"); + } + MemoryInfo memory = sharedMemoryInfoFromInstruction(inst); + memory.dataDwords = 1u; + memory.componentCount = 1u; + const auto data = readU32Pair(inst.source1); + IrValue& address = ir.BitwiseAnd(ir.IAdd(readU32(inst.source0).Value(), ir.Constant(inst.memoryOffset)), ir.Constant(0xfff8u)); + IrValue& active = ir.GetExec(); + std::array old{}; + for (std::uint32_t index = 0u; index < 2u; ++index) { + IrValue& enabled = ir.UGreaterThan(data[index].Value(), ir.Constant(0x7fffffffu)); + IrValue& mask = ir.Select(enabled, ir.Constant(0xffffffffu), ir.Constant(0u)); + IrValue& value = ir.Select(enabled, ir.BitwiseAnd(data[index].Value(), ir.Constant(0x7fffffffu)), ir.Constant(0u)); + memory.offset = index * 4u; + old[index] = &ir.Emit(IrOpcode::SharedAtomicMskor32, IrType::U32, {&address, &mask, &value, &active}, addMemoryInfo(memory, inst.programCounter)); + } + writeOperand(inst.destination, old[0]); + writeOperand(offsetOperand(inst.destination, 1u), old[1]); + return true; +} + bool TranslationContext::dsAppendConsume(const RdnaInstruction& inst, IrOpcode opcode) { const MemoryInfo memory = sharedMemoryInfoFromInstruction(inst); const IrU32 address = readU32(makeM0Operand()); diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 61a82baf3..5cabf104a 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -257,6 +257,7 @@ Throughout the project, every function at every stage either **does exactly what - [Converted unorm images](../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) (10_11_11_UNORM, read and written through an R32_UINT view): samples, gathers, LOD queries, 16-bit data and a DST_SEL of the missing W channel throw. - [Converted unorm image loads](../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) divide by 2^n - 1 with the buffer format path's `OpFDiv`, which Vulkan allows to be up to 2.5 ULP off, so some drivers are not bit-exact. - [64-bit LDS atomics](../../core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp) (`ds_*_u64`, `ds_*_b64`, `ds_*_f64`) read and write the two dwords under a workgroup lock stored after the guest LDS, as LDS is a 32-bit array without 64-bit atomics. They are atomic with respect to each other only: 32-bit accesses to the same dwords issued concurrently are not ordered by the lock. 64-bit GDS atomics throw. `ds_min/max(_rtn)_f64` and `ds_cmpst(_rtn)_f64` follow the NaN and signed-zero rules measured for the f32 forms; the f64 forms were not measured on hardware. +- [`ds_condxchg32_rtn_b64`](../../core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp) uses two 32-bit atomic conditional exchanges: the address mask and bit clearing follow [RDNA3 ISA](https://gpuopen.com/download/rdna3-shader-instruction-set-architecture-feb-2023.pdf), section 16.15, page 530, and the access width follows the 32-bit atomic classification in [RDNA4 ISA](https://gpuopen.com/download/rdna4-instruction-set-architecture.pdf), section 12.5.1, pages 152-153. These details and contended lane ordering were not measured on RDNA2 or the console; pair-wide indivisibility is not assumed, and concurrent 64-bit LDS atomics use a separate lock. GDS throws. VGPR pairs extending beyond v255 throw; allocated-VGPR EXEC nullification is not emulated. - [Min/max S# reduction](../../core/libs/prx/libSceAgcDriver/Graphics/src/GuestSamplerResource.cpp) (libSceAgcDriver) - `FILTER_MODE` 1 and 2 use Vulkan's min and max reduction (the same `SQ_IMG_FILTER_MODE` values RADV programs). Measured on RDNA2 (gfx1035), not on the console: only texels of non-zero weight are reduced, and the weight is taken after rounding to the 8-bit subtexel grid (a sample within 1/512 texel of a centre reduces that texel alone); a driver with other `subTexelPrecisionBits` reduces a different set there. Gathers ignore the mode. - [Min/max S#](../../core/libs/prx/libSceAgcDriver/Graphics/src/GuestSamplerResource.cpp) (libSceAgcDriver, shader recompiler) - throws with anisotropy, a linear mip filter, depth or color-texture compare, or bilinear filtering of an image the recompiler point-samples (sint, converted, depth-bits). - [Raw `buffer_load_dwordx2`](../../core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp) is one 64-bit access on the hardware (with or without glc), so it never sees half of a `buffer_atomic_swap_x2`. Only a glc load that is 8-byte aligned and in bounds, in a program that has the 64-bit storage view (it uses 64-bit buffer atomics), is read with one 64-bit atomic load; every other dwordx2 load is two dword loads and can tear against a concurrent 64-bit write: loads without glc, and loads in a program without 64-bit buffer atomics while another shader writes the pair with `swap_x2`. From dbf10824a76002ce56fdd564c83bab06555757d1 Mon Sep 17 00:00:00 2001 From: Sanjaysrinivas Date: Wed, 7 Oct 2026 22:06:03 +0200 Subject: [PATCH 2/3] docs(shader): avoid src2 PR overlap --- docs/dev/TechnicalDebt.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 5cabf104a..a443cecef 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -215,6 +215,7 @@ Throughout the project, every function at every stage either **does exactly what - An [unnormalized S#](../../core/libs/prx/libSceAgcDriver/Graphics/src/GuestSamplerResource.cpp) (libSceAgcDriver) throws with unequal minification and magnification filters, anisotropy, `TRUNC_COORD`, `MC_COORD_TRUNC` or X/Y clamping other than last-texel or border. - [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`, `ds_wrxchg2st64_rtn_b32`, `ds_wrxchg2_rtn_b64` and `ds_wrxchg2st64_rtn_b64`](../../core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp) are translated as two separate 32-bit or 64-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. +- [`ds_condxchg32_rtn_b64`](../../core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp) uses two 32-bit atomic conditional exchanges: the address mask and bit clearing follow [RDNA3 ISA](https://gpuopen.com/download/rdna3-shader-instruction-set-architecture-feb-2023.pdf), section 16.15, page 530, and the access width follows the 32-bit atomic classification in [RDNA4 ISA](https://gpuopen.com/download/rdna4-instruction-set-architecture.pdf), section 12.5.1, pages 152-153. These details and contended lane ordering were not measured on RDNA2 or the console; pair-wide indivisibility is not assumed, and concurrent 64-bit LDS atomics use a separate lock. GDS throws. VGPR pairs extending beyond v255 throw; allocated-VGPR EXEC nullification is not emulated. - [64-bit image atomics](../../core/shader/recompiler/Translation/src/MemoryInstructions/ImageInstructions.cpp) (`image_atomic_*` with a two-dword data mask) need `VK_EXT_shader_image_atomic_int64` and act on 32_32 UINT, SINT and FLOAT surfaces through an R64_UINT storage view. 64-bit `image_atomic_inc`, `image_atomic_dec` and the float atomics throw, as does any other access in the same shader to an image it updates with 64-bit atomics. - [8_SRGB and 8_8_SRGB loads](../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) on a device that cannot sample R8_SRGB or R8G8_SRGB return the sRGB EOTF rounded to nearest even at bf16 precision, as adriadam10 measured on RDNA2 (gfx1035) in #866; the PS5's gfx1013 is not measured. - [8_SRGB and 8_8_SRGB textures](../../core/libs/prx/libSceAgcDriver/Graphics/src/TextureFormat.cpp) (libSceAgcDriver) that the device samples as R8_SRGB or R8G8_SRGB use its own decode, which need not match gfx1035: NVIDIA's R8_SRGB is one bf16 step off in 29 of 256 codes. @@ -257,7 +258,6 @@ Throughout the project, every function at every stage either **does exactly what - [Converted unorm images](../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) (10_11_11_UNORM, read and written through an R32_UINT view): samples, gathers, LOD queries, 16-bit data and a DST_SEL of the missing W channel throw. - [Converted unorm image loads](../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) divide by 2^n - 1 with the buffer format path's `OpFDiv`, which Vulkan allows to be up to 2.5 ULP off, so some drivers are not bit-exact. - [64-bit LDS atomics](../../core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp) (`ds_*_u64`, `ds_*_b64`, `ds_*_f64`) read and write the two dwords under a workgroup lock stored after the guest LDS, as LDS is a 32-bit array without 64-bit atomics. They are atomic with respect to each other only: 32-bit accesses to the same dwords issued concurrently are not ordered by the lock. 64-bit GDS atomics throw. `ds_min/max(_rtn)_f64` and `ds_cmpst(_rtn)_f64` follow the NaN and signed-zero rules measured for the f32 forms; the f64 forms were not measured on hardware. -- [`ds_condxchg32_rtn_b64`](../../core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp) uses two 32-bit atomic conditional exchanges: the address mask and bit clearing follow [RDNA3 ISA](https://gpuopen.com/download/rdna3-shader-instruction-set-architecture-feb-2023.pdf), section 16.15, page 530, and the access width follows the 32-bit atomic classification in [RDNA4 ISA](https://gpuopen.com/download/rdna4-instruction-set-architecture.pdf), section 12.5.1, pages 152-153. These details and contended lane ordering were not measured on RDNA2 or the console; pair-wide indivisibility is not assumed, and concurrent 64-bit LDS atomics use a separate lock. GDS throws. VGPR pairs extending beyond v255 throw; allocated-VGPR EXEC nullification is not emulated. - [Min/max S# reduction](../../core/libs/prx/libSceAgcDriver/Graphics/src/GuestSamplerResource.cpp) (libSceAgcDriver) - `FILTER_MODE` 1 and 2 use Vulkan's min and max reduction (the same `SQ_IMG_FILTER_MODE` values RADV programs). Measured on RDNA2 (gfx1035), not on the console: only texels of non-zero weight are reduced, and the weight is taken after rounding to the 8-bit subtexel grid (a sample within 1/512 texel of a centre reduces that texel alone); a driver with other `subTexelPrecisionBits` reduces a different set there. Gathers ignore the mode. - [Min/max S#](../../core/libs/prx/libSceAgcDriver/Graphics/src/GuestSamplerResource.cpp) (libSceAgcDriver, shader recompiler) - throws with anisotropy, a linear mip filter, depth or color-texture compare, or bilinear filtering of an image the recompiler point-samples (sint, converted, depth-bits). - [Raw `buffer_load_dwordx2`](../../core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp) is one 64-bit access on the hardware (with or without glc), so it never sees half of a `buffer_atomic_swap_x2`. Only a glc load that is 8-byte aligned and in bounds, in a program that has the 64-bit storage view (it uses 64-bit buffer atomics), is read with one 64-bit atomic load; every other dwordx2 load is two dword loads and can tear against a concurrent 64-bit write: loads without glc, and loads in a program without 64-bit buffer atomics while another shader writes the pair with `swap_x2`. From 34d4cf1796054a31feb7ced250bea8cbb8f66058 Mon Sep 17 00:00:00 2001 From: Sanjaysrinivas Date: Thu, 8 Oct 2026 11:04:26 +0200 Subject: [PATCH 3/3] fix(shader): preserve LDS conditional addresses Use the shared atomic bounds check instead of wrapping at 64 KiB. Expect zero returns and unchanged LDS for aligned out-of-range accesses, and record the gfx1035 hardware measurements. --- .../tests/execution/LdsCondxchg.cpp | 22 ++++++++++--------- .../SharedMemoryInstructions.cpp | 6 ++--- docs/dev/TechnicalDebt.md | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/LdsCondxchg.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/LdsCondxchg.cpp index 407bf68ff..154c1b9bc 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/LdsCondxchg.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/LdsCondxchg.cpp @@ -103,8 +103,9 @@ void Fill(std::uint32_t threads, std::uint32_t offset, bool contended) { in[1] = contended ? 0xfffffffeu : static_cast(seed >> 32u); in[2] = contended ? Sign | (tid + 1u) : ((tid & 1u) != 0u ? Sign : 0u) | ((tid / 4u) % 2u != 0u ? 0x7fffffffu : 0u); in[3] = contended ? Sign | (tid + 1u) : ((tid & 2u) != 0u ? Sign : 0u) | ((tid / 8u) % 2u != 0u ? 0x13579bdfu : 0u); - in[4] = contended ? 0u : LdsDwords * 4u - 24u - tid * 32u; - in[5] = in[4] - offset + (tid & 7u) + ((tid & 8u) != 0u ? 0u : 0x10000u); + const bool outOfRange = !contended && ((tid & 8u) == 0u || offset >= LdsDwords * 4u); + in[4] = contended ? 0u : outOfRange ? tid * 32u : LdsDwords * 4u - 24u - tid * 32u; + in[5] = in[4] + (outOfRange ? 0x10000u : 0u) - offset; in[6] = 0xa5a5a5a5u; in[7] = 0x5a5a5a5au; } @@ -138,10 +139,11 @@ void Run(AgcDriver::VulkanDevice& device, std::uint32_t waveSize, const ShaderRe const auto* in = &Input[tid * Words]; if (!contended) { const bool active = !inactive || tid % 2u == 0u; - Expect(tid, 0u, active ? in[0] : destination == 12u ? in[3] : in[5]); - Expect(tid, 1u, active ? in[1] : destination == 12u ? Sentinel : in[6]); - Expect(tid, 2u, active && (in[2] & Sign) != 0u ? in[2] & ~Sign : in[0]); - Expect(tid, 3u, active && (in[3] & Sign) != 0u ? in[3] & ~Sign : in[1]); + const bool inBounds = in[5] + offset < LdsDwords * 4u; + Expect(tid, 0u, active ? inBounds ? in[0] : 0u : destination == 12u ? in[3] : in[5]); + Expect(tid, 1u, active ? inBounds ? in[1] : 0u : destination == 12u ? Sentinel : in[6]); + Expect(tid, 2u, active && inBounds && (in[2] & Sign) != 0u ? in[2] & ~Sign : in[0]); + Expect(tid, 3u, active && inBounds && (in[3] & Sign) != 0u ? in[3] & ~Sign : in[1]); } for (std::uint32_t index = 4u; index < Words; ++index) Expect(tid, index, in[6u + index % 2u]); } @@ -149,10 +151,10 @@ void Run(AgcDriver::VulkanDevice& device, std::uint32_t waveSize, const ShaderRe } void CheckExecution(AgcDriver::VulkanDevice& device, std::uint32_t waveSize, const ShaderRecompiler::SpirvTarget& target) { - for (const auto offset : {0u, 0x123u, 0xffffu}) Run(device, waveSize, target, offset, 12u, false, false); - Run(device, waveSize, target, 0x123u, 12u, true, false); - Run(device, waveSize, target, 0xffffu, 7u, true, false); - Run(device, waveSize, target, 0x123u, 12u, false, true); + for (const auto offset : {0u, 0x120u, 0xfff8u}) Run(device, waveSize, target, offset, 12u, false, false); + Run(device, waveSize, target, 0x120u, 12u, true, false); + Run(device, waveSize, target, 0xfff8u, 7u, true, false); + Run(device, waveSize, target, 0x120u, 12u, false, true); } } diff --git a/core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp b/core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp index 3c00ca123..0f6d94a9c 100644 --- a/core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp +++ b/core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp @@ -232,15 +232,15 @@ bool TranslationContext::dsCondxchg32(const RdnaInstruction& inst) { memory.dataDwords = 1u; memory.componentCount = 1u; const auto data = readU32Pair(inst.source1); - IrValue& address = ir.BitwiseAnd(ir.IAdd(readU32(inst.source0).Value(), ir.Constant(inst.memoryOffset)), ir.Constant(0xfff8u)); + const IrU32 address = readU32(inst.source0); IrValue& active = ir.GetExec(); std::array old{}; for (std::uint32_t index = 0u; index < 2u; ++index) { IrValue& enabled = ir.UGreaterThan(data[index].Value(), ir.Constant(0x7fffffffu)); IrValue& mask = ir.Select(enabled, ir.Constant(0xffffffffu), ir.Constant(0u)); IrValue& value = ir.Select(enabled, ir.BitwiseAnd(data[index].Value(), ir.Constant(0x7fffffffu)), ir.Constant(0u)); - memory.offset = index * 4u; - old[index] = &ir.Emit(IrOpcode::SharedAtomicMskor32, IrType::U32, {&address, &mask, &value, &active}, addMemoryInfo(memory, inst.programCounter)); + memory.offset = inst.memoryOffset + index * 4u; + old[index] = &ir.Emit(IrOpcode::SharedAtomicMskor32, IrType::U32, {&address.Value(), &mask, &value, &active}, addMemoryInfo(memory, inst.programCounter)); } writeOperand(inst.destination, old[0]); writeOperand(offsetOperand(inst.destination, 1u), old[1]); diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index a443cecef..f634177b2 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -215,7 +215,7 @@ Throughout the project, every function at every stage either **does exactly what - An [unnormalized S#](../../core/libs/prx/libSceAgcDriver/Graphics/src/GuestSamplerResource.cpp) (libSceAgcDriver) throws with unequal minification and magnification filters, anisotropy, `TRUNC_COORD`, `MC_COORD_TRUNC` or X/Y clamping other than last-texel or border. - [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`, `ds_wrxchg2st64_rtn_b32`, `ds_wrxchg2_rtn_b64` and `ds_wrxchg2st64_rtn_b64`](../../core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp) are translated as two separate 32-bit or 64-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. -- [`ds_condxchg32_rtn_b64`](../../core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp) uses two 32-bit atomic conditional exchanges: the address mask and bit clearing follow [RDNA3 ISA](https://gpuopen.com/download/rdna3-shader-instruction-set-architecture-feb-2023.pdf), section 16.15, page 530, and the access width follows the 32-bit atomic classification in [RDNA4 ISA](https://gpuopen.com/download/rdna4-instruction-set-architecture.pdf), section 12.5.1, pages 152-153. These details and contended lane ordering were not measured on RDNA2 or the console; pair-wide indivisibility is not assumed, and concurrent 64-bit LDS atomics use a separate lock. GDS throws. VGPR pairs extending beyond v255 throw; allocated-VGPR EXEC nullification is not emulated. +- [`ds_condxchg32_rtn_b64`](../../core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp) uses two 32-bit atomic conditional exchanges. RDNA2 (gfx1035) measurements confirmed aligned return values, conditional bit clearing and 16-bit offsets; out-of-range accesses return zero without modifying LDS. Misaligned accesses fault the wave on gfx1035; the recompiler does not model this fault. Radeon 780M (gfx1103, RDNA3) measurements confirmed aligned return values, conditional bit clearing, EXEC masking and operand overlap in wave32 and wave64; contended cases passed per-dword conservation checks. Contended lane ordering and gfx1013 remain unmeasured; pair-wide indivisibility is not assumed, and concurrent 64-bit LDS atomics use a separate lock. GDS throws. VGPR pairs extending beyond v255 throw; allocated-VGPR EXEC nullification is not emulated. - [64-bit image atomics](../../core/shader/recompiler/Translation/src/MemoryInstructions/ImageInstructions.cpp) (`image_atomic_*` with a two-dword data mask) need `VK_EXT_shader_image_atomic_int64` and act on 32_32 UINT, SINT and FLOAT surfaces through an R64_UINT storage view. 64-bit `image_atomic_inc`, `image_atomic_dec` and the float atomics throw, as does any other access in the same shader to an image it updates with 64-bit atomics. - [8_SRGB and 8_8_SRGB loads](../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) on a device that cannot sample R8_SRGB or R8G8_SRGB return the sRGB EOTF rounded to nearest even at bf16 precision, as adriadam10 measured on RDNA2 (gfx1035) in #866; the PS5's gfx1013 is not measured. - [8_SRGB and 8_8_SRGB textures](../../core/libs/prx/libSceAgcDriver/Graphics/src/TextureFormat.cpp) (libSceAgcDriver) that the device samples as R8_SRGB or R8G8_SRGB use its own decode, which need not match gfx1035: NVIDIA's R8_SRGB is one bf16 step off in 29 of 256 codes.