Skip to content

feat(wgmma): keep counted K-loop accumulators in SSA - #687

Merged
nihalpasham merged 7 commits into
NVIDIA:mainfrom
uurl:feat/605-wgmma-counted-k-loop
Aug 8, 2026
Merged

nihalpasham merged 7 commits into
NVIDIA:mainfrom
uurl:feat/605-wgmma-counted-k-loop

Conversation

@uurl

@uurl uurl commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

This PR is the fourth of six planned PRs for #605 and builds on the
linear SSA accumulator adapter introduced by #686.

Summary

Extend the value-threaded BF16 WGMMA path across a canonical counted K-loop.

For supported loops, the lowering now keeps all 32 per-thread f32
accumulator values in one convergent inline-PTX lifetime across every K
iteration, rather than materializing the accumulator through memory on each
iteration.

The resulting shape is conceptually:

32 accumulator loads
        ↓
one convergent inline-PTX region
{
    fence

    counted K-loop:
        wgmma.mma_async
        update descriptor A
        update descriptor B
        update loop counter
        branch

    commit_group
    wait_group<0>
}
        ↓
32 accumulator stores

Unsupported or non-canonical control-flow shapes continue through the existing
linear/deferred paths.

Motivation

#686 moved straight-line pointer-form WGMMA regions onto the 32-value SSA
carrier, eliminating accumulator memory materialization from inside the
convergent WGMMA asm scope.

A counted K-loop still presents a stronger lifetime requirement: if each loop
iteration becomes a separate LLVM inline-asm statement, LLVM regains visibility
between asynchronous WGMMA operations and may spill accumulator values while a
group is still in flight.

This PR closes that boundary for the canonical counted-loop case by placing the
entire asynchronous accumulator lifetime, including loop control and descriptor
recurrence, inside one convergent inline-PTX statement.

Implementation

Counted-loop recognition

The WGMMA accumulator preparation pass now uses the reusable
mir-transforms loop and induction analyses to recognize a restricted,
provable counted-loop shape.

The optimization requires a canonical loop with:

  • a single recognizable natural loop;
  • a single preheader;
  • a single latch/back-edge;
  • a compile-time-known positive trip count;
  • a closed fence / MMA / commit / wait_group<0> lifetime;
  • one WGMMA operation per loop iteration;
  • affine descriptor recurrences;
  • the canonical [[f32; 8]; 4] accumulator layout.

If those conditions cannot be proved, the pass leaves the loop untouched and
preserves the existing lowering behavior.

Loop analysis is also skipped for regions containing unreachable blocks. This
keeps malformed/negative-test MIR on the established diagnostic path instead of
querying dominance information that does not exist for unreachable CFG nodes.

Internal loop carrier

A new internal NVVM operation represents a counted BF16 WGMMA loop with:

32 x f32 accumulator inputs
descriptor A base
descriptor B base
descriptor A delta
descriptor B delta
trip count

→

32 x f32 accumulator results

The verifier closes the operation over the expected accumulator, descriptor,
delta, and trip-count types.

Lowering

The counted-loop operation lowers to one convergent, side-effecting LLVM
inline-asm statement.

The 32 accumulator inputs are tied to 32 =f outputs, as in the existing
value-form carrier.

The PTX region:

fence
guard zero trip count
loop:
    wgmma.mma_async
    desc_a += desc_a_delta
    desc_b += desc_b_delta
    trips -= 1
    branch if trips != 0
commit_group
wait_group 0

remains entirely inside that single LLVM-visible asm boundary.

The 32 aggregate results are then recovered with extractvalue and stored back
to the canonical accumulator only after the final wait_group<0>.

Register-lifetime property

For a K-loop with N iterations, accumulator memory traffic is now:

32 loads total
N WGMMA instructions
32 stores total

rather than:

N × 32 loads
N × 32 stores

There is no accumulator ld.f32, st.f32, or local .reg .f32 materialization
inside the counted-loop inline-PTX template.

Descriptor recurrence and loop control are also kept inside the same convergent
asm scope, so LLVM does not observe an intermediate accumulator lifetime between
iterations.

Fallback behavior

This remains a conservative optimization.

Loops that do not satisfy the canonical counted-loop contract are not partially
rewritten. Existing straight-line SSA adaptation and deferred pointer-form
fallback behavior remain available.

Existing malformed WGMMA control-flow cases also continue to produce their
previous diagnostics rather than being consumed by the new loop analysis.

Tests

Coverage includes:

  • recognition and lowering of a canonical counted WGMMA K-loop;
  • exactly 32 accumulator loads before the fused loop region;
  • exactly 32 accumulator stores after the fused loop region;
  • one convergent inline-asm statement for the complete counted loop;
  • tied register constraints for all 32 accumulator values;
  • descriptor base and delta operands carried into the PTX loop;
  • loop trip-count control inside the PTX region;
  • one fence, final commit, and final wait_group<0>;
  • no accumulator ld.f32, st.f32, or .reg .f32 materialization inside the
    counted-loop template;
  • preservation of existing malformed join/control-flow diagnostics.

Validation

dialect-nvvm:
  50 tests passed

mir-lower:
  167 unit tests passed
  92 integration tests passed

cuda-oxide-codegen:
  137 unit tests passed
  all integration tests passed

The existing backend carrier feasibility probe also remains green:

0 bytes stack frame
0 bytes spill stores
0 bytes spill loads
Used 58 registers

That probe validates the 32-value tied-register carrier independently; it is
not a direct ptxas spill measurement of the new counted-loop production path.

WGMMA smoketests also remain green:

wgmma           PASS
wgmma_mma_bf16  PASS

git diff --check is clean.

Scope

This PR does not yet:

  • lower partial wait_group<N> values;
  • keep multiple committed WGMMA groups in flight;
  • pipeline asynchronous groups across the K-loop;
  • change the public WGMMA API;
  • generalize fusion to arbitrary loop CFGs or descriptor expressions.

Those remain for subsequent PRs in #605.

@nihalpasham nihalpasham added enhancement New feature or request perf Performance of generated code or of the compiler itself IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) intrinsics Device intrinsics and libdevice math mappings cuda-feature A CUDA hardware/toolkit feature (TMA, tcgen05, WGMMA) codegen Device code-generation pipeline (Rust MIR to IR to PTX) wgmma-value-path PR series threading WGMMA accumulators through registers (#605) Depends Blocked on another PR/issue or an upstream dependency labels Aug 8, 2026
…d ptxas

The counted-loop fusion emits the first inline-PTX region in this
codebase with local labels, ${:uid} operand escapes, and bra.uni
branches, but nothing had pushed that template past the LLVM dialect.
Add a ptxas feasibility probe that builds the canonical pointer-form
K-loop (trip count 4, one MMA in the latch, affine descriptor
recurrences), lets the deferred accumulator fusion rewrite it, and
requires llc plus ptxas -arch=sm_90a to accept the result with zero
spill bytes while all 32 accumulator values stay live. The probe
asserts the uid-expanded loop labels and both branches so the deferred
pointer fallback cannot pass it vacuously.

Also clear the four clippy -D warnings failures in the new fusion pass
(two question-mark rewrites, one redundant into_iter, one house-style
too_many_arguments allow).
@nihalpasham

Copy link
Copy Markdown
Collaborator

Gist: Step 4: for one narrow, provable loop shape, the whole K-loop now lives inside a single asm region, which is what keeps the 32 accumulators in registers across iterations. The recognizer being strict is a feature, not a limitation: anything it cannot prove falls back to the old path.

I pushed one commit on top:

  • this is the first asm in the codebase containing labels and branches, and nothing had ever assembled it.
  • added a probe that builds the real loop through the real compiler pass and requires llc and the assembler (sm_90a) to accept it with zero spills. They do.

Merging after #686 (the rebase collapses the stack to this PR's own commit).

…flict resolution

The prior merge commit resolved interface_impls.rs and the wgmma files
by taking the branch side of the whole file, which also discarded
main's auto-merged additions in non-conflicted regions (notably the
convert_construct_disjoint_slice registration). Re-resolved keeping the
auto-merge and taking the branch side only inside the marker regions.
@nihalpasham
nihalpasham merged commit dfc5a50 into NVIDIA:main Aug 8, 2026
36 checks passed
nihalpasham added a commit to uurl/cuda-rust that referenced this pull request Aug 8, 2026
@uurl
uurl deleted the feat/605-wgmma-counted-k-loop branch August 10, 2026 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codegen Device code-generation pipeline (Rust MIR to IR to PTX) cuda-feature A CUDA hardware/toolkit feature (TMA, tcgen05, WGMMA) Depends Blocked on another PR/issue or an upstream dependency enhancement New feature or request intrinsics Device intrinsics and libdevice math mappings IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) perf Performance of generated code or of the compiler itself wgmma-value-path PR series threading WGMMA accumulators through registers (#605)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants