Repository navigation
fix(mir-lower): bridge shared-pointer transmutes through the generic address space - #884
Merged
Merged
Conversation
…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.
…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.
This was referenced Aug 14, 2026
This was referenced Aug 18, 2026
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.
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:
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:This matches what
emit_ptr_to_intalready does forptr::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 } -> u64would 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: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).shared_ptr_transmuteexample: round-trips aSharedArraypointer throughusizeand 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.That last one is the
p3:32mode #879's width heuristic was aiming at, passing here with no width knowledge at lowering time. The emitted PTX shows the realcvta.shared.u64/cvta.to.shared.u64conversions.