Shaders: guard divergent wave64 loop latches and fence flat UAV barriers #894

Closed
github-bot wants to merge 0 commits from github-bot/gh-894 into main AGit
Owner

@⁠Starchaser1024 on GitHub · 2026-11-28 08:40 UTC · original
Starchaser1024:fix/wave64-loop-latch-barriers → main · +694 −34 · 4 files · 1 commits · closed without merging

Summary

Two barrier-correctness fixes in victory-shader DXIL lowering for 32-lane hosts (NVIDIA) and compute shaders writing global memory by 64-bit address:

1. Divergent Wave64 loop-closing blocks (lane_regions_of, crates/victory-shader/src/emit/lower.rs, mod.rs)

  • Cause: lane_regions_of excluded loop-closing branches (b == d) from their own divergent region unless PAIR_MEETINGS.get() was set (PAIR_MEETINGS is false for all default Wave64->Wave32 lowerings and 64-thread widen / driver_loop runs). Because every block post-dominates itself (pdom[d] includes d), single-block GFX10 loops (head == latch) and multi-block loop latches (head -> latch) were marked all[latch] == true, allowing CTA-wide GroupMemoryBarrierWithGroupSync (dx.op.barrier(9)) inside exchange_halves and lds_meetings when the two 32-lane halves of a 64-lane wave exit on different iterations, and causing branches_around to return an empty set on 1-block loops.
  • Fix:
    • Unconditionally keep b == d inside its divergent region (b != d in lane_regions_of).
    • Exempt lds_order.looped atomics from lds_meetings (lds_no_meeting) and emit their non-waiting shared-memory fence (dx.op.barrier(8)) ahead of lds_meet so CS_SortRayTiles-style divergent atomic loops continue lowering directly to wave = 32 without widening.
    • Only clear lower::take_needs() in emit_program when WAVE-LOOPS lowers below 64 lanes, preserving NEEDS so widen() widens divergent loop LDS blocks cleanly.

2. UAV_FENCE_GLOBAL on s_barrier for Fields::Flat stores & atomics (writes_uavs, lower.rs)

  • Cause: writes_uavs (lower.rs:1735-1738) checked Fields::Image | Fields::Buffer while omitting Fields::Flat. Compute shaders mutating memory exclusively via global_store_* or global_atomic_* (SLOT_MEMORY / BVH / culling passes) emitted dx.op.barrier(9) (TGSM + group sync) without UAV_FENCE_GLOBAL (bit 2), leaving cross-wave global memory writes unfenced across s_barrier.
  • Fix: Include non-load Fields::Flat global instructions (global_store_*, global_atomic_*) in writes_uavs so s_barrier emits mode = 11 (1 | 2 | 8).

Verification (RTX 5060 Ti 16 GB + WARP + dxil.dll, Windows 11)

  • emit::wave64_tests::one_console_wave_divergent_loop_lds_widens_for_one_and_two_block_loops: 64-thread Wave64 compute test (lanes 0..31 run 2 rounds, lanes 32..63 run 4 rounds) for 1-block and 2-block ordered-LDS loops; dxil.dll validated and executed on RTX 5060 Ti.
  • emit::wave64_tests::one_console_wave_divergent_loop_wave_ops_widen_for_one_and_two_block_loops: 64-thread Wave64 compute test (2 vs 4 rounds, 1-block and 2-block) across cross-half wave reduction, v_readfirstlane_b32, and s_bcnt1_i32_b64 exec 64-bit ballot; dxil.dll validated and executed on RTX 5060 Ti.
  • emit::wave64_tests::cs_sort_ray_tiles_divergent_loop_atomics_stay_wave32_with_fence_only: verifies direct wave = 32 lowering, fence-only dx.op.barrier(8) emission, dxil.dll validation, and collision-free ticket output on RTX 5060 Ti.
  • emit::intersect_tests::flat_global_stores_and_atomics_fence_uavs_at_group_barriers_across_waves: 128-thread (4 Wave32 waves) cross-wave producer/consumer test verifying dx.op.barrier(11) emission, dxil.dll validation, and exact readback on both RTX 5060 Ti and WARP.
  • cargo test -p victory-shader --locked: 297 passed, 0 failed.
  • cargo clippy -p victory-shader --all-targets --all-features --locked: 0 warnings in changed files.
  • cargo xtask policy: 294 files ok.
**@⁠Starchaser1024** on GitHub · 2026-11-28 08:40 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/894) `Starchaser1024:fix/wave64-loop-latch-barriers` → `main` · +694 −34 · 4 files · 1 commits · closed without merging ## Summary Two barrier-correctness fixes in `victory-shader` DXIL lowering for 32-lane hosts (NVIDIA) and compute shaders writing global memory by 64-bit address: ### 1. Divergent Wave64 loop-closing blocks (`lane_regions_of`, `crates/victory-shader/src/emit/lower.rs`, `mod.rs`) - **Cause**: `lane_regions_of` excluded loop-closing branches (`b == d`) from their own divergent region unless `PAIR_MEETINGS.get()` was set (`PAIR_MEETINGS` is `false` for all default Wave64->Wave32 lowerings and 64-thread `widen` / `driver_loop` runs). Because every block post-dominates itself (`pdom[d]` includes `d`), single-block GFX10 loops (`head == latch`) and multi-block loop latches (`head -> latch`) were marked `all[latch] == true`, allowing CTA-wide `GroupMemoryBarrierWithGroupSync` (`dx.op.barrier(9)`) inside `exchange_halves` and `lds_meetings` when the two 32-lane halves of a 64-lane wave exit on different iterations, and causing `branches_around` to return an empty set on 1-block loops. - **Fix**: - Unconditionally keep `b == d` inside its divergent region (`b != d` in `lane_regions_of`). - Exempt `lds_order.looped` atomics from `lds_meetings` (`lds_no_meeting`) and emit their non-waiting shared-memory fence (`dx.op.barrier(8)`) ahead of `lds_meet` so `CS_SortRayTiles`-style divergent atomic loops continue lowering directly to `wave = 32` without widening. - Only clear `lower::take_needs()` in `emit_program` when `WAVE-LOOPS` lowers below 64 lanes, preserving `NEEDS` so `widen()` widens divergent loop LDS blocks cleanly. ### 2. `UAV_FENCE_GLOBAL` on `s_barrier` for `Fields::Flat` stores & atomics (`writes_uavs`, `lower.rs`) - **Cause**: `writes_uavs` (`lower.rs:1735-1738`) checked `Fields::Image | Fields::Buffer` while omitting `Fields::Flat`. Compute shaders mutating memory exclusively via `global_store_*` or `global_atomic_*` (`SLOT_MEMORY` / BVH / culling passes) emitted `dx.op.barrier(9)` (`TGSM` + group sync) without `UAV_FENCE_GLOBAL` (bit `2`), leaving cross-wave global memory writes unfenced across `s_barrier`. - **Fix**: Include non-load `Fields::Flat` global instructions (`global_store_*`, `global_atomic_*`) in `writes_uavs` so `s_barrier` emits `mode = 11` (`1 | 2 | 8`). ## Verification (RTX 5060 Ti 16 GB + WARP + `dxil.dll`, Windows 11) - `emit::wave64_tests::one_console_wave_divergent_loop_lds_widens_for_one_and_two_block_loops`: 64-thread Wave64 compute test (lanes `0..31` run 2 rounds, lanes `32..63` run 4 rounds) for 1-block and 2-block ordered-LDS loops; `dxil.dll` validated and executed on RTX 5060 Ti. - `emit::wave64_tests::one_console_wave_divergent_loop_wave_ops_widen_for_one_and_two_block_loops`: 64-thread Wave64 compute test (2 vs 4 rounds, 1-block and 2-block) across cross-half wave reduction, `v_readfirstlane_b32`, and `s_bcnt1_i32_b64 exec` 64-bit ballot; `dxil.dll` validated and executed on RTX 5060 Ti. - `emit::wave64_tests::cs_sort_ray_tiles_divergent_loop_atomics_stay_wave32_with_fence_only`: verifies direct `wave = 32` lowering, fence-only `dx.op.barrier(8)` emission, `dxil.dll` validation, and collision-free ticket output on RTX 5060 Ti. - `emit::intersect_tests::flat_global_stores_and_atomics_fence_uavs_at_group_barriers_across_waves`: 128-thread (4 Wave32 waves) cross-wave producer/consumer test verifying `dx.op.barrier(11)` emission, `dxil.dll` validation, and exact readback on both RTX 5060 Ti and WARP. - `cargo test -p victory-shader --locked`: 297 passed, 0 failed. - `cargo clippy -p victory-shader --all-targets --all-features --locked`: 0 warnings in changed files. - `cargo xtask policy`: 294 files ok.
Two barrier-correctness fixes in `victory-shader` DXIL lowering:

1. Unconditionally include loop-closing branches (`b == d`) in their own
   divergent region in `lane_regions_of` instead of gating on
   `PAIR_MEETINGS`. Previously, in 64-thread Wave64 compute shaders on
   32-lane hosts (NVIDIA), a loop latch post-dominated itself and was
   marked all-lane (`all[latch] == true`), allowing group-wide barriers
   (`exchange_halves` or `lds_meetings`) inside per-lane and host-wide
   loops where one 32-lane half exits earlier than the other. Exempt
   `lds_order.looped` atomics from `lds_meetings` and emit their
   non-waiting shared-memory fence (`mode = 8`) before checking
   `lds_meet` so `CS_SortRayTiles`-style atomic loops stay in Wave32
   without widening, and keep `NEEDS` intact when `WAVE-LOOPS` still
   requires 64 lanes so `widen()` can widen divergent LDS loops.

2. Include `Fields::Flat` global stores and atomics (`global_store_*`,
   `global_atomic_*`) in `writes_uavs` so `s_barrier` emits
   `UAV_FENCE_GLOBAL` (bit 2, `mode = 11`) in compute shaders that write
   memory exclusively through 64-bit global addresses.

Add `dxil.dll`-validated GPU execution tests covering 1-block and
2-block divergent Wave64 loops (ordered LDS, wave reduction, first-lane
read, 64-bit ballot, and `CS_SortRayTiles`-style looped LDS atomics) and
multi-wave `global_store_dword` / `global_atomic_add` + `s_barrier`
visibility across waves.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 14:29:43 +00:00
Author
Owner

@⁠Aniseto on GitHub · 2026-11-28 09:21 UTC · original

Ran the divergent-loop barrier tests at 72883f5 on an NVIDIA Blackwell desktop part:

Environment: Windows 11 Pro build 26200 x64, NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only, 16 GB) driver 32.0.16.2124, Intel Core i5-12400F, 64 GB RAM, Rust 1.98.1 (pinned by rust-toolchain.toml), MSVC 14.44.35207, DXC 1.9.2609.5 (shared env), debug build.

cargo test -p victory-shader --lib -- divergent_loop cs_sort_ray_tiles flat_global_stores --test-threads=1
5 passed; 0 failed; 0 ignored; 0 measured; 297 filtered out      (exit 0)

All five matching tests pass, including the divergent 64-thread loop cases for one- and two-block loops, the CS_SortRayTiles wave32-with-fence-only case, and the cross-wave flat UAV-store barrier test. Three of them run a dispatch on the real 32-lane adapter; the rest are translation-level. The PR has no comments yet, so this is the first datapoint in the thread and the first from an RTX 5070 Ti.

Not tested: the title's ray-tile shaders, a paired smoke run, the full workspace suite, release profile, AMD and Intel GPUs, native wave64 hosts, Wine, game data.

AI-assisted (OpenCode), model: DeepSeek V4.1 Flash Free - checked out the PR's head commit in a detached worktree, built it, ran the tests, wrote this report; the worktree was not modified; no game data was used.

**@⁠Aniseto** on GitHub · 2026-11-28 09:21 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/894#issuecomment-7237185471) Ran the divergent-loop barrier tests at `72883f5` on an NVIDIA Blackwell desktop part: Environment: Windows 11 Pro build 26200 x64, NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only, 16 GB) driver 32.0.16.2124, Intel Core i5-12400F, 64 GB RAM, Rust 1.98.1 (pinned by `rust-toolchain.toml`), MSVC 14.44.35207, DXC 1.9.2609.5 (shared env), debug build. ``` cargo test -p victory-shader --lib -- divergent_loop cs_sort_ray_tiles flat_global_stores --test-threads=1 5 passed; 0 failed; 0 ignored; 0 measured; 297 filtered out (exit 0) ``` All five matching tests pass, including the divergent 64-thread loop cases for one- and two-block loops, the `CS_SortRayTiles` wave32-with-fence-only case, and the cross-wave flat UAV-store barrier test. Three of them run a dispatch on the real 32-lane adapter; the rest are translation-level. The PR has no comments yet, so this is the first datapoint in the thread and the first from an RTX 5070 Ti. Not tested: the title's ray-tile shaders, a paired smoke run, the full workspace suite, release profile, AMD and Intel GPUs, native wave64 hosts, Wine, game data. AI-assisted (OpenCode), model: **DeepSeek V4.1 Flash Free** - checked out the PR's head commit in a detached worktree, built it, ran the tests, wrote this report; the worktree was not modified; no game data was used.
github-bot changed title from Shaders: guard divergent wave64 loop latches and fence flat UAV barriers to [pr-fix][shaders][NVIDIA] Shaders: guard divergent wave64 loop latches and fence flat UAV barriers 2026-11-29 01:55:16 +00:00
github-bot force-pushed github-bot/gh-894 from 72883f56dd to 788fe236f7 2026-11-29 02:17:32 +00:00 Compare
github-bot changed title from [pr-fix][shaders][NVIDIA] Shaders: guard divergent wave64 loop latches and fence flat UAV barriers to Shaders: guard divergent wave64 loop latches and fence flat UAV barriers 2026-11-29 02:28:37 +00:00
github-bot closed this pull request 2026-11-29 07:48:23 +00:00

Pull request closed

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/trustno1-coder--VIctory-NVIDIA!894
No description provided.