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 <noreply@anthropic.com>
This commit is contained in:
STL1te
2026-09-29 13:59:21 +03:00
parent 3d3f71224e
commit 28b09d4da2
11 changed files with 52 additions and 15 deletions
@@ -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;
@@ -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;
@@ -423,6 +423,7 @@ RdnaInstruction DecodeRdnaMubuf(std::uint32_t programCounter, std::span<const st
instruction.offen = ((word0 >> 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<const st
instruction.offen = ((word0 >> 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);
@@ -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<std::uint32_t> capabilities;
std::vector<std::string> extensions;
};
@@ -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 {
@@ -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) {
@@ -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<MemoryFlags>().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<MemoryFlags>().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<std::uint32_t, 4> 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<std::uint32_t, 4> 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);
@@ -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.
@@ -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");
@@ -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);
}
@@ -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);