Repository navigation
Description
What problem does this solve?
CTA-local shared-memory instruction addresses are a 32-bit hardware domain, but cuda-oxide currently exposes pointer-form ldmatrix APIs and cvta_generic_to_shared_offset -> u64. Pointer indexing therefore naturally uses Rust usize/u64 until each ldmatrix use truncates the shared pointer. In a register-heavy counted BF16 HMMA loop this leaves avoidable wide address recurrences and conversions, while mechanically rewriting the source loop can make libNVVM replicate the loop body early and spill the accumulator set.
The u64 value remains appropriate for WGMMA/tcgen descriptor construction. The missing contract is the .shared::cta.u32 state-space address operand used by instructions such as ldmatrix, plus a compiler-owned way to keep a proven counted register-MMA loop in one lowering lifetime.
Proposed API / behavior
The first layer follows the PTX carrier directly:
let base = unsafe { cvta_generic_to_shared_u32(smem.cast()) };
let address = base.wrapping_add(swizzle_offset_u32);
let a = unsafe { ldmatrix_x4_shared_u32(address) };
let b = unsafe { ldmatrix_x2_shared_u32(other_address) };Semantics:
cvta_generic_to_shared_u32produces a raw.shared::cta.u32state-space address.- Generic/shared pointer conversion happens once. Byte-offset and swizzle arithmetic can remain u32.
- Existing pointer-form
ldmatrix_*APIs remain available. - The existing WGMMA/tcgen u64 descriptor-address API does not change.
- Pointer and shared-u32 forms converge on the same
nvvm.ldmatrixsemantic operation rather than introducing duplicate dialect truth. The LLVM-NVPTX route may materialize an address-space-3 pointer at its intrinsic boundary; the libNVVM route can pass the u32 address directly to inline PTX.
A follow-up compiler layer should recognize only a canonical counted register-MMA loop and replace it with an internal value-form carrier that owns the accumulator recurrence, u32 shared-address recurrence, ldmatrix loads, loop control, and HMMA issue order in one convergent lowering lifetime. Unsupported or unproven loops remain unchanged. This is analogous in ownership to the existing counted WGMMA carriers, but for synchronous register MMA; it should not expose an application-specific MxN kernel API.
I propose implementing this as two reviewable PRs:
.shared::cta.u32ldmatrix source forms and a device oracle;- counted BF16 register-MMA carrier/recognizer, after agreeing on its narrow canonical shape.
Non-goals are automatic kernel fusion, a general scheduling DSL, a tuning database, or an engine-specific GEMM entry point.
Measured feasibility
An isolated sm_120a probe keeps 64 f32 accumulators live across a four-step loop. Each K-step contains two ldmatrix.x4, eight ldmatrix.x2, and sixteen mma.sync.m16n8k16 instructions. The only variable is ten shared-address recurrences represented as u64 or u32.
On CUDA 13.3:
- both
llc -> ptxasandlibNVVM -> LTOIR -> nvJitLinkproduce the same result; - the u64 loop has ten
UMOVlow-word moves and tenUIADD3.64address recurrences per K-step; - the u32 loop has neither; it uses native 32-bit
UIADD3; - HMMA/LDSM counts are identical;
- both forms use 78 general registers and report a zero-byte stack frame with zero spill loads/stores.
This proves the u32 address domain and combined-carrier backend path are feasible. It does not prove a performance win: ptxas retains the four-step loop instead of unrolling it, so final scheduling still needs benchmark evidence.
Alternatives considered
- Keep pointer/usize addressing. This preserves the current source model but emits repeated wide recurrences and low-word moves in the measured carrier.
- Handwrite inline PTX in each kernel. This can express u32 addresses but duplicates compiler contracts and makes every caller own register tying and backend details.
- Mechanically change only source arithmetic to u32. In the motivating 64-accumulator loop, libNVVM expanded all four K-steps before ptxas and introduced 168-216 bytes of spill traffic.
- Introduce a Rust shared-address newtype in the intrinsic ABI. It adds importer and arithmetic machinery without carrying more information than PTX's explicit
.shared::cta.u32operand at this layer. - Change the existing descriptor-address API to u32. That conflates ldmatrix addresses with the u64 carrier used to encode WGMMA/tcgen descriptors.
Additional context
The repository already has the relevant ownership pattern in the counted WGMMA value carriers and their canonical-loop recognizer. The proposed register-MMA carrier should follow that compiler-owned pattern while keeping CTA-shared instruction addresses at PTX width.
Environment used for the feasibility probe: CUDA 13.3.73, target sm_120a, pinned nightly-2026-08-28.
Activity
[-]Keep counted register-MMA shared addressing in the native u32 domain[/-][+]Keep counted register-MMA shared addressing in the PTX u32 domain[/+]uurl commentedon Nov 21, 2026
I tested a public Rust BF16 GEMM derived from Bloomery at
c14028b6, using cuda-rust9df9e7ceonsm_120a. The probe uses a 128×128 CTA tile, K=64, 256 threads per CTA, and 64 FP32 accumulators per thread. Both operands useldmatrix.x4. I kept the kernel source and launch configuration identical across the two compilation paths.--emit-nvvm-ir)Both runs matched all 16,384 BF16 outputs exactly against the CPU reference. Nsight Compute reported zero local-memory traffic for the measured launch on both paths. I did not measure runtime. This probe did not reproduce the spills reported for the x4-only BM128/BN128 case in #1237.
@lucifer1004, could you share the repository, commit, kernel path, and benchmark command for that case? A standalone reproducer that still spills would also work. I'd like to test whether changing the Rust issue order for
ldmatrix.x4andmma.syncremoves the spills.