Skip to content

rdna2spirv: mask carry/sign-out lane masks by EXEC - #1109

Closed
InduwaraSMPN wants to merge 0 commit into
ItsNotRexed:mainfrom
InduwaraSMPN:fix/carry-mask-exec
Closed

InduwaraSMPN wants to merge 0 commit into
ItsNotRexed:mainfrom
InduwaraSMPN:fix/carry-mask-exec

Conversation

@InduwaraSMPN

Copy link
Copy Markdown

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

What and why

Mask the scalar lane-mask destination of VALU carry/borrow and 65-bit MAD instructions by EXEC (self.active_b()) in tools/rdna2spirv.py, matching the AMD RDNA2 ISA (SDST[lane] = EXEC[lane] ? bit : 0) and existing v_cmp_* translation:

  • v_add_co_u32, v_add_co_ci_u32, v_sub_co_u32, v_sub_co_ci_u32, v_subrev_co_u32, v_subrev_co_ci_u32
  • v_mad_u64_u32, v_mad_i64_i32

Previously, both handlers selected 0xFFFFFFFF or 0 from the lane's computed carry/borrow/sign bit without ANDing with %active. Under partial EXEC, inactive lanes whose inputs overflowed/borrowed/were negative leaked 1 bits into VCC or SGPR lane masks, corrupting wave-wide consumers (s_bcnt1_i32_b32/b64, s_cbranch_vccz) and post-EXEC-restore v_cndmask_b32. Also skipped emitting the unused mask select when cd is null on *_co_* (matching v_mad_*64).

Audited all other mask_dst / OpStore %vcc sites in tools/rdna2spirv.py: all v_cmp_* sites already AND with self.active_b(), and v_div_scale_* is not present. Updated the contract table in docs/design.md (EXEC and v_mad_u64_u32, v_mad_i64_i32 rows) and added CPU-only wave32, wave64, and --wave64-emulate tests in tests/semantic/test_carry_exec_mask.py.

AI-assisted: written and verified with an AI coding assistant.

Tested

Environment: Windows 11 (Microsoft Windows NT 10.0.26300.0), Python 3.12.12, CPU-only (no GPU, no llvm-mc / spirv-as / spirv-val, no game files; not GPU-verified).

  • Negative control on unmodified 48ad23f:
    • python -I tests/semantic/test_carry_exec_mask.py -v -> Ran 9 tests in 4.840s, FAILED (failures=48) (all 48 EXEC-masking subtests failed across wave32, wave64, and --wave64-emulate structure; the 2 null sdst sanity tests passed).
  • After fix (e982504):
    • python -I tests/semantic/test_carry_exec_mask.py -v -> Ran 9 tests in 4.712s, OK (all 6 *_co_* ops, v_mad_u64_u32, v_mad_i64_i32, s_bcnt1, s_cbranch_vccz, v_cndmask_b32, null dst; wave32, wave64, and --wave64-emulate SPIR-V structure).
    • python -m compileall -q make_game.py tools tests -> exit 0.
    • python -I make_game.py --help -> exit 0.
    • python -I -m unittest discover -s tests/semantic -v -> Ran 1091 tests in 279.348s, OK.
    • python -I -m unittest discover -s tests -p "test_make_game_wave64.py" -v -> Ran 28 tests in 0.880s, OK.
    • uv run --with pytest pytest tests/test_lane_masks.py tests/test_partial_wave_subgroup.py -q -> 9 passed in 0.43s.
    • uv run --with pytest pytest tests/test_translate.py -q -> 293 skipped in 0.32s (requires llvm-mc with amdgcn and spirv-as/spirv-val on PATH).

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.

@LocranKing

Copy link
Copy Markdown
Contributor

Tested 6de000c on Windows 11 Build 26200 x64 (AMD Ryzen 5 5500, Python 3.12.10):

  • python -I tests/semantic/test_carry_exec_mask.py -v: Ran 9 tests, OK (confirmed negative control on ools/rdna2spirv.py from main: 48 failures across wave32, wave64, and --wave64-emulate structures).
  • git diff --check: clean.

No game files. AI-assisted.

@ddm-j

ddm-j commented Nov 22, 2026

Copy link
Copy Markdown

Tested 6de000c on Linux (Fedora 45), one machine with two GPUs: pass, and first GPU confirmation of the fix. AI-assisted.

  • AMD Radeon 610M (RDNA2 iGPU), RADV Mesa 26.2.6, subgroup 32..64
  • NVIDIA RTX 4070 Ti, driver 615.78.08, subgroup 32..32
  • llvm-mc 23.1.5; runtime built from this PR's head; GPU picked with LEONIDA_VK_DEVICE. "main" below = this PR's tree with tools/rdna2spirv.py from c63773d (this PR's base).

On the GPU. No existing GPU test covers carry-out under partial EXEC (only ps_ops2 uses v_add_co, at full EXEC), so I wrote a local synthetic probe (below). It enables 16 lanes, makes every lane overflow in v_add_co_u32, v_sub_co_u32 (0 − 1) and v_mad_u64_u32 (carry into bit 64), restores EXEC, and stores s_bcnt1 of each mask. The wave64 version runs it twice: lanes 0–15 active, then lanes 32–47 (upper half only).

this PR main
wave32, RADV (32 requested) and NVIDIA 16 16 16 32 32 32
wave64 native, RADV (--require-wave64) 16 16 16 16 16 16 64 64 64 64 64 64
wave64 --wave64-emulate, NVIDIA and RADV at 32 16 16 16 16 16 16 64 64 64 64 64 64

With this PR the counts are what RDNA2's SDST[lane] = EXEC[lane] ? bit : 0 gives. With main, every inactive lane leaks a bit, in all three modes, on both vendors, including when only the upper 32 lanes are active.

CPU tests: tests/semantic/test_carry_exec_mask.py OK (with main's translator: FAILED (failures=48), matching the description). tests/test_translate.py + tests/semantic: 1423 passed.

No regressions, this PR vs main (c63773d):

  • run_gpu_tests.py: RADV 110/110, NVIDIA 83/110 with 27 skipped (wave64). Same as main.
  • valu_fuzz --family f16/mem and lds_fuzz, seed 1: RADV native wave64 50/50 each; NVIDIA --emulate 100/100 each.
  • crosslane_fuzz (default, + --w64-native on RADV): 11–12 mismatches per mode, but the same programs fail with main's translator, and with --no-maskdata main has 0 mismatches in every mode. So they are the known masks-as-data case (Translator: a lane mask read as data gives the wave's bits (SHADER-21 part 1, #210) #293), not this PR.
Probe (wave32 version; synthetic, written for this test)
; group: 32,1,1
; mattr: +wavefrontsize32,-wavefrontsize64
v_mov_b32_e32 v1, -1
v_mov_b32_e32 v2, 1
v_mov_b32_e32 v6, -1
v_mov_b32_e32 v7, -1
v_mov_b32_e32 v8, 0
s_mov_b32 exec_lo, 0xffff
v_add_co_u32 v3, vcc_lo, v1, v2
s_mov_b32 s10, vcc_lo
v_sub_co_u32 v3, vcc_lo, v8, v2
s_mov_b32 s11, vcc_lo
v_mad_u64_u32 v[4:5], s12, v1, v1, v[6:7]
s_mov_b32 exec_lo, -1
s_bcnt1_i32_b32 s6, s10
s_bcnt1_i32_b32 s7, s11
s_bcnt1_i32_b32 s9, s12
v_cmpx_eq_u32_e32 0, v0
v_mov_b32_e32 v1, s6
v_mov_b32_e32 v2, 0
buffer_store_dword v1, v2, s[0:3], 0 offen
v_mov_b32_e32 v1, s7
v_mov_b32_e32 v2, 4
buffer_store_dword v1, v2, s[0:3], 0 offen
v_mov_b32_e32 v1, s9
v_mov_b32_e32 v2, 8
buffer_store_dword v1, v2, s[0:3], 0 offen
s_endpgm

build.py <out> <name>, then VKC_DUMP=out.bin vk_compute_test <name>.spv 1 dump; the first three words are the counts. Happy for it to become a GPU test in this PR or a follow-up.

@undeadhuman37

Copy link
Copy Markdown
Contributor

Tested 6de000c on NVIDIA GeForce RTX 4070 SUPER, driver 621.01, Windows 11 Home 25H2 (26200.9974), VS 2022 Build Tools, Vulkan SDK 1.4.363.1, llvm-mc 21.1.8: pass

  • tests/semantic/test_carry_exec_mask.py: 9 OK. unittest discover -s tests/semantic: 1110 OK.
  • Full run_gpu_tests.py: 112 translated, all pass spirv-val; 83/110 passed, 27 skipped (wave64-native, no 64-wide subgroup on NVIDIA).
  • LEONIDA_W64_EMULATE=1 run_gpu_tests.py --filter cs_: 54 translated, 53/53 passed.
  • Runtime build: 0 warnings; ctest 55/55 passed.

AI-assisted. No game files.

ItsNotRexed added a commit that referenced this pull request Nov 22, 2026
# Conflicts:
#	docs/design.md

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

Tested 6de000c on NVIDIA GeForce RTX 5070 (subgroup 32), driver 621.24, Windows 11 Pro (26300), Vulkan SDK 1.4.363.0, llvm-mc 21.1.8 (built with scripts/build_llvm_mc.ps1), scripts/run_gpu_tests.py:

  • native: 83/110 passed, 27 skipped (main 8df277f here: 64/91 passed, 27 skipped, all wave64 tests needing a 64-wide subgroup)
  • LEONIDA_W64_EMULATE=1: 110/110 passed (main here: 91/91)
  • python -m unittest discover -s tests/semantic: Ran 1110 tests in 203.073s OK

@BACONMANXDGOD

Copy link
Copy Markdown
Contributor

Tested 6de000c: no regression on both GPUs. Windows 11 26200, MSVC 19.44, llvm-mc 23.1.6, Vulkan SDK 1.4.363, runtime built from main c63773d; applied to c63773d (merges cleanly). GPUs: Intel Arrow Lake Xe-LPG iGPU (Windows 32.0.101.8132, subgroup 8..32) and RTX 5070 Ti Laptop (617.42), picked with VK_LOADER_DRIVERS_SELECT.

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

@InduwaraSMPN

Copy link
Copy Markdown
Author

Landed on main in 4da1807 (commit e982504); resetting the branch back to e982504 caused GitHub to auto-close the PR as empty.

@sizzlins

Copy link
Copy Markdown

Tested 6de000c on an RTX 4060 Laptop, NVIDIA 621.01, Windows 11 10.0.26300: pass.

  • tests/semantic/test_carry_exec_mask.py 9/9 OK (native and emulated)
  • run_gpu_tests.py 83/110 passed, 27 skipped (no 64-wide subgroup), no failures

AI-assisted (test run driven by a coding agent).

ItsNotRexed added a commit that referenced this pull request Nov 22, 2026
# Conflicts:
#	docs/design.md
ItsNotRexed added a commit that referenced this pull request Nov 22, 2026
test_carry_exec_mask (#1109) and test_wave64_vcc_half_write (#1125) stored through a V# with word3 = 0, which is
OOB_SELECT 0 (structured with offset) with stride 0: since #1104 every store is out of range, as on RDNA2, and the
tests read back nothing. They now use the raw-buffer V# (OOB_SELECT 3) the other semantic tests use.
test_shader_checks counts the third vk_store_test check (#1136) and its fake harness answers it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

7 participants