Shaders: drop group sync for race-free divergent single-wave barriers on narrow-wave hosts #2035

Open
github-bot wants to merge 1 commit from github-bot/gh-2035 into main AGit
Owner

@⁠rivaldodev on GitHub · 2026-11-25 03:49 UTC · original
rivaldodev:nvidia/narrow-wave-barriers → main · +478 −9 · 5 files · 1 commits · open

Split out from #1133 and narrowed to address all triage and reviewer feedback.

Problem

On the console, s_barrier waits per wave: a wave whose lanes skip the barrier still arrives. In translated DXIL, control flow runs per lane, so a barrier inside a lane-dependent branch or in a loop with a lane-dependent exit is reached by only some threads. On a wave32 host (e.g. NVIDIA) or an unpinned shader on a 32–64 adapter, one 64-thread console wave spans two host waves, and a host wave that skips GroupMemoryBarrierWithGroupSync hangs the GPU.

Narrow, race-free approach (addressing triage, @jeremygold02, and @akimfomin)

  1. Keyed on the shader's effective minimum wave width (WaveLaneCountMin, not WaveLaneCountMax):
    • crates/victory-gpu/src/host.rs records D3D12_OPTIONS1::WaveLaneCountMin via victory_shader::emit::set_host_wave_min.
    • Lower::barrier_unsynced uses let effective_wave = self.s.wave_size.unwrap_or_else(super::host_wave_min);.
    • Shaders pinned to WaveSize(64) keep effective_wave == 64 (never drop sync); unpinned shaders on 32–64 or 32-only adapters use WaveLaneCountMin (32); 64-only adapters use 64 (translate identically to main).
  2. Provably data-race-free omission (!has_shared_rw + single console wave):
    • should_drop_barrier_sync only drops SyncThreadGroup (keeping memory fences 2 | 8) when:
      • effective_wave < console_wave, AND
      • the thread group fits in one console wave (group_threads <= console_wave), AND
      • !has_shared_rw: the shader uses zero LDS (s.lds_dwords == 0) and does not both write and read memory across buffers, images, or Flat global memory (so no invocation can read data written by another invocation across the barrier — eliminating both LDS and UAV/Flat-global read-after-write races).
    • Multi-console-wave groups, uniform barriers, and any shader with LDS or shared read+write memory effects always keep SyncThreadGroup.
  3. Only mark loops with lane-dependent exits:
    • lane_exit_loop_blocks groups back-edges by loop header and only marks a loop when an exiting block (with a successor outside the loop body) has a lane-dependent branch.
  4. Shader cache key & documented side effect:
    • shader_cache::build() keys on host_wave_min() and documents its side effect (initializing crate::host::host() on first call so WaveLaneCountMin is known before the cache key is fixed).

Tests & Hardware Proof

  • emit::lower::tests::divergent_single_wave_barrier_runs_on_narrow_wave_hardware (live GPU dispatch + DXIL validation):
    • 64-thread wave64 compute shader (zero LDS, write-only UAV output) with s_barrier inside if (lane < below) (below = 1, 10, 32, 33, 64).
    • Tested on RTX 4070 Ti SUPER (WaveLaneCountMin=32, Max=32): completes without hanging and all 64 lanes write the exact reference output (19 + lane).
  • emit::lower::tests::lane_loop_blocks_only_marks_loops_with_lane_dependent_exits: verifies uniform-exit loops with inner lane branches are NOT marked, while lane-dependent header exits and early breaks ARE marked.
  • emit::lower::tests::narrow_wave_barrier_sync_only_drops_for_divergent_single_console_wave_groups: verifies 64-only / WaveSize(64)-pinned, uniform barriers, has_shared_rw (LDS / UAV+Flat R/W), multi-console-wave groups, and unknown group sizes all keep SyncThreadGroup.
  • victory-gpu shader_cache::tests: 3 passed, 0 failed.
**@⁠rivaldodev** on GitHub · 2026-11-25 03:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/2035) `rivaldodev:nvidia/narrow-wave-barriers` → `main` · +478 −9 · 5 files · 1 commits · open Split out from #1133 and narrowed to address all triage and reviewer feedback. ### Problem On the console, `s_barrier` waits per wave: a wave whose lanes skip the barrier still arrives. In translated DXIL, control flow runs per lane, so a barrier inside a lane-dependent branch or in a loop with a lane-dependent exit is reached by only some threads. On a wave32 host (e.g. NVIDIA) or an unpinned shader on a 32–64 adapter, one 64-thread console wave spans two host waves, and a host wave that skips `GroupMemoryBarrierWithGroupSync` hangs the GPU. ### Narrow, race-free approach (addressing triage, `@jeremygold02`, and `@akimfomin`) 1. **Keyed on the shader's effective minimum wave width (`WaveLaneCountMin`, not `WaveLaneCountMax`):** - `crates/victory-gpu/src/host.rs` records `D3D12_OPTIONS1::WaveLaneCountMin` via `victory_shader::emit::set_host_wave_min`. - `Lower::barrier_unsynced` uses `let effective_wave = self.s.wave_size.unwrap_or_else(super::host_wave_min);`. - Shaders pinned to `WaveSize(64)` keep `effective_wave == 64` (never drop sync); unpinned shaders on 32–64 or 32-only adapters use `WaveLaneCountMin` (32); 64-only adapters use 64 (translate identically to `main`). 2. **Provably data-race-free omission (`!has_shared_rw` + single console wave):** - `should_drop_barrier_sync` only drops `SyncThreadGroup` (keeping memory fences `2 | 8`) when: - `effective_wave < console_wave`, AND - the thread group fits in **one console wave** (`group_threads <= console_wave`), AND - **`!has_shared_rw`**: the shader uses **zero LDS** (`s.lds_dwords == 0`) and does **not** both write and read memory across buffers, images, or `Flat` global memory (so no invocation can read data written by another invocation across the barrier — eliminating both LDS and UAV/Flat-global read-after-write races). - Multi-console-wave groups, uniform barriers, and any shader with LDS or shared read+write memory effects always keep `SyncThreadGroup`. 3. **Only mark loops with lane-dependent exits:** - `lane_exit_loop_blocks` groups back-edges by loop header and only marks a loop when an exiting block (with a successor outside the loop body) has a lane-dependent branch. 4. **Shader cache key & documented side effect:** - `shader_cache::build()` keys on `host_wave_min()` and documents its side effect (initializing `crate::host::host()` on first call so `WaveLaneCountMin` is known before the cache key is fixed). ### Tests & Hardware Proof - **`emit::lower::tests::divergent_single_wave_barrier_runs_on_narrow_wave_hardware`** (live GPU dispatch + DXIL validation): - 64-thread wave64 compute shader (zero LDS, write-only UAV output) with `s_barrier` inside `if (lane < below)` (`below = 1, 10, 32, 33, 64`). - Tested on **RTX 4070 Ti SUPER** (`WaveLaneCountMin=32, Max=32`): completes without hanging and all 64 lanes write the exact reference output (`19 + lane`). - **`emit::lower::tests::lane_loop_blocks_only_marks_loops_with_lane_dependent_exits`**: verifies uniform-exit loops with inner lane branches are NOT marked, while lane-dependent header exits and early breaks ARE marked. - **`emit::lower::tests::narrow_wave_barrier_sync_only_drops_for_divergent_single_console_wave_groups`**: verifies 64-only / `WaveSize(64)`-pinned, uniform barriers, `has_shared_rw` (LDS / UAV+Flat R/W), multi-console-wave groups, and unknown group sizes all keep `SyncThreadGroup`. - **`victory-gpu` `shader_cache::tests`**: `3 passed, 0 failed`.
Keys the rule on the shader's effective minimum wave width (s.wave_size or D3D12_OPTIONS1::WaveLaneCountMin), restricts omitting SyncThreadGroup to single-console-wave groups with no shared read/write memory effects (no LDS and not both reading and writing buffers/images/flat global memory), refines lane_loop_blocks to exiting branches only, documents shader_cache::build's host initialization side effect, and adds unit tests plus a live narrow-wave GPU dispatch test.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:52:18 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-25 03:54 UTC · original

Triage verdict — Keep as a draft and verify the selected wave width (2a5fe15)

Evidence

  • @⁠rivaldodev's split gives the shader synchronization work its own review. #1133 can be evaluated separately for its descriptor-heap cap handling.
  • WaveLaneCountMax is the maximum supported width. An unpinned shader on an adapter reporting a 32–64 range can still run with wave32, but barrier_unsynced treats it like a 64-only adapter. In the other direction, the emitter already requests exact WaveSize(64) for certain console-wave64 compute shaders, including a group of at most one console wave with LDS. On a 32-only adapter, such a PSO can fail validation before the barrier executes. Barrier rule · Wave size emission · D3D12 WaveSize specification

Checks and limits

  • This is a static comparison of the host capability query, the emitted wave-size contract, and the barrier selection. Dispatch and LDS output remain unverified.

Next step

  • In addition to the LDS cases already on the checklist, distinguish 32-only, 32–64, and 64-only capability ranges in the tests. For each relevant shader, check its emitted wave-size requirement and whether the PSO can be created before using the dispatch result to judge the barrier rule.

This verification was AI-assisted.

**@⁠jeremygold02** on GitHub · 2026-11-25 03:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/2035#issuecomment-7152356092) **Triage verdict — Keep as a draft and verify the selected wave width** (`2a5fe15`) **Evidence** - @⁠rivaldodev's split gives the shader synchronization work its own review. #1133 can be evaluated separately for its descriptor-heap cap handling. - `WaveLaneCountMax` is the maximum supported width. An unpinned shader on an adapter reporting a 32–64 range can still run with wave32, but `barrier_unsynced` treats it like a 64-only adapter. In the other direction, the emitter already requests exact `WaveSize(64)` for certain console-wave64 compute shaders, including a group of at most one console wave with LDS. On a 32-only adapter, such a PSO can fail validation before the barrier executes. [Barrier rule](https://github.com/Marvisak/VIctory/blob/2a5fe1549556fb172d4dd36fe5e73cdc3dc1da38/crates/victory-shader/src/emit/lower.rs#L2912-L2938) · [Wave size emission](https://github.com/Marvisak/VIctory/blob/2a5fe1549556fb172d4dd36fe5e73cdc3dc1da38/crates/victory-shader/src/emit/lower.rs#L563-L605) · [D3D12 WaveSize specification](https://microsoft.github.io/DirectX-Specs/d3d/HLSL_SM_6_6_WaveSize.html) **Checks and limits** - This is a static comparison of the host capability query, the emitted wave-size contract, and the barrier selection. Dispatch and LDS output remain unverified. **Next step** - In addition to the LDS cases already on the checklist, distinguish 32-only, 32–64, and 64-only capability ranges in the tests. For each relevant shader, check its emitted wave-size requirement and whether the PSO can be created before using the dispatch result to judge the barrier rule. This verification was AI-assisted.
Author
Owner

@⁠akimfomin on GitHub · 2026-11-25 04:18 UTC · original

I used PortBridge to review the current bfb7c39c source after the loop and multi-console-wave updates. Your revised detector preserves the converged diamond. A separate case remains for one 64-thread console wave split into two host wave32s, with zero LDS and a shared UAV dependency.

A lane-dependent loop exit can mark a barrier that every thread reaches in the first iteration. If one host wave writes data before that first arrival and the other reads after it, the mode 11 → 10 change permits the read of the old value. The static control grants a completed store full visibility. The difference follows from the missing arrival dependency, separate from the selected-wave-capability question. The existing writes_uavs check also misses Flat global stores and atomics, so zero LDS and that predicate together cannot prove the path independent.

Exact pinned source checks and finite schedule model. I did not build or dispatch a potentially nonuniform barrier shader. The narrow safe omission rule needs positive evidence that no other invocation can observe the ordering. For shared effects, #192 and #1433 already cover broader convergent approaches. AI-assisted PortBridge review (OpenAI Codex).

**@⁠akimfomin** on GitHub · 2026-11-25 04:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/2035#issuecomment-7152730898) I used [PortBridge](https://github.com/akimfomin/PortBridge) to review the current `bfb7c39c` source after the loop and multi-console-wave updates. Your revised detector preserves the converged diamond. A separate case remains for one 64-thread console wave split into two host wave32s, with zero LDS and a shared UAV dependency. A lane-dependent loop exit can mark a barrier that every thread reaches in the first iteration. If one host wave writes data before that first arrival and the other reads after it, the mode 11 → 10 change permits the read of the old value. The static control grants a completed store full visibility. The difference follows from the missing arrival dependency, separate from the selected-wave-capability question. The existing `writes_uavs` check also misses Flat global stores and atomics, so zero LDS and that predicate together cannot prove the path independent. [Exact pinned source checks and finite schedule model](https://github.com/akimfomin/PortBridge/blob/0489c2adb709e285e6a6b5ce577fe9e931223ebb/research/rendering-night/victory-2035-barrier/BARRIER-REVIEW.md). I did not build or dispatch a potentially nonuniform barrier shader. The narrow safe omission rule needs positive evidence that no other invocation can observe the ordering. For shared effects, #192 and #1433 already cover broader convergent approaches. AI-assisted PortBridge review (OpenAI Codex).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 09:18 UTC · original · edited 2026-11-25 22:19 UTC

Automated triage for the maintainers.

Needs rebase. This PR no longer merges cleanly into main (conflict in crates/victory-shader/src/emit/lower.rs). It will be reviewed again once it is rebased.

From the earlier needs-work note, still open: show what the new test does on main on wave32 hardware, and handle (or explain) write-only shaders where the barrier orders two waves' writes.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 09:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/2035#issuecomment-7158156124) · edited 2026-11-25 22:19 UTC <!-- victory-triage v1 head=c61da5c700a9bded2d571d90070116072b8616af verdict=needs-rebase at=2026-11-25T22:24:40Z --> Automated triage for the maintainers. **Needs rebase.** This PR no longer merges cleanly into `main` (conflict in `crates/victory-shader/src/emit/lower.rs`). It will be reviewed again once it is rebased. From the earlier needs-work note, still open: show what the new test does on `main` on wave32 hardware, and handle (or explain) write-only shaders where the barrier orders two waves' writes. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠sinan-cmyk on GitHub · 2026-11-25 09:45 UTC · original

Offline measurement on a retail dump

Result: no count change against main.

I merged head bfb7c39c onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

**@⁠sinan-cmyk** on GitHub · 2026-11-25 09:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/2035#issuecomment-7158760353) ### Offline measurement on a retail dump **Result: no count change against `main`.** I merged head bfb7c39c onto `main` 940d32f (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 23,918 on `main`, 23,918 with this PR. No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
Author
Owner

@⁠rivaldodev on GitHub · 2026-11-25 12:14 UTC · original

Addressed all points from triage, @jeremygold02, and @akimfomin:

  1. Effective wave width (WaveLaneCountMin + s.wave_size): Switched from WaveLaneCountMax to WaveLaneCountMin (host_wave_min()) and keyed Lower::barrier_unsynced on self.s.wave_size.unwrap_or_else(super::host_wave_min) so WaveSize(64)-pinned shaders keep sync, unpinned shaders on 32–64 adapters are handled conservatively, and 64-only adapters are unchanged.
  2. No LDS/UAV/Flat-global data races (!has_shared_rw): Restricted dropping SyncThreadGroup to shaders with zero LDS (s.lds_dwords == 0) that do not both write and read across buffers, images, or Flat global memory (including atomics). Any shader with LDS or shared read+write memory effects keeps SyncThreadGroup, leaving broader convergent synchronization to #192 / #1433.
  3. Live wave32 hardware dispatch test: Added emit::lower::tests::divergent_single_wave_barrier_runs_on_narrow_wave_hardware, which validates DXIL and dispatches a 64-thread wave64 compute shader with a divergent s_barrier (lane < below for below = 1, 10, 32, 33, 64) on live wave32 hardware (RTX 4070 Ti SUPER, WaveLaneCountMin=32, Max=32), checking all 64 lanes against the exact reference output (19 + lane).
  4. Documented shader_cache::build side effect: Noted on both use_shader_cache and build in shader_cache.rs and in the PR description.

Ready for re-triage.

**@⁠rivaldodev** on GitHub · 2026-11-25 12:14 UTC · [original](https://github.com/Marvisak/VIctory/pull/2035#issuecomment-7162115545) Addressed all points from triage, `@jeremygold02`, and `@akimfomin`: 1. **Effective wave width (`WaveLaneCountMin` + `s.wave_size`):** Switched from `WaveLaneCountMax` to `WaveLaneCountMin` (`host_wave_min()`) and keyed `Lower::barrier_unsynced` on `self.s.wave_size.unwrap_or_else(super::host_wave_min)` so `WaveSize(64)`-pinned shaders keep sync, unpinned shaders on 32–64 adapters are handled conservatively, and 64-only adapters are unchanged. 2. **No LDS/UAV/Flat-global data races (`!has_shared_rw`):** Restricted dropping `SyncThreadGroup` to shaders with zero LDS (`s.lds_dwords == 0`) that do **not** both write and read across buffers, images, or `Flat` global memory (including atomics). Any shader with LDS or shared read+write memory effects keeps `SyncThreadGroup`, leaving broader convergent synchronization to #192 / #1433. 3. **Live wave32 hardware dispatch test:** Added `emit::lower::tests::divergent_single_wave_barrier_runs_on_narrow_wave_hardware`, which validates DXIL and dispatches a 64-thread wave64 compute shader with a divergent `s_barrier` (`lane < below` for `below = 1, 10, 32, 33, 64`) on live wave32 hardware (RTX 4070 Ti SUPER, `WaveLaneCountMin=32, Max=32`), checking all 64 lanes against the exact reference output (`19 + lane`). 4. **Documented `shader_cache::build` side effect:** Noted on both `use_shader_cache` and `build` in `shader_cache.rs` and in the PR description. Ready for re-triage.
github-bot force-pushed github-bot/gh-2035 from c61da5c700 to dca7588655 2026-11-26 13:13:14 +00:00 Compare
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 04:05 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 04:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/2035#pullrequestreview-6051707111) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Author
Owner

@⁠catminou4 on GitHub · 2026-11-25 04:25 UTC · original · approved these changes

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, cargo test -p victory-shader --locked.

146 passed / 0 failed / 2 ignored. Narrow-wave host barrier handling (+2 tests) — barrier lowering for sub-32-lane waves; mixed gpu+shader, gpu half Windows-only.

**@⁠catminou4** on GitHub · 2026-11-25 04:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/2035#pullrequestreview-6051847488) · **approved these changes** Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, `cargo test -p victory-shader --locked`. 146 passed / 0 failed / 2 ignored. Narrow-wave host barrier handling (+2 tests) — barrier lowering for sub-32-lane waves; mixed gpu+shader, gpu half Windows-only.
github-bot force-pushed github-bot/gh-2035 from dca7588655 to 00018fa140 2026-11-26 16:39:31 +00:00 Compare
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2035/head:github-bot/gh-2035'
git switch 'github-bot/gh-2035'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2035'
git switch 'github-bot/gh-2035'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2035'
git switch 'github-bot/gh-2035'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2035'
git switch 'main'
git merge --squash 'github-bot/gh-2035'
git switch 'main'
git merge --ff-only 'github-bot/gh-2035'
git switch 'main'
git merge 'github-bot/gh-2035'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2035
No description provided.