Skip to content

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

Merged
Androtim merged 2 commits into
Androtim:mainfrom
RUIZ-Enrique-25002136:perf/shader-uniform-pair-meet
Dec 2, 2026
Merged

Androtim merged 2 commits into
Androtim:mainfrom
RUIZ-Enrique-25002136:perf/shader-uniform-pair-meet

Conversation

@RUIZ-Enrique-25002136

Copy link
Copy Markdown
Contributor

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

@Androtim

Androtim commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

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

Androtim commented Dec 2, 2026

Copy link
Copy Markdown
Owner

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 Androtim added triage: needs work Triage found bugs, missing tests, or requested changes and removed triage: needs gpu test Security & logic checks passed; requires local real-GPU smoke test labels Dec 2, 2026
@Androtim

Androtim commented Dec 2, 2026

Copy link
Copy Markdown
Owner

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 Androtim added triage: needs gpu test Security & logic checks passed; requires local real-GPU smoke test and removed triage: needs work Triage found bugs, missing tests, or requested changes labels Dec 2, 2026
@Androtim
Androtim merged commit 90ff96b into Androtim:main Dec 2, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: shader Shader translator, wave lowering, or DXIL emitter triage: needs gpu test Security & logic checks passed; requires local real-GPU smoke test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants