Repository navigation
Translator: integer clamp on v_add/sub_nc_u32 (was ignored), and v_add_nc_i32 / v_sub_nc_i32 - #553
Conversation
… 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.
# Conflicts: # docs/design.md
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)
|
CI note: |
IAmNotSiob
left a comment
There was a problem hiding this comment.
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).
|
Tested 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. |
…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
This PR fixes two things in
tools/rdna2spirv.py:clampbit onv_add_nc_u32,v_sub_nc_u32andv_subrev_nc_u32is silently ignored, so a saturating add comes out wrapped. On GFX9+ an integer VOP3 add or sub with clamp saturates. LLVM's AMDGPU backend selectsuaddsat/usubsattov_add_nc_u32/v_sub_nc_u32with clamp set, so a compiledsaturate(a + b)on uints is exactly this encoding. Today the translator emits a plainOpIAdd, with no warning.v_add_nc_i32andv_sub_nc_i32raiseUnsupported. These are the targets forsaddsat/ssubsat.I don't know whether the corpus has clamped
_nc_u32adds. I can't check that without game files. Claimed in #374.Change
tools/rdna2spirv.py: a branch forv_add_nc_i32/v_sub_nc_i32, and for the three unsigned ops whenclampis set. Without clamp, the unsigned ops still go through their existing code.OpIAddCarry/OpISubBorrow. A carry gives0xFFFFFFFF, and a borrow gives 0.0x80000000/0x7FFFFFFFdepending 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, stdlibunittest) checks all five ops, with and without clamp, against a plain-Python reference:expas 16-bit halves ORed into0x40000000, so no export channel can be a NaN pattern (see the review on tests/semantic: export test_alu_shift_b64 results in 16-bit halves (fixes CI on Python < 3.14) #454)docs/design.md: an "Integer clamp" contract row.Tested
python -I -m unittest discover -s tests/semantic: all pass.Unsupportedon the signed ops) and 88 failures. The failures are the unsigned ops with clamp returning the wrapped value.python -m compileall -q tools testspasses. 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.