fix: VOP3 clamp on v_add/sub/subrev_nc_u32 and the carry-out add/sub saturates (was silently ignored) #235

Manually merged
github merged 1 commit from gh-pr/235/head into gh-pr/235/base 2026-11-26 05:39:09 +00:00
Owner

@⁠KainanP on GitHub · 2026-11-25 20:15 UTC · original
KainanP:fix/clamp-u32-carry → main · +272 −16 · 5 files · 1 commits · merged 2026-11-26 00:15 UTC

Change

VOP3 clamp on v_add_nc_u32, v_sub_nc_u32, v_subrev_nc_u32 and on the six carry-out ops (v_add_co_u32, v_sub_co_u32, v_subrev_co_u32, v_add_co_ci_u32, v_sub_co_ci_u32, v_subrev_co_ci_u32) was silently ignored on main, both by the reference interpreter (emu/rdna2_exec_vop3.py) and by the HLSL translator (emu/rdna2_hlsl.py, emu/rdna2_hlsl_wave64.py): a saturating add/sub wrapped instead, a silent wrong result (rule 3). LLVM selects uaddsat/usubsat to exactly these ops with clamp set, and llvm-mc 20 (gfx1030) encodes the bit on all nine.

Now, per AMD's RDNA 2 ISA, section 6.5 "ALU Clamp Bit Usage" (printed p. 48): for integer operations clamp "clamps the result to the largest and smallest representable value":

  • v_add_nc_u32 / v_sub_nc_u32 / v_subrev_nc_u32 with clamp: D saturates to [0, 0xFFFFFFFF] instead of wrapping.
  • The carry-out ops with clamp: D is 0xFFFFFFFF on an add carry and 0 on a sub borrow (carry-in included for the _co_ci_ forms), and SDST still gets the real carry/borrow bit (inactive lanes still 0, as before): the pseudo-code of sections 12.7 (p. 134) and 12.12 (p. 179) computes VCC from the unbounded sum, and section 6.5 clamps "the result". That last point is our reading of the ISA; we have not measured it.
  • The carry table, the saturated templates and the refusal now live in a new small module, emu/rdna2_hlsl_clamp.py (43 lines), shared by wave32 and wave64. rdna2_hlsl.py is 384 lines (389 on main), per rule 8.
  • HLSL picks the saturated value from the carry/borrow with integer compares (x + y < x ? 0xFFFFFFFFu : x + y, o ? 0x0u : r), never with clamp() on a widened sum: #232 found the RTX 4060 driver miscompiling clamp(Sext16(a) + Sext16(b), …) followed by a mask, so the generated body has no clamp( at all (a test checks it).
  • clamp on the other integer results is not implemented here, so it is now refused by name (Untranslated / Untranslatable, "… with clamp on an integer result: not implemented") in the interpreter and in HLSL (wave32 and wave64) instead of being ignored: v_mad_u32_u24, v_mad_i32_i24, v_mul_u32_u24, v_mul_i32_i24, v_mad_u64_u32/v_mad_i64_i32, v_cvt_u32_f32/v_cvt_i32_f32 (llvm-mc 20 encodes clamp on all of these), and any other integer-result VOP3 op with the bit set by hand. Measurement request for them: akimfomin/PortBridge#84 (section D).

No new encodings: coverage numbers and the README are unchanged (the suite has 1,441 tests on main e921268, still 1400+).

Linked issue: none (found while preparing #232). Overlap with #232: both PRs touch emu/rdna2_exec_vop3.py, emu/rdna2_hlsl.py and emu/rdna2_hlsl_wave64.py (different lines: #232 adds imports and a dispatch to its int16 modules, this PR changes the carry/_nc_u32 paths and adds the refusal); whichever merges second needs a small rebase, and #232's README count (1400+) wins.

Verification

Branch rebased on main e921268 (head 8d2508e). Tests were run on the same tree rebased on 9a06443; e921268 only adds launcher/docs changes.

python -m pytest                                 → 1423 passed, 17 skipped (RTX 4060 Laptop)
python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed!
ANYXBOXX_GPU=warp  python -m pytest tests/test_rdna2_int_clamp.py tests/test_rdna2_hlsl.py tests/test_rdna2_hlsl_wave64.py tests/test_rdna2_alu_parity.py → 48 passed
ANYXBOXX_GPU=Intel python -m pytest (same files) → 48 passed (Intel UHD 8086:a78b, wave16)

No C++ or Rust change.

Oracle and test data

  • New tests/test_rdna2_int_clamp.py (26 tests). The GPU tests run every one of the nine ops with clamp on 32 and 64 lanes (wave32 and wave64 translations) over edge pairs (0xFFFFFFFF + 1, 0 − 1, equal operands with and without carry-in, 0x80000000 + 0x80000000, …) each on a lane with carry-in 0 and one with carry-in 1, plus random pairs, storing D and every SDST word. They check (1) the interpreter's memory against an independent model written from the ISA rule and (2) the GPU memory against the interpreter, byte for byte, on RTX, WARP and Intel UHD. A sabotaged HLSL template (x + y without saturation) makes the parity check fail on RTX, so the GPU path is really exercised.
  • Failing without this change: 21 of the 26 tests fail on main (interpreter model, single-lane values, HLSL text, the refusals and both GPU tests); the 5 that pass on main are the unclamped wrap control and four single-lane cases where clamp does not change the result (no carry or borrow).
  • Sources of the rule: AMD RDNA 2 ISA (November 2020, SHA-256 22aec03f…4795, printed pages): section 6.5 p. 48 (integer clamp saturates to the representable range), sections 12.7 p. 134 and 12.12 p. 179 (pseudo-code of V_ADD_NC_U32, V_ADD_CO_CI_U32, V_ADD_CO_U32, V_SUB_CO_U32: VCC from the unbounded result). Cross-check with LLVM 20.1.8: test/CodeGen/AMDGPU/uaddsat.ll and usubsat.ll select uaddsat/usubsat i32 to v_add_nc_u32_e64 … clamp / v_sub_nc_u32_e64 … clamp on GFX10, and to the carry-out form v_add_u32_e64 v0, s[4:5], v0, v1 clamp / v_sub_u32_e64 v0, s[4:5], … clamp on GFX8. No LLVM test covers the _co_ci_ forms; for those we rely on section 6.5. Section E of akimfomin/PortBridge#84 asks for a hardware check of these forms.
  • Not implemented and therefore refused: clamp on the 24-bit multiplies, v_mad_u64_u32/v_mad_i64_i32 and the float-to-int conversions.

Checklist

  • The PR covers a single reviewable behaviour. (VOP3 clamp on 32-bit integer results.)
  • A test fails without this change and passes with it.
  • The commands and results of each check are given above.
  • An unimplemented function returns an error (E_NOTIMPL), never a fake success. (Unimplemented clamp forms raise Untranslated / Untranslatable naming the instruction.)
  • README: the same change in all 9 languages (test_readme_sync.py checks it). (No README change: count still 1300+, coverage unchanged.)
  • No game, console or SDK file, no key, no dump, no download link.
  • Findings drawn from a title: none.
  • Code taken from another project: none.

Made with AI assistance: Claude Code with Claude Opus 5.5, for KainanP.

🤖 Generated with Claude Code

**@⁠KainanP** on GitHub · 2026-11-25 20:15 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/235) `KainanP:fix/clamp-u32-carry` → `main` · +272 −16 · 5 files · 1 commits · merged 2026-11-26 00:15 UTC ## Change VOP3 `clamp` on `v_add_nc_u32`, `v_sub_nc_u32`, `v_subrev_nc_u32` and on the six carry-out ops (`v_add_co_u32`, `v_sub_co_u32`, `v_subrev_co_u32`, `v_add_co_ci_u32`, `v_sub_co_ci_u32`, `v_subrev_co_ci_u32`) was silently ignored on `main`, both by the reference interpreter (`emu/rdna2_exec_vop3.py`) and by the HLSL translator (`emu/rdna2_hlsl.py`, `emu/rdna2_hlsl_wave64.py`): a saturating add/sub wrapped instead, a silent wrong result (rule 3). LLVM selects `uaddsat`/`usubsat` to exactly these ops with `clamp` set, and llvm-mc 20 (gfx1030) encodes the bit on all nine. Now, per AMD's RDNA 2 ISA, section 6.5 "ALU Clamp Bit Usage" (printed p. 48): for integer operations clamp "clamps the result to the largest and smallest representable value": - `v_add_nc_u32` / `v_sub_nc_u32` / `v_subrev_nc_u32` with `clamp`: D saturates to [0, 0xFFFFFFFF] instead of wrapping. - The carry-out ops with `clamp`: D is 0xFFFFFFFF on an add carry and 0 on a sub borrow (carry-in included for the `_co_ci_` forms), and **SDST still gets the real carry/borrow bit** (inactive lanes still 0, as before): the pseudo-code of sections 12.7 (p. 134) and 12.12 (p. 179) computes VCC from the unbounded sum, and section 6.5 clamps "the result". That last point is our reading of the ISA; we have not measured it. - The carry table, the saturated templates and the refusal now live in a new small module, `emu/rdna2_hlsl_clamp.py` (43 lines), shared by wave32 and wave64. `rdna2_hlsl.py` is 384 lines (389 on main), per rule 8. - HLSL picks the saturated value from the carry/borrow with integer compares (`x + y < x ? 0xFFFFFFFFu : x + y`, `o ? 0x0u : r`), never with `clamp()` on a widened sum: #232 found the RTX 4060 driver miscompiling `clamp(Sext16(a) + Sext16(b), …)` followed by a mask, so the generated body has no `clamp(` at all (a test checks it). - `clamp` on the **other** integer results is not implemented here, so it is now refused by name (`Untranslated` / `Untranslatable`, "… with clamp on an integer result: not implemented") in the interpreter and in HLSL (wave32 and wave64) instead of being ignored: `v_mad_u32_u24`, `v_mad_i32_i24`, `v_mul_u32_u24`, `v_mul_i32_i24`, `v_mad_u64_u32`/`v_mad_i64_i32`, `v_cvt_u32_f32`/`v_cvt_i32_f32` (llvm-mc 20 encodes `clamp` on all of these), and any other integer-result VOP3 op with the bit set by hand. Measurement request for them: akimfomin/PortBridge#84 (section D). No new encodings: coverage numbers and the README are unchanged (the suite has 1,441 tests on main e921268, still `1400+`). Linked issue: none (found while preparing #232). **Overlap with #232:** both PRs touch `emu/rdna2_exec_vop3.py`, `emu/rdna2_hlsl.py` and `emu/rdna2_hlsl_wave64.py` (different lines: #232 adds imports and a dispatch to its int16 modules, this PR changes the carry/`_nc_u32` paths and adds the refusal); whichever merges second needs a small rebase, and #232's README count (`1400+`) wins. ## Verification Branch rebased on main `e921268` (head `8d2508e`). Tests were run on the same tree rebased on `9a06443`; `e921268` only adds launcher/docs changes. ``` python -m pytest → 1423 passed, 17 skipped (RTX 4060 Laptop) python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed! ANYXBOXX_GPU=warp python -m pytest tests/test_rdna2_int_clamp.py tests/test_rdna2_hlsl.py tests/test_rdna2_hlsl_wave64.py tests/test_rdna2_alu_parity.py → 48 passed ANYXBOXX_GPU=Intel python -m pytest (same files) → 48 passed (Intel UHD 8086:a78b, wave16) ``` No C++ or Rust change. ## Oracle and test data - New `tests/test_rdna2_int_clamp.py` (26 tests). The GPU tests run every one of the nine ops with `clamp` on 32 and 64 lanes (wave32 and wave64 translations) over edge pairs (0xFFFFFFFF + 1, 0 − 1, equal operands with and without carry-in, 0x80000000 + 0x80000000, …) each on a lane with carry-in 0 and one with carry-in 1, plus random pairs, storing D and every SDST word. They check (1) the interpreter's memory against an independent model written from the ISA rule and (2) the GPU memory against the interpreter, byte for byte, on RTX, WARP and Intel UHD. A sabotaged HLSL template (`x + y` without saturation) makes the parity check fail on RTX, so the GPU path is really exercised. - Failing without this change: 21 of the 26 tests fail on `main` (interpreter model, single-lane values, HLSL text, the refusals and both GPU tests); the 5 that pass on `main` are the unclamped wrap control and four single-lane cases where `clamp` does not change the result (no carry or borrow). - Sources of the rule: AMD RDNA 2 ISA (November 2020, SHA-256 `22aec03f…4795`, printed pages): section 6.5 p. 48 (integer clamp saturates to the representable range), sections 12.7 p. 134 and 12.12 p. 179 (pseudo-code of `V_ADD_NC_U32`, `V_ADD_CO_CI_U32`, `V_ADD_CO_U32`, `V_SUB_CO_U32`: VCC from the unbounded result). Cross-check with LLVM 20.1.8: `test/CodeGen/AMDGPU/uaddsat.ll` and `usubsat.ll` select `uaddsat`/`usubsat` i32 to `v_add_nc_u32_e64 … clamp` / `v_sub_nc_u32_e64 … clamp` on GFX10, and to the carry-out form `v_add_u32_e64 v0, s[4:5], v0, v1 clamp` / `v_sub_u32_e64 v0, s[4:5], … clamp` on GFX8. No LLVM test covers the `_co_ci_` forms; for those we rely on section 6.5. Section E of akimfomin/PortBridge#84 asks for a hardware check of these forms. - Not implemented and therefore refused: `clamp` on the 24-bit multiplies, `v_mad_u64_u32`/`v_mad_i64_i32` and the float-to-int conversions. ## Checklist - [x] The PR covers a single reviewable behaviour. (VOP3 `clamp` on 32-bit integer results.) - [x] A test fails without this change and passes with it. - [x] The commands and results of each check are given above. - [x] An unimplemented function returns an error (`E_NOTIMPL`), never a fake success. (Unimplemented `clamp` forms raise `Untranslated` / `Untranslatable` naming the instruction.) - [x] README: the same change in all 9 languages (`test_readme_sync.py` checks it). (No README change: count still `1300+`, coverage unchanged.) - [x] No game, console or SDK file, no key, no dump, no download link. - [x] Findings drawn from a title: none. - [x] Code taken from another project: none. Made with AI assistance: Claude Code with Claude Opus 5.5, for KainanP. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
VOP3 clamp on v_add_nc_u32, v_sub_nc_u32, v_subrev_nc_u32 and on the six
carry-out ops (v_add_co_u32, v_sub_co_u32, v_subrev_co_u32 and their
_co_ci_ forms) was silently ignored by the interpreter and the HLSL
translator, so a saturating add/sub wrapped instead (rule 3).

AMD "RDNA 2" ISA section 6.5 "ALU Clamp Bit Usage" (printed p. 48): for
integer operations clamp "clamps the result to the largest and smallest
representable value". So the _nc_u32 ops saturate D to [0, 0xFFFFFFFF]
and the carry-out ops saturate D to 0xFFFFFFFF on a carry or 0 on a
borrow. SDST keeps the real carry/borrow bit, which the pseudo-code of
sections 12.7 and 12.12 computes from the unbounded sum (our reading,
not measured). Cross-check: LLVM 20.1.8 CodeGen tests uaddsat.ll and
usubsat.ll select uaddsat/usubsat to v_add/sub_nc_u32_e64 with clamp on
GFX10 and to the carry-out v_add/sub_u32_e64 with clamp on GFX8.

HLSL picks the saturated value from the carry/borrow with integer
compares (no clamp() on a widened sum). The carry table, the saturated
templates and the refusal live in the new emu/rdna2_hlsl_clamp.py,
shared by wave32 and wave64, so rdna2_hlsl.py does not grow (rule 8).

Clamp on the other integer results (v_mad_u32_u24, v_mad_i32_i24,
v_mul_u32_u24, v_mad_u64_u32, v_cvt_u32_f32, ...) is not implemented:
it is now refused by name in both the interpreter and HLSL (wave32 and
wave64) instead of being ignored.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:39:08 +00:00
github manually merged commit 8d2508e6d4 into gh-pr/235/base 2026-11-26 05:39:09 +00:00
Author
Owner

@⁠LooperSalty on GitHub · 2026-11-25 21:02 UTC · original

Thanks for this, the clamp semantics and the tests look right (the new tests fail without the change).

The docstrings cite measurements from the Leonida project (ubiquitarianism/leonida, docs/design.md and docs/shader_validation.md at 9f9a7c5f). That repository was taken down on 2026-11-24 after a DMCA notice, and rule 2 of AGENTS.md only allows public sources (AMD's RDNA 2 ISA, LLVM's tests, Microsoft Learn, compatibly licensed code) and measurements from our own runs. Please remove these Leonida citations and, where a behaviour still needs a source, cite the AMD ISA section or an LLVM test instead (or re-measure it with our own tooling and say how it was obtained). Please also cite the AMD ISA section for the V_ADD_NC_U32 saturation.

Small one: rdna2_hlsl.py is now 407 lines (389 on main); please move the clamp helpers (SATURATED_U32_HLSL, carry_write) to a small module, per rule 8.

**@⁠LooperSalty** on GitHub · 2026-11-25 21:02 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/235#issuecomment-7174540299) Thanks for this, the clamp semantics and the tests look right (the new tests fail without the change). The docstrings cite measurements from the Leonida project (ubiquitarianism/leonida, docs/design.md and docs/shader_validation.md at 9f9a7c5f). That repository was taken down on 2026-11-24 after a DMCA notice, and rule 2 of AGENTS.md only allows public sources (AMD's RDNA 2 ISA, LLVM's tests, Microsoft Learn, compatibly licensed code) and measurements from our own runs. Please remove these Leonida citations and, where a behaviour still needs a source, cite the AMD ISA section or an LLVM test instead (or re-measure it with our own tooling and say how it was obtained). Please also cite the AMD ISA section for the V_ADD_NC_U32 saturation. Small one: `rdna2_hlsl.py` is now 407 lines (389 on main); please move the clamp helpers (`SATURATED_U32_HLSL`, `carry_write`) to a small module, per rule 8.
Author
Owner

@⁠LooperSalty on GitHub · 2026-11-25 21:45 UTC · original

Follow-up after the review: (1) rdna2_exec_vop3.py:153 still cites Leonida's docs/design.md "Integer clamp"; please remove it and cite the AMD ISA section for the saturation instead. (2) rdna2_hlsl.py is still 407 lines: please move the carry/clamp helpers (rdna2_hlsl.py:146-151) to a small module (rule 8). (3) Rebase on main.

**@⁠LooperSalty** on GitHub · 2026-11-25 21:45 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/235#issuecomment-7175503370) Follow-up after the review: (1) `rdna2_exec_vop3.py:153` still cites Leonida's docs/design.md "Integer clamp"; please remove it and cite the AMD ISA section for the saturation instead. (2) `rdna2_hlsl.py` is still 407 lines: please move the carry/clamp helpers (`rdna2_hlsl.py:146-151`) to a small module (rule 8). (3) Rebase on main.
Author
Owner

@⁠KainanP on GitHub · 2026-11-25 22:54 UTC · original

Thanks. Updated and rebased on main e921268 (one commit, 8d2508e):

  • No Leonida citation is left. The saturation now cites AMD's RDNA 2 ISA §6.5 "ALU Clamp Bit Usage" (printed p. 48): for integer operations clamp "clamps the result to the largest and smallest representable value". The carry-out ops still write the real carry/borrow bit to SDST because the pseudo-code in §12.7 (p. 134) and §12.12 (p. 179) computes VCC from the unbounded sum; that part is our reading of the ISA, not a measurement. Cross-check with LLVM 20.1.8: CodeGen/AMDGPU/uaddsat.ll and usubsat.ll select uaddsat/usubsat to v_add_nc_u32_e64 … clamp and v_sub_nc_u32_e64 … clamp on GFX10, and to the carry-out v_add_u32_e64 v0, s[4:5], … clamp on GFX8. Section E of akimfomin/PortBridge#84 asks for a hardware check.
  • The carry table, SATURATED_U32_HLSL, carry_write and the refusal moved to the new emu/rdna2_hlsl_clamp.py (43 lines), shared by wave32 and wave64. rdna2_hlsl.py is 384 lines (389 on main).
  • Clamp on the other integer results is still refused by name; the message now says "not implemented".
  • The commit message and PR description were rewritten without Leonida.

Checks: full suite on the RTX 4060 (1423 passed, 17 skipped); test_rdna2_int_clamp.py + HLSL parity files on WARP (48 passed) and Intel UHD (48 passed); ruff clean.

AI use: Claude Code (Claude Opus 5.5).

**@⁠KainanP** on GitHub · 2026-11-25 22:54 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/235#issuecomment-7176921720) Thanks. Updated and rebased on main `e921268` (one commit, `8d2508e`): - No Leonida citation is left. The saturation now cites AMD's RDNA 2 ISA §6.5 "ALU Clamp Bit Usage" (printed p. 48): for integer operations clamp "clamps the result to the largest and smallest representable value". The carry-out ops still write the real carry/borrow bit to SDST because the pseudo-code in §12.7 (p. 134) and §12.12 (p. 179) computes VCC from the unbounded sum; that part is our reading of the ISA, not a measurement. Cross-check with LLVM 20.1.8: `CodeGen/AMDGPU/uaddsat.ll` and `usubsat.ll` select `uaddsat`/`usubsat` to `v_add_nc_u32_e64 … clamp` and `v_sub_nc_u32_e64 … clamp` on GFX10, and to the carry-out `v_add_u32_e64 v0, s[4:5], … clamp` on GFX8. Section E of akimfomin/PortBridge#84 asks for a hardware check. - The carry table, `SATURATED_U32_HLSL`, `carry_write` and the refusal moved to the new `emu/rdna2_hlsl_clamp.py` (43 lines), shared by wave32 and wave64. `rdna2_hlsl.py` is 384 lines (389 on main). - Clamp on the other integer results is still refused by name; the message now says "not implemented". - The commit message and PR description were rewritten without Leonida. Checks: full suite on the RTX 4060 (1423 passed, 17 skipped); `test_rdna2_int_clamp.py` + HLSL parity files on WARP (48 passed) and Intel UHD (48 passed); ruff clean. _AI use: Claude Code (Claude Opus 5.5)._
This discussion has been locked. Commenting is limited to contributors.
No description provided.