Archived
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>
55 lines
2.3 KiB
C++
55 lines
2.3 KiB
C++
/* chain_kernel.h -- the chained form's wait and completion for the core's own kernels
|
|
* (RadKernelChainFn, abi/rad_abi.h).
|
|
*
|
|
* The backend gives a kernel named r4dchain* a record at RAD_CHAIN_KARG_OFF and drops its barrier
|
|
* bit behind another chained kernel. Before the first read one lane waits for the queue's counter
|
|
* to reach the record's target; after the last write the block releases and adds 1. A record that
|
|
* does not check out means: run plain. Every block must reach rad_chain_done exactly once.
|
|
*/
|
|
#pragma once
|
|
#include <hip/hip_runtime.h>
|
|
|
|
#include <cstdint>
|
|
|
|
#include "rad_abi.h"
|
|
|
|
struct RadChainRec {
|
|
unsigned long long magic, target;
|
|
unsigned long long* ctr;
|
|
unsigned long long check;
|
|
};
|
|
|
|
__device__ __forceinline__ const RadChainRec* rad_chain_rec() {
|
|
const RadChainRec* r = (const RadChainRec*)((const char*)__builtin_amdgcn_kernarg_segment_ptr() +
|
|
RAD_CHAIN_KARG_OFF);
|
|
const unsigned long long magic = r->magic, target = r->target, check = r->check;
|
|
if (magic != RAD_CHAIN_MAGIC ||
|
|
check != (magic ^ target ^ (unsigned long long)(uintptr_t)r->ctr ^ RAD_CHAIN_CHECK))
|
|
return nullptr;
|
|
return r;
|
|
}
|
|
|
|
__device__ __forceinline__ void rad_chain_wait() {
|
|
const RadChainRec* r = rad_chain_rec();
|
|
if (!r) return;
|
|
if (threadIdx.x == 0 && threadIdx.y == 0 && threadIdx.z == 0)
|
|
while (__hip_atomic_load(r->ctr, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT) < r->target)
|
|
__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() {
|
|
const RadChainRec* r = rad_chain_rec();
|
|
if (!r) return;
|
|
__builtin_amdgcn_fence(__ATOMIC_RELEASE, "agent");
|
|
__syncthreads();
|
|
if (threadIdx.x == 0 && threadIdx.y == 0 && threadIdx.z == 0)
|
|
__hip_atomic_fetch_add(r->ctr, 1ull, __ATOMIC_RELEASE, __HIP_MEMORY_SCOPE_AGENT);
|
|
}
|