Repository navigation
Description
Background
The Rust→NVVM IR→nvJitLink pipeline currently cannot call CUDA device-runtime APIs from kernels, because those symbols are not defined in any Rust crate and must be resolved at the final device link. We have a local, not-yet-PR'd commit that adds exactly one such API — cudaGraphSetConditional — to support explicit CUDA Graph conditional nodes. We would like to agree on the general design before the PR series grows.
This issue tracks the design discussion for device-side CUDA runtime API support as a whole.
What we have working today
A single-symbol vertical slice, ~370 lines across 16 files:
cuda-device:unsafe extern "C" fn cudaGraphSetConditional(handle: u64, value: u32)declaration- collector: leaves this exact symbol unresolved during monomorphization collection
- mir-lower: declares it at the call site, like
__nv_*libdevice externs - codegen pipeline: detects the call in typed MIR, sets
requires_cuda_device_runtime, and forces the NVVM IR / external-link route - finalizer: discovers
libcudadevrt.a, fingerprints it (SHA-256 handshake with the cargo-oxide driver), and feeds it to nvJitLink as anInputType::Libraryalongside the LTOIR inputs
This produces a working sm_120a cubin whose selector kernel drives a real conditional node, loaded and exercised on hardware.
Empirical findings that should shape the design
All observed with the CUDA 13.3 toolchain (libNVVM 2.0 / nvJitLink 13.3), sm_120a:
cudaGraphSetConditionaldoes not need the archive. nvJitLink recognizes it and lowers the call to the driver service ABI (CALL.ABS.NOINCviac[0x4][0x28]); with and withoutlibcudadevrt.aon the link, the kernel's SASS is instruction-identical. This reserved-symbol behavior appears to be undocumented — the nvJitLink user guide never mentions cudadevrt or reserved symbols.cudaMallocandcudaGraphLaunchdo need the archive. Without it, nvJitLink returns success but silently drops every kernel of the offending module, producing a valid but empty cubin (~2.2 KB). With the archive, real device code fromcuda_device_runtime.cuis linked in and all entries survive. (Consistent with--keep-system-librariesdocs: the device linker GCs unreferenced system-library code.)- Symbol naming splits by family. The classic CDP families (device malloc/free, streams, events, async memcpy/memset, introspection) are declared under CDP2-internal
__cudaCDP2*names, which the header explicitly marks private ("Symbols beginning with __cudaCDP* should not be used outside this header file"). The graph family is exempt:cudaGraphLaunchandcudaGraphSetConditionalare plainextern __device__under their public names. So a curated symbol table can use stable public names for the graph family, while the classic CDP surface would mean depending on internal symbols.
Scope of the API family (CUDA 13.3 headers + docs)
Roughly 40+ extern "C" __device__ entry points in 8 families:
| Family | Examples | Resolution | Public name? |
|---|---|---|---|
| Device mgmt / introspection | cudaGetLastError, cudaDeviceGetAttribute |
archive | no (__cudaCDP2*) |
| Streams | cudaStreamCreateWithFlags, cudaStreamWaitEvent, tail/fire-and-forget stream constants |
archive | no (__cudaCDP2*) |
| Events | cudaEventCreateWithFlags, cudaEventRecordWithFlags |
archive | no (__cudaCDP2*) |
| Memory | device cudaMalloc/cudaFree, cudaMemcpy*Async, cudaMemset*Async |
archive | no (__cudaCDP2*) |
| CDP kernel launch | cudaGetParameterBufferV2, cudaLaunchDeviceV2 |
archive | no (__cudaCDP2*) |
| Device graph launch / node updates | cudaGraphLaunch, cudaGraphKernelNodeSetParam/SetEnabled/SetGridDim |
archive (verified for cudaGraphLaunch) |
yes |
| Conditional nodes | cudaGraphSetConditional |
nvJitLink reserved symbol (empirical) | yes |
| PDL | cudaTriggerProgrammaticLaunchCompletion, cudaGridDependencySynchronize |
header-inline PTX, no symbol | n/a |
Design questions
- Symbol modeling. Should the compiler recognize device-runtime symbols from a curated table (each entry added with a qualifying consumer and a link test), or generically from unresolved externs? We lean curated: deriving the link requirement from any unresolved symbol would mask typos and, combined with finding (2), defer real errors to runtime.
- Scope. Given finding (3), should device-runtime support be scoped to the graph family (stable public names) and leave the classic CDP surface out, unless there is a documented way to reference it without private
__cudaCDP2*symbols? - Silent-drop guard. Should the finalizer verify that expected entry kernels survived the link (or that no requested device-runtime symbol remains undefined) so finding (2) becomes a build-time error?
- Archive provenance. Is the SHA-256 digest handshake for
libcudadevrt.a(cargo-oxide fingerprints, rustc verifies at materialization) worth keeping, given the archive participates in real resolution for most of the family? - PR structure. One PR per symbol family with its own tests, or the mechanism + conditional-node slice first?
cc @nihalpasham — would appreciate your take on the direction before we start sending PRs.
Activity
nihalpasham commentedon Aug 21, 2026
Thanks. I went through the issue, your implementation, and the current compiler, artifact, finalizer, and runtime-loading paths. I think the direction is right.
The compiler model should be:
and:
Both produce the same result:
This does not require changing the public host APIs. However, it does affect host-executed tooling because
cargo-oxide, the shared finalizer, and deferredcuda-hostloading perform the device link.I see seven design requirements:
libcudadevrtin the provenance, handshake, and cache recipe.I would not make these seven separate PRs because several are parts of the same contract. I also would not put everything into one PR. My preference is this three-PR stack:
PR 1 — Final-link safety
Make expected-kernel retention a general finalizer invariant.
The invariant should be:
This is useful independently of CUDA Graphs and gives the later work a safe foundation.
PR 2 — Complete device-runtime link contract
Add the reusable linking mechanism, but keep it dormant—no public Graph API yet.
The following pieces should land together because splitting them could leave one loading path or cache unsound:
libcudadevrtdiscovery.cuda-hostfinalization.The result should be:
PR 3 — First public Graph API
Once all finalization paths understand the contract, expose
cudaGraphSetConditional.For example:
One important test point: the CUDA 13.3 experiment shows that nvJitLink resolves
cudaGraphSetConditionalwithoutlibcudadevrt. Since this is not a documented contract, that function alone cannot prove the archive path. PR 2 must exercise something that actually requires the archive.After these three PRs, additional documented Graph APIs can be added in small families. Device Graph Launch can have its own focused follow-up. Classic Dynamic Parallelism and private
__cudaCDP2*symbols should remain a separate design discussion.If three PRs create too much contributor overhead, two PRs are acceptable:
I do not recommend one all-in-one PR. It would combine finalizer correctness, artifact/provenance protocol changes, compiler recognition, public API safety, and GPU behavior—and
cudaGraphSetConditionalcould accidentally hide a broken archive path.Tests should land with the PR that introduces each rule, rather than in a later cleanup PR.
lucifer1004 commentedon Sep 30, 2026
Thanks for the detailed review — we'll follow the three-PR stack (final-link safety → dormant device-runtime link contract → first public Graph API), with each PR's tests landing in that PR. A few design points before we send PR 1, and one question for PR 2.
PR 1 (final-link safety) — planned shape
_TID_<hash>names). The sidecar routes (.ll/.target/.optionsconsumed bycargo oxideinterop andcuda-host's file loader) carry no kernel list today, so we'd add a<name>.kernelssidecar written by the compiler, rather than falling back to a weaker "non-empty inventory" check.@llvm.used, so we'd keep that and not pass nvJitLink-kernels-used, since that option also lets nvJitLink drop kernels not in the list.ptx-parse; cubin through an ELF symbol-table walk for entry functions), and fail with aMissingKernelserror naming the lost entries. Cached finalization only publishes an image after the check passes.PR 2 question: carrying device-link requirements
oxide-artifactsis the one piece consumed from crates.io (0.2.1), because cutile-rscuda-corereads bundles through it, andcheck-oxide-artifacts-parity.shkeeps the in-tree copy equal to that release. Recording "requires the device runtime" inside.oxartin a way old readers reject (e.g. a new bit inKNOWN_COMPILE_OPTIONS) therefore means publishing a newoxide-artifactsversion and bumping cutile-rscuda-core. Since there's no regular release process for this yet, how would you like format changes handled?oxide-artifacts(new compile-option bit, fail-closed on older readers), published by a maintainer ahead of PR 2; or.oxartfor now (e.g. only in sidecars and the finalizer's link request), accepting that embedded bundles can't declare it until a format bump; orFor the sidecar routes we'd extend the
.optionsheader version (or add a.requiressidecar) with the same fail-closed rule.Two smaller PR 2 points, unless you'd prefer otherwise:
nvjitlink-sysexpose the toolkit root it actually loaded from, and takelibcudadevrt.afrom that same root (CUDA_OXIDE_CUDADEVRTas an explicit override), so the archive and the linker can't come from different toolkits. The archive is pinned in a new materializer handshake and recipe version, and the load-time cache key includes it.cudaGraphLaunch, becausecudaGraphSetConditionalalone links without it.lucifer1004 commentedon Sep 30, 2026
An update from implementing PR 3 (
cudaGraphSetConditional), with one change to the plan and one correction to my earlier comment.cudaGraphSetConditionaldoesn't need the NVVM route. The plan said to select NVVM for direct and reachable helper calls. Measured with CUDA 13.3 on sm_120:nvcc -ptx(whole-program, no-rdc) emits a plain.extern .func cudaGraphSetConditional.ptxaswithout-caccepts it and records it as a driver-resolved extern (externs: cudaGraphSetConditional), not as an unresolved-symbol error. The header declares it__cudart_builtin__.--full-debugcensus, which builds through the direct LLVM PTX route.So the PR keeps the curated registry (exact C ABI, resolution route, CUDA 12.3 minimum), but the only route is "driver-resolved at module load". A listed call:
call to CUDA device runtime API ... does not match its C signature;Any other extern keeps its current meaning. This also means PR 3 no longer depends on PR 2. It sits directly on
main, so the artifact-format question above only matters for PR 2 and any later archive-resolved API.Correction for PR 2. I said its archive fixture would call
cudaGraphLaunch. That's wrong:libcudadevrt.adoesn't definecudaGraphLaunch(the driver resolves it too). The archive does definecudaMallocand similar (via__cudaCDP2*); without it, ptxas fails with "Unresolved extern function". So PR 2's fixture callscudaMalloc.The PR 3 branch is ready: https://github.com/lucifer1004/cuda-oxide/tree/feat/graph-set-conditional. It adds the registry, the ABI check,
cuda_device::graph::{ConditionalHandle, set_conditional}with the same-graph/context and no-racing safety contract, thegraph_conditionalGPU example (standard smoketest lane, compile-only and full-debug pass), and book updates. I'll open it once PR 1 settles, unless you'd rather see it now.feat(finalizer): fail a link that drops an expected kernel (#1367)