Skip to content

Support typed NVVM IR for pre-Blackwell libNVVM targets - #101

Merged
nihalpasham merged 4 commits into
NVIDIA:mainfrom
mohamedsamirx:typed-nvvm-ir-pre-blackwell
Jun 27, 2026
Merged

nihalpasham merged 4 commits into
NVIDIA:mainfrom
mohamedsamirx:typed-nvvm-ir-pre-blackwell

Conversation

@mohamedsamirx

@mohamedsamirx mohamedsamirx commented May 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #98.

Kernels that call sin, exp, sqrt, atan2, or other CUDA libdevice
functions use the NVVM IR path. Pre-Blackwell libNVVM parses LLVM 7 syntax with
typed pointers, while Blackwell and newer targets parse modern opaque-pointer
IR. cuda-oxide now emits, verifies, and loads the correct form for each target.

Before: add libdevice math → pre-Blackwell libNVVM reports `parse expected type`
After:  the same kernel builds and runs through the normal `#[cuda_module]` API

The ordinary LLVM IR → llc → PTX path remains unchanged.

What changed

  • Target-selected NVVM IR

    • Adds one strict CudaArch parser for sm_* and compute_* targets,
      including suffixes such as sm_90a.
    • Selects LLVM 7 typed-pointer IR below compute capability 10.0, including
      sm_90 and sm_90a.
    • Selects modern opaque-pointer IR for Blackwell and newer targets.
    • When an ordinary build discovers libdevice without --arch, uses the
      normal target order: explicit target, compatible run GPU hint, then the
      compiler's feature-based target. Basic headless builds select sm_80.
    • Uses the same resolved target for legalization, text export, libNVVM,
      nvJitLink, embedded metadata, and .target files.
  • Complete LLVM 7 pointer export

    • Gives every internal pointer one neutral type per address space:
      i8 addrspace(N)*.
    • Converts typed results from allocas, GEPs, globals, function addresses,
      calls, and other pointer producers back to that neutral form.
    • Creates exact same-address-space typed views at loads, stores, GEPs,
      calls, and other typed uses.
    • Keeps pointer PHIs, selects, calls, returns, arrays, and structs
      type-consistent without assuming that every address has one pointee type.
    • Preserves pointer address spaces. Only an explicit source
      addrspacecast may cross address spaces.
  • Structured ABI boundaries

    • Preserves pointer pointees and address spaces for device-extern
      declarations after the function body has moved to opaque pointers.
    • Adds typed adapters at supported external calls and returns.
    • Rejects signatures that cannot yet be represented exactly, including
      non-C or unwinding ABIs, variadics, unsupported small by-value values,
      scalar f16, and arrays passed by value.
  • Dedicated NVVM transform crate

    • Adds nvvm-transforms between MIR lowering and text export:

      mir-transforms → mir-lower → nvvm-transforms → verify → llvm-export
      
    • Keeps mir-lower focused on dialect-mir → LLVM conversion.

    • Rewrites fneg by toggling the IEEE sign bit, preserving signed zero and
      NaN payload bits.

    • Expands integer and float-to-integer saturation, modern shuffle/vote
      signatures, LLVM 7 memory intrinsic names, unsupported nneg flags, and
      wide bit operations.

    • Validates the module before changing it and returns an error when no
      equivalent legacy operation is available.

  • Verification and artifact handling

    • Runs nvvmVerifyProgram before compilation and queries the toolkit's NVVM
      IR/LLVM versions when available.
    • Records the source target beside embedded and file-based NVVM artifacts.
    • Removes artifacts from earlier output modes before publishing new output.
    • Rebuilds file-based NVVM output instead of reusing cubins by timestamp
      alone.
    • Selects libNVVM, libdevice, and nvJitLink from the same CUDA toolkit root
      unless a component-specific path override is set.
    • Uses open integer wrappers for libNVVM and nvJitLink result codes so newer
      library error values remain valid across the FFI boundary.
  • Blackwell forward execution

    • Native loading still requires an exact target match.
    • A standard pre-Blackwell target such as sm_86 can be converted to
      original-target PTX and JIT-compiled by the CUDA driver on Blackwell.
    • The driver must support the PTX version emitted by the selected toolkit;
      error 222 now points users to a compatible toolkit or driver upgrade.
    • Newer output cannot run on older GPUs, and architecture-specific targets
      such as sm_90a are not forwarded to another compute capability.

Capability coverage

  • sm_75, sm_86, sm_90, and sm_90a LLVM 7 output
  • sm_100 and sm_120 modern output
  • Pointer PHIs, loop backedges, calls, returns, nested structs, and pointer arrays
  • One address used through more than one pointee type
  • Structured device-extern pointer ABIs
  • Whole-module libNVVM verification
  • Automatic libdevice-triggered NVVM selection
  • Standard pre-Blackwell output running forward on Blackwell
  • Existing PTX path unchanged

Regression examples

  • libdevice_math — the issue nvvmCompileProgram fails with "parse expected type" when libdevice calls (__nv_expf) trigger NVVM IR mode #98/community kernel shapes, SwiGLU, and
    sin, exp, sqrt, atan, atan2, acos, and tan, with CPU numerical
    checks. This replaces the issue-number-based issue98_repro name.
  • legacy_nvvm_pointer_shapes — pointers across branches, a loop,
    non-inlined calls, nested structs, an array of pointers, and f32/u32
    views of the same address.
  • primitive_stress — wide bit operations, saturation, and 50 floating
    point checks through both NVVM dialects.

Compile-only smoke tests use emit-ltoir, so all three examples pass through
the real libNVVM verifier and compiler instead of stopping after text export.

Testing

  • cargo fmt for the root workspace, backend, and changed examples
  • cargo test --workspace --all-targets
  • Strict Clippy for the changed compiler/runtime crates and backend
  • Warning-free workspace rustdoc and all doctests
  • cargo deny check
  • Strict Sphinx build with warnings treated as errors
  • Full LLVM 21 compile-only smoke gate: 119 / 119 examples
  • Zero-flag libdevice builds select sm_80, emit typed-pointer IR, and record .target
  • Real libNVVM compile smoke: libdevice_math, pointer shapes, and primitive stress
  • CUDA 13.0 and 13.3 LTOIR matrix: sm_75, sm_86, sm_90, sm_90a, sm_100, sm_120
  • Generated sm_86 LLVM IR contains typed pointers and no opaque ptr
  • RTX 5090 automatic architecture detection: libdevice_math
  • RTX 5090 forward sm_86 execution with CUDA 13.0: all three regression examples
  • RTX 5090 native sm_120 execution: all three regression examples
  • Ordinary PTX execution regression

Current limits

  • This machine does not have a physical pre-Blackwell GPU or a CUDA 12
    toolkit. The legacy modules were accepted by the real LLVM 7 parser selected
    by CUDA 13 libNVVM and executed through the forward PTX path on Blackwell.
    The contributor's original CUDA 12.4 / T1200 report establishes the failure
    on main; native pre-Blackwell and CUDA 12 validation remain useful follow-up
    hardware checks.
  • LLVM 7 NVVM output does not yet support cuda-oxide debug metadata. The
    compiler reports this instead of emitting unverified legacy debug IR.
  • The legacy NVVM legalizer currently rejects LLVM atomic operations. NVVM
    supports a defined subset, but the required validation and lowering have not
    yet been implemented.
  • LLVM fences require lowering to NVVM memory-barrier operations, while scalar
    f16 is not supported by the CUDA 12 LLVM 7 dialect. Both are rejected
    instead of being emitted with incorrect semantics.
  • UnsupportedPayloadKind(272) is a separate artifact-handling problem and is
    intentionally deferred to another PR.

Checklist

  • Original contributor commit and authorship preserved
  • All PR commits include Signed-off-by
  • No files under crates/cuda-bindings/ are modified
  • New Rust source files include SPDX headers
  • Documentation and example comments use plain, user-facing language

@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks for the PR. This one actually has a larger scope.

  • Typed pointers support for the older version of LLVM IR is on our to do list.

But we'd need to land a couple of other things first, such as migrating from dialect-llvm to upstream pliron-llvm and leveraging a more structured dataflow oriented design for how pointer types are propagated across the pipeline.

I'll keep this PR open till those land and will layer them on top of this one.

@nihalpasham nihalpasham added enhancement New feature or request Depends Blocked on another PR/issue or an upstream dependency llvm-export Textual LLVM/NVVM IR exporter (llvm-export crate) labels Jun 3, 2026
mohamedsamirx and others added 2 commits June 27, 2026 11:43
Select the NVVM dialect from one validated target, legalize current LLVM operations to the supported LLVM 7 subset, and export erased pointers through a canonical byte-pointer representation with exact per-use and ABI adapters.

Verify modules with libNVVM, persist target identity through artifacts, fail closed on ambiguous routing, and bridge unsuffixed legacy LTOIR through PTX for forward execution on Blackwell.

Add the issue NVIDIA#98 community math regression, adversarial pointer-shape coverage, and soundness tests for address spaces, control-flow merges, extern boundaries, and legacy intrinsic rewrites.

Signed-off-by: nihalpasham <[email protected]>
@nihalpasham
nihalpasham force-pushed the typed-nvvm-ir-pre-blackwell branch from a315b72 to 4b9fd56 Compare June 27, 2026 08:02
@nihalpasham nihalpasham added bug Something isn't working IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) intrinsics Device intrinsics and libdevice math mappings cargo-oxide The `cargo oxide` subcommand and build automation and removed enhancement New feature or request Depends Blocked on another PR/issue or an upstream dependency needs-implementation labels Jun 27, 2026
@nihalpasham
nihalpasham force-pushed the typed-nvvm-ir-pre-blackwell branch from e1fc138 to b7cf185 Compare June 27, 2026 08:52
@nihalpasham
nihalpasham merged commit b0774f6 into NVIDIA:main Jun 27, 2026
26 checks passed
meefs pushed a commit to meefs/cuda-oxide that referenced this pull request Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working cargo-oxide The `cargo oxide` subcommand and build automation 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nvvmCompileProgram fails with "parse expected type" when libdevice calls (__nv_expf) trigger NVVM IR mode

2 participants