[PR #1683] rdna2spirv: ds_permute_b32 stores its shuffles, so every lane runs them (SHADER-PERMUTE-EAGER) #1697

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

📋 Pull Request Information

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

Base: main ← Head: shader-permute-eager


📝 Commits (1)

  • 28cb947 rdna2spirv: ds_permute_b32 stores its shuffles, so every lane runs them (SHADER-PERMUTE-EAGER)

📊 Changes

4 files changed (+138 additions, -1 deletions)

View changed files

📝 docs/design.md (+1 -1)
📝 tests/semantic/README.md (+5 -0)
➕ tests/semantic/test_ds_permute_eager.py (+126 -0)
📝 tools/rdna2spirv.py (+6 -0)

📄 Description

Claimed in the coordination issue as: SHADER-PERMUTE-EAGER (claim)

What and why

On an Apple M5 (MoltenVK 1.4.2) ds_permute_b32 delivers wrong words: tools/lds_fuzz.py --wave 32 --permute has about a quarter of its programs differ from the model. Over seeds 1, 2, 3 and 31002, 1,374 of the 1,440 wrong words were last written by a ds_permute_b32 (the others by an op a wrong word fed), and 1,238 of the 1,440 read 0 where a source lane's data belongs. docs/shader_validation.md records 0 mismatches for the op on RADV. The fuzzer files the op under "hypothesis" and exits 0, so the runs looked clean.

Gen.ds_permute lets each destination lane scan the 32 source lanes of its half: per source lane a shuffle of the published target, a shuffle of the data, and an OpSelect that keeps the data when the target is this lane. Each shuffle is used once, and so is every link of that chain. SPIRV-Cross inlines a result that is used once into the expression that uses it, so the 32 steps become one nested ternary inside the EXEC ternary of the register write:

v10 = active ? (hit31 ? data31 : (hit30 ? data30 : ...)) : v10;

A lane that takes an outer arm, or is off in EXEC, runs none of the shuffles inside it. This is the SHADER-529 mechanism; test_dpp_eager_subgroup.py's model of that inlining flags all 64 shuffles of one ds_permute_b32 on main.

  • tools/rdna2spirv.py: Gen.ds_permute stores both shuffles to Function variables (Gen.eager), as the DPP rule stores its ballot bit and its shuffle. A store is a statement every lane runs. Native path only; --wave64-emulate has no subgroup operation in this rule.
  • tests/semantic/test_ds_permute_eager.py: no shuffle of the rule is left in a conditional position, in a wave32 and a wave64 compute shader and a pixel shader; each of the 64 is read by one OpStore to a variable of its own; the module with the stores taken out again has all 64 in a ternary's arm; and the stored form gives the ISA model's words with EXEC narrowed, sources that are off and a collision.
  • docs/design.md (the LDS row) and tests/semantic/README.md: the rule and how it was confirmed.

ds_permute_b32 occurs in no shader of the corpus (docs/testing.md), so no game shader changes; the cost is 64 Function variables per ds_permute_b32.

Tested

Apple M5, MoltenVK 1.4.2, llvm-mc 23.1.6, SPIRV-Tools v2026.4. Before is main 3f81d46ac, after is this branch on the same commit, both with the test programs built from main.

python3 -I tools/lds_fuzz.py --wave 32 <options>, programs that match the model:

Options Before After
--permute --programs 100 --seed 31002 83 of 100 100 of 100
--permute --programs 300 --seed 1 216 of 300 300 of 300
--permute --programs 300 --seed 2 227 of 300 300 of 300
--permute --programs 300 --seed 3 223 of 300 300 of 300
--extended --programs 300 --seed 31002 210 of 300 300 of 300
--programs 300 --seed 31001 (default mix, no ds_permute_b32) 300 of 300 300 of 300
  • Finding the cause: storing the data shuffle alone left 13 of the 100 programs of seed 31002 differing; storing both leaves none.
  • scripts/run_gpu_tests.py: 193/263 passed, 68 skipped, before and after, every test with the same verdict (cs_w32_permute and cs_w32_partial_permute pass in both; the two failures are cs_fma_fused, the MoltenVK 1.4.2 row of docs/testing.md, and cs_imgatomic_ext, which is red on main and claimed by someone else as GPU-IMG-ATOMIC-EXT-VOLUME-TAG). Its translation step reports 273 shaders: 272 translated, 272 pass spirv-val in both. 3 of the 275 modules in its work folder change: cs_w32_permute, cs_w32_partial_permute and cs_w64x_permute.
  • lds_fuzz.py --wave 64 --emulate --extended --programs 200 --seed 31003 (the path this does not touch): 148 of 148 that ran match, before and after.
  • tests/semantic/test_ds_permute_eager.py: 5 tests pass; with main's translator 7 checks fail (each shuffle "is read by OpSelect, not stored").
  • python3 -I scripts/run_semantic_tests.py tests/semantic: 487 files, 6,279 tests, 0 failed before; 488 files, 6,284 tests, 0 failed after.
  • The three shaders of the new test assemble with spirv-as and spirv-val reports nothing.
  • pytest files: 2 failed, 1908 passed, 18 skipped, before and after alike (the two are tests/test_translate.py on cs_dpp_cmp.asm, #1375's subject).
  • Standard-library loop: 498 files, 4 failed, before and after alike; none is touched here (test_mod_pack_output_alias.py: #1412; test_runner_calls_all_tests.py and test_tool_stdout_encoding.py: #1561; test_runtime_paths.py: #1657).
  • tests/test_docs_*.py pass; python3 -I scripts/check_no_game_data.py tests/semantic/test_ds_permute_eager.py is clean.

Not tested: a GPU other than the M5 (on a driver that does not inline this way the change is a store and a load of the same value), wave64 on a 64-wide subgroup and the pixel-shader form on a GPU (both only in the interpreter), and the game corpus (the op is not in it).

GPUs tested: Apple M5 (8-core GPU), MoltenVK 1.4.2, macOS 27.2.1 (26B131)

AI-assisted: yes

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.

🤖 Generated with Claude Code


🔄 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/1683 **Author:** [@hugo0024](https://github.com/hugo0024) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `shader-permute-eager` --- ### 📝 Commits (1) - [`28cb947`](https://github.com/ubiquitarianism/leonida/commit/28cb94705a824f614ab49626f8fbff85616512b5) rdna2spirv: ds_permute_b32 stores its shuffles, so every lane runs them (SHADER-PERMUTE-EAGER) ### 📊 Changes **4 files changed** (+138 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -1) 📝 `tests/semantic/README.md` (+5 -0) ➕ `tests/semantic/test_ds_permute_eager.py` (+126 -0) 📝 `tools/rdna2spirv.py` (+6 -0) </details> ### 📄 Description Claimed in the coordination issue as: SHADER-PERMUTE-EAGER ([claim](https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7156204534)) ### What and why On an Apple M5 (MoltenVK 1.4.2) `ds_permute_b32` delivers wrong words: `tools/lds_fuzz.py --wave 32 --permute` has about a quarter of its programs differ from the model. Over seeds 1, 2, 3 and 31002, 1,374 of the 1,440 wrong words were last written by a `ds_permute_b32` (the others by an op a wrong word fed), and 1,238 of the 1,440 read 0 where a source lane's data belongs. `docs/shader_validation.md` records 0 mismatches for the op on RADV. The fuzzer files the op under "hypothesis" and exits 0, so the runs looked clean. `Gen.ds_permute` lets each destination lane scan the 32 source lanes of its half: per source lane a shuffle of the published target, a shuffle of the data, and an `OpSelect` that keeps the data when the target is this lane. Each shuffle is used once, and so is every link of that chain. SPIRV-Cross inlines a result that is used once into the expression that uses it, so the 32 steps become one nested ternary inside the EXEC ternary of the register write: ```text v10 = active ? (hit31 ? data31 : (hit30 ? data30 : ...)) : v10; ``` A lane that takes an outer arm, or is off in EXEC, runs none of the shuffles inside it. This is the SHADER-529 mechanism; `test_dpp_eager_subgroup.py`'s model of that inlining flags all 64 shuffles of one `ds_permute_b32` on `main`. - `tools/rdna2spirv.py`: `Gen.ds_permute` stores both shuffles to Function variables (`Gen.eager`), as the DPP rule stores its ballot bit and its shuffle. A store is a statement every lane runs. Native path only; `--wave64-emulate` has no subgroup operation in this rule. - `tests/semantic/test_ds_permute_eager.py`: no shuffle of the rule is left in a conditional position, in a wave32 and a wave64 compute shader and a pixel shader; each of the 64 is read by one `OpStore` to a variable of its own; the module with the stores taken out again has all 64 in a ternary's arm; and the stored form gives the ISA model's words with EXEC narrowed, sources that are off and a collision. - `docs/design.md` (the LDS row) and `tests/semantic/README.md`: the rule and how it was confirmed. `ds_permute_b32` occurs in no shader of the corpus (`docs/testing.md`), so no game shader changes; the cost is 64 Function variables per `ds_permute_b32`. ### Tested Apple M5, MoltenVK 1.4.2, llvm-mc 23.1.6, SPIRV-Tools v2026.4. Before is `main` `3f81d46ac`, after is this branch on the same commit, both with the test programs built from `main`. `python3 -I tools/lds_fuzz.py --wave 32 <options>`, programs that match the model: | Options | Before | After | |---|---|---| | `--permute --programs 100 --seed 31002` | 83 of 100 | 100 of 100 | | `--permute --programs 300 --seed 1` | 216 of 300 | 300 of 300 | | `--permute --programs 300 --seed 2` | 227 of 300 | 300 of 300 | | `--permute --programs 300 --seed 3` | 223 of 300 | 300 of 300 | | `--extended --programs 300 --seed 31002` | 210 of 300 | 300 of 300 | | `--programs 300 --seed 31001` (default mix, no `ds_permute_b32`) | 300 of 300 | 300 of 300 | - Finding the cause: storing the data shuffle alone left 13 of the 100 programs of seed 31002 differing; storing both leaves none. - `scripts/run_gpu_tests.py`: 193/263 passed, 68 skipped, before and after, every test with the same verdict (`cs_w32_permute` and `cs_w32_partial_permute` pass in both; the two failures are `cs_fma_fused`, the MoltenVK 1.4.2 row of `docs/testing.md`, and `cs_imgatomic_ext`, which is red on `main` and claimed by someone else as GPU-IMG-ATOMIC-EXT-VOLUME-TAG). Its translation step reports `273 shaders: 272 translated, 272 pass spirv-val` in both. 3 of the 275 modules in its work folder change: `cs_w32_permute`, `cs_w32_partial_permute` and `cs_w64x_permute`. - `lds_fuzz.py --wave 64 --emulate --extended --programs 200 --seed 31003` (the path this does not touch): 148 of 148 that ran match, before and after. - `tests/semantic/test_ds_permute_eager.py`: 5 tests pass; with `main`'s translator 7 checks fail (each shuffle "is read by `OpSelect`, not stored"). - `python3 -I scripts/run_semantic_tests.py tests/semantic`: 487 files, 6,279 tests, 0 failed before; 488 files, 6,284 tests, 0 failed after. - The three shaders of the new test assemble with `spirv-as` and `spirv-val` reports nothing. - pytest files: 2 failed, 1908 passed, 18 skipped, before and after alike (the two are `tests/test_translate.py` on `cs_dpp_cmp.asm`, #1375's subject). - Standard-library loop: 498 files, 4 failed, before and after alike; none is touched here (`test_mod_pack_output_alias.py`: #1412; `test_runner_calls_all_tests.py` and `test_tool_stdout_encoding.py`: #1561; `test_runtime_paths.py`: #1657). - `tests/test_docs_*.py` pass; `python3 -I scripts/check_no_game_data.py tests/semantic/test_ds_permute_eager.py` is clean. Not tested: a GPU other than the M5 (on a driver that does not inline this way the change is a store and a load of the same value), wave64 on a 64-wide subgroup and the pixel-shader form on a GPU (both only in the interpreter), and the game corpus (the op is not in it). GPUs tested: Apple M5 (8-core GPU), MoltenVK 1.4.2, macOS 27.2.1 (26B131) AI-assisted: yes ### 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. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <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#1697
No description provided.