Skip to content

feat(codegen): opt-in build-time cubin materialization for embedded artifacts - #340

Merged
nihalpasham merged 4 commits into
NVIDIA:mainfrom
bitemyapp:feat/materialize-cubin
Jul 21, 2026
Merged

nihalpasham merged 4 commits into
NVIDIA:mainfrom
bitemyapp:feat/materialize-cubin

Conversation

@bitemyapp

@bitemyapp bitemyapp commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does

Some cuda-oxide deployments finish compiling embedded NVVM IR or LTOIR when
the program first loads. That adds startup work and can require libNVVM and
nvJitLink on the deployment machine.

This PR adds an explicit opt-in that finishes that compilation during the host
build and embeds native GPU code instead:

cargo oxide build vecadd --materialize-cubin --arch sm_86
NVVM IR + libdevice -> LTOIR -> nvJitLink -> embedded sm_86 cubin

Default artifact behavior is preserved: without --materialize-cubin, builds
continue to embed PTX.

Why this is useful

For a deployment with a known GPU architecture, the program can load the cubin
directly through the CUDA driver. It does not need to run libNVVM or nvJitLink
at startup.

The trade-off is intentional: a cubin targets one real GPU architecture. It
does not provide the forward compatibility or multiple architectures of PTX
or a CUDA fatbinary.

How it works

  • Build-time and load-time CUDA finalization share one driver-independent
    implementation.
  • The selected architecture, FMA policy (including --no-fmad), and debug
    policy are preserved through libNVVM and nvJitLink.
  • Cache keys include the exact source artifact, ordered options, compiler and
    linker identities, toolkit inputs, and libdevice.
  • The codegen backend still has no direct dependency on the CUDA driver or
    toolkit libraries.
  • Missing architecture, incompatible output modes, multiple final artifacts,
    and unresolved late-link inputs fail with clear errors instead of producing
    an incomplete cubin.

Cargo cache correctness and CI performance

The previous head was not a performance repair. It made target, output, and
materializer fingerprints global CARGO_ENCODED_RUSTFLAGS, so every mode or
architecture change invalidated Cargo's shared dependency cache.

Hosted comparison before this repair:

  • main (52accfa4): compile-examples step 10m25s.
  • Previous PR head (7616282c): 22m52s.
  • generated_intrinsics: 3s -> 127s.
  • generated_intrinsics_blackwell: 2s -> 122s.
  • tcgen05: 4s -> 124s.

This repair splits the cache boundary according to ownership:

  • The exact backend .so SHA-256 remains global because that backend compiles
    every crate.
  • Architecture, output mode, FMA/debug policy, owner filter, and exact
    materializer provenance are a full SHA-256 environment dependency recorded
    by CUDA procedural macros only in crates that can own or instantiate device
    artifacts.
  • Exact materializer provenance is tracked separately and is still rediscovered
    and byte-checked by the backend before cubin generation.

The workflow now checks Cargo compilation events rather than imposing a timing
threshold. PTX -> cubin -> PTX may rebuild vecadd, and an sm_86 -> sm_90
cross-crate switch may rebuild kernel-lib plus its consumer, but either
transition fails CI if an unrelated shared dependency recompiles.

Local validation of the repaired candidate compiled all 145 examples in 181s.
The formerly regressed examples completed in 2s, 2s, and 3s respectively. A
focused retained-log run confirmed that each compiled only its owning example
crate. These local timings are supporting evidence; the authoritative hosted
comparison will be recorded after the repaired head runs in CI.

Validation

  • 86 cargo-oxide tests, including cold/warm/architecture/output/provenance
    Cargo JSON fresh assertions for an owner, downstream consumer, and plain
    shared dependency.
  • 69 CUDA macro unit tests and the existing macro compile-test suite.
  • 17 codegen-backend tests.
  • Strict clippy for the affected root and backend crates.
  • Full GPU-less compile sweep: 145/145 examples passed in 181s.
  • Real PTX -> cubin -> PTX and sm_86 -> sm_90 cross-crate cache transitions.
  • Extracted cubin inspection: CUDA ELF sm=86 with exported vecadd entry.
  • Current macros compiled successfully with the exact previous backend head,
    which recognized vecadd and embedded a non-empty PTX artifact.

No CUDA device was available, so this validation does not make a GPU-execution
claim.

Issue relationship

Issues #378, #388, and #390 are related, but this PR does not close them.

History

The contributor-authored, DCO-compliant commit is preserved. Separate signed
maintainer commits rebuild the feature around the shared finalizer and repair
the Cargo cache boundary.

@nihalpasham nihalpasham added enhancement New feature or request codegen Device code-generation pipeline (Rust MIR to IR to PTX) build-related Build scripts, toolchain discovery, or compilation setup labels Jul 20, 2026
…rtifacts

Setting CUDA_OXIDE_MATERIALIZE_CUBIN (presence-based, mirroring
CUDA_OXIDE_EMIT_NVVM_IR) makes the backend compile NVVM IR and LTOIR
device artifacts to a final cubin before embedding them, so consuming
binaries load device code directly through the CUDA driver: no libNVVM
or nvJitLink on the deployment host, and no first-load compile hit.
PTX and cubin artifacts pass through untouched.

Off by default: the embedded IR keeps compiling at first load, where
cuda-host's execution routing can still take the forward-compatible PTX
bridge when the running GPU is newer than the emitted architecture. A
materialized cubin is pinned to the emitted architecture, so the opt-in
trades multi-arch flexibility for zero runtime JIT dependencies —
appropriate when build and deployment targets are known and fixed.

The backend talks to libnvvm-sys / nvjitlink-sys directly (both dlopen
their libraries at call time), deliberately not through cuda-host: that
would link the backend dylib against the CUDA driver (libcuda.so.1) and
break building or loading the backend on machines without a GPU driver.
The compile sequence (frontend validation, libdevice, -gen-lto, -lto
link) mirrors cuda-host's ltoir module and cargo-oxide's emit-ltoir.

Signed-off-by: Chris Allen <[email protected]>
Signed-off-by: nihalp <[email protected]>
@nihalpasham
nihalpasham force-pushed the feat/materialize-cubin branch from 8d8828f to 05552e7 Compare July 21, 2026 07:19
Move build-time and runtime CUDA artifact finalization onto one driverless implementation with exact toolkit-input provenance, explicit architecture and policy handling, and fail-closed collection contracts. Expose the opt-in through cargo oxide, preserve the default PTX path, add artifact and backend regressions, and exercise both public paths in CI.

Signed-off-by: nihalp <[email protected]>
@nihalpasham
nihalpasham force-pushed the feat/materialize-cubin branch from 05552e7 to 3f04c20 Compare July 21, 2026 07:34
Compile sha2 with optimization in the workspace dev profile so cargo oxide can retain exact content-based backend fingerprints without spending several seconds hashing the same large debug library on every example invocation.

Signed-off-by: nihalp <[email protected]>
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks for the PR. I pushed a maintainer follow-up to fix the Cargo cache regression introduced while completing the cubin materialization path.

  • --materialize-cubin still finishes compilation during the host build and embeds an architecture-specific cubin.
  • Architecture and PTX/cubin changes now rebuild only the CUDA crate and its consumers, rather than unrelated shared dependencies.
  • Backend and materializer inputs remain fingerprinted, and stale output from older macros is rejected.

For example:

Before: sm_86 -> sm_90 -> shared dependencies + kernel + application rebuild
After:  sm_86 -> sm_90 -> kernel + application rebuild

Reviewer note: All 145 compile-only examples passed locally in 181s; the three previously regressed examples completed in 2s, 2s, and 3s. Package and backend tests, strict clippy, formatting, compatibility checks, and independent post-repair review passed. No CUDA device was available, so this is compile and artifact validation only. Hosted CI is pending and remains the final merge gate.

#378, #388, and #390 are related; this PR does not close them.

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

Labels

build-related Build scripts, toolchain discovery, or compilation setup codegen Device code-generation pipeline (Rust MIR to IR to PTX) enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants