Repository navigation
Conversation
emit/lower.rs keeps dozens of tables keyed by values, registers and block indices and asks them about every instruction. structure.rs already had an Fx hasher for this reason; move it to crate::hash and use it in lowering and wave::sections. Upstream measured lowering ~16% faster (whole translation ~8%), with byte-identical output. From upstream PR Marvisak#1002 (0e33bfe), adapted: its bench tests are not taken. Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>
|
Independent follow-up, review them in any order: #1033 (one file, +25/-25) removes two of the four allocations |
ir::build fills every instruction's register lists twice. `effects()` collects `Vec<Reg>` (two allocations), and the caller immediately maps each one into `Vec<(Reg, Value)>` (two more), freeing the first pair. For lists that are almost always two to four entries, that is four allocations and two copies per instruction, on every instruction of every program. Have `effects()` and its helpers collect the `(Reg, Value)` pairs directly, with the placeholder the SSA pass fills in. Two allocations per instruction instead of four; `Inst` and everything downstream are unchanged. Measured with the synthetic bench in #1002 (no game data), ten interleaved rounds per arm, nanoseconds per instruction, at 256/1k/4k/16k instructions: ir::build 308 -> 269, 375 -> 324, 407 -> 340, 402 -> 353 (0.835-0.878) translation 898 -> 872, 898 -> 825, 951 -> 875, 956 -> 912 (0.919-0.971) gfx10::decode, structure, lower and the rest are unchanged. The emitted DXIL is byte-identical at all four sizes. cargo test --workspace --locked: 342 passed, 0 failed, 7 ignored. Both clippy configurations clean; cargo xtask policy ok.
|
Combined measurement of the three open ones, since the individual numbers overlap: this PR, #1033 and #1180 together, one binary pair, twelve interleaved rounds per arm (best of each), nanoseconds per instruction on
So about -15% per translation together, and the emitted DXIL is byte-identical at all four sizes.
The remaining profile after all three, per instruction at 16k: |
|
Thanks - that is the verification this PR needed, and on AMD rather than the Blackwell box I measured on. Two notes so the record is exact:
If you have a minute, the numbers themselves are reproducible on your machine with: It prints nanoseconds per instruction per stage at 256/1k/4k/16k instructions on synthetic GFX10 programs (no game data), and an FNV over the emitted DXIL so a diff shows up immediately. On my machine (Ryzen, Windows 11) |
`Instruction::name()` binary-searches the opcode table, and the pipeline asks about every instruction eight times per translation: `ir::build`, `structure`, `resource` (which has the most call sites), `io` and `lower`. The answer cannot change - the encoding and opcode are decoded from the words and never rewritten - so resolve it once when the instruction is built and read the field after that. Measured with the synthetic bench from Marvisak#1002 (no game data), sixteen interleaved rounds per arm, best of each, nanoseconds per instruction, at 256/1k/4k/16k instructions: resource 17 -> 9, 15 -> 7, 24 -> 16, 23 -> 15 (0.47-0.67) lower 511 -> 479, 428 -> 411, 440 -> 401, 477 -> 428 (0.90-0.96) io 27 -> 23, 26 -> 21, 26 -> 22, 30 -> 22 (0.73-0.85) whole 926 -> 881, 920 -> 874, 958 -> 921, 1019 -> 939 (0.92-0.96) `decode` pays the one search it used to be spared (112 -> 115 at 16k, inside the run-to-run spread); every stage after it reads a field. The emitted DXIL is byte-identical at all four sizes. cargo test --workspace --locked: 342 passed, 0 failed, 7 ignored. Both clippy configurations clean; cargo xtask policy ok.
fb2f488 to
1522921
Compare
|
Where the rest of the time goes, measured on the same synthetic programs with a counting global allocator (temporary, not in this PR). Per instruction:
And the split inside At ~35 ns an allocation, 8 of them is ~280 ns - the whole of that phase. The lists are 1-4 elements each:
That is where the remaining ~25-30% of a translation sits, and it is a storage change ( Whether it is worth it: on the real corpus one translation is 26k programs, so ~280 ns per instruction times the instruction count of the corpus. Happy to take it if the direction sounds right, or to leave it here. |
ir::build fills every instruction's register lists twice. `effects()` collects `Vec<Reg>` (two allocations), and the caller immediately maps each one into `Vec<(Reg, Value)>` (two more), freeing the first pair. For lists that are almost always two to four entries, that is four allocations and two copies per instruction, on every instruction of every program. Have `effects()` and its helpers collect the `(Reg, Value)` pairs directly, with the placeholder the SSA pass fills in. Two allocations per instruction instead of four; `Inst` and everything downstream are unchanged. Measured with the synthetic bench in Marvisak#1002 (no game data), ten interleaved rounds per arm, nanoseconds per instruction, at 256/1k/4k/16k instructions: ir::build 308 -> 269, 375 -> 324, 407 -> 340, 402 -> 353 (0.835-0.878) translation 898 -> 872, 898 -> 825, 951 -> 875, 956 -> 912 (0.919-0.971) gfx10::decode, structure, lower and the rest are unchanged. The emitted DXIL is byte-identical at all four sizes. cargo test --workspace --locked: 342 passed, 0 failed, 7 ignored. Both clippy configurations clean; cargo xtask policy ok.
1522921 to
b6db985
Compare
|
Rebased onto today's Two things worth knowing about the new head (
The measured numbers in the description are from the pre-rebase base (
|
`Instruction::name()` binary-searches the opcode table, and the pipeline asks about every instruction eight times per translation: `ir::build`, `structure`, `resource` (which has the most call sites), `io` and `lower`. The answer cannot change - the encoding and opcode are decoded from the words and never rewritten - so resolve it once when the instruction is built and read the field after that. Measured with the synthetic bench from Marvisak#1002 (no game data), sixteen interleaved rounds per arm, best of each, nanoseconds per instruction, at 256/1k/4k/16k instructions: resource 17 -> 9, 15 -> 7, 24 -> 16, 23 -> 15 (0.47-0.67) lower 511 -> 479, 428 -> 411, 440 -> 401, 477 -> 428 (0.90-0.96) io 27 -> 23, 26 -> 21, 26 -> 22, 30 -> 22 (0.73-0.85) whole 926 -> 881, 920 -> 874, 958 -> 921, 1019 -> 939 (0.92-0.96) `decode` pays the one search it used to be spared (112 -> 115 at 16k, inside the run-to-run spread); every stage after it reads a field. The emitted DXIL is byte-identical at all four sizes. cargo test --workspace --locked: 342 passed, 0 failed, 7 ignored. Both clippy configurations clean; cargo xtask policy ok.
`Instruction::words` is a `Vec<u32>` built with `collect()`, so every decoded instruction allocates for the one to five dwords it has and reads them back through a pointer. A counter over the pipeline put that at 1.17 allocations and 149 bytes per instruction in decode - the largest single allocation source there. The longest instruction the decoder produces is an MIMG with its three extra address dwords, five in all, so keep them in `[u32; 5]` with a length byte and return a slice from `words()`. The struct does not grow: `Vec` is 24 bytes, the array and the byte are 24 with padding. A longer instruction is now a clean `Isa` error rather than a silent truncation. Measured standalone against `ffe310e` with the synthetic bench from Marvisak#1002, twelve interleaved rounds per arm, nanoseconds per instruction: decode 48 -> 31, 99 -> 54, 105 -> 82, 104 -> 85 (0.55-0.82) whole 977 -> 934, 970 -> 921, 1001 -> 981, 1046 -> 1033 (0.949-0.988) The emitted DXIL is byte-identical at all four sizes. Honest about the shape: decode is 18-45% faster, the whole translation is 1-5%, and at 16k instructions that gap is inside this machine's run-to-run spread. Applied on top of Marvisak#1002 + Marvisak#1033 + Marvisak#1180 the same change measured 0.929-0.957 of the translation. cargo test --workspace --locked: 342 passed, 0 failed, 7 ignored. Both clippy configurations with -D warnings; cargo xtask policy.
`Instruction::name()` binary-searches the opcode table, and the pipeline asks about every instruction eight times per translation: `ir::build`, `structure`, `resource` (which has the most call sites), `io` and `lower`. The answer cannot change - the encoding and opcode are decoded from the words and never rewritten - so resolve it once when the instruction is built and read the field after that. Measured with the synthetic bench from Marvisak#1002 (no game data), sixteen interleaved rounds per arm, best of each, nanoseconds per instruction, at 256/1k/4k/16k instructions: resource 17 -> 9, 15 -> 7, 24 -> 16, 23 -> 15 (0.47-0.67) lower 511 -> 479, 428 -> 411, 440 -> 401, 477 -> 428 (0.90-0.96) io 27 -> 23, 26 -> 21, 26 -> 22, 30 -> 22 (0.73-0.85) whole 926 -> 881, 920 -> 874, 958 -> 921, 1019 -> 939 (0.92-0.96) `decode` pays the one search it used to be spared (112 -> 115 at 16k, inside the run-to-run spread); every stage after it reads a field. The emitted DXIL is byte-identical at all four sizes. cargo test --workspace --locked: 342 passed, 0 failed, 7 ignored. Both clippy configurations clean; cargo xtask policy ok. (cherry picked from commit 9e5c130) (cherry picked from commit 80994e762bb3cfebeab062d4fff018712deff7f2)
|
Automated triage for the maintainers. This pull request no longer merges cleanly into Generated by Claude Code |
emit/lower.rs keeps thirty tables keyed by Value, a register or a block
index, and asks them about every instruction of every program. They used
HashMap's default SipHash. structure.rs already had an Fx hasher for the
same reason ("SipHash dominated run time"), but the lowering stage, the
larger half of a translation, never got it.
Move the hasher to crate::hash and use its Map/Set in emit/lower.rs and
wave::sections. No emitted byte changes: nothing iterates these tables,
and the one order that reaches the output (user_data) is a BTreeMap.
Measured with the new emit::bench_tests (synthetic GFX10 compute programs,
no game data), ten interleaved rounds per arm, nanoseconds per instruction:
instrs lowering before -> after whole translation
256 593 -> 514 (0.867) 1002 -> 912 (0.910)
1024 529 -> 428 (0.809) 981 -> 920 (0.938)
4096 514 -> 430 (0.837) 1046 -> 968 (0.925)
16384 543 -> 463 (0.853) 1068 -> 965 (0.904)
The emitted DXIL is byte-identical for all four sizes. ir::build,
structure, resource and io are unchanged, as they should be.
cargo test --workspace --locked: 342 passed, 0 failed, 7 ignored.
Both clippy configurations clean; cargo xtask policy: 173 files ok.
ebdd200 to
21ff7f6
Compare
The straight-line program the bench builds has a single block and no phis, so it cannot say anything about loop handling, which every real shader has. This adds a second shape: the same body with an `s_cmp_eq_u32` and an `s_cbranch_scc1` back edge, so the pipeline sees three blocks and the phis on the header. Measured with both shapes in one session (min of six runs each, the two tests alternating so the machine's noise hits both arms): the loop shape costs 1.07 / 1.14 / 1.11 times the straight-line one per instruction at 256 / 1024 / 4096 instructions. 8192 is the largest body that fits: the back edge is an `i16` of words. So blocking, the phis and the loop header add about 10%, and `lower` and `ir::build` stay within a few percent of their straight-line figures - there is no large hidden cost in that part to chase. No game data.
ir::build fills every instruction's register lists twice. `effects()` collects `Vec<Reg>` (two allocations), and the caller immediately maps each one into `Vec<(Reg, Value)>` (two more), freeing the first pair. For lists that are almost always two to four entries, that is four allocations and two copies per instruction, on every instruction of every program. Have `effects()` and its helpers collect the `(Reg, Value)` pairs directly, with the placeholder the SSA pass fills in. Two allocations per instruction instead of four; `Inst` and everything downstream are unchanged. Measured with the synthetic bench in Marvisak#1002 (no game data), ten interleaved rounds per arm, nanoseconds per instruction, at 256/1k/4k/16k instructions: ir::build 308 -> 269, 375 -> 324, 407 -> 340, 402 -> 353 (0.835-0.878) translation 898 -> 872, 898 -> 825, 951 -> 875, 956 -> 912 (0.919-0.971) gfx10::decode, structure, lower and the rest are unchanged. The emitted DXIL is byte-identical at all four sizes. cargo test --workspace --locked: 342 passed, 0 failed, 7 ignored. Both clippy configurations clean; cargo xtask policy ok.
|
Measured on the real corpus, and the result is against this PR: the hasher change does not show up in the title's own shaders. Setup: the full
-0.3%, inside the noise. The two finished 2 s apart after eleven minutes of work, and the progress lines at 93% were identical to the second. Why this differs from the bench in this PR: the What the corpus does confirm, and this part is worth having on the record: the translation output is identical to So: a type swap that is neutral in practice. That is not the bar for landing here, so I am closing it rather than leaving it in the queue - the DXIL-identical measurement and the bench stay in the thread for whoever picks the hashing question up again ( |
…ks, constants Pull requests of fxceless/victory-gta6, each read before it was taken: - Marvisak#1002: the tiled cascade shadow programs translate exactly on 32-lane hosts (f133e35efed2a48a, refused in this machine's log, is one of them); - Marvisak#1014: the compare of copies in the card's memory hashes a piece with 64 threads (on; VICTORY_CARD_HASH_WIDE=0: one); - Marvisak#837: a NaN or an infinity a pixel program exports does not go into the colour target (on; VICTORY_NAN_GUARD=0); - Marvisak#794: a partial texture read-back leaves the bytes beside it; - Marvisak#1058: small buffers in place with VICTORY_COPY_PLAIN=1; - Marvisak#799, in part: constant views of at most 64 KiB, s_pack_*_b32_b16, two more constant sources of descriptors; - Marvisak#823, in part: programs that may take the wave-wide form, by hash; one by default (particle visibility), the others with VICTORY_WAVE_WIDE_ONLY=all. Not taken: the rest of Marvisak#823 (the builder kills the title there), of Marvisak#799 (approximate), Marvisak#1069 (this tree notes committed pages already), Marvisak#1044 (fewer storage workers, measured on another kind of machine), Marvisak#1016 (about a millisecond), Marvisak#827, Marvisak#854 (large; see the handoff). AI-assisted. Not run with the title. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ir::build fills every instruction's register lists twice. `effects()` collects `Vec<Reg>` (two allocations), and the caller immediately maps each one into `Vec<(Reg, Value)>` (two more), freeing the first pair. For lists that are almost always two to four entries, that is four allocations and two copies per instruction, on every instruction of every program. Have `effects()` and its helpers collect the `(Reg, Value)` pairs directly, with the placeholder the SSA pass fills in. Two allocations per instruction instead of four; `Inst` and everything downstream are unchanged. Measured with the synthetic bench in #1002 (no game data), ten interleaved rounds per arm, nanoseconds per instruction, at 256/1k/4k/16k instructions: ir::build 308 -> 269, 375 -> 324, 407 -> 340, 402 -> 353 (0.835-0.878) translation 898 -> 872, 898 -> 825, 951 -> 875, 956 -> 912 (0.919-0.971) gfx10::decode, structure, lower and the rest are unchanged. The emitted DXIL is byte-identical at all four sizes. cargo test --workspace --locked: 342 passed, 0 failed, 7 ignored. Both clippy configurations clean; cargo xtask policy ok. Co-authored-by: pes7 <pes7@users.noreply.github.com>
11 files conflicted (53 hunks): draw.rs 5, host.rs 10, indirect.rs 2, pipeline.rs 2, shader_cache.rs 2, watch.rs 22 in victory-gpu; emit/lower.rs 1, emit/mod.rs 2, emit/wave64_tests.rs 1, ir.rs 5, resource.rs 1 in victory-shader. Most of this line was in the branch already: its shader commits came in as pull requests (fxceless Marvisak#1002, Marvisak#1124, the far-jump exit paths), and its renderer files are fxceless's line, merged just before. Ours: - crates/victory-gpu/src/host.rs, draw.rs, indirect.rs, watch.rs, whole files (the incoming side: PR Marvisak#1014's 64-thread card hash, a 128-bit key for draws' fresh tables, fxceless's write watch; no measurement on our threading, and the card compare's HLSL stays unmixed). - crates/victory-gpu/src/pipeline.rs (the builder's missing pipelines named by our make::hashes). - crates/victory-shader/src/ir.rs, whole file (both sides give a spill lane's lives stores of their own; ours is the form the branch has run with, the incoming site_webs is the same fix another way). - crates/victory-shader/src/emit/lower.rs, emit/shader.rs, and the early depth part of emit/mod.rs: our early_depth (DEPTH_BEFORE_SHADER, not for programs that export depth, stencil or coverage; VICTORY_EARLY_DEPTH) is kept and the incoming one (VICTORY_EARLY_Z) removed: two declarations of the same flag. - crates/victory-shader/src/resource.rs, emit/wave64_tests.rs. Incoming side, combined into ours: - crates/victory-gpu/src/shader_cache.rs and emit/mod.rs: the wave-wide form and the host wave's own first lane for programs listed by hash (VICTORY_WAVE_WIDE_ONLY, VICTORY_FIRSTLANE_OWN_ONLY; defaults: four compute programs a 32-lane driver refused, three structure-builder programs; mravatins 64ffcc0, on RTX 5060 Ti / 5070). The two lists are part of the shader cache key, so the cache is translated anew once. Our last-resort wave-wide form is unchanged. Not taken: docs/nvidia-render-baseline.md, scripts/play-nvidia.ps1 (docs and scripts stay ours; the script starts the game). Builds: cargo build -j 6 --locked --profile diag -p victory --features diagnostics. Not run in game. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
`Instruction::name()` binary-searches the opcode table, and the pipeline asks about every instruction eight times per translation: `ir::build`, `structure`, `resource` (which has the most call sites), `io` and `lower`. The answer cannot change - the encoding and opcode are decoded from the words and never rewritten - so resolve it once when the instruction is built and read the field after that. Measured with the synthetic bench from Marvisak#1002 (no game data), sixteen interleaved rounds per arm, best of each, nanoseconds per instruction, at 256/1k/4k/16k instructions: resource 17 -> 9, 15 -> 7, 24 -> 16, 23 -> 15 (0.47-0.67) lower 511 -> 479, 428 -> 411, 440 -> 401, 477 -> 428 (0.90-0.96) io 27 -> 23, 26 -> 21, 26 -> 22, 30 -> 22 (0.73-0.85) whole 926 -> 881, 920 -> 874, 958 -> 921, 1019 -> 939 (0.92-0.96) `decode` pays the one search it used to be spared (112 -> 115 at 16k, inside the run-to-run spread); every stage after it reads a field. The emitted DXIL is byte-identical at all four sizes. cargo test --workspace --locked: 342 passed, 0 failed, 7 ignored. Both clippy configurations clean; cargo xtask policy ok. (cherry picked from commit 146dfef)
…ped views, typed offsets refused, fork Marvisak#1002's site pairs W1a (VICTORY_STORE_DROP=f133e35e:2957c) brought the heads back with the stairs still gone: what costs them is the one typed-buffer read-modify-write of CS_ShadowRenderTiledCascadesAlphaCBPPPC (#f133e35e) at 0x2957c. Its first lane reads the word of its tile in the third quarter of u17 (table 0x780; 230,400 bytes in W1a's frame 2040, four words per 16x16 tile of 2560x1440), sets bit 14 and writes the word back. Each switch below can A/B one cause with the program running in full: - VICTORY_TYPED_BIT_EXACT=1 (host): typed buffer views of 32-bit integer formats are made in the float format of the same width. Translated programs declare typed loads and stores as float, and through an integer view a store writes the integer the float rounds to (0 for most integer bit patterns), where the console moves the bits: the read-modify-write would leave 0 for the tile's other flags. From GamingRipperDoc's fix in the fork (Marvisak#684, bit_exact_format). - VICTORY_TYPED_OFFSET=refuse (translator, global): a typed buffer load or store whose soffset or built descriptor base is not known to be 0 is refused instead of going to element `index` from the view's start. As RVZO6's VIctory Marvisak#3009 for stores, and for loads with a constant offset. Folding the offset into the index is not done: the element's size is the host view format's, which the translated program does not see. #f133e35e is not concerned: its six typed accesses have soffset 0, offset 0 and a table descriptor. - VICTORY_WAVE_SITE_PAIRS=<hash>,...|all (translator, per program, in the program's cache key): fork Marvisak#1002's IR forms (brunofgmag, eaeb8f17): a pair of scalars parked in two lanes that hold other scalars elsewhere moves as a pair where every save and restore of it match (WebPair, site_pairs), `s_cbranch_execnz` over a far jump to the end is an early-out, and a scalar join that passes one value through is a copy of it. With them #f133e35e and its siblings translate exactly on 32-lane hosts without the mask copies. ir.rs as in the PR (it applied cleanly), each form gated; its card-free tests ported to wave64_tests.rs, on and off. Op dumps now print a buffer or texture UAV's title view description (its DXGI format first), to tell an integer typed view. Co-Authored-By: Bruno Magalhães <19572425+brunofgmag@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01A4bpLmErkufJL3LGgfR561
Title: Shaders: the lowering tables hash with SipHash - one shared Fx hasher takes 16% off the lowering stage
Rebased onto
main940d32f(head21ff7f6). The numbers below are fromffe310e, the base they were measured on, becauselower.rsandstructure.rswere both rewritten upstream since. The map sites upstream added in those files are on the same Fx hasher now - 22 of them inlower.rsalone - otherwise the file would carry two hashers and the claim would only hold for the tables this change knew about. The one place that stays onstd::collectionsisStructured's public fields: they arepub, so a private hasher there is a private type in a public interface, whichclippy -D warningsrejects.cargo test --workspace --lockedpasses, DXIL differential tests included (247 in this crate).Where the time goes
emit/lower.rskeeps thirty tables keyed byValue, a register or a block index:vals,floats,bools,lane_bits,phi_at,def_block,params,descriptors,scalar,exports,interps,handles,edges,ballots,sections... They werestd::collections::HashMap, whose default hasher is SipHash-1-3 with a randomper-process seed: setup, two rounds per word, finalisation.
structure.rshad already noticed this ("The tablesbelow have small integer keys; SipHash dominated run time") and defined an Fx multiply-rotate hasher for its own
tables; the lowering stage, which is the larger half of a translation, never got it.
What changes
crates/victory-shader/src/hash.rs: theFxhasher andMap/Setaliases, moved out ofstructure.rs(same 20 lines of hasher;
structure.rsnow imports it).emit/lower.rs: 26HashMapand 4HashSetfields becomeMap/Set.emit/wave.rs:sections()returns aMap(its caller stores it in one).emit/bench_tests.rs: an#[ignore]d test that builds synthetic GFX10 compute programs - no game data - of256 to 16 384 instructions and times each stage, so this change and the next one are numbers rather than
impressions.
cargo test --release -p victory-shader --lib bench_tests -- --ignored --nocapture.No emitted byte changes: these are lookup tables, and nothing iterates them. The one table whose order reaches the
output (
user_reads, the user-data slots) is aBTreeMapand stays one.Measured
One binary each, the same synthetic programs, ten interleaved rounds per arm (alternating, best of the ten) so the
machine's other work hits both arms:
Nanoseconds per instruction.
ir::build,structure,resourceandioare unchanged, as they should be.Correctness
four digests match between the two arms.
cargo test --workspace --lockedcargo clippy --workspace --all-targets --locked -- -D warningsand the same with--all-featurescargo xtask policyNo game data used anywhere.
Not included
A second experiment,
ir::buildbuilding its per-instructionreads/writesas(Reg, Value)pairs instead ofVec<Reg>then a secondVec(two allocations per instruction saved), measured -12 to -20% onir::buildbutwas a wash over the whole translation, so it is not in this diff. Say the word if you want it measured again on
main.