Skip to content

wave64: synchronize scalar flbit reads of lane masks - #4392

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
lumenpearson:dev/leonida-wave64-flbit-mask
Nov 23, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
lumenpearson:dev/leonida-wave64-flbit-mask

Conversation

@lumenpearson

@lumenpearson lumenpearson commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

AI-assisted (Codex). Claimed in the coordination issue.

What and why

s_flbit_i32_b32 and s_flbit_i32 reconstruct the whole wave64 mask when their source is a lane mask. The scheduler missed that exchange when deciding whether an EXECZ block could fold or a multi-wave block could run without synchronization. Share the mask-reader classification between both checks. Plain scalar sources can still fold.

Tested

  • Six negative controls on 03b09a08: four EXECZ cases and two SCC cases stop translation. All six translate after the fix, and four ordinary SGPR controls still pass.
  • 24 scheduler tests and 8 design/doc checks pass. All 10 resulting synthetic modules pass spirv-val --target-env vulkan1.2 with SPIRV-Tools v2026.4. The strict CPU interpreter confirms the exact output lanes and words for unsigned flbit.
  • git apply --check for wave64_emulate_opt: record destination read for s_bitset0/1_b32/b64 in _scalar_rw (SHADER-767) #4280 succeeds on this diff. Windows, CPU only, no game files.
  • The seven affected semantic modules pass 122/122 on 03b09a08 and 124/124 with this change. The full discovery ran 7,458 tests and reported 7 failures, 3 errors, 7 skips and 1 expected failure. Targeted baseline checks reproduced the structured-control, mutation and f64 comparison failures, plus the missing spirv-as error (tests: gate vcmp16 SPIR-V validation on tool availability #4305). All ten flbit modules were validated separately with the explicit v2026.4 tools.

Fixes #4394.

@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 6214c468 on Windows 11: python -I tests/semantic/test_wave64_emulate_opt.py -> 24 tests, OK; git diff --check clean.

Scalar flbit reads of lane masks are now synchronized in the wave64 emulation analysis, and the new cases plus the existing CFG suite pass. No blocking issue.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 6214c46 on Windows 11 with NVIDIA GeForce RTX 4090 (driver 32.0.16.2101) and AMD Radeon(TM) Graphics (driver 32.0.21043.5001): pass. This adds a GPU readback for the two scalar flbit instructions to the CPU review above.

I used a synthetic group of 128 threads with 32-lane subgroups. In each emulated wave, lane 37 sets VCC, and lane 0 reads VCC_HI. Bit 5 of VCC_HI is set, thus s_flbit_i32 and s_flbit_i32_b32 must return 26.
On the two GPUs, both instructions write 26 at output words 0 and 64. All other 126 words keep the fill value.
I assembled the modules with the repository assembler and ran vk_compute_test.exe <shader.spv> 1 dump with VKC_SUBGROUP_SIZE=32 and VKC_OUT_WORDS=128.

The merge-base translator cannot translate the same unsigned listing (x_split inside a folded lane-local region). The changed CPU suite gives 24 tests passed at this head, and six subtest failures with the merge base.

I found no defect. AI-assisted.

@Tjabooo

Tjabooo commented Nov 23, 2026

Copy link
Copy Markdown

Tested 6214c46 on AMD Radeon RX 9070 XT (RDNA4, RADV GFX1201), Mesa 26.2.6, Vulkan 1.4.354, Arch Linux: pass.

Suite with emulation: LEONIDA_W64_EMULATE=1 scripts/run_gpu_tests.py --build runtime/build/linux (Release build of this head): 196/210 passed, 0 failed, 14 skipped (all run by d3d12_*_test). All 26 cs_w64x_* / cs_wm_* / cs_wavemask / cs_wave64_* / ms_w64x_* tests pass with compute forced to 32-wide.

The new flbit case on a 32-wide subgroup: I translated test_mask_flbit_is_synchronized_in_a_branch_taken_by_only_one_wave for both ops (128 threads, 2 emulated waves, only wave 0 takes the SCC branch, lane 3 sets VCC). Then I ran it with VKC_SUBGROUP_SIZE=32 VKC_OUT_WORDS=128 vk_compute_test <spv> 1 dump (the runner reports compute subgroup size: 32):

op spirv-val (vulkan1.2) words 0-63 (wave 0) words 64-127 (wave 1)
s_flbit_i32_b32 ok all 28 all fill (0xeeeeeeee)
s_flbit_i32 ok all 28 all fill (0xeeeeeeee)

Lanes 32-63 also read 28, so the upper half sees the mask bit that lives in the lower 32-lane half. That is the exchange this PR adds.

Control: the same listing on the merge base 03b09a0 does not translate (wave64 emulation: unexpected OpControlBarrier in sync-free multi-wave block).

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

@HPHM277

HPHM277 commented Nov 23, 2026

Copy link
Copy Markdown

Tested 6214c468 on Windows 11 build 26100 using the PR’s ten synthetic compute cases on two GPUs:

  • AMD Radeon 780M Graphics (Windows driver 32.0.32029.2010), native wave64 with --require-wave64: 10/10 passed.
  • NVIDIA GeForce RTX 4060 Laptop GPU (driver 617.42), wave64 emulation on 32-wide subgroups: 10/10 passed.

All 20 generated modules passed spirv-val --target-env vulkan1.2 before dispatch. I compared the full output buffers against the scalar references and the expected set of writing lanes: all 2,048 words matched, including the sentinel values in lanes that should not write. This covered both signed and unsigned flbit, the EXECZ cases at 64 and 128 threads (mask and ordinary SGPR sources), and the SCC case where only one wave takes the branch.

@mrbean56

Copy link
Copy Markdown

Tested 6214c46850 on Linux (Debian 13), Python 3.13.5: pass.

  • pytest tests/semantic/test_wave64_emulate_opt.py -q: 24 passed, 20 subtests passed in 10.00s.
  • I checked the set's boundary: s_flbit_i32_b64 / s_flbit_i32_i64 are deliberately absent, and rdna2spirv raises Unsupported for a lane-mask source on those (tools/rdna2spirv.py, "Leading-bit count of a 64-bit value"), so they cannot fold silently. The two added 32-bit forms are the ones that read lane_mask_word.

No GPU, no game data. AI-assisted.

@tupacfan04

Copy link
Copy Markdown
Contributor

APPROVE (6214c468), mergeable, merge clean.

test_wave64_emulate_opt 24/24, 45 related wave64/SALU tests pass, docs checks pass. Negative control: base tools/wave64_emulate_opt.py fails both new tests ("x_split inside a folded lane-local region" / "unexpected OpControlBarrier in sync-free multi-wave block"). Scalar paths add no sync: EXECZ/plain s14 and SCC/plain s14 are byte-identical to base at 64 and 128 threads, as are the s_bcnt1/s_ff1 controls. The lane-mask paths now match the existing mask-reader sync exactly (64-thread EXECZ -> 3 barriers/3 OpAtomicOr; 128-thread SCC -> 3/4/1), and both forms pass spirv-as/spirv-val.

Minor: signed s_flbit_i32 is only checked for FindSMsb presence and never executed (tests/semantic/test_wave64_emulate_opt.py:1249); assert its output like the unsigned case.

AI-assisted review, no game files, no GPU.

@Etoile-Bleu

Copy link
Copy Markdown
Contributor

Tested 6214c4685098 merged into main 03b09a086592 on Windows 11 (10.0.26200), Python 3.13.13: pass.

  • On main with only the PR's new tests copied in, 6 subtests fail in tests/semantic/test_wave64_emulate_opt.py: 4 EXECZ cases (s_flbit_i32_b32 and s_flbit_i32 of vcc_lo, 64 and 128 threads) with wave64 emulation: x_split inside a folded lane-local region, and 2 SCC-branch cases with unexpected OpControlBarrier in a sync-free multi-wave block. The s14 data-source controls translate on main.
  • Merged tree: test_wave64_emulate_opt.py 24 OK, test_design_contract_rows.py 4 OK, test_docs_links.py 6 OK, test_ci_registration.py 8 OK. Full python -I -m unittest discover -s tests/semantic: 7,458 tests (7,456 on main), 7 failures + 2 errors + 1 skip, the identical set that main has in this environment (test_loop_ballot_barrier emulate cases, test_cmp_class_f64.test_rejected_forms x2, test_mutations v_mul_legacy_f32 anchor x2), so nothing new fails.
  • 15 extra translations of my own (EXECZ at 64 and 128 threads, SCC at 128; s_flbit_i32_b32 and s_flbit_i32; sources vcc_lo, vcc_hi, a data SGPR, and vcc_lo copied with s_mov_b32 s12, vcc_lo): main rejects the 11 mask-source ones, the merged tree translates all 15 and the modules pass spirv-val --target-env vulkan1.2 (SPIRV-Tools v2026.4). The s_flbit_i32_b32 outputs run exactly in the CPU interpreter (28 for vcc_lo, -1 for vcc_hi); s_flbit_i32 was only translated and validated, the interpreter has no FindSMsb.

No game files used. AI-assisted.

@plentilha

Copy link
Copy Markdown
Contributor

Tested 6214c468 on NVIDIA GeForce RTX 3070 (driver 621.24 / 32.0.16.2124, Windows 11, subgroup 32..32): pass

  • python -I tests/semantic/test_wave64_emulate_opt.py: 24/24 OK
  • LEONIDA_W64_EMULATE=1 LEONIDA_TEXOFF_FALLBACK=1 python -I scripts/run_gpu_tests.py --build runtime/build/vulkan --require-pass: 195/210 passed, 15 skipped, 0 failed (214/214 translated and spirv-val clean; remaining 15 skips are the 14 d3d12_*_test fixtures and ps_w64x_readlane).

No game data. AI-assisted.

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.

Wave64 emulation rejects scalar flbit reads of lane masks inside branches

10 participants