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

+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);
}