Repository navigation
Description
Summary
cuda-oxide currently represents Rust references and raw pointers identically once their pointee type, source mutability, and address space match:
&mut T -> mir.ptr<T, mutable: true, ...>
*mut T -> mir.ptr<T, mutable: true, ...>
&T -> mir.ptr<T, mutable: false, ...>
*const T -> mir.ptr<T, mutable: false, ...>
Both RigidTy::Ref and RigidTy::RawPtr use the same pointer translation. MirPtrType records the pointee, a mutability bit, and the address space, but not whether the value originated as a raw pointer, shared reference, or unique reference.
The mutability bit cannot by itself mean “exclusive”: Rust permits aliasing *mut T pointers, while &mut T carries unique-borrow requirements. MIR Retag statements are also currently skipped, and MIR-to-LLVM pointer conversion retains only the address space.
Current impact
We do not currently have a demonstrated miscompile from this information loss.
The LLVM exporter intentionally does not emit Rust-derived noalias, readonly, dereferenceable, or related pointer attributes. Current lowering also does not use MirPtrType::is_mutable as proof of exclusivity. LLVM therefore handles these pointers conservatively today.
Dropping exclusivity information is not itself incorrect for valid programs while every downstream pass remains conservative. The gap becomes actionable if we later:
- emit
noaliasor other Rust-derived LLVM attributes; - introduce alias-aware optimizations;
- verify reference use in the MIR dialect;
- rely on ordinary Rust reference rules when lowering GPU-specific APIs.
Motivating example from PR #350
PR #350 contains this shared-memory access:
static mut TILE: SharedArray<[f32; N], 1> = SharedArray::UNINIT;
let i = thread::index_1d().get();
if i < N {
unsafe { TILE[0][i] = input[i] };
}In simple terms, the fully monomorphized MIR first performs the outer mutable indexing:
let row: &mut [f32; N] =
IndexMut::index_mut(&mut TILE, 0);
row[i] = input[i];Every GPU thread requests mutable access to outer index 0, producing overlapping &mut [f32; N] references. Each thread then selects a different inner element i, so the final physical stores do not overlap.
This distinction matters because SharedArray::index_mut requires that no other thread concurrently access the same index. Under that documented contract, the outer access to index 0 is the relevant operation even though the final inner stores are disjoint.
The example runs correctly today, and Compute Sanitizer racecheck reports no hardware race, because the lowered code performs disjoint stores and does not retain Rust reference exclusivity. That makes it a useful concrete example of the semantic information currently lost.
PR #350 did not create this gap. Its shared-to-generic address-space unsizing fix is independent and merely exposed a clear example.
Desired outcome
Preserve enough information in the MIR dialect to distinguish at least:
raw const pointer
raw mutable pointer
shared reference
mutable/unique reference
We do not necessarily need to implement the complete Rust aliasing model or make Retag a runtime operation. We should first define which reference guarantees the MIR dialect promises to retain and where they may be intentionally erased.
At minimum:
- Document that
MirPtrType::is_mutableis not proof of uniqueness. - Do not derive
noaliasor equivalent assumptions fromis_mutable. - Decide how reference kind is propagated through projections, reborrows, casts, slices, and calls.
- Decide how GPU-specific abstractions such as
SharedArrayinteract with ordinary Rust reference rules. - Audit
Retaghandling and document whether any of its information is needed in the compiler representation. - Resolve this design before introducing Rust-derived alias metadata or alias-based optimizations.
Acceptance criteria
- The intended reference/provenance model of the MIR dialect is documented.
- Tests show how
&mut Tand*mut Tare represented and which guarantees survive lowering. - No pass treats pointer mutability as proof of uniqueness.
- The
SharedArray<[T; N], 1>example above has a documented interpretation. - Any future
noaliasor related LLVM metadata is based on preserved, auditable reference information.
Activity
perf(mir-importer): materialize a bare array constant as one immutabl…
vyncint commentedon Aug 11, 2026
Measured the prize, since the issue says the gap "becomes actionable if we later emit
noaliasor other Rust-derived LLVM attributes" but not what it is worth. On an A10G (sm_86, driver 595.71.05), it is worth a lot more than I expected, and the blocked pattern is an ordinary one.The pattern that pays for the missing information
A field-wise copy — the way you would naturally write it:
emits four narrow loads and four narrow stores, even though the element promises 16-byte alignment and #746/#777/#795 stamp that alignment onto the addresses. The loads cannot be grouped, because moving the load of
.babove the store too.aneeds a proof thatdstdoes not overlapsrc, and there is none.Hand-patching the IR isolates it
Same module, same
opt -O2andllc -mcpu=sm_86, one edit —noaliason the two pointer parameters and nothing else:4 x ld.global.b324 x st.global.b32+ noaliasby hand1 x ld.global.nc.v4.b321 x st.global.v4.b32Two wins from the one attribute: 128-bit vectorization, and the read-only
.ncpath, which LLVM will only take when it can prove nothing writes the memory during the kernel.What it costs today
Restructuring the source to
let s = src[i];before the four field stores lets the loads group, which reachesld.global.v4(but never.nc— zero occurrences anywhere in the module). That workaround is a clean A/B for the vectorization half, 1M threads, 30 timed launches after 5 warmup:v4v4Streaming gains least because the same bytes still reach memory; the cache-resident kernel is bound by access issue, which is where collapsing eight instructions into two shows up.
noaliasshould beat the grouped column too, since it adds.ncon top, though I have not timed that half — producing it needs the attribute this issue is about.Not proposing a patch
I am not sending one, because the summary here is right about why:
&mut Tand*mut Tboth arrive asmir.ptr<T, mutable: true>, so there is nothing to keynoaliasoff that would not also mark a legally-aliasing raw pointer. Emitting it from the current type information would be unsound rather than merely optimistic. Filing this as evidence for sizing the reference-kind work, not as a request to shortcut it.Two smaller notes from the same probing session, in case they are useful:
repr(C, align(16))element gives oneld.global.v4.b32/st.global.v4.b32, so no separate v4 work is needed.align 4) control stays at four narrow accesses in every variant, as it must.uurl commentedon Aug 20, 2026
I reworked the transition policy around one distinction: semantic establishment vs. representation normalization.
Proposed policy:
Rvalue::RefSharedRef/UniqueRefRvalue::AddressOfRawConst/RawMutErasedErasedErasedThe key rule is that a concrete pointer/reference kind can only be established at a Rust-typed semantic boundary.
This prevents laundering such as:
while still allowing legitimate reborrows such as:
at
Rvalue::Ref, where rustc's declared result type is authoritative.For compiler-recognized APIs such as
SharedArray::as_ptr,as_mut_ptr, andas_raw_mut_ptr, the intrinsic expansion now uses the rustc-declared result type directly instead of relying on local storage to recover kind.The implementation is in #1062.
MirPointerKindremains semantic-only during lowering and does not introducenoalias,readonly, or other Rust-derived LLVM alias metadata.Retagremains a codegen no-op.