Skip to content

perf(llvm): emit proven Rust reference validity attrs - #1099

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

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

Conversation

@uurl

@uurl uurl commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Closes #1098

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

Summary

Part of #413.

This change extends the Rust-reference parameter attribute pipeline introduced
by #1089 with conservatively proven LLVM nonnull, align, and
dereferenceable attributes.

The policy is intentionally decided at the MIR import boundary, where the
original rustc Ty, layout, and trait information are still available.
Downstream lowering only transports and remaps those facts, and llvm-export
serializes them mechanically.

Semantics

Rust parameter Condition Additional LLVM attributes
&T T: Sized nonnull, align N when N > 1
&mut T T: Sized nonnull, align N when N > 1
&T T: Sized + Freeze, size > 0 dereferenceable(size)
&mut T T: Sized + Unpin, size > 0 dereferenceable(size)
unsized reference — none from this validity policy
raw pointer — none

align 1 is omitted.

Zero-sized pointees never produce dereferenceable(0), so an over-aligned ZST
may carry:

ptr nonnull align 16

without dereferenceable.

Design

The semantic decision remains centralized in mir-importer.

For each source-level argument:

  1. mir-importer checks the original Rust reference type, rustc layout, and
    relevant Freeze / Unpin conditions.
  2. MirFuncOp stores the proven facts by source argument index.
  3. mir-lower remaps source arguments to physical LLVM pointer parameters
    after ABI flattening.
  4. llvm-export validates the mechanical representation and serializes the
    resulting LLVM attributes.

Neither mir-lower nor llvm-export derives Rust semantics from pointer
mutability, address space, LLVM pointer shape, or DisjointSlice.

Conservative cases

This PR deliberately does not emit validity attributes for unsized references.

This preserves the slice alias behavior introduced by #1089 while keeping
validity attributes restricted to sized pointees in this follow-up.

In particular:

  • &UnsafeCell<u32> receives nonnull align 4, but not
    dereferenceable(4)
  • &mut PhantomPinned does not receive dereferenceable
  • over-aligned ZST references receive non-nullness/alignment without
    dereferenceable(0)
  • raw pointers receive no reference-derived validity attributes
  • DisjointSlice remains uninterpreted by the exporter

Validation

Focused coverage includes:

  • valid shared and unique sized references
  • UnsafeCell shared references
  • non-Unpin mutable references
  • over-aligned zero-sized pointees
  • shared and mutable slice behavior inherited from perf(llvm): emit proven Rust reference parameter attrs #1089
  • raw pointers
  • invalid MIR validity markers
  • physical LLVM argument remapping
  • modern opaque-pointer LLVM export
  • legacy typed-pointer LLVM export
  • rejection of validity attributes on non-pointer LLVM arguments

Validation performed:

cargo fmt --all -- --check

cargo test -p dialect-mir \
  test_mir_func_reference_param_attrs_verify

cargo test -p mir-importer \
  reference_param_attrs_follow_rustc_reference_validity_rules

cargo test -p mir-lower \
  reference_param_mapping_targets_only_physical_pointer_components

cargo test -p llvm-export \
  export_reference_parameter_attrs_on_definitions

cargo test -p llvm-export \
  legacy_export_reference_parameter_attrs_follow_typed_pointer

cargo test -p llvm-export \
  export_rejects_reference_attrs_on_non_pointer_parameter

cargo test -p llvm-export \
  export_rejects_invalid_reference_validity_attrs

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 clippy --workspace --all-targets -- -D warnings

cargo oxide build vecadd
cargo oxide build addressof_sharedarray

git diff --check

The final stacked delta against #1089 is one commit touching 7 files.

Out of scope

  • validity attributes for DST / slice references
  • dereferenceable_or_null
  • recursive provenance analysis
  • deriving attributes from DisjointSlice
  • deriving attributes from raw pointers
  • general LLVM-level pointer inference

@uurl
uurl force-pushed the perf/llvm-reference-validity-attrs branch from 341a28b to af66e44 Compare August 20, 2026 12:51
@nihalpasham

Copy link
Copy Markdown
Collaborator

Closing this PR because it is explicitly stacked on #1089, which in turn is stacked on the unsound pointer-kind proof source in #1062.

nonnull, align, and dereferenceable are optimizer-visible claims. They must come from an independent, end-to-end proof of the source reference, pointee layout, and ABI mapping; inheriting #1062 / #1089 does not provide that foundation.

The replacement architecture will first preserve exact source types per operation, construct pointers through one checked policy, and derive validity attributes separately.

#1098 remains open.

@uurl
uurl deleted the perf/llvm-reference-validity-attrs branch August 31, 2026 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Emit LLVM nonnull / align / dereferenceable for proven Rust references

2 participants