rdna2spirv: decline SDWA-selected or sext() operands in Gen.imm_val (SHADER-SDWA-IMM-VAL) #1949

Open
github-bot wants to merge 1 commit from github-bot/gh-1949 into main AGit
Owner

@⁠ShotTechOp on GitHub · 2026-11-25 16:31 UTC · original
ShotTechOp:fix/sdwa-imm-val → main · +134 −5 · 3 files · 1 commits · open

Claimed as SHADER-SDWA-IMM-VAL in #1608.

What changed and why

Gen.imm_val() in tools/rdna2spirv.py returned the raw 32-bit integer value of an inline immediate or seeded SGPR constant without checking whether the operand carried an SDWA sub-dword select (sdwa_sel other than DWORD) or sext().

As a result, SHIFT_REV (v_lshlrev_b32_sdwa, v_lshrrev_b32_sdwa, v_ashrrev_i32_sdwa) constant-folded the unselected 32-bit shift amount instead of routing through src_u() -> sdwa_src() (tools/sdwa_diff.py shift-const-sel):

  • v_lshlrev_b32_sdwa v2, 2, 64 dst_sel:BYTE_0 dst_unused:UNUSED_PRESERVE src0_sel:BYTE_3 src1_sel:WORD_0: BYTE_3(2) = 0, so hardware computes 0x80000040, while main shifted by 2 and produced 0x80000000.
  • v_lshrrev_b32_sdwa v2, -16, v2 ... src0_sel:BYTE_1: BYTE_1(0xFFFFFFF0) = 0xFF (& 31 = 31), while main shifted by -16 & 31 = 16.
  • Seeded SGPR constants (s_mov_b32 s4, 0x05040300 + v_lshlrev_b32_sdwa v1, s4, v1 ... src0_sel:BYTE_1): BYTE_1 = 3, while main shifted by s4 & 31 = 0.

Fix:

  • tools/rdna2spirv.py:
    • Gen.imm_val(): return None when a.get("sext") or a.get("sdwa_sel", "DWORD") != "DWORD" (matching how it already declines abs / neg), so callers fall back to src_u() -> sdwa_src() while preserving constant folding for plain DWORD operands.
    • Remove the now-redundant per-op "sdwa_sel" in a[...] checks in v_bfi_b32 and v_bfm_b32.
  • docs/design.md: note the Gen.imm_val SDWA guard in the SDWA contract row.
  • tests/semantic/test_sdwa_shift_const_sel.py: add semantic execution + spirv_asm.assemble + sdwa_diff.reference cross-check tests for inline constants, negative inline constants, seeded SGPRs, BYTE_0..3/WORD_0..1/DWORD, and sext() across all three _sdwa shift ops.

(Note: tools/sdwa_diff.py is intentionally untouched to avoid conflicting with open PR #1653.)

Negative control (RED on origin/main b0f3d39 -> GREEN on this branch)

  • LEONIDA_TOOLS_DIR=<origin/main tools> python -I tests/semantic/test_sdwa_shift_const_sel.py: FAILED (5 failures across all 3 tests) — inline shifts returned [0x80000000, 0x8000, 0xFFFF8000, 0x80] instead of [0x80000040, 1, 0xFFFFFFFF, 0x80], SGPR shifts returned [1, 256, 0x80000000, 3] instead of [8, 16, 0xFC000000, 6], and all 3 sdwa_diff.reference cases mismatched.
  • python -I tools/sdwa_diff.py --known --ops v_lshlrev_b32,v_lshrrev_b32,v_ashrrev_i32 --programs 30 --seed 1 on origin/main: 28 pass, 2 mismatch (exit 1).
  • On this branch: 3/3 OK in test_sdwa_shift_const_sel.py, and 100 pass, 0 mismatch, 0 skipped in tools/sdwa_diff.py --known --ops v_lshlrev_b32,v_lshrrev_b32,v_ashrrev_i32 --programs 100 --seed 1.

Tests run (Windows 11, Python 3.12.10, CPU / standard library only; no GPU or game files)

  • python -I tests/semantic/test_sdwa_shift_const_sel.py -v -> 3/3 OK (also python -m unittest discover -s tests/semantic -p test_sdwa_shift_const_sel.py -> OK)
  • python -I tools/sdwa_diff.py --known --ops v_lshlrev_b32,v_lshrrev_b32,v_ashrrev_i32 --programs 100 --seed 1 -> 100 pass, 0 mismatch, 0 skipped
  • python -I tests/semantic/test_alu_sdwa_hw.py -> OK
  • python -I tests/semantic/test_sdwa_carry.py -> OK
  • python -I tests/test_sdwa_diff.py -> OK
  • All 12 tests/test_docs_*.py suites -> 12/12 OK
  • python -m compileall -q tools/rdna2spirv.py tests/semantic/test_sdwa_shift_const_sel.py -> OK
  • python -I scripts/check_no_game_data.py tools/rdna2spirv.py docs/design.md tests/semantic/test_sdwa_shift_const_sel.py -> OK
  • git diff --check -> clean

AI-assisted: yes.

**@⁠ShotTechOp** on GitHub · 2026-11-25 16:31 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1949) `ShotTechOp:fix/sdwa-imm-val` → `main` · +134 −5 · 3 files · 1 commits · open Claimed as `SHADER-SDWA-IMM-VAL` in #1608. ### What changed and why `Gen.imm_val()` in `tools/rdna2spirv.py` returned the raw 32-bit integer value of an inline immediate or seeded SGPR constant without checking whether the operand carried an SDWA sub-dword select (`sdwa_sel` other than `DWORD`) or `sext()`. As a result, `SHIFT_REV` (`v_lshlrev_b32_sdwa`, `v_lshrrev_b32_sdwa`, `v_ashrrev_i32_sdwa`) constant-folded the unselected 32-bit shift amount instead of routing through `src_u()` -> `sdwa_src()` (`tools/sdwa_diff.py` `shift-const-sel`): - `v_lshlrev_b32_sdwa v2, 2, 64 dst_sel:BYTE_0 dst_unused:UNUSED_PRESERVE src0_sel:BYTE_3 src1_sel:WORD_0`: `BYTE_3(2) = 0`, so hardware computes `0x80000040`, while `main` shifted by `2` and produced `0x80000000`. - `v_lshrrev_b32_sdwa v2, -16, v2 ... src0_sel:BYTE_1`: `BYTE_1(0xFFFFFFF0) = 0xFF` (`& 31 = 31`), while `main` shifted by `-16 & 31 = 16`. - Seeded SGPR constants (`s_mov_b32 s4, 0x05040300` + `v_lshlrev_b32_sdwa v1, s4, v1 ... src0_sel:BYTE_1`): `BYTE_1 = 3`, while `main` shifted by `s4 & 31 = 0`. **Fix:** - `tools/rdna2spirv.py`: - `Gen.imm_val()`: return `None` when `a.get("sext")` or `a.get("sdwa_sel", "DWORD") != "DWORD"` (matching how it already declines `abs` / `neg`), so callers fall back to `src_u()` -> `sdwa_src()` while preserving constant folding for plain `DWORD` operands. - Remove the now-redundant per-op `"sdwa_sel" in a[...]` checks in `v_bfi_b32` and `v_bfm_b32`. - `docs/design.md`: note the `Gen.imm_val` SDWA guard in the `SDWA` contract row. - `tests/semantic/test_sdwa_shift_const_sel.py`: add semantic execution + `spirv_asm.assemble` + `sdwa_diff.reference` cross-check tests for inline constants, negative inline constants, seeded SGPRs, `BYTE_0..3`/`WORD_0..1`/`DWORD`, and `sext()` across all three `_sdwa` shift ops. *(Note: `tools/sdwa_diff.py` is intentionally untouched to avoid conflicting with open PR #1653.)* ### Negative control (RED on `origin/main` `b0f3d39` -> GREEN on this branch) - `LEONIDA_TOOLS_DIR=<origin/main tools> python -I tests/semantic/test_sdwa_shift_const_sel.py`: **FAILED (5 failures across all 3 tests)** — inline shifts returned `[0x80000000, 0x8000, 0xFFFF8000, 0x80]` instead of `[0x80000040, 1, 0xFFFFFFFF, 0x80]`, SGPR shifts returned `[1, 256, 0x80000000, 3]` instead of `[8, 16, 0xFC000000, 6]`, and all 3 `sdwa_diff.reference` cases mismatched. - `python -I tools/sdwa_diff.py --known --ops v_lshlrev_b32,v_lshrrev_b32,v_ashrrev_i32 --programs 30 --seed 1` on `origin/main`: **28 pass, 2 mismatch** (exit 1). - On this branch: **3/3 OK** in `test_sdwa_shift_const_sel.py`, and **100 pass, 0 mismatch, 0 skipped** in `tools/sdwa_diff.py --known --ops v_lshlrev_b32,v_lshrrev_b32,v_ashrrev_i32 --programs 100 --seed 1`. ### Tests run (Windows 11, Python 3.12.10, CPU / standard library only; no GPU or game files) - `python -I tests/semantic/test_sdwa_shift_const_sel.py -v` -> 3/3 OK (also `python -m unittest discover -s tests/semantic -p test_sdwa_shift_const_sel.py` -> OK) - `python -I tools/sdwa_diff.py --known --ops v_lshlrev_b32,v_lshrrev_b32,v_ashrrev_i32 --programs 100 --seed 1` -> 100 pass, 0 mismatch, 0 skipped - `python -I tests/semantic/test_alu_sdwa_hw.py` -> OK - `python -I tests/semantic/test_sdwa_carry.py` -> OK - `python -I tests/test_sdwa_diff.py` -> OK - All 12 `tests/test_docs_*.py` suites -> 12/12 OK - `python -m compileall -q tools/rdna2spirv.py tests/semantic/test_sdwa_shift_const_sel.py` -> OK - `python -I scripts/check_no_game_data.py tools/rdna2spirv.py docs/design.md tests/semantic/test_sdwa_shift_const_sel.py` -> OK - `git diff --check` -> clean AI-assisted: yes.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:12:24 +00:00
Author
Owner

@⁠efeekrb on GitHub · 2026-11-25 17:45 UTC · original

Tested on an NVIDIA GeForce RTX 4060 Laptop GPU (NVIDIA 610.78), Windows 11 build 26300, with an Intel UHD iGPU also present (LEONIDA_VK_DEVICE=4060). This PR's diff applies cleanly to main at 24a29f8 (where the test programs were built) and to main at b9320a118 (git apply --check).

  • Unit tests with the PR applied: tests/semantic/test_sdwa_shift_const_sel.py ran 3 tests, OK.
  • run_gpu_tests.py, full suite, with the PR applied: 274/274 shaders translated and pass spirv-val. Native 209/264 passed, 54 skipped, 1 failed; LEONIDA_W64_EMULATE=1 243/264 passed, 20 skipped, 1 failed. The one failure is cs_imgatomic_ext (the one #1518 addresses), and the counts are identical to main at 24a29f8 without this PR.

So no change on this GPU in either mode. The suite shows no regression; it does not tell whether a fixture exercises the rule this PR changes. No game files; counts and pass/fail lines only. AI-assisted (Claude Code).

**@⁠efeekrb** on GitHub · 2026-11-25 17:45 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1949#issuecomment-7169951070) Tested on an NVIDIA GeForce RTX 4060 Laptop GPU (NVIDIA 610.78), Windows 11 build 26300, with an Intel UHD iGPU also present (`LEONIDA_VK_DEVICE=4060`). This PR's diff applies cleanly to `main` at `24a29f8` (where the test programs were built) and to `main` at `b9320a118` (`git apply --check`). - Unit tests with the PR applied: `tests/semantic/test_sdwa_shift_const_sel.py` ran 3 tests, OK. - `run_gpu_tests.py`, full suite, with the PR applied: 274/274 shaders translated and pass spirv-val. Native 209/264 passed, 54 skipped, 1 failed; `LEONIDA_W64_EMULATE=1` 243/264 passed, 20 skipped, 1 failed. The one failure is `cs_imgatomic_ext` (the one #1518 addresses), and the counts are identical to `main` at `24a29f8` without this PR. So no change on this GPU in either mode. The suite shows no regression; it does not tell whether a fixture exercises the rule this PR changes. No game files; counts and pass/fail lines only. AI-assisted (Claude Code).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1949/head:github-bot/gh-1949'
git switch 'github-bot/gh-1949'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1949'
git switch 'github-bot/gh-1949'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1949'
git switch 'github-bot/gh-1949'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1949'
git switch 'main'
git merge --squash 'github-bot/gh-1949'
git switch 'main'
git merge --ff-only 'github-bot/gh-1949'
git switch 'main'
git merge 'github-bot/gh-1949'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.