Skip to content

fix(nvvm): lower legacy floating atomic add - #381

Merged
nihalpasham merged 5 commits into
NVIDIA:mainfrom
niklebedenko:codex/upstream-legacy-nvvm-atomic-fadd
Jul 23, 2026
Merged

nihalpasham merged 5 commits into
NVIDIA:mainfrom
niklebedenko:codex/upstream-legacy-nvvm-atomic-fadd

Conversation

@niklebedenko

Copy link
Copy Markdown
Contributor

Closes #380.

Summary

Lower relaxed, device-scoped f32/f64 atomic add to the six dedicated intrinsics accepted by the legacy LLVM 7 NVVM dialect. Reject operations whose type, address space, ordering, scope, or declaration ABI cannot be represented without weakening semantics.

Before: legacy NVVM export rejects valid relaxed f32/f64 `atomicrmw fadd`
After:  supported adds use `llvm.nvvm.atomic.load.add.*`; unsupported semantics fail before mutation

What changed

Legacy atomic-add legalization

  • Recognize typed LLVM AtomicRmwOp fadd operations after existing NVVM atomic lowering.
  • Validate f32/f64, pointer address spaces 0/1/3, monotonic ordering, syncscope("device"), module placement, and pre-existing intrinsic declarations in the module-wide prepass.
  • Rewrite validated operations to the matching official f32/f64 generic/global/shared NVVM intrinsic and preserve the returned old value.
  • Reuse the shared intrinsic-declaration and result-replacement helpers.

Correctness and fail-closed behavior

  • Complete every fallible validation before changing the module, preserving the legalizer's all-or-nothing failure contract.
  • Reject stronger direct orderings, block/system/absent scope, unsupported widths/address spaces, incompatible declarations, and other floating RMW kinds.
  • Leave integer atomic-RMW rejection unchanged.

Known separate limitations

  • Legacy export does not implement stronger floating atomic orderings or block/system-scoped floating add. Those require different NVVM/PTX lowering and are not approximated with weaker semantics.
  • The GPU reproducer uses generic pointers; transform/export tests cover the global and shared overload signatures.

Testing

  • cargo oxide fmt --check
  • cargo test -p nvvm-transforms — 18 tests passed
  • cargo clippy -p nvvm-transforms --all-targets -- -D warnings
  • cargo clippy --manifest-path crates/rustc-codegen-cuda/examples/legacy_atomic_fadd/Cargo.toml --all-targets -- -D warnings
  • Exact legacy IR assertions cover f32/f64 in address spaces 0/1/3 and contain no floating atomicrmw
  • Negative tests cover unsupported address space, scope, ordering, declaration ABI, transactionality, and unchanged integer RMW rejection
  • RTX 3080 (sm_86): optimized and CUDA_OXIDE_NO_OPT=1 legacy NVVM runs both report legacy_atomic_fadd: PASS (f32=256, f64=256)

Checklist

  • Branch rebased onto current upstream/main
  • Correctness claim covered by focused regressions
  • Unsupported cases fail clearly before mutation
  • Existing integer-RMW behavior is unchanged
  • Optimized and unoptimized GPU behavior verified

@nihalpasham nihalpasham added bug Something isn't working intrinsics Device intrinsics and libdevice math mappings IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) llvm-export Textual LLVM/NVVM IR exporter (llvm-export crate) on-hold Paused: valid but deferred until a prerequisite decision or dependency resolves labels Jul 22, 2026
@nihalpasham
nihalpasham force-pushed the codex/upstream-legacy-nvvm-atomic-fadd branch from 9256984 to bd67285 Compare July 22, 2026 08:38
@nihalpasham nihalpasham added codegen Device code-generation pipeline (Rust MIR to IR to PTX) examples Example kernels and host harnesses under crates/rustc-codegen-cuda/examples maintainer-owned tests Adds or improves test coverage and removed on-hold Paused: valid but deferred until a prerequisite decision or dependency resolves labels Jul 22, 2026
niklebedenko and others added 4 commits July 23, 2026 14:36
Route the new example through the legacy NVVM lane in both GPU and compile-only smoke tests, use the atomic wrapper's documented pointer constructor, and require the exact six intrinsic calls in the unit regression. Refresh the standalone lockfile against current main.

Signed-off-by: nihalp <[email protected]>
Restore the exact scalar typed-pointer signatures required by LLVM 7 NVVM and reject every conflicting top-level symbol before legalization mutates the module. Strengthen old-value coverage and launch the smoke example through atomic wrapper slices.

Signed-off-by: nihalp <[email protected]>
PTX atom.add.f64 needs sm_60 hardware. The legacy legalizer accepted an
f64 atomicrmw fadd for any capability, so an sm_5x NVVM build failed
later inside libNVVM/ptxas with an opaque error on the emitted
intrinsic. Thread the build target's compute capability into the
legalizer and reject the rewrite up front, before any mutation, in the
same fail-closed style as the ordering and scope checks. f32 stays
accepted on every target.
@nihalpasham
nihalpasham force-pushed the codex/upstream-legacy-nvvm-atomic-fadd branch from bd67285 to ff9cd04 Compare July 23, 2026 10:30
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks for the PR.

I layered maintainer commits on top:

  • Fixed the exported ABI. The legacy exporter erases every pointer to i8*, but LLVM 7 intrinsic name mangling makes p1f32 mean float addrspace(1)*, so libNVVM rejected the declaration. The six exact NVIDIA signatures now print with typed pointers and are validated fail-closed.
  • Widened the symbol-conflict check: a same-named global or function definition now rejects the module before any rewrite, not just a mismatched declaration.
  • Made the example sound and strict: shared atomic wrappers instead of aliasing UB, and the 256 recorded old values must be exactly 0..255, which proves both atomicity and the returned-old-value ABI on hardware.
  • Added an sm_60 floor while rebasing over feat(llvm-export): support i8/u8/i16/u16/f16/bool in device extern signatures #412: an f64 atomic add targeting sm_5x now fails with a clear diagnostic here instead of an opaque downstream libNVVM error.

Validated: nvvm-transforms, llvm-export, and codegen suites; GPU run on an RTX 5090; the libNVVM verify lane compiles all six signatures in CI without a GPU. Closes #380.

@nihalpasham
nihalpasham merged commit 2c33400 into NVIDIA:main Jul 23, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working codegen Device code-generation pipeline (Rust MIR to IR to PTX) 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) llvm-export Textual LLVM/NVVM IR exporter (llvm-export crate) maintainer-owned tests Adds or improves test coverage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Legacy NVVM export rejects relaxed f32/f64 atomic add

2 participants