perf: split-LDS reads share one barrier, back-to-back exchanges share meetings (16 LDS reads 1.66x on a 4060 Laptop) #576

Open
KainanP wants to merge 3 commits from upstream-pr/576 into main
KainanP commented 2026-11-28 07:39:40 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-28T07:39:40Z.


Stacked on #570 (shader census). This PR also carries the exchange bench commit 82c877ba (emit/exchange_tests.rs, test-only), which uses #570's census::with. Merge #570 first; this diff then reduces to the bench plus the fix.

What

A console wave64 compute program that runs as two host waves of 32 (the "split" form, used on NVIDIA) placed a group barrier before every LDS access of the title, even a read right after a read. Every cross-lane exchange also ended with its own barrier. This PR does two things:

  1. Split-LDS reads share a barrier. A read right after a read shares the first read's barrier. Both reads must be in the same IR block, with no LDS write, title s_barrier or exchange between them. The rest is unchanged:
    • a write still meets first, so the other half's earlier reads are done;
    • the first read after a write still meets, so the write is visible.
  2. Back-to-back exchanges share meetings. Each console wave now has two pairs of exchange dwords. An exchange right after another one in the same DXIL block takes the other pair, so its own meeting is what keeps the first pair until every thread has read it. The last exchange of a run gets the closing meeting. It is appended to its block when the run ends: at the next exchange in another block, at the end of the IR block, or at the v_readlane exchange. N exchanges in a row now cost N + 1 barriers instead of 2N.

The split-LDS barriers now go through group_sync, so #570's census counts them in added_barrier as well as barrier.

Why

YUYING234's GPU-timed run on Marvisak/VIctory#3505 (4060 Laptop) shows the title's own compute dispatches take 92 % of GPU time. Blur, reduction and culling passes are LDS-heavy. The exchange bench found that 1 write + 16 reads in the split form cost 17 barriers and ran 1.65× slower than HLSL written for 32-lane waves.

Bench

RTX 4060 Laptop, exchange_bench (1024 groups × 256 rounds, 30 timed dispatches, median), three alternating before/after runs (spread ≤ 0.001 ms). Every run checks the console's sums.

construct threads barriers (added) before → after before after speed-up HLSL, 1 barrier
ballot (s_bcnt1 of a v_cmp) 64 3 (3) → 3 (3) 0.073 ms 0.073 ms 1.00× 0.072 ms
two ballots in a row 64 5 (5) → 4 (4) 0.140 ms 0.120 ms 1.17× 0.079 ms
v_readlane, uniform lane 64 3 (3) → 3 (3) 0.071 ms 0.071 ms 1.00× 0.053 ms
v_readfirstlane 64 3 (3) → 3 (3) 0.087 ms 0.087 ms 1.00× 0.053 ms
wave OR idiom (DPP + readlane) 64 3 (3) → 3 (3) 0.074 ms 0.074 ms 1.00× 0.072 ms
SCC of a mask (some lane) 64 3 (3) → 3 (3) 0.073 ms 0.073 ms 1.00× 0.074 ms
LDS: 1 write + 1 reads 64 2 (0) → 2 (2) 0.070 ms 0.070 ms 1.00× 0.070 ms
LDS: 1 write + 2 reads 64 3 (0) → 2 (2) 0.103 ms 0.084 ms 1.23× 0.084 ms
LDS: 1 write + 4 reads 64 5 (0) → 2 (2) 0.170 ms 0.119 ms 1.43× 0.119 ms
LDS: 1 write + 8 reads 64 9 (0) → 2 (2) 0.304 ms 0.194 ms 1.57× 0.194 ms
LDS: 1 write + 16 reads 64 17 (0) → 2 (2) 0.573 ms 0.346 ms 1.66× 0.347 ms
ballot (s_bcnt1 of a v_cmp) 256 3 (3) → 3 (3) 0.242 ms 0.242 ms 1.00× 0.265 ms
two ballots in a row 256 5 (5) → 4 (4) 0.415 ms 0.386 ms 1.08× 0.307 ms
v_readlane, uniform lane 256 3 (3) → 3 (3) 0.198 ms 0.198 ms 1.00× 0.170 ms
v_readfirstlane 256 3 (3) → 3 (3) 0.275 ms 0.275 ms 1.00× 0.169 ms
wave OR idiom (DPP + readlane) 256 3 (3) → 3 (3) 0.247 ms 0.247 ms 1.00× 0.251 ms
SCC of a mask (some lane) 256 3 (3) → 3 (3) 0.242 ms 0.242 ms 1.00× 0.286 ms

The second pairs sit after all of the group's first pairs, so a lone exchange keeps its old LDS layout. Interleaving them per console wave (4-dword stride) measured 0.242 → 0.249 ms on single exchanges at 256 threads.

Tests

  • split_lds_reads_share_one_barrier: census barrier/added_barrier counts for 1 write + 1–16 reads (always 2) and for 9 interleavings of writes, reads, title s_barrier and ballots. Fails before (3 barriers for 2 reads).
  • back_to_back_exchanges_share_meetings: ballot = 3 barriers, two ballots = 4 (was 5), in groups of 64 and 256, plus the pair each exchange of a run takes ([false, true, false, true], run continues across a title read). Fails before.
  • split_lds_interleavings_give_the_consoles_results (GPU): the 9 interleavings, including write→read(other half)→write→read, runs of reads across both halves, back-to-back writes, s_barrier between reads and three ballots in a row. They run in groups of one console wave, and of two where the program exchanges, 1024 groups × 256 rounds, against a lockstep model of the console wave. every_exchange_gives_the_consoles_results covers the bench constructs.
  • Mutation check: dropping the barrier before the first read after a write makes the GPU test fail on the 4060. Dropping a write's barrier, using the same pair twice or dropping the closing meeting do not show up on the 4060: the two warps never drift that far. The census and pair-choice tests catch those three instead. Intel UHD and WARP do not accept WaveSize(32) here, so the GPU tests run on NVIDIA only.
  • cargo test -p victory-shader --lib on the RTX 4060 Laptop: 371 passed, 21 ignored. cargo check -p victory and cargo xtask policy pass. Full pytest: 1454 passed, 529 skipped.

AI use: written, tested and benchmarked by Claude Code (Claude Opus 5.5) for KainanP, on synthetic programs only; no game shader was used.

🤖 Generated with Claude Code

<!-- greyfinch-sync:LooperSalty/AnyXboxX:issue:5537978865 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/576). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-28T07:39:40Z. --- > **Stacked on #570** (shader census). This PR also carries the exchange bench commit `82c877ba` (`emit/exchange_tests.rs`, test-only), which uses #570's `census::with`. Merge #570 first; this diff then reduces to the bench plus the fix. ## What A console wave64 compute program that runs as two host waves of 32 (the "split" form, used on NVIDIA) placed a group barrier before **every** LDS access of the title, even a read right after a read. Every cross-lane exchange also ended with its own barrier. This PR does two things: 1. **Split-LDS reads share a barrier.** A read right after a read shares the first read's barrier. Both reads must be in the same IR block, with no LDS write, title `s_barrier` or exchange between them. The rest is unchanged: - a write still meets first, so the other half's earlier reads are done; - the first read after a write still meets, so the write is visible. 2. **Back-to-back exchanges share meetings.** Each console wave now has two pairs of exchange dwords. An exchange right after another one in the same DXIL block takes the other pair, so its own meeting is what keeps the first pair until every thread has read it. The last exchange of a run gets the closing meeting. It is appended to its block when the run ends: at the next exchange in another block, at the end of the IR block, or at the `v_readlane` exchange. N exchanges in a row now cost N + 1 barriers instead of 2N. The split-LDS barriers now go through `group_sync`, so #570's census counts them in `added_barrier` as well as `barrier`. ## Why YUYING234's GPU-timed run on Marvisak/VIctory#3505 (4060 Laptop) shows the title's own compute dispatches take 92 % of GPU time. Blur, reduction and culling passes are LDS-heavy. The exchange bench found that 1 write + 16 reads in the split form cost 17 barriers and ran 1.65× slower than HLSL written for 32-lane waves. ## Bench RTX 4060 Laptop, `exchange_bench` (1024 groups × 256 rounds, 30 timed dispatches, median), three alternating before/after runs (spread ≤ 0.001 ms). Every run checks the console's sums. | construct | threads | barriers (added) before → after | before | after | speed-up | HLSL, 1 barrier | |---|---|---|---|---|---|---| | ballot (s_bcnt1 of a v_cmp) | 64 | 3 (3) → 3 (3) | 0.073 ms | 0.073 ms | 1.00× | 0.072 ms | | two ballots in a row | 64 | 5 (5) → 4 (4) | 0.140 ms | 0.120 ms | 1.17× | 0.079 ms | | v_readlane, uniform lane | 64 | 3 (3) → 3 (3) | 0.071 ms | 0.071 ms | 1.00× | 0.053 ms | | v_readfirstlane | 64 | 3 (3) → 3 (3) | 0.087 ms | 0.087 ms | 1.00× | 0.053 ms | | wave OR idiom (DPP + readlane) | 64 | 3 (3) → 3 (3) | 0.074 ms | 0.074 ms | 1.00× | 0.072 ms | | SCC of a mask (some lane) | 64 | 3 (3) → 3 (3) | 0.073 ms | 0.073 ms | 1.00× | 0.074 ms | | LDS: 1 write + 1 reads | 64 | 2 (0) → 2 (2) | 0.070 ms | 0.070 ms | 1.00× | 0.070 ms | | LDS: 1 write + 2 reads | 64 | 3 (0) → 2 (2) | 0.103 ms | 0.084 ms | 1.23× | 0.084 ms | | LDS: 1 write + 4 reads | 64 | 5 (0) → 2 (2) | 0.170 ms | 0.119 ms | 1.43× | 0.119 ms | | LDS: 1 write + 8 reads | 64 | 9 (0) → 2 (2) | 0.304 ms | 0.194 ms | 1.57× | 0.194 ms | | LDS: 1 write + 16 reads | 64 | 17 (0) → 2 (2) | 0.573 ms | 0.346 ms | 1.66× | 0.347 ms | | ballot (s_bcnt1 of a v_cmp) | 256 | 3 (3) → 3 (3) | 0.242 ms | 0.242 ms | 1.00× | 0.265 ms | | two ballots in a row | 256 | 5 (5) → 4 (4) | 0.415 ms | 0.386 ms | 1.08× | 0.307 ms | | v_readlane, uniform lane | 256 | 3 (3) → 3 (3) | 0.198 ms | 0.198 ms | 1.00× | 0.170 ms | | v_readfirstlane | 256 | 3 (3) → 3 (3) | 0.275 ms | 0.275 ms | 1.00× | 0.169 ms | | wave OR idiom (DPP + readlane) | 256 | 3 (3) → 3 (3) | 0.247 ms | 0.247 ms | 1.00× | 0.251 ms | | SCC of a mask (some lane) | 256 | 3 (3) → 3 (3) | 0.242 ms | 0.242 ms | 1.00× | 0.286 ms | The second pairs sit after all of the group's first pairs, so a lone exchange keeps its old LDS layout. Interleaving them per console wave (4-dword stride) measured 0.242 → 0.249 ms on single exchanges at 256 threads. ## Tests - `split_lds_reads_share_one_barrier`: census `barrier`/`added_barrier` counts for 1 write + 1–16 reads (always 2) and for 9 interleavings of writes, reads, title `s_barrier` and ballots. **Fails before** (3 barriers for 2 reads). - `back_to_back_exchanges_share_meetings`: ballot = 3 barriers, two ballots = 4 (was 5), in groups of 64 and 256, plus the pair each exchange of a run takes (`[false, true, false, true]`, run continues across a title read). **Fails before.** - `split_lds_interleavings_give_the_consoles_results` (GPU): the 9 interleavings, including write→read(other half)→write→read, runs of reads across both halves, back-to-back writes, `s_barrier` between reads and three ballots in a row. They run in groups of one console wave, and of two where the program exchanges, 1024 groups × 256 rounds, against a lockstep model of the console wave. `every_exchange_gives_the_consoles_results` covers the bench constructs. - Mutation check: dropping the barrier before the first read after a write makes the GPU test fail on the 4060. Dropping a write's barrier, using the same pair twice or dropping the closing meeting do **not** show up on the 4060: the two warps never drift that far. The census and pair-choice tests catch those three instead. Intel UHD and WARP do not accept `WaveSize(32)` here, so the GPU tests run on NVIDIA only. - `cargo test -p victory-shader --lib` on the RTX 4060 Laptop: 371 passed, 21 ignored. `cargo check -p victory` and `cargo xtask policy` pass. Full pytest: 1454 passed, 529 skipped. _AI use: written, tested and benchmarked by Claude Code (Claude Opus 5.5) for KainanP, on synthetic programs only; no game shader was used._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/576:upstream-pr/576'
git switch 'upstream-pr/576'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/576'
git switch 'upstream-pr/576'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/576'
git switch 'upstream-pr/576'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/576'
git switch 'main'
git merge --squash 'upstream-pr/576'
git switch 'main'
git merge --ff-only 'upstream-pr/576'
git switch 'main'
git merge 'upstream-pr/576'
git push origin 'main'
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
LooperSalty/AnyXboxX!576
No description provided.