Repository navigation
perf(mir-importer): promote a field-less enum array constant like an integer one - #694
Conversation
…integer one NVIDIA#684 stopped bare array constants being built per thread, but its element gate admits only primitive scalars and nested arrays of those. A field-less enum array was left out, even though it holds nothing but discriminants and so is byte-identical to the `[u32; N]` of its values. It was still materialized into the per-thread local depot -- one `st.local` per element in every thread, for data the module image already carries -- and the lookup then read thread-private memory. There was no way to spell around it either: `&[Op; N]`, the reference form that works for scalar tables, is rejected outright. Replace the borrowed `&[T; N]` gate with a predicate of this path's own, which admits primitive scalars, field-less enums, and nested arrays of either. The byte image is rustc's evaluated allocation, so the discriminants were never the question; what the gate has to establish is that reading an element back is cheap, and for a field-less enum the whole element *is* its discriminant. Payload-carrying enums, tuples and structs stay out, and not because a byte image would be wrong. Projecting one field of a tuple element out of a local array copies the whole array to a fresh stack slot, once per field (NVIDIA#693). Those copies dominate: promoting a `[(u8, u32); 256]` table leaves its 925 PTX instructions, 4096-byte depot and 878 `st.local` exactly as they were and merely adds a 2 KiB global. Verified both ways on the A10G before excluding them. A size guard backs the widening: the copy is sized from the destination's converted type while the bytes come from rustc, so the two must agree before a byte image is trusted. `dialect_stored_size` answers from the `total_size` the storage builders reproduce, and any disagreement falls through to the element-wise path rather than copying over a differently-sized local. Measured on an A10G (sm_86, driver 595.71.05), 64 dependent divergent lookups per thread over 8388608 threads, 50 timed launches after 10 warmup. Every kernel bit-correct against a host reference in both runs, and each enum table is paired with a `[u32; N]` table over the same discriminants so the pair isolates the element type: entries table before after speedup u32 pair (control) 16 [Op; N] 1309.4us 361.4us 3.62x 361.4 -> 361.6us 256 [Op; N] 16073.3us 330.2us 48.68x 327.3 -> 330.0us An enum table now costs what the integer table of the same values costs, to within 1% at both sizes, and the controls did not move. PTX for the 256-entry enum kernel goes from 309 instructions with a 1 KiB depot and 256 `st.local` to 70 with no depot, reading through `ld.global.nc`. `array_constants`' `verify-code-shape.sh` pinned the enum array's `insertvalue` chain, so it now asserts the shape that replaced it: the discriminants in the read-only global's initializer, the copy out of that global, and the same runtime index and element load. Pinning the whole byte image in one pattern also fixes their order, which the previous per-discriminant search did not -- a permuted or truncated table now fails where before it passed. That example's runtime assertions were unaffected; the values were never in question. Adds `enum_table_lookup`, which measures both pairs and keeps their agreement checked. Closes NVIDIA#692. Signed-off-by: Vyncint Ng <[email protected]>
…e spelling The bare-array widening left the second defect of NVIDIA#692 open: the pointer path still checked elements with its own validator, so `const R: &[Op; N] = &OPS` kept hard-erroring with "invalid input program" while `OPS[i]` promoted cleanly. Route both promoted-constant paths through the one element gate. `validate_ptr_to_array_constant_type` now asks `promotable_array_element`, which absorbs the zero-length vacuous admission (`&[]` for any element type) so nothing the pointer path accepted is lost, and the two boundaries cannot drift apart again. The pointer path also gains the same byte-size agreement the bare-array promotion demands, except here disagreement is an input error because there is no element-wise lowering to fall back to. A unit test holds the reference form to the bare-array gate: field-less enum arrays pass, payload-carrying enums stay out, and the promoted empty-slice admission survives the rerouting. `enum_table_lookup` gains `enum256_ref_table`, which reads through `const R: &[Op; 256] = &OPS` against the same host reference. On an RTX 5090 it runs bit-correct at the bare enum table's exact cost (106.5us per launch, 1.21x the u32 control for both spellings), and the emitted IR shows it reading the same deduplicated immutable global the bare table copies from.
|
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 |
|
Gist: A table of C-style enums is stored as the same bytes as a table of the integers, so it deserves the same treatment #684 gave integer tables: keep it in the module image once instead of having every thread rebuild it in private memory. The PR widens the acceptance check the right way, with its own predicate plus a guard that the compiler's byte layout agrees with rustc's. I pushed one commit on top:
Verified on an RTX 5090 (sm_120):
Closes #692. Merging. |
…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]>
…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]>
Closes #692. Follow-up to #684.
Problem
Issue #692 names two defects around field-less enum tables:
const OPS: [Op; 256](field-less#[repr(u32)]enum) was rebuilt element by element in every thread's local depot, even though it is byte-identical to the[u32; 256]of its discriminants. Measured 49x slower than the equivalent u32 table at 256 entries.const R: &[Op; 256] = &OPS;did not even compile: "invalid input program".Fix
promotable_array_element): primitive scalars, field-less enums, and nested arrays of either, plus a byte-size agreement guard between the dialect's storage layout and rustc's evaluated allocation.enum_table_lookupexample with paired enum/u32 kernels so the integer row doubles as a control; bit-correct against a host reference.Maintainer commit on top (1242931)
promotable_array_elementpredicate, soconst R: &[Op; N] = &OPScompiles and the two paths cannot drift apart. This closes the second defect of Field-less enum array constants are still materialized per thread, and &[Op; N] is rejected outright #692, making the "Closes" above accurate.enum256_ref_tablekernel reading through&[Op; 256]at a runtime index, verified bit-exact; on an RTX 5090 it runs at the bare enum table's exact cost, reading the same deduplicated immutable global.