Skip to content

Translator: a constant lane mask gives lane L its bit L (mask_src immediates) - #1367

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
Vonsek:fix/mask-src-immediate
Nov 22, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
Vonsek:fix/mask-src-immediate

Conversation

@Vonsek

@Vonsek Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Requested by Lucas · project thread

Claimed in the coordination issue as: the immediate half of the mask_src finding (finding: 7077396617).

Related: #293 also edits the mask_src docstring and its SGPR line, a few lines above this change. Expect a small textual conflict; whichever lands second rebases.

What and why

Gen.mask_src turned an immediate lane-mask operand into bit 0 of the constant for every lane. With all 32 lanes on, s_and_saveexec_b32 s7, 0xff00 therefore left no lane on, and s_and_saveexec_b32 s7, 5 left all 32 on. A constant used as a lane mask has one bit per lane, so lane L now takes bit L:

  • 64-bit forms: s_*_saveexec_b64, s_andn2_wrexec_b64, s_mov_b64/s_not_b64 into EXEC or VCC, and s_and/or/xor/andn2/orn2_b64 and s_cselect_b64 on the lane-mask path. Lane L takes bit L of the 64-bit value. Inline constants are sign-extended and literals get a zero high word, the same way src64_data reads 64-bit constants.
  • b32 forms: s_*_saveexec_b32 and s_andn2_wrexec_b32. Lane L takes bit L mod 32 of the 32-bit value.
  • 0 and -1 still give no lane and every lane with no per-lane code, so the SPIR-V for those is unchanged.
  • s_bcnt0/1_i32_b64 of a constant now counts the constant's own 64 bits, as scalar data like s_ff1_i32_b64, instead of this lane's copy of the mask. s_bcnt0_i32_b64 s17, 7 gave 32 and now gives 61; salu_diff --inline64 found it.

Not changed:

A literal with bit 31 set in a 64-bit position (zero- vs sign-extension) is not measured, and the tests avoid it.

Tests:

  • New GPU tests runtime/test/shaders/cs_w32_mask_imm.asm and cs_w64_mask_imm.asm, with the expected words derived in their headers. They are registered in check.py, run_gpu_tests.py and regen_fixtures.py.
  • New class LaneMaskConstants in tests/semantic/test_value_semantics.py runs both shaders across every lane (32 and 64 lanes) and checks them against a plain-Python reference.
  • New contract row in docs/design.md.

Tested

NVIDIA GeForce RTX 5080 (driver 616.92, 32-wide subgroups) and AMD Radeon RDNA2 iGPU (Ryzen 9 9950X3D, driver 26.10.1, subgroups 32..64), Windows 11. The device was chosen with LEONIDA_VK_DEVICE and each tool's "device:" line was checked.

  • New tests, red then green: both fail on main on both GPUs, with the bit-0 words predicted in their headers, and pass with this change.
Check main this branch
run_gpu_tests.py, AMD 118/120 120/120
run_gpu_tests.py, RTX 5080 91/120 (28 wave64 skipped) 92/120 (28 skipped)
run_gpu_tests.py, RTX 5080 LEONIDA_W64_EMULATE=1 118/120 120/120
check.py, AMD 108/111 110/111
check.py, RTX 5080 emulated 109/111 111/111
unittest discover -s tests/semantic 2 failures (the new class) OK
pytest --ignore=tests/semantic 2966 passed 2966 passed
salu_diff 400 programs 400/400 400/400
salu_diff --inline64 400 programs 399/400 400/400
cf_diff 300 programs × wave32/64 600/600 600/600
crosslane_fuzz 200 programs, both GPUs identical totals identical totals

AI-assisted (Claude Code).

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.

🤖 Generated with Claude Code

Vonsek and others added 2 commits November 22, 2026 11:33
Gen.mask_src turned an immediate lane-mask operand into bit 0 of the
constant for every lane, so s_and_saveexec_b32 s7, 0xff00 with all lanes
on left none on and an inline 5 left all 32 on. Lane L now takes bit L:
of the 64-bit value for the 64-bit forms (inline constants sign-extended,
literals with a zero high word, as src64_data) and of the 32-bit value at
L mod 32 for s_*_saveexec_b32 and s_andn2_wrexec_b32. 0 and -1 stay
constants. s_bcnt0/1_i32_b64 of a constant now counts the constant's bits
(scalar data, like s_ff1_i32_b64) instead of this lane's mask copy.
SGPR operands are unchanged.

GPU tests cs_w32_mask_imm.asm and cs_w64_mask_imm.asm, words derived in
their headers, failed on main on an RTX 5080 and an RDNA2 iGPU and pass
now; LaneMaskConstants runs both across every lane in the semantic suite.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Conflicts with main's DPP and TEST-175 entries in check.py,
run_gpu_tests.py and regen_fixtures.py: main's lists with
cs_w32_mask_imm / cs_w64_mask_imm added back.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bobcatchris

Copy link
Copy Markdown
Contributor

Independent CPU-side verification of dc0cd5de on Windows 11, Python 3.12.10; no GPU, corpus or game data:

  • python -m pytest -q tests/semantic/test_value_semantics.py: 27 passed.
  • python -m compileall -q tools/rdna2spirv.py tests/semantic/test_value_semantics.py: clean.
  • Negative control with this PR's test_value_semantics.py and both new cs_w32_mask_imm.s / cs_w64_mask_imm.s fixtures copied into a detached main (f85582e2) worktree: 1 failed, 26 passed. LaneMaskConstants::test_words fails on main because an immediate mask does not give lane L its bit L, so the new semantic cases pin the change.

I did not run the new cs_w32_mask_imm.asm / cs_w64_mask_imm.asm GPU fixtures; the RTX 5080 and RDNA2 iGPU results in the PR remain the author's hardware evidence. From the CPU checks, this looks ready.

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

Verified locally (CPU): the full semantic suite on this branch → 1180 tests OK (1 skip), including LaneMaskConstants.test_words (the eight wave32 words), and tests/test_rdna2spirv_cli.py → 6 passed, 22 subtests. The two new .asm files assemble with llvm-mc gfx1013, translate, and their modules pass spirv-val.

Reading the change: the per-lane ShiftRightLogical on the lane (rather than OpBitFieldUExtract) is the right call — it dodges the Intel mod-16 bug this file already documents — and the 0 / -1 fast path keeps the common case free. Truncating a b32 op to 32 bits and a b64 op to 64 with inline constants sign-extended matches src64_data.

I could not add a GPU confirmation: I assembled and translated both shaders on the 5090, but my ad-hoc harness invocation is wrong (existing tests such as cs_exec_const and cs_dpp print the 0xEEEEEEEE fill instead of their words), so I am not claiming a GPU result. Your 5080 + RDNA2 numbers stand on their own.

One gap worth a line in the description, not a blocker: for a wave64 shader, s_and_saveexec_b32 only rewrites EXEC_LO, so lanes 32-63 keep their previous bits; the wide=False path gives every lane bit L mod 32, high half included. It is still strictly better than the bit-0 behaviour it replaces, and the new tests are wave32, but the combination is not modelled.

@freemanovec

Copy link
Copy Markdown
Contributor

Tested dc0cd5de on Intel(R) HD Graphics 530 (SKL GT2), Mesa 26.2.7-arch1.1 (ANV), Vulkan 1.4.354, subgroup 8..32, Arch Linux: pass. AI-only run.

  • python3 -I tests/test_shader_checks.py: 40/40 OK; python3 -I tests/test_run_gpu_tests.py: 27/27 OK.
  • scripts/run_gpu_tests.py --filter mask_imm (native, llvm-mc 23.1.5, SPIRV-Tools v2026.4):
    • cs_w32_mask_imm: PASS (2056 20 2 6152 2050 4112 258 32)
    • cs_w64_mask_imm: SKIP (no 64-wide compute subgroup on this device)
  • LEONIDA_W64_EMULATE=1 scripts/run_gpu_tests.py --filter mask_imm:
    • cs_w32_mask_imm: PASS (2056 20 2 6152 2050 4112 258 32)
    • cs_w64_mask_imm: PASS (2056 1084 828 2052 319 2104 516 2064)

No game files.

@kapitche

Copy link
Copy Markdown
Contributor

Tested on Windows 11 with two GPUs on one machine (runtime built from this PR's head, MSVC 14.51, llvm-mc 23.1.6): no regression. pytest tests/test_translate.py: 384 passed, the same with main's translator, since the new cases are in tests/semantic, which I didn't run here. --filter cs_ gives 41/69 passed with 28 skipped (the wave64 ones) on both the RTX 4080 SUPER and the Intel UHD 770.

@Rainmaker33

Copy link
Copy Markdown

Tested dc0cd5d on NVIDIA GeForce RTX 3090 (Ampere, subgroup 32..32), driver 610.88 (Vulkan 1.4.341), Windows 11 Pro 26300: pass, including the wave64 fixture under emulation.

The PR's diff applied cleanly to main f85582e. VS 2022, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8; harness programs from a CMake build of that main.

  • scripts/run_gpu_tests.py: 106/135 pass, 29 skipped. cs_w32_mask_imm passes (2056 20 2 6152 2050 4112 258 32). cs_w64_mask_imm skips natively (no 64-wide subgroups).
  • With LEONIDA_W64_EMULATE=1: 135/135 pass. cs_w64_mask_imm gives 2056 1084 828 2052 319 2104 516 2064.
  • tests/semantic: 1180 run, OK (1 skipped).

@bvtterbroot

Copy link
Copy Markdown

Tested dc0cd5d on AMD Radeon RX 7900 XTX (RDNA3, Navi 31), AMD driver 26.11.2, Vulkan 1.4.359, Windows 11: pass.

  • run_gpu_tests.py: 135/135, native wave64. cs_w32_mask_imm (2056 20 2 6152 2050 4112 258 32) and cs_w64_mask_imm (2056 1084 828 2052 319 2104 516 2064) pass.
  • python -I -m unittest discover -s tests/semantic -p test_value_semantics.py: 27 tests OK.
  • tests/semantic/regen_fixtures.py --check: clean.

Tools: llvm-mc 21.1.8 (WSL), Vulkan SDK 1.4.363.0, MSVC 14.44. main c2967a5 on the same machine: run_gpu_tests.py 135/135.

@ItsNotRexed
ItsNotRexed merged commit 53f9e87 into ItsNotRexed:main Nov 22, 2026
11 checks passed
@crastinator

Copy link
Copy Markdown
Contributor

Tested dc0cd5d merged into main c2967a5e (clean merge) on AMD Radeon RX 6700 XT (Navi 22, gfx1031, subgroup 32..64), RADV Mesa 26.3.1, CachyOS, Linux 7.3.5, GCC 16.2.1, llvm-mc 23.1.5: pass.

  • The new tests pass: cs_w32_mask_imm (2056 20 2 6152 2050 4112 258 32) and cs_w64_mask_imm natively 64-wide (2056 1084 828 2052 319 2104 516 2064).
  • Whole run_gpu_tests.py: 137/137 native, 137/137 with LEONIDA_W64_EMULATE=1.
  • tests/semantic/test_value_semantics.py: 27 passed.

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

@sarveshgamers

Copy link
Copy Markdown
Contributor

Tested dc0cd5de on dual-GPU setup: AMD Radeon 780M Graphics (driver 32.0.32015.7029, Vulkan 1.4.359, subgroup 32..64) and NVIDIA GeForce RTX 5060 Laptop GPU (driver 32.0.16.2101 / 572.16, subgroup 32..32), Windows 11. AI-assisted.

All five harnesses (vk_compute_test, vk_store_test, vk_harness, vk_texture_test, vk_bindless_test) built with MinGW GCC 16.2.0, llvm-mc 23.0.0git (amdgcn, gfx1013), SPIRV-Tools v2026.4.

AMD Radeon 780M (subgroups 32..64)

  • scripts/run_gpu_tests.py: 135/135 passed (0 failed, 0 skipped)
    • cs_w32_mask_imm: PASS (2056 20 2 6152 2050 4112 258 32)
    • cs_w64_mask_imm: PASS (2056 1084 828 2052 319 2104 516 2064)

NVIDIA GeForce RTX 5060 Laptop GPU (subgroup 32..32)

  • Native (run_gpu_tests.py): 106/135 passed, 29 skipped (wave64 requirements skipped cleanly)
    • cs_w32_mask_imm: PASS (2056 20 2 6152 2050 4112 258 32)
    • cs_w64_mask_imm: SKIP (no 64-wide compute subgroup on this device)
  • Emulated (LEONIDA_W64_EMULATE=1 scripts/run_gpu_tests.py): 135/135 passed (0 failed, 0 skipped)
    • cs_w32_mask_imm: PASS (2056 20 2 6152 2050 4112 258 32)
    • cs_w64_mask_imm: PASS (2056 1084 828 2052 319 2104 516 2064)

Unit & Semantic Verification

  • python -m unittest discover -s tests/semantic: 1180 passed, 1 skipped (test_f64_pack_is_rejected_not_guessed)
  • python -m unittest discover -s tests/semantic -p "test_value_semantics.py": 27 passed (including LaneMaskConstants.test_words and LaneMaskConstants.test_words_follow_from_the_masks)
  • python -I tests/test_shader_checks.py: 40/40 passed
  • python -I tests/test_run_gpu_tests.py: 27/27 passed

No game data or proprietary assets used.

intelshadow added a commit to intelshadow/Leonida that referenced this pull request Nov 22, 2026
…p to ItsNotRexed#1367)

s_ff0/1_i32_b64 of a constant read only the low word, so s_ff0_i32_b64 of
-1 gave 32 instead of -1. It now reads the constant with src64_data, as
s_bcnt does (inline constants sign-extended); the b32 forms are unchanged.

GPU test cs_imm_mask.asm (wave64, exact on any subgroup size; word 6 is
this case), run on the CPU by tests/semantic/test_imm_lane_masks.py on
64- and 32-wide subgroups. One sentence in the "Constant lane masks" row.

AI-assisted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
intelshadow added a commit to intelshadow/Leonida that referenced this pull request Nov 23, 2026
… (s_ff0_i32_b64 of -1 included)

A regression test for ItsNotRexed#1367's constant lane masks and for s_ff0/1_i32_b64
of a negative constant, which read only the low word until 2026-11-22
(s_ff0_i32_b64 of -1 gave 32). Both are fixed on main; nothing else tests
s_ff0 of a negative constant.

runtime/test/shaders/cs_imm_mask.asm: wave64, exact on any subgroup size,
expected 1 100 111 10 60 0 4294967295 64. Registered in check.py and
run_gpu_tests.py; tests/semantic/test_imm_lane_masks.py runs it on one
64-wide and two 32-wide subgroups.

AI-assisted.

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.

10 participants