Skip to content

feat(mir): preserve Rust pointer and reference kind - #1040

Closed
uurl wants to merge 1 commit into
NVIDIA:mainfrom
uurl:feat/mir-pointer-kind
Closed

uurl wants to merge 1 commit into
NVIDIA:mainfrom
uurl:feat/mir-pointer-kind

Conversation

@uurl

@uurl uurl commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Summary

Preserve Rust pointer/reference kind through MIR import instead of collapsing
references and raw pointers that share the same pointee, mutability, and address
space.

Fixes #413.

This introduces MirPointerKind with the following categories:

  • SharedRef for &T
  • UniqueRef for &mut T
  • RawConst for *const T
  • RawMut for *mut T
  • Erased for compiler-generated/internal addresses

The kind is carried by both MirPtrType and MirSliceType.

What changed

dialect-mir

  • Added MirPointerKind.
  • Added pointer kind to MirPtrType.
  • Added pointer kind to MirSliceType.
  • Added constructors that explicitly preserve source pointer kind.
  • Existing synthetic/internal pointer constructors default to Erased.
  • Added verification that concrete pointer kinds agree with the pointer
    mutability bit.
  • Documented the distinction between mutability and Rust reference uniqueness.

MIR importer

Rust types now map as follows:

Rust type MIR pointer kind
&T SharedRef
&mut T UniqueRef
*const T RawConst
*mut T RawMut

Pointer kind is preserved through relevant importer paths, including:

  • Rvalue::Ref
  • Rvalue::AddressOf
  • projected addresses
  • slices and subslices
  • pointer-like local stores
  • static pointer materialization
  • CUDA address-space alignment

Compiler-generated storage and temporary addresses remain Erased.

Pointer normalization is intentionally conservative. It may restore a concrete
Rust kind at an explicit Rust-typed semantic boundary, but generic coercion does
not silently convert one concrete Rust pointer category into another. For
example, it cannot synthesize:

  • SharedRef -> UniqueRef
  • RawConst -> RawMut
  • raw pointer -> reference

MIR lowering

MirPointerKind is semantic-only and is intentionally erased during MIR-to-LLVM
type conversion.

When a cast changes only MIR pointer kind and both sides lower to the same LLVM
type, lowering forwards the value directly instead of emitting an unnecessary
LLVM cast or memory round-trip.

This PR deliberately does not emit LLVM noalias, readonly,
dereferenceable, or related metadata.

Retag remains a codegen no-op; dynamic Stacked Borrows / Tree Borrows state is
outside the scope of this change.

Tests

Added coverage for:

  • distinguishing references from raw pointers
  • preserving pointer kind in slices
  • pointer-kind/mutability consistency
  • preserving pointer kind during address-space alignment
  • restoring fat-pointer kind at typed boundaries
  • rejecting concrete pointer-kind changes during generic normalization
  • erasing pointer-kind-only coercions without emitting LLVM instructions

Validation performed:

cargo fmt --all -- --check
cargo clippy --workspace --all-targets -- -D warnings
cargo test -p dialect-mir --all-targets
cargo test -p mir-importer --all-targets
cargo test -p mir-lower --all-targets

All checks pass.

@nihalpasham nihalpasham added enhancement New feature or request codegen Device code-generation pipeline (Rust MIR to IR to PTX) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) safety Memory safety, soundness, or undefined behavior labels Aug 19, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

The direction is generally right, but this version breaks the compiler, so it can't merge. This needs a rethink deep enough that a fresh PR is the better route.

  • Where it fails: the branch can't build the most basic example.

    $ cargo oxide build vecadd
    Verification failed for 'vecadd':
    MirStoreOp value type must match pointer element type
      mir.ptr<fp32, ..., kind:UniqueRef>  vs  mir.ptr<fp32, ..., kind:RawMut>
    
  • Why: the new rule says a pointer can never change from one concrete kind to another. But Rust does that constantly. It's called a reborrow:

    let p: *mut f32 = ...;
    let r: &mut f32 = unsafe { &mut *p };   // RawMut -> UniqueRef, and it's legal Rust
    • At that &mut * the kind must change (rustc's own MIR type says so), but pointer_kind_retype_allowed refuses, no cast gets inserted, and the store verifier rejects the module.
    • This pattern is everywhere: DisjointSlice::get_mut, the sysroot's slice::index, split_at_mut. Nearly every kernel hits one of them.
  • Why the tests didn't catch it: all 1400+ unit tests pass, but none of them compiles an example end to end, and CI never ran on the fork branch. Any respin needs vecadd plus one SharedArray example in its validation, not just unit tests.

  • Two more problems to fix in the redo:

    • maybe_ptr_coerce used to bridge any thin-pointer mismatch with a cast. The rewrite only casts when the pointees already match, so cases that worked on main (like shared_alloc's pointer feeding a differently-typed local) now fail downstream.

    • the kind can be laundered. Each hop below is legal on its own:

      SharedRef -> Erased -> UniqueRef
      

      Two normalizations later a shared ref claims to be unique. A future noalias pass keyed on UniqueRef would believe it.

Suggested path: let's agree on the transition table in #413 first, then a new PR. The core question is where kind changes are allowed: explicit Rvalue::Ref / Rvalue::AddressOf boundaries should trust rustc's declared type (reborrow, addr-of), while implicit normalizations stay locked down, and Erased needs a rule that can't launder. That policy deserves thought before code.

Also: drop "Fixes #413" from the description. #413 is a tracking issue; this covers the representation half only and stays open either way.

@nihalpasham

Copy link
Copy Markdown
Collaborator

Closing. Please see above.

@uurl
uurl deleted the feat/mir-pointer-kind branch August 19, 2026 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codegen Device code-generation pipeline (Rust MIR to IR to PTX) enhancement New feature or request IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) safety Memory safety, soundness, or undefined behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preserve Rust reference kind and exclusivity through MIR import and lowering

2 participants