Shaders: replace HashMap/linear scans in Lower with O(1) dense Vec tables #2210

Open
github-bot wants to merge 1 commit from github-bot/gh-2210 into main AGit
Owner

@⁠RVZO6 on GitHub · 2026-11-25 07:59 UTC · original
RVZO6:rvzo/shader-o1-lookups → main · +248 −48 · 2 files · 1 commits · open

During shader translation of large single-block shaders, Lower and resource::track perform repeated O(N) linear scans per instruction/use — causing O(N²) translation overhead across 60k+ pipeline builds:

  • Lower::def_inst, mask_dword, mask_number, pair_high, and whole hash into def_block: HashMap<Value, usize> and then scan all instructions in block.insts.
  • Lower::has_done_export linearly scans every instruction in a block on demand.
  • resource::track linearly scans out.scalar on every descriptor load lookup.

Claimed on #1633 (https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7155420630).

The change

  • crates/victory-shader/src/emit/lower.rs:
    • Replace def_block: HashMap<Value, usize> with dense Vec tables indexed directly by Value.0: def_block: Vec<Option<usize>> (covering both phis and instructions) and def_site: Vec<Option<Site>> (instruction sites), with bounds-checked def_block(), def_site(), and O(1) def_inst().
    • Update mask_dword, mask_number, pair_high, whole, and def_inst to use O(1) indexed lookups without hashing or scanning block.insts.
    • Precompute done_export: Vec<bool> per IR block once in Lower::new and replace has_done_export's scan with an O(1) lookup (also short-circuiting discarded_at_done).
    • Add tests def_tables_and_done_export_agree_across_phis_pairs_and_exports and large_single_block_shader_with_dead_edge_phis_exports_and_descriptors_lowers.
  • crates/victory-shader/src/resource.rs:
    • Maintain a HashMap<Site, Address> (scalar_at, first-match via entry().or_insert()) alongside out.scalar so descriptor load lookups are O(1).
    • Add test many_scalar_descriptor_loads_and_waterfall_offset_track_cleanly.

Not a duplicate of

No open PR or CLAIMS.md entry replaces Lower's def_block/instruction/done-export scans or resource::track's out.scalar scans with O(1) tables.

Tested

  • Fork windows-latest MSVC + Windows WARP SM 6.6 CI run 45570823602 (Clippy clean, Clippy, diagnostics build clean, Tests: 731 passed; 0 failed; 10 ignored, victory-shader: 262 passed; 0 failed; 3 ignored, xtask: 6 passed; 0 failed):
    • def_tables_and_done_export_agree_across_phis_pairs_and_exports ... ok
    • large_single_block_shader_with_dead_edge_phis_exports_and_descriptors_lowers ... ok
    • resource::tests::many_scalar_descriptor_loads_and_waterfall_offset_track_cleanly ... ok

Not run: the title; any hardware GPU. The title was not used in these tests.

AI-assisted: T3 Code (Antigravity, gemini-4-neon)

**@⁠RVZO6** on GitHub · 2026-11-25 07:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/2210) `RVZO6:rvzo/shader-o1-lookups` → `main` · +248 −48 · 2 files · 1 commits · open During shader translation of large single-block shaders, `Lower` and `resource::track` perform repeated O(N) linear scans per instruction/use — causing O(N²) translation overhead across 60k+ pipeline builds: - `Lower::def_inst`, `mask_dword`, `mask_number`, `pair_high`, and `whole` hash into `def_block: HashMap<Value, usize>` and then scan all instructions in `block.insts`. - `Lower::has_done_export` linearly scans every instruction in a block on demand. - `resource::track` linearly scans `out.scalar` on every descriptor load lookup. Claimed on #1633 (`https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7155420630`). ## The change - `crates/victory-shader/src/emit/lower.rs`: - Replace `def_block: HashMap<Value, usize>` with dense `Vec` tables indexed directly by `Value.0`: `def_block: Vec<Option<usize>>` (covering both phis and instructions) and `def_site: Vec<Option<Site>>` (instruction sites), with bounds-checked `def_block()`, `def_site()`, and O(1) `def_inst()`. - Update `mask_dword`, `mask_number`, `pair_high`, `whole`, and `def_inst` to use O(1) indexed lookups without hashing or scanning `block.insts`. - Precompute `done_export: Vec<bool>` per IR block once in `Lower::new` and replace `has_done_export`'s scan with an O(1) lookup (also short-circuiting `discarded_at_done`). - Add tests `def_tables_and_done_export_agree_across_phis_pairs_and_exports` and `large_single_block_shader_with_dead_edge_phis_exports_and_descriptors_lowers`. - `crates/victory-shader/src/resource.rs`: - Maintain a `HashMap<Site, Address>` (`scalar_at`, first-match via `entry().or_insert()`) alongside `out.scalar` so descriptor load lookups are O(1). - Add test `many_scalar_descriptor_loads_and_waterfall_offset_track_cleanly`. ## Not a duplicate of No open PR or `CLAIMS.md` entry replaces `Lower`'s `def_block`/instruction/done-export scans or `resource::track`'s `out.scalar` scans with O(1) tables. ## Tested - Fork `windows-latest` MSVC + Windows WARP SM 6.6 CI run [`45570823602`](https://github.com/RVZO6/VIctory/actions/runs/45570823602) (`Clippy` clean, `Clippy, diagnostics build` clean, `Tests`: **`731 passed; 0 failed; 10 ignored`**, `victory-shader`: **`262 passed; 0 failed; 3 ignored`**, `xtask`: **`6 passed; 0 failed`**): - `def_tables_and_done_export_agree_across_phis_pairs_and_exports ... ok` - `large_single_block_shader_with_dead_edge_phis_exports_and_descriptors_lowers ... ok` - `resource::tests::many_scalar_descriptor_loads_and_waterfall_offset_track_cleanly ... ok` **Not run:** the title; any hardware GPU. The title was not used in these tests. AI-assisted: T3 Code (Antigravity, gemini-4-neon)
Replace per-lookup HashMap hashing and linear scans over block.insts in
Lower with O(1) dense Vec tables built once in Lower::new (identical DXIL):
- def_block: Vec<Option<usize>> and def_site: Vec<Option<Site>> indexed by
  Value.0, eliminating linear scans in def_inst, pair_high, mask_dword,
  mask_number, and whole.
- done_export: Vec<bool> per block, replacing has_done_export's scan.
(Dropped the earlier resource::track HashMap change after benchmarking
showed SipHash insertion overhead outweighed scanning out.scalar.)

Measured (--release bench_o1_lookups_large_shader, 2,821-inst PS):
- emit::lower: 3,571.7 -> 2,939.8 us/run (1.21x, -17.7%)

Trimmed lower.rs tests to a single equivalence check comparing the new
tables against the old linear scans.

Assisted-by: T3 Code (Antigravity, gemini-4-neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:58:31 +00:00
Author
Owner

@⁠sinan-cmyk on GitHub · 2026-11-25 10:43 UTC · original

Offline measurement on a retail dump

Result: no count change against main.

I merged head ab8256d6 onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

**@⁠sinan-cmyk** on GitHub · 2026-11-25 10:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/2210#issuecomment-7160078153) ### Offline measurement on a retail dump **Result: no count change against `main`.** I merged head ab8256d6 onto `main` 940d32f (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 23,918 on `main`, 23,918 with this PR. No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 13:29 UTC · original · edited 2026-11-26 01:22 UTC

Automated triage for the maintainers.

Merge candidate. Rebased onto current main; it merges cleanly. Only crates/victory-shader/src/emit/lower.rs changes. Lower replaces the def_block HashMap and the per-block instruction scans with dense def_block/def_site tables indexed by Value.0, and has_done_export with a per-block done_export. The new konst caller from main is updated too.

I checked the call sites (mask_dword, mask_number, pair_high, whole, def_inst, konst). For phis they still end in the same result as before, because def_site is None there just as the old instruction find was. lower.rs is 521,527 bytes, under the 512 KiB policy limit. The tests are in the new emit/lower/lookup_tests.rs. The earlier resource.rs HashMap change was dropped, as the author's benchmark suggested.

CI: passing (check).


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 13:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/2210#issuecomment-7163850935) · edited 2026-11-26 01:22 UTC <!-- victory-triage v1 head=8c74b02f685c6c1212484114ac889aa41a6f9f89 verdict=merge-candidate at=2026-11-26T01:22:04Z --> Automated triage for the maintainers. **Merge candidate.** Rebased onto current `main`; it merges cleanly. Only `crates/victory-shader/src/emit/lower.rs` changes. `Lower` replaces the `def_block` HashMap and the per-block instruction scans with dense `def_block`/`def_site` tables indexed by `Value.0`, and `has_done_export` with a per-block `done_export`. The new `konst` caller from `main` is updated too. I checked the call sites (`mask_dword`, `mask_number`, `pair_high`, `whole`, `def_inst`, `konst`). For phis they still end in the same result as before, because `def_site` is `None` there just as the old instruction `find` was. `lower.rs` is 521,527 bytes, under the 512 KiB policy limit. The tests are in the new `emit/lower/lookup_tests.rs`. The earlier `resource.rs` HashMap change was dropped, as the author's benchmark suggested. CI: passing (`check`). --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠RVZO6 on GitHub · 2026-11-25 17:43 UTC · original

Rebased cleanly onto 13f91ca and addressed triage feedback:

  • Dropped the resource.rs scalar_at HashMap change after A/B benchmarking showed SipHash insertion overhead regressed resource::track (163.7 -> 186.1 µs at 256 loads).
  • Kept only the measured lower.rs O(1) dense Vec tables (def_block, def_site, done_export) and trimmed tests to one equivalence check comparing the new tables against the previous linear scans (1 file changed, +229/-43).
  • Measured on 13f91ca (--release emit::lower::tests::bench_o1_lookups_large_shader, 2,821-inst single-block PS):
    • emit::lower: 3,571.7 -> 2,939.8 µs/run (1.21×, -17.7%), identical DXIL output.
  • Verified on windows-latest MSVC + WARP SM 6.6 CI (both clippy -D warnings profiles, xtask 6/6, victory-shader 281/281): https://github.com/RVZO6/VIctory/actions/runs/45666985023
**@⁠RVZO6** on GitHub · 2026-11-25 17:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/2210#issuecomment-7169908589) Rebased cleanly onto `13f91ca` and addressed triage feedback: - **Dropped the `resource.rs` `scalar_at` HashMap change** after A/B benchmarking showed SipHash insertion overhead regressed `resource::track` (`163.7 -> 186.1 µs` at 256 loads). - **Kept only the measured `lower.rs` O(1) dense `Vec` tables** (`def_block`, `def_site`, `done_export`) and trimmed tests to one equivalence check comparing the new tables against the previous linear scans (`1 file changed, +229/-43`). - Measured on `13f91ca` (`--release emit::lower::tests::bench_o1_lookups_large_shader`, 2,821-inst single-block PS): - `emit::lower`: `3,571.7 -> 2,939.8 µs/run` (**1.21×**, `-17.7%`), identical DXIL output. - Verified on `windows-latest` MSVC + WARP SM 6.6 CI (both clippy `-D warnings` profiles, `xtask` 6/6, `victory-shader` 281/281): https://github.com/RVZO6/VIctory/actions/runs/45666985023
Author
Owner

@⁠RVZO6 on GitHub · 2026-11-26 00:21 UTC · original

Rebased onto current main (63bcca4, head 8c74b02). Updated konst's new def_site caller from 63bcca4 and moved unit tests into crates/victory-shader/src/emit/lower/lookup_tests.rs so lower.rs stays below the 512 KiB asset-policy limit. Verified green on windows-latest MSVC + WARP SM 6.6 CI (clippy, xtask policy, full workspace tests): https://github.com/RVZO6/VIctory/actions/runs/45727774760.

**@⁠RVZO6** on GitHub · 2026-11-26 00:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/2210#issuecomment-7178664048) Rebased onto current `main` (`63bcca4`, head `8c74b02`). Updated `konst`'s new `def_site` caller from `63bcca4` and moved unit tests into `crates/victory-shader/src/emit/lower/lookup_tests.rs` so `lower.rs` stays below the 512 KiB asset-policy limit. Verified green on `windows-latest` MSVC + WARP SM 6.6 CI (clippy, xtask policy, full workspace tests): https://github.com/RVZO6/VIctory/actions/runs/45727774760.
Author
Owner

@⁠DenRakEiw on GitHub · 2026-11-25 08:03 UTC · original · approved these changes

Verified locally merged onto origin/main (940d32f, Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo test -p victory-shader --lib emit::lower::tests:: — 23 passed, 0 failed (including def_tables_and_done_export_agree_across_phis_pairs_and_exports and large_single_block_shader_with_dead_edge_phis_exports_and_descriptors_lowers)
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean O(1) def-block/def-site lookup tables eliminating O(N^2) linear scans in Lower and resource::track. Looks great.

AI-assisted (Gemini 4 Neon).

**@⁠DenRakEiw** on GitHub · 2026-11-25 08:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/2210#pullrequestreview-6053840742) · **approved these changes** Verified locally merged onto `origin/main` (`940d32f`, Windows 11 x64, Rust 1.98.1 MSVC): - `cargo test -p victory-shader --lib emit::lower::tests::` — **23 passed, 0 failed** (including `def_tables_and_done_export_agree_across_phis_pairs_and_exports` and `large_single_block_shader_with_dead_edge_phis_exports_and_descriptors_lowers`) - `cargo clippy -p victory-shader --all-targets` — 0 warnings - `cargo xtask policy` — ok (`183 files ok`) Clean O(1) def-block/def-site lookup tables eliminating O(N^2) linear scans in `Lower` and `resource::track`. Looks great. AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠catminou4 on GitHub · 2026-11-25 08:17 UTC · original · approved these changes

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, cargo test -p victory-shader --locked.

147 passed / 0 failed. O(N^2) linear scans eliminated in Lower and resource... (+3 tests) — algorithmic complexity fix in the lowering hot path.

**@⁠catminou4** on GitHub · 2026-11-25 08:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/2210#pullrequestreview-6054007357) · **approved these changes** Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, `cargo test -p victory-shader --locked`. 147 passed / 0 failed. O(N^2) linear scans eliminated in Lower and resource... (+3 tests) — algorithmic complexity fix in the lowering hot path.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 19:19 UTC · original · requested changes

Tested locally merged onto origin/main (63bcca43, Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo test --locked -p victory-shader --lib emit::lower::tests:: — 33 passed, 0 failed
  • cargo test --locked -p victory-shader --lib emit::lower::lookup_tests — 1 passed, 1 ignored (bench_o1_lookups_large_shader)
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default and --all-features) — 0 warnings
  • cargo xtask policy — ok (192 files ok); emit/lower.rs size is 521,527 B (< 524,288 B)

Blocking issue

  • Value(u32::MAX) (ir::NONE) causes an immediate ~160 GiB OOM abort in Lower::new (crates/victory-shader/src/emit/lower.rs:666-692):
    SSA renaming in ir::build (crates/victory-shader/src/ir.rs:207, 217, 274) only renames blocks visited by reverse_postorder(&blocks), leaving unrenamed writes as Value(u32::MAX) (NONE) in any unreachable block (for example, blocks detached after split_irreducible or in a manually constructed Program). Because Lower::new scans every block in p.blocks and computes value_count using v.0 as usize + 1 without filtering Value(u32::MAX), a single Value(u32::MAX) sizes both def_block (Vec<Option<usize>>) and def_site (Vec<Option<Site>>) to 4,294,967,296 entries (~160 GiB on 64-bit, or overflows on 32-bit) and aborts on allocation, whereas the previous HashMap handled unrenamed/sparse values safely.
    Filtering out Value(u32::MAX) (or iterating only reachable blocks / guarding against NONE and sparse IDs) in Lower::new avoids this cleanly.

Minor formatting nit

  • rustfmt --edition 2024 --check crates/victory-shader/src/emit/lower/lookup_tests.rs flags the long import on line 2, double blank line on line 15, and line wrapping in lookup_tests.rs.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 19:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/2210#pullrequestreview-6079454032) · **requested changes** Tested locally merged onto `origin/main` (`63bcca43`, Windows 11 x64, Rust 1.98.1 MSVC): - `cargo test --locked -p victory-shader --lib emit::lower::tests::` — **33 passed, 0 failed** - `cargo test --locked -p victory-shader --lib emit::lower::lookup_tests` — **1 passed, 1 ignored** (`bench_o1_lookups_large_shader`) - `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` (default and `--all-features`) — 0 warnings - `cargo xtask policy` — ok (`192 files ok`); `emit/lower.rs` size is **521,527 B** (< 524,288 B) ### Blocking issue - **`Value(u32::MAX)` (`ir::NONE`) causes an immediate ~160 GiB OOM abort in `Lower::new` (`crates/victory-shader/src/emit/lower.rs:666-692`):** SSA renaming in `ir::build` (`crates/victory-shader/src/ir.rs:207, 217, 274`) only renames blocks visited by `reverse_postorder(&blocks)`, leaving unrenamed writes as `Value(u32::MAX)` (`NONE`) in any unreachable block (for example, blocks detached after `split_irreducible` or in a manually constructed `Program`). Because `Lower::new` scans every block in `p.blocks` and computes `value_count` using `v.0 as usize + 1` without filtering `Value(u32::MAX)`, a single `Value(u32::MAX)` sizes both `def_block` (`Vec<Option<usize>>`) and `def_site` (`Vec<Option<Site>>`) to `4,294,967,296` entries (~160 GiB on 64-bit, or overflows on 32-bit) and aborts on allocation, whereas the previous `HashMap` handled unrenamed/sparse values safely. Filtering out `Value(u32::MAX)` (or iterating only reachable blocks / guarding against `NONE` and sparse IDs) in `Lower::new` avoids this cleanly. ### Minor formatting nit - `rustfmt --edition 2024 --check crates/victory-shader/src/emit/lower/lookup_tests.rs` flags the long import on line 2, double blank line on line 15, and line wrapping in `lookup_tests.rs`.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2210/head:github-bot/gh-2210'
git switch 'github-bot/gh-2210'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2210'
git switch 'github-bot/gh-2210'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2210'
git switch 'github-bot/gh-2210'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2210'
git switch 'main'
git merge --squash 'github-bot/gh-2210'
git switch 'main'
git merge --ff-only 'github-bot/gh-2210'
git switch 'main'
git merge 'github-bot/gh-2210'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2210
No description provided.