From 3033e5e5545f890477e6557012b8490ea452f4cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Franch?= Date: Fri, 2 Oct 2026 08:08:40 +0200 Subject: [PATCH] feat(shader): implement s_movrels/s_movreld (b32, b64) and s_movrelsd_2_b32 s_movrels reads SGPR[src + M0] and s_movreld writes SGPR[dst + M0]. The 64-bit forms align the offset down to even (M0 & ~1), and s_movrelsd_2_b32 takes the source offset from M0[9:0] and the destination offset from M0[25:16], as measured on hardware. They use the same select-over-registers lowering as the vector forms, over the 106 SGPRs. The embedded vertex-fetch scan forgets every tracked SGPR on a relative write. Co-Authored-By: Claude --- .../execution/ScalarRelativeIndexing.cpp | 109 ++++++++++++++++++ .../include/RdnaDecoder/RdnaOpcode.hpp | 5 + .../recompiler/RdnaDecoder/src/RdnaOpcode.cpp | 5 + .../RdnaDecoder/src/RdnaScalarOpDecoder.cpp | 7 ++ .../Translation/TranslationContext.hpp | 3 + .../Translation/src/EmbeddedVertexFetch.cpp | 11 ++ .../Translation/src/IntegerInstructions.cpp | 49 ++++++++ .../Translation/src/ScalarInstructions.cpp | 6 + 8 files changed, 195 insertions(+) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/ScalarRelativeIndexing.cpp diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ScalarRelativeIndexing.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ScalarRelativeIndexing.cpp new file mode 100644 index 000000000..a786bf3ff --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ScalarRelativeIndexing.cpp @@ -0,0 +1,109 @@ +#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 +#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; +alignas(256) std::array Input{}; +alignas(256) std::array Output{}; + +alignas(256) constexpr std::array Code{ + 0x34060086, 0x7e140280, 0x7e160280, 0x7e180280, 0x7e1a0280, 0x7e1c0280, 0x7e1e0280, 0x7e200280, + 0x7e220280, 0x7e240280, 0x7e260280, 0x7e280280, 0x7e2a0280, 0x7e2c0280, 0x7e2e0280, 0x7e300280, + 0x7e320280, 0xbe9403ff, 0x00002020, 0xbe9503ff, 0x00002021, 0xbe9603ff, 0x00002022, 0xbe9703ff, + 0x00002023, 0xbe9803ff, 0x00002024, 0xbe9903ff, 0x00002025, 0xbe9a03ff, 0x00002026, 0xbe9b03ff, + 0x00002027, 0xbe9c03ff, 0x00002028, 0xbe9d03ff, 0x00002029, 0xbe9e03ff, 0x00002030, 0xbe9f03ff, + 0x00002031, 0xbea003ff, 0x00002032, 0xbea103ff, 0x00002033, 0xbea203ff, 0x00002034, 0xbea303ff, + 0x00002035, 0xbefc0383, 0xbea82e14, 0x7e140228, 0xbea82f14, 0x7e160228, 0x7e180229, 0xbe9630ff, + 0x00000077, 0x7e1a0219, 0xbe9a3122, 0x7e1c021c, 0x7e1e021d, 0x7e20021e, 0xbefc03ff, 0xfc03fc02, + 0xbe954914, 0x7e220218, 0x7e240217, 0xbefc03ff, 0x00020001, 0x7e26021e, 0xbe9e30ff, 0x00000088, + 0x7e28021f, 0x7e2a0220, 0xbefc0381, 0xbea82f1e, 0x7e2c0228, 0x7e2e0229, 0xbefc03ff, 0x00010002, + 0xbe984914, 0x7e300219, 0x7e32021a, 0xe0701000, 0x80010a03, 0xe0701004, 0x80010b03, 0xe0701008, + 0x80010c03, 0xe070100c, 0x80010d03, 0xe0701010, 0x80010e03, 0xe0701014, 0x80010f03, 0xe0701018, + 0x80011003, 0xe070101c, 0x80011103, 0xe0701020, 0x80011203, 0xe0701024, 0x80011303, 0xe0701028, + 0x80011403, 0xe070102c, 0x80011503, 0xe0701030, 0x80011603, 0xe0701034, 0x80011703, 0xe0701038, + 0x80011803, 0xe070103c, 0x80011903, 0xbf810000, +}; + +constexpr std::uint32_t Expected[16] = {0x00002023u, 0x00002022u, 0x00002023u, 0x00000077u, 0x00002034u, 0x00002035u, 0x00002030u, 0x00002022u, 0x00002023u, 0x00002030u, 0x00002031u, 0x00002032u, 0x00002030u, 0x00002031u, 0x00002022u, 0x00002026u}; +constexpr const char* Names[16] = {"movrels", "movrels_b64_lo", "movrels_b64_hi", "movreld", "movreld_b64_lo", "movreld_b64_hi", "movreld_b64_next", "sd2", "sd2_untouched", "base", "movreld_out_of_range", "neighbour", "movrels_b64_aligned_lo", "movrels_b64_aligned_hi", "sd2_second", "sd2_second_neighbour"}; + +void Fill(std::uint32_t, std::uint32_t*) {} + +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}; +} + +std::string Hex(std::uint32_t value) { + char text[16]; + std::snprintf(text, sizeof(text), "0x%08x", value); + return text; +} + +void Expect(std::uint32_t tid, std::uint32_t actual, std::uint32_t expected, const char* name) { + Require(actual == expected, std::string("scalar relative indexing: lane ") + std::to_string(tid) + " " + name + " is " + Hex(actual) + ", expected " + Hex(expected)); +} + +void Run(AgcDriver::VulkanDevice& device) { + for (std::uint32_t tid = 0; tid < Threads; ++tid) Fill(tid, &Input[tid * Inputs]); + Output.fill(0xdeadbeefu); + 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::span code(Code); + const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0u, {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::uint32_t* in = &Input[tid * Inputs]; + const std::uint32_t* out = &Output[tid * Results]; + for (std::uint32_t index = 0; index < 16; ++index) Expect(tid, out[index], Expected[index], Names[index]); + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + Run(*device); + Check(); + std::puts("scalar relative indexing 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 e8db97455..9b9bc0c38 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -137,6 +137,11 @@ enum class RdnaOpcode : std::uint16_t { SFlbitI32I64, SBitreplicateB64B32, SQuadmaskB32, + SMovrelsB32, + SMovrelsB64, + SMovreldB32, + SMovreldB64, + SMovrelsd2B32, SQuadmaskB64, SGetpcB64, SSubvectorLoopBegin, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp index 6e8685542..d36200b53 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp @@ -59,6 +59,11 @@ bool IsScalarAluOpcode(RdnaOpcode opcode) { case RdnaOpcode::SFlbitI32I64: case RdnaOpcode::SBitreplicateB64B32: case RdnaOpcode::SQuadmaskB32: + case RdnaOpcode::SMovrelsB32: + case RdnaOpcode::SMovrelsB64: + case RdnaOpcode::SMovreldB32: + case RdnaOpcode::SMovreldB64: + case RdnaOpcode::SMovrelsd2B32: case RdnaOpcode::SQuadmaskB64: case RdnaOpcode::SAndSaveexecB32: case RdnaOpcode::SOrSaveexecB32: diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp index 08b7b6567..85241ed69 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp @@ -49,6 +49,10 @@ RdnaOpcode decodeSop1Opcode(std::uint32_t opcode) { case 0x2bu: return RdnaOpcode::SXnorSaveexecB64; case 0x2cu: return RdnaOpcode::SQuadmaskB32; case 0x2du: return RdnaOpcode::SQuadmaskB64; + case 0x2eu: return RdnaOpcode::SMovrelsB32; + case 0x2fu: return RdnaOpcode::SMovrelsB64; + case 0x30u: return RdnaOpcode::SMovreldB32; + case 0x31u: return RdnaOpcode::SMovreldB64; case 0x34u: return RdnaOpcode::SAbsI32; case 0x37u: return RdnaOpcode::SAndn1SaveexecB64; case 0x38u: return RdnaOpcode::SOrn1SaveexecB64; @@ -67,6 +71,7 @@ RdnaOpcode decodeSop1Opcode(std::uint32_t opcode) { case 0x45u: return RdnaOpcode::SOrn1SaveexecB32; case 0x46u: return RdnaOpcode::SAndn1WrexecB32; case 0x47u: return RdnaOpcode::SAndn2WrexecB32; + case 0x49u: return RdnaOpcode::SMovrelsd2B32; default: throw std::invalid_argument("unsupported SOP1 opcode " + std::to_string(opcode)); } } @@ -251,6 +256,8 @@ std::uint32_t scalarDestinationDwordCount(RdnaOpcode opcode) { case RdnaOpcode::SAndn1WrexecB64: case RdnaOpcode::SAndn2WrexecB64: case RdnaOpcode::SQuadmaskB64: + case RdnaOpcode::SMovrelsB64: + case RdnaOpcode::SMovreldB64: case RdnaOpcode::SAndn1SaveexecB64: case RdnaOpcode::SBitreplicateB64B32: case RdnaOpcode::SCselectB64: diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index 22a792dd3..2836d64af 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -179,6 +179,9 @@ private: bool vMbcntU32B32(const RdnaInstruction& inst, bool low); bool sBitreplicateB64B32(const RdnaInstruction& inst); bool sQuadmask(const RdnaInstruction& inst, bool wide); + bool sMovrel(const RdnaInstruction& inst); + IrU32 readRelativeScalar(std::uint32_t base, IrValue& offset); + void writeRelativeScalar(std::uint32_t base, IrValue& offset, IrU32 value); bool bfmB32(const RdnaInstruction& inst); IrU32 rightMask32(IrU32 count); IrU64 rightMask64(IrU32 count); diff --git a/core/shader/recompiler/Translation/src/EmbeddedVertexFetch.cpp b/core/shader/recompiler/Translation/src/EmbeddedVertexFetch.cpp index 4e5e270af..da66abcb0 100644 --- a/core/shader/recompiler/Translation/src/EmbeddedVertexFetch.cpp +++ b/core/shader/recompiler/Translation/src/EmbeddedVertexFetch.cpp @@ -290,6 +290,17 @@ EmbeddedFetchPlan EmbeddedVertexFetchAnalyzer::Analyze(const RdnaProgram& progra } } break; + case RdnaOpcode::SMovreldB32: + case RdnaOpcode::SMovreldB64: + case RdnaOpcode::SMovrelsd2B32: + sgprs.fill(SgprValue{}); + break; + case RdnaOpcode::SMovrelsB32: + case RdnaOpcode::SMovrelsB64: + if (isScalarOperand(inst.destination)) { + clearScalarRange(sgprs, inst.destination, decodedDstSize(inst)); + } + break; case RdnaOpcode::SMovkI32: if (isScalarOperand(inst.destination)) { auto& dst = sgprs[scalarSlot(inst.destination)]; diff --git a/core/shader/recompiler/Translation/src/IntegerInstructions.cpp b/core/shader/recompiler/Translation/src/IntegerInstructions.cpp index d89f8fb44..873cabc90 100644 --- a/core/shader/recompiler/Translation/src/IntegerInstructions.cpp +++ b/core/shader/recompiler/Translation/src/IntegerInstructions.cpp @@ -394,6 +394,55 @@ bool TranslationContext::sBitreplicateB64B32(const RdnaInstruction& inst) { return true; } +IrU32 TranslationContext::readRelativeScalar(std::uint32_t base, IrValue& offset) { + IrU32 result(ir.GetScalarReg(static_cast(base))); + for (std::uint32_t reg = base + 1u; reg < NumScalarRegs; ++reg) { + IrValue& hit = ir.IEqual(offset, ir.Constant(reg - base)); + result = IrU32(ir.Select(hit, ir.GetScalarReg(static_cast(reg)), result.Value())); + } + return result; +} + +void TranslationContext::writeRelativeScalar(std::uint32_t base, IrValue& offset, IrU32 value) { + for (std::uint32_t reg = base; reg < NumScalarRegs; ++reg) { + RdnaOperand target{}; + target.kind = RdnaOperandKind::ScalarRegister; + target.reg = reg; + IrValue& hit = ir.IEqual(offset, ir.Constant(reg - base)); + writeRawU32(target, IrU32(ir.Select(hit, value.Value(), ir.GetScalarReg(static_cast(reg))))); + } +} + +bool TranslationContext::sMovrel(const RdnaInstruction& inst) { + const bool wide = inst.op == RdnaOpcode::SMovrelsB64 || inst.op == RdnaOpcode::SMovreldB64; + const bool relativeSource = inst.op != RdnaOpcode::SMovreldB32 && inst.op != RdnaOpcode::SMovreldB64; + const bool relativeDestination = inst.op != RdnaOpcode::SMovrelsB32 && inst.op != RdnaOpcode::SMovrelsB64; + const RdnaOperand& source = sourceAt(inst, 0u); + if ((relativeSource && source.kind != RdnaOperandKind::ScalarRegister) || (relativeDestination && inst.destination.kind != RdnaOperandKind::ScalarRegister)) { + throw std::runtime_error("s_movrel operand is not a scalar register"); + } + IrValue& m0 = ir.GetM0(); + const bool split = inst.op == RdnaOpcode::SMovrelsd2B32; + IrValue& sourceOffset = split ? ir.BitwiseAnd(m0, ir.Constant(0x3ffu)) : wide ? ir.BitwiseAnd(m0, ir.Constant(~1u)) : m0; + IrValue& destinationOffset = split ? ir.BitwiseAnd(ir.ShiftRightLogical(m0, ir.Constant(16u)), ir.Constant(0x3ffu)) : sourceOffset; + const std::uint32_t count = wide ? 2u : 1u; + std::array values{IrU32(ir.Constant(0u)), IrU32(ir.Constant(0u))}; + if (relativeSource) { + for (std::uint32_t index = 0u; index < count; ++index) values[index] = readRelativeScalar(source.reg + index, sourceOffset); + } else if (wide) { + values = readU32Pair(source); + } else { + values[0] = readU32(source); + } + if (!relativeDestination) { + if (wide) writeU32Pair(inst.destination, values); + else writeRawU32(inst.destination, values[0]); + return true; + } + for (std::uint32_t index = 0u; index < count; ++index) writeRelativeScalar(inst.destination.reg + index, destinationOffset, values[index]); + return true; +} + bool TranslationContext::sQuadmask(const RdnaInstruction& inst, bool wide) { const auto compact = [&](IrU32 value) { IrU32 bits(ir.BitwiseOr(value.Value(), ir.ShiftRightLogical(value.Value(), ir.Constant(1u)))); diff --git a/core/shader/recompiler/Translation/src/ScalarInstructions.cpp b/core/shader/recompiler/Translation/src/ScalarInstructions.cpp index 05af0fc8b..05c314cf3 100644 --- a/core/shader/recompiler/Translation/src/ScalarInstructions.cpp +++ b/core/shader/recompiler/Translation/src/ScalarInstructions.cpp @@ -338,6 +338,12 @@ bool TranslationContext::emitScalar(const RdnaInstruction& inst) { return sQuadmask(inst, false); case RdnaOpcode::SQuadmaskB64: return sQuadmask(inst, true); + case RdnaOpcode::SMovrelsB32: + case RdnaOpcode::SMovrelsB64: + case RdnaOpcode::SMovreldB32: + case RdnaOpcode::SMovreldB64: + case RdnaOpcode::SMovrelsd2B32: + return sMovrel(inst); case RdnaOpcode::SBfmB32: return bfmB32(inst); case RdnaOpcode::SBfmB64: