Repository navigation
Address a tuple field read instead of copying the whole array - #709
Merged
nihalpasham merged 3 commits intoAug 8, 2026
Merged
nihalpasham merged 3 commits into
nihalpasham merged 3 commits into
Conversation
`mir.field_addr` verified only struct, union and enum pointees, so a field projection on a tuple array element (`let (a, b) = TABLE[i]`) fell back to the value path: the whole array materialized as one first-class-aggregate value, once per field projected. Extend the verifier, the mir-importer read-path classifier and the mir-lower GEP lowering to accept tuple pointees, reusing the same StructLayoutInfo memory-order slot map the struct path already proves correct. Closes NVIDIA#693. Signed-off-by: Alejandro Soto Franco <[email protected]>
…pointee The mir.field_addr verifier extension for tuple pointees also unlocks tuple-field WRITES (arr[i].1 = x, t.0 = x) and &mut tuple-field borrows: statement.rs and the unified address walker already emit MirFieldAddrOp for those shapes unconditionally, so only the verifier stood in the way, and nothing tested the write side. - dialect-mir ops_test: verify a mir.field_addr on a reordered (u8, u32) tuple pointee used as a mir.store destination, plus a negative case storing the wrong field type through it. - const_tuple_table_lookup example: new tuple_field_store kernel that copies tuple-array elements locally, writes .1 through a runtime index with a thread-unique value, writes .0 through a &mut borrow, and checks bit-exact against a host reference. The rustc-reordered (u8, u32) element locks the memory-slot vs declaration-index distinction for stores as the read kernel already does for loads. Validated: cargo fmt --all; cargo clippy -p dialect-mir -p mir-importer -p mir-lower -- -D warnings; cargo test -p dialect-mir -p mir-lower; cargo oxide run const_tuple_table_lookup on an RTX 5090 (all three kernels exact match, SUCCESS marker printed).
The field_addr tuple read path ends in a mir.load that states the loaded field's natural alignment only. A tuple whose ABI alignment comes from a zero-byte repr(align(N)) field, such as (Align32, u8), therefore emitted an align-1 load in the unoptimized LLVM, tripping the array_constants code-shape guard that keeps over-aligned reads off that shape. Gate the read classifier: a tuple field read falls back to the value path when the tuple carries a field that owns zero bytes while its recorded ABI alignment exceeds 1. Detection compares byte sizes, not element counts, recursing through arrays at any nesting depth so a zero-byte [Align32; 2] cannot slip through on its element count of 2. Ordinary tuples keep the address path. Gate shape from PR NVIDIA#715 (vyncint), with the byte-size gap closed.
Collaborator
|
Gist: The missing tuple arm in
I pushed two commits on top:
Verified on an RTX 5090 (sm_120):
Closes #693. (#715 is the same fix filed independently four days later; closing it as already-fixed.) Merging. |
nihalpasham
approved these changes
Aug 8, 2026
vyncint
added a commit
to vyncint/cuda-oxide
that referenced
this pull request
Aug 8, 2026
…s address NVIDIA#694 kept tuple elements out of immutable-global promotion, and said why: while a tuple field read still copied the whole array to a stack slot, that copy dominated so completely that promoting the table changed nothing measurable and only added a global to the module image. I measured it both ways there and it was neutral. NVIDIA#709 removed that copy. With the read addressing in place, the promotion is what takes the depot away -- the two only pay off together, which is why neither alone looked like much. So `promotable_array_element` now admits a tuple whose every field is itself promotable. Structs stay out because the element-wise path does not build arrays of them either, so there would be nothing to fall back to. A payload-carrying enum stays out because reading one back still round-trips the payload through memory: the address walker resolves enum payload fields for writes, not for reads. Since NVIDIA#694, that predicate is also the gate for the pointer-to-array form (`const R: &[T; N] = &TABLE`, via `validate_ptr_to_array_constant_type`), and its doc says the two forms must not drift apart. Widening it therefore widens that form too, which is intended and safe: nothing on that path enumerates fields. The initializer is rustc's evaluated byte image and the size-agreement check rejects any layout the dialect reproduces differently, so a tuple element travels it exactly as a scalar does. Its boundary test asserted the old exclusion; it now asserts the admission, plus a struct-in-tuple case proving the recursion holds there as well. Measured on `tuple_field_lookup`, a 256-entry `(u8, u32)` table read at a divergent runtime index, comparing this commit against its parent: kernel depot st.local ld.global.nc instrs tuple_one_field 2048 512 0 562 +this 0 0 4 67 tuple_two_fields 2048 512 0 564 +this 0 0 4 59 scalar_control 0 0 4 67 (unchanged) Each thread's 2 KiB private copy of the table is gone and the reads come from the read-only data cache instead, which is the shape the scalar table has always had: `tuple_one_field` now matches `scalar_control` instruction for instruction. `scalar_control` reads a plain `[u32; N]` over the same index stream and does not move, so the comparison is controlled rather than two separate builds. `array_constants`' code-shape guard pinned four tuple tables as `insertvalue` chains, which this replaces with byte images. Each is now asserted as the initializer bytes plus the lowered element type, which is strictly stronger: the reordered `(u8, u32, u64)` table went from four separate slot searches to one pattern that fixes all three fields at their byte offsets, the padding between them, and both elements' stride at once. Checked that a swapped field and a padding byte carrying data are both rejected by it. Signed-off-by: Vyncint Ng <[email protected]>
nihalpasham
pushed a commit
that referenced
this pull request
Aug 10, 2026
…s address (#717) #694 kept tuple elements out of immutable-global promotion, and said why: while a tuple field read still copied the whole array to a stack slot, that copy dominated so completely that promoting the table changed nothing measurable and only added a global to the module image. I measured it both ways there and it was neutral. #709 removed that copy. With the read addressing in place, the promotion is what takes the depot away -- the two only pay off together, which is why neither alone looked like much. So `promotable_array_element` now admits a tuple whose every field is itself promotable. Structs stay out because the element-wise path does not build arrays of them either, so there would be nothing to fall back to. A payload-carrying enum stays out because reading one back still round-trips the payload through memory: the address walker resolves enum payload fields for writes, not for reads. Since #694, that predicate is also the gate for the pointer-to-array form (`const R: &[T; N] = &TABLE`, via `validate_ptr_to_array_constant_type`), and its doc says the two forms must not drift apart. Widening it therefore widens that form too, which is intended and safe: nothing on that path enumerates fields. The initializer is rustc's evaluated byte image and the size-agreement check rejects any layout the dialect reproduces differently, so a tuple element travels it exactly as a scalar does. Its boundary test asserted the old exclusion; it now asserts the admission, plus a struct-in-tuple case proving the recursion holds there as well. Measured on `tuple_field_lookup`, a 256-entry `(u8, u32)` table read at a divergent runtime index, comparing this commit against its parent: kernel depot st.local ld.global.nc instrs tuple_one_field 2048 512 0 562 +this 0 0 4 67 tuple_two_fields 2048 512 0 564 +this 0 0 4 59 scalar_control 0 0 4 67 (unchanged) Each thread's 2 KiB private copy of the table is gone and the reads come from the read-only data cache instead, which is the shape the scalar table has always had: `tuple_one_field` now matches `scalar_control` instruction for instruction. `scalar_control` reads a plain `[u32; N]` over the same index stream and does not move, so the comparison is controlled rather than two separate builds. `array_constants`' code-shape guard pinned four tuple tables as `insertvalue` chains, which this replaces with byte images. Each is now asserted as the initializer bytes plus the lowered element type, which is strictly stronger: the reordered `(u8, u32, u64)` table went from four separate slot searches to one pattern that fixes all three fields at their byte offsets, the padding between them, and both elements' stride at once. Checked that a swapped field and a padding byte carrying data are both rejected by it. Signed-off-by: Vyncint Ng <[email protected]>
alejandro-soto-franco
deleted the
fix/tuple-array-const-runtime-index
branch
September 9, 2026 17:08
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #693.
Problem
Reading one field of a tuple stored in an array copied the whole array first, once per field, because
mir.field_addr's verifier accepted struct, union and enum pointees but not tuples. The read then fell back to the value path, which materializes the entire array as a first-class aggregate.Fix
The missing tuple arm at all three layers, no new MIR op:
dialect-mir:MirFieldAddrOp::verifyaccepts aMirTupleTypepointee (field types via declaration order).mir-importer: the read classifier routes tuple field projections onto the address path.mir-lower:convert_field_addrresolves the GEP slot throughStructLayoutInfo::of_tuple, the same memory-order map the write path already uses. This is the load-bearing part: rustc reorders(u8, u32)in memory, and the GEP must name the memory slot, not the declaration index. Unit tests at both IR layers lock that distinction.Maintainer commits on top
arr[i].1 = x) and&muttuple-field borrows, which previously failed dialect verification loudly. Added the missing coverage: a dialect-level store-through-field_addr test on the reordered(u8, u32)tuple, and atuple_field_storekernel in the example (runtime-index field write plus a&mutfield borrow, bit-exact against a host replica).array_constants' code-shape guard rejecting the over-aligned-ZST tuple kernel on this branch: the address path's final load states natural alignment only, so routing arepr(align(N))-carrying tuple onto it emits an under-aligned (though still correct) load shape. Over-aligned-ZST tuples now stay on the value path. Gate shape from PR perf(mir-importer): address a tuple field in place instead of copying its array #715 (vyncint), with its byte-size gap closed ([Align32; N]array fields are measured in bytes, not element count); unit-tested for the nested-array cases, and ordinary tuples verified to stay on the address path.