perf(shader): use hardware group_sync for uniform pair meetings (#282) #310

Manually merged
github merged 2 commits from github-bot/gh-310 into gh-pr/310/base 2026-12-02 13:35:20 +00:00 AGit
Owner

@⁠RUIZ-Enrique-25002136 on GitHub · 2026-12-02 08:47 UTC · original
RUIZ-Enrique-25002136:perf/shader-uniform-pair-meet → main · +225 −35 · 2 files · 2 commits · merged 2026-12-02 13:34 UTC

Summary

Implements the Lower::meet LDS spin-barrier optimization from Audit #282 (Pass 4, Item 4): when lowering wave64 compute shaders for 32-lane hosts with with_pair_meetings (pairs mode), threadgroup-uniform IR blocks now emit a single hardware GroupMemoryBarrierWithGroupSync (group_sync, barrier(9)) instead of the 4,096-iteration LDS atomic counter spin-poll loop (barrier(8)), while divergent/non-uniform blocks continue to use the per-console-wave LDS spin-poll.

Refs #282

AI-Ready Context & Change Map

For downstream coding/review agents, here is the exact scope, invariant set, and symbol map of this PR:

  1. crates/victory-shader/src/emit/lower.rs — Uniform vs. Divergent Pair Meetings

    • Lower::group_uniform_blocks: Option<Vec<bool>> (lower.rs:355):
      • Computed in Lower::new (lower.rs:2009-2042) alongside fence_only so the reverse_postorder / cut_short / all_lane_blocks_of(x.st, pairs.is_some()) analysis runs whenever pairs.is_some() || insts().any(|i| i.name() == "s_barrier").
      • group_uniform_blocks is Some(all) only when pairs.is_some() && !cut_short, where all is all_lane_blocks_of(x.st, true) (marks an IR block true iff it is not inside any per-lane branch region, any wave-wide branch region Structured::wide, or any loop-closing branch region). When cut_short is true (a plain-jump loop where lanes can abort early via abort_block), group_uniform_blocks is None so every block safely stays on the per-console-wave LDS spin-poll.
    • Lower::group_uniform_meet(&self) -> bool (lower.rs:4917-4923):
      • Returns true iff self.pairs.is_some() and self.group_uniform_blocks marks self.cur_ir as true.
    • Lower::meet(&mut self) (lower.rs:4934-4938):
      • Changed guard from if self.pairs.is_none() to if self.pairs.is_none() || self.group_uniform_meet(), dispatching uniform blocks directly to self.group_sync(c).
    • Double-Buffered Exchange Race-Freedom (Lower::exchange_pairs at lower.rs:5028-5047 & v_readlane_b32 at lower.rs:9975-9981):
      • Critical invariant: In pairs mode, exchange_dword() selects exchange buffer 0 or 1 via (counter >> 1) & 1, where counter is only incremented by the divergent LDS spin-poll inside Lower::meet(). A uniform group_sync does not increment counter.
      • Consequently, two consecutive uniform exchanges (or a uniform exchange followed by a divergent exchange) observe the same counter value and select the same exchange buffer slot.
      • To keep buffer reuse race-free without touching counter, both exchange_pairs() and v_readlane_b32 capture let uniform_pair = self.group_uniform_meet(); before self.meet(), and emit a second self.meet() (group_sync) after loading from the exchange buffer when uniform_pair is true (matching the self.pairs.is_none() double-barrier protocol).
  2. crates/victory-shader/src/emit/lower_tests.rs — Cross-Platform Unit Test Coverage

    • Added pair_meetings_use_group_sync_in_uniform_blocks_and_spin_in_divergent_loops (lower_tests.rs:1054-1206) using super::TEST_HOST_WAVE (32), super::with_pair_meetings, and bitcode::census::barrier_modes:
      • Mixed shader (128 threads, widened divergent loop with ds_write_b32/ds_read_b32 + reconverged uniform post-loop block with v_readlane_b32): verifies wave_size == Some(32), presence of barrier(8) from the divergent loop's LDS spin-poll, and >= 3 barrier(9) hardware group_sync barriers (entry + post-loop v_readlane_b32).
      • Purely uniform shader (128 threads, ds_write_b32/ds_read_b32 + v_readlane_b32, no branches): verifies wave_size == Some(32), 0 barrier(8) spin-polls emitted, and >= 3 barrier(9) hardware group_sync barriers.

Rig Specs Tested On

  • CPU: Linux workstation (offline unit tests, DXIL barrier census, and clippy only)
  • GPU & VRAM: N/A (not executed on live Windows D3D12 hardware)
  • System RAM: N/A
  • OS & GPU Driver: Linux (x86_64) — Windows x86_64-pc-windows-msvc full workspace clippy, wave64_tests GPU execution, and live title frame-time validation deferred to GitHub Actions (windows-latest) / maintainer run

Run Settings & Profile

  • Build & Commit SHA: victory-shader changes on top of origin/main (c7d3102)
  • Console Profile & Internal Resolution / Scale: N/A (unit tests)
  • Ray Tracing: N/A
  • Upscaling: N/A
  • Frame Generation: N/A
  • Launcher Command / Preset & Extra VICTORY_* Overrides: N/A
  • Scene / Area & Session Length: N/A

Measurements & Verification

  • Base FPS (without Frame Gen): N/A (offline compiler unit tests)
  • Frame-Gen FPS (if enabled): N/A
  • VRAM / RAM Usage: N/A
  • Crashes / DRED Resets: N/A
  • Automated Checks:
    • cargo clippy --workspace --all-targets --locked -- -D warnings (requires x86_64-pc-windows-msvc; verified on GitHub Actions windows-latest CI)
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings (requires x86_64-pc-windows-msvc; verified on GitHub Actions windows-latest CI)
    • cargo clippy --locked -p victory-shader --lib -- -D warnings -> 0 warnings, exit 0
    • cargo test --locked -p victory-shader pair_meetings_use_group_sync -- --nocapture -> 1 passed, 0 failed
    • cargo test --locked -p victory-shader -- --test-threads=1 -> 253 passed, 0 failed, 2 ignored
    • cargo test --locked -p xtask -> 8 passed, 0 failed
    • cargo xtask policy -> asset policy: 321 files ok
**@⁠RUIZ-Enrique-25002136** on GitHub · 2026-12-02 08:47 UTC · [original](https://github.com/Androtim/MiniVI/pull/310) `RUIZ-Enrique-25002136:perf/shader-uniform-pair-meet` → `main` · +225 −35 · 2 files · 2 commits · merged 2026-12-02 13:34 UTC ## Summary Implements the `Lower::meet` LDS spin-barrier optimization from Audit #282 (Pass 4, Item 4): when lowering wave64 compute shaders for 32-lane hosts with `with_pair_meetings` (`pairs` mode), threadgroup-uniform IR blocks now emit a single hardware `GroupMemoryBarrierWithGroupSync` (`group_sync`, `barrier(9)`) instead of the 4,096-iteration LDS atomic counter spin-poll loop (`barrier(8)`), while divergent/non-uniform blocks continue to use the per-console-wave LDS spin-poll. Refs #282 ### AI-Ready Context & Change Map For downstream coding/review agents, here is the exact scope, invariant set, and symbol map of this PR: 1. **`crates/victory-shader/src/emit/lower.rs` — Uniform vs. Divergent Pair Meetings** - **`Lower::group_uniform_blocks: Option<Vec<bool>>` (`lower.rs:355`):** - Computed in `Lower::new` (`lower.rs:2009-2042`) alongside `fence_only` so the `reverse_postorder` / `cut_short` / `all_lane_blocks_of(x.st, pairs.is_some())` analysis runs whenever `pairs.is_some() || insts().any(|i| i.name() == "s_barrier")`. - `group_uniform_blocks` is `Some(all)` only when `pairs.is_some() && !cut_short`, where `all` is `all_lane_blocks_of(x.st, true)` (marks an IR block `true` iff it is not inside any per-lane branch region, any wave-wide branch region `Structured::wide`, or any loop-closing branch region). When `cut_short` is true (a plain-jump loop where lanes can abort early via `abort_block`), `group_uniform_blocks` is `None` so every block safely stays on the per-console-wave LDS spin-poll. - **`Lower::group_uniform_meet(&self) -> bool` (`lower.rs:4917-4923`):** - Returns `true` iff `self.pairs.is_some()` and `self.group_uniform_blocks` marks `self.cur_ir` as `true`. - **`Lower::meet(&mut self)` (`lower.rs:4934-4938`):** - Changed guard from `if self.pairs.is_none()` to `if self.pairs.is_none() || self.group_uniform_meet()`, dispatching uniform blocks directly to `self.group_sync(c)`. - **Double-Buffered Exchange Race-Freedom (`Lower::exchange_pairs` at `lower.rs:5028-5047` & `v_readlane_b32` at `lower.rs:9975-9981`):** - **Critical invariant:** In `pairs` mode, `exchange_dword()` selects exchange buffer `0` or `1` via `(counter >> 1) & 1`, where `counter` is only incremented by the divergent LDS spin-poll inside `Lower::meet()`. A uniform `group_sync` does **not** increment `counter`. - Consequently, two consecutive uniform exchanges (or a uniform exchange followed by a divergent exchange) observe the same `counter` value and select the same exchange buffer slot. - To keep buffer reuse race-free without touching `counter`, both `exchange_pairs()` and `v_readlane_b32` capture `let uniform_pair = self.group_uniform_meet();` before `self.meet()`, and emit a second `self.meet()` (`group_sync`) after loading from the exchange buffer when `uniform_pair` is true (matching the `self.pairs.is_none()` double-barrier protocol). 2. **`crates/victory-shader/src/emit/lower_tests.rs` — Cross-Platform Unit Test Coverage** - Added `pair_meetings_use_group_sync_in_uniform_blocks_and_spin_in_divergent_loops` (`lower_tests.rs:1054-1206`) using `super::TEST_HOST_WAVE` (32), `super::with_pair_meetings`, and `bitcode::census::barrier_modes`: - **Mixed shader** (128 threads, widened divergent loop with `ds_write_b32`/`ds_read_b32` + reconverged uniform post-loop block with `v_readlane_b32`): verifies `wave_size == Some(32)`, presence of `barrier(8)` from the divergent loop's LDS spin-poll, and `>= 3` `barrier(9)` hardware `group_sync` barriers (entry + post-loop `v_readlane_b32`). - **Purely uniform shader** (128 threads, `ds_write_b32`/`ds_read_b32` + `v_readlane_b32`, no branches): verifies `wave_size == Some(32)`, `0` `barrier(8)` spin-polls emitted, and `>= 3` `barrier(9)` hardware `group_sync` barriers. ## Rig Specs Tested On - **CPU:** Linux workstation (offline unit tests, DXIL barrier census, and clippy only) - **GPU & VRAM:** N/A (not executed on live Windows D3D12 hardware) - **System RAM:** N/A - **OS & GPU Driver:** Linux (x86_64) — Windows `x86_64-pc-windows-msvc` full workspace clippy, `wave64_tests` GPU execution, and live title frame-time validation deferred to GitHub Actions (`windows-latest`) / maintainer run ## Run Settings & Profile - **Build & Commit SHA:** `victory-shader` changes on top of `origin/main` (`c7d3102`) - **Console Profile & Internal Resolution / Scale:** N/A (unit tests) - **Ray Tracing:** N/A - **Upscaling:** N/A - **Frame Generation:** N/A - **Launcher Command / Preset & Extra `VICTORY_*` Overrides:** N/A - **Scene / Area & Session Length:** N/A ## Measurements & Verification - **Base FPS (without Frame Gen):** N/A (offline compiler unit tests) - **Frame-Gen FPS (if enabled):** N/A - **VRAM / RAM Usage:** N/A - **Crashes / DRED Resets:** N/A - **Automated Checks:** - [ ] `cargo clippy --workspace --all-targets --locked -- -D warnings` (requires `x86_64-pc-windows-msvc`; verified on GitHub Actions `windows-latest` CI) - [ ] `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` (requires `x86_64-pc-windows-msvc`; verified on GitHub Actions `windows-latest` CI) - [x] `cargo clippy --locked -p victory-shader --lib -- -D warnings` -> 0 warnings, exit 0 - [x] `cargo test --locked -p victory-shader pair_meetings_use_group_sync -- --nocapture` -> 1 passed, 0 failed - [x] `cargo test --locked -p victory-shader -- --test-threads=1` -> 253 passed, 0 failed, 2 ignored - [x] `cargo test --locked -p xtask` -> 8 passed, 0 failed - [x] `cargo xtask policy` -> `asset policy: 321 files ok`
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 08:49:29 +00:00
Author
Owner

@⁠Androtim on GitHub · 2026-12-02 09:05 UTC · original · edited 2026-12-02 13:07 UTC

Automated triage for the maintainers (not an approval).

Verdict: needs-gpu-test · CI (check): passing

Review Summary

The change in crates/victory-shader/src/emit/lower.rs cleanly optimizes Lower::meet under pairs (with_pair_meetings) mode by emitting a hardware group_sync (barrier(9)) instead of the per-console-wave LDS spin-poll (barrier(8)) when self.cur_ir is threadgroup-uniform.

Verification

  • Uniformity analysis (Lower::new, Lower::group_uniform_meet): Reuses the same cut_short (plain-jump back-edge) and all_lane_blocks_of(x.st, pairs.is_some()) analysis already used on main to classify fence_only for s_barrier. Blocks inside per-lane branches, wave-wide branches, divergent loops, or shaders with cut-short loops stay on the LDS spin-poll.
  • Double-buffered exchange race-freedom (Lower::exchange_pairs, v_readlane_b32): Because a uniform group_sync does not advance the LDS meeting counter that exchange_dword() uses to alternate exchange buffers, capturing let uniform_pair = self.group_uniform_meet() and emitting a second self.meet() after loading from LDS prevents subsequent uniform or divergent exchanges from overwriting the slot before all lanes finish reading. v_readfirstlane_b32 routes through exchange_halves / exchange_pairs, so all pair-exchange callers are covered.
  • Tests (crates/victory-shader/src/emit/lower_tests.rs): pair_meetings_use_group_sync_in_uniform_blocks_and_spin_in_divergent_loops verifies both a mixed divergent-loop + uniform post-loop shader and a purely uniform shader via bitcode::census::barrier_modes, and cleanly restores TEST_HOST_WAVE.
  • CI: Passing.

Why needs-gpu-test

This modifies DXIL barrier emission for wave64-on-wave32 compute shaders (Lower::meet, exchange_pairs, v_readlane_b32) and has only been validated with offline compiler/census tests so far. A local real-GPU smoke test on a 32-lane host (with pair meetings active) is warranted to confirm no TDRs/deadlocks or LDS exchange corruption before merging.

(Automated technical triage review; not a formal maintainer approval.)


Generated with Antigravity CLI (agy).

**@⁠Androtim** on GitHub · 2026-12-02 09:05 UTC · [original](https://github.com/Androtim/MiniVI/pull/310#issuecomment-7340370589) · edited 2026-12-02 13:07 UTC <!-- minivi-triage v1 head=4005603a23caae6de8547134282c8a72b416742c verdict=needs-gpu-test ci=passing at=2026-12-02T13:07:32Z --> Automated triage for the maintainers (not an approval). **Verdict:** `needs-gpu-test` · **CI (`check`):** `passing` ### Review Summary The change in [`crates/victory-shader/src/emit/lower.rs`](crates/victory-shader/src/emit/lower.rs) cleanly optimizes `Lower::meet` under `pairs` (`with_pair_meetings`) mode by emitting a hardware `group_sync` (`barrier(9)`) instead of the per-console-wave LDS spin-poll (`barrier(8)`) when `self.cur_ir` is threadgroup-uniform. ### Verification - **Uniformity analysis (`Lower::new`, `Lower::group_uniform_meet`)**: Reuses the same `cut_short` (plain-jump back-edge) and `all_lane_blocks_of(x.st, pairs.is_some())` analysis already used on `main` to classify `fence_only` for `s_barrier`. Blocks inside per-lane branches, wave-wide branches, divergent loops, or shaders with cut-short loops stay on the LDS spin-poll. - **Double-buffered exchange race-freedom (`Lower::exchange_pairs`, `v_readlane_b32`)**: Because a uniform `group_sync` does not advance the LDS meeting counter that `exchange_dword()` uses to alternate exchange buffers, capturing `let uniform_pair = self.group_uniform_meet()` and emitting a second `self.meet()` after loading from LDS prevents subsequent uniform or divergent exchanges from overwriting the slot before all lanes finish reading. `v_readfirstlane_b32` routes through `exchange_halves` / `exchange_pairs`, so all pair-exchange callers are covered. - **Tests (`crates/victory-shader/src/emit/lower_tests.rs`)**: `pair_meetings_use_group_sync_in_uniform_blocks_and_spin_in_divergent_loops` verifies both a mixed divergent-loop + uniform post-loop shader and a purely uniform shader via `bitcode::census::barrier_modes`, and cleanly restores `TEST_HOST_WAVE`. - **CI**: Passing. ### Why `needs-gpu-test` This modifies DXIL barrier emission for wave64-on-wave32 compute shaders (`Lower::meet`, `exchange_pairs`, `v_readlane_b32`) and has only been validated with offline compiler/census tests so far. A local real-GPU smoke test on a 32-lane host (with pair meetings active) is warranted to confirm no TDRs/deadlocks or LDS exchange corruption before merging. *(Automated technical triage review; not a formal maintainer approval.)* --- _Generated with Antigravity CLI (`agy`)._
Author
Owner

@⁠Androtim on GitHub · 2026-12-02 11:48 UTC · original

CI run 46844652097 failed on a single clippy::manual-contains lint in the new unit test:

  • crates/victory-shader/src/emit/lower_tests.rs:1163:9: replace mixed_modes.iter().any(|&m| m == 8) with mixed_modes.contains(&8).

Everything else compiled cleanly (victory-shader + workspace). Once fixed + green CI + real-GPU A/B & diagnostics visual PNG check, this is in the immediate priority merge wave.

**@⁠Androtim** on GitHub · 2026-12-02 11:48 UTC · [original](https://github.com/Androtim/MiniVI/pull/310#issuecomment-7344429686) CI run `46844652097` failed on a single `clippy::manual-contains` lint in the new unit test: - `crates/victory-shader/src/emit/lower_tests.rs:1163:9`: replace `mixed_modes.iter().any(|&m| m == 8)` with `mixed_modes.contains(&8)`. Everything else compiled cleanly (`victory-shader` + workspace). Once fixed + green CI + real-GPU A/B & diagnostics visual PNG check, this is in the immediate priority merge wave.
Author
Owner

@⁠Androtim on GitHub · 2026-12-02 12:51 UTC · original

Fixed the clippy::manual_contains lint in crates/victory-shader/src/emit/lower_tests.rs (commit 4005603 pushed to perf/shader-uniform-pair-meet) and ran real-GPU verification merged cleanly onto main (3230974):

  • Static & Unit / Real-GPU Tests:
    • cargo +1.98.1-x86_64-pc-windows-gnu xtask policy PASS
    • Dual workspace cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --locked -- -D warnings (default & --features diagnostics) PASS
    • victory-shader lower::tests (22 passed, 0 failed, 1 ignored) and real-GPU wave64_tests (19 passed, 0 failed) PASS (352 passed overall; the 3 Pipeline State Validation failures in rect::tests x2 and barycentric_tests::first_half_values_render_on_hardware_and_warp fail identically on unmodified main 3230974 on this host's dxil.dll)
  • Warm-Cache Paired Same-Path Plain---release A/B vs main (3230974, after 20s 1440p warmup, Series X + DLSS quality, RTX 4060 Ti 8 GB, 0 crashes/DRED):
    • 1440p (90s): Median 14.9 -> 14.9 fps (mean 13.6 -> 13.6 fps; 1440p CPU-bound), preparing 23.9 -> 25.1 ms, copyback 10.3 -> 10.5 ms, crashes=0
    • 4K DLSS quality (90s): Median 6.2 -> 6.3 fps (mean 6.24 -> 6.35 fps), preparing 20.3 -> 19.65 ms (preparing_core_ms med 34.00 -> 32.50 ms), thin_ms avg 601.2 -> 573.3 ms, used_max 6702 -> 6638 MB (12 MB over -> 0 MB over), working_set_mb avg 18,013 -> 17,079 MB, crashes=0
  • 1440p 90s --features diagnostics Visual Gate (diag-pr310-1440p-90s): PASS (6/6 gates) — world_windows=20, max_draws=14621, crashes=0, used_max=6232 MB / budget_min=6681 MB, final_waiting=2811, mapped_rate=1.36 ms/s, pngs=24 lit=20/24 luma=100.02 near_black=0.06% edge_energy=7.88 magenta=0.0%. Inspected frame-1828.png, frame-1834.png, frame-1836.png — clean geometry, lighting, foliage, water, and UI.
**@⁠Androtim** on GitHub · 2026-12-02 12:51 UTC · [original](https://github.com/Androtim/MiniVI/pull/310#issuecomment-7345985198) Fixed the `clippy::manual_contains` lint in `crates/victory-shader/src/emit/lower_tests.rs` (commit `4005603` pushed to `perf/shader-uniform-pair-meet`) and ran real-GPU verification merged cleanly onto `main` (`3230974`): - **Static & Unit / Real-GPU Tests:** - `cargo +1.98.1-x86_64-pc-windows-gnu xtask policy` PASS - Dual workspace `cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --locked -- -D warnings` (default & `--features diagnostics`) PASS - `victory-shader` `lower::tests` (`22 passed, 0 failed, 1 ignored`) and real-GPU `wave64_tests` (`19 passed, 0 failed`) PASS (`352 passed` overall; the 3 `Pipeline State Validation` failures in `rect::tests` x2 and `barycentric_tests::first_half_values_render_on_hardware_and_warp` fail identically on unmodified `main` `3230974` on this host's `dxil.dll`) - **Warm-Cache Paired Same-Path Plain-`--release` A/B vs `main` (`3230974`, after 20s 1440p warmup, Series X + DLSS quality, RTX 4060 Ti 8 GB, 0 crashes/DRED):** - **1440p (90s):** Median `14.9 -> 14.9 fps` (mean `13.6 -> 13.6 fps`; 1440p CPU-bound), `preparing` `23.9 -> 25.1 ms`, `copyback` `10.3 -> 10.5 ms`, `crashes=0` - **4K DLSS quality (90s):** Median `6.2 -> 6.3 fps` (mean `6.24 -> 6.35 fps`), `preparing` `20.3 -> 19.65 ms` (`preparing_core_ms med` `34.00 -> 32.50 ms`), `thin_ms avg` `601.2 -> 573.3 ms`, `used_max` `6702 -> 6638 MB` (`12 MB over -> 0 MB over`), `working_set_mb avg` `18,013 -> 17,079 MB`, `crashes=0` - **1440p 90s `--features diagnostics` Visual Gate (`diag-pr310-1440p-90s`):** **PASS (6/6 gates)** — `world_windows=20, max_draws=14621, crashes=0, used_max=6232 MB / budget_min=6681 MB, final_waiting=2811, mapped_rate=1.36 ms/s, pngs=24 lit=20/24 luma=100.02 near_black=0.06% edge_energy=7.88 magenta=0.0%`. Inspected `frame-1828.png`, `frame-1834.png`, `frame-1836.png` — clean geometry, lighting, foliage, water, and UI.
github-bot changed target branch from main to gh-pr/310/base 2026-12-02 13:35:18 +00:00
github manually merged commit 4005603a23 into gh-pr/310/base 2026-12-02 13:35:20 +00:00
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/Androtim--MiniVI!310
No description provided.