[PR #2119] shaders: coalesce and inline wave64 x_round exchanges (SHADER-W64-XROUND-BARRIERS) #2125

Open
opened 2026-11-26 21:20:07 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2119
Author: @robot00f
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: perf/w64-xround-barriers


📝 Commits (1)

  • 72d722a shaders: coalesce and inline wave64 x_round exchanges (SHADER-W64-XROUND-BARRIERS)

📊 Changes

7 files changed (+1026 additions, -178 deletions)

View changed files

📝 docs/design.md (+1 -1)
📝 runtime/test/shaders/check.py (+3 -1)
➕ runtime/test/shaders/cs_w64x_xround_coalesce.asm (+117 -0)
📝 scripts/run_gpu_tests.py (+2 -1)
📝 tests/semantic/test_wave64_emulate_opt.py (+5 -6)
➕ tests/semantic/test_wave64_xround_barriers.py (+462 -0)
📝 tools/rdna2spirv.py (+436 -169)

📄 Description

Claimed in the coordination issue as: SHADER-W64-XROUND-BARRIERS (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7172190888; follow-up to https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7159151871 and https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7159367489)

What and why

Coalesces wave-wide Workgroup exchanges and eliminates helper-call / modulo overhead in Gen.x_round (emulate_inline in tools/rdna2spirv.py) under --wave64-emulate on 32-wide GPUs, while keeping %x_buf byte-identical ((2*L + 6*W + 7) dwords):

  1. Division-free inline Gen._x_round_inline:
    • Drops the outlined %x_pre_*_fn OpFunctionCalls (CGSCC inliner cloning across hundreds of callsites increased NVVM compile time by +12% to +21% vs inline).
    • Cycles %x_it through 0..5 (lcm(2, 3) = 6, preserving both it & 1 parity and it % 3 ring-slot indexing) with zero OpUMods and omits the two dead scheduler-word reset OpAtomicStores on multi-wave workgroups whenever mini_blocks and tail_blocks are empty.
    • Narrows barrier memory semantics from 0x948 (ImageMemory | WorkgroupMemory | UniformMemory | AcquireRelease) to 0x108 (WorkgroupMemory | AcquireRelease) whenever "mem" is clean in x_dirty (and in x_order_barrier when only "lds" is dirty).
  2. Block-local Any-vote and first-lane caching (x_vcache, x_flcache, x_vkeys):
    • Shares Any votes and first-active-lane indices across scc_from_mask, branch_condition, wave_mask, v_readfirstlane_b32, and DPP fi:0 within a basic block, and preserves x_gen across internal OpSelectionMerge regions (buffer_load, buffer_store, ds_*, _emit_rfl_run) that do not write %active, %vcc, %vcc_hi, or %sgpr.
    • Derives Any from a cached (lo, hi) mask in 2 ALU ops ((lo | hi) != 0, 0 barriers, 0 atomics); when branch_condition reuses a cached Any vote while x_dirty is non-empty, emits a bare x_order_barrier() to preserve wave64_emulate_opt.py's sb_clears_dirty invariant.
    • Restricts x_vkeys registration in mask_dst to full-wave masks (n == 2 SGPR pairs and full vcc in wave64) so 32-bit half-mask writes never alias 64-bit x_vcache entries.
  3. Deferred SCC vote coalescing (_pending_scc_vote in emulate_inline):
    • Defers uncached scc_from_mask / scc_any_half votes until %scc is read/overwritten or a label/merge/barrier/branch/%pc store occurs.
    • Coalesces a pending SCC vote with a subsequent branch_condition on the same mask (1 OpAtomicOr, 1 barrier), with another 1-bit g_vote on a distinct mask (2 -> 1 barrier via 2-word bits=(p_bit, bit2)), with a data-only x_round (v_readlane_b32, ds_bpermute_b32, ds_swizzle_b32, quad_any, DPP fi:1, 2 -> 1 barrier via data + bits), or with x_order_barrier (2 -> 1 barrier), and drops the vote (0 barriers) if %scc is overwritten before being read.
  4. Batched contiguous v_readfirstlane_b32 runs (Gen._emit_rfl_run):
    • Establishes first once (0 barriers if already in x_flcache/x_mcache, else 1 round on item 0) and transfers up to 64 remaining values per wave from the single running lane wave_lane == first across one OpControlBarrier (4 -> 2 or 1 round barriers for V# scalarization, 8 -> 2 or 1 for T# scalarization, piggybacking any pending SCC vote into the same barrier).

NVVM ablation note (#1608): Reproducing the 192-VGPR unfolded binary if/else ScreenProbes shape on GTX 1050 Ti (sp_tree_156: 464.1 KiB, 156 barriers -> 4.168 s; sp_tree_324: 898.7 KiB, 324 barriers -> 12.224 s) and stripping all middle OpControlBarrier instructions (NO_BARS) leaves vkCreateComputePipelines time unchanged (12.13 s -> 12.22 s, and 4.91 s -> 4.88 s on 320-barrier loops). Thus the ~11 s baseline on pure unfolded saveexec trees is dominated by %pc-guarded OpSelectionMerge block splitting (SROA/Mem2Reg Phi webs across 192 VGPRs) + %x_pre_*_fn cloning rather than OpControlBarrier itself; where cross-lane votes, mask reads, and v_readfirstlane_b32 runs coalesce (probe_80 / probe_160), cutting exchange blocks in half cuts NVVM compile time by -68.5% to -69.6% (3.2x–3.3x faster).

Tested

  1. Cold vkCreateComputePipelines benchmarks (vk_pipeline_check --jobs 1, __GL_SHADER_DISK_CACHE=0, unique OpString nonce per trial, 3 trials each, exact origin/main vs this PR):

    Synthetic wave64 workload origin/main barriers / OpFunctionCall / OpUMod PR barriers / OpFunctionCall / OpUMod origin/main median (3 runs) PR median (3 runs) Delta
    probe_80 (80 live-SCC saveexec + execz + 4x readfirstlane + readlane blocks) 641 / 640 / 5 (555.0 KiB) 321 / 0 / 0 (554.8 KiB) 6.062 s ([6.369, 6.062, 6.030]) 1.907 s ([1.679, 2.144, 1.907]) -68.5% (3.18x)
    probe_160 (160 probe/waterfall blocks) 1,281 / 1,280 / 5 (1,100.9 KiB) 641 / 0 / 0 (1,101.9 KiB) 17.433 s ([17.338, 17.710, 17.433]) 5.296 s ([5.310, 5.296, 5.121]) -69.6% (3.29x)
    sp_tree_156 (192-VGPR unfolded binary if/else tree, dead-SCC votes only) 156 / 339 / 4 (464.1 KiB) 156 / 184 / 0 (529.8 KiB) 4.168 s ([4.168, 3.750, 4.587]) 3.649 s ([3.789, 3.635, 3.649]) -12.5%
    sp_tree_324 (192-VGPR unfolded binary if/else tree, dead-SCC votes only) 324 / 699 / 4 (898.7 KiB) 324 / 376 / 0 (1,036.5 KiB) 12.224 s ([14.261, 12.181, 12.224]) 11.479 s ([11.853, 11.479, 11.279]) -6.1%
  2. GPU execution (LEONIDA_W64_EMULATE=1, subgroup 32..32):

    • vk_compute_test cs_w64x_xround_coalesce.spv 1 dump: 3/3 runs 0 mismatches, out[0..7] = 147 308 1832 444 625 1335 2379 332.
    • All 24/24 wave64 compute test shaders (cs_dpp_w64, all 20 cs_w64x_*, cs_w64x_partial, cs_w64x_partial_scmp, cs_wave64_ballot, and cs_wavemask) translate, pass spirv-val, and match expected outputs before (23/23) and after (24/24).
  3. CPU semantic, translator, and repository checks:

    • python -I tests/semantic/test_wave64_xround_barriers.py (9/9, 40/64/128-thread forward & reverse workgroup interpreter + spirv-val)
    • python -I tests/semantic/test_wave64_emulate_opt.py (33/33), test_wave64_emulate_barriers.py (13/13), test_loop_ballot_barrier.py (15/15), test_cs_shared_memory_limit.py (15/15), test_w64x_scalar_load_order.py (5/5)
    • python -I tests/test_shader_checks.py (88/88), test_ci_registration.py (16/16), test_text_write_line_endings.py (3/3), test_docs_*.py, scripts/check_no_game_data.py (exit 0).

GPUs tested: NVIDIA GeForce GTX 1050 Ti (Pascal, 4 GB), driver 582.28, Vulkan 1.4.312, Windows 10 Pro 10.0.19045

AI-assisted: yes (Antigravity, Gemini 4 Carbon)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2119 **Author:** [@robot00f](https://github.com/robot00f) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/w64-xround-barriers` --- ### 📝 Commits (1) - [`72d722a`](https://github.com/ubiquitarianism/leonida/commit/72d722a11892f7cbde2b5a43413f72bbef8f1747) shaders: coalesce and inline wave64 x_round exchanges (SHADER-W64-XROUND-BARRIERS) ### 📊 Changes **7 files changed** (+1026 additions, -178 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -1) 📝 `runtime/test/shaders/check.py` (+3 -1) ➕ `runtime/test/shaders/cs_w64x_xround_coalesce.asm` (+117 -0) 📝 `scripts/run_gpu_tests.py` (+2 -1) 📝 `tests/semantic/test_wave64_emulate_opt.py` (+5 -6) ➕ `tests/semantic/test_wave64_xround_barriers.py` (+462 -0) 📝 `tools/rdna2spirv.py` (+436 -169) </details> ### 📄 Description Claimed in the coordination issue as: `SHADER-W64-XROUND-BARRIERS` (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7172190888; follow-up to https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7159151871 and https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7159367489) ### What and why Coalesces wave-wide Workgroup exchanges and eliminates helper-call / modulo overhead in `Gen.x_round` (`emulate_inline` in `tools/rdna2spirv.py`) under `--wave64-emulate` on 32-wide GPUs, while keeping `%x_buf` byte-identical (`(2*L + 6*W + 7)` dwords): 1. **Division-free inline `Gen._x_round_inline`:** - Drops the outlined `%x_pre_*_fn` `OpFunctionCall`s (CGSCC inliner cloning across hundreds of callsites increased NVVM compile time by `+12%` to `+21%` vs inline). - Cycles `%x_it` through `0..5` (`lcm(2, 3) = 6`, preserving both `it & 1` parity and `it % 3` ring-slot indexing) with **zero `OpUMod`s** and omits the two dead scheduler-word reset `OpAtomicStore`s on multi-wave workgroups whenever `mini_blocks` and `tail_blocks` are empty. - Narrows barrier memory semantics from `0x948` (`ImageMemory | WorkgroupMemory | UniformMemory | AcquireRelease`) to `0x108` (`WorkgroupMemory | AcquireRelease`) whenever `"mem"` is clean in `x_dirty` (and in `x_order_barrier` when only `"lds"` is dirty). 2. **Block-local `Any`-vote and first-lane caching (`x_vcache`, `x_flcache`, `x_vkeys`):** - Shares `Any` votes and first-active-lane indices across `scc_from_mask`, `branch_condition`, `wave_mask`, `v_readfirstlane_b32`, and DPP `fi:0` within a basic block, and preserves `x_gen` across internal `OpSelectionMerge` regions (`buffer_load`, `buffer_store`, `ds_*`, `_emit_rfl_run`) that do not write `%active`, `%vcc`, `%vcc_hi`, or `%sgpr`. - Derives `Any` from a cached `(lo, hi)` mask in 2 ALU ops (`(lo | hi) != 0`, `0` barriers, `0` atomics); when `branch_condition` reuses a cached `Any` vote while `x_dirty` is non-empty, emits a bare `x_order_barrier()` to preserve `wave64_emulate_opt.py`'s `sb_clears_dirty` invariant. - Restricts `x_vkeys` registration in `mask_dst` to full-wave masks (`n == 2` SGPR pairs and full `vcc` in wave64) so 32-bit half-mask writes never alias 64-bit `x_vcache` entries. 3. **Deferred SCC vote coalescing (`_pending_scc_vote` in `emulate_inline`):** - Defers uncached `scc_from_mask` / `scc_any_half` votes until `%scc` is read/overwritten or a label/merge/barrier/branch/`%pc` store occurs. - Coalesces a pending SCC vote with a subsequent `branch_condition` on the same mask (`1` `OpAtomicOr`, `1` barrier), with another 1-bit `g_vote` on a distinct mask (`2 -> 1` barrier via 2-word `bits=(p_bit, bit2)`), with a `data`-only `x_round` (`v_readlane_b32`, `ds_bpermute_b32`, `ds_swizzle_b32`, `quad_any`, DPP `fi:1`, `2 -> 1` barrier via `data + bits`), or with `x_order_barrier` (`2 -> 1` barrier), and drops the vote (`0` barriers) if `%scc` is overwritten before being read. 4. **Batched contiguous `v_readfirstlane_b32` runs (`Gen._emit_rfl_run`):** - Establishes `first` once (`0` barriers if already in `x_flcache`/`x_mcache`, else `1` round on item 0) and transfers up to 64 remaining values per wave from the single running lane `wave_lane == first` across **one** `OpControlBarrier` (`4 -> 2` or `1` round barriers for V# scalarization, `8 -> 2` or `1` for T# scalarization, piggybacking any pending SCC vote into the same barrier). **NVVM ablation note (`#1608`):** Reproducing the 192-VGPR unfolded binary `if/else` `ScreenProbes` shape on GTX 1050 Ti (`sp_tree_156`: `464.1 KiB`, `156` barriers -> `4.168 s`; `sp_tree_324`: `898.7 KiB`, `324` barriers -> `12.224 s`) and stripping all middle `OpControlBarrier` instructions (`NO_BARS`) leaves `vkCreateComputePipelines` time unchanged (`12.13 s -> 12.22 s`, and `4.91 s -> 4.88 s` on 320-barrier loops). Thus the ~11 s baseline on pure unfolded `saveexec` trees is dominated by `%pc`-guarded `OpSelectionMerge` block splitting (`SROA`/`Mem2Reg` Phi webs across 192 VGPRs) + `%x_pre_*_fn` cloning rather than `OpControlBarrier` itself; where cross-lane votes, mask reads, and `v_readfirstlane_b32` runs coalesce (`probe_80` / `probe_160`), cutting exchange blocks in half cuts NVVM compile time by **`-68.5%` to `-69.6%` (`3.2x`–`3.3x` faster)**. ### Tested 1. **Cold `vkCreateComputePipelines` benchmarks (`vk_pipeline_check --jobs 1`, `__GL_SHADER_DISK_CACHE=0`, unique `OpString` nonce per trial, 3 trials each, exact `origin/main` vs this PR):** | Synthetic wave64 workload | `origin/main` barriers / `OpFunctionCall` / `OpUMod` | PR barriers / `OpFunctionCall` / `OpUMod` | `origin/main` median (`3` runs) | PR median (`3` runs) | Delta | | --- | --- | --- | --- | --- | --- | | `probe_80` (80 live-SCC `saveexec` + `execz` + 4x `readfirstlane` + `readlane` blocks) | `641` / `640` / `5` (`555.0 KiB`) | `321` / `0` / `0` (`554.8 KiB`) | `6.062 s` (`[6.369, 6.062, 6.030]`) | `1.907 s` (`[1.679, 2.144, 1.907]`) | **`-68.5%`** (`3.18x`) | | `probe_160` (160 probe/waterfall blocks) | `1,281` / `1,280` / `5` (`1,100.9 KiB`) | `641` / `0` / `0` (`1,101.9 KiB`) | `17.433 s` (`[17.338, 17.710, 17.433]`) | `5.296 s` (`[5.310, 5.296, 5.121]`) | **`-69.6%`** (`3.29x`) | | `sp_tree_156` (192-VGPR unfolded binary `if/else` tree, dead-SCC votes only) | `156` / `339` / `4` (`464.1 KiB`) | `156` / `184` / `0` (`529.8 KiB`) | `4.168 s` (`[4.168, 3.750, 4.587]`) | `3.649 s` (`[3.789, 3.635, 3.649]`) | **`-12.5%`** | | `sp_tree_324` (192-VGPR unfolded binary `if/else` tree, dead-SCC votes only) | `324` / `699` / `4` (`898.7 KiB`) | `324` / `376` / `0` (`1,036.5 KiB`) | `12.224 s` (`[14.261, 12.181, 12.224]`) | `11.479 s` (`[11.853, 11.479, 11.279]`) | **`-6.1%`** | 2. **GPU execution (`LEONIDA_W64_EMULATE=1`, `subgroup 32..32`):** - `vk_compute_test cs_w64x_xround_coalesce.spv 1 dump`: **3/3 runs `0 mismatches`**, `out[0..7] = 147 308 1832 444 625 1335 2379 332`. - All **24/24** wave64 compute test shaders (`cs_dpp_w64`, all 20 `cs_w64x_*`, `cs_w64x_partial`, `cs_w64x_partial_scmp`, `cs_wave64_ballot`, and `cs_wavemask`) translate, pass `spirv-val`, and match expected outputs before (23/23) and after (24/24). 3. **CPU semantic, translator, and repository checks:** - `python -I tests/semantic/test_wave64_xround_barriers.py` (9/9, 40/64/128-thread forward & reverse workgroup interpreter + `spirv-val`) - `python -I tests/semantic/test_wave64_emulate_opt.py` (33/33), `test_wave64_emulate_barriers.py` (13/13), `test_loop_ballot_barrier.py` (15/15), `test_cs_shared_memory_limit.py` (15/15), `test_w64x_scalar_load_order.py` (5/5) - `python -I tests/test_shader_checks.py` (88/88), `test_ci_registration.py` (16/16), `test_text_write_line_endings.py` (3/3), `test_docs_*.py`, `scripts/check_no_game_data.py` (exit 0). GPUs tested: NVIDIA GeForce GTX 1050 Ti (Pascal, 4 GB), driver 582.28, Vulkan 1.4.312, Windows 10 Pro 10.0.19045 AI-assisted: yes (Antigravity, Gemini 4 Carbon) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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
starred/leonida#2125
No description provided.