Repository navigation
D3D12-SHADER-DOUBLE-BUFFERED-EXCHANGE: double-buffer wave64 exchange banks to halve loop barriers - #2688
Conversation
|
Independent RTX 4090 exchange verification. Native Windows 11, NVIDIA 616.64, MSVC, Rust 1.98.1, and SDK 26100 DXIL validation.
Synthetic input only. AI-assisted with OpenAI Codex (GPT-6.2 Sol). |
…oop barriers (D3D12-SHADER-DOUBLE-BUFFERED-EXCHANGE)
Addresses the d3d12/docs/findings.md item ("exchanges inside loops (two
barriers each: about 110 of the 134 barriers of the 8 x 8 material
program 1f1fbd86759b3da2 are in loops)" / "What would make it cheap:
one barrier per exchange (two dwords used by turns)").
When exchange_sites() > 0 and LDS has room for a second shared bank
(2 * waves extra dwords), allocate two alternating shared exchange banks
and thread a per-thread 1-bit bank parity SSA value (i32 in {0, 1})
across IR blocks. Every shared-bank exchange (including inside loops)
alternates banks with one group_sync after its stores and flips
bank ^= 1, eliminating the second post-load barrier while keeping bank
reads and next-iteration stores strictly separated by the preceding
barrier.
Falls back cleanly to the 2-barrier single-bank path when
LEONIDA_EXCHANGE_SITES=0 or when LDS is too full for the second bank.
AI-assisted: Antigravity (Gemini 4 Neon)
52e1a0c to
2ef9796
Compare
|
Independent check on Windows (not the author), at
Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, Rust 1.98.1 pinned. The behavioural claims (fewer loop barriers / same exchange results) are covered by the crate's own paired/wave64 tests; I did not measure the performance change on a GPU. AI-assisted: yes (opencode). |
Test-only merge overlap with #2685MEASURED. This head ( This checks the merge hunks. The combined shader behavior needs a separate test run. AI-assisted: Codex (GPT-6.2 Sol). |
|
Native Windows follow-up to the combined Wine test. I merged MSVC Rust 1.98.1 release, Windows 11, NVIDIA 616.64. I made the two fixtures require Windows SDK 26100 DXIL validation, native hardware PSO creation and dispatch. DXGI confirmed the RTX 4090 for every case. Results held in three fresh runs per fixture:
All 11 stock |
Co-authored-by: Cursor <cursoragent@cursor.com>
|
Merged main (#2685 landed in wave64_tests.rs); kept both tests, no production change. cargo test -p leonida-shader --release emit:: after the merge: 269 passed, 0 failed, 5 ignored. AI-assisted: Claude Sonnet 5.5 |
|
Independent game-run A/B test with a freshly rebuilt shader cache on Windows 10 + NVIDIA RTX 5070 12 GB (driver 621.40) + Ryzen 9 5950X ( Verdict:
AI-assisted: Antigravity. |
There was a problem hiding this comment.
Nice barrier reduction for cyclic/shared wave64 exchanges, and the end-of-lower() bank_in phi wiring handles late ballot_of/wave_any emissions on self.tail[b] cleanly.
One correctness edge case to guard before merging:
d3d12/crates/leonida-shader/src/emit/lower.rs(exchange_many, lines 6018-6060 in the diff): whenself.double_bank.is_some()andirresolves toNone, execution falls back to the single-bank path targeting bank0(store(0); group_sync; load(0); group_sync). That fallback can be reached ifat == self.s.entry(e.g.,ballot_oforwave_anyfor entryEXECwithdef_block == None) after IR block 0 already emitted a double-bank exchange on bank0(which intentionally omits the trailinggroup_sync) and then splitself.s.entry(self.tail[0] != Some(self.s.entry)), e.g., withLEONIDA_EXCHANGE_SITES=1or >24 exchanges before a guarded memory op in block 0. Because the fallback writes bank0immediately after the preceding bank-0load with no intervening barrier, the two halves can race on bank0.
Adding if self.double_bank.is_some() { self.group_sync(at); } before the single-bank fallback loop (or ensuring every fallback under double_bank fences first) closes this race cleanly. Please also update the branch so the new d3d12 (windows-latest) CI workflow runs.
(AI-assisted review.)
|
Independent offscreen A/B check of This covers the single exchange in the checked-in loop fixture. The multi-site case raised in the other review and in-game frame time remain open. AI-assisted with OpenAI Codex. |
|
Follow-up with two distinct exchanges per loop iteration on AI-assisted with OpenAI Codex. |
|
@aslan2142 Addressed in
AI-assisted: Gemini 4 Neon (including an independent read-only reviewer pass). |
Claimed in #2570 as
D3D12-SHADER-DOUBLE-BUFFERED-EXCHANGE(#issuecomment-7267219686).What and why
Addresses the open shader-performance item in
d3d12/docs/findings.md:Previously,
exchange_many()ind3d12/crates/leonida-shader/src/emit/lower.rsemitted twogroup_syncbarriers for every exchange inside a cyclic IR block (one after storing into the shared pair, and a second after loading so the next exchange would not overwrite a slow reader).This change allocates a second shared exchange bank (
2 * wavesextra LDS dwords whenexchange_sites() > 0and LDS has room) and threads a per-thread 1-bitbankparity SSA value (i32in{0, 1}) across reachable IR blocks:kstores into bankk & 1, executes onegroup_sync, loads both halves from bankk & 1, and flipsbank ^= 1.k - 1after finishing their reads of bank(k - 2) & 1 == k & 1, storing to bankk & 1is race-free without a post-load barrier.bankphis in shaders with no shared-bank exchanges are eliminated byopt.rs(FOLD | DEAD).LEONIDA_EXCHANGE_SITES=0or LDS has no room for the second bank.Verification (Red -> Green)
Hardware / OS: native Windows 11 Pro (
10.0.26200) + AMD Radeon RX 7900 XTX (gfx1100, driver32.0.32029.2010), pinned toolchainrustc 1.98.1.exchanges_in_a_loop_meet_once_with_double_buffered_banksemitted3barriers both withLEONIDA_EXCHANGE_SITES=24and0(3 vs 3).main40e7fe69d):3barriers to2(1 barrier per loop exchange instead of 2), DXIL validates viadxil.dll, and live GPU dispatch on RX 7900 XTX matches the exact 8-iteration wave OR sum bit-for-bit.cargo test -p leonida-shader --lib wave64_tests -- --test-threads=4—31 passed; 0 failed(3.87s).cargo test -p leonida-shader --lib paired_tests -- --test-threads=4—16 passed; 0 failed(7.30s).python -I scripts/check_no_game_data.pyon both touched files — PASS.AI-assisted: Antigravity (Gemini 4 Neon)