libr4d: skip the scalar-cache invalidate behind chained waits in kernels that make no scalar load after the wait

r4d_chain_wait() ends in r4d_chain_acquire(): the agent-scope fence for the
vector caches and s_dcache_inv for the scalar data cache. The latter is needed
only by a kernel that reads, behind the wait, something an earlier launch of the
chain wrote through a scalar load (a value all lanes share). s_dcache_inv also
costs the refetch of every kernel argument afterwards, on every wave.

New r4d_chain_wait_vec() waits with the fence alone. It is used at 41 wait
sites in 20 units (the IQR m16/m64, w2a8/w4a8/w8a8/bf16/i8_kpar and fp8a8
GEMMs, the activation quantisers, the attention decode and paged-window
kernels, hc_*, the norm and activation kernels of r4d_model_bf16, rowtopk,
ple_gate, prefetch, dflash_conv, the all-reduce). A wait site keeps the full
flush when any instantiation of its kernel has a scalar load after the acquire.
-DR4D_CHAIN_SCINV_ALWAYS puts the instruction behind every wait again.

tests/chain_scalar_audit.py (ctest chain_scalar_audit, label slow, no card)
keeps this honest: it compiles every chained HIP unit to gfx1201 assembly with
the flags of compile_commands.json and fails when a chained kernel without
s_dcache_inv has a scalar load, from a base other than the kernarg segment
s[0:1], behind its global_inv. Building the audit with
-DR4D_CHAIN_SURVEY_NO_SCALAR (every wait skips the flush) flags 1123 kernels,
so the check does detect the real cases; as committed it reports 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Code000andClaude Opus 5.5 committed 2026-11-17 20:43:11 +01:00
1 parent ca7a7212ec
commit e912b04f5a
23 files changed
+209 -45

No files matched your search

+5 -5
View File
@@ -407,7 +407,7 @@ __device__ __forceinline__ void hc_enter_body(HC_ENTER_PARAMS) {
template <class CH>
__global__ __launch_bounds__(kThreads)
void hc_enter_kernel(HC_ENTER_PARAMS) {
if (CH::on) r4d_chain_wait();
if (CH::on) r4d_chain_wait_vec();
hc_enter_body(x, h, rows, n, hc, xrow, hrow);
if (CH::on) r4d_chain_done();
}
@@ -2170,7 +2170,7 @@ void hc_read_fused_kernel(const unsigned short* __restrict__ h, const float* __r
long qrow, long srow, long rqrow, long rsrow, float wadd,
unsigned int* __restrict__ sync,
unsigned na, unsigned gxa, unsigned gxb, unsigned nb_all) {
if (CH::on) r4d_chain_wait();
if (CH::on) r4d_chain_wait_vec();
__shared__ unsigned s_ticket;
if (threadIdx.x == 0)
s_ticket = __hip_atomic_fetch_add(&sync[0], 1u, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT);
@@ -3076,7 +3076,7 @@ void hc_norm_down_tiled_kernel(const unsigned short* __restrict__ h, const float
long kc, float wadd, unsigned int* __restrict__ ctr,
unsigned int* __restrict__ sync, float eps,
unsigned nn, unsigned gdx, unsigned gdy, unsigned gdz) {
if (CH::on) r4d_chain_wait();
if (CH::on) r4d_chain_wait_vec();
__shared__ unsigned s_ticket;
if (threadIdx.x == 0)
s_ticket = __hip_atomic_fetch_add(&sync[0], 1u, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT);
@@ -3696,7 +3696,7 @@ __device__ __forceinline__ void hc_up_mix_tiled_body(HC_UMT_PARAMS) {
template <int RT, int NT, bool F8, bool I8, class CH>
__global__ __launch_bounds__(kThreads)
void hc_up_mix_tiled_kernel(HC_UMT_PARAMS) {
if (CH::on) r4d_chain_wait();
if (CH::on) r4d_chain_wait_vec();
hc_up_mix_tiled_body<RT, NT, F8, I8>(upv, uld, dbuf, h, w, scalein, x, q, qs, rq, rqs, ctr,
rows, n, hc, lr, hrow, xrow, qrow, srow, rqrow, rsrow,
wadd);
@@ -4761,7 +4761,7 @@ __device__ __forceinline__ void mtp_enter_tiled_body(MTP_ENTER_TILED_PARAMS) {
template <int RT, int NT, bool F8, class CH, int KT = kTKT>
__global__ __launch_bounds__(kThreads)
void mtp_enter_tiled_kernel(MTP_ENTER_TILED_PARAMS) {
if (CH::on) r4d_chain_wait();
if (CH::on) r4d_chain_wait_vec();
mtp_enter_tiled_body<RT, NT, F8, KT>(h, e, wh, we, fchv, fcev, fld, x, rows, n, hc, flat, eps, wadd, hrow, erow, xrow);
if (CH::on) r4d_chain_done();
}