fix(shader): saturate VOP3 integer mad/sad results on clamp #507

Merged
adriadam10 merged 1 commit from upstream-pr/507 into main 2026-10-05 18:27:34 +00:00
adriadam10 commented 2026-10-04 11:03:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T11:03:55Z.


What

clamp on the VOP3 integer mad/sad ops and on the 16-bit max3/min3/med3 and align ops threw VOP3 source modifiers are not implemented. It is now decoded and applied as the hardware does:

  • v_mad_u32_u24, v_mad_u32_u16, v_sad_u8, v_sad_hi_u8, v_sad_u16, v_sad_u32, v_msad_u8, v_mqsad_u32_u8 (each dword): the exact sum saturates to 0xffffffff.
  • v_mad_i32_i24, v_mad_i32_i16: the exact sum saturates to [0x80000000, 0x7fffffff].
  • v_mad_u16: saturates to 0xffff; v_mad_i16: to [-32768, 32767]. op_sel still applies (high destination half keeps the low half).
  • v_qsad_pk_u16_u8, v_mqsad_pk_u16_u8: each 16-bit lane (accumulator half + SAD) saturates to 0xffff.
  • v_max3/min3/med3_{i16,u16}, v_alignbit_b32, v_alignbyte_b32: clamp has no effect on hardware, so it is accepted and ignored.

The 32-bit cases go through one helper that adds the addend to a (high:low) 64-bit value (the full 24x24 product via SMulHi/UMulHi, the sign of the 16x16 product, or 0 for SAD) and saturates only when clamp is set; without clamp the result is computed as before. #502's saturateInteger16 treats the value as signed (it needs that for v_pk_sub_u16), which is wrong for v_mad_u16 sums above 0x7fffffff, so the 16-bit case has its own unsigned UMin.

Vop3Sad.cpp checked that clamp on the SAD ops was refused; that check is removed, the new tests cover it.

v_mad_u64_u32/v_mad_i64_i32 (VOP3b) clamp still throws.

Behaviour measured on HW (RDNA2, gfx1035)

4096 rows (edge words + random) for each of the 22 ops, run with and without clamp: the Python model (exact sum, then saturate to the destination width and signedness) matches 57344 of 57344 clamped and unclamped results for the 14 saturating ops. For max3/min3/med3_{i16,u16} and alignbit/alignbyte, the clamped and unclamped results are identical on all 4096 rows.

Tested

Two new execution tests, 32 rows each, expected values from the hardware (the model matches every cell):

  • agc_driver_vop3_integer_clamp_tests: the 11 mad/sad ops plus 5 op_sel + clamp forms of the 16-bit mads (high destination half included). Rows are picked so each op has saturating and non-saturating rows, at both bounds for the signed ones.
  • agc_driver_vop3_integer_clamp_wide_tests: qsad/mqsad (every lane saturating and not) and the 8 ops where clamp is ignored.

Both pass on RADV, local lavapipe and CI's lavapipe (Mesa 25.2.8). The tests fail without each part of the fix: unsigned or signed wrap instead of saturation, dropping the high product or the addend sign, no upper or lower 16-bit bound, a zero high word for v_mad_i32_i16, the qsad lane not masked or its high lane wrapping, v_mqsad_u32_u8 wrapping, and clamp still refused by the decoder all fail (14 mutations). The full Release ctest passes except agc_driver_pixel_helper_lanes, which also fails on main on local RADV.

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: #504 (its commit is included; review only the last commit)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4733829731 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/507). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T11:03:55Z. --- ### What `clamp` on the VOP3 integer mad/sad ops and on the 16-bit max3/min3/med3 and align ops threw `VOP3 source modifiers are not implemented`. It is now decoded and applied as the hardware does: - `v_mad_u32_u24`, `v_mad_u32_u16`, `v_sad_u8`, `v_sad_hi_u8`, `v_sad_u16`, `v_sad_u32`, `v_msad_u8`, `v_mqsad_u32_u8` (each dword): the exact sum saturates to `0xffffffff`. - `v_mad_i32_i24`, `v_mad_i32_i16`: the exact sum saturates to `[0x80000000, 0x7fffffff]`. - `v_mad_u16`: saturates to `0xffff`; `v_mad_i16`: to `[-32768, 32767]`. `op_sel` still applies (high destination half keeps the low half). - `v_qsad_pk_u16_u8`, `v_mqsad_pk_u16_u8`: each 16-bit lane (accumulator half + SAD) saturates to `0xffff`. - `v_max3/min3/med3_{i16,u16}`, `v_alignbit_b32`, `v_alignbyte_b32`: `clamp` has no effect on hardware, so it is accepted and ignored. The 32-bit cases go through one helper that adds the addend to a (high:low) 64-bit value (the full 24x24 product via `SMulHi`/`UMulHi`, the sign of the 16x16 product, or 0 for SAD) and saturates only when `clamp` is set; without `clamp` the result is computed as before. `#502`'s `saturateInteger16` treats the value as signed (it needs that for `v_pk_sub_u16`), which is wrong for `v_mad_u16` sums above `0x7fffffff`, so the 16-bit case has its own unsigned `UMin`. `Vop3Sad.cpp` checked that `clamp` on the SAD ops was refused; that check is removed, the new tests cover it. `v_mad_u64_u32`/`v_mad_i64_i32` (VOP3b) `clamp` still throws. #### Behaviour measured on HW (RDNA2, gfx1035) 4096 rows (edge words + random) for each of the 22 ops, run with and without `clamp`: the Python model (exact sum, then saturate to the destination width and signedness) matches 57344 of 57344 clamped and unclamped results for the 14 saturating ops. For max3/min3/med3_{i16,u16} and alignbit/alignbyte, the clamped and unclamped results are identical on all 4096 rows. ### Tested Two new execution tests, 32 rows each, expected values from the hardware (the model matches every cell): - `agc_driver_vop3_integer_clamp_tests`: the 11 mad/sad ops plus 5 `op_sel` + `clamp` forms of the 16-bit mads (high destination half included). Rows are picked so each op has saturating and non-saturating rows, at both bounds for the signed ones. - `agc_driver_vop3_integer_clamp_wide_tests`: qsad/mqsad (every lane saturating and not) and the 8 ops where `clamp` is ignored. Both pass on RADV, local lavapipe and CI's lavapipe (Mesa 25.2.8). The tests fail without each part of the fix: unsigned or signed wrap instead of saturation, dropping the high product or the addend sign, no upper or lower 16-bit bound, a zero high word for `v_mad_i32_i16`, the qsad lane not masked or its high lane wrapping, `v_mqsad_u32_u8` wrapping, and `clamp` still refused by the decoder all fail (14 mutations). The full Release ctest passes except `agc_driver_pixel_helper_lanes`, which also fails on `main` on local RADV. ### 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: #504 (its commit is included; review only the last commit) - [x] AI-assisted: yes
oneandonlydean commented 2026-10-04 21:08:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T21:08:01Z.


Checked the saturation by hand:

  • wideAdd builds the high word of the exact 64-bit sum (high + carry + sext(addend)), so both the signed overflow test and the 0x80000000/0x7fffffff select are right.
  • The 16-bit sums can't wrap before saturateU16Result.
  • Without clamp every path computes the same value as before.

The clamp bit matches LLVM's VOP3_CLAMP profiles on these ops. On my machine both new tests fail on main and pass with the change.

This and #508 both edit supportsNativeVop3Clamp, so whichever lands second needs a one-line rebase. Once #504 is in and after my PPSA21564 check, it's ready to merge from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5984396266 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/507#issuecomment-5984396266). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T21:08:01Z. --- Checked the saturation by hand: - `wideAdd` builds the high word of the exact 64-bit sum (`high + carry + sext(addend)`), so both the signed overflow test and the `0x80000000`/`0x7fffffff` select are right. - The 16-bit sums can't wrap before `saturateU16Result`. - Without `clamp` every path computes the same value as before. The clamp bit matches LLVM's `VOP3_CLAMP` profiles on these ops. On my machine both new tests fail on main and pass with the change. This and #508 both edit `supportsNativeVop3Clamp`, so whichever lands second needs a one-line rebase. Once #504 is in and after my PPSA21564 check, it's ready to merge from my side.
oneandonlydean commented 2026-10-05 07:26:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T07:26:31Z.


#610 landed today and added isF32DivisionStepOpcode(opcode) to the supportsNativeVop3Clamp return line, which this PR also changes, so it conflicts now. Keeping main's new term with your rename fixes it:

return supportsNativeVop3ResultModifiers(opcode) || usesInexactClampControl(opcode) || usesIntegerSaturateClamp(opcode) ||
    isF32DivisionStepOpcode(opcode);

Could you rebase? I tried this on current main together with #504 and #508, and the full ctest passes. #508 changes the same line, so whichever of the two lands second will need the same one-line fix again (all terms kept).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5990011888 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/507#issuecomment-5990011888). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T07:26:31Z. --- #610 landed today and added `isF32DivisionStepOpcode(opcode)` to the `supportsNativeVop3Clamp` return line, which this PR also changes, so it conflicts now. Keeping main's new term with your rename fixes it: ```cpp return supportsNativeVop3ResultModifiers(opcode) || usesInexactClampControl(opcode) || usesIntegerSaturateClamp(opcode) || isF32DivisionStepOpcode(opcode); ``` Could you rebase? I tried this on current main together with #504 and #508, and the full ctest passes. #508 changes the same line, so whichever of the two lands second will need the same one-line fix again (all terms kept).
adriadam10 commented 2026-10-05 08:58:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T08:58:22Z.


Rebased on current main after #610 and #504:

  • supportsNativeVop3Clamp: kept isF32DivisionStepOpcode next to the usesIntegerSaturateClamp rename, as suggested. #504 dropped out as already merged.
  • agc_driver_vop3* and agc_shader* tests pass on RADV.
  • #508 still uses the old name on that line; it needs the same one-line fix after whichever of the two lands first.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5991300582 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/507#issuecomment-5991300582). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T08:58:22Z. --- Rebased on current `main` after #610 and #504: - `supportsNativeVop3Clamp`: kept `isF32DivisionStepOpcode` next to the `usesIntegerSaturateClamp` rename, as suggested. #504 dropped out as already merged. - `agc_driver_vop3*` and `agc_shader*` tests pass on RADV. - #508 still uses the old name on that line; it needs the same one-line fix after whichever of the two lands first.
adriadam10 commented 2026-10-05 17:05:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T17:05:54Z.


Rebased on main (conflict in supportsNativeVop3Clamp with the cube clamp: both kept). Vop3IntegerClamp, Vop3IntegerClampWide and Vop3Sad pass on RADV and lavapipe.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5999257573 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/507#issuecomment-5999257573). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T17:05:54Z. --- Rebased on main (conflict in `supportsNativeVop3Clamp` with the cube clamp: both kept). Vop3IntegerClamp, Vop3IntegerClampWide and Vop3Sad pass on RADV and lavapipe.
oneandonlydean commented 2026-10-05 18:13:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T18:13:04Z.


Thanks, the rebase is right: the supportsNativeVop3Clamp line now has usesIntegerSaturateClamp together with #508's isCubeOpcode, and the rest of the commit is unchanged. Ready to merge from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6000353310 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/507#issuecomment-6000353310). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T18:13:04Z. --- Thanks, the rebase is right: the `supportsNativeVop3Clamp` line now has `usesIntegerSaturateClamp` together with #508's `isCubeOpcode`, and the rest of the commit is unchanged. 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!507
No description provided.