Skip to content

fix(mir-importer): compute element addresses through fat-pointer derefs - #165

Merged
nihalpasham merged 1 commit into
mainfrom
fix/issue58-fatptr-mutable-place
Jun 11, 2026
Merged

nihalpasham merged 1 commit into
mainfrom
fix/issue58-fatptr-mutable-place

Conversation

@nihalpasham

Copy link
Copy Markdown
Collaborator

What

Writes through a.get_mut(i) on a &mut [f32; N], and direct (*fat)[i] = x assignments, now compile and hit the original memory.

&mut [T] is a fat pointer: { data ptr, len }

Before: mutable place through a fat-pointer Deref -> loud rejection
        ("cannot compute a mutable in-memory address through
          fat-pointer deref (projection [Deref, Index])")
After:  load the fat value, extract field 0 (the data pointer, which
        addresses the ORIGINAL elements), continue the walk with
        ptr_offset (slices) / array_element_addr (arrays)

The assignment translator also gains the missing (Deref, Index | ConstantIndex) arm, reusing the same walker, so read and write paths agree.

Evidence

  • The original bug class was SILENT write loss, so the review traced the emitted IR end to end: the store hits a GEP off the fat pointer's data pointer reaching the kernel parameter's memory, with no alloca spill anywhere in the chain.
  • Regression example slice_get_mut covers the issue's exact get_mut shape plus index-assignment variants across six kernels; executes on sm_120.
  • Independent adversarial review with revert-proof: reverting reproduces both original errors verbatim.
  • Tests/clippy/fmt clean; compile-only smoketest green.

Fixes #58

Writes through `a.get_mut(i)` on a `&mut [f32; N]` were rejected with
"cannot compute a mutable in-memory address through fat-pointer deref
(projection [Deref, Index(..)])": the inlined `slice::get_mut` body
takes `&mut (*fat)[i]` through a fat `&mut [f32]` local, and the
unified place-address walker refused to walk through slice-shaped
pointees. The direct form `a[i] = v` failed separately because the
2-level assignment match had no `(Deref, Index)` arm.

Teach `translate_place_addr_from_slot` to handle a mid-chain fat deref
the same way the value-space read path already does: load the whole
scalarized (data ptr, len) value from the slot, extract field 0 (the
thin data pointer, which addresses the ORIGINAL elements), and continue
the walk. A following `Index` / `ConstantIndex` over that pointer now
emits `mir.ptr_offset` (the pointee is the element type, not an array),
while array pointees keep the existing `mir.array_element_addr` path.
This is sound for both mutable and shared borrows, so the shared-read
value-copy fallback for this shape disappears as well.

The 2-level assignment match in statement.rs gains a
`(Deref, Index | ConstantIndex)` arm that obtains the element address
from the same walker (now `pub(crate)`) and stores through it, covering
both thin `&mut [T; N]` and fat `&mut [T]` bases.

The new `slice_get_mut` example pins all six shapes (get_mut + map,
get_mut + if let, indexed assignment through thin and fat pointers,
`&mut a[i]`, and a struct-field write through a get_mut element) with
full host-side readback verification.

Fixes #58

Validation:
- cargo test -p mir-importer -p mir-lower: all green
- cargo clippy -p mir-importer --all-targets -- -D warnings: clean
- cargo fmt --check (root) and cargo fmt --all -- --check (example)
- scripts/smoketest.sh --compile-only --skip '^issue': 81/81 pass
- Pre-fix backend reproduces both original errors on the new example
@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 8358556 into main Jun 11, 2026
24 checks passed
EthanAnro pushed a commit to EthanAnro/cuda-oxide that referenced this pull request Jun 11, 2026
Reconcile two independent extensions of the place-address walker in
mir-importer:

- This branch (issue NVIDIA#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 NVIDIA#165, issue NVIDIA#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 deleted the fix/issue58-fatptr-mutable-place 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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mutable array element borrow in kernel

1 participant