Skip to content

wave64: align emulate barrier detection for s_flbit, mixed s_cmp_*_u64, and ds_nop - #4596

Open
kotekompotek wants to merge 1 commit into
ItsNotRexed:mainfrom
kotekompotek:contribution/wave64-emulate-barrier-sync
Open

kotekompotek wants to merge 1 commit into
ItsNotRexed:mainfrom
kotekompotek:contribution/wave64-emulate-barrier-sync

Conversation

@kotekompotek

Copy link
Copy Markdown

Claimed in the coordination issue as: #4571 (comment)

What and why

Fixes three --wave64-emulate barrier-prediction / x_memory_order mismatches between tools/wave64_emulate_opt.py and tools/rdna2spirv.py that crash rdna2spirv.translate() with AssertionError inside folded s_cbranch_execz regions (x_split inside a folded lane-local region) and multi-wave sync_free guards (unexpected OpControlBarrier in sync-free multi-wave block). This is separate from #4479 (lane_either mixed mask+constant pairs) and does not change _holds_lane_mask or lane_either:

  1. s_flbit_i32_b32 and s_flbit_i32 on lane masks (tools/wave64_emulate_opt.py): rdna2spirv.py calls lane_mask_word() -> wave_mask() -> x_round() when holds_lane_mask(a[1]) is true, but _classify_block_body() and _insn_may_barrier() omitted both opcodes. Added both to the lane-mask scan checks (plain scalar data operands still fold and stay sync_free).
  2. s_cmp_eq_u64 / s_cmp_lg_u64 on mixed mask+data SGPR pairs (tools/wave64_emulate_opt.py): rdna2spirv.py:7473-7483 materializes each individual register of a 2-register SGPR operand with wave_mask() -> x_round() whenever cur_masks.get(r) is not None, even if the other half holds arbitrary runtime scalar data (not EITHER). Added _has_any_lane_mask_word() and checked it for s_cmp_eq_u64 / s_cmp_lg_u64 in _classify_block_body() and _insn_may_barrier() (plain-data pairs still fold and stay sync_free).
  3. ds_nop in Gen.x_memory_order() (tools/rdna2spirv.py): Gen.insn() calls x_memory_order() before _insn() returns early on NOOPS, and x_memory_order() did not skip "ds_nop" (unlike NOOPS and wave64_emulate_opt._x_memory_order_access()). As a result, ds_nop was treated as an LDS read+write, emitting a spurious x_order_barrier() and crashing folded/sync-free blocks that combine ds_read_*/ds_write_* with ds_nop. Added "ds_nop" to Gen.x_memory_order()'s early return.
  4. Tests & docs: added FindSMsb support to run_emulated_workgroup() in tests/semantic/test_wave64_emulate_opt.py, added tests/semantic/test_wave64_emulate_barriers.py (unit + 64/128-thread workgroup interpreter regression tests), and documented the rules in docs/design.md.

Tested

CPU-only (Windows 10 x64, Python 3.12.6, synthetic assembly fixtures only; no GPU, no spirv-val, no llvm-mc, and no game corpus run on this machine — binary SPIR-V assembly checked via tools/spirv_asm.py in test_wave64_emulate_barriers.py):

  • Before fix on origin/main (03b09a08 / d0835dd9): all 8 minimal wave64 reproducers (s_flbit_i32_b32, s_flbit_i32, mixed mask+data s_cmp_eq_u64/s_cmp_lg_u64, and ds_read_b32 + ds_nop, each in a 64-thread s_cbranch_execz fold candidate and a 128-thread s_cbranch_scc0 guard) crashed rdna2spirv.translate() with AssertionError.
  • After fix:
    • python -I tests/semantic/test_wave64_emulate_barriers.py -> 13/13 OK (64-thread & 128-thread strict uniform-barrier workgroup interpreter, forward & reverse invocation order, plus spirv_asm.assemble(..., target_env="vulkan1.2")).
    • python -I tests/semantic/test_wave64_emulate_opt.py -> 22/22 OK.
    • python -m pytest -q tests/semantic/test_wave64_emulate_barriers.py tests/semantic/test_wave64_emulate_opt.py tests/semantic/test_w64x_scalar_load_order.py tests/semantic/test_wave64_subgroup_ops.py tests/test_docs_repo_paths.py tests/test_docs_design_rows.py tests/test_docs_links.py tests/test_docs_table_rows.py tests/test_design_contract_rows.py tests/test_resource_hygiene.py tests/test_text_write_line_endings.py tests/test_semantic_translator_use.py -> 70 passed.
    • python -m py_compile and git diff --check clean; LF line endings verified.

AI-assisted (developed and verified with an AI coding assistant).

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.

@Kam471 Kam471 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.

Ran this at ae5fefb8 on Windows 11, RTX 5070 Ti.

  • tests/semantic/test_wave64_emulate_barriers.py -> 13 tests, OK; tests/semantic/test_wave64_emulate_opt.py -> 22 tests, OK; git diff --check clean.
  • GPU emulated regression: LEONIDA_W64_EMULATE=1 run_gpu_tests.py --filter cs_w64x with this translator -> 19/19 passed (cs_w64x_sbarrier, cs_w64x_mask_reuse(2), cs_w64x_wg128_branches, cs_w64x_partial_scmp, ...).

The three changes are consistent with the translator: s_flbit_i32(_b32) joins the s_bcnt/s_ff cross-lane mask readers in both _classify_block_body and _insn_may_barrier; s_cmp_eq/lg_u64 now rejects any operand whose register window contains a lane-mask word (_has_any_lane_mask_word), which the scalar pair compare otherwise reads as plain data; and ds_nop is skipped in Gen.x_memory_order like the other non-LDS ds_ ops, so it no longer forces an ordering split. The docs note that this is CPU-interpreter checked only. No blocking issue.

@tylerstraub

Copy link
Copy Markdown
Contributor

Replying to @kotekompotek.

Checked the Vulkan 1.2 validation gap at ae5fefb8 on Linux. I ran the new barrier regression suite with a wrapper around its translation helper. For every returned module, I assembled the emitted text with the PR’s assembler and passed the binary to spirv-val --target-env vulkan1.2 (SPIRV-Tools v2026.1).

All 21 modules validated, including the folded and multi-wave cases for the flbit lane-mask reads, mixed u64 comparisons, and LDS operations around ds_nop. All 13 original tests also passed. This was a CPU-side validation run.

AI-assisted (Codex).

AI-assisted.

@theswiss24

Copy link
Copy Markdown

Tested ae5fefb8 (contains main d0835dd9) on four GPUs, three machines, with test programs built from main d0835dd9 on each (this PR changes no C++): pass on all four. Every test has the same result as main on each device.

GPU driver, OS run_gpu_tests.py (LEONIDA_W64_EMULATE=1)
RTX 2060 SUPER (Turing) NVIDIA 580.178.04, Ubuntu 24.04.5 195/210 passed, 15 skipped, 0 failed
GTX 1650 Mobile (Turing) NVIDIA 595.99.02, Ubuntu 24.04.5 195/210 passed, 15 skipped, 0 failed
MX150 (Pascal) NVIDIA 580.178.04, Linux Mint 22.3 185/210 passed, 25 skipped, 0 failed (no mesh shaders or fragment barycentrics)
Intel UHD 630 (Gen9.5) Mesa ANV 25.2.8, Ubuntu 24.04.5 181/210 passed, 27 skipped, 2 failed, the same 2 as main (cs_fmt_int_narrow, my finding in #4571, and cs_signed_zero_nan, the known Mesa 25.2 NIR issue)

llvm-mc 20.1.2 on all. No game files.

Tested and written by Claude (Claude Code), posting from @theswiss24's account.

@justr4mzi

Copy link
Copy Markdown
Contributor

Verified PR #4596 on Windows 11 (25H2 26200.9974) / AMD Radeon RX 6700 XT (gfx1031, Adrenalin 26.11.2, Vulkan 1.4.315, Vulkan SDK 1.4.363.0 + SPIRV-Tools v2026.4, LLVM_MC) at head ae5fefb8:

  • AMD RX 6700 XT GPU runs (scripts/run_gpu_tests.py --filter cs_w64) under LEONIDA_VK_VALIDATION=1 (0 Vulkan validation errors, 21/21 translated and spirv-val clean):
    • Native wave64: 21/21 passed.
    • Emulated wave64 (LEONIDA_W64_EMULATE=1): 21/21 passed (cs_w64_mask_detect, cs_w64_mask_imm, cs_w64x_sbarrier, cs_w64x_partial_scmp, cs_w64x_wg128_branches, ...).
  • CPU / semantic / docs / hygiene checks:
    • python -m unittest tests/semantic/test_wave64_emulate_barriers.py tests/semantic/test_wave64_emulate_opt.py: 35 tests OK (7.119s).
    • tests/test_shader_checks.py: 75/75 OK (1.342s).
    • python -I -m unittest discover -s tests -p "test_docs*.py": 20/20 OK (18.489s).
    • python -I -m py_compile tools/rdna2spirv.py tools/wave64_emulate_opt.py, scripts/check_no_game_data.py, and git diff --check upstream/main...HEAD: clean (0).

Changes align _classify_block_body and _insn_may_barrier consistently for s_flbit_i32(_b32) and mixed-mask s_cmp_eq/lg_u64 operands (_has_any_lane_mask_word), while skipping ds_nop in Gen.x_memory_order.

AI-assisted (Gemini, Antigravity).

@Flyander Flyander 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.

The barrier prediction now tracks both s_flbit lane-mask reads and mixed u64 comparisons with mask words, matching the translator paths; ds_nop is consistently treated as a no-op for x_memory_order. The synthetic semantic reports cover the previously crashing folded regions, and I found no blocking issue.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested head ae5fefb8 — Windows, CPython 3.15.2, CPU only, synthetic fixtures, no game files: pass.

  • python -m unittest discover -s tests/semantic -p "test_wave64_emulate_barriers.py" -> 13/13 OK.
  • python -m unittest discover -s tests/semantic -p "test_wave64_emulate_opt.py" -> 22/22 OK.
  • Control (main's tools/ + the PR's test files): the new test_wave64_emulate_barriers.py is properly red — 14 failures across its 13 tests. The modified test_wave64_emulate_opt.py passes on main too (22/22), so its diff is test-side only; the barriers file is the one that discriminates the change.
  • git diff --check and scripts/check_no_game_data.py clean.

AI-assisted (Devin).

@KkelebekK

Copy link
Copy Markdown

Tested e38cb588 (merge-base = main 3faac557, git merge-tree clean) on AMD Radeon 780M (RADV PHOENIX, Mesa 26.2.6, gfx1103) and NVIDIA RTX 4070 Laptop (620.70.06), CachyOS Linux 7.3.4, Python 3.14.10, llvm-mc 23.1.4: pass, with one scope note: no GPU test exercises the changed paths, so the GPU runs are no-regression only and the evidence for the change itself is CPU-side.

This PR changes no C++, so the test programs were built from main af70fc78 (the only runtime/ delta to 3faac557 is the xgameruntime shims).

  • python -I -m unittest discover -s tests/semantic -p "test_wave64_emulate_barriers.py": 13 OK. test_wave64_emulate_opt.py: 22 OK. All tests/semantic/test_wave64*.py: 211 OK. tests/test_docs*.py: 30 OK.
  • Control, main's tools/ plus this PR's test files: 10 of the 13 barrier tests fail (13 failure records, all three groups: s_flbit, mixed s_cmp_*_u64, ds_nop). test_wave64_emulate_opt.py passes on main too (its change is interpreter-side: FindSMsb).
  • Independent operand check with llvm-mc -triple=amdgcn--amdpal -mcpu=gfx1036 -show-inst and tools/rdna2_isa_tables.py: s_flbit_i32_b32 (SOP1 0x15) and s_flbit_i32 (0x17) are (SDST, SSRC0), so a[1] is the source the PR tests for a lane mask; s_cmp_eq_u64 / s_cmp_lg_u64 (SOPC 0x12/0x13) are (SSRC0, SSRC1), both 2-dword pairs, which matches the x.get("n", 1) window in _has_any_lane_mask_word. The PR's tests assemble through the same translator they check, so this is the check that does not go through it.
  • Cross-check against the translator: s_flbit_i32_b32 and s_flbit_i32 take lane_mask_word on a mask source (rdna2spirv.py ~6300 and ~7061); s_cmp_eq/lg_u64 materialise each word through wave_mask unless both operands are full-wave masks (~7850). s_flbit_i32_b64/_i64 are not added here, and do not need to be: the translator raises Unsupported on a lane-mask source (~6504).
  • scripts/run_gpu_tests.py (full suite, 217 tests), per-test status identical to main 3faac557, 0 FAIL:
Configuration Result
780M, plain (native wave64) 202/217 passed, 15 skipped
780M, LEONIDA_W64_EMULATE=1 (extra) 202/217 passed, 15 skipped
RTX 4070, plain 170/217 passed, 47 skipped
RTX 4070, LEONIDA_W64_EMULATE=1 201/217 passed, 16 skipped
  • --filter cs_w64x: 19/19 on the 780M and 19/19 on the RTX 4070 with emulation.

Synthetic inputs only, no game data. AI-assisted.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested e38cb58 merged onto main 3faac55 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: 170/217 passed, 47 skipped, 0 failed. main 3faac55 gives 170/217, 47 skipped, 0 failed. All 221 test shaders translate and pass spirv-val.
  • tests/semantic/test_wave64_emulate_barriers.py: ran 13 tests in 0.990s; OK.
  • tests/semantic/test_wave64_emulate_opt.py: ran 22 tests in 5.494s; OK.

AI-assisted (Claude Code).

@memorypressure

Copy link
Copy Markdown

Verified merged with current main: both changed semantic suites pass in isolated and plain discovery (test_wave64_emulate_barriers 13, test_wave64_emulate_opt 22), the docs guards are green, and the five ballot-barrier emulate failures reproduce identically on fresh main — not yours. One checklist gap: the contract table row is updated for the changed translator behaviour but no standalone semantic .asm fixture accompanies it — please add one so the new rule is pinned in the standard form alongside the inline cases (which are substantive). Not stacked on #2956 or #4152 despite shared files. No CI checks reported. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@kacper-serewis

Copy link
Copy Markdown
Contributor

SHADER-876 full cached-corpus result at e38cb58 against 3faac55: all 26,503 indexed shaders translated and passed Vulkan 1.2 spirv-val on both revisions under identical --wave64-emulate settings. SHA-256 matched for every emitted module across 208 batches. CPU only, SPIRV-Tools v2026.4. This establishes byte-for-byte parity for the indexed corpus; GPU execution was outside this comparison.

@465d6a0eac

Copy link
Copy Markdown
Contributor

Game-copy check of e38cb588, --wave64-emulate, merged state compared with main 3faac557 (e38cb588 already contains it). Own 1.0.2.1 copy, all 26,503 shaders, WSL Ubuntu, Python 3.14.4, llvm-mc 21.1.8, SPIRV-Tools v2026.1, CPU only, counts only. No game shader changes.

3faac557 e38cb588
translated 26,503 26,503
pass spirv-val 26,503 26,503
.spv byte-identical 26,503 / 26,503

All 26,503 already translated at the base, so the three AssertionError paths the PR fixes (s_flbit on a lane mask, mixed s_cmp_*_u64, ds_nop) aren't reached by any game shader, and the changed barrier prediction alters no game module. Nothing to regress there. The head is 0718bfd3 now; I tested e38cb588.

AI-assisted (Claude Code).

@nicknickalive

Copy link
Copy Markdown

Tested 0718bfd (trial-merged onto main 561b65a) on NVIDIA GeForce RTX 5090 (610.88, subgroup 32..32) and AMD Radeon iGPU (RDNA2, AMD 32.0.21036.18, subgroup 32..64), Windows 11 Pro 26200: pass.

  • pytest test_wave64_emulate_barriers.py test_wave64_emulate_opt.py: 39 passed
  • tools-only; no GPU test changed.

No game files. AI-assisted.

@lunarminus

Copy link
Copy Markdown

Tested 0718bfd3 merged onto main d0835dd9 on an AMD Radeon 760M (RDNA3 iGPU, gfx1103, subgroup 32..64), RADV Mesa 26.3.1, CachyOS Linux: pass

The merge was a fast-forward: 0718bfd3 already contains main 561b65a8, so the tested tree is the head itself. Against 561b65a8 the PR changes 5 files: ds_nop in x_memory_order and mixed s_cmp_*_u64 in wave64_emulate_opt.py, plus tests and docs. The s_flbit part of the description is already on main as 6214c468.

  • Test programs built from this tree (runtime/, RelWithDebInfo).
  • tests/semantic/test_wave64_emulate_barriers.py: 13/13 OK. test_wave64_emulate_opt.py: 26/26 OK.
  • Your pytest list: 77 passed, 64 subtests passed. unittest discover -s tests/semantic -p 'test_wave64*.py': 244 OK.
  • Control, 561b65a8's tools/ with this PR's test files: 7 of the 13 barrier tests fail (the 3 ds_nop and the 4 mixed s_cmp_*_u64 tests). The other 6 pass, including all 5 s_flbit tests, which matches 6214c468 being on main already.
  • scripts/run_gpu_tests.py, all tests: 238/238 shaders translated and pass spirv-val.
Mode Result
plain (native wave64) 216/232 passed, 16 skipped, 0 failed
LEONIDA_W64_EMULATE=1 216/232 passed, 16 skipped, 0 failed
  • Per-test comparison, both modes:
    • Against main 561b65a8 without the PR (an earlier run on this machine): all 232 tests have the same status.
    • Against main d0835dd9 without the PR: all 210 of its tests have the same status. The other 22 tests (20 PASS; 2 SKIP, ps_live_width and ps_m2_literal) were added to main between d0835dd9 and 561b65a8. This PR adds no GPU fixture.
  • --filter cs_w64 with LEONIDA_VK_VALIDATION=1: 21/21 passed (19 of them cs_w64x) in both modes. The result is the same on 561b65a8 and on d0835dd9, and no log has a VUID line.
  • No GPU test exercises the changed paths, so the GPU runs only show that nothing regressed. The control above is the evidence for the change itself.

No game files. AI-assisted (Claude Code).

@kotekompotek
kotekompotek force-pushed the contribution/wave64-emulate-barrier-sync branch 3 times, most recently from ace237b to ffa6ab9 Compare November 23, 2026 16:12
…64, and ds_nop

Fixes three --wave64-emulate AssertionError crashes and sync mismatches:
- tools/wave64_emulate_opt.py (_classify_block_body, _insn_may_barrier): recognize s_flbit_i32_b32 and s_flbit_i32 on lane masks, and s_cmp_eq_u64/s_cmp_lg_u64 on mixed mask+data SGPR pairs via _has_any_lane_mask_word().
- tools/rdna2spirv.py (Gen.x_memory_order): treat ds_nop as a no-op matching NOOPS and wave64_emulate_opt._x_memory_order_access().
- tests/semantic/test_wave64_emulate_opt.py: add FindSMsb to run_emulated_workgroup OpExtInst handler.
- tests/semantic/test_wave64_emulate_barriers.py: add unit and 64/128-thread workgroup interpreter regression tests.
- docs/design.md: document the wave64-emulate barrier alignment rules and CPU verification scope.

Verified locally (CPU only, no GPU/spirv-val/llvm-mc):
- python -I tests/semantic/test_wave64_emulate_barriers.py -> 13/13 OK
- python -I tests/semantic/test_wave64_emulate_opt.py -> 22/22 OK
- pytest -q (12 test files) -> 70 passed

# Conflicts:
#	docs/design.md

# Conflicts:
#	docs/design.md
#	tools/wave64_emulate_opt.py

# Conflicts:
#	tools/wave64_emulate_opt.py
@cerroverb

Copy link
Copy Markdown

Second check on a Steam Deck OLED, AMD Custom GPU 0932 (RADV VANGOGH, subgroup 32..64), Mesa 26.2.6, Vulkan 1.4.354, Linux, head 80417b9.

  • With LEONIDA_W64_EMULATE=1, run_gpu_tests.py 223/239 passed, 16 skipped, 0 failed.
  • Native wave64, 245 shaders translated and all pass spirv-val. 222/239 passed, 16 skipped, 1 failed. The failure is ms_wave_info_s3 with (-1,-1,-1,-1) at (0,0), the known flake in ms_wave_info_s3 intermittently fails on RX 7900 XT / RADV NAVI31 (pass rate 100% -> 2.5%, same binaries) #6172. It passed 3 of 3 filtered reruns on this head and failed 8 of 30 filtered runs on main e0816d6 on the same machine, so it does not come from this PR.
  • ctest 63/63 passed (shader_library_corpus and vk_sync_validation skipped).
  • test_wave64_emulate_barriers.py and test_wave64_emulate_opt.py, 43 tests OK.

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.