Skip to content

fix(mir-lower): bridge shared-pointer transmutes through the generic address space - #884

Merged
nihalpasham merged 2 commits into
mainfrom
fix/874-shared-ptr-transmute
Aug 14, 2026
Merged

nihalpasham merged 2 commits into
mainfrom
fix/874-shared-ptr-transmute

Conversation

@nihalpasham

@nihalpasham nihalpasham commented Aug 14, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #874. Replaces #879 (the review there explains why width threading can't work).

What this fixes

Transmuting a shared-memory pointer used to be a hard error. Now the scalar forms just work:

// all of these compiled to an error before, and work now:
let bits: usize = transmute(shared_ptr);     // shared ptr -> integer
let back: *mut T = transmute(bits);          // integer -> shared ptr
let alias: *mut u32 = transmute(shared_ptr); // shared ptr <-> generic ptr

The how: no looking at width, bridge through generic

A shared pointer's physical size depends on the output mode (8 bytes on the llc path, 4 in modern NVVM's p3:32), and that mode is only decided after lowering runs. So instead of asking "how wide is it?", every transmute converts through the 64-bit generic address space, which is the same on every target:

shared -> int    addrspacecast to generic, then ptrtoint
int -> shared    inttoptr to generic, then addrspacecast
ptr <-> ptr      one addrspacecast

This matches what emit_ptr_to_int already does for ptr::addr(): the Rust-visible integer is always the generic address, never the raw shared offset. Zero new options, zero plumbing; behavior is identical under llc, legacy NVVM, and modern NVVM.

Aggregates stay rejected, on purpose

struct { shared_ptr } -> u64 would round-trip through memory and capture the pointer's raw shared-space bytes. Those bytes are an offset, not an address, and the first shared allocation sits at offset 0:

scalar path:     shared_ptr as usize   -> 0x7f31_4000_0000   generic address, correct
memory path:     transmute(Wrapper(p)) -> 0x0                raw offset, reads as NULL

So the aggregate case keeps a loud, located error that names both types and points at the scalar remedy. One hardening on top: the ptr-to-non-integer-scalar arm used to emit a raw ptrtoint; it now genericizes first, so no arm can leak a raw offset.

Proof

  • cargo test -p mir-lower: green, with four new transmute tests (none branch on backend or dialect mode).
  • New shared_ptr_transmute example: round-trips a SharedArray pointer through usize and through a generic alias, then 32 threads write through the reconstructed pointer and the host checks every element. Each transmute sits in an #[inline(never)] device fn so LLVM can't fold the pairs away.
  • PASS on an RTX 5090 (sm_120) three ways:
cargo oxide run shared_ptr_transmute                              llc path
CUDA_OXIDE_NO_OPT=1 cargo oxide run shared_ptr_transmute          unoptimized
cargo oxide run shared_ptr_transmute --emit-nvvm-ir --arch=sm_120 modern NVVM

That last one is the p3:32 mode #879's width heuristic was aiming at, passing here with no width knowledge at lowering time. The emitted PTX shows the real cvta.shared.u64 / cvta.to.shared.u64 conversions.

…neric address space

A Transmute touching a shared-memory (addrspace(3)) pointer was rejected
wholesale because the pointer's physical width is target-mode dependent
(64-bit PTX/legacy, 32-bit modern NVVM p3:32) and lowering runs before a
data layout is chosen. The scalar forms never needed the physical width:
Rust transmute legality is defined by rustc layout, where every nvptx64
pointer is 8 bytes, and the codebase's own emit_ptr_to_int invariant
already makes a shared pointer's Rust-visible integer form its 64-bit
GENERIC address, never a raw space-local offset.

Narrow the rejection to the transmutes that would actually observe
physical pointer bytes, and let the scalar arms handle the rest through
the generic-address bridge:

- shared ptr -> integer: addrspacecast to generic, then ptrtoint
  (already handled by the early emit_ptr_to_int return; unchanged)
- integer -> shared ptr: inttoptr to generic, then addrspacecast into
  the shared space (emit_int_to_ptr already implemented this; the
  blanket guard just never let it run)
- ptr <-> ptr across address spaces: direct addrspacecast
- ptr -> non-integer scalar: the intermediate ptrtoint now also goes
  through emit_ptr_to_int, so the invariant holds on every arm

Aggregate transmutes containing shared pointers (including transparent
wrappers, which stay llvm.struct at this stage) remain intentionally
unsupported: their memory round-trip would store the pointer's physical
bytes at an unknowable width and leak raw shared-local offsets into
Rust-visible bytes. The rejection now sits on the memory-round-trip
dispatch points and names the types with an actionable diagnostic.

Behavior is identical under both NVVM dialects and the llc path: no
LoweringOptions changes, no width plumbing.

New GPU example shared_ptr_transmute round-trips a shared pointer
through usize and through a generic pointer alias, then writes and reads
shared memory through the reconstructed pointers; verified on sm_120
through the default llc path, CUDA_OXIDE_NO_OPT=1, and the modern NVVM
path (--emit-nvvm-ir --arch=sm_120, p3:32 datalayout). Unit tests cover
usize -> shared, shared <-> generic, and the aggregate rejection.

Closes #874. Supersedes #879.
@nihalpasham nihalpasham added bug Something isn't working codegen Device code-generation pipeline (Rust MIR to IR to PTX) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) labels Aug 14, 2026
…ur transmute helpers

The as-cast form clippy suggests lowers through the pointer-cast path;
Transmute lowering is the path this example exists to regression-test.
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) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Transmute involving a shared-memory pointer is rejected (target-mode-dependent layout)

1 participant