Skip to content

fix(codegen): pin library-crate device artifacts with a link-anchor symbol - #166

Merged
nihalpasham merged 1 commit into
mainfrom
fix/issue72-cuda-module-in-lib-crate
Jun 11, 2026
Merged

nihalpasham merged 1 commit into
mainfrom
fix/issue72-cuda-module-in-lib-crate

Conversation

@nihalpasham

Copy link
Copy Markdown
Collaborator

What

A #[cuda_module] defined in a library crate now survives into the final binary; previously it failed at runtime with ModuleNotFound.

A library crate ships its PTX in an extra object file holding only the
.oxart data section. That object becomes an rlib archive member, and
linkers only extract members that define a symbol somebody references.

Before: artifact object defines NO symbols -> linker silently drops the
        archive member -> ModuleNotFound at runtime
After:  artifact object defines a per-bundle anchor symbol; the
        generated load path references its address (through black_box)
        -> undefined reference -> linker extracts the member

Evidence

  • Regression example cuda_module_in_lib puts the #[cuda_module] in a nested library crate and loads it from the binary; executes on sm_120 (this is the actual runtime repro, so the fix is proven on hardware, not just by object inspection).
  • Independent adversarial review with revert-proof: reverting loses both anchor symbols, shrinks .oxart, and reproduces the missing-bundle failure. Retention proven by nm/objcopy inspection, under -Wl,--gc-sections (SHF_GNU_RETAIN), and for a partial-use rlib via lld --why-extract showing the exact designed extraction chain.
  • Anchors are local/hidden after link (no dynsym pollution); the same-crate path is unchanged and cross_crate_kernel still passes.
  • New oxide-artifacts unit tests pin the anchor definition; tests/clippy/fmt clean; compile-only smoketest green.

Fixes #72

…ymbol

A #[cuda_module] in a library crate compiles its PTX into an extra
object file holding only the .oxart data section. For binary crates
that object is passed straight to the linker and always survives, but
for library crates it becomes an rlib archive member, and linkers only
extract archive members that define a symbol someone references. The
artifact object defined no symbols, so the member was silently dropped
and kernels::load(&ctx) failed at runtime with
ModuleNotFound { name: "<lib package name>" }.

Fix it with a symbol handshake, the same mechanism nvcc uses to anchor
fatbin registration objects:

- oxide-artifacts: build_host_object_for_target can now define a
  global hidden data symbol at the start of the .oxart section.
- rustc-codegen-cuda: derives the anchor name from CARGO_PKG_NAME and
  CARGO_PKG_VERSION and defines it in every embedded artifact object.
- cuda-macros: the generated load_named() reads the anchor's address
  through black_box, so any caller of load() carries an undefined
  reference that forces the linker to extract the artifact member.
  Modules with only generic kernels skip the reference (their PTX is
  monomorphized and embedded in the consuming crate); modules whose
  concrete kernels are all cfg-gated guard the reference with the
  disjunction of those cfg conditions.
- reserved-oxide-symbols: owns the anchor naming contract
  (artifact_anchor_symbol). The version is part of the name so two
  versions of one package each keep their own bundle.

The new cuda_module_in_lib example puts the #[cuda_module] in a nested
library crate and verifies, before any CUDA call, that both the
library bundle and the binary's own bundle are embedded in the
executable, then loads and launches kernels from both modules.

Fixes #72
@nihalpasham nihalpasham added bug Something isn't working host-apis Host-side runtime APIs (cuda-host, cuda-core, cuda-async) codegen Device code-generation pipeline (Rust MIR to IR to PTX) labels Jun 11, 2026
@nihalpasham
nihalpasham merged commit 1094493 into main Jun 11, 2026
24 checks passed
@nihalpasham
nihalpasham deleted the fix/issue72-cuda-module-in-lib-crate branch June 12, 2026 10:19
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 codegen Device code-generation pipeline (Rust MIR to IR to PTX) host-apis Host-side runtime APIs (cuda-host, cuda-core, cuda-async)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ModuleNotFound after I move a #[cuda_module] into a separate crate

1 participant