Skip to content

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

Closed
SinisterRob wants to merge 1 commit into
Marvisak:mainfrom
SinisterRob:shader/linear-unconditional-folds
Closed

SinisterRob wants to merge 1 commit into
Marvisak:mainfrom
SinisterRob:shader/linear-unconditional-folds

Conversation

@SinisterRob

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

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.

Copy link
Copy Markdown
Owner

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

@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.

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).

@SinisterRob
SinisterRob deleted the shader/linear-unconditional-folds branch November 26, 2026 05:45
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.

5 participants