Skip to content

fix(mir-lower): preserve address spaces through typed enum fillers - #1332

Merged
nihalpasham merged 5 commits into
NVIDIA:mainfrom
YunusMutlu:fix/enum-filler-global-load
Oct 17, 2026
Merged

nihalpasham merged 5 commits into
NVIDIA:mainfrom
YunusMutlu:fix/enum-filler-global-load

Conversation

@YunusMutlu

@YunusMutlu YunusMutlu commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

What this adds

Niche-encoded enums whose payload leaves a run of bytes that no typed slot claims now store that run as the widest layout-neutral integer array instead of [N x i8]. The block-argument scalarizer in mir-lower splits a merged aggregate only up to 16 leaves. Option<([u64; 2], &[F32x4])> as { [16 x i8], ptr, i64 } is 18 leaves, so the Some/None join stayed one aggregate phi, opt could not take it apart, and NVPTX address-space inference lost the slice pointer behind an extractvalue. As { [2 x i64], ptr, i64 } it is 4 leaves, the join becomes scalar phis, and the 128-bit access lands in the global window again.

before  { [16 x i8], ptr, i64 }  18 leaves  aggregate phi survives -O2  ld.v4.b32 / st.v4.b32         (LD.E.128 / ST.E.128)
after   { [2 x i64], ptr, i64 }   4 leaves  scalar phis, folded          ld.global.v4.b32 / st.global.v4.b32  (LDG.E.128 / STG.E.128)
let wrapped = if lo == u64::MAX { None } else { Some(([lo, hi], input_quads)) };
let Some((_cookie, quads)) = wrapped else { return };
out_quads[i] = quads[i];

What we fixed

  • A 16-byte filler at an 8-aligned offset in an enum aligned to at least 8 lowers as [2 x i64]; i128 would impose 16-byte natural alignment and is not used.
  • The rule is general: for the largest width w in {8, 4, 2} with size and offset multiples of w and w not above the enum alignment, the filler is iw when size == w and [size / w x iw] otherwise. 24-, 32- and 40-byte fillers beside a slice reach ld.global / st.global too, and the 32- and 40-byte cases lose their .local spill; align-4 and align-2 enums get [4 x i32] and [8 x i16] and lose theirs.
  • Anything no width divides keeps the byte array; the slot map's size and alignment checks remain hard errors.
  • vectorization carries load and store kernels through a wide Option, launched with a round-trip check and a None-arm check, plus a 24-byte case, and asserts the 128-bit global access in the embedded PTX.
Implementation
  • make_enum_filler_type is the one builder both filler sites of build_enum_slot_map call; the rule is the one rustc's type_padding_filler uses for padding (largest of i64, i32, i16 whose alignment fits, i128 excluded).
  • Layout neutrality holds by construction: same byte count, natural alignment w not above the enum alignment, offset a multiple of w, so the natural struct walk inserts no gap and every later field keeps its byte offset.
  • The filler is never read field-wise; a payload without a typed slot round-trips through memory as a whole aggregate, so the only observable change is the leaf count.
  • The doc comment names the scalarizer's 16-leaf budget as the gate and what stays a byte array.
  • A [0 x i128] anchor still raises 16-byte enum alignment where rustc requires it (u128 payload test).
  • Tests: builder cases for the offset and alignment conditions, 16 bytes at align 4 ([4 x i32]), 24 bytes ([3 x i64]), 12 bytes at align 4 ([3 x i32]), byte-array fallbacks, and a scalarizer test showing { [16 x i8], i64, i64 } stays one block argument while the typed storage splits.

Verification

Checked at 878b53c98 (the PR head merged with main e84e5b3d7):

  • ✅ cargo test -p mir-lower --all-targets: 313 unit and 147 integration tests pass; three of the new builder tests fail at the PR head and the 24-byte example check fails there with exit 1. Workspace clippy with -D warnings, rustdoc, cargo oxide fmt --check, the example lock check and the 18 guards pass. All 60 CI checks passed.
  • ✅ cargo oxide run vectorization on an RTX 5090 (sm_120a): SUCCESS for 26 vector types; the wide-option kernels show ld.global.v2.b64 / st.global.v2.b64; compute-sanitizer memcheck 0 errors, 0 leaks.
  • ✅ cargo oxide run vectorization --arch sm_86: ld.global.v4.b32 / st.global.v4.b32; SASS LDG.E.128 / STG.E.128. With main's backend the same example reports all three global accesses lost and exits 1.
  • ✅ --emit-nvvm-ir --arch sm_120: SUCCESS through libNVVM.
  • ✅ Probe kernels that read the filler back (cookies, mixed fields with padding, NaN bit patterns, loop-carried and three-way merges, Option::map, by-value aggregate return) copy bit-exactly optimized, unoptimized and with main's backend; memcheck clean. 24-, 32- and 40-byte probes emit ld.global.v2.b64 / st.global.v2.b64 with no aggregate phi after -O2 and no .local frame.
  • ✅ Raising the scalarizer budget to 64 in a source copy flips every 24/32/40-byte case to global with no filler change, which confirms the budget as the mechanism.

Caveats

  • A run that no integer width divides stays a byte array. Option<(bool, [u64; 2])> places 23 bytes at offset 1, so its merge stays one aggregate phi with a .local spill; no pointer is involved, so no address space is lost. The 16-leaf budget itself is a follow-up.
  • A filler wider than 14 i64 words beside a pointer and a length exceeds the budget and stays an aggregate phi with a generic access.
  • Payloads with internal padding, for example (u32, u16, u8, u64, &[T]), gain the global access but keep a smaller spill.
  • No runtime speedup was measured: a bandwidth-bound streaming copy on sm_86 ran in about 745 us either way. The benefit is the global state space and the removed spills.

Closes #1331.


@nihalpasham nihalpasham added the cuda-oxide SIMT programming model: rustc backend, cargo oxide, cuda-device, cuda-host, book label Oct 11, 2026
@YunusMutlu
YunusMutlu force-pushed the fix/enum-filler-global-load branch from c626c73 to 55b5c39 Compare October 13, 2026 10:51
@copy-pr-bot

copy-pr-bot Bot commented Oct 13, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

The 16-byte [2 x i64] filler fixed one width of a size-general loss.
The block-argument scalarizer splits a merged aggregate only up to 16
leaves, and a [N x i8] filler costs one leaf per byte, so every run of
15 bytes or more beside a pointer and a length kept the enum join as
one aggregate phi and lost the pointer's global address space.

Pick the widest of 8, 4 and 2 bytes that divides the run's size and
offset and does not exceed the enum alignment, and type the run as that
integer or an array of it. This is the rule rustc uses for its own
padding; i128 stays out because of its 16-byte natural alignment. 24-,
32- and 40-byte payloads beside a slice now reach ld.global and
st.global, the 32- and 40-byte cases lose their .local spill, and
align-4 and align-2 enums get [4 x i32] and [8 x i16]. The doc comment
names the leaf budget, the builder tests cover the offset, alignment
and size conditions plus 24- and 12-byte fillers, and a scalarizer test
shows byte-array storage staying whole while typed storage splits.

Signed-off-by: nihalpasham <[email protected]>
The two wide-option kernels of the vectorization example were inspected
in the PTX but never run. Launch both with a round-trip check, take the
None arm once and check that the output stays untouched, and add a
24-byte Option<([u64; 3], &[F32x4])> copy kernel with the same 128-bit
global load check. That check fails with the 16-byte-only filler rule
(a generic ld.v2.b64) and passes with the widest-integer rule, so the
generalization cannot regress silently.

Signed-off-by: nihalpasham <[email protected]>
The typed over-budget filler keeps an aggregate phi and a generic access but no local spill; only the bool-carrier byte array spills.

Signed-off-by: nihalpasham <[email protected]>
@nihalpasham

Copy link
Copy Markdown
Collaborator

/ok to test 878b53c

@nihalpasham
nihalpasham merged commit 0ee73e8 into NVIDIA:main Oct 17, 2026
60 checks passed
@nihalpasham nihalpasham added perf Performance of generated code or of the compiler itself IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) codegen Device code-generation pipeline (Rust MIR to IR to PTX) examples Example kernels and host harnesses under crates/rustc-codegen-cuda/examples labels Oct 17, 2026
@nihalpasham nihalpasham changed the title fix(mir-lower): preserve address spaces through 16-byte enum fillers fix(mir-lower): preserve address spaces through typed enum fillers Oct 17, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Nice find. The 16-byte case was the symptom, so I went one step further before landing:

  • The real limit is the block-argument scalarizer: it splits a merged value only up to 16 leaves. { [16 x i8], ptr, i64 } is 18 leaves and stays one aggregate phi, { [2 x i64], ptr, i64 } is 4 and splits. The doc comment now says that instead of "aggregate scalarization".
  • I generalized your rule to the widest layout-neutral integer, the same rule rustc uses for its own padding. 24-, 32- and 40-byte payloads next to a slice now hit ld.global/st.global too, and the 32- and 40-byte ones stop spilling to local memory.
  • Builder tests for the offset and alignment conditions, a scalarizer test that shows the byte array staying whole while the typed form splits, and the example now launches the two wide-option kernels (plus a 24-byte one) and checks the results.

Checked at 878b53c98 on the 5090 at sm_120a and sm_86 and through libNVVM, memcheck clean; 313 mir-lower tests, clippy, guards and all 60 CI checks green. That takes care of #1331. The 16-leaf budget itself is a follow-up: a 23-byte run behind a bool carrier still rides an unsplit phi.

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) cuda-oxide SIMT programming model: rustc backend, cargo oxide, cuda-device, cuda-host, book examples Example kernels and host harnesses under crates/rustc-codegen-cuda/examples IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) perf Performance of generated code or of the compiler itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

16-byte enum fillers can erase global pointer address spaces during lowering

2 participants