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

Closed
Starchaser1024 wants to merge 0 commits from upstream-pr/894 into main
Starchaser1024 commented 2026-11-28 08:40:21 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Starchaser1024 on 2026-11-28T08:40:21Z.


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.
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5538391610 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/894). Originally posted by [Starchaser1024](https://github.com/Starchaser1024) on 2026-11-28T08:40:21Z. --- ## 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.
Aniseto commented 2026-11-28 09:21:24 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Aniseto on 2026-11-28T09:21:24Z.


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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7237185471 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/894#issuecomment-7237185471). Originally posted by [Aniseto](https://github.com/Aniseto) on 2026-11-28T09:21:24Z. --- 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.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
trustno1-coder/VIctory-NVIDIA!894
No description provided.