Shaders: lane conditions along live phi edges are values, and each set is met once #2060

Merged
0xkn0wh3r3 merged 1 commit from upstream-pr/2062 into main 2026-11-25 21:54:15 +00:00
0xkn0wh3r3 commented 2026-11-25 04:18:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-25T04:18:52Z.


The problem

Translating shaders with control flow spends time and allocations in one step that keeps repeating the same work.

Lower::live_edges walks every phi a lane reads, back through its incoming edges, carrying the last few branch conditions known along the path. Two costs:

  • Each step allocated a Vec for those conditions, cloned for every edge and for the seen list.
  • Each step asked the lane-condition diagram (meet) whether some lane meets the conditions. Paths into one join ask again for the same sets.

On a synthetic program of 256 blocks, 4,915 work items asked 3,047 times for 883 distinct sets.

The fix

  • The conditions (at most four) are now a small Copy value (Known), not a Vec.
  • Whether a set is met is kept per set for the walk, so each set is asked once.

The walk and its answers are unchanged.

Measured with synthetic wave32 compute programs (divergent ifs, uniform loops, gathers) through range::build, resource::track, io::raise and emit_with, release build, two rounds each:

program emit_with on main with the change
256 blocks 4.67-4.74 ms 4.15-4.27 ms
64 blocks 1.28-1.31 ms 1.19-1.21 ms
512 small blocks 3.50-3.52 ms 3.26-3.30 ms

Allocations in emit_with are down about 40%. The DXIL is byte-identical for every program (FNV hash of the output).

Testing

  • cargo test -p victory-shader --lib -- --test-threads=1 on an RTX 5070 Ti (driver 621.24): all pass except emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride, emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros and emit::synthetic_tests::gathers_at_a_level_match_the_hardware. Those fail the same way on main on NVIDIA (the test host's views, #1835; #1887 fixes two of them here).
  • cargo clippy -p victory-shader --all-targets: clean.
  • The timings above come from a synthetic benchmark outside the tree (hand-encoded programs through the public stage functions). No game shaders here.

#999 changes meet itself and other parts of lower.rs. This change touches only live_edges.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6597112969 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2062). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-25T04:18:52Z. --- ## The problem Translating shaders with control flow spends time and allocations in one step that keeps repeating the same work. `Lower::live_edges` walks every phi a lane reads, back through its incoming edges, carrying the last few branch conditions known along the path. Two costs: - Each step allocated a `Vec` for those conditions, cloned for every edge and for the seen list. - Each step asked the lane-condition diagram (`meet`) whether some lane meets the conditions. Paths into one join ask again for the same sets. On a synthetic program of 256 blocks, 4,915 work items asked 3,047 times for 883 distinct sets. ## The fix - The conditions (at most four) are now a small `Copy` value (`Known`), not a `Vec`. - Whether a set is met is kept per set for the walk, so each set is asked once. The walk and its answers are unchanged. Measured with synthetic wave32 compute programs (divergent ifs, uniform loops, gathers) through `range::build`, `resource::track`, `io::raise` and `emit_with`, release build, two rounds each: | program | `emit_with` on main | with the change | |---|---|---| | 256 blocks | 4.67-4.74 ms | 4.15-4.27 ms | | 64 blocks | 1.28-1.31 ms | 1.19-1.21 ms | | 512 small blocks | 3.50-3.52 ms | 3.26-3.30 ms | Allocations in `emit_with` are down about 40%. The DXIL is byte-identical for every program (FNV hash of the output). ## Testing - `cargo test -p victory-shader --lib -- --test-threads=1` on an RTX 5070 Ti (driver 621.24): all pass except `emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride`, `emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros` and `emit::synthetic_tests::gathers_at_a_level_match_the_hardware`. Those fail the same way on `main` on NVIDIA (the test host's views, #1835; #1887 fixes two of them here). - `cargo clippy -p victory-shader --all-targets`: clean. - The timings above come from a synthetic benchmark outside the tree (hand-encoded programs through the public stage functions). No game shaders here. ## Related #999 changes `meet` itself and other parts of `lower.rs`. This change touches only `live_edges`.
catminou4 commented 2026-11-25 05:05:53 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T04:33:50Z.

Review: APPROVED


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

144 passed / 0 failed / 2 ignored. Lane conditions along live phi edges as values — phi-edge condition lowering fix.

<!-- greyfinch-sync:Marvisak/VIctory:review:6051901225 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2062#pullrequestreview-6051901225). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T04:33:50Z. **Review:** APPROVED --- Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, `cargo test -p victory-shader --locked`. 144 passed / 0 failed / 2 ignored. Lane conditions along live phi edges as values — phi-edge condition lowering fix.
DenRakEiw commented 2026-11-25 05:05:53 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T04:36:24Z.

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:: — 21 passed, 0 failed
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean memoization and value-based handling of lane conditions along live phi edges. Looks good.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6051919187 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2062#pullrequestreview-6051919187). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T04:36:24Z. **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::` — 21 passed, 0 failed - `cargo clippy -p victory-shader --all-targets` — 0 warnings - `cargo xtask policy` — ok (`183 files ok`) Clean memoization and value-based handling of lane conditions along live phi edges. Looks good. AI-assisted (Gemini 4 Neon).
sinan-cmyk commented 2026-11-25 10:07:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sinan-cmyk on 2026-11-25T09:22:34Z.


Offline measurement on a retail dump

Result: no count change against main.

I merged head a0163c26 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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7158245280 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2062#issuecomment-7158245280). Originally posted by [sinan-cmyk](https://github.com/sinan-cmyk) on 2026-11-25T09:22:34Z. --- ### Offline measurement on a retail dump **Result: no count change against `main`.** I merged head a0163c26 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).
Marvisak commented 2026-11-25 10:07:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T09:25:43Z.


Automated triage for the maintainers.
Merge candidate.

This is a performance-only change to Lower::live_edges in crates/victory-shader/src/emit/lower.rs. The list of known branch conditions becomes a fixed Copy array (Known, at most MAX_KNOWN) instead of a Vec. The result of Bools::meet is also cached per condition set for the walk.

I checked that it behaves the same as before:

  • Known::and drops the oldest condition just as the old drain did.
  • Slots beyond len always keep their initial value, so the derived Eq/Hash match the old Vec contents.
  • meet (structure.rs) takes &self and starts a fresh budget on each call, so caching its answer cannot change results.

The author reports byte-identical DXIL and about 5–10% faster emit_with. A separate offline run in the thread found no change in validation counts. No other open PR changes live_edges.

CI: not run (no check runs on this head). A maintainer may need to approve the workflow run.

Recommending a look, not approving.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7158316281 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2062#issuecomment-7158316281). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T09:25:43Z. --- <!-- victory-triage v1 head=a0163c269b20fdff3ea8be7d9d9f34de9cbbf861 verdict=merge-candidate at=2026-11-25T09:25:35Z --> Automated triage for the maintainers. **Merge candidate.** This is a performance-only change to `Lower::live_edges` in `crates/victory-shader/src/emit/lower.rs`. The list of known branch conditions becomes a fixed `Copy` array (`Known`, at most `MAX_KNOWN`) instead of a `Vec`. The result of `Bools::meet` is also cached per condition set for the walk. I checked that it behaves the same as before: - `Known::and` drops the oldest condition just as the old `drain` did. - Slots beyond `len` always keep their initial value, so the derived `Eq`/`Hash` match the old `Vec` contents. - `meet` (`structure.rs`) takes `&self` and starts a fresh budget on each call, so caching its answer cannot change results. The author reports byte-identical DXIL and about 5–10% faster `emit_with`. A separate offline run in the thread found no change in validation counts. No other open PR changes `live_edges`. CI: not run (no check runs on this head). A maintainer may need to approve the workflow run. Recommending a look, not approving. --- _Generated by [Claude Code](https://claude.ai/code)_
upstream-sync closed this pull request 2026-11-25 22:03:24 +00:00
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!2060
No description provided.