feat(shader): implement clamp on v_mad_u64_u32 and v_mad_i64_i32 #563
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/563"
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?
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T21:07:21Z.
What
clampon the VOP3bv_mad_u64_u32andv_mad_i64_i32. llvm-mc (-mcpu=gfx1013) accepts it (word0 bit 15), and the decoder threw "VOP3 source modifiers are not implemented". Found by the modifier sweep of the shader recompiler.negandomodstill throw.Behaviour measured on HW (RDNA2 gfx1035, raw kernels, 2176 rows: every pair of 12 edge values × 8 addends, plus 1024 random rows; 2176/2176 match the model):
v_mad_u64_u32 clamp: the 64-bit result saturates to0xffffffffffffffffwhen the exacta*b + coverflows.v_mad_i64_i32 clamp: the 64-bit result saturates toINT64_MAX/INT64_MIN.The signed case overflows only when the product and the addend have the same sign and the wrapped sum has the other sign. The saturated value then takes the sign of the addend. Everything is computed in 32-bit halves, like the existing path.
Tested
Linux, no title. New execution test
agc_driver_vop3_mad_clamp_tests(Vop3MadClamp.cpp): 32 lanes, expected values from the HW. Rows include exactUINT64_MAX,INT64_MAXandINT64_MINresults (not saturated) and the values just past each one, plus every sign combination. It passes on RADV, local lavapipe and CI's lavapipe (Mesa 25.2.8, 0 s cold). Full ctest Release: 183/184 pass; the failing one,agc_driver_pixel_helper_lanes, also fails onmainwith local RADV.The test fails without the fix: the decoder throws. It also fails under each of these mutations: decoder dropping the clamp bit, no signed saturation, no unsigned saturation, saturated sign taken from the result, overflow tested on the addend alone.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T22:38:17Z.
Thanks. llvm-mc for gfx1013 accepts
clampon both (v_mad_u64_u32 ... clampsets word0 bit 15, the VOP3B CLMP field), and the RDNA2 ISA guide (ALU clamp bit usage) says that for integer operations clamp saturates to the largest or smallest representable value, which matches your measurement.I went through the saturation on the halves: for the unsigned form the existing carry is exactly the 64-bit overflow, since the product is at most (2^32 - 1)^2. For the signed form,
(mulHigh ^ high) & (add[1] ^ high)has its top bit set exactly when the product and the addend have the same sign and the wrapped sum has the other one, and the saturated value then takes the addend's sign (INT64_MAX as 0x7fffffff:ffffffff, INT64_MIN as 0x80000000:00000000). The sdst carry is computed from the unsaturated sum as before, as you measured.agc_driver_vop3_mad_clamppasses on my NVIDIA machine on current main, and fails on main's code.One note: #264 edits the same condition in
checkNativeVop3Modifiers(it adds theVDivScaleF32neg exception), so whichever lands second needs a one-line rebase that keeps both.This only adds an encoding that main refused, so it's ready to merge from my side.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T01:18:20Z.
#264 is on main now, so this needs the one-line rebase I mentioned in
RdnaVectorOpDecoder.cpp:checkNativeVop3Modifiersshould keep both #264'sVDivScaleF32neg exception and your clamp onv_mad_u64_u32/v_mad_i64_i32. Ready to merge from my side after that.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T01:36:04Z.
Rebased on current main:
checkNativeVop3Modifierskeeps both #264'sVDivScaleF32neg exception and the clamp exception forv_mad_u64_u32/v_mad_i64_i32.agc_driver_vop3_mad_clamp_testspasses on RADV and lavapipe, full ctest green on RADV apart from the knownagc_driver_pixel_helper_lanes.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T02:47:17Z.
Thanks, keeping #264's
VDivScaleF32neg exception next to the clamp exception is right, and the commit is otherwise unchanged.agc_driver_vop3_mad_clamppasses here on the merged branch, and PPSA21564 never uses clamp on these two opcodes. Ready to merge from my side. Note that #474 edits the same line, so whichever lands second needs a one-line rebase (I've left the combined line on #474).Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T02:59:33Z.
Rebased on current main after #474: the clamp exception now sits on the same line as main's
VDivScaleF32/VDivScaleF64neg exceptions (both kept). Commit otherwise unchanged;agc_driver_vop3_mad_clamppasses on RADV and lavapipe.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T07:26:34Z.
Thanks. The range-diff against the previous head shows only the
checkNativeVop3Modifiersline: the clamp exception forv_mad_u64_u32/v_mad_i64_i32now sits next to main'sVDivScaleF32andVDivScaleF64neg exceptions, with both kept, and the rest of the commit is unchanged. It merges cleanly with current main,agc_driver_vop3_mad_clamppasses, and the full ctest passes here with it merged.PPSA21564 never uses clamp on these two opcodes, so this doesn't need my PPSA21564 check. Ready to merge from my side.