Repository navigation
perf(mir-importer): promote a tuple array constant now that its fields address - #717
Conversation
|
Ran the full sweep this PR's body offered, on the combined stack (#694 + #715 + 190 / 190 pass, 0 fail, 1218s, A10G (sm_86, driver 595.71.05) — 190 rather than 188 because the stack adds So the "scoped rather than swept, happy to run the full 188" caveat in the body One correction while I am here, in case it matters to review: partway through this Also worth knowing when weighing these: across the 16 pre-existing examples that |
…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]>
51cd664 to
369de33
Compare
|
Rebased onto The commit duplicating #709 is dropped. What is left is the promotion half, which The headline number changed, downward. This PR used to quote 16 625x, which was
Both bit-correct; the control moved 0.2%. Parent Worth flagging for review: the rebase is not a pure replay. #694 made Also: apologies for #715 duplicating #709. I have changed how I scan before filing — |
|
Reviewed and GPU-verified on an RTX 5090:
Merging. Related to #745 but doesn't close it; that one's root cause lives in mir-lower. |
Rebased onto
main, now a single commit (369de33d). This PR was previouslystacked on #694 and #715; #694 has merged, and #715 was closed as already-fixed by
#709. The commit that duplicated #709 has been dropped, so what remains is only
the half that #709 did not do.
Why this is the other half
#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. Neither alone looked like much; together they remove the
per-thread table entirely.
promotable_array_elementtherefore now admits a tuple whose every field is itselfpromotable. 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.
It widens the reference form too, deliberately
Since #694 that predicate is also the gate for the pointer-to-array form
(
const R: &[T; N] = &TABLE, viavalidate_ptr_to_array_constant_type), and itsdoc says
TABLE[i]and(&TABLE)[i]must not drift apart. Widening it widens thatform as well. That is safe rather than incidental: 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 (
"bare tuple-array support must not widen pointer-to-array constants"). It now asserts the admission, plus astruct-in-tuple case proving the recursion holds on that path too. The size guard
from #694 is unchanged and still backs both forms.
Measured, against this commit's actual parent
Re-measured after the rebase rather than carried over, because the baseline moved:
with #709 merged, the old "before" no longer exists. Both runs are the same commit
of
tuple_field_lookup— a 256-entry(u8, u32)table read at a divergent runtimeindex, 1048576 threads x 64 lookups, 10 timed launches after 3 warmup — built by
the two compilers and nothing else changed.
d82a1506)tuple_one_field(PAIRS[i].1)tuple_two_fields(let (a,b)=PAIRS[i])scalar_control(CODES[i])Every kernel bit-correct against a host reference in both builds.
scalar_controlreads a plain[u32; N]over the same index stream and moved by0.2%, so this is a controlled comparison rather than two unrelated runs.
Evidence: parent
.gpu-evidence/20260808T131239Z.log, this commit20260808T130553Z.log.The PTX explains the size of it:
st.localld.global.nctuple_one_fieldon parenttuple_one_field+ thistuple_two_fieldson parenttuple_two_fields+ thisscalar_controlEach thread's 2 KiB private copy of the table is gone and the reads come from the
read-only data cache instead — the shape the scalar table has always had.
tuple_one_fieldnow matchesscalar_controlinstruction for instruction.On the numbers this PR used to quote
The previous body reported 16 625x / 16 557x. That figure was the composition of
two fixes measured against a baseline that had neither, and its middle column
("#715 only") is essentially what
mainis today. Quoting it as this PR's deltawould overstate it, so it is withdrawn in favour of the 7044x / 3751x above.
Re-measuring was not merely pedantic: parent
tuple_two_fieldsis 564 instructionshere where the old table recorded 1084, because #709 already removes the second
whole-array copy. Runtime-wise the two implementations of that fix land in the same
place — 479 684.6 us against my old 479 670 us — so #709 fully substitutes for the
commit that was dropped.
Gates
Scoped smoketest on this branch, A10G, 12/12 pass
(
.gpu-evidence/20260808T131526Z.log) — every example touching tuple or constarray constants, plus
vecaddas a pass control andwgmmaas acompile-only control:
const_tuple_table_lookupis #709's own example and passes unchanged.Locally:
cargo fmt --check(workspace and the new example crate),clippy --all-targets --lib --tests -D warnings, the unit-test set including-p mir-importer(1394 tests), andarray_constants/verify-code-shape.shagainsta freshly built backend.
The code-shape guard
array_constantspinned four tuple tables asinsertvaluechains, which thisreplaces 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. I checked that a swapped field and a padding byte carrying data are both
rejected by it.