Merge pull request #1198 from Sanjaysrinivas/feat/shader-conditional-exchange

feat(shader): add LDS conditional exchange
This commit is contained in:
Adrià
2026-10-08 11:34:31 +02:00
committed by GitHub
7 changed files with 214 additions and 0 deletions
@@ -0,0 +1,176 @@
#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp"
#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp"
#include "Recompiler.hpp"
#include "VulkanTestDevice.hpp"
#include <algorithm>
#include <array>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <span>
#include <string>
#include <vector>
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<std::uint32_t, MaxThreads * Words> Input{};
alignas(256) std::array<std::uint32_t, MaxThreads * Words> 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<std::uint32_t>({
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<std::uint32_t, 4> BufferDescriptor(const void* data, std::uint32_t bytes) {
const auto address = reinterpret_cast<std::uintptr_t>(data);
return {static_cast<std::uint32_t>(address), static_cast<std::uint32_t>((address >> 32u) & 0xffffu), bytes, 0x01016facu};
}
template<typename TUse>
void Translate(std::span<const std::uint32_t> code, std::uint32_t waveSize, const ShaderRecompiler::SpirvTarget& target, TUse&& use) {
std::vector<std::uint32_t> userData(8, 0u);
const auto input = BufferDescriptor(Input.data(), static_cast<std::uint32_t>(Input.size() * 4u));
const auto output = BufferDescriptor(Output.data(), static_cast<std::uint32_t>(Output.size() * 4u));
std::copy(input.begin(), input.end(), userData.begin());
std::copy(output.begin(), output.end(), userData.begin() + 4);
const std::array<ShaderRecompiler::MemoryRegion, 1> memory{{{reinterpret_cast<std::uintptr_t>(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<std::uintptr_t>(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<typename TCheck>
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<const std::uint32_t> code) { Translate(code, 32u, device.Target(), [](const auto&) {}); };
constexpr std::array<std::uint32_t, 3> boundary{0xd9f80000u, 0xfe00fe00u, 0xbf810000u};
translate(boundary);
ExpectError([&] { translate(std::span(boundary).first(1u)); }, "truncated DS instruction");
constexpr std::array<std::uint32_t, 3> sourceOverflow{0xd9f80000u, 0x0200ff00u, 0xbf810000u};
constexpr std::array<std::uint32_t, 3> destinationOverflow{0xd9f80000u, 0xff000400u, 0xbf810000u};
constexpr std::array<std::uint32_t, 3> 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<std::uint32_t>(seed);
in[1] = contended ? 0xfffffffeu : static_cast<std::uint32_t>(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);
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;
}
}
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<bool, MaxThreads + 1u> 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<std::uintptr_t>(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;
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]);
}
if (contended) CheckContention(waveSize);
}
void CheckExecution(AgcDriver::VulkanDevice& device, std::uint32_t waveSize, const ShaderRecompiler::SpirvTarget& target) {
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);
}
}
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;
}
}
@@ -932,6 +932,7 @@ enum class RdnaOpcode : std::uint16_t {
DsXorRtnB64,
DsMskorRtnB64,
DsWrxchgRtnB64,
DsCondxchg32RtnB64,
DsCmpstRtnB64,
DsCmpstRtnF64,
DsMinRtnF64,
@@ -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<const std::
if (info.opcode == RdnaOpcode::DsWriteAddtidB32 && data1 != 0u) {
throw std::runtime_error("DS write addtid data1 operand is not supported");
}
if (info.opcode == RdnaOpcode::DsCondxchg32RtnB64) {
if (data0 + 2u > 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");
}
@@ -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);
@@ -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;
@@ -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);
const IrU32 address = readU32(inst.source0);
IrValue& active = ir.GetExec();
std::array<IrValue*, 2> 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 = 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]);
return true;
}
bool TranslationContext::dsAppendConsume(const RdnaInstruction& inst, IrOpcode opcode) {
const MemoryInfo memory = sharedMemoryInfoFromInstruction(inst);
const IrU32 address = readU32(makeM0Operand());
+1
View File
@@ -247,6 +247,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. 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.