Skip to content

rdna2spirv: run DPP's ballot and shuffle in every lane under SPIRV-Cross (SHADER-529) - #3227

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
agulerman:shader-529-dpp-eager-subgroup
Nov 23, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
agulerman:shader-529-dpp-eager-subgroup

Conversation

@agulerman

Copy link
Copy Markdown

Claimed in the coordination issue as: SHADER-529

What and why

SPIRV-Cross (MoltenVK) inlines a result that is used once into the expression that uses it. The DPP lowering in Gen.dpp_insn left two subgroup ops where that inlining makes them conditional:

  • the ballot of the source lanes' EXEC bits ended up on the right of an OpLogicalAnd, which becomes a short-circuiting && in MSL: bool _255 = _246 && spvSubgroupBallotBitExtract(spvSubgroupBallot(_99), _251);
  • the shuffle ended up in the arm of a scalar OpSelect, which becomes a ternary: (_255 ? spvSubgroupShuffle(_93[0u], _251) : 0u), itself inside the active && enabled && src_ok ? … : old write.

So lanes outside their row skip the ballot, and the shuffle reads from lanes that never ran it. With row_shr:1, lane 1 finds lane 0 inactive and keeps its old value (cs_dpp_ops).

The fix stores both results to a Function variable of their own and loads them back (Gen.eager). A store is a statement, so every lane runs it. In SPIRV-Cross's MSL each op is now its own statement:

bool _100 = spvSubgroupBallotBitExtract(spvSubgroupBallot(_99), _253);
bool _258 = _248 && _100;
uint _101 = spvSubgroupShuffle(_93[0u], _253);

Only the non-emulated DPP path changes. The --wave64-emulate path uses no subgroup ops.

Tested

No GPU or Mac here, so this is not yet run on MoltenVK. Someone with a Mac should run cs_dpp* with check.py before this is called fixed on hardware.

  • runtime/test/shaders/build.py with LLVM 18's llvm-mc: 217 of the 218 GPU test shaders assemble (ps_sdwa_f16_const needs a newer llvm-mc). Before and after, all 217 translate and pass spirv-val. Exactly the nine cs_dpp* shaders translate differently.
  • SPIRV-Cross 1.3.239 (Ubuntu 24.04 package), --msl --msl-version 30000 on cs_dpp_ops: the before and after excerpts above.
  • New tests/semantic/test_dpp_eager_subgroup.py, 7 tests:
    • a static check modelling SPIRV-Cross's single-use forwarding, run over 11 DPP16/DPP8 forms (with and without a lane off in EXEC). It fails 22 of 22 cases on main and passes with this change. Its own tests cover what it flags and what it leaves alone.
    • the same programs run in spirv_wave_interp.py against a plain-Python DPP reference (values unchanged).
  • python -m unittest discover -s tests/semantic, re-run after rebasing onto main at 1628210: 4,149 tests. The only failure is test_fma_khr.test_spirv_tools_accept_it, because the distro's older spirv-as doesn't know FMAKHR. It fails the same way on main.
  • The standard-library tests/test_*.py loop, split as CI splits it: all 225 pass.
  • The pytest translator tests: 1,479 pass. The only failure is ps_sdwa_f16_const, which LLVM 18's llvm-mc can't assemble; CI uses LLVM 21.

Follow-ups (not in this PR)

The same static check over all 217 test shaders also flags subgroup ops in ternary arms outside DPP. Many look harmless: either the condition is wave-uniform (simd_any(x) ? FindLSB(Ballot(x)) : 0), or it's a reduction where the lanes that skip it would contribute 0 anyway. Some may not be. The ones to look at first are the swizzle/permlane shuffles behind the EXEC select in dst() (ps_w64_a_swizzle, ps_w64_a_permlane, cs_w32_permute, cs_w64x_permute), which give _87 ? spvSubgroupShuffle(...) : old. I can post them as a finding: if that's useful.

Checklist

  • 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. It's added to the DPP row, and says it's not run on a Mac yet.

@bobcatchris

Copy link
Copy Markdown
Contributor

The Mac run requested in the PR body, done on Apple M2 (Mac14,3), macOS 26.4 (25E246), MoltenVK 1.4.2 (Homebrew, direct), subgroup 4..32, llvm-mc 23.1.6, Python 3.13.18.

PR head a750e60 alone: python -I scripts/run_gpu_tests.py --filter cs_dpp --build runtime/build/linux translates 9 shaders, 9 pass spirv-val, but 0/9 run — all eight wave32 fixtures skip ("the device can't run this shader at 32 lanes on request") and cs_dpp_w64 skips (no 64-wide compute subgroup). So the head alone cannot answer the hardware question here.

To actually run them I locally merged open PR #2886 (the exact-32-lane harness fix) into a throwaway worktree of this head — no push, no change to your branch — and re-ran: 9 shaders translated, 9 pass spirv-val; 8/9 passed, 1 skipped (cs_dpp_w64 still skips). The passed words: cs_dpp 1 0 3 2 5 4 7 6, cs_dpp8 7 6 5 4 3 2 1 0, cs_dpp_bank 4294967295 ... 7 7 7 7, cs_dpp_bound 10 10 11 12 13 14 15 16, cs_dpp_fi 1 2 3 30583 5 6 7 30583, cs_dpp_mask 1 0 3 2 4008636142 x4, cs_dpp_ops 100 10 11 12 13 14 15 16, cs_dpp_row 0 0 1 2 3 4 5 6.

A/B: I also ran the same filter on #2886's head alone (without this PR) on this M2 — the eight fixtures give identical words both times. So this M2/MoltenVK 1.4.2 machine does not reproduce the lane-conditional DPP bug your static analysis found, and the change causes no regression here. It remains a Mac data point, not a refutation of the M3 Max/SPIRV-Cross finding.

Full builds at these heads fail on AppleClang 17 at vk_pipeline_check.cpp:567 (BUILD-822, fixed by open PR #2927); GPU binaries built, so all runs above are valid. Synthetic shaders and counts only; no game files.

AI-assisted.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested a750e60 on an RTX 5090 (driver 616.92), Windows 11, Vulkan SDK 1.4.363. The GPU test programs were built from the same tree:

  • scripts/run_gpu_tests.py: 143/189 passed, 46 skipped, 0 failed. main 1628210 gives 143/189, 46 skipped, 0 failed. All 194 test shaders translate and pass spirv-val.
  • tests/semantic/test_dpp_eager_subgroup.py: ran 7 tests in 1.364s; OK.

AI-assisted (Claude Code).

@TreXito

TreXito commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested a750e600 (base 16282101) on NVIDIA GeForce RTX 3060 Ti (Ampere, subgroup 32..32), driver 580.173.02 (Vulkan 1.4.312), Linux (Ubuntu 26.04 container on a TrueNAS 26.0 host): pass. This is a native Vulkan run, not MoltenVK, so it only shows that the eager stores don't change results off the Mac. The SPIRV-Cross case itself still needs a Mac.

Linux runtime built from the branch (CMake + Ninja, GCC, RelWithDebInfo); python3 -I scripts/run_gpu_tests.py, whole suite, fresh --work for each run:

base 16282101 this head
native wave32 142/189 passed, 46 skipped, 1 failed 142/189 passed, 46 skipped, 1 failed
LEONIDA_W64_EMULATE=1 173/189 passed, 15 skipped, 1 failed 173/189 passed, 15 skipped, 1 failed

AI-assisted (Claude Code). Synthetic test shaders only, no game files.

…oss (SHADER-529)

SPIRV-Cross (MoltenVK) inlines a single-use result into the expression that
uses it. The DPP lowering put the source-EXEC ballot on the right of an
OpLogicalAnd (a short-circuiting && in MSL) and the shuffle in the arm of a
scalar OpSelect (a ternary), so lanes out of their row skipped the ballot and
the shuffle read from lanes that never ran it: row_shr:1's lane 1 found lane 0
inactive and kept its old value (cs_dpp_ops on MoltenVK).

Both results are now stored to a Function variable of their own (Gen.eager)
and loaded back; a store is a statement every lane runs. Only the DPP path
changes: of the 217 GPU test shaders that assemble here, exactly the nine
cs_dpp* ones translate differently, all still pass spirv-val, and SPIRV-Cross
1.3.239's MSL now computes the ballot and the shuffle as their own statements.

tests/semantic/test_dpp_eager_subgroup.py models SPIRV-Cross's single-use
forwarding and fails on the old lowering for all 11 DPP16/DPP8 forms tested;
it also checks the values in spirv_wave_interp.py against a DPP reference.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GN11jbD4AeqgwR5C5Z5Xs9
@iurysf

iurysf commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Re-tested 4d65bae6 (head moved since the earlier results) on Windows 11, Python 3.14, llvm-mc 23.1.6, CPU only: pass. test_dpp_eager_subgroup.py 7 tests OK; with only tools/rdna2spirv.py restored to main: 22 failures. Not run: full semantic suite, GPU tests, MoltenVK. Tested with AI assistance (Claude); synthetic shaders only, no game data.

@Impre-visible

Copy link
Copy Markdown

Tested 4d65bae on NVIDIA GeForce GTX 1660 (Turing, TU116, subgroup 32..32), driver 620.73.04 (proprietary, Vulkan 1.4.357), Arch Linux (kernel 6.18): pass, no regression.

Native build of runtime/ from this head (Ninja, RelWithDebInfo, clang 23.1.5), llvm-mc 23.1.5, SPIRV-Tools v2026.4.

  • spirv-val on the modules the PR's semantic tests translate (test_dpp_eager_subgroup.py): distinct modules: 33, pass spirv-val: 33, rejected: 0
  • tests/semantic: 7329 OK
  • build.py: 224 shaders, 224 translated, 224 pass spirv-val; check.py: 139 of 174 pass, 35 skipped (wave64 without emulation, mesh fixtures)
  • run_gpu_tests.py: 204 shaders: 204 translated, 204 pass spirv-val; 153/199 passed, 46 skipped, 0 failed
  • LEONIDA_W64_EMULATE=1 run_gpu_tests.py: 184/199 passed, 15 skipped, 0 failed

AI-assisted run (Claude). Synthetic programs only, no game files involved.

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.

8 participants