Skip to content

feat(cuda-device): add bf16x2 FMA intrinsic - #191

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
niklebedenko:prepare-bf16x2-fma
Jun 17, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
niklebedenko:prepare-bf16x2-fma

Conversation

@niklebedenko

Copy link
Copy Markdown
Contributor

Closes #190.

Problem

cuda-oxide did not have an end-to-end representation for the PTX fma.rn.bf16x2 instruction even though it is the packed bf16 arithmetic operation available on sm_80+. That left users unable to express packed bf16x2 FMA through the cuda-device API and meant the compiler pipeline had no focused regression covering importer recognition and MIR lowering for this instruction.

The general compiler/runtime rule is: a public CUDA device intrinsic for a concrete PTX instruction should be represented consistently through the device API, dialect op, MIR importer dispatch, MIR lowering, and a repo-native example that proves the path remains wired together.

Fix

Add cuda_device::bf16x2::fma_bf16x2(a, b, c) -> u32, where each u32 stores two bf16 lanes. The importer recognizes that intrinsic path and emits a new nvvm.fma_bf16x2 op, and MIR lowering converts that op to inline PTX:

fma.rn.bf16x2 $0, $1, $2, $3;

The new bf16x2_fma rustc-codegen-cuda example exercises the path from device API through codegen and checks the packed result on execution-capable systems.

Diligence

I checked the sibling packed intrinsic path used by tcgen05::cvt_f32x2_bf16x2: public device declaration, dialect op registration, importer terminator dispatch, conversion interface registration, and inline-PTX emission. The new intrinsic follows that same abstraction boundary rather than adding an example-only workaround.

This PR intentionally does not add packed bf16x2 add/mul intrinsics. PTX fma.rn.bf16x2 is supported on sm_80+, while add.bf16x2 and mul.bf16x2 require sm_90+; adding all three under one Ampere-facing module would imply a common architecture contract that PTX does not provide. Ampere callers can express packed add as FMA with a packed 1.0 multiplier.

All new Rust source files use the repository's NVIDIA copyright and SPDX header.

Tests

Added crates/rustc-codegen-cuda/examples/bf16x2_fma, including Cargo.lock and a short README. The example converts (2.0, 4.0), (3.0, 5.0), and (7.0, 11.0) into packed bf16x2 operands, runs the FMA intrinsic in a kernel, and checks for packed (13.0, 31.0).

Validation

cargo fmt --all -- --check
cargo test -p dialect-nvvm --all-targets
cargo test -p mir-importer --all-targets
cargo test -p mir-lower --all-targets
cargo test -p cuda-device --all-targets
cargo clippy --workspace -- -D warnings
cd crates/rustc-codegen-cuda && cargo clippy --all-targets -- -D warnings
scripts/smoketest.sh --compile-only --no-color
cargo oxide run bf16x2_fma

Local results: all commands pass after applying cargo fmt --all; cargo test -p dialect-nvvm --all-targets passed with the focused FmaBf16x2Op construction/verification test; scripts/smoketest.sh --compile-only --no-color passed 93/93 examples, including bf16x2_fma; cargo oxide run bf16x2_fma passed from the repository root on sm_86 and printed matching expected and got packed bf16x2 values (0x41f84150).

niklebedenko and others added 2 commits June 17, 2026 09:28
…te, doc row

Layered on top of the contributor PR:
- replace 3 U+2014 em-dashes in the bf16x2 importer/lowering doc comments
  (tracked files must not contain em-dashes)
- example: print a PASS/FAIL marker (it exited 0 silently on success, so a
  full smoketest run scored it FAIL despite correct output) and skip
  cleanly on pre-sm_80 devices via compute_capability()
- supported-features: add a Packed bf16x2 FMA row
@nihalpasham nihalpasham added intrinsics Device intrinsics and libdevice math mappings cuda-feature A CUDA hardware/toolkit feature (TMA, tcgen05, WGMMA) enhancement New feature or request labels Jun 17, 2026
@nihalpasham

nihalpasham commented Jun 17, 2026 •

Copy link
Copy Markdown
Collaborator

LGTM

  • layered: the example exited 0 with no marker (smoketest scored it FAIL on success), so it now prints PASS and gates on sm_80; added the supported-features row
  • rebased (conflicts on ops/mod.rs vs asm::register, interface_impls.rs, terminator/mod.rs)

Reviewer note: GPU sm_120: kernel returns 0x41f84150 (lanes 13, 31); PTX has fma.rn.bf16x2

@nihalpasham
nihalpasham merged commit 8330941 into NVIDIA:main Jun 17, 2026
24 checks passed
nihalpasham added a commit to honeyspoon/cuda-oxide that referenced this pull request Jun 18, 2026
… bf16x2 to AsmKind::Pure

The asm_kind docblock was accidentally prepended to asm_kind_opt in the
original PR. Remove the stray block so asm_kind_opt has a single, accurate
doc comment.

Migrate convert_fma_bf16x2 from InlineAsmOp::new(..., false) to
InlineAsmOp::build(..., AsmKind::Pure), covering the bf16x2 FMA op added
by PR NVIDIA#191 after this branch was cut. The PTX instruction is a pure
register-to-register FMA with no memory side effects and no warp
synchronization, so Pure is the correct classification.
@niklebedenko
niklebedenko deleted the prepare-bf16x2-fma branch June 24, 2026 11:40
nihalpasham added a commit that referenced this pull request Jul 30, 2026
…licit std feature) (#593)

* Port to pliron master API + timing-robust alignment stamping (pliron PR 182/191 testing)

EXPERIMENTAL BRANCH - do not merge as-is. The workspace and
rustc-codegen-cuda manifests carry a TEMPORARY [patch] section pointing
pliron/pliron-derive/pliron-llvm at the local clone ~/dev/pliron so
upstream PRs can be tested by switching branches there.

Port to pliron master (4159dc0, 52 commits past our 222dd962 pin):
- Type::register_instance -> Type::instantiate (renamed upstream)
- drop unused get_existing() wrappers (Type::get_instance removed upstream)
- DialectName::new -> DialectName::try_new
- pliron::pass_manager::AnalysisManager -> pliron::pass::AnalysisManager

Timing-robust alignment stamping (found while testing pliron PR #182,
"[DialectConversion] Convert all block args, not just used ones"):
stamp_memory_op_alignment ran inside the MirFuncOp rewrite and read live
operand types to recover repr(align(N)) ABI alignment. That relied on
the old driver's lazy block-arg conversion; PR #182 converts all block
args before the first rewrite, so stores/refs of block args (params,
mem2reg join/loop args) silently lost their alignment stamp (probe
showed the Al16 entry spill store degrade from align 16 to align 4).
Hoist the stamping to a whole-module pre-pass before
apply_dialect_conversion, next to the dynamic-smem pre-pass, making it
independent of conversion order under both old and new drivers.

New example pr182_probe: over-aligned aggregates in block-arg positions
(entry spill of a by-value repr(align(16)) param, mem2reg join struct,
loop-carried tuple, dead params). Its .ll is byte-identical between
pliron master and PR #182 with this fix.

Validation (pliron clone on master and on pr-182, RTX 5090 / CC 12.0):
cargo test --workspace 2992 passed, 0 failed; smoketest 171/171.

* Opt in to pliron-llvm's std feature (pliron PR #191)

pliron PR #191 makes pliron-llvm (without llvm-sys) no_std and gates
its std support behind a new `std` feature. Our default-features =
false dependency (which exists to keep llvm-sys out) must now request
std explicitly. Note: cuda-oxide happens to build on PR #191 even
without this, because our direct pliron dependency unifies pliron/std
into the graph, but relying on that is fragile; declare the intent.

This commit only resolves when the patched pliron-llvm has the std
feature (pliron clone on pr-191); keep it at the branch tip until the
PR lands upstream.

Validation (pliron clone on pr-191): cargo test --workspace 2992
passed, 0 failed; smoketest 171/171.

* Recover repr(align) from conversion history instead of a pre-pass

Per upstream guidance (Vaivaswatha, on pliron PR #182): the dialect
conversion driver records every value type change it makes, and hands
each rewrite an OperandsInfo carrying the full pre-conversion type
history of its operands. That is the intended way to read source-dialect
types late, so use it instead of stamping alignment in a module pre-pass
before conversion starts.

- mir.store / mir.ref: recover the stored/referenced value's ABI
  alignment via value_abi_align (current type first, then the operand's
  recorded history, newest first) at the op's own rewrite.
- mir.load / mir.alloca: read their own result types, which the driver
  never converts ahead of the op's rewrite.
- Delete stamp_module_memory_op_alignments / stamp_memory_op_alignment
  and the copy_alignment attribute round-trip. Net -59 lines.

Equivalence and validation (RTX 5090 / CC 12.0):
- pr182_probe .ll byte-identical to the pre-pass fix on pliron master.
- All 312 .ll artifacts across all 171 examples byte-identical between
  the pre-pass fix and this fix on pliron pr-182 (full-run comparison).
- cargo test --workspace 2992 passed, 0 failed on pr-182 and pr-191.
- smoketest 171/171 on pr-182 and pr-191.

* Rename pr182_probe example to repr_align_block_args

The example guards a durable compiler invariant, not a one-off PR test:
repr(align(N)) ABI alignment must survive dialect conversion for values
in block-arg positions (function params, mem2reg join/loop args), where
the conversion driver may replace MIR types before any per-op rewrite
runs. Name it for what it checks. Docs reframed the same way; the
pliron PR #182 origin stays as context.

* deps: bump pliron to master 0d3f25df and drop the local-clone patch

Picks up DialectConversion converting all block args (pliron#182),
no_std pliron-llvm behind an explicit std feature (pliron#191),
BytesAttr global initializers (pliron#198), and the conversion-trait
renames (pliron#201). The repr(align) recovery already reads the
OperandsInfo conversion history at rewrite time, so no pre-pass is
needed under the eager block-arg conversion.

* fix(examples): keep device_ffi_test's nvvm-dis text dump best-effort

An nvvm-dis older than the CUDA toolkit cannot parse newer LTOIR, and
under set -e the inspection failure aborted the whole build. Guard it
exactly like the small_type_ffi_test and mathdx_ffi_test siblings
already do: the binary LTOIR is the consumed artifact, the text dump
is debugging aid only.

* style: rustfmt the port; re-resolve the standalone lock to the git pin

The repr_align_block_args example and one blank line in dialect-mir
predate the fmt gate; the rustc-codegen-cuda Cargo.lock still recorded
pliron as a path dependency left over from the removed local-clone
patch, and now records the pinned git source.

* style(examples): collapse the guarded write in repr_align_block_args
meefs pushed a commit to meefs/cuda-oxide that referenced this pull request Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda-feature A CUDA hardware/toolkit feature (TMA, tcgen05, WGMMA) enhancement New feature or request intrinsics Device intrinsics and libdevice math mappings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add cuda-device support for packed bf16x2 FMA

2 participants