Skip to content

feat: add bf16x2 packed arithmetic and ReLU-fused FMA (sm_80+) - #279

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
honeyspoon:upstream-bf16x2-arith
Jun 28, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
honeyspoon:upstream-bf16x2-arith

Conversation

@honeyspoon

@honeyspoon honeyspoon commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Add packed bf16x2 arithmetic operations extending the existing fma.rn.bf16x2: binary ops (add, sub, mul, min, max), unary ops (neg, abs), and a ReLU-fused FMA variant. All are pure per-thread register arithmetic lowered to inline PTX with AsmKind::Pure.

Summary

The existing fma_bf16x2 performs fused multiply-add on packed bf16x2 values. This PR adds the remaining packed bf16x2 arithmetic family, completing the ALU coverage needed for bf16 inference kernels. Each operation takes packed u32 (bf16x2) inputs and produces a packed u32 output, operating on both lanes in a single instruction.

The fma.rn.relu.bf16x2 variant fuses a ReLU clamp into the FMA, eliminating a separate max(x, 0) step in activation layers.

Device API PTX Instruction Type
add_bf16x2(a, b) add.rn.bf16x2 Binary
sub_bf16x2(a, b) sub.rn.bf16x2 Binary
mul_bf16x2(a, b) mul.rn.bf16x2 Binary
min_bf16x2(a, b) min.bf16x2 Binary
max_bf16x2(a, b) max.bf16x2 Binary
neg_bf16x2(a) neg.bf16x2 Unary
abs_bf16x2(a) abs.bf16x2 Unary
fma_relu_bf16x2(a, b, c) fma.rn.relu.bf16x2 Ternary

Multi-variant families use parameterized dispatch helpers (emit_binary_bf16x2, emit_unary_bf16x2, convert_binary_bf16x2, convert_unary_bf16x2) to avoid code duplication across variants.

Part of #274

Changes

  • cuda-device: 8 new safe pub fn stubs in bf16x2.rs (pure register arithmetic, no safety contract needed).
  • dialect-nvvm: 8 new NVVM ops registered in the bf16x2 module.
  • mir-importer: Parameterized emit_binary_bf16x2 / emit_unary_bf16x2 helpers with thin per-variant wrappers, plus match arms in try_dispatch_intrinsic.
  • mir-lower: Parameterized convert_binary_bf16x2 / convert_unary_bf16x2 helpers; 8 MirToLlvmConversion impls. All use AsmKind::Pure.
  • tests: Lowering test for fma_relu_bf16x2.
  • example: bf16x2_arith end-to-end GPU example testing all 8 ops with known values and bf16 tolerance comparison.

Testing

  • cargo test -p mir-lower --test lowering_test passes
  • cargo fmt --check and cargo clippy clean
  • E2E example included (bf16x2_arith)

Checklist

  • All commits signed off (git commit -s)
  • No files under cuda-bindings/ modified
  • SPDX headers on new source files

@honeyspoon
honeyspoon force-pushed the upstream-bf16x2-arith branch 3 times, most recently from a5567f0 to 99182c6 Compare June 26, 2026 17:44
@nihalpasham
nihalpasham force-pushed the upstream-bf16x2-arith branch from 99182c6 to 1700fcc Compare June 28, 2026 03:32
@nihalpasham nihalpasham added enhancement New feature or request 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) tests Adds or improves test coverage examples Example kernels and host harnesses under crates/rustc-codegen-cuda/examples labels Jun 28, 2026
honeyspoon and others added 2 commits June 28, 2026 10:24
Add packed bf16x2 arithmetic operations extending the existing
fma.rn.bf16x2: add, sub, mul, min, max, neg, abs, and fma.rn.relu.
All are pure per-thread register arithmetic lowered to inline PTX
with AsmKind::Pure.

Part of NVIDIA#274

Signed-off-by: abder <[email protected]>
@nihalpasham
nihalpasham force-pushed the upstream-bf16x2-arith branch from 1700fcc to 43219da Compare June 28, 2026 05:00
@nihalpasham
nihalpasham merged commit d52477a into NVIDIA:main Jun 28, 2026
26 checks passed
@honeyspoon
honeyspoon deleted the upstream-bf16x2-arith branch June 30, 2026 01:47
@honeyspoon honeyspoon mentioned this pull request Jul 3, 2026
6 of 7 tasks
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) enhancement New feature or request examples Example kernels and host harnesses under crates/rustc-codegen-cuda/examples intrinsics Device intrinsics and libdevice math mappings IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) tests Adds or improves test coverage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants