Skip to content

fix(mir-importer): lower tuple array constants with rustc layout - #383

Merged
nihalpasham merged 5 commits into
NVIDIA:mainfrom
niklebedenko:codex/upstream-tuple-array-constants
Jul 23, 2026
Merged

nihalpasham merged 5 commits into
NVIDIA:mainfrom
niklebedenko:codex/upstream-tuple-array-constants

Conversation

@niklebedenko

Copy link
Copy Markdown
Contributor

Closes #382.

Summary

MIR import now materializes byte-backed array constants whose elements are supported tuples. Array stride and tuple field positions come from rustc layout, while pointer-bearing allocations remain explicitly unsupported rather than being reconstructed from relocation placeholder bytes.

Before: tuple array elements were rejected, and packed tuple slicing could ignore padding.
After:  supported tuples use rustc offsets and stride; pointer provenance fails closed.

What changed

Layout-aware aggregate decoding

  • Carries the Rust array type through recursive byte-backed array construction and uses rustc's element size as the stride.
  • Routes tuple elements through the shared typed constant decoder.
  • Uses rustc field offsets and field sizes for array elements and standalone tuple constants.
  • Preserves primitive and recursively nested array handling.

Correctness and safety hardening

  • Validates field counts, layout shape, byte ranges, and checked offset arithmetic before constructing MIR values.
  • Rejects array constants containing pointer provenance before relocation bytes can be mistaken for exposed-provenance pointers.
  • Updates feature documentation and registers the negative example with the error-smoke suite.

Known separate limitations

  • Struct and initialized-union array elements remain unsupported.
  • Pointer-bearing tuple arrays remain unsupported until aggregate relocations can be represented faithfully.

Testing

  • cargo oxide fmt --check
  • Strict -D warnings clippy for mir-importer, array_constants, and error_tuple_array_provenance.
  • 17 mir-importer tests and error-example status synchronization.
  • Optimized and no-opt LLVM assertions for padded direct, nested-ZST, and enum tuple constants.
  • Exact negative diagnostic assertion for pointer provenance.
  • RTX 3080 (sm_86) optimized and no-opt execution: all 24 threads passed.

Checklist

  • Branch based on current upstream/main
  • Layout and provenance claims covered by positive and negative regressions
  • Unsupported provenance fails clearly
  • Optimized and unoptimized code shape and GPU behavior verified

@nihalpasham nihalpasham added bug Something isn't working codegen Device code-generation pipeline (Rust MIR to IR to PTX) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) maintainer-owned tests Adds or improves test coverage labels Jul 22, 2026
@nihalpasham
nihalpasham force-pushed the codex/upstream-tuple-array-constants branch from 7cdd563 to 858b9cc Compare July 22, 2026 08:47
@nihalpasham nihalpasham added the examples Example kernels and host harnesses under crates/rustc-codegen-cuda/examples label Jul 22, 2026
@nihalpasham
nihalpasham force-pushed the codex/upstream-tuple-array-constants branch from 858b9cc to fe7240d Compare July 23, 2026 10:30
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks for the PR

I layered maintainer commits on top:

  • Tuple fields are now sliced at rustc's real layout offsets (padding, field reordering, ZSTs), cross-checked against the dialect's own tuple metadata so any disagreement fails closed instead of decoding garbage.
  • Over-aligned tuples keep their alignment through nested arrays all the way down to the PTX stack depot.
  • Pointer-bearing tuple and array constants are rejected with a clear diagnostic instead of decoding relocation bytes as an address. A relocation's stored bytes are only the offset into the target allocation; until the per-field decoders carry the provenance map, rejecting loudly beats miscompiling silently. Two error examples pin the diagnostics in the smoketest error lane, and this closed a live silent miscompile for direct pointer-bearing tuple constants.

Validated: 804 importer and 209 mir-lower tests, the expanded array_constants example on an RTX 5090 (including a repr(align(32)) tuple whose address low bits are checked on device), and both error examples. Closes #382.

niklebedenko and others added 5 commits July 23, 2026 16:52
Use rustc's exact array sizes and tuple offsets, cross-check the translated tuple layout, decode non-empty all-ZST tuples through their aggregate path, and reject pointer relocations before byte lowering. Strengthen runtime, LLVM-shape, and diagnostic coverage while preserving current struct and enum constant handling.

Signed-off-by: nihalp <[email protected]>
Restore the established primitive and nested-array boundary for pointer-to-array constants, and reject direct tuple pointer relocations before byte decoding can fabricate an address. Add focused unit and end-to-end diagnostics coverage without widening supported aggregate forms.

Signed-off-by: nihalp <[email protected]>
opt with plain -O2 keeps the external over-aligned helper, which owns
the align-32 spill; once the middle-end internalizes non-root helpers,
opt inlines it into check_array_constants and GlobalDCE deletes the
definition, so extracting the helper symbol finds nothing. Pick the
function that actually survives in each artifact and assert the spill
shapes there. The unoptimized-IR assertions keep using the helper
symbol, which always exists there.
@nihalpasham
nihalpasham force-pushed the codex/upstream-tuple-array-constants branch from fe7240d to 5ce84c6 Compare July 23, 2026 11:24

@viktoriavikulya viktoriavikulya left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally and it works. Consider extracting the retry logic into a shared utility — we have similar code in the auth module.

@nihalpasham
nihalpasham merged commit 6f3318e into NVIDIA:main Jul 23, 2026
33 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) examples Example kernels and host harnesses under crates/rustc-codegen-cuda/examples IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) maintainer-owned tests Adds or improves test coverage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Array value constants with tuple elements fail MIR import

3 participants