Skip to content

Add mma.sync.aligned.m16n8k16 bf16 tensor core intrinsic - #321

Merged
nihalpasham merged 4 commits into
NVIDIA:mainfrom
honeyspoon:add-mma-bf16
Jul 2, 2026
Merged

nihalpasham merged 4 commits into
NVIDIA:mainfrom
honeyspoon:add-mma-bf16

Conversation

@honeyspoon

@honeyspoon honeyspoon commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds the Ampere BF16 mma.sync.aligned.m16n8k16 tensor-core operation as an
unsafe, register-valued cuda-device intrinsic.

The API consumes the calling lane's C, A, and B fragments as scalar register
arrays and returns the four D registers. It does not hide pointer loads,
stores, a local stack frame, or memory ordering inside the MMA operation.

Partially addresses #274. Related to #289; this PR does not close either issue.

What changed

Device API and safety

  • Adds mma_m16n8k16_f32_bf16([f32; 4], [u32; 4], [u32; 2]) -> [f32; 4].
  • Documents the exact per-lane A, B, C, and D fragment mapping.
  • Documents the full-warp convergence, exited-lane, PTX 7.0, and sm_80+
    requirements.
  • Marks the operation unsafe and #[must_use].

Compiler pipeline

  • Represents the operation as ten scalar SSA operands and four scalar SSA
    results in the NVVM dialect.
  • Verifies the exact operand count, result count, and register types.
  • Extracts and reconstructs Rust arrays in the MIR importer without pointer
    transport.
  • Lowers to one convergent inline PTX instruction using direct register
    constraints and no memory clobber.

Target selection

  • Detects this exact MMA spelling as requiring PTX 7.0 and sm_80.
  • Rejects explicit lower targets before emitting invalid PTX.
  • Includes whitespace/escaped-whitespace and near-miss detection tests.

Regression coverage

  • Adds dialect verifier, MIR importer, lowering, and target-selection tests.
  • Covers one and two MMA calls so repeated use cannot redeclare fixed PTX
    temporary names.

Testing

  • cargo fmt --all -- --check
  • cargo check -p cuda-device -p dialect-nvvm -p mir-importer -p mir-lower --all-targets
  • cargo test -p dialect-nvvm -p mir-importer -p mir-lower
  • Strict affected-crate clippy with warnings denied
  • One-call and two-call Rust-to-PTX builds select PTX 7.0 / sm_80
  • CUDA 13.3 ptxas accepts both kernels
  • SASS contains one/two HMMA.16816.F32.BF16 instructions with zero stack
    bytes and no local loads/stores
  • Explicit sm_75 is rejected before PTX generation
  • Numerical execution on a physical sm_80+ GPU

Checklist

  • Contributor authorship preserved
  • Every PR commit has DCO sign-off
  • Public API and safety documentation updated
  • No crates/cuda-bindings/ files changed

@honeyspoon
honeyspoon force-pushed the add-mma-bf16 branch 2 times, most recently from 3aee669 to 04d59fd Compare July 1, 2026 15:55
honeyspoon and others added 4 commits July 2, 2026 12:35
- Replace no-op `impl Verify` with real validation (3 MirPtrType
  operands, 0 results) matching ldmatrix/stmatrix verifier patterns.
- Align safety docs with movmatrix style (divergent-flow UB, ISA floor).
- Strengthen lowering test to assert constraints, AsmKind::Convergent,
  operand count, and result count (not just template substring).
- Run cargo fmt to fix import block wrapping in interface_impls.rs.

Signed-off-by: nihalp <[email protected]>
@nihalpasham nihalpasham added enhancement New feature or request perf Performance of generated code or of the compiler itself intrinsics Device intrinsics and libdevice math mappings codegen Device code-generation pipeline (Rust MIR to IR to PTX) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) cuda-feature A CUDA hardware/toolkit feature (TMA, tcgen05, WGMMA) device-apis User-facing device-side and kernel-authoring APIs (cuda-device, cuda-macros) tests Adds or improves test coverage labels Jul 2, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks for the PR.

I layered on the maintainer-side finishing work:

  • made the API and compiler pipeline register-valued end to end, so it composes directly with ldmatrix and repeated MMA calls.
  • added exact operand/result verification, lane-layout and convergence safety docs, and strict PTX 7.0 / sm_80 detection.
  • hardened instruction matching against lookalikes such as not_mma, $mma, and %mma.
  • added importer, lowering, target-selection, literal-fragment, loaded-fragment, and repeated-call coverage.

Big picture: the before-after looks like this now -

Before: fragment pointers -> hidden loads/stores -> in-place accumulator
After:  C/A/B registers -> mma.sync -> returned D registers

This partially addresses #274 by adding the BF16 m16n8k16 form. It is related to #289 but does not close it. The remaining check is a 32-lane GPU numerical comparison against a CPU BF16 matrix reference.

@nihalpasham
nihalpasham merged commit b7ac654 into NVIDIA:main Jul 2, 2026
28 checks passed
@honeyspoon honeyspoon mentioned this pull request Jul 2, 2026
49 tasks done
nihalpasham added a commit that referenced this pull request Jul 3, 2026
* Add mma.sync.aligned.m16n8k16 f16 tensor core intrinsic

Adds the FP16 variant of the Ampere mma.sync instruction, following the
same register-only 4-layer pattern established by the BF16 (#321) and
F64 (#323) MMA variants:

  cuda-device stub  → dialect-nvvm op → mir-importer emit → mir-lower convert

The F16 variant has identical register geometry to BF16 (4 f32 C/D, 4 i32
packed A, 2 i32 packed B = 10 operands, 4 results) and uses the same
inline-asm constraints (=f,=f,=f,=f,f,f,f,f,r,r,r,r,r,r).

Pipeline detection, lowering tests, and near-miss rejection tests are
included.

* fix: harden f16 warp MMA contract

Signed-off-by: nihalp <[email protected]>

* style: format standalone MMA example

Signed-off-by: nihalp <[email protected]>

---------

Signed-off-by: nihalp <[email protected]>
Co-authored-by: nihalp <[email protected]>
@honeyspoon
honeyspoon deleted the add-mma-bf16 branch July 23, 2026 13:53
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) device-apis User-facing device-side and kernel-authoring APIs (cuda-device, cuda-macros) 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 tests Adds or improves test coverage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants