fix: VOP3 clamp on v_add/sub/subrev_nc_u32 and the carry-out add/sub saturates (was silently ignored) #235
No reviewers
Labels
No labels
accessibility
bloqué : binaire console
bug
chargeur
console-dll
cpu
documentation
duplicate
enhancement
good first issue
gpu
help wanted
invalid
performance
question
runtime-gdk
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/LooperSalty--AnyXboxX!235
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/235/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 UTCChange
VOP3
clamponv_add_nc_u32,v_sub_nc_u32,v_subrev_nc_u32and 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 onmain, 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 selectsuaddsat/usubsatto exactly these ops withclampset, 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_u32withclamp: D saturates to [0, 0xFFFFFFFF] instead of wrapping.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.emu/rdna2_hlsl_clamp.py(43 lines), shared by wave32 and wave64.rdna2_hlsl.pyis 384 lines (389 on main), per rule 8.x + y < x ? 0xFFFFFFFFu : x + y,o ? 0x0u : r), never withclamp()on a widened sum: #232 found the RTX 4060 driver miscompilingclamp(Sext16(a) + Sext16(b), …)followed by a mask, so the generated body has noclamp(at all (a test checks it).clampon 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 encodesclampon 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, still1400+).Linked issue: none (found while preparing #232). Overlap with #232: both PRs touch
emu/rdna2_exec_vop3.py,emu/rdna2_hlsl.pyandemu/rdna2_hlsl_wave64.py(different lines: #232 adds imports and a dispatch to its int16 modules, this PR changes the carry/_nc_u32paths and adds the refusal); whichever merges second needs a small rebase, and #232's README count (1400+) wins.Verification
Branch rebased on main
e921268(head8d2508e). Tests were run on the same tree rebased on9a06443;e921268only adds launcher/docs changes.No C++ or Rust change.
Oracle and test data
tests/test_rdna2_int_clamp.py(26 tests). The GPU tests run every one of the nine ops withclampon 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 + ywithout saturation) makes the parity check fail on RTX, so the GPU path is really exercised.main(interpreter model, single-lane values, HLSL text, the refusals and both GPU tests); the 5 that pass onmainare the unclamped wrap control and four single-lane cases whereclampdoes not change the result (no carry or borrow).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 ofV_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.llandusubsat.llselectuaddsat/usubsati32 tov_add_nc_u32_e64 … clamp/v_sub_nc_u32_e64 … clampon GFX10, and to the carry-out formv_add_u32_e64 v0, s[4:5], v0, v1 clamp/v_sub_u32_e64 v0, s[4:5], … clampon 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.clampon the 24-bit multiplies,v_mad_u64_u32/v_mad_i64_i32and the float-to-int conversions.Checklist
clampon 32-bit integer results.)E_NOTIMPL), never a fake success. (Unimplementedclampforms raiseUntranslated/Untranslatablenaming the instruction.)test_readme_sync.pychecks it). (No README change: count still1300+, coverage unchanged.)Made with AI assistance: Claude Code with Claude Opus 5.5, for KainanP.
🤖 Generated with Claude Code
@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.pyis 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:45 UTC · original
Follow-up after the review: (1)
rdna2_exec_vop3.py:153still cites Leonida's docs/design.md "Integer clamp"; please remove it and cite the AMD ISA section for the saturation instead. (2)rdna2_hlsl.pyis still 407 lines: please move the carry/clamp helpers (rdna2_hlsl.py:146-151) to a small module (rule 8). (3) Rebase on main.@KainanP on GitHub · 2026-11-25 22:54 UTC · original
Thanks. Updated and rebased on main
e921268(one commit,8d2508e):CodeGen/AMDGPU/uaddsat.llandusubsat.llselectuaddsat/usubsattov_add_nc_u32_e64 … clampandv_sub_nc_u32_e64 … clampon GFX10, and to the carry-outv_add_u32_e64 v0, s[4:5], … clampon GFX8. Section E of akimfomin/PortBridge#84 asks for a hardware check.SATURATED_U32_HLSL,carry_writeand the refusal moved to the newemu/rdna2_hlsl_clamp.py(43 lines), shared by wave32 and wave64.rdna2_hlsl.pyis 384 lines (389 on main).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).