Repository navigation
fix(collector): actionable errors for heap allocation and missing #[device] - #168
Merged
Merged
Conversation
…g #[device] helpers Two long-standing inscrutable failure modes in device codegen now fail fast in the collector, with spans pointing into user code: - Heap allocation in a kernel (vec!, Box) used to die during constant translation with "Struct constant field 1 has unsupported type" spanned into alloc/src/boxed.rs. The collector now recognizes the __rust_alloc shim family when it is reached from device code and reports "heap allocation is not supported in kernels (no device allocator); use fixed-size arrays or SharedArray", pointing at the allocating line inside the kernel. The shim list is the single switch point to revisit if a device allocator ever lands. - A helper called from a kernel without #[device] resolves to cuda-device's public thread::index_* stub, whose unreachable body gets MIR-inlined into the helper as panic machinery. The collector used to mistake the collapsed helper for an intrinsic placeholder and silently drop it, which ended in an opaque "Symbol ... not found" module-verification error (or a string-constant translation error in inlined shapes). The collector now detects the stub's panic-message marker and reports the missing #[device] with the helper's own span; bodies that materialize panic message strings in statements get a likely-causes diagnostic instead of the translate_constant bail. Conditional panics whose message travels only in call arguments keep compiling to a device-side trap. Infrastructure: each discovery edge now propagates the nearest user-code span and the originating kernel (DiscoveryCtx), with MIR inlining unwound through the source-scope tree (outermost_user_span), so diagnostics point at the line the user wrote instead of sysroot internals. New error-category fixtures pin the message texts: error_heap_alloc and error_missing_device_attr, registered in smoketest ERROR_EXAMPLES and STATUS.md. Validation: cargo test + clippy -D warnings on rustc_codegen_cuda, rustfmt clean (workspace root and both example dirs), full compile-only smoketest 82/82 green, and both fixtures verified to reproduce the original errors with the collector change reverted. Fixes #76 Fixes #108
Review follow-ups for the diagnostics pair: - The heap-allocation guard matched the __rust_alloc shim family by bare name, but none of those names is actually reserved: a user may define their own fn called __rust_alloc, and it compiles for the device like any other helper. The matcher now also requires the sysroot path prefix (alloc::/std::alloc::), and the call site skips local definitions by DefId. The helper_fn example gains a name-collision probe pinning that a local __rust_alloc compiles without tripping the guard. - Corrected four comments claiming panic paths lower to a device-side trap; the actual lowering is LLVM `unreachable`. The pre-existing gap that follows from that (the optimizer may delete user-written assert\! paths that provably reach it) is real and tracked separately, not fixed here.
useless_vec fires on the vec\! that IS the heap-alloc fixture's shape under test, and non_snake_case fires on the #[device]-generated symbol for the __rust_alloc collision probe. Scoped allows with comments, per the never-rewrite-the-shape-under-test rule.
# Conflicts: # crates/rustc-codegen-cuda/STATUS.md
nihalpasham
added a commit
to haixuanTao/cuda-oxide
that referenced
this pull request
Jun 12, 2026
The collector skips the whole libm crate, so a libm call the float-math dispatch does not recognize can never resolve: it used to surface only at module verification as a bare "Symbol libm__cbrtf not found", far from the offending kernel. Bail out at the call site instead, naming the libm function and where to add the mapping, following the per-site diagnostics direction of NVIDIA#168.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Two "the error sends you to the wrong place" bugs become clear, spanned-at-your-code diagnostics.
The heap guard fires in the collector at the first point the allocator shim family is provably reached from device code. Per review hardening: only sysroot paths can match (none of the
__rust_*names is actually reserved; a user-defined__rust_allocis legal and now compiles — pinned by a name-collision probe inhelper_fn), and local definitions are skipped byDefId.Both behaviors are pinned as error-category fixtures (
error_heap_alloc,error_missing_device_attr), registered in smoketest's ERROR_EXAMPLES and STATUS.md (status-guard script green).Also corrects four comments that claimed panic paths lower to a device-side trap; the real lowering is LLVM
unreachable. The soundness gap that implies (the optimizer may delete user-writtenassert\!paths) is pre-existing, demonstrated during review, and will be tracked as its own issue, not silently fixed here.Evidence
helper_fn(with the collision probe) executes on sm_120.Fixes #76
Fixes #108