Skip to content

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

Open
rivaldodev wants to merge 1 commit into
Marvisak:mainfrom
rivaldodev:nvidia/narrow-wave-barriers
Open

rivaldodev wants to merge 1 commit into
Marvisak:mainfrom
rivaldodev:nvidia/narrow-wave-barriers

Conversation

@rivaldodev

@rivaldodev rivaldodev commented Nov 25, 2026 •

Copy link
Copy Markdown

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.

@jeremygold02

Copy link
Copy Markdown
Contributor

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. Descriptors: clamp view indices past a capped host heap to a null slot #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.

@rivaldodev
rivaldodev force-pushed the nvidia/narrow-wave-barriers branch from 2a5fe15 to 5b34703 Compare November 25, 2026 04:05
@rivaldodev rivaldodev changed the title Shaders: narrow-wave host barrier handling Shaders: drop group sync only for divergent single-console-wave barriers on narrow-wave hosts Nov 25, 2026
@rivaldodev
rivaldodev marked this pull request as ready for review November 25, 2026 04:05
Copilot AI balanced review requested due to automatic review settings November 25, 2026 04:05

Copilot AI 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.

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.

@rivaldodev
rivaldodev force-pushed the nvidia/narrow-wave-barriers branch from 5b34703 to bfb7c39 Compare November 25, 2026 04:09
@akimfomin

Copy link
Copy Markdown

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

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

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, 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.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump

Result: no count change against main.

I merged head bfb7c39 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).

@rivaldodev
rivaldodev force-pushed the nvidia/narrow-wave-barriers branch from bfb7c39 to c61da5c Compare November 25, 2026 12:14
@rivaldodev rivaldodev changed the title Shaders: drop group sync only for divergent single-console-wave barriers on narrow-wave hosts Shaders: drop group sync for race-free divergent single-wave barriers on narrow-wave hosts Nov 25, 2026
@rivaldodev

Copy link
Copy Markdown
Author

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 Shaders: replace single-wave Wave64 LDS lockstep with barriers and lower 32-lane swizzle/DPP at WaveSize(32) #192 / Shaders: console flow for groups of several console waves on 32-lane hosts #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.

HoLa-Lee pushed a commit to HoLa-Lee/victory-wave-wide-barriers that referenced this pull request Nov 25, 2026
docs/value-vs-upstream.md: the most-starred forks are all doing shader fidelity (JMAN730 42 stars,
DexNom wave64 masks, PastiiJay FidelityFX) and nobody measures frame composition, host memory or how
to drive the menus. Group by group: rpcrt4 and Marvisak#928 are upstream's; COPY_PLAIN, FRAME_LOG,
DRIVER_WATCH supersede ours; the texture-refresh numbers complement Marvisak#388 and Marvisak#1873; the staging
budget, the page-bitmap detile, the pad-stick gap (Marvisak#1702's missing half), the terms page, the
committed saves, the wave64 sentence (Marvisak#2105/Marvisak#599/Marvisak#2035), the ALERT_ERROR_SUPPORT trap and the
memory series complement Marvisak#1479 - with what is worth sending where.
… on narrow-wave hosts

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.
@rivaldodev
rivaldodev force-pushed the nvidia/narrow-wave-barriers branch from c61da5c to dca7588 Compare November 26, 2026 13:08
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.

7 participants