startup: lower v_{add,sub,subrev}_co_ci_u32 in gfx2hlsl #2420

Merged
stan495 merged 1 commit from upstream-pr/2428 into main 2026-11-25 18:24:02 +00:00
stan495 commented 2026-11-25 16:27:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by stan495 on 2026-11-25T16:27:46Z.


Summary

Lower RDNA 2 32-bit carry/borrow-in and out vector integer arithmetic ( _add_co_ci_u32, _sub_co_ci_u32, _subrev_co_ci_u32, both VOP2 _e32 and VOP3b _e64 spellings) in ools/startup/gfx2hlsl.py.

  • ** ools/startup/gfx2hlsl.py**:
    • Add carry_in_out_op and op_v_{add,sub,subrev}_co_ci_u32.
    • Accept constant carry-in ( -> u, -1 / xffffffff -> 1u) or a wave32 lane-mask carry-in ( cc / cc_lo / tracked mask SGPR via (%s & 1u)), refusing high-half masks ( cc_hi, exec_hi), non-mask SGPRs, modifiers, and exec* carry-out destinations.
    • Evaluate sum/difference and combined carry/borrow through scratch  and w before committing D and SDST (marking SGPR SDST as a non-uniform lane mask in self.masks), so D or SDST can safely overlap any source operand, and gate inactive lanes when exec_known is false.
  • ** ests/startup_gfx2hlsl_add_sub_co_ci_check.py**:
    • Pure-Python controls covering both _e32 (implicit cc_lo) and _e64 (SGPR lane mask with overlapping dst/sdst/src), constant /-1 carry-in, inactive-lane exec_known=False behavior, refusals (clamp, non-mask carry-in, cc_hi, EXEC carry-out, wrong operand count), and a negative control verifying carry-in 1 changes result and carry-out, checked across 768 ISA grid points.
  • CMakeLists.txt, .github/workflows/windows.yml, AGENTS.md: Register startup_gfx2hlsl_add_sub_co_ci_check.py.

Verification

  • Commit: bd6ddcfc (based on upstream/main 7092683a)
  • Commands & Results:
    • python -B tests/startup_gfx2hlsl_add_sub_co_ci_check.py -> PASS (checked 768 grid points, 0 failures)
    • python -B tests/startup_gfx2hlsl_add_sub_co_check.py -> PASS
    • python -B tests/startup_gfx2hlsl_handler_arity_check.py -> PASS (33 accepted instructions, 99 scalar refusals, 16 load refusals)
    • python -B tests/startup_gfx2hlsl_operand_count_check.py -> PASS (VEC_F 32 ops, VEC_U 41 ops, 0 failed)
    • python -B tests/docs_links_check.py -> PASS (19 Markdown files, 464 links)
    • python -B tests/bidi_controls_check.py -> PASS (1723 tracked files)
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6608905269 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2428). Originally posted by [stan495](https://github.com/stan495) on 2026-11-25T16:27:46Z. --- ## Summary Lower RDNA 2 32-bit carry/borrow-in and out vector integer arithmetic ( _add_co_ci_u32, _sub_co_ci_u32, _subrev_co_ci_u32, both VOP2 _e32 and VOP3b _e64 spellings) in ools/startup/gfx2hlsl.py. - ** ools/startup/gfx2hlsl.py**: - Add carry_in_out_op and op_v_{add,sub,subrev}_co_ci_u32. - Accept constant carry-in ( -> u, -1 / xffffffff -> 1u) or a wave32 lane-mask carry-in ( cc / cc_lo / tracked mask SGPR via (%s & 1u)), refusing high-half masks ( cc_hi, exec_hi), non-mask SGPRs, modifiers, and exec* carry-out destinations. - Evaluate sum/difference and combined carry/borrow through scratch  and w before committing D and SDST (marking SGPR SDST as a non-uniform lane mask in self.masks), so D or SDST can safely overlap any source operand, and gate inactive lanes when exec_known is false. - ** ests/startup_gfx2hlsl_add_sub_co_ci_check.py**: - Pure-Python controls covering both _e32 (implicit cc_lo) and _e64 (SGPR lane mask with overlapping dst/sdst/src), constant /-1 carry-in, inactive-lane exec_known=False behavior, refusals (clamp, non-mask carry-in, cc_hi, EXEC carry-out, wrong operand count), and a negative control verifying carry-in 1 changes result and carry-out, checked across 768 ISA grid points. - **CMakeLists.txt, .github/workflows/windows.yml, AGENTS.md**: Register startup_gfx2hlsl_add_sub_co_ci_check.py. ## Verification - **Commit**: `bd6ddcfc` (based on upstream/main 7092683a) - **Commands & Results**: - `python -B tests/startup_gfx2hlsl_add_sub_co_ci_check.py` -> PASS (checked 768 grid points, 0 failures) - `python -B tests/startup_gfx2hlsl_add_sub_co_check.py` -> PASS - `python -B tests/startup_gfx2hlsl_handler_arity_check.py` -> PASS (33 accepted instructions, 99 scalar refusals, 16 load refusals) - `python -B tests/startup_gfx2hlsl_operand_count_check.py` -> PASS (VEC_F 32 ops, VEC_U 41 ops, 0 failed) - `python -B tests/docs_links_check.py` -> PASS (19 Markdown files, 464 links) - `python -B tests/bidi_controls_check.py` -> PASS (1723 tracked files)
MasterDomino commented 2026-11-25 18:01:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-25T17:02:18Z.

Review: COMMENTED


Review of head bd6ddcfc, merged locally with current main e06aca8b. The merge is clean.

Result: no P1 or P2 findings. One observation and a nit.

What I checked

  • Semantics: the add path takes the carry from both steps (A + B, then + ci). The sub and subrev paths take the borrow from both B > A and ci > (A - B), which is A - B - ci < 0 without a 33-bit intermediate. Both match the RDNA 2 definitions. Everything goes through a and w before D and SDST are written, the same way as carry_out_op, so overlapping D, SDST or the carry-in with a source is safe.
  • Carry-in from exec_lo: it's accepted as a lane mask. The prologue always declares exec = 1, so the emitted (exec & 1u) compiles and reads 1 on active lanes, whether or not exec is known.
  • No cache revision needed: these ops had no handler before, so no stored translation can contain them.

Commands executed (Windows 11 build 26300)

python -B tests/startup_gfx2hlsl_add_sub_co_ci_check.py
python -B tests/startup_gfx2hlsl_{add_sub_co,handler_arity,operand_count,bit_alu,mask_overwrite}_check.py
Check Result
This PR's check pass, checked 768 grid points, 0 failures
Neighbouring checks: add/sub carry-out, handler arity, operand count, bit ALU, mask overwrite pass
Synthetic words vs llvm-mc 15.0.7 (-mcpu=gfx1013), all six _e32 and _e64 forms in the test identical words, e.g. v_add_co_ci_u32_e64 v4, s18, v4, v3, s18 gives d5281204 004a0704 both ways
llvm-mc-assembled DPP (quad_perm) and SDWA forms refused (v_add_co_ci_u32_dpp, SDWA form of v_add_co_ci_u32)
llvm-mc-assembled _e64 with exec_lo carry-in, and with an inline-constant S0 accepted, with the expected expressions

Mutations, each applied to carry_in_out_op and then reverted:

  1. No carry out of the + ci step: the test fails, first at A=0, B=0xffffffff, ci=1, and on the -1 constant carry-in case.
  2. No borrow out of the - ci step: the test fails at A=0, B=0, ci=1 for sub and subrev.
  3. No operand swap for subrev: the test fails at A=0, B=1, ci=0.

Observation, not blocking

The test is translator-level only. There's no GPU draw and no binary <llvm-mc> mode, which most gfx2hlsl lowering PRs carry. For pure 32-bit uint arithmetic the compiler risk is low, and the llvm-mc agreement above covers the encodings. A binary mode in the check would keep them pinned in CI's LLVM 15 job.

Nit

  • PR body escapes: the body has stray escape characters. \t became a tab before "ools/", \v dropped the v of v_add_co_ci_u32 and vcc, and \a turned the scratch register a into a control character. A literal 0u also seems to have lost its 0.

Title and body are neutral. The change is original in-repo work.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6061693216 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2428#pullrequestreview-6061693216). Originally posted by [MasterDomino](https://github.com/MasterDomino) on 2026-11-25T17:02:18Z. **Review:** COMMENTED --- Review of head `bd6ddcfc`, merged locally with current `main` `e06aca8b`. The merge is clean. **Result: no P1 or P2 findings. One observation and a nit.** ### What I checked - **Semantics:** the add path takes the carry from both steps (`A + B`, then `+ ci`). The sub and subrev paths take the borrow from both `B > A` and `ci > (A - B)`, which is `A - B - ci < 0` without a 33-bit intermediate. Both match the RDNA 2 definitions. Everything goes through `a` and `w` before `D` and `SDST` are written, the same way as `carry_out_op`, so overlapping `D`, `SDST` or the carry-in with a source is safe. - **Carry-in from `exec_lo`:** it's accepted as a lane mask. The prologue always declares `exec = 1`, so the emitted `(exec & 1u)` compiles and reads 1 on active lanes, whether or not exec is known. - **No cache revision needed:** these ops had no handler before, so no stored translation can contain them. ### Commands executed (Windows 11 build 26300) ```powershell python -B tests/startup_gfx2hlsl_add_sub_co_ci_check.py python -B tests/startup_gfx2hlsl_{add_sub_co,handler_arity,operand_count,bit_alu,mask_overwrite}_check.py ``` | Check | Result | |---|---| | This PR's check | pass, `checked 768 grid points, 0 failures` | | Neighbouring checks: add/sub carry-out, handler arity, operand count, bit ALU, mask overwrite | pass | | **Synthetic words vs llvm-mc 15.0.7** (`-mcpu=gfx1013`), all six `_e32` and `_e64` forms in the test | identical words, e.g. `v_add_co_ci_u32_e64 v4, s18, v4, v3, s18` gives `d5281204 004a0704` both ways | | llvm-mc-assembled DPP (`quad_perm`) and SDWA forms | refused (`v_add_co_ci_u32_dpp`, `SDWA form of v_add_co_ci_u32`) | | llvm-mc-assembled `_e64` with `exec_lo` carry-in, and with an inline-constant `S0` | accepted, with the expected expressions | **Mutations**, each applied to `carry_in_out_op` and then reverted: 1. **No carry out of the `+ ci` step:** the test fails, first at `A=0, B=0xffffffff, ci=1`, and on the `-1` constant carry-in case. 2. **No borrow out of the `- ci` step:** the test fails at `A=0, B=0, ci=1` for sub and subrev. 3. **No operand swap for subrev:** the test fails at `A=0, B=1, ci=0`. ### Observation, not blocking The test is translator-level only. There's no GPU draw and no `binary <llvm-mc>` mode, which most `gfx2hlsl` lowering PRs carry. For pure 32-bit `uint` arithmetic the compiler risk is low, and the llvm-mc agreement above covers the encodings. A `binary` mode in the check would keep them pinned in CI's LLVM 15 job. ### Nit - **PR body escapes:** the body has stray escape characters. `\t` became a tab before "ools/", `\v` dropped the `v` of `v_add_co_ci_u32` and `vcc`, and `\a` turned the scratch register `a` into a control character. A literal `0u` also seems to have lost its `0`. Title and body are neutral. The change is original in-repo work. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-25 19:03:40 +00:00
MasterDomino commented 2026-11-25 19:03:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-25T18:23:58Z.

Review: APPROVED


Approved at head bd6ddcfc. This is the same commit I reviewed above, with no P1 or P2 findings. Under the updated merge scope, translation PRs can now be merged after verification.

Re-verified against current main

The branch merges cleanly with main a6c8d80d; tools/startup/gfx2hlsl.py auto-merges.

python -B tests/startup_gfx2hlsl_{add_sub_co_ci,add_sub_co,handler_arity,operand_count,bit_alu,mask_overwrite}_check.py
  • This PR's check: checked 768 grid points, 0 failures.
  • The five neighbouring checks: all pass on the merged tree.

Negative control (from my review above)

Each mutant fails the new check:

  • dropping the carry out of the + ci step;
  • dropping the borrow out of the - ci step;
  • removing the subrev operand swap.

The six synthetic _e32 and _e64 words match llvm-mc 15.0.7.

Required CI passed at this head ("Build and test on Windows" and both Linux jobs). The /O2 and coverage skips come from explicit workflow conditions. There are no review threads.

The non-blocking observation still stands: the check is translator-level, with no draw and no binary mode. The PR-body escape nit is cosmetic.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6062829528 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2428#pullrequestreview-6062829528). Originally posted by [MasterDomino](https://github.com/MasterDomino) on 2026-11-25T18:23:58Z. **Review:** APPROVED --- Approved at head `bd6ddcfc`. This is the same commit I reviewed above, with no P1 or P2 findings. Under the updated merge scope, translation PRs can now be merged after verification. ### Re-verified against current `main` The branch merges cleanly with `main` `a6c8d80d`; `tools/startup/gfx2hlsl.py` auto-merges. ```powershell python -B tests/startup_gfx2hlsl_{add_sub_co_ci,add_sub_co,handler_arity,operand_count,bit_alu,mask_overwrite}_check.py ``` - **This PR's check:** `checked 768 grid points, 0 failures`. - **The five neighbouring checks:** all pass on the merged tree. ### Negative control (from my review above) Each mutant fails the new check: - dropping the carry out of the `+ ci` step; - dropping the borrow out of the `- ci` step; - removing the subrev operand swap. The six synthetic `_e32` and `_e64` words match llvm-mc 15.0.7. Required CI passed at this head ("Build and test on Windows" and both Linux jobs). The `/O2` and coverage skips come from explicit workflow conditions. There are no review threads. The non-blocking observation still stands: the check is translator-level, with no draw and no `binary` mode. The PR-body escape nit is cosmetic. AI-assisted; commands listed were executed locally.
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!2420
No description provided.