Skip to content

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

Merged
pragyaangaur merged 1 commit into
pragyaangaur:mainfrom
brainbudt:fix/shader-capped-back-edge-barriers
Nov 27, 2026
Merged

pragyaangaur merged 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.

@hackgods

Copy link
Copy Markdown
Contributor

An in-game interaction worth knowing about before merge. M5 Pro, D3DMetal 4.0b3, Series X, own dump, diagnostics builds. Text only, no game data.

We bisected a black world (only the HUD drawn) down to this PR in a local stack that also carries #435 (literal-halves retry) and #401. #408 and #435 conflict in lower.rs/barriers.rs. Our merge combines them: step_and_cap returns the in-step blocks, the group-uniform scalars and the cap escapes, and uses #435's rule (a whole-wave vote counts as uniform in literal halves) for both checks. On that tree:

through #425 (no #408) + #408
compute shaders "does not translate" 39 50
refused for "an s_barrier where a lane may have left the program early at a loop's iteration limit" 0 15
world frames (mean luma) ~45, lit 0, black except HUD

The 15 newly refused programs include the two tile-list passes (eae3498e6553afa8, b8f3f7979d6bbc50) and others in the lighting chain (6bb493a39685ddcc, 8cb27ccf2d50c606, df11dcd012cd7382, d9978c76c746d809, …). Their dispatches are dropped, so the lit target never gets written: the same symptom #435 had just fixed.

So in this game the cap-escape refusal costs the picture, while the hang it guards against never showed up in our runs before #408. Two questions:

  • Could a capped back edge in a loop the whole group runs together (same trip count in every half or wave, which is what these tile loops look like) count as in step, so the barrier stays allowed?
  • Or could it fall back to a non-refusing form, e.g. a group-uniform cap or a barrier-safe exit, instead of refusing the program?

I can test any variant in-game here. Our merge resolution may differ from what you'd do, so treat the counts as indicative. Locally we've reverted #408 for now.

Prepared with Claude Code.

@pragyaangaur
pragyaangaur merged commit e271798 into pragyaangaur:main Nov 27, 2026
1 check passed
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.

3 participants