Repository navigation
fix(mir-importer): preserve runtime Index projections in Rvalue::Ref - #121
Merged
Merged
Conversation
`Rvalue::Ref` for places shaped `&(*ptr).field[i]` lowered to a pointer to the array's first slot — the runtime `Index` projection was silently dropped, so every load through the resulting reference read slot 0 regardless of `i`. Two sites cooperated: 1. `Rvalue::Ref` Case 2 walked `place.projection[2..]` with an inline loop that handled only nested `Field` projections; everything else hit `_ => break`, after which the function unconditionally returned the partial field address. 2. `translate_place_addr_from_slot` was already the general-purpose tail walker, but it implemented only `Field` and `ConstantIndex`; runtime `Index` fell through to `_ => return Ok(None)`. The fix: - Extend `translate_place_addr_from_slot` with a runtime-`Index` arm that loads the index local (`translate_place` against the local with empty projection) and emits `MirArrayElementAddrOp`. The helper now takes `body` and `value_map` so it can recurse for index-place translation. - Replace the inline nested-Field loop in `Rvalue::Ref` Case 2 with a single call to the extended helper for `&place.projection[2..]`. The bug surfaced as a kernel miscompile in Impulse2's sum-factorisation port: a closure `|k| node.value() * w[k] + mate.value() * w[k ^ 1]` returned the slot-0 product for both `k = 0` and `k = 1`, which downstream LLVM legitimately folded into a single shuffle. Add `examples/closure_index_miscompile/` (14-kernel bisection) plus a README documenting trigger conditions and root cause. With this patch all 14 kernels report PASS; without it the four kernels that exercise `&(*upvar).0[idx]` through a closure with ≥ 2 convergent shuffles report `diff = 0`. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> Signed-off-by: nik <[email protected]>
This was referenced Jun 7, 2026
Closed
When the projection tail after `&(*ptr).field` contains an element the address walker cannot lower (Deref, Downcast, Subslice, from-end ConstantIndex, or indexing a non-array pointee), translate_place_addr_from_slot returns Ok(None). The Rvalue::Ref Case 2 handler used to fall through and return the partial field address in that situation, i.e. a pointer to the wrong place. That is the same failure mode as the dropped-Index miscompile in issue NVIDIA#120, just one projection kind further out: every access through the borrow reads or writes the field's first bytes regardless of the remaining projections. Turn the fall-through into a hard TranslationErr::unsupported that names the unhandled tail projection and the full place, so the build fails loudly instead of producing silently wrong PTX. Also correct the walker's doc comment: it advertised a Deref arm ("load the pointer; subsequent projections apply to the pointee") that the implementation never had; Deref currently punts via the catch-all Ok(None) arm. Hardening follow-up to PR NVIDIA#121 (issue NVIDIA#120).
The `&raw const/mut (*ptr).field...` handler had its own copy of the pre-NVIDIA#121 inline tail loop: it recognised only nested `Field` projections and silently `break`'d on anything else, then returned the partial field address. For places like `&raw mut (*p).0[i]` the runtime `Index` was dropped and the resulting raw pointer addressed element 0 -- the identical wrong-prefix-address bug that issue NVIDIA#120 reported for `Rvalue::Ref`, just on the raw-pointer path. Replace the loop with a call to translate_place_addr_from_slot over the projection tail (mutability taken from RawPtrKind::Mut), which gives raw pointers the runtime-Index / ConstantIndex support added in PR NVIDIA#121 for free. When the walker cannot lower the tail, fail loudly with TranslationErr::unsupported naming the tail projection, mirroring the Rvalue::Ref policy, instead of emitting a wrong address. Hardening follow-up to PR NVIDIA#121 (issue NVIDIA#120).
Rvalue::Ref previously dispatched across four hand-rolled cases (bare Deref reborrow, Deref+Field prefix, bare local, slot walk) and Rvalue::AddressOf duplicated two of them. Each copy had its own subset of supported projections, which is how the issue NVIDIA#120 family of wrong-address bugs kept reappearing: every duplicated walker was one missing arm away from handing out a prefix address. Both rvalues now go through a single entry, translate_place_address, which starts at the base local's alloca slot and walks the FULL projection list via translate_place_addr_from_slot: - New Deref arm: when the walked place holds a thin pointer, emit one mir.load of the current address (the loaded pointer IS the pointee's address) and continue into the pointee. This subsumes the old `&(*ptr)` and `&(*ptr).field` special cases and additionally supports shapes like `&h.0[k]` (projection [Field, Deref, Index]) that no previous case could address. - ZST pointees skip the load (replicates translate_place's SharedArray exception: a pointer to a ZST is the ZST place's runtime representation, there is nothing to load). - Fat (slice-shaped) pointees are never walked through: slices scalarize to (ptr, len), so a single load treated as a thin address would be a silent miscompile. A trailing `&*s` reborrow still loads the fat value whole (that IS the borrow result, and matches the old reborrow case); mid-chain fat deref hard-errors for mutable borrows and punts to the value fallback for shared ones. - Downcast is an explicit, documented unsupported arm: enum payload addressing needs the variant/niche layout machinery tracked in issues NVIDIA#131/NVIDIA#146, and this arm is the designed extension point. Fallback policy is now uniform and mutability-aware: when no address can be computed, shared borrows fall back to value + mir.ref (reads through a copy are sound); mutable borrows and `&raw mut` hard-error, because writes through a copy are silently lost. Bare slot-less (ZST) locals keep the synthesised-pointer fallback for both mutabilities since a ZST has no bytes for a write to lose. AddressOf also gains the bare-local slot path (`addr_of\!(local)` now returns the local's slot instead of a copy), matching Ref. Hardening follow-up to PR NVIDIA#121 (issue NVIDIA#120).
Extend the PR NVIDIA#121 example with six regression kernels covering the borrow / raw-pointer address shapes the unified walker must lower: - test_mut_ref_writethrough: `&mut pair.0[k]` write-through; both lanes start equal, +5.0 is stored through the borrow, the ORIGINAL local is read back. Distinguishes correct (diff 5), dropped-index (diff -5) and value-copy (diff 0) lowerings. - test_constant_index_tail: `&(*pr).0[1]` ConstantIndex tail. - test_raw_addr_of_const: `core::ptr::addr_of\!(pr.0[k])` raw reads through the Rvalue::AddressOf path. - test_raw_addr_of_mut_writethrough: `addr_of_mut\!(acc[k])` raw write with original-local readback. - test_inline_never_node_fn: the exact issue NVIDIA#120 MIR shape, an #[inline(never)] `fn node(p: &Pair, k: usize) -> &f32 { &p.0[k] }` whose body is `_0 = &(((*_1).0)[_2])`. - test_holder_deref_tail: `Holder<'a>(&'a [f32; 2])` so `&hr.0[k]` carries a Deref INSIDE the projection tail ([Deref, Field(0), Deref, Index(k)]), exercising the walker's new Deref arm. The harness now tracks per-kernel failures, prints a final SUCCESS marker when every kernel passes, and exits non-zero on any wrong diff so CI/smoketest can gate on it. Also list the example in the rustc-codegen-cuda README examples table and document the new kernels (and the loud-failure policy) in the example README. Companion to the PR NVIDIA#121 hardening commits (issue NVIDIA#120).
Collaborator
|
Thanks for the PR. Added a few things on top:
|
nihalpasham
added a commit
to advpropsys/cuda-oxide
that referenced
this pull request
Jun 10, 2026
…/binaryop-cmp main advanced while this branch was in flight; clean auto-merge (the new address-walker work in mir-importer rvalue.rs touches different regions than the Cmp/enum-tag changes). Re-ran mir-importer / mir-lower / dialect-mir suites green and re-verified the ord_cmp and closure_index_miscompile examples compile at sm_90.
nihalpasham
added a commit
to advpropsys/cuda-oxide
that referenced
this pull request
Jun 10, 2026
…0.2.1 The example landed on main (PR NVIDIA#121) with a lock generated before the v0.2.1 workspace bump; building it locally regenerates the lock. Same regeneration already applied to ord_cmp in the merge commit.
meefs
pushed a commit
to meefs/cuda-oxide
that referenced
this pull request
Sep 30, 2026
Signed-off-by: Melih Elibol <[email protected]>
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.
Fixes #120: a MIR importer lowering bug where
Rvalue::Refcould drop runtimeIndexprojections in places shaped like&(*ptr).field[i]. The lowered reference pointed at the containing field instead of the indexed element, so kernels using closure-captured or struct-contained indexed data could silently read the wrong value.The fix:
Rvalue::Reflowering path to preserve runtimeIndexprojections through the tail of the projected place.closure_index_miscompile, a reduced multi-kernel example that bisects the affected shape.The example is larger than ideal because the failure depends on closure/struct projection shape, but it is self-contained and uses only generic pair-wrapper kernels.