Skip to content

fix(mir-importer): load DisjointSlice through &self before extracting len - #392

Merged
nihalpasham merged 5 commits into
NVIDIA:mainfrom
SomeB1oody:fix/disjoint-slice-len-through-ref
Jul 23, 2026
Merged

nihalpasham merged 5 commits into
NVIDIA:mainfrom
SomeB1oody:fix/disjoint-slice-len-through-ref

Conversation

@SomeB1oody

Copy link
Copy Markdown
Contributor

Summary

Fixes #343. DisjointSlice::len() inside a kernel crashed device codegen with MirExtractFieldOp operand must be tuple, slice, struct, array, or scalar (newtype). DisjointSlice::len is intercepted by the mir-importer as an intrinsic (emit_len). Because len(&self) receives the slice behind a reference, the translated operand is a thin mir.ptr<mir.disjoint_slice<T>>, not the fat (ptr, len) value. emit_len fed that pointer straight into mir.extract_field, which only accepts the fat value, so device codegen died in dialect verification. The bug only happens when MIR inliner makes the call intact (len() is #[inline] and normally inlined into the kernel).

Changes

  • mir-importer/…/intrinsics/indexing.rs: add resolve_disjoint_slice_value, which parses DisjointSlice to the fat (ptr, len) value. A pointer operand carries the fat pointee type to load. A fat value passes through unchanged. Use it in both emit_len and emit_get_thread_local. An unexpected operand type reports a TypeError.
  • mir-importer/…/values.rs: make the existing insert_at helper pub(crate) so the new helper can reuse it.
  • crates/rustc-codegen-cuda/examples/disjoint_slice_len: every in-bounds thread writes len() + is_empty() as u32 to its slot. The host asserts all 256 elements equal 256 (compilation check and correctness check).
  • scripts/smoketest.sh: add NOINLINE_MIR_EXAMPLES list pins RUSTFLAGS=-Zinline-mir=no for disjoint_slice_len.

Testing

On an RTX 5090, CUDA 13, pinned nightly-2026-04-03, error in #343 is reproduced under RUSTFLAGS="-Zinline-mir=no". Same command compiles and runs after the fix.

  • cargo oxide run <example> passes
  • cargo test --workspace passes
  • New example added (if applicable)

Checklist

  • All commits signed off (git commit -s)
  • SPDX headers on new source files

@SomeB1oody
SomeB1oody force-pushed the fix/disjoint-slice-len-through-ref branch from 6b9a032 to 508a390 Compare July 20, 2026 22:00
@nihalpasham nihalpasham added bug Something isn't working codegen Device code-generation pipeline (Rust MIR to IR to PTX) examples Example kernels and host harnesses under crates/rustc-codegen-cuda/examples IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) maintainer-owned tests Adds or improves test coverage labels Jul 22, 2026
SomeB1oody and others added 3 commits July 22, 2026 13:08
…ting `len`

Fixes NVIDIA#343

Problem:
`DisjointSlice::len` is intercepted by the mir-importer as an intrinsic (`emit_len`). Because `len(&self)` receives the slice behind a reference, the translated operand is a thin `mir.ptr<mir.disjoint_slice<T>>`, not the fat `(ptr, len)` value. `emit_len` fed that pointer straight into `mir.extract_field`, which only accepts the fat value, so device codegen died in dialect verification

It only happens when inline is disabled. The default release pipeline inlines `len()` into the kernel

Fix:
Add `resolve_disjoint_slice_value`, which parses `DisjointSlice` to the fat `(ptr, len)` value. A pointer operand carries the fat pointee type to load. A fat value passes through unchanged.
Add `crates/rustc-codegen-cuda/examples/disjoint_slice_len` for regression test. It runs with inline disabled.

Signed-off-by: SomeB1oody <[email protected]>
Keep the repair at the len interceptor boundary: require exactly the fully monomorphized &DisjointSlice receiver shape, emit one typed load, and reject direct, double-pointer, and ordinary-slice near misses. Leave the unrelated get_thread_local path and private values helper unchanged.

Strengthen the GPU regression with a non-block-multiple length, refresh its lockfile against current main, and add exact operation-shape tests.

Signed-off-by: nihalp <[email protected]>
Append the forced no-inline flag after inherited plain or encoded Cargo flags, then require the generated LLVM to load the DisjointSlice receiver before extracting its length. This prevents conflicting environment flags from turning the regression into a false pass.

Signed-off-by: nihalp <[email protected]>
@nihalpasham
nihalpasham force-pushed the fix/disjoint-slice-len-through-ref branch from 508a390 to bd1d68e Compare July 22, 2026 08:10
Restrict the no-inline regression assertion to the write_len kernel and require one non-empty load/extract SSA def-use pair. Unrelated DisjointSlice helpers elsewhere in the module can no longer make the gate pass.

Signed-off-by: nihalp <[email protected]>
Treat CARGO_ENCODED_RUSTFLAGS presence, rather than non-emptiness, as Cargo's selected flag source. An explicitly empty encoded value now receives the forced no-inline flag instead of shadowing the plain RUSTFLAGS update.

Signed-off-by: nihalp <[email protected]>
@nihalpasham

nihalpasham commented Jul 23, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for the PR.

Loading DisjointSlice through &self before extracting len is the right fix for the #343 crash.

I layered four maintainer commits on top:

  • Constrained the receiver handling to exactly one pointer around DisjointSlice, so any other shape gets a clear diagnosed type error instead of a verifier crash.
  • Made the regression unconditional: the example always builds with -Zinline-mir=no, so the un-inlined len() path is exercised on every smoketest run, not just dev builds.
  • Scoped the new .ll shape gate to this example only, so it can't affect other examples.
  • Preserved empty encoded rustflags when appending the pin, so inherited compiler flags can't silently strip it and produce a false pass.

Validated against current main: 805 importer tests, the example on an RTX 5090 (257 elements, deliberately not a block-size multiple), and the shape gate confirming the single load-then-extract sequence. Closes #343.

@nihalpasham
nihalpasham merged commit 5ca9106 into NVIDIA:main Jul 23, 2026
33 checks passed
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) examples Example kernels and host harnesses under crates/rustc-codegen-cuda/examples IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) maintainer-owned tests Adds or improves test coverage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash when compiling DisjointSlice::len

2 participants