aql: the prefetch stream takes the half-lap system acquire too

The expert-prefetch stream (rad_xp_stream_create, agent_only) downgraded every system-scope
acquire to agent scope because it reads no host-written data. Its kernel arguments are
host-written into its own ring, though: after a wrap, an agent-scope acquire leaves the slot's
old line in the L2 and the launch runs with the previous lap's arguments. Keep the exemption
for host writes and epochs; take the system acquire on the ring's half lap (one in 2048
prefetch launches). New device test: launches on a prefetch stream across three ring laps read
their own arguments -- 8709 of 12805 stale without the fix, 0 with it.

Fixes #491

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 20:32:20 +01:00
1 parent 01f08152dc
commit 946a6f9e2b
2 files changed
+33 -3

No files matched your search

+7 -3
View File
@@ -1245,9 +1245,13 @@ inline int acquire_scope(Stream* s, uint64_t idx, bool forced, bool* asked) {
if (*asked) {
s->last_sys_acq = (int64_t)idx;
s->epoch = ep;
/* The prefetch stream reads nothing the host writes; a system acquire there would only
* invalidate the L2 under the compute stream. */
if (s->agent_only && !forced) return HSA_FENCE_SCOPE_AGENT;
/* The prefetch stream reads no data the host writes, so a host write or epoch does not
* make it acquire at system scope (that would only invalidate the L2 under the compute
* stream). Its own kernel arguments are host-written into its ring like any stream's,
* though: after the ring laps, an agent-scope acquire leaves the slot's old line in the
* L2 and a launch could run with the previous lap's arguments. The half lap keeps the
* system acquire: one in 2048 prefetch launches. */
if (s->agent_only && !forced && !lap) return HSA_FENCE_SCOPE_AGENT;
return HSA_FENCE_SCOPE_SYSTEM;
}
return HSA_FENCE_SCOPE_AGENT;
+26
View File
@@ -191,6 +191,32 @@ TEST(a_chained_kernel_sees_a_shared_value_the_launch_in_front_wrote) {
* with no host wait between them: without that acquire every launch past the first lap stores
* where the launch a lap before did (as they do on a prefetch stream, which never takes one and
* counts on the pass beside it to have turned the L2 over). */
/* The prefetch stream (rad_xp_stream_create, agent-scope acquires) also reads its own arguments
* from a host-written ring: across three laps every launch must see its own slot. */
TEST(a_prefetch_stream_launch_reads_its_own_arguments_after_the_ring_has_lapped) {
if (std::strcmp(rad::device_backend_name(), "aql") != 0) SKIP("the prefetch stream is an AQL stream");
const unsigned n = 3 * 4096 + 517;
RadStream xs = nullptr;
if (rad_xp_stream_create(&xs) != RAD_OK || !xs) SKIP("no prefetch stream on this device");
DevBuf<unsigned> seen((int64_t)n * sizeof(unsigned), RAD_MEM_DEVICE);
REQUIRE(!!seen);
std::vector<unsigned> got(n);
{
rad_dev_chain_infer(1);
CHECK_OK(rad_memset_async(seen, 0, (int64_t)n * sizeof(unsigned), xs));
CHECK_OK(rad_stream_sync(xs));
CHECK_OK(rad::aql::chain_marks(xs, seen, n));
CHECK_OK(rad_stream_sync(xs));
CHECK_OK(rad_memcpy_async(got.data(), seen, (int64_t)n * sizeof(unsigned), xs));
CHECK_OK(rad_stream_sync(xs));
unsigned wrong = 0;
for (unsigned i = 0; i < n; ++i) wrong += got[i] != i + 1u;
CHECK_EQ(wrong, 0u);
}
rad_dev_chain_infer(0);
rad_stream_destroy(xs);
}
TEST(a_chained_launch_reads_its_own_arguments_after_the_ring_has_lapped) {
if (std::strcmp(rad::device_backend_name(), "aql") != 0) SKIP("launches chain on the AQL backend only");
const unsigned n = 3 * 4096 + 517;