[PR #793] [CLOSED] wave64_emulate_opt: flow-sensitive must-be-zero proof in _exec_writes_stay_inactive #830

Closed
opened 2026-11-26 20:19:11 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/793
Author: @SweatierKey
Created: 11/24/2026
Status: ❌ Closed

Base: main ← Head: feat/w64-flow-sensitive-exec-fold


📝 Commits (1)

  • e72218e wave64_emulate_opt: flow-sensitive must-be-zero proof in _exec_writes_stay_inactive

📊 Changes

4 files changed (+307 additions, -6 deletions)

View changed files

📝 docs/design.md (+1 -0)
📝 tests/semantic/test_wave64_emulate_opt.py (+52 -0)
📝 tests/test_wave64_emulate_opt_cfg.py (+92 -0)
📝 tools/wave64_emulate_opt.py (+162 -6)

📄 Description

Claimed in the coordination issue as: SHADER-W64-FLOW-EXEC-FOLD (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7133434431)

What and why

In --wave64-emulate, find_local_exec_folds folds single-entry/single-exit s_cbranch_execz/s_cbranch_execnz regions behind if (%active) only when _exec_writes_stay_inactive can prove that every EXEC write inside the region keeps active == False for lanes that entered with EXEC == 0.

Previously, _exec_writes_stay_inactive was flow-insensitive per 64-bit SGPR pair: if any instruction anywhere in the region wrote a non-zero-preserving mask into s[r:r+1] (for instance, computing a negated inner branch mask with s_or_b64 / s_xor_b64 s[r:r+1], ..., -1), the pair was permanently disqualified — even when the very next instruction overwrote s[r:r+1] with old EXEC via s_and_saveexec_b64 s[r:r+1], s[r:r+1] before restoring s_mov_b64 exec, s[r:r+1]. On large wave64 compute shaders that reuse scratch SGPR pairs for nested saveexec guards, this falsely rejected thousands of outer s_cbranch_execz folds and forced wave votes + OpControlBarrier round cuts.

This change replaces the flow-insensitive check (when CFG context run, succ, active_blocks is supplied from find_local_exec_folds) with a sound forward must-be-zero dataflow over 64-bit SGPR pairs s[0..126] and vcc:

  • Tracks exact zero-preserving writes (s_*_saveexec_b64, s_andn1/andn2_wrexec_b64, v_cmp_*, s_and/andn2/or/xor/mov/cselect/cmov_b64).
  • Kills every overlapping 64-bit pair (s_bits | (s_bits >> 1)) on any partial 32-bit or non-zero-preserving SGPR write (including VOP3B carry-out masks) and kills vcc on any partial/non-preserving write.
  • Meets predecessor states with bitwise AND across branches/loops and runs a post-convergence verification pass over every block in the region.
  • Keeps the legacy flow-insensitive fallback when _exec_writes_stay_inactive is called without CFG context.

Aggregate impact measured on cached large wave64 compute shaders (counts only):

  • On the two largest wave64 compute shaders, OpControlBarrier count drops from 4,090 -> 595 (-85.5%) and 4,073 -> 578 (-85.8%); across 6 large wave64 compute shaders, total OpControlBarrier drops from 8,263 -> 1,273 (-84.6%), and all translated SPIR-V modules pass spirv-val --target-env vulkan1.2.

Tested

  • python3 -I tests/test_wave64_emulate_opt_cfg.py: 33/33 passed (including 7 new positive and negative soundness tests in FlowSensitiveExecInactiveTest: reused scratch saveexec, v_cmp + s_cselect_b64, unoverwritten s_xor -1, diamond merge with 1 non-zero branch, loop back-edge clobber, partial 32-bit overwrite of pair high half, VOP3B carry-out).
  • LEONIDA_REQUIRE_SPIRV_TOOLS=1 python3 -I tests/semantic/test_wave64_emulate_opt.py: 32/32 passed (including new test_flow_sensitive_reused_scratch_saveexec_folds_without_barriers on 64-thread and 128-thread workgroups: 1 superblock, 0 wave votes, bit-exact outputs, passes spirv-val).
  • LEONIDA_REQUIRE_SPIRV_TOOLS=1 python3 -I scripts/run_semantic_tests.py tests/semantic: 465 files, 6109 tests, 0 failures.
  • python3 -I tests/test_docs_links.py (16/16), python3 -I tests/test_text_write_line_endings.py (3/3), python3 -I scripts/check_no_game_data.py, python3 -m compileall -q make_game.py tools tests: all passed.
  • Real GPU execution (vk_compute_test.exe with --wave64-emulate and spirv-val enabled): 30/30 wave64 compute fixtures (cs_w64*, cs_w64x_*, cs_wave64_ballot, cs_wm_*, cs_wqm_w64*, cs_dpp_w64) plus the nested scratch-saveexec shader passed with exact readback outputs.

GPUs tested: NVIDIA GeForce RTX 5070 Ti, driver 616.92, Windows 11

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.

🔄 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/793 **Author:** [@SweatierKey](https://github.com/SweatierKey) **Created:** 11/24/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/w64-flow-sensitive-exec-fold` --- ### 📝 Commits (1) - [`e72218e`](https://github.com/ubiquitarianism/leonida/commit/e72218ed2a4116737cd4bfebbfddb79558c077ce) wave64_emulate_opt: flow-sensitive must-be-zero proof in _exec_writes_stay_inactive ### 📊 Changes **4 files changed** (+307 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -0) 📝 `tests/semantic/test_wave64_emulate_opt.py` (+52 -0) 📝 `tests/test_wave64_emulate_opt_cfg.py` (+92 -0) 📝 `tools/wave64_emulate_opt.py` (+162 -6) </details> ### 📄 Description Claimed in the coordination issue as: SHADER-W64-FLOW-EXEC-FOLD (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7133434431) ### What and why In `--wave64-emulate`, `find_local_exec_folds` folds single-entry/single-exit `s_cbranch_execz`/`s_cbranch_execnz` regions behind `if (%active)` only when `_exec_writes_stay_inactive` can prove that every `EXEC` write inside the region keeps `active == False` for lanes that entered with `EXEC == 0`. Previously, `_exec_writes_stay_inactive` was flow-insensitive per 64-bit SGPR pair: if any instruction anywhere in the region wrote a non-zero-preserving mask into `s[r:r+1]` (for instance, computing a negated inner branch mask with `s_or_b64` / `s_xor_b64 s[r:r+1], ..., -1`), the pair was permanently disqualified — even when the very next instruction overwrote `s[r:r+1]` with old `EXEC` via `s_and_saveexec_b64 s[r:r+1], s[r:r+1]` before restoring `s_mov_b64 exec, s[r:r+1]`. On large wave64 compute shaders that reuse scratch SGPR pairs for nested `saveexec` guards, this falsely rejected thousands of outer `s_cbranch_execz` folds and forced wave votes + `OpControlBarrier` round cuts. This change replaces the flow-insensitive check (when CFG context `run`, `succ`, `active_blocks` is supplied from `find_local_exec_folds`) with a sound forward must-be-zero dataflow over 64-bit SGPR pairs `s[0..126]` and `vcc`: - Tracks exact zero-preserving writes (`s_*_saveexec_b64`, `s_andn1/andn2_wrexec_b64`, `v_cmp_*`, `s_and/andn2/or/xor/mov/cselect/cmov_b64`). - Kills every overlapping 64-bit pair `(s_bits | (s_bits >> 1))` on any partial 32-bit or non-zero-preserving SGPR write (including VOP3B carry-out masks) and kills `vcc` on any partial/non-preserving write. - Meets predecessor states with bitwise AND across branches/loops and runs a post-convergence verification pass over every block in the region. - Keeps the legacy flow-insensitive fallback when `_exec_writes_stay_inactive` is called without CFG context. Aggregate impact measured on cached large wave64 compute shaders (counts only): - On the two largest wave64 compute shaders, `OpControlBarrier` count drops from `4,090 -> 595` (`-85.5%`) and `4,073 -> 578` (`-85.8%`); across 6 large wave64 compute shaders, total `OpControlBarrier` drops from `8,263 -> 1,273` (`-84.6%`), and all translated SPIR-V modules pass `spirv-val --target-env vulkan1.2`. ### Tested - `python3 -I tests/test_wave64_emulate_opt_cfg.py`: 33/33 passed (including 7 new positive and negative soundness tests in `FlowSensitiveExecInactiveTest`: reused scratch `saveexec`, `v_cmp` + `s_cselect_b64`, unoverwritten `s_xor -1`, diamond merge with 1 non-zero branch, loop back-edge clobber, partial 32-bit overwrite of pair high half, VOP3B carry-out). - `LEONIDA_REQUIRE_SPIRV_TOOLS=1 python3 -I tests/semantic/test_wave64_emulate_opt.py`: 32/32 passed (including new `test_flow_sensitive_reused_scratch_saveexec_folds_without_barriers` on 64-thread and 128-thread workgroups: 1 superblock, 0 wave votes, bit-exact outputs, passes `spirv-val`). - `LEONIDA_REQUIRE_SPIRV_TOOLS=1 python3 -I scripts/run_semantic_tests.py tests/semantic`: 465 files, 6109 tests, 0 failures. - `python3 -I tests/test_docs_links.py` (16/16), `python3 -I tests/test_text_write_line_endings.py` (3/3), `python3 -I scripts/check_no_game_data.py`, `python3 -m compileall -q make_game.py tools tests`: all passed. - Real GPU execution (`vk_compute_test.exe` with `--wave64-emulate` and `spirv-val` enabled): 30/30 wave64 compute fixtures (`cs_w64*`, `cs_w64x_*`, `cs_wave64_ballot`, `cs_wm_*`, `cs_wqm_w64*`, `cs_dpp_w64`) plus the nested scratch-saveexec shader passed with exact readback outputs. GPUs tested: NVIDIA GeForce RTX 5070 Ti, driver 616.92, Windows 11 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:19:11 +01:00
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#830
No description provided.