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

Merged
adriadam10 merged 1 commit from upstream-pr/563 into main 2026-10-05 07:26:55 +00:00
adriadam10 commented 2026-10-04 21:07:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T21:07:21Z.


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 #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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4737631349 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/563). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T21:07:21Z. --- ### 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 #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 <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
oneandonlydean commented 2026-10-04 22:38:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T22:38:17Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985221203 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/563#issuecomment-5985221203). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T22:38:17Z. --- 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.
oneandonlydean commented 2026-10-05 01:18:20 +00:00 (Migrated from github.com)

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: 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5986481113 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/563#issuecomment-5986481113). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T01:18:20Z. --- #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 commented 2026-10-05 01:36:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T01:36:04Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5986625645 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/563#issuecomment-5986625645). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T01:36:04Z. --- 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`.
oneandonlydean commented 2026-10-05 02:47:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T02:47:17Z.


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).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5987229129 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/563#issuecomment-5987229129). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T02:47:17Z. --- 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).
adriadam10 commented 2026-10-05 02:59:33 +00:00 (Migrated from github.com)

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/VDivScaleF64 neg exceptions (both kept). Commit otherwise unchanged; agc_driver_vop3_mad_clamp passes on RADV and lavapipe.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5987331708 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/563#issuecomment-5987331708). Originally posted by [adriadam10](https://github.com/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`/`VDivScaleF64` neg exceptions (both kept). Commit otherwise unchanged; `agc_driver_vop3_mad_clamp` passes on RADV and lavapipe.
oneandonlydean commented 2026-10-05 07:26:34 +00:00 (Migrated from github.com)

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 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5990012436 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/563#issuecomment-5990012436). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T07:26:34Z. --- 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 in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!563
No description provided.