Archived
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:
1 parent
78ad2096a3
commit
8f2e511fcc
6 files changed
+315
-4
No files matched your search
@@ -137,6 +137,123 @@ TEST(chained_launches_follow_the_plugins_ask_on_the_aql_backend_only) {
|
||||
CHECK_EQ(rad_dev_chain_enabled(), 0);
|
||||
}
|
||||
|
||||
/* A chained kernel reads behind its wait what the launch in front of it wrote, a value all its
|
||||
* lanes share included. The compiler loads such a value through the scalar data cache, which no
|
||||
* fence it emits empties: only the packet processor's acquire does, when the kernel is launched,
|
||||
* before the launch in front has written. The wait must therefore empty that cache itself
|
||||
* (chain_kernel.h): one reader in 1800 saw the value the reader before it loaded on an R9700
|
||||
* before it did, with the packets' acquire on. Behind a gate, so the card has the readers launched
|
||||
* ahead of the stores they wait for, as a played pass has them. */
|
||||
TEST(a_chained_kernel_sees_a_shared_value_the_launch_in_front_wrote) {
|
||||
if (std::strcmp(rad::device_backend_name(), "aql") != 0) SKIP("launches chain on the AQL backend only");
|
||||
const unsigned pairs = 400, blocks = 256;
|
||||
Stream st;
|
||||
DevBuf<unsigned> x(64 * sizeof(unsigned), RAD_MEM_DEVICE);
|
||||
DevBuf<unsigned> seen((int64_t)pairs * blocks * sizeof(unsigned), RAD_MEM_DEVICE);
|
||||
REQUIRE(x && seen);
|
||||
std::vector<unsigned> got((size_t)pairs * blocks);
|
||||
{
|
||||
rad_dev_chain_infer(1);
|
||||
CHECK_OK(rad_memset_async(x, 0, 64 * sizeof(unsigned), st.s));
|
||||
CHECK_OK(rad_memset_async(seen, 0, (int64_t)pairs * blocks * sizeof(unsigned), st.s));
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
void* gate = nullptr;
|
||||
REQUIRE_EQ(rad_stream_gate(st.s, &gate), RAD_OK);
|
||||
REQUIRE(rad_gate_room(st.s) >= (int64_t)2 * pairs);
|
||||
CHECK_OK(rad::aql::chain_probe(st.s, x, seen, pairs, blocks));
|
||||
CHECK_OK(rad_gate_open(gate));
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
CHECK_OK(rad_memcpy_async(got.data(), seen, (int64_t)pairs * blocks * sizeof(unsigned), st.s));
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
int64_t stale = 0;
|
||||
for (unsigned i = 0; i < pairs; ++i)
|
||||
for (unsigned b = 0; b < blocks; ++b) stale += got[(size_t)i * blocks + b] != i + 1u;
|
||||
CHECK_EQ(stale, 0);
|
||||
}
|
||||
rad_dev_chain_infer(0);
|
||||
}
|
||||
|
||||
/* A chained launch reads its own arguments and its chain record from a slot of the argument ring
|
||||
* that the host wrote and that another launch read a lap of the ring ago; the L2 still holds what
|
||||
* that launch read. Only a system-scope acquire of the packet processor's makes the card forget
|
||||
* it: the one the ring asks for every half lap. Three laps of launches that each store where their own arguments say,
|
||||
* 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). */
|
||||
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;
|
||||
Stream st;
|
||||
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), st.s));
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
CHECK_OK(rad::aql::chain_marks(st.s, seen, n));
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
CHECK_OK(rad_memcpy_async(got.data(), seen, (int64_t)n * sizeof(unsigned), st.s));
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
unsigned wrong = 0;
|
||||
for (unsigned i = 0; i < n; ++i) wrong += got[i] != i + 1u;
|
||||
CHECK_EQ(wrong, 0u);
|
||||
}
|
||||
rad_dev_chain_infer(0);
|
||||
}
|
||||
|
||||
/* The host writes a launch's arguments and chain record into VRAM through the BAR, and the card's
|
||||
* host data path keeps such a write until it is flushed (aql.cpp, host_writes_out). A launch the
|
||||
* card starts within a microsecond or two of its doorbell otherwise reads the slot as it was. What
|
||||
* it reads first is its chain record, so that is what this asks every launch for, on every card:
|
||||
* a lap of card-side copies with chaining off leaves every slot with their arguments and a record
|
||||
* no kernel accepts; then the same copies chained and 50 us apart, the host quiet between them as
|
||||
* the mover is between the copies of a loan recall, each saying whether it found a record. One
|
||||
* that does not would count nothing and stall the queue; the probe counts for it. The packets
|
||||
* keep the packet processor's acquire here, whose 0.9 us is all that gave the slot time to arrive
|
||||
* before the flush in front of the doorbell. */
|
||||
TEST(a_copy_issued_after_a_pause_finds_the_record_written_for_it) {
|
||||
if (std::strcmp(rad::device_backend_name(), "aql") != 0) SKIP("the AQL backend's argument ring");
|
||||
const unsigned lap = 4096 + 64, words = 8192;
|
||||
const int64_t bytes = 256 * 1024;
|
||||
for (int card = 0; card < rad_dev_count(); ++card) {
|
||||
REQUIRE_EQ(rad_dev_set(card), RAD_OK);
|
||||
DevBuf<unsigned> seen((int64_t)words * sizeof(unsigned), RAD_MEM_DEVICE);
|
||||
DevBuf<uint8_t> src(bytes, RAD_MEM_DEVICE), dst(bytes, RAD_MEM_DEVICE);
|
||||
REQUIRE(seen && src && dst);
|
||||
std::vector<unsigned> got(words);
|
||||
{
|
||||
const unsigned n = 1500;
|
||||
Stream st;
|
||||
rad_dev_chain_infer(0);
|
||||
CHECK_OK(rad::aql::chain_asks(st.s, src, dst, (uint64_t)bytes, seen, lap));
|
||||
CHECK_OK(rad_memset_async(seen, 0, (int64_t)words * sizeof(unsigned), st.s));
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
rad_dev_chain_infer(1);
|
||||
for (unsigned i = 0; i < n; ++i) {
|
||||
/* In runs of eight with a host wait between them, as a recall ends in one. */
|
||||
if (i % 8 == 0) CHECK_OK(rad_stream_sync(st.s));
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
while (std::chrono::steady_clock::now() - t0 < std::chrono::microseconds(50)) {}
|
||||
CHECK_OK(rad::aql::chain_asks(st.s, src, dst, (uint64_t)bytes, seen + i, 1));
|
||||
}
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
CHECK_OK(rad_memcpy_async(got.data(), seen, (int64_t)words * sizeof(unsigned), st.s));
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
/* A launch that read the slot as the lap left it stores where that launch did. */
|
||||
unsigned without = 0, missing = 0;
|
||||
for (unsigned i = 0; i < words; ++i) {
|
||||
without += (got[i] & 0x80000000u) != 0;
|
||||
missing += i < n && got[i] != 1u;
|
||||
}
|
||||
CHECK_EQ(without, 0u);
|
||||
CHECK_EQ(missing, 0u);
|
||||
}
|
||||
rad_dev_chain_infer(0);
|
||||
}
|
||||
rad_dev_set(0);
|
||||
}
|
||||
|
||||
/* An engine of more than one rank claims its cards (rad_dev_cards_claim). The claim that makes a
|
||||
* card shared returns only once everything issued on that card's queues before it has finished:
|
||||
* from then on no chained launch waits on the card behind a collective, and nothing issued under
|
||||
|
||||
Reference in new issue
Block a user