Skip to content

rdna2spirv: a wave32 compute or mesh mask read uses one subgroup reduction, not two (-9% on a mask-read loop, same output) - #7261

Open
tedin7 wants to merge 1 commit into
ItsNotRexed:mainfrom
tedin7:shader-speed-mask
Open

tedin7 wants to merge 1 commit into
ItsNotRexed:mainfrom
tedin7:shader-speed-mask

Conversation

@tedin7

@tedin7 tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in #7049 (comment)

rdna2spirv: a wave32 compute mask read uses one subgroup reduction, not two

Gen.wave_mask builds the whole wave's mask (for s_bcnt1, s_ff1, v_mbcnt and similar) with two OpGroupNonUniformBitwiseOr reductions: one for lanes 0-31 and one for lanes 32-63. In a wave32 compute or mesh shader the lane is LocalInvocationIndex & 31 (wave_lane), so it is always below 32. The second reduction always gave 0. Now these shaders emit one reduction and use the constant 0 for the high word, as the wave32 fragment path already does. Wave64, emulated and fragment shaders are unchanged. Each subgroup reduction costs several shuffles on a 32-wide NVIDIA subgroup.

Measured

  • GPU (RTX 5080, driver 580.65.06, 8192 groups x 64 threads; a loop of v_readlane, v_cmp, s_bcnt1 and v_mbcnt): 0.426 ms becomes 0.387 ms per dispatch (-9%). A hand-written GLSL version runs 0.373 ms. The output buffer is byte-identical: FNV-1a over the whole buffer matches main and GLSL.
  • Modules: over 2,362 test modules (265 fixtures plus crosslane, lds, valu f16 and mem fuzz listings), 297 change; all of them are wave32 compute. Every module still passes spirv-val.

Tests

  • New tests/semantic/test_wave32_mask_reduction.py: a two-wave wave32 group with s_bcnt1 and v_mbcnt of a VCC mask. It expects one reduction per read and checks the counts per lane with run_compute_workgroup. It fails on main (4 != 2) and passes here.
  • tests/semantic: every file passes except test_float_nan_fract.py, which fails the same way on main (3 NaN-payload failures).
  • tests/test_translate.py: 638 passed.
  • scripts/run_gpu_tests.py on an RTX 3090 (NVIDIA 580.105.08, 32-wide subgroups): the result of every test matches main. Native: 183/235 passed, 51 skipped. With LEONIDA_W64_EMULATE=1: 214/235 passed, 20 skipped. The one failure (ms_wave_info_s3) fails on main too.
  • Fuzzers on the RTX 3090, wave32, seed 11, 500 programs each, branch and main side by side:
    • crosslane_fuzz: 451 match, 32 mismatch, 17 hypothesis-only on both, with the same failing programs. These mismatches are already on main.
    • lds_fuzz, valu_fuzz --family f16 and valu_fuzz --family mem: 500/500 match on both.

A wave32 compute or mesh lane is LocalInvocationIndex mod 32, so the reduction for lanes 32-63 in Gen.wave_mask was always 0. Emit one OpGroupNonUniformBitwiseOr and a constant 0 high word, as the wave32 fragment path already does. 0.426 -> 0.387 ms per dispatch on a mask-heavy kernel (RTX 5080), same output bytes.
@dadsmallbuk1

Copy link
Copy Markdown

Tested 7dda9b4 on NVIDIA GeForce RTX 5090 (32-wide subgroups), driver 617.42, Windows 11 (26200), native MSVC 2022 RelWithDebInfo, llvm-mc 21.1.8 and SPIRV-Tools: pass.

  • Semantic, CPython 3.12: the new test_wave32_mask_reduction plus the existing mask-read suites (test_alu_bcnt_u32, test_ballot_votes, test_lane_mask_reductions, test_loop_ballot_barrier, test_mbcnt_lo_hi, test_scalar_bcnt, test_wave64_halves_readlane_uniform): 123 tests OK. The 122 existing ones are also OK at the merge-base.
  • GPU, scripts/run_gpu_tests.py --filter cs_w32: 17/17 pass, all translated and passing spirv-val.
  • GPU, --filter cs_: 76/110 pass, 0 fail. The 34 skipped are the native wave64 fixtures (no 64-wide compute subgroup on this device).
  • Code read: only Gen.wave_mask changes, for non-wave64 cs/ms, where wave_lane() is LocalInvocationIndex & 31. The dropped high-half reduction could only be 0 there, so the result is unchanged. Wave64, emulated and fragment paths are untouched.

AI-assisted (Claude Code).

@kamilolejnik

Copy link
Copy Markdown

Tested 7dda9b4 on Intel Iris Xe Graphics (TGL GT2, Xe-LP, variable subgroup 8..32), Mesa ANV 26.3.1, Vulkan 1.4.363, Linux 6.12 (Arch container on Debian 13), llvm-mc 23.1.5, SPIRV-Tools v2026.4, GCC: pass. Branch and its merge-base main 8575d36 side by side, same binaries.

  • GPU, scripts/run_gpu_tests.py, every test's status is identical to main:
    • native: 176/239 passed, 63 skipped, 0 failed (both)
    • LEONIDA_W64_EMULATE=1: 206/239 passed, 33 skipped, 0 failed (both)
  • crosslane_fuzz.py --programs 300 --seed 11 --wave 32 --modes native, same vk_compute_test, --translator pointed at each side: 276 match, 15 mismatch, 9 hypothesis-only on both, and the 24 non-matching programs are the same set (all of them include maskdata, Translator: a lane mask read as data gives the wave's bits (SHADER-21 part 1, #210) #293).
  • CPU: test_wave32_mask_reduction plus test_alu_bcnt_u32, test_ballot_votes, test_lane_mask_reductions, test_loop_ballot_barrier, test_mbcnt_lo_hi, test_scalar_bcnt, test_wave64_halves_readlane_uniform: 123 OK. The new test against main's translator fails with 4 != 2, as described.

So the single-reduction mask read holds on a driver whose subgroup size is not fixed at 32, not only on NVIDIA. No timing measured here. Synthetic fixtures only, no game files. AI-assisted (Claude Code).

@crastinator

Copy link
Copy Markdown
Contributor

Tested 7dda9b4c merged onto main 26ecd9f9 (clean merge) on AMD + RADV: pass, no change in any result.

AMD Radeon RX 6700 XT (gfx1031, RDNA2), RADV Mesa 26.3.1, CachyOS, Linux 7.3.5. Each run done on main and on the PR in the same build:

main this PR
run_gpu_tests.py native 223/240, 16 skipped 223/240, 16 skipped
run_gpu_tests.py, LEONIDA_W64_EMULATE=1 223/240, 16 skipped 223/240, 16 skipped
crosslane_fuzz --wave 32 --seed 11 --programs 500 451 match, 32 mismatch, 17 hyp-only the same 451 / 32 / 17 (mismatch lines identical)
lds_fuzz, valu_fuzz --family f16, --family mem (wave32, seed 11, 500) 500/500 each 500/500 each
tests/semantic/test_wave32_mask_reduction.py 1 failed passed
tests/test_translate.py + the new test 647 passed

Per-test results are identical between main and the PR, native and emulated. The only failure, ms_wave_info_s3, fails on main too. The crosslane numbers match your RTX 3090 exactly, so those 32 mismatches don't depend on the vendor and already exist on main.

On AMD the subgroup could be 64 wide. Every wave32 cs module in the run's index.json (80 of them) asks for subgroup_size 32, so a RADV subgroup is one guest wave, and the single reduction covers the whole mask, as on NVIDIA. I didn't time the mask-read loop here, since the benchmark isn't in the PR.

AI-assisted (Claude Code).

@carlos-seiffert

Copy link
Copy Markdown

Tested 7dda9b4 (merged onto main 86a7807) on NVIDIA GeForce RTX 2060 (Turing), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 (10.0.26300), subgroup size 32: pass (no regression)

What I ran: python -I scripts/run_gpu_tests.py with the Vulkan tools built from 86a7807 (llvm-mingw + Ninja, -DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF, Vulkan-Headers 6802bb47), llvm-mc 23.1.6 and SPIRV-Tools from conda-forge.

main 86a7807 this PR on main
full GPU suite 191/240 passed, 48 skipped, 1 fail 191/240 passed, 48 skipped, 1 fail (same as main)

No GPU test changed status against main. This PR adds no GPU fixture of its own, so the run checks that the translator change breaks none of the existing GPU tests; it does not exercise the new rule directly.

Unit tests changed by the PR: tests/semantic/test_wave32_mask_reduction.py: OK.

The one failure on both sides is ms_wave_info_s3, which fails on main on this GPU (10/10 runs) and is fixed by #7136 / #5741 / #5855; it is unrelated to this PR.

AI-assisted run (Claude Code); the numbers are from my machine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants