Skip to content

Add cvt_f16x2_f32 intrinsic for f32-to-f16x2 packing - #66

Merged
nihalpasham merged 9 commits into
NVIDIA:mainfrom
honeyspoon:upstream-cvt-f16x2
Jun 12, 2026
Merged

nihalpasham merged 9 commits into
NVIDIA:mainfrom
honeyspoon:upstream-cvt-f16x2

Conversation

@honeyspoon

Copy link
Copy Markdown
Contributor

Summary

Add cvt_f16x2_f32 — packs two f32 values into a single u32 containing two packed f16 values in a single PTX instruction.

Maps to PTX: cvt.rn.f16x2.f32 d, hi, lo;

CUDA C equivalent: __float2half2_rn()

Motivation

WMMA GEMM epilogues accumulate in f32 but need to write half-precision output. The scalar approach requires two separate f32→f16 conversions plus bit manipulation ((lo_bits) | (hi_bits << 16)). This intrinsic compiles to a single PTX instruction that does both conversions and the pack.

Implementation

Follows the same 4-crate pattern as the existing CvtF32x2Bf16x2Op (bfloat16 variant in tcgen05.rs):

  • cuda-device/src/convert.rs: cvt_f16x2_f32(lo: f32, hi: f32) -> u32 stub
  • dialect-nvvm/src/ops/convert.rs: CvtF16x2F32Op — 2 f32 operands, 1 i32 result
  • mir-importer/.../intrinsics/convert.rs: Standard 2-arg emit with result storage via emit_store_result_and_goto
  • mir-lower/.../intrinsics/convert.rs: llvm.inline_asm with constraint string "=r,f,f"

The lowering is structurally identical to the existing convert_cvt_f32x2_bf16x2 in crates/mir-lower/src/convert/intrinsics/tcgen05.rs (lines 510–539), differing only in the PTX instruction name.

Testing

cargo fmt --check           # pass
cargo clippy -p cuda-device  # pass (workspace clippy blocked by hi_sparse_bitset MSRV on rustc 1.93)

Validated in a WMMA GEMM kernel on SM_89: f32 accumulator → packed f16x2 output with correct values (cosine similarity 1.000000 vs scalar reference).

@nihalpasham nihalpasham added enhancement New feature or request intrinsics Device intrinsics and libdevice math mappings labels Jun 6, 2026
@honeyspoon
honeyspoon force-pushed the upstream-cvt-f16x2 branch from 9b8e62a to 21cb520 Compare June 7, 2026 23:59
Add unit test for CvtF16x2F32Op verifying it lowers to inline asm
containing the cvt.rn.f16x2.f32 PTX instruction. Also fix dialect_llvm
→ llvm_export import and add missing convergent parameter in convert.rs.
@honeyspoon
honeyspoon force-pushed the upstream-cvt-f16x2 branch from de1bae5 to f15567e Compare June 11, 2026 00:29
@honeyspoon

Copy link
Copy Markdown
Contributor Author

Hey could we run the ci checks on this plz

CI runs a workspace-wide cargo fmt check; the two manually wrapped
expressions in the new test fail it.
rustdoc treats [15:0], [31:16] and args[0]/args[1] as intra-doc links
and fails the RUSTDOCFLAGS='-D warnings' doc gate with unresolved-link
errors. Backtick them so they render as code instead. Also realign the
lowering module's PTX table columns.
FP32Type::get takes a shared reference; passing &mut ctx trips
clippy::unnecessary_mut_passed.
The inserted Type Conversions match arm consumed the opening '====='
line of the next section banner.
cvt_f16x2_f32 and cvt_f32x2_bf16x2 use different argument naming
(lo/hi vs a/b) while sharing the same first-arg-low packing, and the
PTX operand order is reversed relative to the Rust signature. Spell
out the exact bit lanes on both stubs and link them to each other so
neither convention has to be inferred from the lowering.
…nsic

Promotes the PR #66 review repro to a feature-named example so the
examples-compile CI gate covers the intrinsic. Verifies 256 packed
values bit-exactly against a host scalar round-to-nearest-even
reference, cross-checks a device-side scalar kernel against the
intrinsic kernel, and pins the lane order with a constant-literal
kernel (f16(1.5)=0x3E00 low, f16(-2.25)=0xC080 high).
@nihalpasham
nihalpasham merged commit 050d476 into NVIDIA:main Jun 12, 2026
24 checks passed
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks for the PR. LGTM 👍🏾

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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