Skip to content

feat(cargo-oxide): add emit-ltoir to build a crate's LTOIR in one step - #256

Merged
nihalpasham merged 3 commits into
NVIDIA:mainfrom
alejandro-soto-franco:pr/emit-ltoir
Jun 20, 2026
Merged

nihalpasham merged 3 commits into
NVIDIA:mainfrom
alejandro-soto-franco:pr/emit-ltoir

Conversation

@alejandro-soto-franco

Copy link
Copy Markdown
Contributor

What

Adds cargo oxide emit-ltoir <crate>, which builds a crate's device code to a
binary LTOIR artifact in one step.

cargo oxide build --emit-nvvm-ir stops at NVVM IR, leaving the caller to run
libNVVM separately to get linkable LTOIR. emit-ltoir folds both halves into a
single command for the Tile-to-SIMT interop workflow (#96), where cuda-oxide
is the SIMT participant: it builds the crate in NVVM IR mode, compiles the
emitted <crate>.ll with libNVVM -gen-lto, and writes <crate>.ltoir.

Details

Tests

  • Unit tests for the arch normalization (nvvm_compute_arch) and the
    capability parse (compute_capability).
  • Documents the command and the compute_100 floor in the cargo-oxide README.

cargo test -p cargo-oxide (26 passed), cargo fmt --check, and
cargo clippy -p cargo-oxide are all clean.

Part of #96.

`cargo oxide build --emit-nvvm-ir` stops at NVVM IR, leaving the caller to run
libNVVM separately to get linkable LTOIR. `emit-ltoir` folds both halves into a
single command for the Tile-to-SIMT interop workflow (NVIDIA#96), where cuda-oxide is
the SIMT participant: it builds the crate in NVVM IR mode, compiles the emitted
`<crate>.ll` with libNVVM `-gen-lto`, and writes `<crate>.ltoir`.

`--arch` is required (LTOIR is architecture-specific) and accepts sm_XX,
compute_XX, or a bare XX; `-o/--output` overrides the default `<crate>.ltoir`
path. The command reuses the existing libnvvm-sys binding and the NVVM IR build
path, and rejects metadata interop examples.

cuda-oxide exports NVVM IR 2.0 (opaque pointers), which libNVVM only accepts for
compute_100 and newer (Blackwell+); older targets reject the module while
parsing types, surfacing as a bare `parse expected type` with no hint at the
cause. On a libNVVM failure below compute_100, `emit-ltoir` now appends a short
explanation pointing at NVIDIA#98 and suggesting sm_100 or newer. The hint is gated on
actual failure so it disappears if the floor ever moves.

Adds unit tests for the arch normalization and the capability parse, and
documents the command and the compute_100 floor in the cargo-oxide README.

Part of NVIDIA#96.

Signed-off-by: Alejandro Soto Franco <[email protected]>
@nihalpasham nihalpasham added enhancement New feature or request cargo-oxide The `cargo oxide` subcommand and build automation interop Interop with external compilers / frontends labels Jun 20, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks for the PR. LGTM 👍🏾

Reviewer note: three fixes layered on our side before merge.

  1. libdevice omission (correctness): compile_nvvm_to_ltoir was adding only the kernel module to the libNVVM program. Any kernel that calls libdevice math (expf, sinf, etc.) would produce LTOIR with unresolved __nv_* symbols. Fixed by adding libdevice.10.bc first, matching the pattern in cuda-host/src/ltoir.rs.

  2. Double success message: codegen_build printed "✓ Build succeeded" before emit_ltoir printed its own "✓ LTOIR written to ...". Fixed by adding a quiet: bool parameter to codegen_build; emit_ltoir passes true so only the unified LTOIR summary is shown.

  3. README flags table: the duplicate --arch row was merged into one row with "Applies to: run, build, pipeline, emit-ltoir". A one-line note below the table clarifies that --arch is required for emit-ltoir only.

Partially addresses #96 (the Phase 1 emit-ltoir checklist item). Related to #98 (the helpful error hint is wired up; the typed-pointer export root cause remains a separate workstream).

…n README flags table

Three correctness and polish fixes layered on alejandro-soto-franco's original:

1. compile_nvvm_to_ltoir now adds libdevice.10.bc before the kernel module,
   matching the pattern in cuda-host/src/ltoir.rs. Without this, any kernel
   that calls libdevice math (__nv_expf, __nv_sinf, etc.) would produce LTOIR
   with unresolved __nv_* symbols, silently breaking the artifact at link time.

2. codegen_build gains a quiet: bool parameter. emit_ltoir passes quiet=true
   so the intermediate "Build succeeded" line is suppressed; the unified
   "LTOIR written to ..." line is the single summary the user sees.

3. README flags table merges the two --arch rows into one (the duplicate was
   introduced by this PR), widens the "Applies to" column uniformly, and adds
   a one-line note clarifying that --arch is required for emit-ltoir.
Wrap the bare issue URL in angle brackets and backtick-escape the
<crate-dir>/<crate> placeholder to prevent rustdoc -D warnings from
treating them as an invalid hyperlink and unclosed HTML tags.
@nihalpasham
nihalpasham merged commit cdfeac0 into NVIDIA:main Jun 20, 2026
24 checks passed
@alejandro-soto-franco
alejandro-soto-franco deleted the pr/emit-ltoir branch June 28, 2026 03:18
meefs pushed a commit to meefs/cuda-oxide that referenced this pull request Sep 30, 2026
…NVIDIA#225, NVIDIA#185) (NVIDIA#256)

Three squash merges landed from branches that had resolved a merge against
main by keeping pre-merge copies of files, so each one quietly reverted an
earlier, unrelated merge:

- NVIDIA#239 (2482cb9) reverted NVIDIA#240 (1216aef): the const raw-pointer surface
  (as_ptr, program_id/num_programs, the cast_* and get_tensor_base helpers,
  unsafe on load_ptr_tko/store_ptr_tko), the compiler's const-pointer
  typing, the two GPU test files and the matching book and tutorial text.
- NVIDIA#247 (5f5f4c5) reverted NVIDIA#225 (a2843e7): integer division and remainder
  serialization (rounding<zero> and inferred signedness on divi/remi) and
  its test.
- NVIDIA#187 (35a9cc2) reverted NVIDIA#185 (cbf52c0): the flake.nix toolkit-assembly
  hardening and host-arch driver directory derivation.

This re-applies all three verbatim as cherry-picks. The only overlapping
file, cutile-book/guide/performance.md, merges cleanly with NVIDIA#239's own
edits. A per-file scan of the last 60 merges finds no other reversal still
missing at HEAD: NVIDIA#249's removal of the About page was deliberate, and the
two setup-python flips in .github/workflows/pages.yml were re-applied by
the later dependabot bump.

Verified: cargo fmt, cargo build, and the restored GPU tests
(const_pointers, program_id) plus integer_ops, memory_and_atomic_ops and
optimization_hints pass on an RTX 5090 with CUDA 13.3.

Signed-off-by: Melih Elibol <[email protected]>
Co-authored-by: Gabriel Wu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cargo-oxide The `cargo oxide` subcommand and build automation enhancement New feature or request interop Interop with external compilers / frontends

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants