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

Open
opened 2026-11-26 21:19:32 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1949
Author: @ShotTechOp
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/sdwa-imm-val


📝 Commits (1)

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

📊 Changes

3 files changed (+134 additions, -5 deletions)

View changed files

📝 docs/design.md (+1 -1)
➕ tests/semantic/test_sdwa_shift_const_sel.py (+129 -0)
📝 tools/rdna2spirv.py (+4 -4)

📄 Description

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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1949 **Author:** [@ShotTechOp](https://github.com/ShotTechOp) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/sdwa-imm-val` --- ### 📝 Commits (1) - [`b73b4b2`](https://github.com/ubiquitarianism/leonida/commit/b73b4b2d97a092b9378b1defecc961d28349b14c) rdna2spirv: decline SDWA-selected or sext() operands in Gen.imm_val (SHADER-SDWA-IMM-VAL) ### 📊 Changes **3 files changed** (+134 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -1) ➕ `tests/semantic/test_sdwa_shift_const_sel.py` (+129 -0) 📝 `tools/rdna2spirv.py` (+4 -4) </details> ### 📄 Description 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1957
No description provided.