Skip to content

fix(mir-lower): legalize address-space 3 slice fat-pointer casts in SSA - #1061

Merged
nihalpasham merged 1 commit into
NVIDIA:mainfrom
uurl:fix/as3-fat-pointer-casts
Aug 20, 2026
Merged

nihalpasham merged 1 commit into
NVIDIA:mainfrom
uurl:fix/as3-fat-pointer-casts

Conversation

@uurl

@uurl uurl commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1060

Summary

Legalize semantic slice fat-pointer casts in SSA, including casts involving shared-memory addrspace(3) pointers, without round-tripping the aggregate through memory.

Instead of treating the fat pointer as raw bytes, the lowering now handles its two semantic components independently:

{ data pointer, metadata }
        |
        +-- extract data pointer
        |       |
        |       +-- addrspacecast when required
        |
        +-- extract metadata unchanged
        |
        +-- reconstruct destination aggregate in SSA

This implements the sound fat-pointer bridge described in #883.

Why

The generic aggregate cast fallback uses an alloca + store + load round-trip.

That is unsafe for aggregates containing shared-memory pointers because the physical representation of an addrspace(3) pointer is target-mode dependent and its raw shared-memory value is not the Rust-visible generic address.

A slice fat pointer does not require byte reinterpretation. Its data pointer can be converted semantically with addrspacecast, while its length metadata can be copied unchanged.

Implementation

emit_pointer_cast now recognizes the narrow LLVM representation used by slice fat pointers:

{ pointer, integer metadata }

When both source and destination match that shape and their metadata types match, the lowering:

  1. extracts the data pointer
  2. extracts the metadata
  3. emits addrspacecast for the data pointer when the address spaces differ
  4. reconstructs the destination aggregate with insertvalue

No temporary memory representation is created.

Other aggregate casts continue to use the existing fallback behavior.

The legalization is intentionally restricted to the slice-shaped { pointer, integer metadata } representation within the semantic pointer-cast path.

It does not extend to:

  • other aggregate shapes
  • trait-object-like { ptr, ptr } aggregates
  • aggregate Transmute
  • cluster-shared addrspace(7)

Tests

Added mir-lower coverage for:

  • shared/generic slice fat-pointer casts
  • generic/generic slice fat-pointer casts
  • metadata preservation
  • SSA reconstruction
  • absence of alloca, store, and load
  • required addrspacecast behavior

Extended shared_ptr_transmute with an end-to-end GPU regression that checks:

  • slice length preservation
  • shared data-pointer identity
  • write-through visibility after the fat-pointer cast

Validation performed:

cargo oxide fmt --check
    PASS

git diff --check
    PASS

cargo test -p mir-lower
    255 unit tests passed
    117 lowering tests passed

cargo clippy -p mir-lower --all-targets -- -D warnings
    PASS

cargo oxide run shared_ptr_transmute
    PASS
    sum=528
    scalar_readback=0xC0DE
    slice_readback=0x1234

cargo oxide sanitize shared_ptr_transmute --tool memcheck
    0 errors

cargo oxide sanitize shared_ptr_transmute --tool racecheck
    0 hazards

Refs #883

@nihalpasham nihalpasham changed the title fix(mir-lower): legalize AS3 slice fat-pointer casts in SSA fix(mir-lower): legalize address-space 3 slice fat-pointer casts in SSA Aug 20, 2026
@uurl
uurl force-pushed the fix/as3-fat-pointer-casts branch from 5e58abd to e30d8c4 Compare August 20, 2026 01:37
@nihalpasham nihalpasham added bug Something isn't working miscompile Produces incorrect PTX/IR (wrong generated code) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) labels Aug 20, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator
  • Verified end to end: mir-lower 255+117 tests green, clippy clean, cargo oxide run shared_ptr_transmute passes on an RTX 5090 (sum=528, scalar_readback=0xC0DE, slice_readback=0x1234), per-example fmt/clippy clean.
  • Reachability holds: Transmute still routes to emit_transmute with its own AS3 rejections, so the { ptr, integer } recognizer only fires for PtrToPtr/coercion/Subtype fat-pointer casts with identical metadata, and AS7 bails out. No way to mis-fire it on a struct-shaped non-fat value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) miscompile Produces incorrect PTX/IR (wrong generated code)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mir-lower: legalize AS3 slice fat-pointer casts in SSA

2 participants