Repository navigation
fix(mir-lower): genericize named-space pointer values - #626
Merged
nihalpasham merged 6 commits intoAug 1, 2026
Merged
nihalpasham merged 6 commits into
nihalpasham merged 6 commits into
Conversation
Signed-off-by: nik <[email protected]>
niklebedenko
force-pushed
the
upstreaming/named-space-pointer-representation
branch
from
August 1, 2026 01:36
fe71c19 to
aaf57da
Compare
…to-ptr PointerExposeAddress now yields the CUDA generic address, so the reverse boundary must interpret the integer the same way. A bare inttoptr into a named space reinterprets the generic address as a space-local offset and silently breaks the expose/recover round trip (usize -> *mut SharedArray). emit_int_to_ptr mirrors emit_ptr_to_int: inttoptr to the generic space, then addrspacecast into the destination space. Wired into PointerWithExposedProvenance, emit_pointer_cast's int-to-ptr arm, and emit_transmute's int-to-ptr arms (the shared-width rejection still fires first for transmutes involving shared pointers). The addressof_sharedarray example now stores through a pointer recovered from the exposed integer and observes the write through the original allocation.
…ptors tcgen05 SMEM descriptors encode (start_address >> 4) & 0x3FFF over the raw .shared window offset (PTX ISA), but the examples derived descriptor bases from Rust pointer casts, which yield the CUDA generic address. Those bits are only correct when the undocumented shared-window base happens to be 2^18-aligned. Add the CUDA C++ __cvta_generic_to_shared analog as a first-class intrinsic: a cuda-device stub, an nvvm.cvta_generic_to_shared dialect op (verified: generic/shared MIR pointer operand, u64 result), an exact importer recognizer, and a PTX-free lowering (addrspacecast to addrspace(3) + ptrtoint, which llc selects as cvta.to.shared). gemm_sol, tcgen05, and tcgen05_matmul now derive descriptor bases through it, and the Tcgen05SmemDescriptor doc examples show the same shape. The emitted PTX packs descriptors from the shared-space symbol address directly, spec-exact with no window-alignment dependence.
The shared-memory address-casting test asserted the pre-NVIDIA#626 behavior: a direct 'as u64' on a shared static had to yield the small space-local offset, and the genericizing cvta was documented as a codegen quirk to eliminate. The contract is now the opposite (the nvcc model): both cast routes must agree on the nonzero generic address, and the raw .shared offset is available only through the explicit cvta_generic_to_shared intrinsic, which the test now also executes on GPU (generic 0x..00000400 vs offset 0x400 on sm_120). Also documents why the intrinsic must not gain a crate-root re-export: the importer recognizes the exact rendered def-path, and a root pub use changes rustc's visible path and silently breaks interception.
Collaborator
|
LGTM on the approach — Rust-visible pointer values are generic addresses, never null for a valid allocation). I pushed fixes for 3 gaps:
Verified on hardware:
Merging after #624; closes #625. |
# Conflicts: # crates/mir-importer/src/translator/terminator/mod.rs # crates/rustc-codegen-cuda/examples/addressof_sharedarray/src/main.rs
…all raw-address sites The name now says what the result is: the raw .shared window offset, not a pointer. Renamed across the device stub, the nvvm dialect op, the importer recognizer, the lowering, and all callers. Sweep every example that exposes a shared address as an integer through the intrinsic, not just the first two descriptor sites: - gemm_sol: all 28 per-stage SMEM descriptor bases across its kernels, plus 5 cluster::map_shared_rank addresses feeding mbarrier_arrive_cluster (the mapa result is a raw shared::cluster address; a bare 'as u64' would now genericize it) - gemm_sol_final: 16 SMEM descriptor bases and 4 mapped barrier addresses across both kernels - mcast_barrier_test: 2 mapped barrier addresses The emitted PTX keeps every chain register-pure: mapa results feed mbarrier.arrive.release.cluster directly and descriptor packing derives from shared-symbol movs, with all round-trip casts folded.
nihalpasham
force-pushed
the
upstreaming/named-space-pointer-representation
branch
from
August 1, 2026 12:52
ffafe56 to
32882d2
Compare
nihalpasham
approved these changes
Aug 1, 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 #625.
Summary
CUDA named-space pointers are offsets within a memory space, while Rust
pointer-address operations and aggregate layout require one stable logical
pointer representation. This change casts named-space pointers to CUDA generic
space before exposing them as integers and stores direct shared-pointer enum
payloads in generic pointer slots. A valid shared allocation at local offset
zero is therefore non-null, and enum storage remains 64-bit in both legacy PTX
and modern NVVM modes.
What changed
Central pointer-address representation
emit_ptr_to_intboundary used byPointerExposeAddress,pointer-to-integer semantic casts, and the pointer-to-integer transmute that
implements
ptr::addrin core.ptrtoint. Every nonzero CUDA addressspace first receives
addrspacecastto generic space, so the integercontains both the address and memory-space identity.
ptrtointsites.ldmatrixintentionallyconsumes a native 32-bit shared offset, while enum niche discrimination
reads a carrier that is already stored as a generic pointer; neither is a
Rust pointer-address exposure boundary.
lacks target-mode-specific physical width at that boundary. (Maintainer
follow-up: the reverse integer-to-pointer
as-cast boundary is nowhandled rather than left as a bare
inttoptr; see below.)Target-stable enum storage
build_enum_slot_mapnow distinguishes a direct shared-pointer field froman aggregate that merely contains one. A direct field claims a generic
physical pointer slot, while
field_llvm_typesretains its semantic sharedtype.
helper to cross between semantic and physical address spaces.
payload construction, discriminant inspection, and extraction round-trip
through two explicit address-space casts.
addressof_sharedarrayexample with a noinlineRust function that conditionally constructs a direct shared-pointer enum,
matches its discriminant, extracts the pointer, and compares its address
with the original. The same kernel now observes both
ptr::addr()andis_null().constant, and local pointers, plus the no-op generic-pointer case.
shared-pointer niche carriers, shared-pointer vectors, and unsupported
reverse or aggregate transmutes. The existing shared-pointer error example
now protects the unsupported nested-aggregate boundary instead of expecting
direct pointer fields to fail.
Known separate limitations
arbitrary nested pointer-bearing aggregates would need a recursive,
layout-aware conversion and remains deliberately unsupported.
target-mode dependent and is rejected; the supported niche shape uses a
generic physical carrier with a semantic direct shared-pointer payload.
blocked by the cubin
e_versionvalidation; that fix landed separately infix(finalizer): accept CUDA cubin ELF format versions #622, so the runtime LTOIR route is unblocked on current main.
Maintainer follow-ups on this branch
Four commits stacked during review and landing, so the branch as merged is
the author's fix plus:
d515c07e-- the reverse boundary.usize as *mut SharedArray<T, N>lowered as a bare
inttoptr, reinterpreting the now-generic exposedinteger as a space-local offset.
emit_int_to_ptrmirrors this PR'semit_ptr_to_int(enter named spaces through generic), wired intoPointerWithExposedProvenance, the pointer-cast int arm, and thetransmute int arms. Unit tests cover all four named spaces, and the
addressof_sharedarraykernel now recovers a pointer from its exposedinteger through a noinline function and compares pointer values (a wrong
value can still store to the right slot because hardware masks the
shared-window base, so the value comparison is the real check).
fce395d5+ rename in32882d26--cvta_generic_to_shared_offset, afirst-class intrinsic (device stub,
nvvm.cvta_generic_to_shared_offsetop, exact importer recognizer, PTX-free lowering:
addrspacecasttoaddrspace(3)+ptrtoint). Hardware SMEM descriptors encode(start_address >> 4) & 0x3FFFover the raw.sharedoffset, sodescriptor bases must not be derived from
ptr as u64(a generic address)once this PR lands. All raw-address exposure sites now route through it:
gemm_sol (28 per-stage descriptor bases + 5
cluster::map_shared_rankbarrier addresses feeding
mbarrier_arrive_cluster, whose PTX consumes araw
shared::clusteraddress), gemm_sol_final (16 + 4),tcgen05/tcgen05_matmul (8), and mcast_barrier_test (2). Emitted PTX
verified register-pure for both classes.
c1322d55--compiler_featuresasserted the pre-PR semantics (a directas u64had to yield the small raw offset) and failed at runtime underthe new contract. It now asserts the generic-address contract and
executes the new intrinsic on GPU (generic
0x..00000400vs raw offset0x400).3700dcd5-- merge with main resolving the two textual conflicts withfeat(cuda-device): expose raw shared-array pointer #624 (the exact SharedArray recognizer and the shared example, whose
kernels collided on one output slot; both extensions kept).
Maintainer validation: full executing smoketest 177/177 on an RTX 5090
(sm_120, CUDA 13.3), 1270 tests across the four touched crates, the
reverse-boundary regression red on the branch as submitted and green with
the fix, and per-example
cargo fmt --checkclean.Testing
bead880cd7461c67a14bc69d10fa7def538f3394,cargo oxide run addressof_sharedarray --arch=sm_86failed with cargo exit101 because
SharedPointerPayloadcontained a target-dependent sharedpointer. The earlier address-only form of the same command failed at the
pointer-to-integer transmute boundary.
preserving the original issue-54 result (
21), observingis_null() == falseand a nonzero address, and round-tripping the direct enum payload.cargo oxide emit-ltoir addressof_sharedarray --arch=sm_100passed on theclean branch and produced a 4,900-byte compute_100 LTOIR artifact. The
unoptimized IR records
p3:32, stores the enum as{ i64, ptr }, casts theshared pointer to generic on construction, restores shared space on
extraction, and genericizes again before
ptrtoint.applied transiently,
scripts/smoketest.sh --only '^addressof_sharedarray$' --no-colorpassed1/1 on the RTX 3080 and observed the same address and enum runtime results.
Without that condition, the clean branch reaches host execution and fails
solely with
Finalizer(InvalidCubin). The unrelated edit was reverted beforecommit.
cargo test -p mir-lower --all-targetspassed all 149 unit tests and 76lowering integration tests.
scripts/smoketest.sh --compile-only --no-color --keep-logspassed all178 examples, including real-libNVVM verification lanes and the repurposed
nested shared-pointer expected-failure fixture.
scripts/check-error-example-status.shpassed.cargo clippy --workspace --all-targets -- -D warnings, both changed-exampleclippy checks, workspace and example formatting checks, and
git diff --checkpassed.Checklist
main, or its dependency is explicit.mainand passes on this branch.