diff --git a/core/device/aql.cpp b/core/device/aql.cpp index 47965ae3..2b94021b 100644 --- a/core/device/aql.cpp +++ b/core/device/aql.cpp @@ -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; diff --git a/tests/device_test.cpp b/tests/device_test.cpp index f1fa771a..8b788785 100644 --- a/tests/device_test.cpp +++ b/tests/device_test.cpp @@ -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 seen((int64_t)n * sizeof(unsigned), RAD_MEM_DEVICE); + REQUIRE(!!seen); + std::vector 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;