Skip to content

Translator: integer clamp on v_add/sub_nc_u32 (was ignored), and v_add_nc_i32 / v_sub_nc_i32 - #553

Merged
ItsNotRexed merged 3 commits into
ItsNotRexed:mainfrom
LouisRiverstone:valu-add-nc-clamp
Nov 22, 2026
Merged

ItsNotRexed merged 3 commits into
ItsNotRexed:mainfrom
LouisRiverstone:valu-add-nc-clamp

Conversation

@LouisRiverstone

Copy link
Copy Markdown
Contributor

This PR fixes two things in tools/rdna2spirv.py:

  1. The clamp bit on v_add_nc_u32, v_sub_nc_u32 and v_subrev_nc_u32 is silently ignored, so a saturating add comes out wrapped. On GFX9+ an integer VOP3 add or sub with clamp saturates. LLVM's AMDGPU backend selects uaddsat/usubsat to v_add_nc_u32/v_sub_nc_u32 with clamp set, so a compiled saturate(a + b) on uints is exactly this encoding. Today the translator emits a plain OpIAdd, with no warning.
  2. v_add_nc_i32 and v_sub_nc_i32 raise Unsupported. These are the targets for saddsat/ssubsat.

I don't know whether the corpus has clamped _nc_u32 adds. I can't check that without game files. Claimed in #374.

Change

  • tools/rdna2spirv.py: a branch for v_add_nc_i32/v_sub_nc_i32, and for the three unsigned ops when clamp is set. Without clamp, the unsigned ops still go through their existing code.
    • Unsigned: OpIAddCarry / OpISubBorrow. A carry gives 0xFFFFFFFF, and a borrow gives 0.
    • Signed: the wrapped result, or, on signed overflow, 0x80000000 / 0x7FFFFFFF depending on S0's sign. Overflow uses the usual sign-bit tests: (S1^D)&(S0^D) for add, (S0^S1)&(S0^D) for sub.
  • tests/semantic/test_alu_add_nc_clamp.py (new, stdlib unittest) checks all five ops, with and without clamp, against a plain-Python reference:
  • docs/design.md: an "Integer clamp" contract row.

Tested

  • python -I -m unittest discover -s tests/semantic: all pass.
  • Without the translator change, the new file gives 276 errors (Unsupported on the signed ops) and 88 failures. The failures are the unsigned ops with clamp returning the wrapped value.
  • python -m compileall -q tools tests passes. Not run on a GPU, and no game files used.

The saturation rule comes from the ISA's clamp definition and LLVM's instruction selection, and hasn't been measured on hardware. The contract row says so.

AI-assisted (Claude Code): it wrote the change and the tests, and ran the checks listed above.

… adds

v_add_nc_i32 and v_sub_nc_i32 raised Unsupported. The VOP3 clamp bit on
v_add_nc_u32 / v_sub_nc_u32 / v_subrev_nc_u32 was silently ignored, so a
saturating add came out wrapped. On GFX9+ an integer add/sub with clamp
saturates (LLVM selects uaddsat/usubsat and saddsat/ssubsat to these ops):
unsigned to [0, 0xFFFFFFFF], signed to [INT_MIN, INT_MAX].

tests/semantic/test_alu_add_nc_clamp.py checks all five ops with and without
clamp against a plain-Python reference; docs/design.md gets a contract row.
LouisRiverstone and others added 2 commits November 21, 2026 22:02
cs_wqm_w32 (SHADER-34) and cs_w32_lane_wrap (SHADER-52) check 32-lane
EXEC/VCC masks and lane selects. On Mesa llvmpipe, whose compute
subgroups are 8 wide, they can't produce their words, and the
"GPU tests on llvmpipe" step in runtime.yml has failed on main since
91b36cf. Skip them where vk_compute_test reports a widest subgroup
below 32 lanes, the rule ps_bitmask_perm already uses, in check.py and
run_gpu_tests.py (which reuses check.py's helper). Devices with 32 or
more lanes still run and judge them.

AI-assisted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01D7JEiKsTUaHQbCTdc4Eghy
(cherry picked from commit be46677)
@LouisRiverstone

Copy link
Copy Markdown
Contributor Author

CI note: main is red on linux-gpu-tools (cs_w32_lane_wrap / cs_wqm_w32 need 32 lanes, llvmpipe has 8), which made this PR red for a reason that is not its own. To get a green run I merged current main and cherry-picked @NRXM's fix from #555 (be46677, authorship kept; the two conflicts with main resolved by keeping both sides: cs_movrelsd_2 stays in WAVE32_DUMPS, and both new test classes stay in tests/test_run_gpu_tests.py). It is the same change as #555, so whichever lands first, the other merges cleanly or drops it. tests/test_run_gpu_tests.py, tests/test_shader_checks.py and tests/semantic pass.

@IAmNotSiob IAmNotSiob 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.

Went through the saturation logic. The unsigned forms use the carry/borrow of OpIAddCarry/OpISubBorrow, giving 0xFFFFFFFF or 0. The signed overflow tests are the standard ones: add (y^w)&(x^w), sub (x^y)&(x^w), with the limit chosen by x's sign. subrev swaps the operands before the sub, so the borrow direction is right too. Merged with main, tests/semantic passes (787). LGTM. AI-assisted review (Claude Code).

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested 12b6b9fcd2d80b99b73a356cecc82b6158eda778 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

I ran five synthetic pixel shaders through the PR translator and Vulkan harness: unsigned add, subtract and reverse subtract, and signed add and subtract (the VOP3 NC forms). Each shader evaluated the same runtime operands with clamp enabled and disabled, then exported both 32-bit results in 16-bit halves for exact readback.

All 40 draws matched an independent integer reference in all 4,096 pixels per draw. The eight input pairs per instruction covered overflow, underflow, reversed operand order and an in-range control. For example, signed INT_MAX + 1 returned 0x7fffffff with clamp and 0x80000000 without it; unsigned 0 - 1 returned 0 and 0xffffffff respectively. All five fragment modules and the vertex module passed Vulkan 1.2 SPIR-V validation.

This measures translated Vulkan execution on NVIDIA. Native RDNA2 instruction behavior remains unmeasured by this probe.

AI-assisted verification.

@ItsNotRexed
ItsNotRexed merged commit e421255 into ItsNotRexed:main Nov 22, 2026
9 checks passed
DiarisA pushed a commit to DiarisA/Leonida that referenced this pull request Nov 22, 2026
…d_nc_i16 / v_sub_nc_i16

The VOP3 clamp bit on v_add_nc_u16 and v_sub_nc_u16 was silently dropped, so a saturating 16-bit add came out
wrapped (0xFFFF + 1 gave 0, 0 - 1 gave 0xFFFF). It now saturates to [0, 0xFFFF]. v_add_nc_i16 and v_sub_nc_i16,
which raised Unsupported, translate through the same handler: without clamp they give the u16 bits, with clamp they
saturate to [-0x8000, 0x7FFF]. This is the 16-bit counterpart of the 32-bit fix (ItsNotRexed#553).

Source: llc 18 selects llvm.uadd/usub.sat.i16 to v_add/sub_nc_u16 clamp and sadd/ssub.sat.i16 to
v_add/sub_nc_i16 clamp for gfx1013. The contract row in docs/design.md says so.

tests/semantic/test_alu_add_nc16_clamp.py runs all four ops with and without clamp, with op_sel source and
destination halves, against a Python reference that is itself checked against hand-computed words.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01D37mtb4J4WutkmHXBsd9cp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants