Shaders: replace HashMap/linear scans in Lower with O(1) dense Vec tables #2208
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2210"
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?
Imported from Marvisak/VIctory. Originally posted by RVZO6 on 2026-11-25T07:59:32Z.
During shader translation of large single-block shaders,
Lowerandresource::trackperform 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, andwholehash intodef_block: HashMap<Value, usize>and then scan all instructions inblock.insts.Lower::has_done_exportlinearly scans every instruction in a block on demand.resource::tracklinearly scansout.scalaron 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:def_block: HashMap<Value, usize>with denseVectables indexed directly byValue.0:def_block: Vec<Option<usize>>(covering both phis and instructions) anddef_site: Vec<Option<Site>>(instruction sites), with bounds-checkeddef_block(),def_site(), and O(1)def_inst().mask_dword,mask_number,pair_high,whole, anddef_instto use O(1) indexed lookups without hashing or scanningblock.insts.done_export: Vec<bool>per IR block once inLower::newand replacehas_done_export's scan with an O(1) lookup (also short-circuitingdiscarded_at_done).def_tables_and_done_export_agree_across_phis_pairs_and_exportsandlarge_single_block_shader_with_dead_edge_phis_exports_and_descriptors_lowers.crates/victory-shader/src/resource.rs:HashMap<Site, Address>(scalar_at, first-match viaentry().or_insert()) alongsideout.scalarso descriptor load lookups are O(1).many_scalar_descriptor_loads_and_waterfall_offset_track_cleanly.Not a duplicate of
No open PR or
CLAIMS.mdentry replacesLower'sdef_block/instruction/done-export scans orresource::track'sout.scalarscans with O(1) tables.Tested
windows-latestMSVC + Windows WARP SM 6.6 CI run45570823602(Clippyclean,Clippy, diagnostics buildclean,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 ... oklarge_single_block_shader_with_dead_edge_phis_exports_and_descriptors_lowers ... okresource::tests::many_scalar_descriptor_loads_and_waterfall_offset_track_cleanly ... okNot run: the title; any hardware GPU. The title was not used in these tests.
AI-assisted: T3 Code (Antigravity, gemini-4-neon)
Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T08:03:29Z.
Review: APPROVED
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 (includingdef_tables_and_done_export_agree_across_phis_pairs_and_exportsandlarge_single_block_shader_with_dead_edge_phis_exports_and_descriptors_lowers)cargo clippy -p victory-shader --all-targets— 0 warningscargo xtask policy— ok (183 files ok)Clean O(1) def-block/def-site lookup tables eliminating O(N^2) linear scans in
Lowerandresource::track. Looks great.AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T08:17:47Z.
Review: APPROVED
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.
Imported from Marvisak/VIctory. Originally posted by sinan-cmyk on 2026-11-25T10:43:05Z.
Offline measurement on a retail dump
Result: no count change against
main.I merged head
ab8256d6ontomain940d32f(clean merge) and ranvictory translate --validate --listonshaders.rpf(1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result withmainprogram by program (25,889 programs). Validated: 23,918 onmain, 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).
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T13:29:24Z.
Automated triage for the maintainers.
Merge candidate. Rebased onto current
main; it merges cleanly. Onlycrates/victory-shader/src/emit/lower.rschanges.Lowerreplaces thedef_blockHashMap and the per-block instruction scans with densedef_block/def_sitetables indexed byValue.0, andhas_done_exportwith a per-blockdone_export. The newkonstcaller frommainis 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, becausedef_siteisNonethere just as the old instructionfindwas.lower.rsis 521,527 bytes, under the 512 KiB policy limit. The tests are in the newemit/lower/lookup_tests.rs. The earlierresource.rsHashMap change was dropped, as the author's benchmark suggested.CI: passing (
check).Generated by Claude Code
[PR #2210] Shaders: eliminate O(N^2) linear scans in Lower and resource::trackto [PR #2210] Shaders: replace HashMap/linear scans in Lower with O(1) dense Vec tablesImported from Marvisak/VIctory. Originally posted by RVZO6 on 2026-11-25T17:43:27Z.
Rebased cleanly onto
13f91caand addressed triage feedback:resource.rsscalar_atHashMap change after A/B benchmarking showed SipHash insertion overhead regressedresource::track(163.7 -> 186.1 µsat 256 loads).lower.rsO(1) denseVectables (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).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.windows-latestMSVC + WARP SM 6.6 CI (both clippy-D warningsprofiles,xtask6/6,victory-shader281/281): https://github.com/RVZO6/VIctory/actions/runs/45666985023Imported from Marvisak/VIctory. Originally posted by RVZO6 on 2026-11-26T00:21:06Z.
Rebased onto current
main(63bcca4, head8c74b02). Updatedkonst's newdef_sitecaller from63bcca4and moved unit tests intocrates/victory-shader/src/emit/lower/lookup_tests.rssolower.rsstays below the 512 KiB asset-policy limit. Verified green onwindows-latestMSVC + WARP SM 6.6 CI (clippy, xtask policy, full workspace tests): https://github.com/RVZO6/VIctory/actions/runs/45727774760.Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T19:19:51Z.
Review: CHANGES_REQUESTED
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 failedcargo 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 warningscargo xtask policy— ok (192 files ok);emit/lower.rssize is 521,527 B (< 524,288 B)Blocking issue
Value(u32::MAX)(ir::NONE) causes an immediate ~160 GiB OOM abort inLower::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 byreverse_postorder(&blocks), leaving unrenamed writes asValue(u32::MAX)(NONE) in any unreachable block (for example, blocks detached aftersplit_irreducibleor in a manually constructedProgram). BecauseLower::newscans every block inp.blocksand computesvalue_countusingv.0 as usize + 1without filteringValue(u32::MAX), a singleValue(u32::MAX)sizes bothdef_block(Vec<Option<usize>>) anddef_site(Vec<Option<Site>>) to4,294,967,296entries (~160 GiB on 64-bit, or overflows on 32-bit) and aborts on allocation, whereas the previousHashMaphandled unrenamed/sparse values safely.Filtering out
Value(u32::MAX)(or iterating only reachable blocks / guarding againstNONEand sparse IDs) inLower::newavoids this cleanly.Minor formatting nit
rustfmt --edition 2024 --check crates/victory-shader/src/emit/lower/lookup_tests.rsflags the long import on line 2, double blank line on line 15, and line wrapping inlookup_tests.rs.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.