shader: lower v_add_co_ci_u32, v_sub_co_ci_u32 and v_subrev_co_ci_u32 in pixel_aot #853

Merged
alvaroolt merged 1 commit from upstream-pr/859 into main 2026-11-23 14:33:08 +00:00
alvaroolt commented 2026-11-23 14:16:03 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by alvaroolt on 2026-11-23T14:16:03Z.


Change

tools/pixel_aot.py now lowers the carry-in add and subtracts v_add_co_ci_u32, v_sub_co_ci_u32 and v_subrev_co_ci_u32 in both encodings, VOP2 (_e32) and VOP3 (_e64): six gfx1013 forms that the form-coverage report listed as unobserved. They complete the carry chain that #779 started with v_add_co_u32/v_sub_co_u32/v_subrev_co_u32. In that chain, the carry-out written to a lane mask becomes the carry-in of the next 32-bit word, as in 64-bit address arithmetic.

  • The carry-in must be a per-lane condition the translator already tracks, written by a comparison or a carry-out. This is the same rule v_cndmask_b32 uses. The carry-out replaces the condition in its mask.
  • The add computes src0 + src1 + carry and carries when either 32-bit step wraps. v_sub_co_ci_u32 computes src0 - src1 - borrow and borrows when src1 + borrow > src0 (unsigned). subrev swaps the sources.
  • The semantics come from LLVM 15 (llvmorg-15.0.7). SelectAddcSubb selects V_ADDC_U32_e64 for ISD::ADDCARRY and V_SUBB_U32_e64 for ISD::SUBCARRY. V_SUBBREV_U32 is declared with v_subb_u32 as its reversed-operand counterpart. The semantics are not checked against the ISA document and not measured on a console.
  • _e32 reads and writes vcc/vcc_lo and takes a VGPR as src1. _e64 names both masks, which must have the same width.
  • Refused, with static tests:
    • the spelling without a suffix;
    • clamp and float modifiers;
    • exec or a VGPR as a mask;
    • an _e32 form with another mask or an SGPR src1;
    • masks of different widths;
    • an untracked or constant carry-in;
    • missing or extra operands;
    • the DPP and SDWA forms.
  • The scanner (tools/scan_unsupported_shaders.py) gains matching patterns.
  • One existing scanner refusal in tests/rdna2_tools_check.py, v_add_co_ci_u32_e64 v1, vcc, v2, v3, vcc, is now a supported line. It moved to the accepted list, and the refusal list now uses the same line with an extra operand, plus new DPP, SDWA, clamp and mask cases.

Overlap: the open PR #423 covers only the _dpp spellings of these instructions. This PR does not touch them; they stay refused here.

Verification

All results are for the working tree at commit b554187, rebased on 8307b9a.

Check Command Result
New draws ctest --preset dev --no-tests=error -R '^pixel_aot_carry_in_' -V pixel_aot_carry_in_hardware and pixel_aot_carry_in_warp passed
Second hardware vendor same command with XBOX2WINDOWS_ADAPTER=Radeon, -R '^pixel_aot_carry_in_hardware$' passed
Full suite, dev ctest --preset dev --no-tests=error -j 8 433 passed, 0 failed, 7 skipped of 440
Full suite, release ctest --preset release --no-tests=error -j 4 433 passed, 0 failed, 7 skipped of 440
LLVM 15.0.7 round trip python -B tests/pixel_aot_check.py binary <llvm-mc> <llvm-objcopy> <LLVM-C.dll> exit 0, covers the three new programs
Scanner python -B tests/rdna2_tools_check.py with LLVM 15 74 tests OK
Pure-Python checks the 24 checks of the platform-independent job all exit 0

The 7 skips are pre-existing capability skips: bvh_wave64_{hardware,warp}, wave_shuffle_warp, and spirv_minlod{,_levels}_{hardware,warp}.

Adapters:

  • NVIDIA GeForce RTX 5080, driver 32.0.16.2124;
  • AMD Radeon(TM) Graphics iGPU (1002:13c0), driver 32.0.11034.2;
  • WARP (Microsoft Basic Render Driver 10.0.26100.9818).

All on Windows 11 build 26300, with the debug layer unavailable (Graphics Tools not installed).

Test first: before the change, tests/form_coverage_check.py failed with Unsupported instruction 24: v_add_co_ci_u32_e32 v12, vcc_lo, s46, v19, vcc_lo, and the scanner test failed on v_add_co_ci_u32_e32 v1, vcc_lo, s2, v3, vcc_lo.

Evidence and follow-up

The fixture is synthetic: three programs in tests/pixel_aot_check.py (carry_in_group, draw-carry-in).

  • Program layout. Each program computes the carry-out form of the same operation on (A, B) into vcc_lo and on (A, D) into s60. It then computes op(C, D, vcc_lo) in _e32 and op(D, C, s60) in _e64, with the carry-out in s61. Two v_cndmask_b32_e64 selects export the carry-outs as 0.5 or 1.0.
  • Rows. Each operation runs on four rows:
    • both carry-ins set, with both carry-outs coming from the carry-in alone (C + D = 0xffffffff, or C = D);
    • both carry-ins clear, with a carry or borrow from the operands;
    • one row with only vcc_lo set, and one with only s60 set.
  • Offset. The result words have 2.0 (0x40000000) added before export. This is a bijection modulo 2^32, so no bit is lost. It keeps every exported word a normal binary32 or zero, including the all-ones result of a borrow taken from the borrow-in alone. The test asserts this for every input and output.
  • Negative controls. 27 wrong references must report pixel_mismatch. That is 39 draws from 3 compiles. The wrong references cover:
    • a dropped carry-in or borrow-in, and a carry-in read as set;
    • a carry-out or borrow-out without the carry-in term on each mask;
    • a dropped carry-out of the operands;
    • the carry-in of one encoding read from the other's mask;
    • subtract for add, add for subtract, and a borrow-in added;
    • reversed operands, and the borrow-out of the other operand order.
  • Mutation check. Removing the {r} < {p} term from the add's carry-out in tools/pixel_aot.py makes the first row report mismatch on WARP (Expected ['pass', ...], got ['mismatch', ...]).

Assumptions: the instruction semantics come from LLVM, as stated above. The draws show what the translated shader computes on desktop GPUs, not what the console computes. docs/experiments.md has a new paragraph with the reproduction command, next to the carry-out paragraph. That paragraph no longer lists the carry-in forms as refused.

This change was AI-assisted (Claude Code); I reviewed the diff and the test results.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6561278491 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/859). Originally posted by [alvaroolt](https://github.com/alvaroolt) on 2026-11-23T14:16:03Z. --- ## Change `tools/pixel_aot.py` now lowers the carry-in add and subtracts `v_add_co_ci_u32`, `v_sub_co_ci_u32` and `v_subrev_co_ci_u32` in both encodings, VOP2 (`_e32`) and VOP3 (`_e64`): six gfx1013 forms that the form-coverage report listed as unobserved. They complete the carry chain that #779 started with `v_add_co_u32`/`v_sub_co_u32`/`v_subrev_co_u32`. In that chain, the carry-out written to a lane mask becomes the carry-in of the next 32-bit word, as in 64-bit address arithmetic. - The carry-in must be a per-lane condition the translator already tracks, written by a comparison or a carry-out. This is the same rule `v_cndmask_b32` uses. The carry-out replaces the condition in its mask. - The add computes `src0 + src1 + carry` and carries when either 32-bit step wraps. `v_sub_co_ci_u32` computes `src0 - src1 - borrow` and borrows when `src1 + borrow > src0` (unsigned). `subrev` swaps the sources. - The semantics come from LLVM 15 (`llvmorg-15.0.7`). `SelectAddcSubb` selects `V_ADDC_U32_e64` for `ISD::ADDCARRY` and `V_SUBB_U32_e64` for `ISD::SUBCARRY`. `V_SUBBREV_U32` is declared with `v_subb_u32` as its reversed-operand counterpart. The semantics are not checked against the ISA document and not measured on a console. - `_e32` reads and writes `vcc`/`vcc_lo` and takes a VGPR as `src1`. `_e64` names both masks, which must have the same width. - Refused, with static tests: - the spelling without a suffix; - ` clamp` and float modifiers; - `exec` or a VGPR as a mask; - an `_e32` form with another mask or an SGPR `src1`; - masks of different widths; - an untracked or constant carry-in; - missing or extra operands; - the DPP and SDWA forms. - The scanner (`tools/scan_unsupported_shaders.py`) gains matching patterns. - One existing scanner refusal in `tests/rdna2_tools_check.py`, `v_add_co_ci_u32_e64 v1, vcc, v2, v3, vcc`, is now a supported line. It moved to the accepted list, and the refusal list now uses the same line with an extra operand, plus new DPP, SDWA, clamp and mask cases. Overlap: the open PR #423 covers only the `_dpp` spellings of these instructions. This PR does not touch them; they stay refused here. ## Verification All results are for the working tree at commit `b554187`, rebased on `8307b9a`. | Check | Command | Result | |---|---|---| | New draws | `ctest --preset dev --no-tests=error -R '^pixel_aot_carry_in_' -V` | `pixel_aot_carry_in_hardware` and `pixel_aot_carry_in_warp` passed | | Second hardware vendor | same command with `XBOX2WINDOWS_ADAPTER=Radeon`, `-R '^pixel_aot_carry_in_hardware$'` | passed | | Full suite, `dev` | `ctest --preset dev --no-tests=error -j 8` | 433 passed, 0 failed, 7 skipped of 440 | | Full suite, `release` | `ctest --preset release --no-tests=error -j 4` | 433 passed, 0 failed, 7 skipped of 440 | | LLVM 15.0.7 round trip | `python -B tests/pixel_aot_check.py binary <llvm-mc> <llvm-objcopy> <LLVM-C.dll>` | exit 0, covers the three new programs | | Scanner | `python -B tests/rdna2_tools_check.py` with LLVM 15 | 74 tests OK | | Pure-Python checks | the 24 checks of the platform-independent job | all exit 0 | The 7 skips are pre-existing capability skips: `bvh_wave64_{hardware,warp}`, `wave_shuffle_warp`, and `spirv_minlod{,_levels}_{hardware,warp}`. Adapters: - NVIDIA GeForce RTX 5080, driver 32.0.16.2124; - AMD Radeon(TM) Graphics iGPU (1002:13c0), driver 32.0.11034.2; - WARP (Microsoft Basic Render Driver 10.0.26100.9818). All on Windows 11 build 26300, with the debug layer unavailable (Graphics Tools not installed). Test first: before the change, `tests/form_coverage_check.py` failed with `Unsupported instruction 24: v_add_co_ci_u32_e32 v12, vcc_lo, s46, v19, vcc_lo`, and the scanner test failed on `v_add_co_ci_u32_e32 v1, vcc_lo, s2, v3, vcc_lo`. ## Evidence and follow-up The fixture is synthetic: three programs in `tests/pixel_aot_check.py` (`carry_in_group`, `draw-carry-in`). - **Program layout.** Each program computes the carry-out form of the same operation on (A, B) into `vcc_lo` and on (A, D) into `s60`. It then computes op(C, D, `vcc_lo`) in `_e32` and op(D, C, `s60`) in `_e64`, with the carry-out in `s61`. Two `v_cndmask_b32_e64` selects export the carry-outs as 0.5 or 1.0. - **Rows.** Each operation runs on four rows: - both carry-ins set, with both carry-outs coming from the carry-in alone (C + D = 0xffffffff, or C = D); - both carry-ins clear, with a carry or borrow from the operands; - one row with only `vcc_lo` set, and one with only `s60` set. - **Offset.** The result words have 2.0 (0x40000000) added before export. This is a bijection modulo 2^32, so no bit is lost. It keeps every exported word a normal binary32 or zero, including the all-ones result of a borrow taken from the borrow-in alone. The test asserts this for every input and output. - **Negative controls.** 27 wrong references must report `pixel_mismatch`. That is 39 draws from 3 compiles. The wrong references cover: - a dropped carry-in or borrow-in, and a carry-in read as set; - a carry-out or borrow-out without the carry-in term on each mask; - a dropped carry-out of the operands; - the carry-in of one encoding read from the other's mask; - subtract for add, add for subtract, and a borrow-in added; - reversed operands, and the borrow-out of the other operand order. - **Mutation check.** Removing the `{r} < {p}` term from the add's carry-out in `tools/pixel_aot.py` makes the first row report `mismatch` on WARP (`Expected ['pass', ...], got ['mismatch', ...]`). Assumptions: the instruction semantics come from LLVM, as stated above. The draws show what the translated shader computes on desktop GPUs, not what the console computes. `docs/experiments.md` has a new paragraph with the reproduction command, next to the carry-out paragraph. That paragraph no longer lists the carry-in forms as refused. This change was AI-assisted (Claude Code); I reviewed the diff and the test results. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets.
upstream-sync closed this pull request 2026-11-24 06:45:07 +00:00
R0Y00 commented 2026-11-24 06:49:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T14:32:35Z.


Independent review of b554187: draw-carry-in passed on RTX 4060 Laptop and WARP, including the wrong-reference cases. LLVM 15 reassembled the real encodings. The two-step carry and borrow tests agree with a wider integer calculation at wraparound, including a borrow from the incoming bit alone.

AMD RDNA 2 section 12.7 confirms the operand order and carry-in for these opcodes. Its reverse-subtract row reverses the result operands but repeats the forward borrow expression. The implementation follows LLVM 15's reverse-subtract pairing and takes the borrow from the actual subtrahend. Keep that distinction in any documentation and keep the console hardware result unclaimed.

https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7104156716 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/859#issuecomment-7104156716). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T14:32:35Z. --- Independent review of b554187: draw-carry-in passed on RTX 4060 Laptop and WARP, including the wrong-reference cases. LLVM 15 reassembled the real encodings. The two-step carry and borrow tests agree with a wider integer calculation at wraparound, including a borrow from the incoming bit alone. AMD RDNA 2 section 12.7 confirms the operand order and carry-in for these opcodes. Its reverse-subtract row reverses the result operands but repeats the forward borrow expression. The implementation follows LLVM 15's reverse-subtract pairing and takes the borrow from the actual subtrahend. Keep that distinction in any documentation and keep the console hardware result unclaimed. https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
R0Y00/Xbox2Windows!853
No description provided.