Skip to content

Add packed atomic add intrinsics (f16x2, bf16x2) - #322

Merged
nihalpasham merged 5 commits into
NVIDIA:mainfrom
honeyspoon:add-packed-atomics
Jul 2, 2026
Merged

nihalpasham merged 5 commits into
NVIDIA:mainfrom
honeyspoon:add-packed-atomics

Conversation

@honeyspoon

@honeyspoon honeyspoon commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds low-level packed FP16x2 and BF16x2 global atomic-add intrinsics to
cuda-device, with exact PTX semantics and target requirements.

Each 16-bit lane is atomic independently. The returned lanes are the previous
lane values, but they are not guaranteed to form one coherent old 32-bit
snapshot.

Partially addresses #274. It does not fix #289's general requested-target/PTX
mapping problem; both issues remain open.

What changed

Device API and memory contract

  • Adds unsafe atom_add_f16x2(*mut u32, u32) -> u32 and
    atom_add_bf16x2(*mut u32, u32) -> u32 operations.
  • Documents relaxed ordering, device scope, global memory, independent lane
    atomicity, unspecified lane order, alignment, and overlapping-access rules.
  • Keeps the API raw and register-packed; no typed or emulated operation is
    implied.

Compiler pipeline

  • Imports each public stub through a shared generic operation helper.
  • Verifies a mutable generic/global pointer to unsigned u32, an unsigned
    u32 addend, and an unsigned u32 result.
  • Lowers to side-effecting inline PTX with a memory clobber.

Target selection

  • FP16x2 selects cuda-oxide's sm_70 floor and PTX 6.2.
  • Native BF16x2 selects sm_90 and PTX 7.8.
  • BF16x2 is deliberately native-only; this PR does not claim CUDA C++'s older
    hardware emulation.
  • Explicit unsupported targets and mnemonic near misses are rejected.

Example and tests

  • Adds a packed_atomic_add example with a compile-only path and a runtime
    lane-sum smoke test for sm_90+ hardware.
  • Adds dialect, importer, lowering, target-detection, PTX, and assembler
    regressions.

Testing

  • Root and example formatting checks
  • cargo test -p dialect-nvvm -p mir-lower -p mir-importer --all-targets
  • cargo check --workspace --all-targets
  • Workspace and example clippy with warnings denied
  • Combined example emits PTX 7.8 / sm_90 and both exact atomics
  • CUDA 13.3 ptxas -arch=sm_90 accepts the combined PTX
  • Explicit sm_89 and sm_80 reject native BF16x2 before PTX generation
  • FP16-only build emits PTX 6.2 / sm_70; the instruction assembles
    forward for sm_75 on CUDA 13.3
  • Runtime lane-sum smoke test passes on sm_120
  • Compute Sanitizer memcheck reports zero errors on the runtime example

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-packed-atomics branch 2 times, most recently from 47297f0 to 06b1a6b Compare July 1, 2026 15:55
@nihalpasham
nihalpasham force-pushed the add-packed-atomics branch from 06b1a6b to f81fae1 Compare July 2, 2026 07:16
@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) device-apis User-facing device-side and kernel-authoring APIs (cuda-device, cuda-macros) tests Adds or improves test coverage examples Example kernels and host harnesses under crates/rustc-codegen-cuda/examples labels Jul 2, 2026
honeyspoon and others added 5 commits July 2, 2026 14:08
atom_add_f16x2() and atom_add_bf16x2() return the previous value at the
address — callers should not silently discard it.

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

Both NvvmAtomAddF16x2Op and NvvmAtomAddBf16x2Op now verify their
result is a 32-bit integer via a shared verify_packed_atomic_result()
helper.

Signed-off-by: nihalp <[email protected]>
- Mark atom_add_f16x2 and atom_add_bf16x2 as `unsafe fn` (they take
  raw pointers, clippy::unnecessary_safety_doc would fire otherwise).
- Improve safety docs with exact byte requirements and racing UB note.
- Extract shared emit_packed_atom_add helper (eliminates 97% duplicate
  code between the two emit functions).
- Extract shared convert_packed_atom_add helper parameterized by PTX
  type string.
- Run cargo fmt to fix import block wrapping.

Signed-off-by: nihalp <[email protected]>
@nihalpasham
nihalpasham force-pushed the add-packed-atomics branch from f81fae1 to 960bebd Compare July 2, 2026 08:46
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks for the PR.

The key semantic detail is that the two packed lanes are atomic independently:

u32 word:  [ high 16-bit lane | low 16-bit lane ]
atomicity: [ atomic separately | atomic separately ]
           not one atomic 32-bit snapshot

The final hardware floors are:

  • f16x2: PTX 6.2, available on cuda-oxide's sm_70 minimum.
  • Native bf16x2: PTX 7.8 and sm_90+, not sm_80. Older CUDA C++ support uses emulation, while this API exposes the native instruction.

Layered the following on top:

  • added complete pointer/type verification,
  • precise unsafe API documentation,
  • side-effecting inline PTX with a memory clobber,
  • exact feature detection,
  • architecture rejection tests, and an
  • end-to-end example.

This completes the packed-atomic part of #274, but #274 remains open for its other tracked work. It does not fix #289, which covers broader PTX ISA selection.

@nihalpasham
nihalpasham merged commit af0b2a3 into NVIDIA:main Jul 2, 2026
28 checks passed
@honeyspoon
honeyspoon deleted the add-packed-atomics branch July 23, 2026 13:53
elibol pushed a commit that referenced this pull request Nov 14, 2026
…ecode decoder (#1448)

Transplanted from NVlabs/cutile-rs#322, which was closed when the
repository moved here. Closes #1443.

Fixes two input-validation bugs in `cutile-ir`'s bytecode `Reader`:

- `read_bytes` checked `self.pos + n > len`, which overflows for a huge
section length. Debug builds panic on the overflow. In release builds
the
  sum wraps, the check passes, and the slice then panics. It now checks
  `n > self.remaining()`.
- `read_varint` accepted a 10th byte with payload > 1. The extra high
bits
  were shifted out, so the value was silently truncated. For example, a
section length could wrap to 1 and be accepted. It now rejects a 10th
byte
  other than `0x00`/`0x01`.

Tests:
- `decode_rejection.rs`: `reject_section_length_overflow` and
`reject_section_length_varint_overflow` both fail on `main` and pass
with
  this change.
- `decoder.rs`: `varint_u64_boundaries` round-trips 0, 127, 128, 2^63
and
  `u64::MAX`.

`cargo test -p cutile-ir`, `cargo fmt --check` and `cargo clippy
--all-targets -- -D warnings` pass under `cutile-rs/` with the pinned
1.98.0 toolchain. The change is identical to #322, where `bash
scripts/run_all.sh` passed locally.

Signed-off-by: dawnop <[email protected]>
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 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.

Select PTX ISA features from the requested target architecture

2 participants