Skip to content

Shaders: live phi edges look up the earlier condition sets they cover - #2864

Open
Anzymanz wants to merge 2 commits into
Marvisak:mainfrom
Anzymanz:live-edges-subset-index
Open

Anzymanz wants to merge 2 commits into
Marvisak:mainfrom
Anzymanz:live-edges-subset-index

Conversation

@Anzymanz

Copy link
Copy Markdown

From 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. On 13f91ca one program took 527 s inside translate --cache under 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_edges skips 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 (Linux perf, 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_edges in emit/lower.rs:

  • A set holds at most 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.
  • Below 16 the scan stays as it is: it is cheaper than the lookups for the common case.
  • Known gets set() (sorted, without repeats) and subsets(); none becomes the NONE constant 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_with from a small harness outside the tree (native Linux, release build), main f3578e3 against 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 Debug form of the HostShader, 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):

sample main this branch
the 100 slowest programs on main 896 / 1,563 / 2,337 G 536 / 492 / 596 G
600 programs picked at random 81.8 / 81.9 / 81.9 G 82.3 / 82.3 / 82.4 G

So the slow programs do 2-4x less work, and typical programs about 0.6% more (the sets are sorted when stored). On main the 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):

program main this branch
cbe258e224519eac 24.8 s, 22.3 s 3.4 s, 3.1 s
6f06d377496b2eb7 8.1 s, 9.4 s 3.1 s, 2.0 s
134379ec4e54d218 0.4 s, 8.4 s 2.5 s, 0.7 s

Testing

  • 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.dll from DXC v1.9.2609: 300 passed, 2 failed, the same 2 as on main with the same setup (hosts_that_do_not_fuse_are_named and stub_pipeline_is_created: under vkd3d-proton the "WARP" adapter is the real GPU and the DXIL digest is not checked).
  • Native Linux cargo test --release -p victory-shader (with Shader tests: the descriptor-join tests build off Windows #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

`Lower::live_edges` skips a phi reached with a set of conditions when an
earlier set of that phi is a subset of it. It found one by scanning every
earlier set, and in programs with much control flow a phi is reached with
tens of thousands of sets, so the scan made the walk quadratic: some
programs took tens of seconds to lower, nearly all of it here.

A set holds at most four 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.
Below that the scan stays: it is cheaper than the lookups. The walk takes
the same decisions, so the live edges are the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@kotekompotek kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Reviewed and verified 5e5cd11f on Windows 11 (RTX 5090 + WARP + dxil.dll) against main (842b0cb):

  • The Known::set() + subsets() subset lookup once a phi reaches INDEXED = 16 sets is mathematically equivalent to the original linear subset scan (Known::and caps length at MAX_KNOWN = 4, so O(2^4) = 16 lookups match canonical sorted deduped subsets 1-to-1).
  • Local checks: emit::lower::tests 33/33, emit::waterfall_tests 5/5, cargo clippy --locked -p victory-shader --all-targets -- -D warnings 0 warnings, cargo xtask policy (190 files ok).
  • Two notes:
    1. crates/victory-shader/src/emit/lower.rs is at 522,192 bytes (2,096 bytes below the 512 KiB xtask policy cap), so combining this with another lower.rs PR can easily cross the limit.
    2. None of the existing 33 lower::tests exceed INDEXED = 16; adding a small unit test exercising done.len() >= 16 would directly cover the HashSet index path.

@cloph-dsp

Copy link
Copy Markdown

Local verification on Windows / rustc 1.100.0 / MSVC, no game data, no GPU, no clippy/rustup.

  • cargo check --locked -p victory-shader clean.
  • cargo test --locked -p victory-shader --lib emit::lower::tests --quiet -> 33 passed, 0 failed.
  • Diff vs merge-base f3578e3: 1 file, +44/-10; git diff --check clean.
  • No new test was added in this PR; the changed walk is covered by the existing lower tests, and the author reports identical output across 25,889 containers.
  • Full victory-shader lib was not run: emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index crashes on unmodified base 13f91ca on this machine (pre-existing).

AI-assisted (Hy4 flash).

…ests

`Known` and the sets of it each phi was reached with (`Reached`, the scan
and the index) move from `Lower::live_edges` into `emit/lower/known.rs`,
so the index path can be tested on its own: a test adds 4,000 paths of
two to four of twelve conditions and checks each answer against a scan of
every earlier set (well past the 16 sets where the index takes over), and
one checks a set's sorting and subsets. `lower.rs` gets about 1 KB smaller.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Anzymanz

Copy link
Copy Markdown
Author

Thanks both. Following @fuzeofficlal's two notes, 20d1878 adds:

  • A test of the index path. Known and the per-phi bookkeeping (Reached: the scan, then the index from 16 sets on) move out of live_edges into emit/lower/known.rs. reached_sets_match_a_scan_past_the_index adds 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 distinct Bools, so structure.rs gets a #[cfg(test)] Bool::test(n).
  • lower.rs size. With Known moved out it is 519,410 bytes, about 1 KB under main (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); native cargo test --release -p victory-shader 174 passed (both new tests included); cargo clippy -p victory-shader --locked -- -D warnings clean; cargo xtask policy 192 files ok.

AI-assisted (Claude Code, Claude Opus 5.5).

@pawrenity

Copy link
Copy Markdown

Re-checked the new head 20d18785 (delta since 5e5cd11f) on Windows 11, NVIDIA GeForce RTX 4070 + WARP.

Delta review (git diff 5e5cd11f 20d18785). The move into emit/lower/known.rs is faithful: Known (get, and, set, subsets, NONE) is the same code, and Reached::add is the old covered-check / push / index-extend block unchanged (scan below INDEXED = 16 sets, subset lookups from then on, the index filled with all sets at 16 and one per push after). live_edges calls seen.entry(v).or_default().add(known) and skips on false, as before. The only additions are #[derive(Debug)] on Known and a #[cfg(test)] Bool::test constructor in structure.rs. No behaviour change found.

New tests. Both pass. They are unit tests of the new Known/Reached types rather than a regression for a bug on main (those types don't exist there), so there is no base-fails check to run. As a mutation check, dropping the per-push self.index.extend(...) makes reached_sets_match_a_scan_past_the_index fail, 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 both emit::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. main 63bcca4 with 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) on main too on this machine, so it is unrelated to this PR.
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings: clean.
  • rustfmt --check on known.rs: clean.
  • Merges cleanly into main 63bcca4.

AI-assisted (Claude Code).

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. This replaces the linear subset scan in Lower::live_edges with a per-phi index once a phi has 16 earlier condition sets, moving Known and the new Reached into emit/lower/known.rs. The two agree: a Known holds at most MAX_KNOWN = 4 conditions, so an earlier set is a subset exactly when one of the at most 16 subsets of known.set() is indexed, and unused slots stay zeroed so the derived Hash/Eq hold. reached_sets_match_a_scan_past_the_index checks 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

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 20d18785 (git merge-tree against main 63bcca43 is clean):

  • The indexed subset lookup in crates/victory-shader/src/emit/lower/known.rs preserves 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, and emit/lower.rs traversal still preserves the ordered 4-condition history.
  • Merging onto 63bcca43 shrinks crates/victory-shader/src/emit/lower.rs from 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) and cargo clippy -p victory-shader --all-targets -- -D warnings (default and --all-features): clean.
  • known::tests 2/2, structure::tests 13/13 (1 ignored), emit::lower::tests 33/33, and waterfall_tests 5/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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants