feat(shader): saturate VOP2 integer SDWA results on clamp #510

Merged
adriadam10 merged 2 commits from upstream-pr/510 into main 2026-10-05 08:33:38 +00:00
adriadam10 commented 2026-10-04 11:37:47 +00:00 (Migrated from github.com)

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


What

clamp on VOP2 SDWA integer ops threw VOP2 SDWA output modifiers are not supported. It is now accepted on every op with an integer SDWA rule except v_cndmask_b32, with the result measured on hardware:

  • v_add_nc_u32, v_sub_nc_u32, v_subrev_nc_u32, v_add_co_ci_u32, v_sub_co_ci_u32, v_subrev_co_ci_u32, v_mul_u32_u24: unsigned saturation of the 32-bit result (0xffffffff on carry-out, 0 on borrow-out).
  • v_mul_i32_i24: signed saturation of the 48-bit product to [0x80000000, 0x7fffffff].
  • v_mul_hi_{i32_i24,u32_u24}, v_min/max_{i32,u32}, v_lshrrev_b32, v_ashrrev_i32, v_lshlrev_b32, v_and/or/xor/xnor_b32: clamp has no effect, so the translator leaves them unchanged.

The carry-out written to VCC is the same as without clamp. The saturation applies to the 32-bit result, before dst_sel picks the bits that get written. It is not done at the width of the destination field.

v_cndmask_b32 with clamp and omod on integer ops still throw. So do the VOP3 forms of these ops with clamp; that's a separate PR.

Behaviour measured on HW (RDNA2, gfx1035)

First, all 22 integer VOP2 SDWA ops with clamp and DWORD selectors, on 256 rows: the 8 ops above saturate and the other 14 match the unclamped result. Then the 12 forms in the test (byte and word selectors, sext, all three dst_unused modes, the three carry-outs) on 2048 rows: the Python model matches all 30720 results. The model saturates the full-width result and then applies dst_sel. The alternative, saturating to the destination field width, mismatches 1958 of them.

Tested

New execution test agc_driver_sdwa_integer_clamp_tests: 32 rows with expected values from the hardware. For each op, the rows cover a result below, inside and above the clamped range, the exact bounds, and both carry-in values. It passes on RADV and lavapipe.

It fails without each part of the change (7 mutations): the decoder refusing clamp, no saturation on the _nc ops, on v_add_co_ci_u32, on the borrow ops or on the 24-bit multiplies, unsigned saturation for v_mul_i32_i24, and saturating v_mul_hi_*. 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: #509 (this branch is stacked on it; its commit is the first one here)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4734031592 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/510). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T11:37:47Z. --- ### What `clamp` on VOP2 SDWA integer ops threw `VOP2 SDWA output modifiers are not supported`. It is now accepted on every op with an integer SDWA rule except `v_cndmask_b32`, with the result measured on hardware: - `v_add_nc_u32`, `v_sub_nc_u32`, `v_subrev_nc_u32`, `v_add_co_ci_u32`, `v_sub_co_ci_u32`, `v_subrev_co_ci_u32`, `v_mul_u32_u24`: unsigned saturation of the 32-bit result (`0xffffffff` on carry-out, `0` on borrow-out). - `v_mul_i32_i24`: signed saturation of the 48-bit product to `[0x80000000, 0x7fffffff]`. - `v_mul_hi_{i32_i24,u32_u24}`, `v_min/max_{i32,u32}`, `v_lshrrev_b32`, `v_ashrrev_i32`, `v_lshlrev_b32`, `v_and/or/xor/xnor_b32`: `clamp` has no effect, so the translator leaves them unchanged. The carry-out written to VCC is the same as without `clamp`. The saturation applies to the 32-bit result, before `dst_sel` picks the bits that get written. It is not done at the width of the destination field. `v_cndmask_b32` with `clamp` and `omod` on integer ops still throw. So do the VOP3 forms of these ops with `clamp`; that's a separate PR. #### Behaviour measured on HW (RDNA2, gfx1035) First, all 22 integer VOP2 SDWA ops with `clamp` and DWORD selectors, on 256 rows: the 8 ops above saturate and the other 14 match the unclamped result. Then the 12 forms in the test (byte and word selectors, `sext`, all three `dst_unused` modes, the three carry-outs) on 2048 rows: the Python model matches all 30720 results. The model saturates the full-width result and then applies `dst_sel`. The alternative, saturating to the destination field width, mismatches 1958 of them. ### Tested New execution test `agc_driver_sdwa_integer_clamp_tests`: 32 rows with expected values from the hardware. For each op, the rows cover a result below, inside and above the clamped range, the exact bounds, and both carry-in values. It passes on RADV and lavapipe. It fails without each part of the change (7 mutations): the decoder refusing `clamp`, no saturation on the `_nc` ops, on `v_add_co_ci_u32`, on the borrow ops or on the 24-bit multiplies, unsigned saturation for `v_mul_i32_i24`, and saturating `v_mul_hi_*`. 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: #509 (this branch is stacked on it; its commit is the first one here) - [x] AI-assisted: yes
oneandonlydean commented 2026-10-04 21:08:05 +00:00 (Migrated from github.com)

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


Thanks. The unsigned saturation on the add/sub forms matches what LLVM selects for uaddsat/usubsat (v_add_u32/v_sub_u32 with clamp), and the 48-bit saturation for the 24-bit multiplies is right arithmetically. My own model with "saturate the 32-bit result, then apply dst_sel" matches all 480 expected values in the test. I also checked that the new clamp branches in addU32, subbU32 and integer24 can't be reached from VOP3, VOP3B or scalar forms, since the decoder still refuses clamp there, and that the integer24 branch stays out of the way of #507's mad saturation. The new test fails on main and the full ctest passes on my machine. Ready to merge from my side after #509. Since v_add_nc_u32 and the subtracts now go through vAddSubNcU32, I'll run it through my PPSA21564 check before merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5984396795 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/510#issuecomment-5984396795). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T21:08:05Z. --- Thanks. The unsigned saturation on the add/sub forms matches what LLVM selects for `uaddsat`/`usubsat` (`v_add_u32`/`v_sub_u32` with clamp), and the 48-bit saturation for the 24-bit multiplies is right arithmetically. My own model with "saturate the 32-bit result, then apply `dst_sel`" matches all 480 expected values in the test. I also checked that the new clamp branches in `addU32`, `subbU32` and `integer24` can't be reached from VOP3, VOP3B or scalar forms, since the decoder still refuses clamp there, and that the `integer24` branch stays out of the way of #507's mad saturation. The new test fails on main and the full ctest passes on my machine. Ready to merge from my side after #509. Since `v_add_nc_u32` and the subtracts now go through `vAddSubNcU32`, I'll run it through my PPSA21564 check before merging.
oneandonlydean commented 2026-10-05 08:31:21 +00:00 (Migrated from github.com)

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


Gated on PPSA21564 on top of #509: no visual or FPS change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5990922082 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/510#issuecomment-5990922082). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T08:31:21Z. --- Gated on PPSA21564 on top of #509: no visual or FPS change.
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!510
No description provided.