Skip to content

fix(shader): account for generated loop-cap returns in barrier and half-wave analyses - #408

Open
brainbudt wants to merge 1 commit into
pragyaangaur:mainfrom
brainbudt:fix/shader-capped-back-edge-barriers
Open

brainbudt wants to merge 1 commit into
pragyaangaur:mainfrom
brainbudt:fix/shader-capped-back-edge-barriers

Conversation

@brainbudt

Copy link
Copy Markdown
Contributor

Following up on the review finding noted on #376: lowering turns an unconditional back edge (LaneExit::Jump to a loop header) into a conditional branch to a generated early return when the loop iteration limit is hit (crates/victory-shader/src/emit/lower.rs). Previously, in_step, branch_regions postdominator analysis, and explicit s_barrier lowering only considered the console CFG, allowing one wave/half to return early at the cap while another reached a half-wave exchange or s_barrier (GroupMemoryBarrierWithGroupSync) with missing participants.

This change:

  • Extends in_step (step_and_cap in crates/victory-shader/src/emit/lower/barriers.rs) to treat a capped unconditional back edge (latch, header) as uniform only when header, latch, and every back edge sharing header's loop counter run in step (with uniform branch conditions on conditional back edges). Otherwise marks blocks reachable from header and divergent sibling arms (bounded at reconvergence points where step[p] holds) as not-in-step and cap_escaped.
  • Adds a virtual terminal exit in branch_regions (crates/victory-shader/src/emit/lower/halves.rs) for capped unconditional back edges so a closed self-loop does not make a sibling path a false postdominator/reconvergence point.
  • Rejects explicit s_barrier in cap_escaped blocks (crates/victory-shader/src/emit/lower.rs), while preserving safe barriers before divergent cap branches, behind uniform branches, and after uniform capped loops.
  • Adds native lowering regression tests in crates/victory-shader/src/emit/lower/tests.rs covering all hazard shapes (divergent inner capped loop, shared loop counter with a divergent conditional back edge, closed self-loop sibling exchange and s_barrier) and positive controls (uniform capped loops, conditional back edges, earlier joins, and +1 emitted barrier verification against s_nop).

Tested locally on macOS ARM64 (no game files):

  • cargo test -p victory-shader (220 passed, 0 failed, 3 ignored)
  • cargo clippy -p victory-shader --all-targets
  • cargo xtask policy (262 files ok)

Created with Mistral Large 4, GPT-6.2 Sol, and Gemini 4 Neon in T3 Code.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant