Skip to content

fix(collector): actionable errors for heap allocation and missing #[device] - #168

Merged
nihalpasham merged 5 commits into
mainfrom
fix/issue76-108-actionable-diagnostics-v2
Jun 11, 2026
Merged

nihalpasham merged 5 commits into
mainfrom
fix/issue76-108-actionable-diagnostics-v2

Conversation

@nihalpasham

Copy link
Copy Markdown
Collaborator

What

Two "the error sends you to the wrong place" bugs become clear, spanned-at-your-code diagnostics.

#108 before: vec\![x] in a kernel -> "Struct constant field 1 has
             unsupported type" spanned into alloc/src/boxed.rs
     after:  "heap allocation is not supported in kernels (no device
             allocator); use fixed-size arrays or SharedArray"
             spanned at the user's vec\!/Box line

#76  before: helper without #[device] -> "Type translation not yet
             implemented for: RigidTy(...)" pointing into core's
             panic machinery
     after:  "`thread::index_1d` only works inside #[kernel] /
             #[device] functions; here it resolves to a host-only stub
             that panics..." at the helper definition, with a call-site
             note and the #[device] suggestion

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_alloc is legal and now compiles — pinned by a name-collision probe in helper_fn), and local definitions are skipped by DefId.

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-written assert\! paths) is pre-existing, demonstrated during review, and will be tracked as its own issue, not silently fixed here.

Evidence

  • Both fixtures fail with the exact pinned messages; helper_fn (with the collision probe) executes on sm_120.
  • Adversarial review caught the false-positive hole; the fix is in this branch with the probe as its regression.
  • Tests/clippy/fmt clean; full compile-only smoketest 85/85 on the current-main merge.

Fixes #76
Fixes #108

…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.
@nihalpasham nihalpasham added bug Something isn't working QoL-improvement Quality-of-life improvement; not a correctness fix codegen Device code-generation pipeline (Rust MIR to IR to PTX) labels Jun 11, 2026
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
nihalpasham merged commit d2a3ce1 into main Jun 11, 2026
24 checks passed
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.
@nihalpasham
nihalpasham deleted the fix/issue76-108-actionable-diagnostics-v2 branch June 12, 2026 10:19
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) QoL-improvement Quality-of-life improvement; not a correctness fix

Projects

None yet

1 participant