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

Closed
SinisterRob wants to merge 1 commit from upstream-pr/2640 into main
SinisterRob commented 2026-11-25 17:24:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by SinisterRob on 2026-11-25T17:24:06Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5495965020 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2640). Originally posted by [SinisterRob](https://github.com/SinisterRob) on 2026-11-25T17:24:06Z. --- 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.
kotekompotek commented 2026-11-25 18:02:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T17:47:57Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7170015251 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2640#issuecomment-7170015251). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T17:47:57Z. --- 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.
oni-swr commented 2026-11-25 18:02:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-25T17:58:11Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7170254007 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2640#issuecomment-7170254007). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-25T17:58:11Z. --- **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`.
Marvisak commented 2026-11-26 00:03:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T23:22:07Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7177464370 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2640#issuecomment-7177464370). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T23:22:07Z. --- <!-- 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)_
Dukestar69 commented 2026-11-26 05:07:03 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T05:07:03Z.

Review: APPROVED


Tested merged onto origin/main (63bcca4) on Windows 11 x64:

  • cargo test -p victory-shader --lib emit::bitcode::tests: 12 passed, 0 failed, 1 ignored.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6068831618 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2640#pullrequestreview-6068831618). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T05:07:03Z. **Review:** APPROVED --- Tested merged onto `origin/main` (`63bcca4`) on Windows 11 x64: - `cargo test -p victory-shader --lib emit::bitcode::tests`: **12 passed, 0 failed, 1 ignored**. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
upstream-sync closed this pull request 2026-11-26 06:02:38 +00:00
reallyvibe commented 2026-11-29 00:18:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reallyvibe on 2026-11-29T00:18:15Z.


Would you be comfortable with me opening a follow-up PR that preserves your original commit and authorship and adds the regression coverage separately? If you set this aside because of a concern or have a replacement in mind, I’d defer to that.

I checked the patch on Windows against main 63bcca43. On a 1,024-block unconditional chain, source hops fell from 524,800 to 1,023, target hops from 524,800 to zero, and duplicate-edge scans from 1,024 to zero. Disabling either optimization independently tripped the corresponding counter assertion. The extended branching-root test also preserved distinct phi predecessors through a multi-hop chain.

The separate #2665 and #2657 combinations passed the synthetic DXIL checks after resolving #2665’s folding-loop conflict. The measurements are synthetic, so real-title performance remains unmeasured.

AI-assisted (OpenAI Codex).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7248018596 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2640#issuecomment-7248018596). Originally posted by [reallyvibe](https://github.com/reallyvibe) on 2026-11-29T00:18:15Z. --- Would you be comfortable with me opening a follow-up PR that preserves your original commit and authorship and adds the regression coverage separately? If you set this aside because of a concern or have a replacement in mind, I’d defer to that. I checked the patch on Windows against main `63bcca43`. On a 1,024-block unconditional chain, source hops fell from 524,800 to 1,023, target hops from 524,800 to zero, and duplicate-edge scans from 1,024 to zero. Disabling either optimization independently tripped the corresponding counter assertion. The extended branching-root test also preserved distinct phi predecessors through a multi-hop chain. The separate #2665 and #2657 combinations passed the synthetic DXIL checks after resolving #2665’s folding-loop conflict. The measurements are synthetic, so real-title performance remains unmeasured. AI-assisted (OpenAI Codex).

Pull request closed

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!2638
No description provided.