Skip to content

perf(llvm): emit proven Rust reference parameter attrs - #1089

Closed
uurl wants to merge 2 commits into
NVIDIA:mainfrom
uurl:perf/llvm-reference-attrs
Closed

uurl wants to merge 2 commits into
NVIDIA:mainfrom
uurl:perf/llvm-reference-attrs

Conversation

@uurl

@uurl uurl commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1088

This PR is stacked on top of #1062; its actual delta is one commit touching 7 files. Once #1062 lands, I’ll rebase this PR onto main so the review diff contains only the LLVM attribute-emission changes.

Summary

Part of #413.

This change emits LLVM noalias / readonly parameter attributes from Rust reference semantics that are proven during MIR import.

#1062 preserves source pointer/reference kind through dialect MIR. This PR builds on that information and captures the additional rustc semantic conditions required to safely emit LLVM parameter attributes.

The policy follows rustc conservatively:

MIR pointer kind Additional condition LLVM attributes
SharedRef pointee is Freeze noalias readonly
UniqueRef pointee is Unpin noalias
RawConst — none
RawMut — none
Erased — none

Design

The semantic decision is made at the MIR import boundary, where the original rustc type is still available.

For each source-level parameter, mir-importer records only the facts that rustc semantics justify.

These facts are then:

  1. stored on MirFuncOp
  2. propagated through MIR lowering
  3. remapped from source arguments to physical LLVM arguments after ABI flattening
  4. serialized mechanically by llvm-export

llvm-export deliberately performs no Rust-semantic inference.

For slices, only the physical pointer component receives the attributes. The length parameter remains unannotated.

Safety / conservative cases

This PR intentionally does not emit attributes for:

  • raw pointers
  • erased/compiler-generated pointers
  • &UnsafeCell<T> and other non-Freeze shared references
  • non-Unpin mutable references
  • DisjointSlice based solely on its lowered representation

This avoids deriving Rust aliasing semantics from LLVM pointer shape or mutability alone.

Validation

Focused tests cover:

  • valid SharedRef and UniqueRef attributes
  • rejection of attributes on raw pointers
  • rejection of invalid readonly combinations
  • rustc Freeze / Unpin behavior
  • source-to-physical LLVM argument remapping
  • slice pointer vs. metadata handling
  • LLVM textual export
  • legacy typed-pointer export
  • rejection of reference attributes on non-pointer LLVM parameters

Full validation:

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
cargo test -p llvm-export --all-targets

cargo oxide build vecadd
cargo oxide build addressof_sharedarray

git diff --check

End-to-end pipeline validation also confirms that the attributes reach generated LLVM IR.

For example, vecadd now lowers to:

define ptx_kernel void @vecadd(
    ptr noalias readonly %v0,
    i64 %v1,
    ptr noalias readonly %v2,
    i64 %v3,
    ptr %v4,
    i64 %v5
)

This verifies that:

  • shared Freeze references receive noalias readonly
  • slice metadata remains unannotated
  • raw/non-proven pointers remain bare
  • the source-to-physical LLVM argument mapping is correct

addressof_sharedarray also confirms that shared-memory/raw pointer parameters do not acquire reference-derived attributes accidentally.

Out of scope

  • dereferenceable
  • DisjointSlice alias inference
  • recursive provenance analysis
  • raw-pointer alias inference

@nihalpasham nihalpasham added soundness-audit Correctness case under active maintainer audit; not cleared for merge yet perf Performance of generated code or of the compiler itself codegen Device code-generation pipeline (Rust MIR to IR to PTX) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) llvm-export Textual LLVM/NVVM IR exporter (llvm-export crate) Depends Blocked on another PR/issue or an upstream dependency labels Aug 20, 2026
@nihalpasham

nihalpasham commented Aug 20, 2026 •

Copy link
Copy Markdown
Collaborator
  • Not a merge call yet, same soundness-audit tag and reason as feat(mir): preserve Rust pointer and reference kind #1062, which this stacks on: wrong noalias miscompiles quietly, so I'm walking the chain from is_freeze/is_unpin to the printed attribute myself before signing off. Blocked on feat(mir): preserve Rust pointer and reference kind #1062 either way.

  • The policy table matches rustc's own (&T + Freeze pointee -> noalias readonly, &mut T + Unpin -> noalias, raw pointers and Erased -> nothing), the proof runs where the real Ty is available, and the MirFuncOp verifier plus the fail-closed argument remap reject anything malformed. Two changes were needed on top, done and held with the feat(mir): preserve Rust pointer and reference kind #1062 fixes.

  • The one smoketest failure is the optimization working, and the test couldn't see it. aligned_field_loads asserts fusion width in PTX; with noalias readonly on the parameter, llc routes the reads through the read-only cache:

    before:  ld.global.v2.b32     {%r8, %r9}, [%rd16];
    after:   ld.global.nc.v2.b32  {%r8, %r9}, [%rd16];   <- __ldg fast path
    

    Fusion held in both directions: align(8) kernels fuse to v2, the natural-align-4 controls stay scalar, and every read additionally became .nc. The shape check now accepts the optional nc. qualifier so it checks fusion width, not the full instruction spelling.

  • The behavior change has to be said where raw launches are documented: overlapping read/write buffers through a raw launch used to be latent UB and is now a real miscompile (loads hoisted or cached across stores). Typed launches already rule it out since writable parameters take &mut DeviceBuffer. Added that to the cuda_launch! module docs and the launching-kernels book page.

  • Spot-checks so far, evidence rather than sign-off: tiled_gemm's PTX keeps strict st.shared -> bar.sync -> ld.shared -> bar.sync -> st ordering, attrs land only on pointer halves (slice lengths, raw pointers, DisjointSlice stay bare), and &UnsafeCell<u32> / &mut PhantomPinned get nothing per the fixture test. With the fixes: 95 workspace suites, 216/216 compile smoketest, GPU execution on tiled_gemm, barrier, and aligned_field_loads, book builds under -W.

@nihalpasham

Copy link
Copy Markdown
Collaborator

Closing this PR because it is explicitly stacked on #1062 and derives noalias / readonly from the pointer-kind model in that branch.

#1062 is now closed because that model is unsound as a proof source: it cannot establish, end to end, that each pointer label came from the exact Rust fact that justifies it. Emitting optimizer attributes from those labels could turn a classification mistake into an LLVM aliasing miscompile.

The replacement will keep pointer construction and alias proof separate. This work can be reconsidered after the new proof exists end to end.

#1088 remains open.

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) Depends Blocked on another PR/issue or an upstream dependency IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) llvm-export Textual LLVM/NVVM IR exporter (llvm-export crate) perf Performance of generated code or of the compiler itself soundness-audit Correctness case under active maintainer audit; not cleared for merge yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Emit LLVM noalias / readonly for Rust references with proven semantics

2 participants