Skip to content

feat(mir-importer): accept provably no-op drop glue; for-loops over arrays - #167

Merged
nihalpasham merged 2 commits into
mainfrom
fix/issue138-noop-drop-glue-v2
Jun 11, 2026
Merged

nihalpasham merged 2 commits into
mainfrom
fix/issue138-noop-drop-glue-v2

Conversation

@nihalpasham

Copy link
Copy Markdown
Collaborator

What

for x in arr over [T; N] now compiles for element types whose drop does no work.

for x in arr      desugars to     core::array::IntoIter (has Drop\!)
                                  -> MIR emits a Drop terminator
Before: translate_drop rejects EVERY Drop terminator
After:  Drop whose monomorphized glue provably does nothing lowers to a
        plain branch; effectful destructors still fail loudly

The decision is NOT needs_drop (which is true for IntoIter<u32, 4> even though its glue is empty); a small analysis walks the monomorphized drop-glue MIR and accepts only locals-only writes, empty shim calls, and const-folded branches. The reviewer probed this adversarially: an empty impl Drop is accepted, a raw-pointer-writing destructor is rejected in two shapes (per-element drop, and an IntoIter-only drop forcing the nested glue chain).

Making the loop machinery actually run required three more arms on the same path: deep projection-chain assignments ((*iter).alive.start = v), the assert_inhabited intrinsic (from MaybeUninit::assume_init_read), and fat references to slice-tailed unsized structs (the iterator's &mut PolymorphicIter<[MaybeUninit<T>]> is a (ptr, len) pair). This branch includes a reconciling merge with #165's fat-pointer mutable-place work: one unified walker carries the union of both capabilities (verified by re-running BOTH bite-proofs on the merged code and re-checking #165's store-chain IR guarantee).

Evidence

  • Example array_for_loop ([u32; 4] and [Point; 4] sums, host-verified) passes on sm_120; slice_get_mut (fix(mir-importer): compute element addresses through fat-pointer derefs #165's canary) also passes on the merged code.
  • error_drop_glue still fails: its destructor writes through a raw pointer, exactly what the analysis must reject. STATUS.md row narrowed to "(effectful)" with an explanation.
  • Revert-proof reproduces the issue's original drop of std::array::IntoIter ... not supported error.
  • Tests 70/70 across importer+lower; clippy/fmt clean; compile-only smoketest green.

Fixes #138

…e and the IntoIter machinery

`for x in arr` over a by-value `[T; N]` never compiled on the device:
the loop's `core::array::IntoIter<T, N>` has an `impl Drop`, and
`translate_drop` rejected every MIR `Drop` terminator. Fixing the drop
also surfaced three more gaps on the same path, all inside the
monomorphized `PolymorphicIter<[MaybeUninit<T>]>::next` that backs the
loop. All four are fixed:

- Drop terminators: new `drop_glue` analysis walks the monomorphized
  drop-glue MIR and proves it does nothing observable (only writes to
  its own locals, calls that resolve to empty shims, branches folded by
  constant or UbChecks discriminants). Provably no-op drops lower to a
  plain branch; genuinely effectful destructors keep the loud error,
  so the no-silent-miscompile guarantee from PR #27 stands
  (error_drop_glue still fails as required).

- Deeply projected assignments: `(*iter).alive.start = v` is a
  3-projection destination. statement.rs now walks any 3+ projection
  chain to a destination address with the same place-address walker
  that `Rvalue::Ref` uses, then stores through it, instead of
  enumerating projection combinations by hand.

- `assert_inhabited::<T>()` (planted by `MaybeUninit::assume_init_read`):
  lowers to a unit no-op for inhabited `T` (decided from the layout's
  `VariantsShape`) and to `unreachable` for uninhabited `T`.

- Fat references to slice-tailed structs:
  `&mut PolymorphicIter<[MaybeUninit<T>]>` is a fat (ptr, len) pair.
  It is now modelled as `MirSliceType` with the struct as element type,
  so function-boundary flattening and `PtrMetadata` reuse the existing
  slice machinery. The place-address walker continues through fat
  derefs by extracting the data pointer; borrows of the unsized tail
  produce a real (tail ptr, len) pair with the carried-over metadata;
  `(*slice)[i]` gets element-size pointer arithmetic; and the unsize
  cast `&S<[T; N]> -> &S<[T]>` materializes the element count from the
  struct's trailing array.

The new `array_for_loop` example host-verifies both issue shapes: a
`for` loop over `[u32; 4]` and over an array of Copy structs.

Fixes #138
Reconcile two independent extensions of the place-address walker in
mir-importer:

- This branch (issue #138) taught the walker to continue through
  fat-pointer derefs for MirSliceType (tail borrows of slice-tailed
  unsized structs, deep projection-chain assignments, indexed element
  addresses), but punted on MirDisjointSliceType.
- origin/main (PR #165, issue #58) taught the same walker to compute
  mutable element addresses through fat derefs for BOTH MirSliceType
  and MirDisjointSliceType, with a (Deref, Index) assignment arm in
  statement translation.

Unified resolution, one mechanism with the union of capabilities:

- Fat-pointee detection uses main's broader fat_elem_ty (slice AND
  disjoint slice), feeding this branch's continuation machinery
  (load pair, extract data pointer, tail-borrow slice construction,
  next-projection dispatch). DisjointSlice no longer punts mid-chain.
- Index / ConstantIndex arms keep the emit_indexed_element_addr
  helper, with main's PointeeKind::Direct naming (array GEP vs plain
  ptr_offset off a slice data pointer).
- statement.rs: main's (Deref, Index | ConstantIndex) arm and this
  branch's generic 3+ projection fallback now share one
  store_through_place_address helper (walk the chain mutably, store
  through the address; punts are loud errors, never value copies).

Validated: mir-importer + mir-lower tests, clippy -D warnings, fmt,
array_for_loop + slice_get_mut compile with stores through GEPs off
the fat data pointer (no spilled copy), error_drop_glue still fails
with the destructor diagnostic, compile-only smoketest 84/84.
@nihalpasham nihalpasham added bug Something isn't working codegen Device code-generation pipeline (Rust MIR to IR to PTX) labels Jun 11, 2026
@nihalpasham
nihalpasham merged commit dd85242 into main Jun 11, 2026
24 checks passed
nihalpasham added a commit that referenced this pull request Jun 11, 2026
# Conflicts:
#	crates/rustc-codegen-cuda/STATUS.md
@nihalpasham
nihalpasham deleted the fix/issue138-noop-drop-glue-v2 branch June 12, 2026 10:19
meefs pushed a commit to meefs/cuda-oxide that referenced this pull request Sep 30, 2026
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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

device codegen fails if you iterate over an array.iter()

1 participant