Skip to content

feat: add packed f16x2 fma ftz and saturating forms - #659

Merged
nihalpasham merged 3 commits into
NVIDIA:mainfrom
honeyspoon:feat/f16x2-fma-modifiers
Aug 5, 2026
Merged

nihalpasham merged 3 commits into
NVIDIA:mainfrom
honeyspoon:feat/f16x2-fma-modifiers

Conversation

@honeyspoon

Copy link
Copy Markdown
Contributor

Completes the packed_alu fma modifier coverage for f16x2. The family already had fma.rn.f16x2 and fma.rn.relu.f16x2; LLVM also declares the ftz and sat combinations, each with a single tablegen selection record, and all four lower on the pinned backend.

What this adds

Intrinsic PTX Native floor
fma_ftz_f16x2 fma.rn.ftz.f16x2 PTX 4.2 / sm_53
fma_sat_f16x2 fma.rn.sat.f16x2 PTX 4.2 / sm_53
fma_ftz_sat_f16x2 fma.rn.ftz.sat.f16x2 PTX 4.2 / sm_53
fma_ftz_relu_f16x2 fma.rn.ftz.relu.f16x2 PTX 7.0 / sm_80

Floors are not collapsed across the group. The first three mirror the existing fma_f16x2 per-backend floors exactly - PTX 6.0 / sm_70 for the pinned LLVM route, PTX 4.2 / sm_75 for libNVVM.

None of the four is reachable any other way: generic llvm.fma.v2f16 lowers only to the plain fma.rn.f16x2, which already existed, and ftz/sat/relu have no Rust or generic-LLVM spelling. Verified on the pinned llc.

Why the bf16x2 counterparts are excluded

They do not exist in the PTX ISA. ptxas rejects hand-written fma.rn.ftz.bf16x2:

error : Illegal modifier '.ftz' for instruction 'fma'

while accepting fma.rn.relu.bf16x2 beside it, so the assembler is the authority. LLVM declares the intrinsics anyway, which is why instruction selection is merely what fails first (Cannot select: intrinsic %llvm.nvvm.fma.rn.ftz.bf16x2); they also carry no tablegen selection record in intrinsics/imported.json. The recipe comment and the bf16x2 module docs state this, so the six-vs-two asymmetry reads as an ISA fact rather than an omission.

Fail-closed recipe lookup

packed_alu_recipe returned PackedAluRecipe unconditionally, so an operation one format supports and another does not had nowhere to go but a panic. It now returns Option and packed_bf16x2_alu_recipe rejects the four pairs by name, so the family errors with is outside the closed packed-ALU recipe instead of admitting a variant that cannot lower.

packed_alu_backend_floor also takes the caller's already-resolved recipe rather than re-deriving it, so the new Option is discharged where it is created rather than behind an expect justified by a caller invariant.

Evidence

Measured for both backend profiles:

  • Pinned LLVM 22.1.2 - the inline-PTX bundles lower with every exact instruction head present and assemble under CUDA 13.3 ptxas. Independent typed calls to the llvm.nvvm symbols emit the same heads.
  • CUDA 13.3 libNVVM - the same forms compile to PTX 9.3, assemble, and survive -gen-lto plus nvJitLink.

Hardware verification

The launched f16x2_arith example goes from 9 to 14 checked operations on an sm_86 RTX A2000, with cases chosen to discriminate rather than merely execute:

  • A subnormal input pair (0x0002_0001) is preserved by fma_f16x2 and flushed to zero by fma_ftz_f16x2 and fma_ftz_relu_f16x2 - direct proof the ftz modifier acts, not just that the stub compiles.
  • 2*3+7 = 13 and 4*5+11 = 31 both clamp to 1.0 under the saturating forms.

All 14 pass bit-exactly.

What I could not verify

Two gaps, stated rather than left to be found:

  • Assembly is checked at sm_75, not at the admitted sm_70 floor. CUDA 13.3 ptxas no longer accepts sm_70, so the evidence records assembly at sm_75, as the existing fma_f16x2 record already does for the same reason.
  • Only sm_86 was exercised on hardware. No Blackwell or Hopper run.

Verification

Rebased on e28248c1; 3 commits, all DCO signed off.

  • cargo run -p cuda-intrinsics-gen -- check - clean
  • probe --all --skip-terminal - 857/857 routes; all four new ones lower to their expected instruction
  • check-abi-history --base-ref upstream/main - 853 base entries preserved, 857 total (this PR uses i0854-i0857)
  • cargo fmt --all -- --check - clean in all three workspaces
  • cargo clippy --workspace --all-targets -- -D warnings - clean
  • RUSTDOCFLAGS='-D warnings' cargo doc - clean
  • Tests: cuda-intrinsics-gen 288, cuda-intrinsics 857, dialect-nvvm 50, mir-lower 245
  • scripts/smoketest.sh -o '^f16x2_arith$' - launched on hardware

Full CI is green on the fork PR (50/50 including compile all examples): honeyspoon#116

Follows #649. Independent of it - different family and contract, no shared code, non-overlapping ABI ids.

https://claude.ai/code/session_01LXYptgNnAcDSbkgyWGYQzk

Completes the `packed_alu` fma modifier coverage for `f16x2`. The family
already had `fma.rn.f16x2` and `fma.rn.relu.f16x2`; LLVM also declares the
`ftz` and `sat` combinations, each with a single tablegen selection record,
and all four lower on the pinned backend.

Adds 4 intrinsics:

- `fma_ftz_f16x2` - `fma.rn.ftz.f16x2`
- `fma_sat_f16x2` - `fma.rn.sat.f16x2`
- `fma_ftz_sat_f16x2` - `fma.rn.ftz.sat.f16x2`
- `fma_ftz_relu_f16x2` - `fma.rn.ftz.relu.f16x2`

The four `bf16x2` counterparts are excluded. LLVM declares them but the NVPTX
backend has no selection pattern, so instruction selection fails with
"Cannot select: intrinsic %llvm.nvvm.fma.rn.ftz.bf16x2"; they also carry no
tablegen selection record in the imported ledger. `packed_alu_recipe` now
returns `Option` and rejects those pairs by name, so the family fails closed
with "is outside the closed packed-ALU recipe" instead of admitting a variant
that cannot lower. That is also why the dispatcher had to become fallible
rather than panicking on an unknown pair.

Architecture floors differ inside the group and are not collapsed: `ftz`,
`sat`, and `ftz.sat` are native at PTX 4.2 / sm_53, while `ftz.relu` needs
PTX 7.0 / sm_80. The first three therefore mirror the existing `fma_f16x2`
per-backend floors exactly - PTX 6.0 / sm_70 for the pinned LLVM route, PTX
4.2 / sm_75 for libNVVM, the latter because CUDA 13.3 ptxas no longer accepts
sm_70.

Evidence is measured, not asserted, for both backend profiles. On the pinned
LLVM the inline-PTX bundles lower with every exact instruction head present
and assemble under CUDA 13.3 ptxas; independent typed calls to the
`llvm.nvvm` symbols emit the same heads. On CUDA 13.3 libNVVM the same forms
compile to PTX 9.3, assemble, and survive `-gen-lto` plus nvJitLink.

Hardware verification extends the launched `f16x2_arith` example from 9 to 14
checked operations, run on an sm_86 RTX A2000. The new cases are chosen to
discriminate rather than merely execute: a subnormal input pair
(`0x0002_0001`) is preserved by `fma_f16x2` and flushed to zero by
`fma_ftz_f16x2` and `fma_ftz_relu_f16x2`, and `2*3+7 = 13` / `4*5+11 = 31`
clamp to 1.0 under the saturating forms. All 14 pass bit-exactly.

Verified: `check`, `probe --all --skip-terminal` (833/833 routes, all four new
ones lowering to the expected instruction), `check-abi-history` (829 base
entries preserved), `cargo fmt --all --check` in all three workspaces,
`cargo clippy --workspace --all-targets -D warnings`, `RUSTDOCFLAGS='-D
warnings' cargo doc`, cuda-intrinsics-gen (288 tests), and
`scripts/smoketest.sh -o '^f16x2_arith$'` launched on hardware.

Claude-Session: https://claude.ai/code/session_01LXYptgNnAcDSbkgyWGYQzk
Signed-off-by: abder <[email protected]>
…oor lookup

Two review findings on the preceding commit, neither observable in the
generated output - regeneration is byte-identical.

`packed_alu_backend_floor` re-derived the recipe it was handed a moment
earlier and used `expect` to discharge the `Option` the previous commit
introduced. Every one of its three callers already holds the resolved recipe,
so it now takes that by reference. The panic path is gone rather than argued
unreachable, and a redundant lookup goes with it.

The four new f16x2 match arms were also inserted wherever they happened to
fit, which split the relu pair and left the arm order out of step with the
enum. `FmaFtzRelu` now follows `FmaRelu`, so the arms read in enum order.

Verified: `check`, `probe --all --skip-terminal` (833/833), 288 generator
tests, `cargo fmt --all --check`, `cargo clippy --workspace --all-targets
-D warnings`, `RUSTDOCFLAGS='-D warnings' cargo doc`, and the launched
`f16x2_arith` example on sm_86.

Claude-Session: https://claude.ai/code/session_01LXYptgNnAcDSbkgyWGYQzk
Signed-off-by: abder <[email protected]>
… backend

Carried over from the NVIDIA#649 review, where the maintainer independently checked
a near-identical exclusion against the PTX ISA grammar rather than taking the
backend failure at face value. Doing that here changes the answer's basis.

The `ftz` and `sat` fma forms do not exist for `bf16x2` in the ISA at all.
ptxas rejects hand-written `fma.rn.ftz.bf16x2` with "Illegal modifier `.ftz`
for instruction `fma`", and accepts `fma.rn.relu.bf16x2` beside it, so the
assembler - not LLVM - is the authority. LLVM declaring the intrinsics anyway
is why instruction selection is merely what fails first. The recipe comment
and the `bf16x2` module docs now say that, so the asymmetry between six `f16x2`
fma forms and two `bf16x2` ones reads as an ISA fact rather than an omission.

Also documents the `f16x2` fma modifier set with its split floors: the four
non-ReLU forms are native at PTX ISA 4.2, while both ReLU forms require sm_80
and PTX ISA 7.0.

Verified: `check`, `probe --all --skip-terminal` (857/857), `check-abi-history`
(853 base entries preserved), 288 generator tests, `cargo fmt --all --check`,
`cargo clippy --workspace --all-targets -D warnings`, `RUSTDOCFLAGS='-D
warnings' cargo doc`, and `f16x2_arith` launched on sm_86.

Claude-Session: https://claude.ai/code/session_01LXYptgNnAcDSbkgyWGYQzk
Signed-off-by: abder <[email protected]>
@nihalpasham nihalpasham added intrinsics Device intrinsics and libdevice math mappings codegen Device code-generation pipeline (Rust MIR to IR to PTX) enhancement New feature or request labels Aug 5, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Ran the full battery locally (sm_120):

  • cargo test --workspace: 3294 passed. check-abi-history vs main: 853 base entries preserved, 857 total, append-only holds.
  • normalized compare of the catalog and both evidence JSONs: 0 existing records modified, exactly the 4 new ones. The 866 modified probe files change only the catalog SHA header (scripted over all of them, not sampled).
  • all 4 new forms probed through llc and the real ptxas 13.3 (binary SHA matches your recorded evidence): emitted heads fma.rn.{ftz,sat,ftz.sat,ftz.relu}.f16x2 confirmed at sm_70/sm_80.
  • f16x2_arith on GPU: all 6 fma variants exact against the host oracle, including the subnormal-flush and saturation checks.
  • checked the bf16x2 exclusion against real ptxas: fma.rn.ftz.bf16x2 and fma.rn.sat.bf16x2 both rejected with Illegal modifier, while the fma.rn.relu.bf16x2 control assembles. Your final commit documenting that at the exclusion site is exactly right; the pinned LLVM declares those intrinsics but has no selection patterns, so they would die at instruction selection.

Merging.

@nihalpasham
nihalpasham merged commit 8de78e7 into NVIDIA:main Aug 5, 2026
33 checks passed
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) 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.

2 participants