Shaders: live phi edges look up the earlier condition sets they cover #2864
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!2864
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2864"
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?
@Anzymanz on GitHub · 2026-11-25 22:39 UTC · original
Anzymanz:live-edges-subset-index→main· +172 −37 · 3 files · 2 commits · openFrom a tester working with Claude (AI-assisted: Claude Code, Claude Opus 5.5 found the hot spot, wrote the change and ran the measurements; I directed and checked it).
The problem
Some programs take tens of seconds to translate, nearly all of it in
Lower::live_edges. On13f91caone program took 527 s insidetranslate --cacheunder Proton (the warm-up sat at 26,502 of 26,503 containers for minutes and looked hung); #2062 made each step cheaper, but the shape is still quadratic.live_edgesskips a phi reached with a set of conditions when an earlier set of that phi is a subset of it. It finds one by scanning every earlier set of the phi, and in programs with a lot of control flow a phi is reached with very many sets (up to about 24,000 on one phi in the three slow programs below), so the scan grows with the square of them. A profile of one such program (Linuxperf, release build with debug info) put 92% of its translation time in that scan (fill_phis→live_edges→slice::contains).The change
One function,
live_edgesinemit/lower.rs:MAX_KNOWN(4) conditions, so it has at most 16 subsets. Once a phi has 16 earlier sets, they are also kept in an index as sets (sorted, without repeats), and each subset of the new set is looked up there instead of scanning.Knowngetsset()(sorted, without repeats) andsubsets();nonebecomes theNONEconstant they share.The walk takes the same decisions (an earlier set is a subset of the new one exactly when one of its subsets is in the index), so the live edges are the same.
Measurements
All on the title's shader pack (1.0.2.1, my own copy), 25,889 distinct containers, each through
emit::translate_withfrom a small harness outside the tree (native Linux, release build),mainf3578e3against this branch. No game data here, only counts, times and hashes.Output: for every one of the 25,889 containers the result is identical (BLAKE3 of the
Debugform of theHostShader, or the same error): 0 differences.Work (user-mode instructions retired,
perf stat, 8 threads, three alternating rounds, the harness's own parsing subtracted; instructions rather than time because the machine was shared):mainmainSo the slow programs do 2-4x less work, and typical programs about 0.6% more (the sets are sorted when stored). On
mainthe cost of the slow ones varies a lot between runs: the walk's order follows hash-map iteration order.Time, one thread, single programs (two runs each,
main→ branch):maincbe258e224519eac6f06d377496b2eb7134379ec4e54d218Testing
cargo test -p victory-shader --lib --release -- --test-threads=1, cross-built for Windows and run under GE-Proton 11-14 (vkd3d-proton) on an RX 9070 XT with RADV (Mesa 26.3.1),dxil.dllfrom DXC v1.9.2609: 300 passed, 2 failed, the same 2 as onmainwith the same setup (hosts_that_do_not_fuse_are_namedandstub_pipeline_is_created: under vkd3d-proton the "WARP" adapter is the real GPU and the DXIL digest is not checked).cargo test --release -p victory-shader(with #2766 so the tests build off Windows): 172 passed, 0 failed.cargo clippy -p victory-shader --locked -- -D warnings: clean.rustfmt: no change in the edited lines.🤖 Generated with Claude Code
@fuzeofficlal on GitHub · 2026-11-25 23:00 UTC · original
Reviewed and verified
5e5cd11fon Windows 11 (RTX 5090 + WARP +dxil.dll) againstmain(842b0cb):Known::set()+subsets()subset lookup once a phi reachesINDEXED = 16sets is mathematically equivalent to the original linear subset scan (Known::andcaps length atMAX_KNOWN = 4, soO(2^4) = 16lookups match canonical sorted deduped subsets 1-to-1).emit::lower::tests33/33,emit::waterfall_tests5/5,cargo clippy --locked -p victory-shader --all-targets -- -D warnings0 warnings,cargo xtask policy(190 files ok).crates/victory-shader/src/emit/lower.rsis at522,192bytes (2,096bytes below the 512 KiBxtask policycap), so combining this with anotherlower.rsPR can easily cross the limit.lower::testsexceedINDEXED = 16; adding a small unit test exercisingdone.len() >= 16would directly cover theHashSetindex path.@cloph-dsp on GitHub · 2026-11-25 23:00 UTC · original
Local verification on Windows / rustc 1.100.0 / MSVC, no game data, no GPU, no clippy/rustup.
cargo check --locked -p victory-shaderclean.cargo test --locked -p victory-shader --lib emit::lower::tests --quiet-> 33 passed, 0 failed.f3578e3: 1 file, +44/-10;git diff --checkclean.emit::memory_tests::heap_direct_texture_reads_mark_their_heap_indexcrashes on unmodified base13f91caon this machine (pre-existing).AI-assisted (Hy4 flash).
@Anzymanz on GitHub · 2026-11-25 23:08 UTC · original
Thanks both. Following @fuzeofficlal's two notes,
20d1878adds:Knownand the per-phi bookkeeping (Reached: the scan, then the index from 16 sets on) move out oflive_edgesintoemit/lower/known.rs.reached_sets_match_a_scan_past_the_indexadds 4,000 paths of two to four of twelve conditions (repeats and any order) and checks every answer against a scan of all earlier kept sets; it keeps well over 64 sets, so most answers come from the index. A second test checks a set's sorting, repeats and subsets. Tests need distinctBools, sostructure.rsgets a#[cfg(test)] Bool::test(n).lower.rssize. WithKnownmoved out it is 519,410 bytes, about 1 KB undermain(520,563), so this no longer eats the margin under the 512 KiB cap.Checked again: the whole pack's 25,889 containers translate to the same output as
main(0 differences); nativecargo test --release -p victory-shader174 passed (both new tests included);cargo clippy -p victory-shader --locked -- -D warningsclean;cargo xtask policy192 files ok.AI-assisted (Claude Code, Claude Opus 5.5).
@pawrenity on GitHub · 2026-11-25 23:49 UTC · original
Re-checked the new head
20d18785(delta since5e5cd11f) on Windows 11, NVIDIA GeForce RTX 4070 + WARP.Delta review (
git diff 5e5cd11f 20d18785). The move intoemit/lower/known.rsis faithful:Known(get,and,set,subsets,NONE) is the same code, andReached::addis the old covered-check / push / index-extend block unchanged (scan belowINDEXED = 16sets, subset lookups from then on, the index filled with all sets at 16 and one per push after).live_edgescallsseen.entry(v).or_default().add(known)and skips onfalse, as before. The only additions are#[derive(Debug)]onKnownand a#[cfg(test)]Bool::testconstructor instructure.rs. No behaviour change found.New tests. Both pass. They are unit tests of the new
Known/Reachedtypes rather than a regression for a bug onmain(those types don't exist there), so there is no base-fails check to run. As a mutation check, dropping the per-pushself.index.extend(...)makesreached_sets_match_a_scan_past_the_indexfail, so the test does exercise the indexed path.Results on
20d18785(--test-threads=1):cargo test -p victory-shader --lib --locked -- emit::lower structure::: 48 passed, 0 failed, 1 ignored (includes bothemit::lower::known::tests).cargo test -p victory-shader --lib --locked -- --test-threads=1 --skip heap_direct_texture_reads_mark_their_heap_index: 300 passed, 3 failed, 3 ignored.main63bcca4with the same command: 298 passed, the same 3 failed (stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride,stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros,synthetic_tests::gathers_at_a_level_match_the_hardware). The skipped test crashes the process (STATUS_ACCESS_VIOLATION) onmaintoo on this machine, so it is unrelated to this PR.cargo clippy -p victory-shader --all-targets --locked -- -D warnings: clean.rustfmt --checkonknown.rs: clean.main63bcca4.AI-assisted (Claude Code).
@Marvisak on GitHub · 2026-11-26 04:24 UTC · original
Automated triage for the maintainers.
Merge candidate. This replaces the linear subset scan in
Lower::live_edgeswith a per-phi index once a phi has 16 earlier condition sets, movingKnownand the newReachedintoemit/lower/known.rs. The two agree: aKnownholds at mostMAX_KNOWN= 4 conditions, so an earlier set is a subset exactly when one of the at most 16 subsets ofknown.set()is indexed, and unused slots stay zeroed so the derivedHash/Eqhold.reached_sets_match_a_scan_past_the_indexchecks the index against an independent scan, and the whole-pack hash comparison in the description shows no output change.CI: passing.
Generated by Claude Code
@kotekompotek on GitHub · 2026-11-25 22:50 UTC · original · reviewed
Sound optimization: 'some earlier set is a subset' is exactly enumerable via the new set's subsets (2^len each) — once |done| exceeds 1<<MAX_KNOWN that's bounded work instead of a linear scan over tens of thousands of entries. The sorted-set canonicalization in set() makes HashSet indexing well-defined, and the <=MAX_KNOWN bound means the subset enumeration can't blow up. LGTM.
@Dukestar69 on GitHub · 2026-11-26 09:50 UTC · original · approved these changes
Verified against
origin/main(63bcca4):cargo test -p victory-shader --lib known:: -- --test-threads=1: passed.cargo test -p victory-shader --lib structure:: -- --test-threads=1: 13/13 passed (1 ignored bench).cargo clippy -p victory-shader --all-targets -- -D warnings: clean.cargo xtask policy: 192 files ok.Cleanly indexes earlier condition sets for live phi edge lookups.
Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
@fuzeofficlal on GitHub · 2026-11-26 11:39 UTC · original · approved these changes
Reviewed
20d18785(git merge-treeagainstmain63bcca43is clean):crates/victory-shader/src/emit/lower/known.rspreserves the exact original subset test: sorting and deduplicating stored predicates leaves membership unchanged, the empty set is handled cleanly both below and above the 16-entry index threshold, andemit/lower.rstraversal still preserves the ordered 4-condition history.63bcca43shrinkscrates/victory-shader/src/emit/lower.rsfrom 520,563 bytes to 519,410 bytes (4,878 bytes below the 524,288-byte policy limit).Verified locally on Windows 11 with RTX 5090 and WARP (
VICTORY_WARP=1):xtask policy(191 files ok) andcargo clippy -p victory-shader --all-targets -- -D warnings(default and--all-features): clean.known::tests2/2,structure::tests13/13 (1 ignored),emit::lower::tests33/33, andwaterfall_tests5/5 (both RTX 5090 and WARP) all pass, plus an independent exhaustive differential model check over 2,418,025 ordered predicate pairs (length 0..=4).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.