Skip to content

fix(mir-importer): materialize relocation-free pointer/integer unions - #1059

Merged
nihalpasham merged 1 commit into
NVIDIA:mainfrom
uurl:fix/mir-importer-relocation-free-pointer-int-unions
Aug 20, 2026
Merged

nihalpasham merged 1 commit into
NVIDIA:mainfrom
uurl:fix/mir-importer-relocation-free-pointer-int-unions

Conversation

@uurl

@uurl uurl commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1058

Summary

Allow a narrow class of pointer/integer union constants to use the existing byte-image materialization path when their evaluated allocation contains no relocation provenance.

Previously, classify_union_constant_storage rejected all pointer/integer overlap because a byte image cannot represent both arbitrary integer bits and pointer provenance.

That remains correct for relocation-bearing constants. However, when the evaluated union storage contains no relocations—for example, when initialized through a full-width integer alternative—rustc's evaluated byte image is already the complete storage truth.

What changed

translate_union_constant_from_alloc now recognizes relocation-free mixed pointer/integer unions before invoking the provenance-aware classifier.

The byte-image exception is intentionally limited to unions where:

  • storage size is exactly one target pointer word,
  • ABI alignment is exactly one target pointer word,
  • non-ZST pointer alternatives are thin pointers,
  • pointer alternatives use the same address space,
  • integer alternatives are exactly pointer-width, and
  • no relocation overlaps the union storage.

The existing classify_union_constant_storage behavior is unchanged.

This is intentional: relocation-bearing pointer/integer unions and the device-static union validation path continue to use the existing strict provenance rules.

Still rejected

This change does not admit:

  • relocation-bearing pointer/integer unions,
  • fat pointers,
  • nested pointer aggregates,
  • partial-width integer alternatives,
  • mixed pointer address spaces,
  • padded or over-aligned pointer unions, or
  • mixed pointer/integer device-global union initializers.

Thin-pointer-only unions carrying relocations continue to use the existing typed pointer carrier.

Tests

Extended array_constants with direct and runtime-indexed [U; N] pointer/integer union constants initialized through their integer alternatives.

The LLVM code-shape regression verifies that these constants:

  • materialize through an [8 x i8] byte image on the current 64-bit target, and
  • do not reconstruct a pointer using inttoptr.

Existing pointer-only union checks continue to verify the provenance-preserving representation.

Validated locally with:

cargo fmt --all -- --check
cargo test -p mir-importer --lib
cargo clippy -p mir-importer --all-targets -- -D warnings
cargo oxide run array_constants
CUDA_OXIDE_NO_OPT=1 cargo oxide run array_constants
scripts/smoketest.sh --only '^array_constants$' --no-color --keep-logs
bash crates/rustc-codegen-cuda/examples/array_constants/verify-code-shape.sh
git diff --check

mir-importer result:

1101 passed; 0 failed

The optimized and non-optimized array_constants runs, focused smoketest, and LLVM code-shape verification all pass.

Documentation

Update supported-features.md to distinguish:

  • relocation-free pointer/integer unions, which may use a byte image,
  • relocation-bearing pointer/integer unions, which remain unsupported, and
  • thin-pointer-only device-global unions, which may preserve the supported single relocation at byte zero.

Follow-up to #927.

nihalpasham pushed a commit that referenced this pull request Aug 20, 2026
The docs gate runs `cargo doc` without `--document-private-items`, so it never
renders a private item and never checks the intra-doc links in one. #1014 and
#1042 cleaned `cuda-macros`, `mir-importer/statement.rs` and the codegen
backend's collector; this is one of the four that were left:

    error: unresolved link to `classify_op`
      --> crates/llvm-export/src/export/ops.rs:55

`classify_op` is a `macro_rules!` defined further down the same file, at :138.
A `macro_rules!` macro is in scope only textually after its definition unless
it is exported, so no path resolves from a doc comment above it -- rustdoc says
as much: "`macro_rules` named `classify_op` exists in this crate, but it is not
in scope at this link's location". It becomes a plain code span, and since the
sentence is one step of a four-step contract about this file, it gains "below"
so the reference still points somewhere.

Three of the four remain. One is `make_disjoint_slice_struct` in
`mir-lower/src/convert/ops/aggregate.rs`, which needs only a
`crate::convert::types::` prefix; the other two are `MirFieldAddrOp` and
`MirArrayElementAddrOp` in `mir-importer/src/translator/rvalue.rs`. Both files
are under open pull requests (#1062 and #1064 for the first, #1059 and #1062
for the second), so they are left rather than raced.
`--document-private-items` can join the docs gate once they land.

Verified: `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps
--document-private-items -p llvm-export` reports one unresolved link on `main`
and none with this change. The gate's own invocation passes before and after.
fmt clean.

Comment-only change.

Signed-off-by: Vyncint Ng <[email protected]>
@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 Aug 20, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator
  • Verified end to end: mir-importer 1101 tests green, cargo oxide run array_constants passes on an RTX 5090 in both the default and CUDA_OXIDE_NO_OPT=1 pipelines, verify-code-shape.sh passes, per-example fmt/clippy clean, 95 workspace suites green, full compile-only smoketest 216/216.
  • The gate is fail-closed on every axis attacked: it sits after the range-scoped relocation scan and refuses any union whose storage is not one naturally aligned full-width pointer word, so pointer-only and pointer-bearing unions cannot be rerouted through the byte image, and relocations straddling into the union's byte range are caught by the overlap predicate.
  • The book paragraph matches the implemented boundary.

@nihalpasham
nihalpasham merged commit e12a86b into NVIDIA:main Aug 20, 2026
49 checks passed
@uurl
uurl deleted the fix/mir-importer-relocation-free-pointer-int-unions branch August 22, 2026 06:09
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 relocation-free pointer/integer union constants via byte images

2 participants