victory-shader: hash the lowering tables with Fx instead of SipHash (lowering -16%) #1002
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!1002
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1002/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@pes7 on GitHub · 2026-11-24 14:52 UTC · original
pes7:perf/shader-fx-hash→main· +446 −127 · 7 files · 3 commits · closed without mergingTitle: 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.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.@Zipettte on GitHub · 2026-11-24 15:12 UTC · original
Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 0e33bfe:
cargo test -p victory-shaderpasses, 120 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).Tested with AI assistance (Claude).
@pes7 on GitHub · 2026-11-24 16:08 UTC · original
Independent follow-up, review them in any order: #1033 (one file, +25/-25) removes two of the four allocations
ir::buildmakes per instruction -ir::build-13%, whole translation -4.7%, byte-identical output. It does not touch anything in this diff.@pes7 on GitHub · 2026-11-24 18:30 UTC · original
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
mainffe310evs the three applied:mainlowerresourcestructureir::buildmain-> all threeSo about -15% per translation together, and the emitted DXIL is byte-identical at all four sizes.
decodeis the one stage that pays: it resolves the name it used to be spared (#1180), 110 -> 113 at 16k, and growsInstructionby two words. That is inside the run-to-run spread here, but it is the honest trade.The remaining profile after all three, per instruction at 16k:
lower419 (48%), SSA construction 259,decode113,structure34,io25,resource15. The next thing worth attacking is the lowering dispatcher (amatchon ~250 opcode-name string arms), which is a structural change, not a one-liner.@pes7 on GitHub · 2026-11-24 18:31 UTC · original
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:
0e33bfeto74e2dbf. The only difference is a second commit that adds thegfx10::decodecolumn to the bench (emit/bench_tests.rs); no production code changed, so your result stands for the current head too.valu_testshalf of the suite, which you also ran green.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)
lowergoes 593 -> 514 at 256 instructions and the whole translation 1002 -> 912;mainon the same binary pair is the "before" column. A second CPU would tell us whether that holds or it is a one-machine result - useful either way.@pes7 on GitHub · 2026-11-24 18:47 UTC · original
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:
gfx10::decodeir::build(includes its own decode)structure+resource+io+lowerAnd the split inside
ir::build, timed by marks:decode110, then fields + effects + push = ~285 ns per instruction, withlink,liveness, SSA renaming and phi cleanup together under 20 ns. So the phase that looks like "SSA" in the table is almost entirely allocation.At ~35 ns an allocation, 8 of them is ~280 ns - the whole of that phase. The lists are 1-4 elements each:
Instruction::words: Vec<u32>, a heap vector for an instruction that is usually one dword;Fields::Sop.src,Fields::Smem.data,Fields::Mubuf.addr,Fields::Valu.src:Vec<Operand>;Inst::reads/Inst::writes:Vec<(Reg, Value)>.That is where the remaining ~25-30% of a translation sits, and it is a storage change (
SmallVec-style inline arrays, or one arena per program with(start, len)inInst), not an algorithm. It also changes public types ingfx10andir, so it does not belong in these three PRs - I would rather do it as its own change with this measurement as the target.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.
victory translate --listreports the total for the pack; that is what to multiply against.Happy to take it if the direction sounds right, or to leave it here.
@pes7 on GitHub · 2026-11-24 22:03 UTC · original
Rebased onto today's
main(70564ae), which rewrote much ofemit/lower.rs(+3 300 lines: BVH lowering, thelow16table, a changedhalf_outputs): five conflicts, resolved by keeping the upstream structure and putting the maps on the Fx hasher.Two things worth knowing about the new head (
b6db985):HashMap/HashSetsites (the whole-file constants analysis, liveness,touched/konst/can_be). They are oncrate::hash::{Map, Set}now too, otherwise the file would have two hashers and the claim would only hold for the tables this PR knew about;mainis +420/-103, larger than the original change because it has to carry those sites too.The measured numbers in the description are from the pre-rebase base (
ffe310e), wherelower593 -> 514 and the whole translation 1 002 -> 912 ns per instruction.structure.rsandlower.rshave both changed a lot since, so treat them as the number for the change, not for today's tree - I can re-run the same bench on70564aeagainst amainbuild if anyone wants the current figure before merging.cargo test -p victory-shader --locked: 209 passed, 0 failed (DXIL differential tests included, so the output is still byte-identical). Both clippy configurations with-D warningspass, as doescargo xtask policy.@Marvisak on GitHub · 2026-11-25 01:17 UTC · original · edited 2026-11-25 16:00 UTC
Automated triage for the maintainers.
This pull request no longer merges cleanly into
main(conflicts incrates/victory-shader/src/emit/lower.rs). It will be reviewed once it is rebased onto the currentmain. The earlier verdict (merge-candidate) was for an older state ofmain.Generated by Claude Code
@pes7 on GitHub · 2026-11-25 16:26 UTC · original
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
shaders.rpfof the Xbox build (26 503 containers, 26 084 programs, 40 601 701 IR instructions),victory translate <shaders.rpf> --list. The two binaries ran at the same time, one thread each (--jobs 1) on a 32-core machine, so they shared identical conditions and the difference between them is the change rather than the machine's load:main940d32f21ff7f6-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
#[ignore]d bench measuresloweron synthetic straight-line and loop programs, where the stage is a large share of the translation and the hash tables are hot. A real program averages 1 557 instructions with loads, stores, control flow and resource analysis around it, solower's hashing is a much smaller part of the 25 ms a program takes - small enough that -16% of it is invisible.What the corpus does confirm, and this part is worth having on the record: the translation output is identical to
mainon all 26 084 programs - every count in the summary (containers, programs, structured, resources by kind, DXIL per stage, eachemit:/resources:line) matches, and the only differences between the two--listoutputs are the(e.g. ...)examples, which are picked by whichever thread finishes first.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 (
FIND-1is the arena change, which is a different and larger lever). Say the word if you want it reopened with this measurement in the description instead.Pull request closed