Skip to content

feat(finalizer): fail a link that drops an expected kernel - #1367

Merged
nihalpasham merged 4 commits into
NVIDIA:mainfrom
lucifer1004:feat/final-link-kernel-check
Nov 9, 2026
Merged

nihalpasham merged 4 commits into
NVIDIA:mainfrom
lucifer1004:feat/final-link-kernel-check

Conversation

@lucifer1004

@lucifer1004 lucifer1004 commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

What This PR Does

Verifies that every expected kernel entry point survives nvJitLink / PTX finalization in cuda-artifact-finalizer, failing closed with FinalizerError::MissingKernels if nvJitLink silently drops a module or strips an expected kernel:

  1. Entry Inventory Check (cuda-artifact-finalizer): After linking, inspects ELF cubin symbols (FUNC with STO_CUDA_ENTRY) or PTX .entry callables (ptx-parse) and verifies every expected kernel is present.
  2. Expected Kernel Provenance (mir-importer, rustc-codegen-cuda, cuda-host, cargo-oxide): Build-time materialization passes the collector's kernel export names, embedded bundles pass their Kernel entries, and sidecar .ll builds write <module>.kernels (cuda-oxide expected-kernels v1).
BEFORE:
  nvJitLink drops kernel during LTO link ──► returns Ok(empty cubin) ──► fails only at launch time

AFTER:
  Finalizer checks ELF `STO_CUDA_ENTRY` / PTX `.entry` symbols against `expected_kernels`
  ──► fails immediately at link/finalization time with `FinalizerError::MissingKernels`

Verification

  • cargo test -p cuda-artifact-finalizer -p cuda-host --all-targets: PASS
  • just fmt-check, cargo clippy, sync-example-locks.sh --check, and repository guards: PASS

Refs #1071

@copy-pr-bot

copy-pr-bot Bot commented Sep 30, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

nvJitLink can report success while dropping a module's kernels: an
unresolved device-runtime symbol makes it discard the module, and the
link-time optimizer strips a kernel it finds unreachable. The result is a
well-formed but empty image, which is_valid_cubin accepts.

Every finalizer link now takes the kernels its output must define and
checks the output's entry points after the link: cubin entries from the
ELF symbol table (FUNC symbols with STO_CUDA_ENTRY), PTX entries from
ptx-parse. A missing kernel is FinalizerError::MissingKernels, and an
image whose entries cannot be read is UnreadableEntryInventory. Rooting
stays with the @llvm.used the exporter already emits; nvJitLink's
-kernels-used is not passed, since it also lets the link drop kernels it
does not list.

The expected kernels reach every finalization route:
- build-time materialization: the collector's kernel export names;
- embedded bundles finalized at load: the bundle's Kernel entries;
- sidecar files (cargo oxide interop, cuda-host's file loader): a new
  <module>.kernels sidecar the compiler writes next to the .ll, before the
  completing .target. An artifact the compiler emitted must have it; a
  manual .ll is checked against one only when present.

Cached cubins are checked on a hit too, since a stored image may predate
the check. The cuda-host build/link functions and the finalizer entry
points take the expected kernels as a new argument.

Tests: synthetic empty and partial cubins, PTX with only device
functions, unreadable inventories, the sidecar format, and a live link
whose unrooted kernel nvJitLink drops (cubin and PTX); the live file-cache
test now roots its kernel, which it previously linked away.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: Zihua Wu <[email protected]>
@lucifer1004
lucifer1004 force-pushed the feat/final-link-kernel-check branch from 66f9ad8 to 770c4e8 Compare September 30, 2026 00:58
@nihalpasham nihalpasham added the cuda-oxide SIMT programming model: rustc backend, cargo oxide, cuda-device, cuda-host, book label Oct 11, 2026
@nihalpasham nihalpasham added enhancement New feature or request codegen Device code-generation pipeline (Rust MIR to IR to PTX) safety Memory safety, soundness, or undefined behavior labels Nov 3, 2026
@nihalpasham
nihalpasham self-requested a review as a code owner November 3, 2026 11:57
nihalpasham
nihalpasham previously approved these changes Nov 3, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

/ok to test 6bf77be

@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks, verifying that every expected kernel entry survives nvJitLink / PTX finalization (STO_CUDA_ENTRY in ELF cubins and .entry in PTX) closes a silent link-time drop hazard cleanly.

At 6bf77beb, rebased onto main's cuda-oxide/ layout, updated the example lockfiles, and verified cuda-artifact-finalizer, cuda-host, mir-importer, and rustc-codegen-cuda suites and clippy. Merging once CI is green.

@nihalpasham

Copy link
Copy Markdown
Collaborator

/ok to test 6c46708

@nihalpasham

Copy link
Copy Markdown
Collaborator

/ok to test 63e6f01

@nihalpasham
nihalpasham merged commit 8d82548 into NVIDIA:main Nov 9, 2026
78 checks passed
nihalpasham added a commit that referenced this pull request Nov 9, 2026
…oir (#1766)

## What This PR Does

Passes the generated `call_<decl>` kernel entry names (`call_relu`,
`call_scale_in_place`, `call_warp_sum`) to `LtoLinker::link_ltoir` in
`examples/tilelib_relu` and ignores `*.kernels` sidecars in
`cuda-oxide/.gitignore`:

1. **Expected Kernel List
(`crates/rustc-codegen-cuda/examples/tilelib_relu/src/main.rs`)**
2. **Sidecar Ignore Rule (`cuda-oxide/.gitignore`)**

---

### 1. Matching `caller.cu`'s Generated Entry Names in `tilelib_relu`

`caller_source` in `tilelib_relu` generates one `__global__` wrapper
kernel per callable `#[tile_export]` named `call_<decl>`. Passing
`exports.iter().map(|desc| format!("call_{}", desc.decl))` to
`LtoLinker::link_ltoir` matches the actual cubin entry table:

```text
BEFORE (#1367):
  link_ltoir(..., &["caller"])
  ──► fails at runtime: `missing expected kernel(s) ["caller"] (3 entry point(s) present)`

AFTER:
  expected_kernels = ["call_relu", "call_scale_in_place", "call_warp_sum"]
  ──► `link_ltoir` verifies all 3 generated wrapper kernels in the linked cubin
```

---

## Verification

* `cargo oxide run tilelib_relu --emit-nvvm-ir --arch sm_100`: **PASS**
(`3/3 tile exports verified through the native ABI`)
* `cargo clippy --manifest-path
cuda-oxide/crates/rustc-codegen-cuda/examples/tilelib_relu/Cargo.toml
--all-targets -- -D warnings`: **PASS**
* `rustfmt --check --edition 2024
cuda-oxide/crates/rustc-codegen-cuda/examples/tilelib_relu/src/main.rs`:
**PASS**

---------

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

Labels

codegen Device code-generation pipeline (Rust MIR to IR to PTX) cuda-oxide SIMT programming model: rustc backend, cargo oxide, cuda-device, cuda-host, book enhancement New feature or request safety Memory safety, soundness, or undefined behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants