Skip to content

fix(mir-importer): materialize initialized union constants - #857

Merged
nihalpasham merged 3 commits into
NVIDIA:mainfrom
uurl:fix/initialized-union-constants
Aug 13, 2026
Merged

nihalpasham merged 3 commits into
NVIDIA:mainfrom
uurl:fix/initialized-union-constants

Conversation

@uurl

@uurl uurl commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #856

Teach the MIR importer to materialize pointer-free initialized union constants from their evaluated rustc allocation.

Instead of trying to reconstruct an active union field, this change preserves the allocation's physical byte image and transmutes that storage into the corresponding MirUnionType.

Implementation

The importer now:

  • recognizes MirUnionType in constant translation;
  • validates that the Rust layout size matches the MIR union storage size;
  • rejects pointer-bearing union types and allocations with overlapping relocations;
  • materializes initialized bytes as u8 constants;
  • preserves uninitialized bytes as typed undef;
  • constructs a [u8; size] storage value;
  • transmutes that byte image into the exact union type.

Union constants are also admitted as bare array elements, enabling runtime-indexed [U; N] constants.

The byte-only constant decoding path intentionally does not support non-ZST unions, because that path has already lost rustc's initialization mask and cannot faithfully distinguish initialized bytes from inactive/uninitialized storage.

Covered cases

The array_constants example now exercises:

  • direct initialized unions;
  • runtime-indexed arrays of unions;
  • unions nested inside tuples;
  • unions nested inside structs;
  • partially initialized unions;
  • MaybeUninit<T> arrays.

The supported-features documentation has been updated accordingly.

Out of scope

This PR intentionally does not add support for:

  • pointer-bearing union constants;
  • union allocations containing pointer relocations;
  • fat-pointer/provenance-sensitive union constants;
  • pointer-to-array union constants such as &[U; N];
  • device-global initializer relocations.

Those cases require provenance-aware constant materialization rather than a raw byte-image transmute.

Validation

  • cargo fmt --all
  • cargo test -p cargo-oxide - 174/174 passed
  • cargo clippy -p cargo-oxide --all-targets -- -D warnings
  • cargo test -p mir-importer --lib - 1068/1068 passed
  • 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
  • example-local cargo fmt -- --check
  • git diff --check

uurl and others added 2 commits August 12, 2026 21:49
The union-lane edit flipped the script's mode from 100755 to 100644,
almost certainly by accident. smoketest invokes it through bash so
nothing failed there, but the example header documents direct ./
invocation, which needs the bit.

Signed-off-by: nihalpasham <[email protected]>
@nihalpasham nihalpasham added bug Something isn't working codegen Device code-generation pipeline (Rust MIR to IR to PTX) labels Aug 13, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Gist: materializes initialized union constants from rustc's evaluated byte image without guessing an active field: initialized bytes preserved, uninitialized inactive bytes stay undef, and the image is transmuted into the layout-exact union type; nested, runtime-indexed, and MaybeUninit<T> forms included, pointer relocations inside unions still fail closed. Pushed 1dae97e restoring the executable bit on verify-code-shape.sh, which the union-lane edit flipped to 644 (smoketest runs it through bash, but the example header documents direct ./ invocation). Closes #856.

Verified here:

  • ran on an RTX 5090 (sm_120): array_constants PASS in both optimization modes, including the partially-initialized-union and MaybeUninit lanes where undef handling matters;
  • ./verify-code-shape.sh works again by direct invocation after the mode fix;
  • 1068 mir-importer tests pass.

One durability note for the record: correctness of the undef bytes rides on MirUndefOp lowering to llvm.undef; if the exporter ever migrates undef to poison, this path must be revisited. Landing once CI is green, after #855 (the two touch the same decoder comments and example, and the reconciliation is prepared).

# Conflicts:
#	crates/mir-importer/src/translator/rvalue.rs
#	crates/rustc-codegen-cuda/examples/array_constants/src/main.rs
@nihalpasham
nihalpasham merged commit 17eb3cc into NVIDIA:main Aug 13, 2026
44 checks passed
@uurl
uurl deleted the fix/initialized-union-constants branch August 13, 2026 08:21
nihalpasham added a commit that referenced this pull request Aug 17, 2026
…m the tree (#964)

* docs(book): the dialect inventories in mlir-dialects have drifted from the tree

All four hand-maintained inventories on this page are stale and nothing
checks any of them. Counts below are from the tree at a766fc2.

**dialect-mir types: seven -> nine.** `mir.union` (Rust unions, every field
a view of the same bytes) and `mir.fp16` (`f16`) both have
`#[pliron_type(...)]` declarations in `dialect-mir/src/types.rs` and no row.
`mir.union` is load-bearing right now -- #857 and #927 are both about union
constants.

**dialect-mir operations: 54 across 11 categories -> 62 across 12.** Six
categories are short and one whole module is missing:

  control_flow   5 -> 6    `mir.unroll_hint`
  memory         9 -> 11   `mir.memcpy`, `mir.memmove`
  comparison     6 -> 7    `mir.cmp`
  aggregate      8 -> 10   `mir.construct_slice`, `mir.construct_disjoint_slice`
  enum_ops       3 -> 4    `mir.set_discriminant`
  debug          - -> 1    `mir.dbg_value`, absent from the table entirely

**LLVM dialect: 62 -> 69**, counted at the pinned `pliron` revision
(8447aa42), where `pliron-llvm/src/ops.rs` declares 51 ops with
`#[pliron_op(` plus 18 through its binop macros. The table gains
`indirectbr`, `insert_element`, `shuffle_vector`, `poison`, and a row for
the four with no existing category (`freeze`, `va_arg`, `blockaddress`,
`blocktag`).

Two of the names in it are wrong rather than merely missing:

  * `inline_asm_multi` does not exist. It is in no version of `pliron-llvm`
    and nowhere in this repository -- this page is the only place the string
    appears. Same shape as the fictional `shuffle_xor_{f32,i32}` rows #815
    removed from the API quick reference.
  * `extractelement` is spelled `llvm.extract_element`.

Also notes that `llvm-export` contributes one op of its own, `llvm.dbg_value`,
which the surrounding prose did not mention, and that this table tracks an
upstream crate -- so it moves when the pin moves (as it did in #919), not
when this repository changes.

**dialect-nvvm: 123 -> 543.** This one is not a drift so much as a different
tree. Six of the twelve modules listed no longer exist: `thread` and `warp`
are gone (the warp ops are now split across `warp_shuffle`, `redux`, `vote`,
`warp_match`, `warp_barrier`, `active_mask` and `elect`), and `mbarrier`
split into `mbarrier_basic` and `mbarrier_extended`. The counts for those
that do exist are 4x-9x low: `tcgen05` is listed at 24 and has 210, `tma` at
15 and has 111. Eighteen modules are missing outright, including `sreg` at 44
ops.

Rewritten around the split `adding-new-intrinsics.md` already documents --
7 hand-written modules (18 ops) against 34 generated ones (525 ops, resolved
from 986 catalog entries) -- because that is what decides where a
contributor would add an op, and a table that says `warp` when the file is
`ops/generated/warp_shuffle.rs` sends them to the wrong place.

The per-module "Minimum SM / GPU Family" columns go with it. Those
requirements are per intrinsic, not per module, and the catalog already
records them: `intrinsics/generated-reference.md` renders the PTX version and
minimum SM for each of the 986 entries and is regenerated with the ops. The
page now points there instead of keeping a second copy by hand.

`crates/dialect-mir/README.md` carries its own copy of the operation
inventory, stale in the same way and by a different amount (it says 55, and
already had `set_discriminant` the book was missing). That is a separate
file with no overlap here and is fixed separately.

Verified: every count above recomputed from the tree; `just book`
(`sphinx-build -W --keep-going`) succeeds; check-book-api-names,
check-toolchain-parity and check-cli-doc-coverage all pass.

Signed-off-by: Vyncint Ng <[email protected]>

* docs(book): stamp the dialect-nvvm counts to the catalog SHA

The page's own thesis is that unchecked hand counts drift. The LLVM
table got an "at the pinned pliron revision" stamp; give the
dialect-nvvm totals the equivalent anchor: the catalog SHA-256 that
every ops/generated/ file header prints. A reader can now tell at a
glance whether 543/41/986 describe the catalog they are looking at.

---------

Signed-off-by: Vyncint Ng <[email protected]>
Co-authored-by: nihalpasham <[email protected]>
nihalpasham pushed a commit that referenced this pull request Aug 17, 2026
…ehind the code (#965)

Both inventories in `crates/dialect-mir/README.md` are hand-maintained and
nothing checks them. Counts below are from the tree at a766fc2.

**Types: "seven" -> nine.** Two types have `#[pliron_type(...)]`
declarations in `src/types.rs` and no row: `MirUnionType` and `MirFP16Type`.
`MirUnionType` is not a corner: #857 and #927 are both about union constants,
so a reader who came to this README to understand that work found no mention
of the type carrying it.

**Operations: 55 across 11 modules -> 62 across 12.** Five modules are short
and one is missing outright:

  control_flow   5 -> 6    unroll_hint
  memory         9 -> 11   memcpy, memmove
  comparison     6 -> 7    cmp
  aggregate      8 -> 10   construct_slice, construct_disjoint_slice
  debug          - -> 1    dbg_value, no row at all

The `Ops` column and the header now agree with `src/ops/`, and the header
says so, since one row per file in that directory is the invariant that
makes the table checkable at a glance.

The book keeps a second copy of this same operation inventory in
`cuda-oxide-book/compiler/mlir-dialects.md`. It is stale too, and by a
different amount -- it says 54 where this file says 55, because it never
picked up `set_discriminant` which this one already had. Two hand-kept
copies drifting apart at different rates is the argument for reading
`src/ops/` rather than either. That file is fixed separately; there is no
overlap between the two changes.

Verified: every row recomputed from `src/types.rs` and `src/ops/`, both
tables now match exactly; check-crate-inventory and check-toolchain-parity
pass.

Signed-off-by: Vyncint Ng <[email protected]>
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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support initialized union constants in the MIR importer

2 participants