Shaders: replace HashMap/linear scans in Lower with O(1) dense Vec tables #2210
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!2210
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-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?
@RVZO6 on GitHub · 2026-11-25 07:59 UTC · original
RVZO6:rvzo/shader-o1-lookups→main· +248 −48 · 2 files · 1 commits · openDuring 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)
@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
main940d32f(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).
@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. 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
@RVZO6 on GitHub · 2026-11-25 17:43 UTC · original
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/45666985023@RVZO6 on GitHub · 2026-11-26 00:21 UTC · original
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.@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 (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).
@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.
@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 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.