Skip to content

hk.arch

Stability: stable, except where an entry says otherwise (stability).

hk.arch: the machine -- where this workgroup and warp are, and the instructions that order and schedule them.

hk.arch.block_idx

hk.arch.block_idx()

hk.arch.block_idx.x / .y / .z: hoisted, read once per kernel.

hk.arch.warp_id

hk.arch.warp_id() -> Int

This warp's index in the workgroup (wave-uniform).

hk.arch.lane_id

hk.arch.lane_id() -> Int

This lane's index in its wave: 0..31 on gfx11, 0..63 on gfx950. Not wave-uniform, so not a branch condition for a workgroup operation.

hk.arch.xcd_id

hk.arch.xcd_id() -> Int

The XCD this workgroup runs on, 0..7 on a gfx950 (read from the hardware: s_getreg HW_REG_XCC_ID), wave-uniform. Workgroups are dealt to the XCDs round robin, so it is usually block id mod 8 -- but a kernel that relies on sharing an XCD's L2 should ask, not assume.

hk.arch.xcd_count

hk.arch.xcd_count() -> int

XCDs (L2 domains) of the GPU this kernel is traced for, a constant: 8 on an MI355X, 4 on an MI350P or a DPX partition, 1 on a part with one L2 (KFD's num_xcc, else the arch's default; HK_XCDS overrides). What hk.dsl.placement deals workgroups over.

hk.arch.barrier

hk.arch.barrier(drain: bool = False) -> None

s_barrier: orders execution across the workgroup, not memory. drain=True spends an lgkmcnt(0) in front of it.

hk.arch.sched_barrier

hk.arch.sched_barrier(allow=()) -> None

Nothing but the classes in allow crosses this line in the scheduler; it costs no instruction. allow is a class name, several, or a mask.

hk.arch.sched_group_barrier

hk.arch.sched_group_barrier(classes, size: int, sync_id: int = 0) -> None

Ask the scheduler for size instructions of classes here, after the groups before this one with the same sync_id: a run of these is an interleave. classes is a name from SCHED_CLASSES, several, or a mask.

for _ in range(4):                     # one staging load, two WMMAs
    hk.arch.sched_group_barrier("vmem_read", 1)
    hk.arch.sched_group_barrier("mma", 2)

hk.arch.SCHED_CLASSES

The instruction classes of __builtin_amdgcn_sched_barrier and sched_group_barrier, by name. An inline-asm instruction is in none of them: include/rdna3's LDS fragment reads and writes are asm, its staging loads and WMMAs are not.

hk.arch.pingpong

hk.arch.pingpong(behind) -> Iterator[None]

Run the warps where behind holds one barrier behind the rest.

On entry those warps wait at one extra s_barrier; on exit the others do, and the halves meet again. In between, every barrier pairs a program point in one half with the next one in the other, so a schedule that alternates memory and math between barriers runs the two halves out of phase: with eight warps, the two waves on a SIMD take turns at the matrix core. HipKittens' CDNA4 GEMM is built on it.

with hk.arch.pingpong(warp_id // 4 == 1):
    ...                     # memory, barrier, math, barrier, ...

behind is wave-uniform (from warp_id, not lane_id). hk-lds-pipeline knows the skew: inside, a warp is only sure another has passed a point two barriers later, not one.

hk.arch.pin

hk.arch.pin(*values) -> None

Keep these register tiles' and vectors' values here: what computes them stays above this point and what reads them below. No instruction. sched_barrier fences only the scheduler; LLVM still sinks a value's computation to its first use, across barriers and into another phase of a hand-written schedule. A value with an LDS read in flight is refused, as any read of it is, until it is waited for.

hk.arch.setprio

hk.arch.setprio(level: int) -> None

s_setprio: this wave's issue priority, 0..3.

hk.arch.vm_wait

hk.arch.vm_wait(keep: int = 0) -> None

s_waitcnt vmcnt(keep): retire vector-memory loads, keep in flight.

hk.arch.wg_arrive

hk.arch.wg_arrive(counter, expected: Scalar, *, scope: str = 'device') -> Int

Count this workgroup's arrival at counter (an element view of an int32 global) and return how many arrived before it. The last to arrive resets the counter. Contains barriers: every warp must reach it.

scope="xcd": every workgroup arriving at this counter runs on one XCD of a gfx950 (block ids equal modulo 8: workgroups are dealt to the XCDs round robin), so the stores before it need only reach the shared L2 -- no device-wide fence, which writes back and invalidates the L2 under every other workgroup of the XCD. The caller guarantees the placement.

hk.arch.mma_fence

hk.arch.mma_fence(on: bool = True) -> None

Whether hk-mma-fence guards this kernel (CDNA; on by default). The pass puts a scheduling fence between a matrix-core chain and the first other access to its accumulator: on gfx950 LLVM threaded accumulator reads through AGPR MFMA chains, read one a branch after its MFMA with too few wait states (a hazard recognizer bug: ROCm/llvm-project#5153) and computed wrong scores. Turning it off is for an A/B, not a fix.

hk.arch.agpr

hk.arch.agpr(tile) -> None

Surface: an experiment: behind an option that is off by default; experimental, and may be removed without notice.

Hold tile, an fp32 MMA accumulator just made by hk.zeros, in AGPRs (CDNA): every mma into it runs on the AGPRs (inline asm, C/D "+a"), every other access reads it into a VGPR copy after the MFMAs' wait states and writes it back (include/hk/cdna_agpr.cuh). Outputs are bitwise the VGPR form's. LLVM picks the VGPR form unless register pressure forces AGPRs, and an asm MFMA is one its scheduler cannot see: measured slower on attn_cdna, so the kernels that offer it keep it off by default.

Experimental: it may be removed without notice.

hk.arch.target

hk.arch.target()

The hk.target.Target this kernel is being traced for: wave size, register file, matrix-core fragments. For a kernel body whose tiling is a property of the target, chosen while tracing.