Shaders: avoid quadratic planning on unconditional empty-block chains #2640

Closed
github-bot wants to merge 1 commit from github-bot/gh-2640 into main AGit
Owner

@⁠SinisterRob on GitHub · 2026-11-25 17:24 UTC · original
shader/linear-unconditional-folds → main · +10 −4 · 1 files · 1 commits · closed without merging

Folding a linear chain of empty blocks walks the growing chain twice per block. The writer follows predecessor links back to the source, then scans the source’s successor through the folded blocks to check whether two edges land at the same target.

This change records the resolved source when accepting a fold. If that source later folds, its link still leads to the final source. With only one outgoing edge the source cannot split a critical edge or reach the target twice, so the planner can skip the duplicate-edge walk. The existing cycle check still runs before accepting the fold.

Blocks in a synthetic unconditional chain Before After
256 0.105 ms 0.014 ms
1,024 1.71 ms 0.052 ms
4,096 26.9 ms 0.212 ms
8,192 107 ms 0.428 ms

These are CPU planning times, median of nine calls to Module::plan on a prebuilt module, using an optimized standalone Linux test build of bitcode.rs (rustc 1.97.0). Each module consists of a straight chain of branches ending in a return. All four emitted byte streams matched the base revision. I also compared emitted bytes on 10,000 seeded small control-flow graphs containing branches, joins and back edges. All matched. The 12 existing non-ignored writer tests passed.

Title shader chain lengths and frame-time impact are unmeasured. This is independent of the phi correctness change in #2622. AI-assisted, synthetic inputs throughout.

**@⁠SinisterRob** on GitHub · 2026-11-25 17:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/2640) `shader/linear-unconditional-folds` → `main` · +10 −4 · 1 files · 1 commits · closed without merging Folding a linear chain of empty blocks walks the growing chain twice per block. The writer follows predecessor links back to the source, then scans the source’s successor through the folded blocks to check whether two edges land at the same target. This change records the resolved source when accepting a fold. If that source later folds, its link still leads to the final source. With only one outgoing edge the source cannot split a critical edge or reach the target twice, so the planner can skip the duplicate-edge walk. The existing cycle check still runs before accepting the fold. | Blocks in a synthetic unconditional chain | Before | After | |---:|---:|---:| | 256 | 0.105 ms | 0.014 ms | | 1,024 | 1.71 ms | 0.052 ms | | 4,096 | 26.9 ms | 0.212 ms | | 8,192 | 107 ms | 0.428 ms | These are CPU planning times, median of nine calls to `Module::plan` on a prebuilt module, using an optimized standalone Linux test build of `bitcode.rs` (rustc 1.97.0). Each module consists of a straight chain of branches ending in a return. All four emitted byte streams matched the base revision. I also compared emitted bytes on 10,000 seeded small control-flow graphs containing branches, joins and back edges. All matched. The 12 existing non-ignored writer tests passed. Title shader chain lengths and frame-time impact are unmeasured. This is independent of the phi correctness change in #2622. AI-assisted, synthetic inputs throughout.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:10:58 +00:00
github-bot closed this pull request 2026-11-26 06:30:51 +00:00
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 17:47 UTC · original

Reviewed: two changes, both right. fold[b] now records the ultimate source (plan.src(*p)) so the source survives its own later fold — matching the updated doc. The succs(from).len() == 1 early-out is sound: with a single outgoing edge the fold can neither create a critical edge nor a second path to the target, so the reject checks are skipped — that removes the quadratic filter over successors. CI green.

**@⁠kotekompotek** on GitHub · 2026-11-25 17:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/2640#issuecomment-7170015251) Reviewed: two changes, both right. `fold[b]` now records the ultimate source (`plan.src(*p)`) so the source survives its own later fold — matching the updated doc. The `succs(from).len() == 1` early-out is sound: with a single outgoing edge the fold can neither create a critical edge nor a second path to the target, so the reject checks are skipped — that removes the quadratic filter over successors. CI green.
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 17:58 UTC · original

Independent verification of head f01104eca8947cf6dd309f24841833e1575194e6 merged onto origin/main 13f91caee25f2fdf2c062be5f7a27f058e51cbca.

  • Environment: Windows 11 Pro 10.0.26200, rustc 1.98.1 (48a229cea 2026-09-01), -j 4, CPU-only. I still set VICTORY_WARP=1, VICTORY_D3D12_WARP=1 and put dxil.dll (1.8.2502.11) on PATH as a precaution, since victory-shader tests also dispatch on WARP.
  • Diff: bitcode.rs Module::plan: the fold source is resolved when stored, and a single-successor source skips the critical-edge and duplicate-path checks. I traced that those checks cannot trigger with one outgoing edge; the existing plan.fold assertions still hold. Merges cleanly.
  • clippy (-p victory-shader --all-targets): no warnings.
  • cargo test --locked --no-fail-fast -p victory-shader: 267 passed, 3 failed, 3 ignored, identical to origin/main on WARP (267/3/3). The same 3 fail on both: a_mask_joined_with_data_at_a_uniform_loop_is_its_number, gathers_at_a_level_match_the_hardware, permutes_read_the_lane_named_and_zero_from_lanes_left_out (WARP baseline, "no pipeline for a wave of 32"); not caused by this PR.
  • Negative control: the PR adds no test, so I wrote a temporary one (not committed): a chain of N empty unconditional blocks, timing plan(). origin/main: N=4000 77.5 ms, 8000 299 ms, 16000 1.216 s (about 4x per doubling, quadratic). PR head: 1.89 ms, 3.66 ms, 7.04 ms (linear). Layout is the same on both (entry + exit).
  • Found: no bugs. Suggest a regression test like the above (a doubling-ratio check rather than an absolute time).

Independent verification by Claude Code (Claude Sonnet 5.5), an AI. No game files, dumps or shader names were used. Each PR head was merged (no conflicts) onto origin/main and run with --locked; clippy used --all-targets.

**@⁠oni-swr** on GitHub · 2026-11-25 17:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/2640#issuecomment-7170254007) **Independent verification** of head `f01104eca8947cf6dd309f24841833e1575194e6` merged onto `origin/main` `13f91caee25f2fdf2c062be5f7a27f058e51cbca`. - Environment: Windows 11 Pro 10.0.26200, rustc 1.98.1 (48a229cea 2026-09-01), `-j 4`, CPU-only. I still set `VICTORY_WARP=1`, `VICTORY_D3D12_WARP=1` and put dxil.dll (1.8.2502.11) on PATH as a precaution, since `victory-shader` tests also dispatch on WARP. - Diff: `bitcode.rs` `Module::plan`: the fold source is resolved when stored, and a single-successor source skips the critical-edge and duplicate-path checks. I traced that those checks cannot trigger with one outgoing edge; the existing `plan.fold` assertions still hold. Merges cleanly. - clippy (`-p victory-shader --all-targets`): no warnings. - `cargo test --locked --no-fail-fast -p victory-shader`: **267 passed, 3 failed, 3 ignored**, identical to `origin/main` on WARP (267/3/3). The same 3 fail on both: `a_mask_joined_with_data_at_a_uniform_loop_is_its_number`, `gathers_at_a_level_match_the_hardware`, `permutes_read_the_lane_named_and_zero_from_lanes_left_out` (WARP baseline, "no pipeline for a wave of 32"); not caused by this PR. - **Negative control:** the PR adds no test, so I wrote a temporary one (not committed): a chain of N empty unconditional blocks, timing `plan()`. `origin/main`: N=4000 77.5 ms, 8000 299 ms, 16000 1.216 s (about 4x per doubling, quadratic). PR head: 1.89 ms, 3.66 ms, 7.04 ms (linear). Layout is the same on both (entry + exit). - Found: no bugs. Suggest a regression test like the above (a doubling-ratio check rather than an absolute time). Independent verification by Claude Code (Claude Sonnet 5.5), an AI. No game files, dumps or shader names were used. Each PR head was merged (no conflicts) onto `origin/main` and run with `--locked`; clippy used `--all-targets`.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 23:22 UTC · original

Automated triage for the maintainers.

In Module::plan (crates/victory-shader/src/emit/bitcode.rs) a folded block now records its resolved source (plan.src(*p)), and a source with a single successor skips the critical-edge and duplicate-target checks, removing the quadratic walk on long chains of empty blocks. I checked that the only reader of the stored source is Plan::src, which still walks the chain, so the result is unchanged; and that with one outgoing edge the old checks could not reject (critical needs more than one successor, and that successor's plan.to is the fold's target through the chain just folded). There is no regression test for the complexity; a small doubling-ratio test would be welcome but is not required. CI: check passing.

This is a recommendation for a maintainer to look, not an approval.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 23:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/2640#issuecomment-7177464370) <!-- victory-triage v1 head=f01104eca8947cf6dd309f24841833e1575194e6 verdict=merge-candidate at=2026-11-25T23:21:59Z --> Automated triage for the maintainers. In `Module::plan` (crates/victory-shader/src/emit/bitcode.rs) a folded block now records its resolved source (`plan.src(*p)`), and a source with a single successor skips the critical-edge and duplicate-target checks, removing the quadratic walk on long chains of empty blocks. I checked that the only reader of the stored source is `Plan::src`, which still walks the chain, so the result is unchanged; and that with one outgoing edge the old checks could not reject (`critical` needs more than one successor, and that successor's `plan.to` is the fold's target through the chain just folded). There is no regression test for the complexity; a small doubling-ratio test would be welcome but is not required. CI: `check` passing. This is a recommendation for a maintainer to look, not an approval. --- _Generated by [Claude Code](https://claude.ai/code)_

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2640
No description provided.