Skip to content

fix(mir-lower): give an aggregate niche payload's leaves typed slots - #1591

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
midagedev:fix/niche-payload-leaf-slots
Nov 3, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
midagedev:fix/niche-payload-leaf-slots

Conversation

@midagedev

@midagedev midagedev commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

What This PR Does

Decomposes struct/tuple enum payloads with a pointer niche (Option<RopePair>, Option<(f32, f32, &mut f32)>, Option<(usize, &T)>) into per-leaf typed slots in mir-lower (build_enum_slot_map), keeping construction and payload extraction in SSA registers instead of spilling through a .local stack depot.


Why Pointer-Niche Struct/Tuple Payloads Spilled to .local

Previously, when a struct or tuple payload inside an Option had its niche on a pointer field, build_enum_slot_map gave only the pointer a typed slot in the enum's LLVM struct and represented all other fields as [N x i8] or i64 filler bytes. Reading or writing the payload then went through an alloca stack slot with byte-slice stores and a wide struct load, which LLVM's SROA pass could not promote back to registers.

BEFORE (Option<(f32, f32, &mut f32)> with pointer niche at byte 8):
  LLVM enum struct: { i64_filler, ptr }
  construct / read: alloca + byte-slice stores + wide load ──► 16 B .local stack depot in PTX

AFTER (leaf-by-leaf typed slots when leaves are aligned and non-overlapping):
  LLVM enum struct: { f32, f32, ptr }
  construct / read: llvm.extractvalue + llvm.insertvalue   ──► pure SSA registers (0 B .local)
  • Fails closed to the existing memory path if any leaf overlaps another variant's differently typed claim, is unaligned (packed struct), or contains i1 (bool).

Verification

  • GPU Execution (RTX 5090, sm_120a): cargo oxide run enum_niche_payload and --verify-ptx pass with 0 .local bytes across all entries (compute-sanitizer --tool memcheck: 0 errors); set_discriminant_niche also drops from 16 B .local to 0 B.
  • Tests & CI Gates: cargo test -p mir-lower --all-targets (517/517 PASS), just fmt-check, cargo clippy, and repository guards all pass.

Closes #1590

An enum payload that is a struct or tuple with a pointer niche carrier and
non-pointer leaves (`Option<(f32, f32, &mut T)>`, `Option<{&mut T, u64, u64}>`)
gave only the pointer leaf a typed slot. The other leaves fell into
`[N x i8]` filler bytes, so construct spilled the payload through a slot and
the payload read loaded it back at another width. SROA cannot rebuild
mixed-type slices, and the slot reached PTX as a local depot.

Every leaf of such a payload now claims a typed slot at its rustc offset,
reusing the carrier's slot where the types match, with construct and payload
reads using insertvalue/extractvalue; size, alignment, field offsets and the
niche encoding are unchanged, and a payload the rule cannot prove keeps the
old lowering.

Measured with one device-only probe source against each tree's backend:
parent e027ac3 emits a 16 B `.local` depot in the `smooth_triplets` entry
plus 16 B in the outlined `Cells::next_cell`; this commit emits none.
`lease_borrow` and `lease_raw` emit none on either commit.
Across the 241 examples under rustc-codegen-cuda/examples, only
enum_niche_payload and set_discriminant_niche change PTX, and both lose
their 16 B depot; vectorization and repr_u32_enum_stride are unchanged per
entry.

The NVIDIA#1332 slot-map test now expects the 16 bytes under the carrier as one
i128 slot instead of [2 x i64] filler; alignment stays 16.

Adds the enum_niche_payload example and the leaf-slot unit tests.

Signed-off-by: midagedev <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Oct 21, 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.

@nihalpasham nihalpasham added the cuda-oxide SIMT programming model: rustc backend, cargo oxide, cuda-device, cuda-host, book label Oct 29, 2026
@nihalpasham nihalpasham added bug Something isn't working IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) codegen Device code-generation pipeline (Rust MIR to IR to PTX) perf Performance of generated code or of the compiler itself labels Nov 3, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

/ok to test 3c953b7

@copy-pr-bot

copy-pr-bot Bot commented Nov 3, 2026

Copy link
Copy Markdown

/ok to test 3c953b7

@nihalpasham, there was an error processing your request: E2

See the following link for more information: https://docs.gha-runners.nvidia.com/cpr/e/2/

nihalpasham
nihalpasham previously approved these changes Nov 3, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

/ok to test 3c953b7

@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks, decomposing pointer-niche struct/tuple payloads into per-leaf typed slots when every leaf is aligned and non-overlapping eliminates the .local stack depot cleanly while failing closed on overlapping or bool-bearing layouts. Layered 1 small rebase update on top (3c953b70):

  • Updated enum_niche_payload to cuda-core 0.5.0 after #1614.

Verified mir-lower suites, set_discriminant_niche, and enum_niche_payload (--verify-ptx and compute-sanitizer clean, 0 .local bytes) on RTX 5090. Merging once CI is green.

@nihalpasham

Copy link
Copy Markdown
Collaborator

/ok to test 1103a60

@nihalpasham
nihalpasham merged commit e55d1ea into NVIDIA:main Nov 3, 2026
77 checks passed
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) cuda-oxide SIMT programming model: rustc backend, cargo oxide, cuda-device, cuda-host, book 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.

mir-lower: niche enum payload with mixed leaves lowers to a local depot

2 participants