aql: flush the host data path before every doorbell; empty the scalar cache behind a chained wait

Two latent bugs that the packet processor's acquire (about 0.9 us a launch) has
been hiding by timing.

1. Kernel arguments and the chain record are written into VRAM through the BAR,
   and the card's host data path keeps such a write until it is flushed. Nothing
   ordered that against the doorbell (the sfence only orders the CPU's own
   buffers). A kernel the card starts a microsecond or two after its doorbell
   reads the slot as the launch one lap of the ring before left it: old
   arguments, or no chain record, in which case the launch counts nothing and
   the chained launch behind it waits for good. The backend now stores to the
   card's HDP flush register (HSA_AMD_AGENT_INFO_HDP_FLUSH) in front of every
   doorbell, in publish() and in a played pass's ring(). Without a register
   (the argument ring in host memory, or a driver that gives none) nothing
   changes, and a warning says so at startup.

2. rad_chain_wait()/r4d_chain_wait() end in an agent-scope acquire fence, which
   invalidates the vector caches. A load whose address every lane shares is a
   scalar load, served from the scalar data cache, and nothing but the packet
   processor's acquire (which runs when the kernel is launched, before the
   launch in front has written) empties that. A chained kernel can therefore
   read a stale scale, count or activation from the arena address an earlier
   launch read. The waits now issue s_dcache_inv behind the fence
   (r4d_chain_acquire in libr4d, chain_kernel.h in the core's blit kernels).

Evidence on an R9700 with the packet acquire left as it is: a probe of
1024-block readers behind a chained wait saw a stale shared value in 1 launch
of 1800 without the s_dcache_inv and in none with it; card-side copies issued
one by one with 50 us between them stalled the queue in its first lap, four
runs in four, and six runs in nine of 300000 back to back ended with a unit
moved by another launch's arguments, when the packets went without their
acquire and without the flush, and in none with the flush.

New device tests: a chained reader sees a shared value the launch in front
wrote, a chained launch reads its own arguments after the ring has lapped, and
a copy issued after a pause finds the record written for it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Code000andClaude Opus 5.5 authored and StillDeadcode committed 2026-11-17 18:16:12 +01:00
1 parent 78ad2096a3
commit 8f2e511fcc
6 files changed
+315 -4

No files matched your search

+80 -3
View File
@@ -21,7 +21,8 @@
* an invalidation. A slot is rewritten one lap after it was last read, so a system-scope
* acquire is placed at least once per half lap; without it every kernel after the first lap
* reads its predecessor's arguments. Slots are 1 KiB so no argument fetch shares a cache line
* with the next slot;
* with the next slot. No fence makes a write through the BAR arrive: the card's host data
* path is flushed in front of every doorbell (host_writes_out);
* - host-written memory: the first dispatch after the host waits on any queue takes a
* system-scope acquire, since host writes for the next step follow such a wait;
* - copies touching host or peer memory carry the scope themselves, as do the barrier packets
@@ -225,6 +226,9 @@ struct Dev {
bool has_vram = false;
uint32_t kfd_id = 0; /* the driver's gpu_id: its sysfs and ioctls name it so */
bool host_kargs = false; /* no large BAR: the argument ring lives in host memory */
/* The card's host data path flush register (HSA_AMD_AGENT_INFO_HDP_FLUSH), where the host
* writes arguments into VRAM; see host_writes_out. */
volatile uint32_t* hdp_flush = nullptr;
};
struct Global {
@@ -441,6 +445,18 @@ bool init_devices() {
RAD_INFO("device: card %d (%s) keeps kernel arguments in pinned host memory, as a "
"test asked", i, bus);
}
if (!g.dev[(size_t)i].host_kargs) {
hsa_amd_hdp_flush_t hdp{};
if (hsa_agent_get_info(g.dev[(size_t)i].agent,
(hsa_agent_info_t)HSA_AMD_AGENT_INFO_HDP_FLUSH,
&hdp) == HSA_STATUS_SUCCESS)
g.dev[(size_t)i].hdp_flush = hdp.HDP_MEM_FLUSH_CNTL;
if (!g.dev[(size_t)i].hdp_flush)
RAD_WARN("device: card %d (%s) gives no host data path flush register, so a kernel "
"may start before the arguments written for it through the BAR have "
"reached VRAM; only the packet processor's acquire, which holds a launch "
"back, leaves them the time", i, bus);
}
}
/* Without the handler a fault falls back to the runtime's own handling, which the
* memory-fault report below replaces; warn at startup. */
@@ -600,6 +616,7 @@ struct Stream {
hsa_queue_t* q = nullptr;
uint64_t mask = 0;
char* karg = nullptr; /* VRAM the CPU writes through the BAR, or host memory */
volatile uint32_t* hdp = nullptr; /* Dev::hdp_flush where `karg` is VRAM */
std::atomic<bool> busy{false};
uint64_t wr = 0; /* next packet index */
uint64_t rd = 0; /* last read index seen */
@@ -1093,10 +1110,37 @@ inline int reserve(Stream* s, uint64_t* out) {
return RAD_OK;
}
/* What the host wrote for the card, on its way before the packets that read it are announced.
*
* An argument slot in VRAM is written through the BAR: the bytes sit in the processor's
* write-combining buffers until a store fence, and behind the link in the card's host data path,
* which hands them on to memory in its own time unless it is told to flush. Nothing orders that
* against the doorbell. A kernel the card starts soon enough after its packet was announced
* reads the slot as the launch a lap of the ring before left it: first its chain record, which
* it obeys (an old target, so no wait) or does not accept (so it counts nothing and the chained
* launch behind it waits for good), then, behind its wait, its arguments.
*
* On an R9700 "soon enough" is a microsecond or two. What kept launches from it so far is that the
* packet processor's acquire holds a launch about 0.9 us and an ordered launch waits for the
* kernel in front: the margin is timing, not ordering. Without the packet's acquire (a test that
* issues it so) runs of fourteen 256 KiB card-side copies issued one by one: back to back, six runs
* in nine of 300 000 ended with a unit moved by another launch's arguments; with the host quiet
* for 50 us behind each doorbell the queue stalled in its first lap, four runs in four, on either
* card. With the flush below neither happened.
*
* The register is the driver's (HSA_AMD_AGENT_INFO_HDP_FLUSH, the remapped page it gives a
* process for this), and the store travels the link behind the slot's bytes and in front of the
* doorbell's: the order the driver's own submissions use. It goes in front of every doorbell, a
* chained launch's or not: no launch was ordered against the write. */
inline void host_writes_out(Stream* s) {
_mm_sfence();
if (s->hdp) *s->hdp = 1;
}
inline void publish(Stream* s, uint64_t idx, uint16_t header, uint16_t setup) {
s->wr = idx + 1;
hsa_queue_store_write_index_relaxed(s->q, idx + 1);
_mm_sfence(); /* the argument slot, written before the header */
host_writes_out(s); /* the argument slot, in VRAM before the header */
__atomic_store_n(reinterpret_cast<uint32_t*>(
static_cast<hsa_kernel_dispatch_packet_t*>(s->q->base_address) +
(idx & s->mask)),
@@ -1372,6 +1416,9 @@ struct PlayRun {
void ring() {
if (!unrung) return;
/* The chained records' argument slots (put) are VRAM the packets read as soon as they
* are announced. */
host_writes_out(s);
hsa_queue_store_write_index_relaxed(s->q, next);
hsa_signal_store_relaxed(s->q->doorbell_signal, (hsa_signal_value_t)(next - 1));
unrung = 0;
@@ -1795,6 +1842,7 @@ int a_stream_create(RadStream* out, int high_priority) {
return RAD_E_DEVICE;
}
s->karg = static_cast<char*>(ka);
s->hdp = d.host_kargs ? nullptr : d.hdp_flush;
s->chain_ctr = reinterpret_cast<uint64_t*>(s->karg + (size_t)(s->mask + 1) * kKargSlot);
*reinterpret_cast<volatile uint64_t*>(s->chain_ctr) = 0;
_mm_sfence();
@@ -4651,6 +4699,32 @@ bool ride_reusable(void* token) {
std::lock_guard<std::mutex> lk(pool().mu);
return reusable(g);
}
int chain_probe(void* stream, unsigned* x, unsigned* seen, unsigned pairs, unsigned blocks) {
if (!ours(stream) || !x || !seen) return RAD_E_INVAL;
for (unsigned i = 0; i < pairs; ++i)
if (rad_aql_probe_store(x, i + 1u, stream) < 0 ||
rad_aql_probe_load(x, seen + (size_t)i * blocks, blocks, stream) < 0)
return RAD_E_DEVICE;
return RAD_OK;
}
int chain_asks(void* stream, const void* src, void* dst, uint64_t bytes, unsigned* seen, unsigned n) {
Stream* s = ours(stream);
if (!s || !src || !dst || !seen || !s->chain_ctr || bytes == 0 || (bytes & 15)) return RAD_E_INVAL;
for (unsigned i = 0; i < n; ++i)
if (rad_aql_probe_ask(src, dst, bytes, seen, i + 1u,
reinterpret_cast<unsigned long long*>(s->chain_ctr), stream) < 0)
return RAD_E_DEVICE;
return RAD_OK;
}
int chain_marks(void* stream, unsigned* seen, unsigned n) {
if (!ours(stream) || !seen) return RAD_E_INVAL;
for (unsigned i = 0; i < n; ++i)
if (rad_aql_probe_mark(seen, i + 1u, stream) < 0) return RAD_E_DEVICE;
return RAD_OK;
}
} // namespace aql
size_t aql_code_registered(void) { return a_code_mark(); }
@@ -4961,8 +5035,11 @@ hipError_t hipPeekAtLastError(void) {
namespace rad {
namespace aql {
/* A build without the AQL backend hands out no ride. */
/* A build without the AQL backend hands out no ride and launches no chain. */
bool ride_reusable(void*) { return false; }
int chain_probe(void*, unsigned*, unsigned*, unsigned, unsigned) { return RAD_E_UNSUPPORTED; }
int chain_marks(void*, unsigned*, unsigned) { return RAD_E_UNSUPPORTED; }
int chain_asks(void*, const void*, void*, uint64_t, unsigned*, unsigned) { return RAD_E_UNSUPPORTED; }
} // namespace aql
} // namespace rad
+25
View File
@@ -72,6 +72,25 @@ void set_next_fence(uint8_t f);
/* Whether the signal behind a rad_ride token could be handed out again now (tests/device_test.cpp). */
bool ride_reusable(void* token);
/* `pairs` launches of two chained kernels onto `stream`, one behind the other: the first stores
* i + 1 over the 64 words at `x`; the second loads x[0] the way a kernel loads a value all its
* lanes share (a scalar load) and each of its `blocks` blocks stores what it saw, at
* seen[i * blocks + block]. Every reader must have seen the store in front of it
* (tests/device_test.cpp). RAD_E_UNSUPPORTED in a build without the backend. */
int chain_probe(void* stream, unsigned* x, unsigned* seen, unsigned pairs, unsigned blocks);
/* `n` chained launches onto `stream`, launch i storing i + 1 at seen[i]: each takes where to
* store from its own arguments, so a launch that read another's leaves a word unwritten
* (tests/device_test.cpp). RAD_E_UNSUPPORTED in a build without the backend. */
int chain_marks(void* stream, unsigned* seen, unsigned n);
/* `n` chained launches onto `stream`, each a card-side copy of `bytes` (a multiple of 16) from
* `src` to `dst` that also stores at seen[i] whether it found a chain record in its argument
* slot: i + 1 if every block did, that with the top bit set if one did not. A block that found
* none still counts, so the launches behind it are not left waiting (tests/device_test.cpp).
* RAD_E_UNSUPPORTED in a build without the backend. */
int chain_asks(void* stream, const void* src, void* dst, uint64_t bytes, unsigned* seen, unsigned n);
} // namespace aql
} // namespace rad
@@ -90,4 +109,10 @@ int rad_aql_blit_fill(void* dst, int value, uint64_t bytes, void* stream);
/* rad_memcpy_ranges_async's kernel: `prefix` a multiple of 16, both bases 16-byte aligned. */
int rad_aql_blit_ranges(void* dst, const void* src, uint64_t prefix, const RadCopyRange* ranges,
int n, void* stream);
/* aql::chain_probe's two kernels, aql::chain_marks's one and aql::chain_asks's one. */
int rad_aql_probe_store(unsigned* x, unsigned v, void* stream);
int rad_aql_probe_load(const unsigned* x, unsigned* seen, unsigned blocks, void* stream);
int rad_aql_probe_mark(unsigned* seen, unsigned v, void* stream);
int rad_aql_probe_ask(const void* src, void* dst, uint64_t bytes, unsigned* seen, unsigned v,
unsigned long long* ctr, void* stream);
}
+69
View File
@@ -250,6 +250,75 @@ extern "C" int rad_aql_blit_ranges(void* dst, const void* src, uint64_t prefix,
return launched();
}
/* aql::chain_probe's pair (aql.h). The reader's load has one address for the whole wave and
* nothing in the kernel writes there, so the compiler makes it a scalar load: served from the
* scalar data cache, which the wait must have emptied (chain_kernel.h) for the kernel to see the
* store in front of it. */
namespace {
__global__ __launch_bounds__(64) void r4dchain_probe_store(unsigned* __restrict__ x, unsigned v) {
rad_chain_wait();
x[threadIdx.x] = v;
rad_chain_done();
}
__global__ __launch_bounds__(32) void r4dchain_probe_load(const unsigned* __restrict__ x,
unsigned* __restrict__ seen) {
rad_chain_wait();
const unsigned v = x[0];
if (threadIdx.x == 0) seen[blockIdx.x] = v;
rad_chain_done();
}
/* aql::chain_marks's kernel: where it stores comes from its arguments alone. */
__global__ __launch_bounds__(32) void r4dchain_probe_mark(unsigned* __restrict__ seen, unsigned v) {
rad_chain_wait();
if (threadIdx.x == 0) seen[v - 1u] = v;
rad_chain_done();
}
/* aql::chain_asks's kernel: a card-side copy like r4dchain_copy16 that says whether there was a
* chain record in its argument slot when it started. A block that found none counts nothing in
* rad_chain_done, so it counts by the address in its arguments instead and the launches behind
* it do not wait for it for good. */
__global__ __launch_bounds__(kThreads) void r4dchain_probe_ask(const u32x4* __restrict__ src,
u32x4* __restrict__ dst, uint64_t n,
unsigned* __restrict__ seen, unsigned v,
unsigned long long* __restrict__ ctr) {
const bool found = rad_chain_rec() != nullptr;
rad_chain_wait();
const uint64_t stride = (uint64_t)gridDim.x * kThreads;
for (uint64_t i = (uint64_t)blockIdx.x * kThreads + threadIdx.x; i < n; i += stride) dst[i] = src[i];
if (threadIdx.x == 0)
__hip_atomic_fetch_or(&seen[v - 1u], found ? v : (v | 0x80000000u), __ATOMIC_RELAXED,
__HIP_MEMORY_SCOPE_AGENT);
rad_chain_done();
if (!found && threadIdx.x == 0)
__hip_atomic_fetch_add(ctr, 1ull, __ATOMIC_RELEASE, __HIP_MEMORY_SCOPE_AGENT);
}
} // namespace
extern "C" int rad_aql_probe_ask(const void* src, void* dst, uint64_t bytes, unsigned* seen, unsigned v,
unsigned long long* ctr, void* stream) {
const uint64_t n = bytes / 16;
hipLaunchKernelGGL(r4dchain_probe_ask, dim3(blocks_for(n, 4 * kThreads)), dim3(kThreads), 0,
(hipStream_t)stream, (const u32x4*)src, (u32x4*)dst, n, seen, v, ctr);
return launched();
}
extern "C" int rad_aql_probe_mark(unsigned* seen, unsigned v, void* stream) {
hipLaunchKernelGGL(r4dchain_probe_mark, dim3(1), dim3(32), 0, (hipStream_t)stream, seen, v);
return launched();
}
extern "C" int rad_aql_probe_store(unsigned* x, unsigned v, void* stream) {
hipLaunchKernelGGL(r4dchain_probe_store, dim3(1), dim3(64), 0, (hipStream_t)stream, x, v);
return launched();
}
extern "C" int rad_aql_probe_load(const unsigned* x, unsigned* seen, unsigned blocks, void* stream) {
if (blocks == 0) return 0;
hipLaunchKernelGGL(r4dchain_probe_load, dim3(blocks), dim3(32), 0, (hipStream_t)stream, x, seen);
return launched();
}
extern "C" int rad_aql_blit_fill(void* dst, int value, uint64_t bytes, void* stream) {
if (bytes == 0) return 0;
const hipStream_t s = (hipStream_t)stream;
+5
View File
@@ -37,6 +37,11 @@ __device__ __forceinline__ void rad_chain_wait() {
__builtin_amdgcn_s_sleep(1);
__syncthreads();
__builtin_amdgcn_fence(__ATOMIC_ACQUIRE, "agent");
/* The fence empties the vector caches. A load every lane shares may be a scalar one, and the
* scalar data cache still holds what a launch read from the address before the launch in front
* of this one wrote it: only the packet processor's acquire empties that cache, and it runs
* when the kernel is launched, before the launch in front has written. */
asm volatile("s_dcache_inv" ::: "memory");
}
__device__ __forceinline__ void rad_chain_done() {