Skip to content

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

Closed
tiguan26 wants to merge 1 commit into
ProjectLeonida:mainfrom
tiguan26:fix/sdwa-co-ci-select
Closed

tiguan26 wants to merge 1 commit into
ProjectLeonida:mainfrom
tiguan26:fix/sdwa-co-ci-select

Conversation

@tiguan26

Copy link
Copy Markdown
Contributor

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.

…rces (SHADER-SDWA-CO-CI-SELECT)

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 <noreply@anthropic.com>

@abnalstlll-byte abnalstlll-byte 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.

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.

@madhyn

madhyn commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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

@Deedlyy

Deedlyy commented Nov 24, 2026

Copy link
Copy Markdown

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

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 24, 2026
@tiguan26 tiguan26 closed this Nov 25, 2026
@tiguan26

Copy link
Copy Markdown
Contributor Author

Closing as superseded. The same fix landed on main in 073b8bb 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants