rdna2spirv: SDWA selects on the long-form carry ops land on their sources (SHADER-SDWA-CO-CI-SELECT) #1116

Closed
github-bot wants to merge 1 commit from gh-pr/1116/head into gh-pr/1116/base
Owner

@⁠tiguan26 on GitHub · 2026-11-24 18:44 UTC · original
tiguan26:fix/sdwa-co-ci-select → main · +111 −1 · 3 files · 1 commits · closed without merging

Claimed in the coordination issue as: SHADER-SDWA-CO-CI-SELECT

What and why

Gen.insn() attaches the SDWA src0_sel/src1_sel from args[1] on (first = 1). In the long form of v_add_co_ci_u32, v_sub_co_ci_u32 and v_subrev_co_ci_u32 (vD, carry out, src0, src1, carry in) args[1] is the carry out, so src0_sel landed on it, src1_sel narrowed src0 and src1 was read whole: a silent wrong result, not a refusal.

With v1 = v4 = 0x00050003, src0_sel:WORD_1 src1_sel:WORD_0 and no carry in, main gives:

op main expected
v_sub_co_ci_u32_sdwa 0xfffb0000, borrow 1 2, borrow 0
v_add_co_ci_u32_sdwa 0x50006, carry 0 8, carry 0
v_subrev_co_ci_u32_sdwa 0x50000, borrow 0 0xfffffffe, borrow 1

The fix starts the selects at src0 for these three ops when the long form is printed (SDWA_CARRY_OPS). The short form vD, src0, src1 already gave the expected words and is unchanged; so is every other op.

Impact: the SDWA row's hardware measurement over the 172 SDWA forms in the corpus lists no mismatch for these ops, so the current corpus most likely has no long-form carry op with a narrowing select. This is a correctness fix for the rule, not a known game bug. Found while reviewing #1093.

Tested

Linux, Python 3.13.18, on main 6293c1c:

  • New tests/semantic/test_sdwa_carry_select.py (4 tests): the reported words, both forms of all three ops with carry in 0 and 1 over byte/word/dword selects, and 24 seeded cases, against a plain-Python reference of the ISA semantics. On main the three long-form tests fail and the short-form control passes; with the fix all four pass.
  • python -I scripts/run_semantic_tests.py tests/semantic: 471 files, 6172 tests, 0 files failed.
  • tests/test_docs_design_rows.py, test_docs_table_rows.py, test_docs_links.py, test_text_write_line_endings.py, python -m compileall -q tools tests, scripts/check_no_game_data.py on the three files, git diff --check: clean.
  • The SDWA row in docs/design.md gains one sentence on the long-form carry ops and how it was checked.

GPUs tested: n/a (CPU interpreter only; not GPU-checked)

AI-assisted: yes (Claude)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • 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.
**@⁠tiguan26** on GitHub · 2026-11-24 18:44 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1116) `tiguan26:fix/sdwa-co-ci-select` → `main` · +111 −1 · 3 files · 1 commits · closed without merging Claimed in the coordination issue as: SHADER-SDWA-CO-CI-SELECT ### What and why `Gen.insn()` attaches the SDWA `src0_sel`/`src1_sel` from `args[1]` on (`first = 1`). In the long form of `v_add_co_ci_u32`, `v_sub_co_ci_u32` and `v_subrev_co_ci_u32` (`vD, carry out, src0, src1, carry in`) `args[1]` is the carry out, so `src0_sel` landed on it, `src1_sel` narrowed src0 and src1 was read whole: a silent wrong result, not a refusal. With v1 = v4 = `0x00050003`, `src0_sel:WORD_1 src1_sel:WORD_0` and no carry in, `main` gives: | op | main | expected | |---|---|---| | `v_sub_co_ci_u32_sdwa` | `0xfffb0000`, borrow 1 | `2`, borrow 0 | | `v_add_co_ci_u32_sdwa` | `0x50006`, carry 0 | `8`, carry 0 | | `v_subrev_co_ci_u32_sdwa` | `0x50000`, borrow 0 | `0xfffffffe`, borrow 1 | The fix starts the selects at src0 for these three ops when the long form is printed (`SDWA_CARRY_OPS`). The short form `vD, src0, src1` already gave the expected words and is unchanged; so is every other op. Impact: the SDWA row's hardware measurement over the 172 SDWA forms in the corpus lists no mismatch for these ops, so the current corpus most likely has no long-form carry op with a narrowing select. This is a correctness fix for the rule, not a known game bug. Found while reviewing #1093. ### Tested Linux, Python 3.13.18, on `main` 6293c1c: - New `tests/semantic/test_sdwa_carry_select.py` (4 tests): the reported words, both forms of all three ops with carry in 0 and 1 over byte/word/dword selects, and 24 seeded cases, against a plain-Python reference of the ISA semantics. On `main` the three long-form tests fail and the short-form control passes; with the fix all four pass. - `python -I scripts/run_semantic_tests.py tests/semantic`: 471 files, 6172 tests, 0 files failed. - `tests/test_docs_design_rows.py`, `test_docs_table_rows.py`, `test_docs_links.py`, `test_text_write_line_endings.py`, `python -m compileall -q tools tests`, `scripts/check_no_game_data.py` on the three files, `git diff --check`: clean. - The SDWA row in `docs/design.md` gains one sentence on the long-form carry ops and how it was checked. GPUs tested: n/a (CPU interpreter only; not GPU-checked) AI-assisted: yes (Claude) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
Gen.insn() attached src0_sel/src1_sel from args[1] on. In the long form
of v_add_co_ci_u32, v_sub_co_ci_u32 and v_subrev_co_ci_u32 (vD, carry
out, src0, src1, carry in) args[1] is the carry out, so src0_sel landed
on it, src1_sel narrowed src0 and src1 was read whole: a silent wrong
result. With v1 = v4 = 0x00050003, src0_sel:WORD_1 src1_sel:WORD_0 and
no carry in, v_sub_co_ci_u32_sdwa gave 0xfffb0000 with a borrow instead
of 2. The selects now start at src0 for these ops; the short form
(vD, src0, src1) was right and is unchanged.

tests/semantic/test_sdwa_carry_select.py checks both forms of all three
ops, carry in 0 and 1, byte/word/dword selects and seeded cases against
a plain-Python reference. The SDWA row in docs/design.md says so.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:39:00 +00:00
github-bot closed this pull request 2026-11-26 09:39:01 +00:00
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 18:48 UTC · original · approved these changes

Reviewed and verified. In ools/rdna2spirv.py, the long-form 5-argument layout of VOP2 carry instructions ( _add_co_ci_u32, _sub_co_ci_u32, _subrev_co_ci_u32) places carry_out at rgs[1]. Adjusting irst = 2 when len(args) == 5 correctly applies src0_sel to rgs[2] and src1_sel to rgs[3]. Verified against ests/semantic/test_sdwa_carry_select.py and semantic regression suite (0 failures). 100% clean-room compliant with zero game data.

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 18:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1116#pullrequestreview-6045319322) · **approved these changes** Reviewed and verified. In ools/rdna2spirv.py, the long-form 5-argument layout of VOP2 carry instructions ( _add_co_ci_u32, _sub_co_ci_u32, _subrev_co_ci_u32) places carry_out at rgs[1]. Adjusting irst = 2 when len(args) == 5 correctly applies src0_sel to rgs[2] and src1_sel to rgs[3]. Verified against ests/semantic/test_sdwa_carry_select.py and semantic regression suite (0 failures). 100% clean-room compliant with zero game data.
Author
Owner

@⁠madhyn on GitHub · 2026-11-24 19:30 UTC · original

Verified b4288ecad on Arch Linux (Python 3.14):

  • PYTHONPATH=tests/semantic python3 -m unittest discover -s tests/semantic -p "test_*sdwa*.py": 46 tests passed (0.728 s)

AI-assisted: yes

**@⁠madhyn** on GitHub · 2026-11-24 19:30 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1116#issuecomment-7142195648) Verified `b4288ecad` on Arch Linux (Python 3.14): - `PYTHONPATH=tests/semantic python3 -m unittest discover -s tests/semantic -p "test_*sdwa*.py"`: **46 tests passed** (0.728 s) AI-assisted: yes
Author
Owner

@⁠Deedlyy on GitHub · 2026-11-24 20:16 UTC · original

Recommendation: [MERGE]

Reasoning: Fixes a silent wrong result: in the long form of v_add/sub/subrev_co_ci_u32 SDWA selects landed on the carry-out operand instead of src0/src1. Small fix (+7) with a 103-line semantic test showing the before/after values. Verified by madhyn (46 SDWA tests pass). CI (label, change-scope, no-game-data and the Runtime jobs) is still queued awaiting maintainer workflow approval, so nothing has run in CI yet; GitHub reports no merge conflict with main. Merge once the queued checks are green.

Impact level: 4/10

**@⁠Deedlyy** on GitHub · 2026-11-24 20:16 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1116#issuecomment-7143283538) **Recommendation:** [MERGE] **Reasoning:** Fixes a silent wrong result: in the long form of `v_add/sub/subrev_co_ci_u32` SDWA selects landed on the carry-out operand instead of src0/src1. Small fix (+7) with a 103-line semantic test showing the before/after values. Verified by madhyn (46 SDWA tests pass). CI (label, change-scope, no-game-data and the Runtime jobs) is still queued awaiting maintainer workflow approval, so nothing has run in CI yet; GitHub reports no merge conflict with `main`. Merge once the queued checks are green. **Impact level:** 4/10
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-25 03:14 UTC · original

Closing as superseded. The same fix landed on main in 073b8bb3a by @⁠thesubtleties (SHADER-SDWA-CARRY-SEL): first = 2 for the long form of these three ops. Its tests/semantic/test_sdwa_carry.py covers more than this PR's tests do (wave32 and wave64, per-lane carry in, sext, dst_sel, the short form), so there is nothing left here to rebase. Thanks to @⁠madhyn and @⁠Deedlyy for checking it.

**@⁠tiguan26** on GitHub · 2026-11-25 03:14 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1116#issuecomment-7151726456) Closing as superseded. The same fix landed on main in 073b8bb3a by @⁠thesubtleties (SHADER-SDWA-CARRY-SEL): `first = 2` for the long form of these three ops. Its `tests/semantic/test_sdwa_carry.py` covers more than this PR's tests do (wave32 and wave64, per-lane carry in, `sext`, `dst_sel`, the short form), so there is nothing left here to rebase. Thanks to @⁠madhyn and @⁠Deedlyy for checking it.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.