Repository navigation
fix(mir-lower): coerce unsize data pointer to the slice's address space - #350
Conversation
|
Added a minimal standalone repro that doesn't depend on our (admittedly custom) rust-gpu-style shader stack: The key insight while minimizing: the address space has to survive into the importer's pointer typing for the bug to trigger. A raw-pointer round-trip ( static mut TILE: SharedArray<[f32; N], 1> = SharedArray::UNINIT;
// ...threads stage input into TILE[0]...
let row: &[f32; N] = unsafe { &TILE[0] };
let total = sum_slice(row); // fn sum_slice(values: &[f32]) -> f32
So this reproduces with plain |
c056d27 to
b329a31
Compare
Unsizing `&mut [T; N]` -> `&mut [T]` when the array lives in shared memory
(addrspace 3) inserted the `ptr addrspace(3)` data pointer directly into the
canonical slice struct `{ ptr addrspace(0), i64 }`, so the lowered module
failed verification with "Value being inserted / extracted does not match
the type of the indexed aggregate".
`emit_unsize_cast` now compares the source pointer's address space with the
slice's field-0 slot and inserts an `addrspacecast` when they differ (PTX
lowers this to `cvta.shared`). Matching address spaces are unchanged.
The trigger is plain SharedArray usage: indexing a nested
`SharedArray<[T; N], M>` yields a real addrspace(3) `&[T; N]`, and passing
it to anything slice-taking is the unsize (see the shared_slice_unsize
example added in the next commit). The stale scope note in
`emit_pointer_cast` claiming the variant had no trigger is updated
accordingly.
Co-Authored-By: Claude Fable 5 <[email protected]>
Signed-off-by: haixuantao <[email protected]>
Minimal standalone repro for the addrspace coercion in emit_unsize_cast, using only cuda-oxide constructs: indexing a `SharedArray<[f32; N], 1>` yields a real addrspace(3) `&[f32; N]`, and passing it to a `#[device]` fn taking `&[f32]` is the unsize that previously failed module verification. (A raw-pointer round-trip through `as_mut_ptr` erases the address space and does NOT reproduce.) The kernel sums a staged tile through the slice view, so a regression is either the verifier error at build time or a wrong sum at run time. Verified on an RTX 5080 (sm_120): PASS sum=8128. Without the fix the build fails with "Value being inserted / extracted does not match the type of the indexed aggregate". Co-Authored-By: Claude Fable 5 <[email protected]> Signed-off-by: haixuantao <[email protected]>
Signed-off-by: nihalp <[email protected]>
0aaaaf6 to
97f88eb
Compare
|
Thanks for the PR. LGTM.
For example: shared pointer (AS3) -> addrspacecast -> slice pointer (AS0)Reviewer note: Compiler-side lowering was independently verified through A broader reference-exclusivity question is tracked separately in #413 and is not caused by this fix. |
Summary
Fixes shared-memory array unsizing. A shared
&[T; N]carries an address-space-3data pointer, while cuda-oxide's canonical slice stores a generic
address-space-0 pointer. Inserting the shared pointer directly made the LLVM
aggregate type-invalid.
emit_unsize_castnow reads both real pointer address spaces and emits oneaddrspacecastonly when they differ before constructing the ordinary slicefat pointer. This matches the NVPTX shared-to-generic boundary (
cvta.shared).The PR adds a shared-memory regression, a generic-pointer no-cast control, and
a standalone compile/run example.
Maintainer verification
mainat6a92f548; the sole conflict retained all casttests from Extend cast lowering coverage #347 and both unsize tests from this PR.
mir-lowerall-target suite — 168 passed (95 library and 73integration tests).
rustc-codegen-cuda, and example clippy with warningsdenied — passed.
cargo oxide fmt --check, including every standaloneexample — passed.
shared_slice_unsizedevice-pipeline smoke test — 1/1 passed.addrspacecast; LLVM 21 at-O0lowers it to
cvta.shared.u64.PASS shared_slice_unsize: sum=8128 (expected 8128).0 errors.
docs/doctests, CodeQL, naming guard, and status guard.
The broader question of preserving Rust reference kind and exclusivity through
MIR import/lowering is tracked separately in #413. It is not caused by this
address-space fix, and no current miscompile from the erased exclusivity
information has been demonstrated.