Skip to content

Shaders: lanes of a wave reach group barriers together, as on the console - #2920

Open
lhnizdo wants to merge 8 commits into
Marvisak:mainfrom
lhnizdo:pr/barrier-sync
Open

lhnizdo wants to merge 8 commits into
Marvisak:mainfrom
lhnizdo:pr/barrier-sync

Conversation

@lhnizdo

@lhnizdo lhnizdo commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

The problem

The console runs s_barrier per wave. A branch skips a block only when no lane of the wave needs it; otherwise the wave runs the block, barrier included, with the other lanes masked off. The host's GroupMemoryBarrierWithGroupSync counts threads: each lane takes its own branch, so lanes that skip a block holding a barrier never arrive there, and the rest of the group waits for them forever. That's a GPU hang (TDR).

The title's compute programs where this hung submissions here:

  • compute_graph_ops: the matrix programs (6 barriers on one side of a per-lane branch), and CS_FormulaOperation, which loops until any lane reaches its own count.
  • sky_scattering CS_MultipleScattering: 7 barriers.
  • Blendshape and density-shadow programs: 1 barrier each.
  • CS_BatchCullPreSkinnedMeshlets: the meshlet culling, in the submission of a device hang (the debug layer reported the TDR).

What this does

  1. Balancing (9eadbf3).
    • At each per-lane branch, the lighter side's edge gets as many barriers as the two sides' counts to the program's end differ. A host barrier counts arrivals, not instructions, so this is enough.
    • Counts are taken over forward edges, as the sets possible along uniform paths. A branch leaving or going round a loop without barriers needs none.
  2. Early exits (0e0b272).
    • A side that meets no more barriers on any path runs to its end and exits, and needs nothing added: a host barrier doesn't wait for threads that have exited.
    • shortcut: D3D12 leaves a barrier after a thread's exit undefined. NVIDIA and AMD count exited threads out; an early return before a barrier works on both.
  3. Loop exits and console mode (8de7c96).
    • A per-lane branch leaving (or going round) a loop that holds a barrier is decided for the whole wave, as the console decides s_cbranch_vccz/execz and their nz forms, where that only makes lanes leave sooner.
    • Console mode: a program whose barriers still can't be balanced by counting runs as the console runs it. Every branch on a lane mask is decided for the wave, and every vector instruction is guarded by its own EXEC, so the wave's lanes reach each barrier together. It costs guards, so it's used only where counting fails.
  4. 5d7e5e0 moves this code as is into lower/barriers.rs. With it, lower.rs would pass the asset policy's 512 KiB per-file limit.
    • lower.rs is now 524,190 bytes, close to the limit, so the next large addition will need a split too.

Numbers

Measured on the earlier upstream base the commits were first written on, not re-measured on 63bcca4:

  • Balancing alone: 55 programs got extra barriers, and all 1,951 emitted compute programs still validated.
  • With early exits: unbalanced programs went from 37 to 28. The meshlet culling's base and _Prev variants are balanced; its ParaboloidShadows ones aren't yet.
  • Console mode: covers the programs counting can't, 53 of the title's, among them the luminance histograms, shadow classification and shadow-map culling.

Tested

In the game, on an RTX 2080 (32-lane waves), Windows 10 19045: these commits have been in the builds here that reached gameplay through the opening cutscene (with the title's pools made larger, #698), and in those loading a save straight into the world, together with #2460 #2461 #2462 #2464.

No unit tests are added. The cases above are the title's programs, and I'd welcome a pointer to the preferred place for synthetic barrier cases.

Also builds on this: the wave64 split (#1960) and a console mode for a group of one console wave split into two host waves (work in progress on my side, for the InstanceCulling passes) both use the wave-wide branch decisions.

AI-assisted (Claude Code); the runs were on my machine. No game data.

🤖 Generated with Claude Code

https://claude.ai/code/session_018sSQTfPXZpSkjMMNa7RB5G

lhnizdo and others added 4 commits November 26, 2026 00:56
…iers


The console runs s_barrier per wave: a branch skips a block only when no
lane of the wave needs it, and otherwise the wave runs the block, barrier
included, with the other lanes masked off. On the host each lane takes
its own branch, so lanes skipping such a block never arrive at its
barriers and the group's other lanes wait for them forever: a GPU hang.
The title's compute_graph_ops matrix programs (6 barriers on one side of
a per-lane branch), sky_scattering CS_MultipleScattering (7), and its
blendshape and density-shadow programs (1 each) were all in hung
submissions.

A host barrier counts arrivals, not instructions, so at each per-lane
branch the lighter side's edge now gets as many barriers as the two
sides' counts to the program's end differ. Counts are taken over forward
edges as the sets possible along uniform paths; a branch leaving or going
round a loop without barriers needs none. 55 programs get extra barriers;
all 1,951 emitted compute programs still validate.

Not countable, and emitted unbalanced as before (refused, they would not
run at all): a loop with a barrier left per lane, counts that differ
along uniform paths inside a per-lane branch, and indirect jumps. 33 of
the title's programs, among them compute_graph_ops CS_FormulaOperation,
the luminance histograms and the shadow classification, can still hang.
shortcut: the extra barriers are at the start of the lighter edge, so a
lane there may read LDS after the join before the heavier side's last
writes before it show.

Run in the game on an RTX 2080 since: in the builds here that reached
gameplay through the opening cutscene (with the title's pools made
larger), and in those loading a save straight into the world.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018sSQTfPXZpSkjMMNa7RB5G
… balanced


The title's meshlet culling (CS_BatchCullPreSkinnedMeshlets, which runs
since the culling ports) sends lanes with nothing left to do straight to
the program's end while the others run a section whose barrier count
depends on a uniform choice; no fixed count balances that, so the
program fell back to unbalanced, and it was in the submission of the
last device hang (debug layer: TDR).

A side that meets no more barriers on any path runs to its end and
exits, and a host barrier does not wait for threads that have exited, so
such a branch needs nothing added. shortcut: D3D12 leaves a barrier after
a thread's exit undefined; NVIDIA and AMD count exited threads out (an
early return before a barrier works there).

Unbalanced programs 37 -> 28; the culling's base and _Prev variants are
balanced, its ParaboloidShadows ones are not yet. All programs emit and
validate as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018sSQTfPXZpSkjMMNa7RB5G
…riers run as the console runs them


A per-lane branch leaving (or going round) a loop that holds a group
barrier is decided for the whole wave, as the console decides
s_cbranch_vccz/execz and their nz forms, where that only makes lanes leave
sooner (compute_graph_ops CS_FormulaOperation looped until any lane reached
its own count, and lanes that left first held the others at its barrier).

A program whose barriers still cannot be balanced by counting (53 of the
title's) runs in console mode: every branch on a lane mask decided for the
wave, every vector instruction guarded by its own EXEC, so lanes reach each
barrier together.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018sSQTfPXZpSkjMMNa7RB5G
With it, lower.rs would pass the asset policy's 512 KiB limit for a
file. Moved as is: the barriers a block holds, loops holding them,
branches decided for the wave, the balance and console mode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018sSQTfPXZpSkjMMNa7RB5G

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified head 5d7e5e0c cleanly on origin/main (63bcca43) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + D3D12 WARP + Windows SDK 10.0.26100.0 dxil.dll).

Findings

  1. emit/lower.rs size headroom (98 B below 512 KiB cap): crates/victory-shader/src/emit/lower.rs grows from 520,563 B to 524,190 B — only 98 bytes below cargo xtask policy's 524,288 B limit. Any small concurrent change to lower.rs will trip policy unless a bit more helper logic is moved into emit/lower/barriers.rs.
  2. Edge selection bug in barriers.rs::leaves_loop on barrier-free loop latches: leaves_loop computes (ti, ni) = (inside(r, taken) && !back(b, taken), inside(r, not_taken) && !back(b, not_taken)). When b is the latch of a barrier-free loop whose taken edge is the back-edge and not_taken exits the loop, both ti and ni are false, so if ti { not_taken } else { taken } selects taken (the back-edge) instead of the exit edge not_taken. after(taken) then sees back(b, taken) and returns [0], losing all barrier counts after that loop.
  3. Minor observations:
    • No unit tests are added yet for wave_loop_exits, barrier_balance, or console_mode.
    • s_cbranch_scc0/1 following s_cmp_eq_u64/s_cmp_lg_u64 (which structure.rs models per-lane as !(a ^ c)) would map s_cbranch_scc1 to waveAnyTrue rather than all-lanes-equal in wave_loop_exits / console_branches.

Architecture & Local Verification

  • The three-tier approach (wave_loop_exits -> barrier_balance edge-block padding -> console_mode st.whens + st.execs fallback) preserves group_barrier's UAV fence (1 | 2 | 8) and mesh no_barrier handling cleanly.
  • cargo test --locked -p victory-shader --lib -- emit::flow_tests emit::lower:: emit::crosslane_tests (RTX 5090 + WARP + dxil.dll): 39/39 passed.
  • cargo clippy --locked -p victory-shader --tests -- -D warnings (default and --all-features) & cargo xtask policy (192 files ok): clean.

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

Scoped review: the three-tier design is the honest shape for this — wave-uniform loop exits first, balanced extra barriers where counting permits, and console_mode (whole-wave branch + EXEC guards) as the fallback when balance can't be proven. The named real-world programs (luminance histograms, shadow classification) give it a testable target list. The invariants are subtle — a barrier on an edge must be provably unreachable by lanes that didn't take the branch — so this one deserves a maintainer eye on barrier_balance's counting, which I did not fully trace.

Marvisak commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs work. The new commit bdc3d48 changes three things in crates/victory-shader/src/emit/lower/barriers.rs, and they read right to me:

  • The parked-lane guard now covers the loop's natural body (the header plus the blocks that reach a latch), not an RPO range. So only blocks the header dominates read its phi.
  • Parked lanes are left out of the wave's exit vote in Lower::wave_branch. For the any-lane form the vote is lane & !parked; for the all-lanes form it is lane | parked. That matches EXEC leaving those lanes out on the console.
  • Only loops with a parking latch get the phi and the guards. Every other loop is lowered as on a3e4d54.

CI still fails on this head, with the same failure as before. emit::barrier_tests::a_loop_ended_by_a_mask_compare_goes_round_while_any_lane_is_left panics at barrier_tests.rs:351: pipeline: "The parameter is incorrect.". The runner's SM 6.6 WARP refuses to create the pipeline for the mask-compare loop in console mode, and this test is skipped on your Windows 10 machine. Please run it against an SM 6.6 WARP (for example the redistributable WARP, as CI uses) with the D3D12 debug layer on to get the reason, then fix the lowering rather than the test. A green check would change the verdict.

CI: failing.


Generated by Claude Code

lhnizdo and others added 3 commits November 26, 2026 07:20
…dware adapter and WARP

Review of Marvisak#2920:
- leaves_loop took a loop latch's back edge as the side leaving a loop
  without barriers (and the back edge of a branch going round), whose
  count is 0 over forward edges, losing every barrier after the loop. It
  takes the forward side now: the other side of a back edge, else the
  side leaving the loop.
- SCC of `s_cmp_eq` of two masks is set when all lanes are equal, not
  when any lane has the condition: such a branch is decided the other way
  round for the wave (Lower::wave_test, shared by the loop exits and
  console mode).
- The comments describe the patterns instead of naming programs.
- The wave-wide branch's lowering moves to lower/barriers.rs: lower.rs
  is 522,167 bytes, 2 KiB under the asset policy's limit.

barrier_tests: one group of 64 threads in two waves of 32, hand-written
GFX10 checked against the decoder, each run on the hardware adapter and
WARP, which must complete and store what the console would: a barrier on
one side of a per-lane branch (balanced), a loop with a barrier left per
lane (its exit decided for the wave), a side meeting no more barriers
(it exits, which D3D12 leaves undefined) and the same with a barrier
after the join (console mode).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018sSQTfPXZpSkjMMNa7RB5G
… own exit

A loop whose exit console mode decides for the wave keeps lanes that
would have left going round with the others; on the console EXEC leaves
them out of the rest of the loop. Here their vector instructions still
ran, guarded only by the EXEC the structure works out per instruction,
which cannot say that a lane left in an earlier round: a counter in the
loop counted every lane to the wave's last round (barrier_tests, the
loop left per lane, stored 4 for every thread).

At every loop header a phi says whether a lane is parked, false on
entry. A latch whose other side leaves the loop parks the lanes that
would have left while the wave goes round, and every instruction in the
loop is guarded by !parked too. Only in console mode.

shortcut: only at a latch (the do-while shape); a branch going round
from the middle of a loop keeps its lanes running to the latch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018sSQTfPXZpSkjMMNa7RB5G
…ails without it

- a_loop_ended_by_a_mask_compare_goes_round_while_any_lane_is_left (on
  the hardware adapter and WARP): a loop with a barrier going round while
  `s_cmp_eq_u32 exec_lo, 0` is clear. Without the flip of an equality of
  masks, the wave left after its first lane.
- a_loop_without_barriers_pads_the_side_that_skips_it (lowering only):
  the side holding a loop without barriers, then a barrier, is the
  heavier one. Counted by the latch's back edge, it looked lighter and was
  padded instead (the edge into `v_and_b32` rather than `s_or_b32`).
  NVIDIA's results do not show that, so the lowering's added barriers
  (Lowered::extra_barriers, in tests) are checked.
- a_loop_without_barriers_counts_the_barriers_after_it runs the same
  program on the GPU, with LDS written before the side's barrier and read
  after the join (cleared first: LDS keeps what a dispatch left).

The test programs get 512 bytes of LDS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018sSQTfPXZpSkjMMNa7RB5G
@lhnizdo

lhnizdo commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews, @fuzeofficlal and @kotekompotek, and for the triage. Pushed as f6dfddf, dd15e11 and a3e4d54:

Tests (emit/barrier_tests.rs).

  • The setup: one group of 64 threads in two waves of 32. Hand-written GFX10, whose encodings a test checks against gfx10::decode. Each program is lowered, validated by dxil.dll, and run on the hardware adapter and on WARP. It must complete and store what the console would:
    • a_barrier_on_one_side_of_a_lane_branch_is_balanced: threads 0–15 meet a barrier the others skip, then all meet one after the join.
    • a_loop_with_a_barrier_left_per_lane_ends_for_the_wave: each thread goes round (thread & 3) + 1 times with a barrier in every round, counting rounds under EXEC.
    • a_side_meeting_no_more_barriers_exits: threads 0–15 meet one or two barriers (a uniform choice), the others none to the end. This is the path that relies on exited threads not being waited for, so it runs on both adapters.
    • barriers_that_cannot_be_balanced_run_as_the_console_runs_them: the same with a barrier after the join ({2, 3} against {1}), which goes to console mode.
    • a_loop_ended_by_a_mask_compare_goes_round_while_any_lane_is_left: the s_cmp_eq case below.
    • a_loop_without_barriers_counts_the_barriers_after_it, and a_loop_without_barriers_pads_the_side_that_skips_it (lowering only): the leaves_loop case below.
  • Results: on an RTX 2080 all 8 pass. Windows 10's WARP has no shader model 6.6, so the WARP half skips here. The whole victory-shader suite gives 303 passed, 4 failed, the same 4 as main on this card.
  • They found a real bug: the loop test first stored 4 for every thread instead of its own count. Console mode kept the wave in the loop until its last lane was done (right), but lanes that had finished still ran the loop's vector instructions. The guard is the EXEC the structure works out per instruction, and that can't say a lane left in an earlier round. Fixed in dd15e11: at every loop header a phi says whether a lane is parked. A latch whose exit is decided for the wave parks the lanes that would have left, and every guard inside the loop includes !parked. It only applies in console mode.
    • shortcut: lanes are parked only at a latch (the do-while shape); a branch going round from the middle of a loop keeps its lanes running to the latch.
  • Each fix's test fails without it. I checked by undoing the fix: the mask-compare test fails without the flip. The leaves_loop bug doesn't show in results on NVIDIA (the over-padded side's extra barriers pair harmlessly there). So its test checks the lowering instead: the added barrier must go on the edge into the join, not into the loop's side. That fails without the fix (v_and_b32 instead of s_or_b32).

Fixes.

  • The leaves_loop bug you found, @fuzeofficlal, is real. At a latch of a loop without barriers, it took the back edge, losing every barrier after the loop. The branch going round a loop had the same mistake. It now takes the forward side: the other side of a back edge, else the side leaving the loop.
  • s_cbranch_scc* after s_cmp_eq of two masks is decided the other way round now. SCC is set there when all lanes are equal, while for s_and_b64 and the like, and for s_cmp_lg, it's set when any lane has the condition.
    • The mapping is in one place, Lower::wave_test, used by both the loop exits and console mode.
    • shortcut: only SCC set in the branch's own block is looked at.
  • On console_branches skipping lane branches that aren't s_cbranch_*: a lane condition only reaches a branch through VCC, EXEC or SCC in GFX10, and those six are the conditional forms on them. Anything else ends a block another way (s_setpc is an indirect jump, which barrier_balance already refuses).
  • The comments describe the patterns instead of naming programs.
  • lower.rs headroom: the wave-wide branch's lowering moved to lower/barriers.rs, so lower.rs is 523,668 bytes with the parked lanes, still under the limit.

On the early exit: if the hardware and WARP runs ever disagree here, the fallback is to send such programs to console mode instead (they'd cost guards but not rely on undefined behaviour). It's a one-line change in barrier_balance, and I can make it the default if you'd prefer not to rely on it at all.

…and only in loops that park

Three corrections to the parked lanes (dd15e11):
- The guard and the latch test use the loop's natural body (the header
  and the blocks reaching a latch without passing it) instead of an RPO
  range, which could hold blocks beside the loop that the header's phi
  does not dominate.
- Parked lanes take no part in the wave's exit test: on the console EXEC
  leaves them out, and their frozen registers could keep saying "go on".
  For any lane only unparked ones count; for all lanes parked ones agree.
- Only loops with a latch that parks (its exit decided for the wave, the
  other side leaving the loop) get the phi and the guards; every other
  loop is lowered as before.

victory-shader on an RTX 2080: 306 passed, 4 failed (the same 4 as main
on this card); the 8 barrier tests pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018sSQTfPXZpSkjMMNa7RB5G
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.

4 participants