Skip to content

feat(shader): implement clamp on v_mad_u64_u32 and v_mad_i64_i32 - #563

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
adriadam10:feat/vop3-mad64-clamp
Oct 5, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
adriadam10:feat/vop3-mad64-clamp

Conversation

@adriadam10

Copy link
Copy Markdown
Collaborator

What

clamp on the VOP3b v_mad_u64_u32 and v_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. neg and omod still 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 to 0xffffffffffffffff when the exact a*b + c overflows.
  • v_mad_i64_i32 clamp: the 64-bit result saturates to INT64_MAX / INT64_MIN.
  • The carry-out (sdst) does not change with clamp: still overflow for u64, still the sign of the exact result for i64 (the feat(shader): implement v_mad_i64_i32 #435 rule).

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 exact UINT64_MAX, INT64_MAX and INT64_MIN results (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 on main with 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

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

@github-actions github-actions Bot added shader core/shader agc libSceAgc* labels Oct 4, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks. llvm-mc for gfx1013 accepts clamp on both (v_mad_u64_u32 ... clamp sets 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_clamp passes on my NVIDIA machine on current main, and fails on main's code.

One note: #264 edits the same condition in checkNativeVop3Modifiers (it adds the VDivScaleF32 neg 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.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 4, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

#264 is on main now, so this needs the one-line rebase I mentioned in RdnaVectorOpDecoder.cpp: checkNativeVop3Modifiers should keep both #264's VDivScaleF32 neg exception and your clamp on v_mad_u64_u32/v_mad_i64_i32. Ready to merge from my side after that.

@adriadam10
adriadam10 force-pushed the feat/vop3-mad64-clamp branch from a6a168f to 5ce854d Compare October 5, 2026 01:36
@adriadam10

Copy link
Copy Markdown
Collaborator Author

Rebased on current main: checkNativeVop3Modifiers keeps both #264's VDivScaleF32 neg exception and the clamp exception for v_mad_u64_u32/v_mad_i64_i32. agc_driver_vop3_mad_clamp_tests passes on RADV and lavapipe, full ctest green on RADV apart from the known agc_driver_pixel_helper_lanes.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 5, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks, keeping #264's VDivScaleF32 neg exception next to the clamp exception is right, and the commit is otherwise unchanged. agc_driver_vop3_mad_clamp passes 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).

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 5, 2026
Co-Authored-By: Claude <noreply@anthropic.com>
@adriadam10

Copy link
Copy Markdown
Collaborator Author

Rebased on current main after #474: the clamp exception now sits on the same line as main's VDivScaleF32/VDivScaleF64 neg exceptions (both kept). Commit otherwise unchanged; agc_driver_vop3_mad_clamp passes on RADV and lavapipe.

@adriadam10
adriadam10 force-pushed the feat/vop3-mad64-clamp branch from 5ce854d to a15a21c Compare October 5, 2026 02:59
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 5, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks. The range-diff against the previous head shows only the checkNativeVop3Modifiers line: the clamp exception for v_mad_u64_u32/v_mad_i64_i32 now sits next to main's VDivScaleF32 and VDivScaleF64 neg exceptions, with both kept, and the rest of the commit is unchanged. It merges cleanly with current main, agc_driver_vop3_mad_clamp passes, 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants