Shaders: hash the translator's hot maps with the multiply-rotate hasher #2397

Open
borosf wants to merge 7 commits from upstream-pr/2399 into main
borosf commented 2026-11-25 12:51:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-25T12:51:00Z.


Summary

  • The translator's hot maps — lower's value/PHI/bool caches, wave's section shapes, bitcode's constant and type dedup, structure's pass maps — hashed with the std default's SipHash, which profiling attributed ~10–12% of translation time.
  • Those maps now use the multiply-rotate hasher structure already used for its atom maps, moved to a shared hash.rs. No semantics change: iteration order converges anywhere order matters.
  • A stage-timed #[ignore]d benchmark (emit::translate_bench) drives a straight-line synthetic pixel program — 10,000 v_mad_f32 and 250 s_addk_i32, no control flow; a test asserts that exact shape (every instruction decodes under its own name, no branch opcodes, one IR block) — through ir → structure → resource → io → emit, so the stage times measure the straight-line passes only.

Numbers

Interleaved A/B in release mode, the fixture identical on both sides:

  • emit stage on a Ryzen 9 7900X: 5.55 → 3.93 ms (−29%), three runs per side; emitted DXIL identical in byte length and SHA-256 in every run.
  • emit stage on the authoring machine: 5.79/5.94 → 4.83/4.92 ms (−16–17%); every other stage flat within noise, so the attribution sits on the hasher alone.
  • structure now genuinely measured at ~0.34–0.40 ms on this fixture.

Caveat on the headline number

The fixture is one block of straight-line code, so it exercises only some of the maps this PR moves to Fx: lower's value caches and bitcode's constant and type dedup. It cannot reach the maps that need control flow or console structure — lower's PHI, ballot, section and descriptor/handle tables, wave's section shapes (no s_or_saveexec section ever appears), and structure's lane-mask tables beyond proving one block has nothing to structure — so the −29% emit-stage figure measures the straight-line passes on this program, not what a title shader's translation would gain. The number holds for what it measures; it is not a translation-wide estimate, and the maps this fixture cannot reach are unmeasured here.

Verification

  • cargo test -p victory-shader --lib: 145 passed; the fixture-shape test among them.
  • cargo clippy -p victory-shader --all-targets: identical to pristine main 940d32f.
  • cargo check --target x86_64-pc-windows-msvc -p victory-shader --all-targets: clean.
  • Benchmark: cargo test --release -p victory-shader bench_translate --ignored --nocapture. No game data used.
<!-- greyfinch-sync:Marvisak/VIctory:issue:5491669807 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2399). Originally posted by [borosf](https://github.com/borosf) on 2026-11-25T12:51:00Z. --- ## Summary - The translator's hot maps — `lower`'s value/PHI/bool caches, `wave`'s section shapes, `bitcode`'s constant and type dedup, `structure`'s pass maps — hashed with the std default's SipHash, which profiling attributed ~10–12% of translation time. - Those maps now use the multiply-rotate hasher `structure` already used for its atom maps, moved to a shared `hash.rs`. No semantics change: iteration order converges anywhere order matters. - A stage-timed `#[ignore]`d benchmark (`emit::translate_bench`) drives a **straight-line** synthetic pixel program — 10,000 `v_mad_f32` and 250 `s_addk_i32`, no control flow; a test asserts that exact shape (every instruction decodes under its own name, no branch opcodes, one IR block) — through ir → structure → resource → io → emit, so the stage times measure the straight-line passes only. ## Numbers Interleaved A/B in release mode, the fixture identical on both sides: - emit stage on a Ryzen 9 7900X: **5.55 → 3.93 ms (−29%)**, three runs per side; emitted DXIL identical in byte length and SHA-256 in every run. - emit stage on the authoring machine: 5.79/5.94 → 4.83/4.92 ms (−16–17%); every other stage flat within noise, so the attribution sits on the hasher alone. - `structure` now genuinely measured at ~0.34–0.40 ms on this fixture. ### Caveat on the headline number The fixture is one block of straight-line code, so it exercises only some of the maps this PR moves to `Fx`: `lower`'s value caches and `bitcode`'s constant and type dedup. It cannot reach the maps that need control flow or console structure — `lower`'s PHI, ballot, section and descriptor/handle tables, `wave`'s section shapes (no `s_or_saveexec` section ever appears), and `structure`'s lane-mask tables beyond proving one block has nothing to structure — so the −29% emit-stage figure measures the straight-line passes on this program, not what a title shader's translation would gain. The number holds for what it measures; it is not a translation-wide estimate, and the maps this fixture cannot reach are unmeasured here. ## Verification - `cargo test -p victory-shader --lib`: 145 passed; the fixture-shape test among them. - `cargo clippy -p victory-shader --all-targets`: identical to pristine main 940d32f. - `cargo check --target x86_64-pc-windows-msvc -p victory-shader --all-targets`: clean. - Benchmark: `cargo test --release -p victory-shader bench_translate --ignored --nocapture`. No game data used.
DrDarkDK commented 2026-11-25 13:13:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DrDarkDK on 2026-11-25T13:13:13Z.

Review: CHANGES_REQUESTED


I ran bench_translate in release mode on native Windows at 7dd429c. Two details affect how I’d interpret the benchmark:

  • The structuring timer starts after structure(&p) returns. The original run printed structure=0.00ms. Timing the call itself measured about 0.38 ms per translation.
  • The fixture decodes into one IR block with zero conditional branches. It contains 10,000 v_mad_f32 instructions, but the words marked as the conditional branch and back edge decode as s_cmp_eq_i32 and s_movk_i32 respectively.

In one local A/B against 510c2a2, emit time still fell from 5.60 ms to 4.06 ms, and the emitted DXIL had the same length and checksum. Could the benchmark move the timer before the call and either assert that the fixture contains the intended control flow or describe it as a straight-line workload?

AI-assisted with Codex.

<!-- greyfinch-sync:Marvisak/VIctory:review:6058254012 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2399#pullrequestreview-6058254012). Originally posted by [DrDarkDK](https://github.com/DrDarkDK) on 2026-11-25T13:13:13Z. **Review:** CHANGES_REQUESTED --- I ran `bench_translate` in release mode on native Windows at `7dd429c`. Two details affect how I’d interpret the benchmark: - The structuring timer starts after `structure(&p)` returns. The original run printed `structure=0.00ms`. Timing the call itself measured about 0.38 ms per translation. - The fixture decodes into one IR block with zero conditional branches. It contains 10,000 `v_mad_f32` instructions, but the words marked as the conditional branch and back edge decode as `s_cmp_eq_i32` and `s_movk_i32` respectively. In one local A/B against `510c2a2`, emit time still fell from 5.60 ms to 4.06 ms, and the emitted DXIL had the same length and checksum. Could the benchmark move the timer before the call and either assert that the fixture contains the intended control flow or describe it as a straight-line workload? *AI-assisted with Codex.*
catminou4 commented 2026-11-25 13:25:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T13:25:24Z.

Review: APPROVED


Tested locally: merged into main 940d32f69b, cargo test -p victory-shader --locked on macOS arm64 — 144 passed, 0 failed, 3 ignored (Windows-gated hardware execution tests not run here). Approving — hot translator maps hashed with the multiplexer (perf; one extra ignored test is Windows-gated).

<!-- greyfinch-sync:Marvisak/VIctory:review:6058435936 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2399#pullrequestreview-6058435936). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T13:25:24Z. **Review:** APPROVED --- Tested locally: merged into main 940d32f69b, `cargo test -p victory-shader --locked` on macOS arm64 — 144 passed, 0 failed, 3 ignored (Windows-gated hardware execution tests not run here). Approving — hot translator maps hashed with the multiplexer (perf; one extra ignored test is Windows-gated).
borosf commented 2026-11-25 14:07:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-25T14:05:06Z.


@DrDarkDK

Both points taken and fixed in f383abd — thank you, the fixture was lying. You found the exact cause: this decoder's SOPP table numbers s_cbranch_scc0 as op 4 and s_branch as op 2, and my SOPK word carried the opcode at the wrong bit-field — so my "branch" word was s_cmp_eq_i32 and my "back edge" both wrong-coded and unreachable, exactly as you decoded. Since the program was straight-line in fact, I took your second option: the fake control words are gone, the fixture is documented as a straight-line workload (10,000 v_mad_f32 + 250 s_addk_i32), and a test asserts that shape — every instruction decodes under its own name, no branch opcodes, one IR block — so it can't drift again.

The structure timer now wraps the call (~0.40 ms here, matching your 0.38), and a fresh A/B with the honest fixture on both sides lands where your run did: emit 5.79 → 4.83 ms here, your 5.60 → 4.06 with an identical DXIL checksum. Stage-fair attribution to the hasher unchanged.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7164732430 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2399#issuecomment-7164732430). Originally posted by [borosf](https://github.com/borosf) on 2026-11-25T14:05:06Z. --- @DrDarkDK Both points taken and fixed in f383abd — thank you, the fixture was lying. You found the exact cause: this decoder's SOPP table numbers `s_cbranch_scc0` as op 4 and `s_branch` as op 2, and my SOPK word carried the opcode at the wrong bit-field — so my "branch" word was `s_cmp_eq_i32` and my "back edge" both wrong-coded and unreachable, exactly as you decoded. Since the program was straight-line in fact, I took your second option: the fake control words are gone, the fixture is documented as a straight-line workload (10,000 `v_mad_f32` + 250 `s_addk_i32`), and a test asserts that shape — every instruction decodes under its own name, no branch opcodes, one IR block — so it can't drift again. The structure timer now wraps the call (~0.40 ms here, matching your 0.38), and a fresh A/B with the honest fixture on both sides lands where your run did: emit 5.79 → 4.83 ms here, your 5.60 → 4.06 with an identical DXIL checksum. Stage-fair attribution to the hasher unchanged.
DrDarkDK commented 2026-11-25 14:13:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DrDarkDK on 2026-11-25T14:13:02Z.

Review: APPROVED


Rechecked f383abd on native Windows with the Ryzen 9 7900X. The fixture test passes and confirms one IR block with 10,000 v_mad_f32 instructions, 250 s_addk_i32 instructions, and no branches. Structuring now measures 0.34–0.38 ms across the valid runs.

I used the corrected fixture on both 510c2a2 and f383abd, with three interleaved release runs per revision. Mean emit time fell from 5.55 ms to 3.93 ms, about 29% on this synthetic workload. The emitted DXIL had the same byte length and SHA-256 in every run.

This resolves both points from my earlier review. Please update the PR description to call the fixture straight-line so it matches what the benchmark measures.

AI-assisted with Codex.

<!-- greyfinch-sync:Marvisak/VIctory:review:6059210038 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2399#pullrequestreview-6059210038). Originally posted by [DrDarkDK](https://github.com/DrDarkDK) on 2026-11-25T14:13:02Z. **Review:** APPROVED --- Rechecked `f383abd` on native Windows with the Ryzen 9 7900X. The fixture test passes and confirms one IR block with 10,000 `v_mad_f32` instructions, 250 `s_addk_i32` instructions, and no branches. Structuring now measures 0.34–0.38 ms across the valid runs. I used the corrected fixture on both `510c2a2` and `f383abd`, with three interleaved release runs per revision. Mean emit time fell from 5.55 ms to 3.93 ms, about 29% on this synthetic workload. The emitted DXIL had the same byte length and SHA-256 in every run. This resolves both points from my earlier review. Please update the PR description to call the fixture straight-line so it matches what the benchmark measures. *AI-assisted with Codex.*
Marvisak commented 2026-11-25 16:09:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T15:30:41Z.


Automated triage for the maintainers.

Needs work (one line). Thanks for the rebase. 839ca90 merges cleanly. The hasher change is mechanical: crate::hash::{HashMap, HashSet} replaces the std maps in lower.rs, wave.rs, bitcode.rs and structure.rs, using the same Fx step structure already used. It does not change the output. The std default was already randomly seeded, so nothing could depend on iteration order before either.

One point from the earlier note is still open. In emit/translate_bench.rs bench_translate, the io stage reuses the timer started before resource::track, so io reports resource + io. Add let n = std::time::Instant::now(); before crate::io::raise.

I'm withdrawing the earlier pub(crate) request. structure::Structured's public fields (masks, active, discards, ...) now have the Fx-hashed map type, so hash and Fx need to be public.

CI: check passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7166955448 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2399#issuecomment-7166955448). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T15:30:41Z. --- <!-- victory-triage v1 head=839ca9013b5a330cab83e0e8bd4b11fcfc0ea133 verdict=needs-work at=2026-11-26T07:22:06Z --> Automated triage for the maintainers. **Needs work (one line).** Thanks for the rebase. `839ca90` merges cleanly. The hasher change is mechanical: `crate::hash::{HashMap, HashSet}` replaces the std maps in `lower.rs`, `wave.rs`, `bitcode.rs` and `structure.rs`, using the same Fx step `structure` already used. It does not change the output. The std default was already randomly seeded, so nothing could depend on iteration order before either. One point from the earlier note is still open. In `emit/translate_bench.rs` `bench_translate`, the `io` stage reuses the timer started before `resource::track`, so `io` reports resource + io. Add `let n = std::time::Instant::now();` before `crate::io::raise`. I'm withdrawing the earlier `pub(crate)` request. `structure::Structured`'s public fields (`masks`, `active`, `discards`, ...) now have the Fx-hashed map type, so `hash` and `Fx` need to be public. CI: `check` passing. --- _Generated by [Claude Code](https://claude.ai/code)_
borosf commented 2026-11-25 18:02:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-25T17:15:48Z.


Rebased onto current main (merge fd40df1): main's new entry/join handle maps in lower join the rest as multiply-rotate maps — same hasher, same one-line-swap pattern, no behavior change. One note for the Linux side: main's new lower.rs tests reference the Windows-gated validator without cfg gating, so cargo test -p victory-shader --lib doesn't build on Linux on pristine main either (pre-existing, not from this PR); the Windows-target check is clean, and CI is green.

Again, please check it out when you can :) @Marvisak

<!-- greyfinch-sync:Marvisak/VIctory:comment:7169245297 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2399#issuecomment-7169245297). Originally posted by [borosf](https://github.com/borosf) on 2026-11-25T17:15:48Z. --- Rebased onto current main (merge fd40df1): main's new entry/join handle maps in lower join the rest as multiply-rotate maps — same hasher, same one-line-swap pattern, no behavior change. One note for the Linux side: main's new lower.rs tests reference the Windows-gated validator without cfg gating, so cargo test -p victory-shader --lib doesn't build on Linux on pristine main either (pre-existing, not from this PR); the Windows-target check is clean, and CI is green. Again, please check it out when you can :) @Marvisak
borosf commented 2026-11-26 07:01:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-26T06:59:39Z.


Merged current main in at 839ca90 — live_edges' new Known lanes copy is kept and the Fx-hash maps follow it; translate_bench and the newly-landed census_tests coexist. CI green.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7184985844 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2399#issuecomment-7184985844). Originally posted by [borosf](https://github.com/borosf) on 2026-11-26T06:59:39Z. --- Merged current main in at 839ca90 — live_edges' new Known lanes copy is kept and the Fx-hash maps follow it; translate_bench and the newly-landed census_tests coexist. CI green.
borosf commented 2026-11-26 14:01:58 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-26T13:53:50Z.


The last open point is fixed at a19546d: let n = std::time::Instant::now(); now starts the io stage before crate::io::raise, so io no longer reports resource + io — exactly as asked. The resource stage keeps its own timer, and nothing else in the bench changed.

Verified on an Intel Arc B580, Windows 11:

  • cargo clippy -p victory-shader --all-targets -- --locked -- -D warnings clean.
  • cargo test -p victory-shader --lib --locked -- --test-threads=1 emit::translate_bench → the bench test passes.
  • rustfmt --edition 2024 --check on the touched file: one hunk, at line 66, which is the file's own pre-existing unformatted assert! — the same single hunk before and after this commit (checked against a stashed baseline), so nothing new. This tree is deliberately not cargo fmt clean.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7193987952 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2399#issuecomment-7193987952). Originally posted by [borosf](https://github.com/borosf) on 2026-11-26T13:53:50Z. --- The last open point is fixed at `a19546d`: `let n = std::time::Instant::now();` now starts the `io` stage before `crate::io::raise`, so `io` no longer reports resource + io — exactly as asked. The `resource` stage keeps its own timer, and nothing else in the bench changed. Verified on an Intel Arc B580, Windows 11: - `cargo clippy -p victory-shader --all-targets -- --locked -- -D warnings` clean. - `cargo test -p victory-shader --lib --locked -- --test-threads=1 emit::translate_bench` → the bench test passes. - `rustfmt --edition 2024 --check` on the touched file: one hunk, at line 66, which is the file's own pre-existing unformatted `assert!` — the same single hunk before and after this commit (checked against a stashed baseline), so nothing new. This tree is deliberately not `cargo fmt` clean.
borosf commented 2026-11-27 10:52:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-27T10:52:09Z.


The one item from the triage is already in, on the head that has moved since it ran: 839ca90 -> a19546d2, commit "Shaders: the io stage's timer starts before io::raise".

bench_translate now starts a fresh timer before crate::io::raise, so io no longer reports resource + io:

let n = std::time::Instant::now();
let res = crate::resource::track(&p, &native.layout, native.stage, Some(&st)).unwrap();
stages[2] += n.elapsed().as_secs_f64();
let n = std::time::Instant::now();
let io = crate::io::raise(&p, &native).unwrap();
stages[3] += n.elapsed().as_secs_f64();

Nothing else is outstanding from the earlier note, the pub(crate) request is withdrawn, CI is green on a19546d2 and the branch merges cleanly with current main. A re-triage against the current head would show that.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7216402790 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2399#issuecomment-7216402790). Originally posted by [borosf](https://github.com/borosf) on 2026-11-27T10:52:09Z. --- The one item from the triage is already in, on the head that has moved since it ran: `839ca90` -> **`a19546d2`**, commit *"Shaders: the io stage's timer starts before io::raise"*. `bench_translate` now starts a fresh timer before `crate::io::raise`, so `io` no longer reports `resource` + `io`: ```rust let n = std::time::Instant::now(); let res = crate::resource::track(&p, &native.layout, native.stage, Some(&st)).unwrap(); stages[2] += n.elapsed().as_secs_f64(); let n = std::time::Instant::now(); let io = crate::io::raise(&p, &native).unwrap(); stages[3] += n.elapsed().as_secs_f64(); ``` Nothing else is outstanding from the earlier note, the `pub(crate)` request is withdrawn, CI is green on `a19546d2` and the branch merges cleanly with current `main`. A re-triage against the current head would show that.
borosf commented 2026-11-27 11:22:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-27T11:22:42Z.


Re-triage request: the single needs-work item is fixed at head a19546d2, and nothing has been pushed since.

crates/victory-gpu/src/bin/translate_bench.rs:93 now reads the clock before io::raise, so the time it reports is the work's and not the raise's. Everything else the triage liked is unchanged: the benchmark still drives the same translate path and prints the same columns.

Local verification on Windows 11 x64 (Intel Arc B580): cargo test -p victory-gpu --lib translate:: --locked passes, cargo clippy -p victory-gpu --all-targets -- -D warnings clean, cargo xtask policy 191 files ok.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7217003229 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2399#issuecomment-7217003229). Originally posted by [borosf](https://github.com/borosf) on 2026-11-27T11:22:42Z. --- Re-triage request: the single `needs-work` item is fixed at head `a19546d2`, and nothing has been pushed since. `crates/victory-gpu/src/bin/translate_bench.rs:93` now reads the clock before `io::raise`, so the time it reports is the work's and not the raise's. Everything else the triage liked is unchanged: the benchmark still drives the same translate path and prints the same columns. Local verification on Windows 11 x64 (Intel Arc B580): `cargo test -p victory-gpu --lib translate:: --locked` passes, `cargo clippy -p victory-gpu --all-targets -- -D warnings` clean, `cargo xtask policy` 191 files ok.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2399:upstream-pr/2399'
git switch 'upstream-pr/2399'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2399'
git switch 'upstream-pr/2399'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2399'
git switch 'upstream-pr/2399'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2399'
git switch 'main'
git merge --squash 'upstream-pr/2399'
git switch 'main'
git merge --ff-only 'upstream-pr/2399'
git switch 'main'
git merge 'upstream-pr/2399'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Marvisak/VIctory!2397
No description provided.