Skip to content

Fix enum constant pointer provenance during MIR import - #595

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
uurl:fix/mir-importer-enum-constant-provenance
Jul 31, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
uurl:fix/mir-importer-enum-constant-provenance

Conversation

@uurl

@uurl uurl commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Closes #594

Description

Summary

Preserve rustc pointer provenance while importing enum constants.

This changes enum constant translation to retain the enclosing allocation and its relocation map instead of decoding the enum exclusively from raw bytes.

Problem

Pointer-bearing constants are represented by rustc as allocation bytes plus relocation metadata. The bytes beneath a relocation contain only the pointer addend; they do not contain a complete exposed address.

The previous enum constant path did not retain this provenance information. As a result, it could not safely:

  • distinguish a relocated niche pointer from an all-zero None carrier;
  • reconstruct a direct pointer payload;
  • preserve an interior byte offset into a device static.

The existing negative example therefore rejected every enum constant containing pointer relocations.

Implementation

This PR:

  • retains the outer allocation when translating enum constants;
  • adds allocation-aware enum variant decoding;
  • detects pointer relocations overlapping a niche carrier;
  • treats one exact, full-width relocation as the untagged pointer variant;
  • reconstructs direct thin-reference fields through the existing static-global pointer path;
  • preserves non-zero relocation addends using checked u64 conversion;
  • audits enum relocations after translating active variant fields;
  • rejects partial, ambiguous, nested, or otherwise unconsumed relocations;
  • adds unit coverage for relocation-range overlap detection.

Runtime regression

The former error_enum_constant_provenance fixture is converted into the successful enum_constant_provenance runtime example.

The example validates:

  • a niche-encoded Option<&T> pointing at an interior device-static element;
  • the None niche variant;
  • a direct-tagged empty variant;
  • a direct-tagged variant with a reference payload.

Expected output:

enum_constant_provenance: PASS

Unsupported cases

This PR intentionally does not add support for:

  • anonymous promoted allocations;
  • pointer relocations nested inside aggregate enum fields;
  • pointer/integer overlapping enum storage;
  • address-space-3 pointer layouts;
  • fat pointers with provenance.

These cases continue to fail closed.

Validation

The following checks pass:

cargo fmt --all -- --check
git diff --check
cargo check -p mir-importer --all-targets
cargo test -p mir-importer
cargo test -p mir-lower
cargo clippy -p mir-importer --all-targets -- -D warnings
cargo clippy --manifest-path crates/rustc-codegen-cuda/examples/enum_constant_provenance/Cargo.toml --all-targets -- -D warnings
scripts/check-error-example-status.sh
scripts/smoketest.sh --compile-only -o '^enum_constant_provenance$'
cargo oxide run enum_constant_provenance

Results:

  • mir-importer: 892 tests passed;
  • mir-lower: 145 unit tests and 76 integration tests passed;
  • libNVVM verification and compilation passed;
  • the runtime regression passed on an NVIDIA GeForce RTX 5060 Laptop GPU.

@nihalpasham nihalpasham added bug Something isn't working enhancement New feature or request codegen Device code-generation pipeline (Rust MIR to IR to PTX) labels Jul 31, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

LGTM as-is.

Verified on hardware:

  • enum_constant_provenance prints PASS on GPU - the niche relocation selects Some, and the addend into the static array reads the right element.
  • error_enum_pointer_overlap and error_enum_shared_pointer_layout still fail closed;
  • 892 mir-importer tests pass.

Merging;

closes #594.

@nihalpasham
nihalpasham merged commit 600d61a into NVIDIA:main Jul 31, 2026
33 checks passed
@uurl
uurl deleted the fix/mir-importer-enum-constant-provenance branch August 1, 2026 18:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working codegen Device code-generation pipeline (Rust MIR to IR to PTX) enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support pointer provenance in enum constants

2 participants