shader: lower signed and clamped 32-bit add/sub in the direct compute path #3011

Merged
legacy-code-archaeologist merged 1 commit from upstream-pr/3011 into main 2026-11-27 16:08:23 +00:00
legacy-code-archaeologist commented 2026-11-27 05:03:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-27T05:03:43Z.


Summary

Lowers the VOP3-only v_add_nc_i32 and v_sub_nc_i32, and the clamp forms of v_add_nc_u32, v_sub_nc_u32 and v_subrev_nc_u32, in the direct C++ compute path (arithmetic()):

  • without clamp, the signed forms wrap modulo 2^32;
  • with clamp, _i32 saturates to the i32 range (toward the sign of S0) and _u32 saturates to 0 / 0xffffffff.

Overflow uses 32-bit sign tests, so no Int64Ops is needed. Neg/abs, omod, op_sel, SDWA and DPP forms are refused. LLVM 15.0.7 selects exactly these clamp forms for llvm.{s,u}{add,sub}.sat.i32 on gfx1013. tools/compute_store_aot.py lowers the unclamped signed forms the same way and refuses clamp; gfx2hlsl.py has none of them.

The block sits at the top of arithmetic(), away from the anchors of the open slices #2986, #2996, #2998, #3004 and #3007. git merge-tree against each branch and against cpp-migration is clean.

Fixes #3009, refs #2935

Tests

tests/native_add_sub_sat_check.py, tests/cmake/native_add_sub_sat.cmake, docs/experiments/native-direct-add-sub-sat.md. There are four cases at the i32/u32 edges with seven results each, checked in every lane of two groups. The words were assembled with LLVM 15.0.7.

  • ctest --preset dev --no-tests=error -R '^native_add_sub_sat_' -V:
    • controls passed. They check the module's add/sub/select. A neg modifier is refused. These references each differ from the expected words: unclamped, signedness-swapped, saturate-toward-S1 and unswapped-subrev.
    • hardware skipped (77) on NVIDIA Quadro M1000M (driver 32.0.15.7371, debug layer unavailable); the readback needs SM 6.6 wave32. The readback is not measured.
  • ctest --preset dev --no-tests=error -R '^native_' -j 3: 120 of 125 passed. native_graphics_wqm_hardware timed out under -j 3 load and passed alone (27.7 s). The 4 failing hardware tests (native_graphics_vertex_vcc_join, native_rtv_variant, native_startup_request_cache, startup_compute_dispatch_native) fail identically on the base e0d83759 on this machine.

Environment: Windows 10.0.26100, VS 2026 BuildTools 18, base cpp-migration e0d83759.

AI-assisted: written with Claude Code. I reviewed it and ran the commands above.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5521494234 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3011). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-27T05:03:43Z. --- ## Summary Lowers the VOP3-only `v_add_nc_i32` and `v_sub_nc_i32`, and the `clamp` forms of `v_add_nc_u32`, `v_sub_nc_u32` and `v_subrev_nc_u32`, in the direct C++ compute path (`arithmetic()`): - without clamp, the signed forms wrap modulo 2^32; - with clamp, `_i32` saturates to the i32 range (toward the sign of S0) and `_u32` saturates to 0 / 0xffffffff. Overflow uses 32-bit sign tests, so no Int64Ops is needed. Neg/abs, omod, op_sel, SDWA and DPP forms are refused. LLVM 15.0.7 selects exactly these clamp forms for `llvm.{s,u}{add,sub}.sat.i32` on gfx1013. `tools/compute_store_aot.py` lowers the unclamped signed forms the same way and refuses clamp; `gfx2hlsl.py` has none of them. The block sits at the top of `arithmetic()`, away from the anchors of the open slices #2986, #2996, #2998, #3004 and #3007. `git merge-tree` against each branch and against `cpp-migration` is clean. Fixes #3009, refs #2935 ## Tests `tests/native_add_sub_sat_check.py`, `tests/cmake/native_add_sub_sat.cmake`, `docs/experiments/native-direct-add-sub-sat.md`. There are four cases at the i32/u32 edges with seven results each, checked in every lane of two groups. The words were assembled with LLVM 15.0.7. - `ctest --preset dev --no-tests=error -R '^native_add_sub_sat_' -V`: - controls **passed**. They check the module's add/sub/select. A neg modifier is refused. These references each differ from the expected words: unclamped, signedness-swapped, saturate-toward-S1 and unswapped-subrev. - hardware **skipped (77)** on NVIDIA Quadro M1000M (driver 32.0.15.7371, debug layer unavailable); the readback needs SM 6.6 wave32. **The readback is not measured.** - `ctest --preset dev --no-tests=error -R '^native_' -j 3`: 120 of 125 passed. `native_graphics_wqm_hardware` timed out under -j 3 load and passed alone (27.7 s). The 4 failing hardware tests (`native_graphics_vertex_vcc_join`, `native_rtv_variant`, `native_startup_request_cache`, `startup_compute_dispatch_native`) fail identically on the base `e0d83759` on this machine. Environment: Windows 10.0.26100, VS 2026 BuildTools 18, base `cpp-migration` `e0d83759`. AI-assisted: written with Claude Code. I reviewed it and ran the commands above. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
KelboLive commented 2026-11-27 05:38:35 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-27T05:38:35Z.

Review: COMMENTED


What works

  1. Clean lowering of four additional VOP3 forms (v_add_nc_i32,
    v_sub_nc_i32, and the clamp forms of v_add_nc_u32, v_sub_nc_u32,
    v_subrev_nc_u32). The saturation logic correctly follows RDNA 2 ISA:
  • Signed overflow detected via XOR-sign tests, saturating toward S0's
    sign.
  • Unsigned add overflow via unsigned less-than of result vs S0.
  • Unsigned sub overflow via unsigned less-than of S1 vs S0.
  1. All operand modifiers (neg, abs, omod, op_sel, SDWA, DPP) are
    correctly refused. The if ((signed_add_sub && !i.wide) || i.subword || i.dpp_control || permuted || i.abs || i.neg || i.scale || (i.a & 0x7800)) check is comprehensive.
  2. LLVM 15 source: correctly maps to llvm.sadd.sat, llvm.ssub.sat,
    llvm.uadd.sat, llvm.usub.sat.

Issues

  1. The overflow detection for signed add and sub uses:
    add: XOR(a, result) AND XOR(b, result) -- this detects when
    the operand signs differ from the result sign.
    sub: XOR(a, b) AND XOR(a, result) -- this detects when the
    operands share a sign but the result differs.
    Both are correct 32-bit sign tests. Good.
  2. The magic literals 2147483648, 2147483647, 4294967295 and 0
    could benefit from named constants, but in a compact IR builder
    context they're acceptable.
  3. No test case for v_subrev_nc_u32's operand swap in the fixture.
    The controls check v_subrev_nc_u32 without clamp but the
    discussion suggests the clamp form is the main addition here.

Recommendations

  • Verify the compute_store_aot.py comment is up to date (it currently
    refuses clamp, which aligns with the PR's scope).
  • The magic i32 range values could be i32::max / i32::min or explicit
    consts for readability, though they're readable enough as-is.

Overall

Solid saturation math with correct overflow detection. The control
test correctly validates mutants and refuses operand modifiers.
Approvable.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6083930619 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3011#pullrequestreview-6083930619). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-27T05:38:35Z. **Review:** COMMENTED --- ## What works 1. Clean lowering of four additional VOP3 forms (v_add_nc_i32, v_sub_nc_i32, and the clamp forms of v_add_nc_u32, v_sub_nc_u32, v_subrev_nc_u32). The saturation logic correctly follows RDNA 2 ISA: - Signed overflow detected via XOR-sign tests, saturating toward S0's sign. - Unsigned add overflow via unsigned less-than of result vs S0. - Unsigned sub overflow via unsigned less-than of S1 vs S0. 2. All operand modifiers (neg, abs, omod, op_sel, SDWA, DPP) are correctly refused. The `if ((signed_add_sub && !i.wide) || i.subword || i.dpp_control || permuted || i.abs || i.neg || i.scale || (i.a & 0x7800))` check is comprehensive. 3. LLVM 15 source: correctly maps to llvm.sadd.sat, llvm.ssub.sat, llvm.uadd.sat, llvm.usub.sat. ## Issues 1. The overflow detection for signed add and sub uses: add: XOR(a, result) AND XOR(b, result) -- this detects when the operand signs differ from the result sign. sub: XOR(a, b) AND XOR(a, result) -- this detects when the operands share a sign but the result differs. Both are correct 32-bit sign tests. Good. 2. The magic literals `2147483648`, `2147483647`, `4294967295` and `0` could benefit from named constants, but in a compact IR builder context they're acceptable. 3. No test case for v_subrev_nc_u32's operand swap in the fixture. The controls check `v_subrev_nc_u32` without `clamp` but the discussion suggests the clamp form is the main addition here. ## Recommendations - Verify the compute_store_aot.py comment is up to date (it currently refuses clamp, which aligns with the PR's scope). - The magic i32 range values could be i32::max / i32::min or explicit consts for readability, though they're readable enough as-is. ## Overall Solid saturation math with correct overflow detection. The control test correctly validates mutants and refuses operand modifiers. Approvable.
legacy-code-archaeologist commented 2026-11-27 15:58:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-27T15:58:06Z.


@R0Y00 Moved #3011 (shader/direct-add-sub-i32) onto main (811c4ceb -> commit caef0c16), keeping tests and review history intact. No dependency on cpp-migration was exposed (arithmetic() only needed ordering after the new v_dot4_* handler).

Verified on caef0c16:

  • Built compile_dxil, native_store_replay, native_compute_ir_dump
  • native_add_sub_sat_controls: Passed (0.60s)
  • ^native_.*_(controls|ir)$: 37/37 passed (19.52s)
  • tests/docs_links_check.py: passed (92 files, 586 links)
  • native_add_sub_sat_hardware: skipped (77, Quadro M1000M SM 6.1 lacks SM 6.6 wave32; GPU readback unmeasured locally)

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7222697390 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3011#issuecomment-7222697390). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-27T15:58:06Z. --- @R0Y00 Moved #3011 (`shader/direct-add-sub-i32`) onto `main` (`811c4ceb` -> commit `caef0c16`), keeping tests and review history intact. No dependency on `cpp-migration` was exposed (`arithmetic()` only needed ordering after the new `v_dot4_*` handler). Verified on `caef0c16`: - Built `compile_dxil`, `native_store_replay`, `native_compute_ir_dump` - `native_add_sub_sat_controls`: Passed (`0.60s`) - `^native_.*_(controls|ir)$`: `37/37` passed (`19.52s`) - `tests/docs_links_check.py`: passed (`92` files, `586` links) - `native_add_sub_sat_hardware`: skipped (`77`, Quadro M1000M SM 6.1 lacks SM 6.6 wave32; GPU readback unmeasured locally) AI-assisted.
R0Y00 commented 2026-11-27 16:08:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-27T16:08:12Z.

Review: APPROVED


Independently reviewed caef0c16 integrated over main ca47eb1d (tree 5b0e68e1c88aa2bd16a336d9268f60281516d9ee). Checked wrap, signed overflow sign tests, unsigned carry and borrow, reverse subtraction operand order, and preservation of the existing non-clamped unsigned route.

Built compile_dxil native_store_replay native_compute_ir_dump with dev and release. Ran ctest --test-dir <build> --no-tests=error -j 1 -V -R '^native_(add_sub_sat|mad64|pk_add_sub_16|pk_mul_mad_16)_'. Debug and Release /O2 passed 8/8 each, zero skips. The hardware replay matched all 2,064 result and sentinel words across the seven add/sub forms. Invalid modifiers and reference mutations were detected. The adjacent integer MAD and packed arithmetic regressions passed. git diff --check HEAD^1 HEAD passed.

NVIDIA GeForce RTX 4060 Laptop GPU, driver 32.0.16.1656, Windows 26200.9974, SDK/DXC 10.0.26100.0, wave32, D3D12 debug layer disabled. GitHub Windows, Linux Python and gfx1013 checks succeeded. Hosted /O2 skipped; local /O2 passed.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6090785171 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3011#pullrequestreview-6090785171). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-27T16:08:12Z. **Review:** APPROVED --- Independently reviewed `caef0c16` integrated over `main` `ca47eb1d` (tree `5b0e68e1c88aa2bd16a336d9268f60281516d9ee`). Checked wrap, signed overflow sign tests, unsigned carry and borrow, reverse subtraction operand order, and preservation of the existing non-clamped unsigned route. Built `compile_dxil native_store_replay native_compute_ir_dump` with `dev` and `release`. Ran `ctest --test-dir <build> --no-tests=error -j 1 -V -R '^native_(add_sub_sat|mad64|pk_add_sub_16|pk_mul_mad_16)_'`. Debug and Release `/O2` passed 8/8 each, zero skips. The hardware replay matched all 2,064 result and sentinel words across the seven add/sub forms. Invalid modifiers and reference mutations were detected. The adjacent integer MAD and packed arithmetic regressions passed. `git diff --check HEAD^1 HEAD` passed. NVIDIA GeForce RTX 4060 Laptop GPU, driver `32.0.16.1656`, Windows `26200.9974`, SDK/DXC `10.0.26100.0`, wave32, D3D12 debug layer disabled. GitHub Windows, Linux Python and gfx1013 checks succeeded. Hosted `/O2` skipped; local `/O2` passed.
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!3011
No description provided.