From 28b09d4da2f29dd7d91a9865703a3bdab1d964dc Mon Sep 17 00:00:00 2001 From: STL1te Date: Tue, 29 Sep 2026 13:59:21 +0300 Subject: [PATCH] feat(shader): GLC/DLC buffer loads and stores are coherent A buffer load with GLC/DLC bypasses the caches another workgroup's stores may be stale in; a radix sort polling a partition's published prefix with one read stale flags on the host, handed overlapping ranges to two partitions, and lost half its keys. A tree built from the duplicates held cycles, and the shaders walking it looped until the loop guard stopped them. As in the Kyty recompiler (GLC/DLC buffer cache policy support): the decoder reads DLC on MUBUF/MTBUF, a buffer load or store with GLC or DLC (atomics use GLC only to return data) is MemoryInfo::coherent, the guest storage buffers of a program with one are decorated Coherent, and its accesses are Volatile. Through the BDA table (GPU-selected V#s) the loads and stores are Volatile. Co-Authored-By: Claude Opus 5.5 --- .../IrMetadata/ResourceInfo.hpp | 3 +++ .../include/RdnaDecoder/RdnaInstruction.hpp | 1 + .../RdnaDecoder/src/RdnaMemoryOpDecoder.cpp | 2 ++ .../include/SpirvBackend/SpirvAnalysis.hpp | 2 ++ .../SpirvBackend/SpirvEmitterState.hpp | 2 ++ .../SpirvBackend/src/SpirvAnalysis.cpp | 2 ++ .../SpirvBackend/src/SpirvBdaRead.cpp | 26 ++++++++++++------- .../src/SpirvMemory/SpirvBufferAccess.cpp | 2 ++ .../src/SpirvMemory/SpirvEmitterHelpers.cpp | 6 +++++ .../SpirvMemory/SpirvMemoryInstructions.cpp | 15 ++++++++--- .../MemoryInstructions/BufferInstructions.cpp | 6 +++-- 11 files changed, 52 insertions(+), 15 deletions(-) diff --git a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourceInfo.hpp b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourceInfo.hpp index 2a10445db..5b4bed6d5 100644 --- a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourceInfo.hpp +++ b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourceInfo.hpp @@ -45,6 +45,9 @@ struct MemoryInfo { bool idxen = false; bool offen = false; bool planningOnly = false; + // A buffer load or store with GLC or DLC: it bypasses the caches another workgroup's stores + // may be stale in (a look-back polling a flag another workgroup publishes). + bool coherent = false; // A buffer access whose V# the program computes on the GPU (loaded at a runtime offset, say), // so no binding can hold it: the shader decodes the V# and goes through the BDA page table. bool gpuDescriptor = false; diff --git a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaInstruction.hpp b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaInstruction.hpp index bd8bfbb80..e27d5fb18 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaInstruction.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaInstruction.hpp @@ -148,6 +148,7 @@ struct RdnaInstruction { bool formatted = false; bool gds = false; bool glc = false; + bool dlc = false; bool slc = false; bool idxen = false; bool offen = false; diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp index 04d2f8ece..b643c7fbb 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp @@ -423,6 +423,7 @@ RdnaInstruction DecodeRdnaMubuf(std::uint32_t programCounter, std::span> 12u) & 1u) != 0u; instruction.idxen = ((word0 >> 13u) & 1u) != 0u; instruction.glc = ((word0 >> 14u) & 1u) != 0u; + instruction.dlc = ((word0 >> 15u) & 1u) != 0u; instruction.slc = ((word1 >> 22u) & 1u) != 0u; applyMemoryInfo(instruction, info); setRawWords(instruction, code, wordIndex, 2u); @@ -462,6 +463,7 @@ RdnaInstruction DecodeRdnaMtbuf(std::uint32_t programCounter, std::span> 12u) & 1u) != 0u; instruction.idxen = ((word0 >> 13u) & 1u) != 0u; instruction.glc = ((word0 >> 14u) & 1u) != 0u; + instruction.dlc = ((word0 >> 15u) & 1u) != 0u; instruction.slc = ((word1 >> 22u) & 1u) != 0u; applyMemoryInfo(instruction, info); setRawWords(instruction, code, wordIndex, 2u); diff --git a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvAnalysis.hpp b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvAnalysis.hpp index a8f27bdf0..53eefef93 100644 --- a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvAnalysis.hpp +++ b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvAnalysis.hpp @@ -18,6 +18,8 @@ struct SpirvRequirements { bool functionScratch = false; bool pixelValidMask = false; bool bufferInt64Atomics = false; + // A buffer load or store is coherent (MemoryInfo::coherent): the guest buffers are Coherent. + bool coherentBuffers = false; std::vector capabilities; std::vector extensions; }; diff --git a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp index cb4e5c2eb..8e555b598 100644 --- a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp +++ b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp @@ -67,6 +67,8 @@ struct MemoryResourceAccess { std::uint32_t indexOffset = 0; std::uint32_t byteOffset = 0; bool addIndexOffset = false; + // Memory operands of the loads and stores: Volatile for a coherent access (MemoryInfo::coherent). + std::uint32_t memoryAccess = 0; }; struct SpirvEmitterState { diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp index a34fb380b..4fd16c7db 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp @@ -31,6 +31,8 @@ SpirvRequirements AnalyzeProgramRequirements(const IrProgram& program) { throw std::runtime_error("buffer operation has invalid memory metadata"); } const auto& memory = program.Resources().memoryInfo.at(memoryIndex); + // Through the BDA table a coherent access is a Volatile load or store (BdaAccessMask). + requirements.coherentBuffers = requirements.coherentBuffers || (memory.coherent && !memory.gpuDescriptor); if (memory.gpuDescriptor) { // The V#'s ADD_TID is known at run time only. if (program.Resources().stage != IrShaderStage::Compute) { diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp index 9c0333992..18761547e 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp @@ -16,7 +16,15 @@ void EmitBdaOverflowCheck(SpirvEmitterState& state, std::uint32_t address, std:: StopBdaInvocationIf(state, overflow); } -std::uint32_t EmitBdaByte(SpirvEmitterState& state, std::uint32_t guest, std::uint32_t instruction) { +// The memory operands of a BDA load or store: Volatile for a coherent access (MemoryInfo::coherent), +// which must see what other workgroups store while the dispatch runs. +std::uint32_t BdaAccessMask(const SpirvEmitterState& state, const IrValue& inst) { + const auto index = inst.Flags().index; + const auto& memory = state.program.Resources().memoryInfo; + return spv::MemoryAccessAlignedMask | (index < memory.size() && memory[index].coherent ? spv::MemoryAccessVolatileMask : 0u); +} + +std::uint32_t EmitBdaByte(SpirvEmitterState& state, std::uint32_t guest, std::uint32_t instruction, std::uint32_t accessMask) { const auto byteType = state.module.Type(spv::OpTypeInt, 8u, 0u); const auto bytePointer = TypePointer(state, spv::StorageClassPhysicalStorageBuffer, byteType); const auto physical = state.module.AllocateId(); @@ -26,7 +34,7 @@ std::uint32_t EmitBdaByte(SpirvEmitterState& state, std::uint32_t guest, std::ui const auto pointer = state.module.AllocateId(); state.module.AddFunction(spv::OpConvertUToPtr, bytePointer, pointer, physical); const auto loaded = state.module.AllocateId(); - state.module.AddFunction(spv::OpLoad, byteType, loaded, pointer, spv::MemoryAccessAlignedMask, 1u); + state.module.AddFunction(spv::OpLoad, byteType, loaded, pointer, accessMask, 1u); return Unary(state, spv::OpUConvert, TypeU32(state), loaded); } // Unmapped bytes read as zero; the fault is recorded and every invocation reaches the @@ -41,7 +49,7 @@ std::uint32_t EmitBdaByte(SpirvEmitterState& state, std::uint32_t guest, std::ui const auto pointer = state.module.AllocateId(); state.module.AddFunction(spv::OpConvertUToPtr, bytePointer, pointer, physical); const auto loaded = state.module.AllocateId(); - state.module.AddFunction(spv::OpLoad, byteType, loaded, pointer, spv::MemoryAccessAlignedMask, 1u); + state.module.AddFunction(spv::OpLoad, byteType, loaded, pointer, accessMask, 1u); const auto widened = Unary(state, spv::OpUConvert, TypeU32(state), loaded); state.module.AddFunction(spv::OpBranch, merge); EmitLabel(state, merge); @@ -50,11 +58,11 @@ std::uint32_t EmitBdaByte(SpirvEmitterState& state, std::uint32_t guest, std::ui return value; } -std::uint32_t EmitBdaBytes(SpirvEmitterState& state, std::uint32_t address, std::uint32_t bytes, std::uint32_t instruction) { +std::uint32_t EmitBdaBytes(SpirvEmitterState& state, std::uint32_t address, std::uint32_t bytes, std::uint32_t instruction, std::uint32_t accessMask) { auto result = ConstantU32(state, 0u); for (std::uint32_t byte = 0; byte < bytes; ++byte) { const auto guest = Binary(state, spv::OpIAdd, TypeScalarU64(state), address, BdaConstant(state, byte)); - const auto value = EmitBdaByte(state, guest, instruction); + const auto value = EmitBdaByte(state, guest, instruction, accessMask); result = Binary(state, spv::OpBitwiseOr, TypeU32(state), result, Binary(state, spv::OpShiftLeftLogical, TypeU32(state), value, ConstantU32(state, byte * 8u))); } return result; @@ -130,7 +138,7 @@ std::uint32_t EmitBdaRead(SpirvValueEmitContext& ctx, const IrValue& inst, std:: if (bits == 32u) return EmitBdaDwordReads(ctx, inst, address, 0u, 1u)[0]; const auto instruction = ConstantU32(state, inst.Flags().pc); EmitBdaOverflowCheck(state, address, bits / 8u, instruction); - return EmitBdaBytes(state, address, bits / 8u, instruction); + return EmitBdaBytes(state, address, bits / 8u, instruction, BdaAccessMask(state, inst)); } void EmitBdaWrite(SpirvValueEmitContext& ctx, const IrValue& inst, std::uint32_t address, std::uint32_t value) { @@ -146,7 +154,7 @@ void EmitBdaWrite(SpirvValueEmitContext& ctx, const IrValue& inst, std::uint32_t EmitIfCondition(state, Binary(state, spv::OpINotEqual, TypeBool(state), physical, BdaConstant(state, 0u)), [&] { const auto pointer = state.module.AllocateId(); state.module.AddFunction(spv::OpConvertUToPtr, TypePhysicalU32Pointer(state), pointer, physical); - state.module.AddFunction(spv::OpStore, pointer, value, spv::MemoryAccessAlignedMask, 4u); + state.module.AddFunction(spv::OpStore, pointer, value, BdaAccessMask(state, inst), 4u); state.module.AddFunction(spv::OpFunctionCall, state.module.Type(spv::OpTypeVoid), state.module.AllocateId(), state.bdaNoteWriteFunction, address); }); }); @@ -186,7 +194,7 @@ std::array EmitBdaDwordReads(SpirvValueEmitContext& ctx, const if (!isUsed(dword)) continue; if (dword != first) addresses[dword] = dwordAddress(dword); EmitBdaOverflowCheck(state, addresses[dword], 4u, instruction); - into[dword] = EmitBdaBytes(state, addresses[dword], 4u, instruction); + into[dword] = EmitBdaBytes(state, addresses[dword], 4u, instruction, BdaAccessMask(state, inst)); } }; addresses[first] = dwordAddress(first); @@ -213,7 +221,7 @@ std::array EmitBdaDwordReads(SpirvValueEmitContext& ctx, const const auto pointer = state.module.AllocateId(); state.module.AddFunction(spv::OpConvertUToPtr, TypePhysicalU32Pointer(state), pointer, element); wideValues[dword] = state.module.AllocateId(); - state.module.AddFunction(spv::OpLoad, TypeU32(state), wideValues[dword], pointer, spv::MemoryAccessAlignedMask, 4u); + state.module.AddFunction(spv::OpLoad, TypeU32(state), wideValues[dword], pointer, BdaAccessMask(state, inst), 4u); } state.module.AddFunction(spv::OpBranch, merge); EmitLabel(state, byteLabel); diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvBufferAccess.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvBufferAccess.cpp index d97843e58..068400061 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvBufferAccess.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvBufferAccess.cpp @@ -109,6 +109,8 @@ MemoryResourceAccess PrepareStorageBufferResourceAccess(SpirvEmitterState& state access.objectPointer = state.module.AllocateId(); state.module.AddFunction(spv::OpAccessChain, pointerType, access.objectPointer, variable, ConstantU32(state, arrayIndex)); access.byteOffset = state.memoryByteOffsets.at(arrayIndex); + // A GLC/DLC access polls or publishes what other workgroups see: never cached or combined. + access.memoryAccess = mem.coherent ? spv::MemoryAccessVolatileMask : 0u; access.length = state.module.AllocateId(); state.module.AddFunction(spv::OpArrayLength, TypeU32(state), access.length, access.objectPointer, 0u); // A null V# binds a placeholder the shader must not touch: nothing is in bounds. diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp index 926b7ec1a..39c27768a 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp @@ -356,6 +356,12 @@ void DefineDescriptors(SpirvEmitterState& state) { state.module.AddAnnotation(spv::OpDecorate, state.storageBufferVariable, spv::DecorationAliased); state.module.AddAnnotation(spv::OpDecorate, state.storageBufferU64Variable, spv::DecorationAliased); } + if (state.requirements.coherentBuffers) { + // A coherent load polls what other workgroups store (RDNA stores reach L2 without + // GLC too): every alias of the buffers takes part in device-scope visibility. + state.module.AddAnnotation(spv::OpDecorate, state.storageBufferVariable, spv::DecorationCoherent); + if (state.storageBufferU64Variable != 0u) state.module.AddAnnotation(spv::OpDecorate, state.storageBufferU64Variable, spv::DecorationCoherent); + } break; case DescriptorBindingKind::BdaPagetable: state.bdaPagetableVariable = Define(StorageBufferBlockType(state), "bda_pagetable"); diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp index 827ec4512..f539dcaf5 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp @@ -110,7 +110,8 @@ std::uint32_t LoadWordInBounds(SpirvValueEmitContext& ctx, const MemoryResourceA auto& state = ctx.state; const auto pointer = EmitMemoryElementPointer(state, resource, index); const auto value = state.module.AllocateId(); - state.module.AddFunction(spv::OpLoad, TypeU32(state), value, pointer); + if (resource.memoryAccess != 0u) state.module.AddFunction(spv::OpLoad, TypeU32(state), value, pointer, resource.memoryAccess); + else state.module.AddFunction(spv::OpLoad, TypeU32(state), value, pointer); return value; } @@ -374,7 +375,8 @@ std::uint32_t FormattedLoad(SpirvValueEmitContext& ctx, const IrValue& inst, con void StoreWordInBounds(SpirvValueEmitContext& ctx, const MemoryResourceAccess& resource, std::uint32_t index, std::uint32_t data) { auto& state = ctx.state; - state.module.AddFunction(spv::OpStore, EmitMemoryElementPointer(state, resource, index), data); + if (resource.memoryAccess != 0u) state.module.AddFunction(spv::OpStore, EmitMemoryElementPointer(state, resource, index), data, resource.memoryAccess); + else state.module.AddFunction(spv::OpStore, EmitMemoryElementPointer(state, resource, index), data); } void StoreSubwordInBounds(SpirvValueEmitContext& ctx, const MemoryInfo& mem, const MemoryResourceAccess& resource, std::uint32_t address, std::uint32_t index, std::uint32_t bits, std::uint32_t data) { @@ -392,8 +394,13 @@ void StoreSubwordInBounds(SpirvValueEmitContext& ctx, const MemoryInfo& mem, con }; if (mem.kind == ResourceKind::Scratch) { const auto old = state.module.AllocateId(); - state.module.AddFunction(spv::OpLoad, TypeU32(state), old, pointer); - state.module.AddFunction(spv::OpStore, pointer, merge(old)); + if (resource.memoryAccess != 0u) { + state.module.AddFunction(spv::OpLoad, TypeU32(state), old, pointer, resource.memoryAccess); + state.module.AddFunction(spv::OpStore, pointer, merge(old), resource.memoryAccess); + } else { + state.module.AddFunction(spv::OpLoad, TypeU32(state), old, pointer); + state.module.AddFunction(spv::OpStore, pointer, merge(old)); + } } else { AtomicUpdate(state, pointer, mem.kind, merge); } diff --git a/core/shader/recompiler/Translation/src/MemoryInstructions/BufferInstructions.cpp b/core/shader/recompiler/Translation/src/MemoryInstructions/BufferInstructions.cpp index 92b69df70..fa9006ed7 100644 --- a/core/shader/recompiler/Translation/src/MemoryInstructions/BufferInstructions.cpp +++ b/core/shader/recompiler/Translation/src/MemoryInstructions/BufferInstructions.cpp @@ -33,7 +33,8 @@ MemoryInfo bufferMemoryInfoFromInstruction(const RdnaInstruction& inst) { } bool TranslationContext::bufferLoad(const RdnaInstruction& inst) { - const MemoryInfo memory = bufferMemoryInfoFromInstruction(inst); + MemoryInfo memory = bufferMemoryInfoFromInstruction(inst); + memory.coherent = inst.glc || inst.dlc; IrOpcode opcode; switch (memory.dataBits) { case 8u: @@ -80,7 +81,8 @@ bool TranslationContext::bufferLoad(const RdnaInstruction& inst) { } bool TranslationContext::bufferStore(const RdnaInstruction& inst) { - const MemoryInfo memory = bufferMemoryInfoFromInstruction(inst); + MemoryInfo memory = bufferMemoryInfoFromInstruction(inst); + memory.coherent = inst.glc || inst.dlc; IrValue* resource = getBufferResource(memory); const BufferAddress address = readBufferAddress(inst); const IrU32 data = readU32(inst.destination);