From dc8a7169300aa26b31d328b1240d8ad71a922173 Mon Sep 17 00:00:00 2001 From: Dean Galvin Date: Fri, 2 Oct 2026 18:23:56 +1300 Subject: [PATCH] fix(shader): wrap the v_readlane_b32 lane select at the wave size The hardware reads the lane select modulo the wave size (S1[4:0] in wave32, S1[5:0] in wave64), as ReadLaneEliminator already assumes with selector % waveSize. On the single-subgroup path EmitReadLane passed the raw select to OpGroupNonUniformShuffle, which is undefined for an id at or above the subgroup size. It is now masked with WaveSize() - 1, like v_writelane_b32. The two-lane wave64 path already wrapped. --- .../tests/execution/ReadLane.cpp | 86 +++++++++++++++++++ .../src/SpirvAlu/SpirvAluEmitterMath.cpp | 4 +- 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/ReadLane.cpp diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ReadLane.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ReadLane.cpp new file mode 100644 index 000000000..ff98068ca --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ReadLane.cpp @@ -0,0 +1,86 @@ +#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 + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t Threads = 32; +constexpr std::uint32_t Stride = 4; +alignas(256) std::array Input{}; +alignas(256) std::array Output{}; + +alignas(256) constexpr std::array ReadLaneCode{ + 0x34020082, 0xe0302000, 0x80000401, 0xe0302004, 0x80000501, 0xbf8c3f70, 0xd7600008, 0x00014b04, + 0x7e280505, 0xd7600009, 0x00002904, 0x7e140208, 0x7e160209, 0x7e180214, 0xe0702000, 0x80010a01, + 0xe0702004, 0x80010b01, 0xe0702008, 0x80010c01, 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}; +} + +void Run(AgcDriver::VulkanDevice& device) { + for (std::uint32_t tid = 0; tid < Threads; ++tid) { + Input[tid * Stride] = 0xa0000000u + tid * 0x01010101u; + Input[tid * Stride + 1] = 69u + tid; + } + Output.fill(0xdeadbeefu); + 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::span code(ReadLaneCode); + const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0, {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; + const auto result = ShaderRecompiler::Recompile(request); + device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device.WaitIdle(); +} + +void Check() { + for (std::uint32_t tid = 0; tid < Threads; ++tid) { + const std::array expected{Input[5u * Stride], Input[5u * Stride], 69u}; + for (std::uint32_t j = 0; j < expected.size(); ++j) { + const auto actual = Output[tid * Stride + j]; + Require(actual == expected[j], "read lane: lane " + std::to_string(tid) + " result " + std::to_string(j) + " is " + std::to_string(actual) + ", expected " + std::to_string(expected[j])); + } + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + if (device->Target().subgroupSize < Threads) { + std::printf("skipped, subgroup size %u cannot hold a wave32\n", device->Target().subgroupSize); + return VulkanTestSkipped; + } + Run(*device); + Check(); + std::puts("read lane tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp index 7c5c23704..dfc017fbf 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp @@ -531,7 +531,9 @@ std::uint32_t EmitReadFirstLane(SpirvValueEmitContext& ctx, const IrValue& inst) } std::uint32_t EmitReadLane(SpirvValueEmitContext& ctx, const IrValue& inst) { - return ctx.Shuffle(inst, 0, ctx.Arg(inst, 1)); + auto& state = ctx.state; + const auto lane = Binary(state, spv::OpBitwiseAnd, TypeU32(state), ctx.Arg(inst, 1), ConstantU32(state, state.program.WaveSize() - 1u)); + return ctx.Shuffle(inst, 0, lane); } std::uint32_t EmitWriteLane(SpirvValueEmitContext& ctx, const IrValue& inst) {