Skip to content

wave64_emulate_opt: flow-sensitive must-be-zero proof in _exec_writes_stay_inactive - #793

Closed
SweatierKey wants to merge 1 commit into
ProjectLeonida:mainfrom
SweatierKey:feat/w64-flow-sensitive-exec-fold
Closed

SweatierKey wants to merge 1 commit into
ProjectLeonida:mainfrom
SweatierKey:feat/w64-flow-sensitive-exec-fold

Conversation

@SweatierKey

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-W64-FLOW-EXEC-FOLD (#14 (comment))

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.

@KernelSmoker KernelSmoker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent Linux/CPU verification of PR head e72218ed2a4116737cd4bfebbfddb79558c077ce on base d0da7dade8b688f2e0f7f23f4155d6cbb438809e: tests/test_wave64_emulate_opt_cfg.py passed 26/26 and tests/semantic/test_wave64_emulate_opt.py passed 31/31; git diff --check passed. The semantic module exercised generated shaders through the CPU interpreter. No GPU or game data used. At the latest hosted-check refresh, the PR checks were still queued. AI-assisted independent verification.

@Sangonapou

Copy link
Copy Markdown
Contributor

Tested e72218e on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32..32), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300): pass

Runtime built at this head with scripts\build_runtime.ps1 (MSVC 19.51); llvm-mc 23.1.6, SPIRV-Tools 2026.4. No game files.

  • CPU: tests/semantic/test_wave64_emulate_opt.py 32 OK, tests/test_wave64_emulate_opt_cfg.py 33 OK.
  • GPU suite: 258 shaders, 258 translated, 258 pass spirv-val; native 198/249 passed, 49 skipped; LEONIDA_W64_EMULATE=1 (the path this changes, on a 32-wide device) 230/249 passed, 17 skipped. The only failures are cs_imgload_tfe and cs_imgload_d16_tfe, which fail the same way on main on this card (fixture expectations, tests: correct resident TFE status expectations (SHADER-1266) #892).

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

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants