Applies software prefetch (double-buffering) to FlyDSL GPU kernel loops, overlapping async data loads with compute to hide memory latency. Use when buffer_load feeds into MFMA/compute and load latency is exposed.
Ships in flydsl. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Apply software prefetch (double-buffering) to overlap async data loads with compute in FlyDSL GPU kernel loops.
API note. The worked examples below are transcribed from the PA decode
kernel and are schematic: they demonstrate the loop structure — prologue,
loop-carried state, epilogue — not a copy-pasteable kernel. The loads are still
spelled as raw buffer_ops.buffer_load, which now lives in
kernels/common/buffer_ops.py (moved out of flydsl.expr in #880); for new
code use fx.rocdl.make_buffer_tensor + fx.copy instead. The MMA is shown in
the current atom form (mma = fx.make_mma_atom(...), then fx.gemm(mma, d, a, b, c)); the raw rocdl.mfma_* intrinsics take (result_type, operands), not
(a, b, acc). See the kernel-code-cleanup skill for the full mapping.
GPU global memory loads (buffer_ops.buffer_load, buffer_load_dwordx4)
are asynchronous -- the load instruction returns immediately and the
hardware fetches data in the background. The data is only needed when a
subsequent instruction actually consumes it. If we issue the load early
enough, the data arrives by the time we need it, effectively hiding the load
latency behind compute work.
Without prefetch (load latency fully exposed):
for i in range(N):
data = load(ptr + i) # <-- stall: wait for data
result = compute(data) # <-- cannot start until load completes
Timeline:
|--load--|--stall--|--compute--|--load--|--stall--|--compute--|
With prefetch (load overlapped with compute):
# Pre-load first iteration BEFORE the loop
next_data = load(ptr + 0)
for i in range(N):
# Swap: the prefetched data becomes current
data = next_data
# Issue load for NEXT iteration (async, non-blocking)
if i + 1 < N:
next_data = load(ptr + i + 1)
# Compute using CURRENT data -- overlaps with next load
result = compute(data)
Timeline:
|--load₀--|--compute₀ + load₁--|--compute₁ + load₂--|--compute₂--|
The total time drops from N * (load + compute) to roughly
load + N * max(load, compute).
range(..., init=...) with Loop-Carried PrefetchIn FlyDSL kernels, Python-level for _pi in range(N) gets traced into N flat
copies that LLVM re-rolls. This makes the data = next_data swap invisible
to MLIR — both variables alias the same SSA value, so LLVM hoists loads as
loop-invariant.
Solution: Use FlyDSL's runtime range(..., init=...) (loop-carried values) to
create genuine SSA phi nodes. See the flydsl-kernel-authoring skill, section
"Runtime Loops with Loop-Carried Values", for the full pattern and three critical
pitfalls.
Given a loop like:
for i in range(START, END):
# === LOAD PHASE ===
offsets = compute_offsets(i)
data_A = buffer_ops.buffer_load(rsrc_A, offsets, vec_width=4)
data_B = buffer_ops.buffer_load(rsrc_B, offsets, vec_width=4)
# === COMPUTE PHASE ===
fx.gemm(mma, acc, transform(data_A), transform(data_B), acc) # d, a, b, c
Apply the following transformation using range(..., init=...):
offsets_0 = compute_offsets(START)
next_data_A = buffer_ops.buffer_load(rsrc_A, offsets_0, vec_width=4)
next_data_B = buffer_ops.buffer_load(rsrc_B, offsets_0, vec_width=4)
init_state = [_unwrap(v) for v in [next_data_A, next_data_B, acc]]
_start = fx.Int64(0)
_stop = fx.Int64(N - 1) # N-1 iterations; last handled in epilogue
_step = fx.Int64(1)
for iv, state in range(_start, _stop, _step, init=init_state):
# Swap: prefetched -> current
data_A = state[0]
data_B = state[1]
acc = state[2]
# Prefetch next iteration (async, non-blocking)
offsets_next = compute_offsets(iv + 1)
next_data_A = buffer_ops.buffer_load(rsrc_A, offsets_next, vec_width=4)
next_data_B = buffer_ops.buffer_load(rsrc_B, offsets_next, vec_width=4)
# Compute using current data (overlaps with next load)
fx.gemm(mma, acc, transform(data_A), transform(data_B), acc)
results = yield [_unwrap(v) for v in [next_data_A, next_data_B, acc]]
data_A = results[0]
data_B = results[1]
acc = results[2]
fx.gemm(mma, acc, transform(data_A), transform(data_B), acc)
Any offset calculations, block table lookups, or scale factor loads needed for the next iteration's data should also be carried as loop state:
init_state = [_unwrap(v) for v in [
next_data_A, next_data_B, next_block_id, next_scale, acc
]]
for iv, state in range(_start, _stop, _step, init=init_state):
data_A, data_B, block_id, scale, acc = state
# Prefetch next iteration
next_block_id = load_block_table(iv + 1)
offsets_next = compute_offsets(iv + 1, next_block_id)
next_data_A = buffer_ops.buffer_load(rsrc_A, offsets_next, vec_width=4)
next_data_B = buffer_ops.buffer_load(rsrc_B, offsets_next, vec_width=4)
next_scale = buffer_ops.buffer_load(rsrc_scale, next_block_id, vec_width=1)
# Compute with current data
fx.gemm(mma, acc, transform(data_A) * scale, transform(data_B), acc)
results = yield [_unwrap(v) for v in [
next_data_A, next_data_B, next_block_id, next_scale, acc
]]
State inventory (15 values carried across iterations):
vector<4xi32> — K data (4 tiles x 2 loads)i32 — partition_starti32 — block table values (phys_block/page_off or phys_0/phys_1)f32 — running_max, running_sum (online softmax)vector<4xf32> — PV accumulators# Pack/unpack helpers
def _pack(kv_flat, part_start, bt_vals, rmax, rsum, acc_pv):
raw = kv_flat + [part_start] + bt_vals + [rmax, rsum] + acc_pv
return [v.ir_value() if hasattr(v, 'ir_value') else v for v in raw]
def _unpack(state):
kv_flat = list(state[0:8])
kv = [[kv_flat[t*2], kv_flat[t*2+1]] for t in range(4)]
return kv, state[8], list(state[9:11]), state[11], state[12], [state[13], state[14]]
# Prologue
pf_0 = issue_bt_k_loads(partition_0)
init_state = _pack(flatten(pf_0['kv']), pf_0['part_start'], ...)
# Runtime loop (bounds MUST be a typed DSL integer like fx.Int64, not Python ints!)
for iv, state in range(fx.Int64(0), fx.Int64(N - 1), fx.Int64(1), init=init_state):
kv, part_start, bt, rmax, rsum, acc = _unpack(state)
rmax, rsum, acc = compute_qk_softmax_pv(kv, part_start, bt, rmax, rsum, acc)
pf_next = issue_bt_k_loads(next_partition(iv + 1))
results = yield _pack(flatten(pf_next['kv']), pf_next['part_start'], ...)
# Epilogue: compute last partition, write output
kv, part_start, bt, rmax, rsum, acc = _unpack(results)
compute_qk_softmax_pv(kv, part_start, bt, rmax, rsum, acc)
write_output(rmax, rsum, acc)
ISA result: 8 K-prefetch buffer_load_dwordx4 appear at the END of the
loop body (after PV MFMA), overlapping with the MFMA pipeline drain. The
prologue has 8 K loads before the loop. The epilogue has 8 V loads only (no
K loads needed).
Loop bounds must be a typed DSL integer such as fx.Int64(...), NOT a
Python int. A plain int makes the AST rewriter unroll the loop and silently
ignore init=. If you write range(0, 15, 1, init=...), the AST rewriter
treats the constant bounds as a Python range and unrolls; only plain
Python-int bounds are unrolled, so a typed bound still produces a runtime
scf.for (the rewriter index-casts non-Python-int bounds into scf.for).
Use fx.Int64(0), fx.Int64(15), fx.Int64(1) instead.
Prefer internal types; unwrap only at hard boundaries. Most loop-carried
values can remain fx.Int32, fx.Float32, or fx.Vector. Prefer these
concrete types over wrapping a raw value in ArithValue directly -- note
fx.Vector subclasses ArithValue, so this is about which constructor you
reach for, not about avoiding the base class. If a
low-level helper explicitly expects raw ir.Value, unwrap at that boundary.
Build LDS views at the top of the kernel, not inside the runtime loop.
Allocate shared memory with fx.SharedAllocator().allocate(...).peek() and
build each .view() once up front. A view created inside the scf.for body
is defined in the loop scope; using it in the epilogue (outside the loop)
causes an SSA dominance error. Building it once at the top makes it dominate
both the loop and the epilogue.
This optimization applies whenever you see this pattern in a kernel:
| Signal | Description |
|---|---|
for ... in range(N) loop with buffer_load followed by MFMA | Load-then-compute in a loop body |
| Block table lookup inside loop | buffer_load(block_table_rsrc, idx) followed by buffer_load(cache_rsrc, page_id * stride) |
| KV cache iteration | Paged attention, flash attention, any tiled GEMM with paged memory |
| Scale factor loads | FP8 per-token quantization scales loaded per KV block |
FlyDSL kernels compile to GCN ISA where s_waitcnt insertion is controlled by
the compiler, not by the programmer. You cannot directly eliminate s_waitcnt
instructions. Instead, prefetch restructures the code so the compiler places
s_waitcnt after enough compute work to hide the latency.
Always check register headroom before adding prefetch buffers:
On CDNA3 (gfx942 MI300X/MI308), VGPRs are tracked as two physical files that share one combined 512-entry occupancy budget per SIMD:
Prefetch buffers physically live in arch_vgpr and MFMA accumulators in
accum_vgpr, but occupancy is governed by their sum (arch_vgpr + accum_vgpr), so growing prefetch buffers does compete with MFMA accumulators
for the shared 512 budget and can cost occupancy.
# Estimate arch_vgpr cost of prefetch buffers:
# - Each buffer_load_dwordx4 = 4 arch_vgpr per load
# - 8 K-cache loads = 8 x 4 = 32 arch_vgpr for one buffer set
# - Double-buffering = 2 x 32 = 64 arch_vgpr (but one set is reused)
# - Net additional arch_vgpr ~ 32 (the "next" buffer)
#
# On MI300X (gfx942): arch_vgpr + accum_vgpr share ONE combined 512 budget/SIMD
# Occupancy = 512 / (arch_vgpr_alloc + accum_vgpr_alloc) waves per SIMD
# (combined-pool model — NOT 256/max; that was gfx908/CDNA1 only)
#
# Example: arch=148, accum=148 -> combined 296 -> 512//296 = 1 wave
# Adding 32 arch_vgpr -> combined 328 -> still 1 wave (safe)
# To reach 2 waves you need combined (arch+accum) <= 256
# arch+accum > 512 -> SPILL (exceeds the combined per-SIMD budget)
Critical thresholds (gfx942, combined arch+accum budget):
| Combined arch_vgpr + accum_vgpr | Max Waves/SIMD | Impact |
|---|---|---|
| <= 128 | 4 | High occupancy |
| <= 170 | 3 | Good occupancy |
| <= 256 | 2 | Moderate occupancy |
| <= 512 | 1 | Minimum occupancy |
| > 512 | SPILL | Register overflow -> severe perf regression |
How to check current VGPR allocation (from rocprofv3 database):
SELECT ks.KernelName, ki.arch_vgpr_count, ki.accum_vgpr_count
FROM rocpd_kernel_dispatch kd
JOIN rocpd_info_kernel_symbol ks ON kd.kernel_symbol_id = ks.id
JOIN rocpd_info_kernel ki ON kd.kernel_id = ki.id
WHERE ks.KernelName LIKE '%target_kernel%'
LIMIT 5;
NOTE: maxnreg has been removed (it never reached LLVM). Forcing
accum_vgpr=0 to free register space for prefetch was never viable anyway: it
pushes MFMA results through arch_vgpr via v_accvgpr_read spills, measured at a
4.5x GPU kernel regression. Target occupancy with waves_per_eu.
CAN do:
buffer_load is issued earlier via range(..., init=...) loop-carried valuess_waitcnt further from the loadCANNOT do:
s_waitcnt vmcnt(N) counter valuesvmcnt(N>0) instead of vmcnt(0)s_barrier) — these come from explicit gpu.barrier() or cross-wave reduce primitivesA powerful technique specific to multi-phase kernels (like paged attention with softmax reduce):
If a kernel has a phase that spends time in s_barrier waits (e.g., softmax
cross-wave reduce), and the next phase needs data from global memory (e.g.,
V-value loads), hoist those loads into the barrier-stalling region. The barrier
must wait regardless — issuing loads during that wait is essentially free.
# BEFORE: V-value loads happen AFTER softmax reduce completes
softmax_reduce(qk_scores) # <-- 96K stall cycles in barriers
v_data = buffer_ops.buffer_load(rsrc_v, offsets, vec_width=4) # <-- additional load latency
# AFTER: V-value loads issued BEFORE/DURING softmax reduce
v_data_prefetch = buffer_ops.buffer_load(rsrc_v, offsets, vec_width=4) # <-- async, non-blocking
softmax_reduce(qk_scores) # <-- barrier stalls now overlap with v_data fetch
v_data = v_data_prefetch # <-- data likely already arrived
This works because:
buffer_load returns immediately (async)Writing a load early in the Python source does not mean it is issued early.
LLVM's machine scheduler reorders instructions within each basic block, and it
may move a load down next to the instruction that consumes it, for example to
lower register pressure. Basic-block boundaries limit it: a runtime if in the
loop body (lowered to scf.if) is a boundary, and loads above it stay above it.
A kernel can depend on such a branch without anyone noticing. Removing or
peeling the branch then moves the loads, even though no load code changed.
Observed on aiter's FlyDSL pa_decode (gfx950): peeling a per-tile validity
if out of the KV loop let the scheduler sink the V loads down to the PV MFMAs,
and bf16 cases with page size 64/128 got 17% slower. Putting an explicit
fence where the branch used to be restored the early issue, and the change then
cut geomean kernel time by 3.4%:
v_next = load_v_tile(...) # meant to be in flight during QK + softmax
rocdl.sched_barrier(0) # nothing is scheduled across this point
qk = compute_qk(...)
Whenever you add or remove a runtime branch in a hot loop, or move loads, diff
the final ISA (FLYDSL_DUMP_IR=1 FLYDSL_DUMP_DIR=<dir>, then *_final_isa.s).
Check where the buffer_load/global_load instructions sit relative to the
MFMAs and to the s_waitcnt vmcnt that guards their use. Add
rocdl.sched_barrier(0) only where the dump shows that a load moved.
range(..., init=...) to carry prefetched data across iterations (Python variable swap is invisible to MLIR)state, no computationarch=256, accum=256 plus a non-zero spill count.
Treat any spill as the regression; staying at or under 256 combined is what
buys a 2nd wave/SIMD. See "Register Budget" above.v.ir_value() if hasattr(v, 'ir_value') else vwaves_per_eu / occupancy.load_B depends on the result of load_A (e.g., block table lookup -> cache load), they must stay sequential within the prefetch block.KV_QUANT_MODE), the prefetch must replicate the same conditions.results.init=: use a typed DSL integer such as fx.Int64(...), NOT a plain int, or the AST rewriter will unroll the loop and silently ignore init=.After applying prefetch:
rocprofv3 --kernel-trace for this: profiler overhead dominates kernels of a few microseconds. For stalls, capture ATT (/kernel-trace-analysis) and look for:
VMEM stall cycles in the loop bodywaves_per_eu (occupancy) didn't drop. If it did, consider prefetching fewer buffers (e.g., only keys, not values).range(1)): no next iteration to prefetchWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
/flydsl:prefetch-data-loadfrom flydsl
npx claudepluginhub rocm/flydsl --plugin flydsl16 skills in this plugin
/add-target-atom-opAdd a new target-specific Mma / Copy Op type to a FlyDSL backend dialect (`lib/Dialect/Fly<TARGET>/<SUBTARGET>/` + `include/flydsl/Dialect/Fly<TARGET>/IR/`). Covers the MmaOp/CopyOp type design, the stateful-vs-stateless variants, and the `emitAtomCall` / `emitAtomCallSSA` lowering contract to the backend dialect (LLVM/ROCDL/NVVM/SPIR-V/...). Use when adding a new tensor-core / matrix instruction (MFMA, WMMA, HMMA, WGMMA, ...), a new buffer / shared-memory / global copy atom, a new stateful copy (per-atom offset or descriptor), or bringing up a new backend dialect (`FlyPTX`, `FlyCPU`, ...). Reference implementations live in `FlyROCDL`. Usage: /add-target-atom-op
/bisect-perf-regressionFind the exact commit that caused a GPU kernel performance regression using binary search (git bisect). Given a good commit (fast), a bad commit (slow, defaults to HEAD), and a benchmark command, automatically checks out commits, runs the benchmark, extracts the metric, and narrows down to the offending commit. Reports the regression commit with its diff and suggested root cause. Usage: /bisect-perf-regression <good_commit> [bad_commit] -- <bench_cmd>
/build-flydslBuild and install FlyDSL (Flexible Layout Python DSL) on a remote host or Docker container. FlyDSL is a Python DSL and MLIR-based compiler stack for authoring high-performance GPU kernels with explicit layouts and tiling on AMD GPUs. Requires building LLVM/MLIR from source (~30min) then FlyDSL C++ and Python bindings (~5min). Usage: /build-flydsl [container@host]
/build-rocm-imageConnect to a remote host via SSH and build a Docker image with rocprofv3, aiter, and FlyDSL. Use when user wants to build/rebuild the ROCm development image on a remote host. Usage: /build-rocm-image <hostname>
/capture-kernel-traceCapture GPU kernel ATT (Advanced Thread Trace) via rocprofv3 on a remote Docker container or locally. Discovers kernel names, configures input.yaml with the target kernel_include_regex, runs rocprofv3 -i input.yaml with FLYDSL_DEBUG_ENABLE_DEBUG_INFO=1, and downloads the latest ui_output_agent_* directory for analysis. Usage: /capture-kernel-trace <test_script.py> [kernel_name_pattern]
/debug-flydsl-kernelDebug FlyDSL GPU kernels that produce NaN, inf, wrong results, or crash. Covers cache invalidation, tracing pitfalls (runtime conditionals, range vs range_constexpr), loop-carried state packing, buffer_load addressing, MFMA operand layout verification, LDS bank conflict diagnosis, and systematic error isolation (all-1s test, single-partition test, host-side tensor inspection). Use when a FlyDSL kernel produces incorrect output or compilation errors. Usage: /debug-flydsl-kernel
Other plugins' skills for the same job