Repository navigation
shader: lower signed and clamped 32-bit add/sub in the direct compute path - #3011
Open
legacy-code-archaeologist wants to merge 1 commit into
Open
legacy-code-archaeologist wants to merge 1 commit into
legacy-code-archaeologist wants to merge 1 commit into
Conversation
… path
v_add_nc_i32 and v_sub_nc_i32 wrap without clamp. With clamp they
saturate to the i32 range toward the sign of S0, and v_add_nc_u32,
v_sub_nc_u32 and v_subrev_nc_u32 saturate to 0 or 0xffffffff, the forms
LLVM 15 selects for llvm.{s,u}{add,sub}.sat.i32. Overflow uses 32-bit
sign tests. Neg/abs, omod, op_sel, SDWA and DPP forms are refused.
Fixes R0Y00#3009
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
KelboLive
reviewed
Nov 27, 2026
KelboLive
left a comment
Contributor
There was a problem hiding this comment.
What works
- 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.
- All operand modifiers (neg, abs, omod, op_sel, SDWA, DPP) are
correctly refused. Theif ((signed_add_sub && !i.wide) || i.subword || i.dpp_control || permuted || i.abs || i.neg || i.scale || (i.a & 0x7800))check is comprehensive. - LLVM 15 source: correctly maps to llvm.sadd.sat, llvm.ssub.sat,
llvm.uadd.sat, llvm.usub.sat.
Issues
- 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. - The magic literals
2147483648,2147483647,4294967295and0
could benefit from named constants, but in a compact IR builder
context they're acceptable. - No test case for v_subrev_nc_u32's operand swap in the fixture.
The controls checkv_subrev_nc_u32withoutclampbut 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Lowers the VOP3-only
v_add_nc_i32andv_sub_nc_i32, and theclampforms ofv_add_nc_u32,v_sub_nc_u32andv_subrev_nc_u32, in the direct C++ compute path (arithmetic()):_i32saturates to the i32 range (toward the sign of S0) and_u32saturates 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.i32on gfx1013.tools/compute_store_aot.pylowers the unclamped signed forms the same way and refuses clamp;gfx2hlsl.pyhas 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-treeagainst each branch and againstcpp-migrationis 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:ctest --preset dev --no-tests=error -R '^native_' -j 3: 120 of 125 passed.native_graphics_wqm_hardwaretimed 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 basee0d83759on this machine.Environment: Windows 10.0.26100, VS 2026 BuildTools 18, base
cpp-migratione0d83759.AI-assisted: written with Claude Code. I reviewed it and ran the commands above.
🤖 Generated with Claude Code