shader: lower clamped integer, carry-out, MAD/SAD, and f16 pack VALU operations ("integer output modifiers") #515

Open
opened 2026-11-27 23:30:25 +00:00 by Sheffy007 · 2 comments
Sheffy007 commented 2026-11-27 23:30:25 +00:00 (Migrated from github.com)
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T23:30:25Z.


Problem

In victory-shader (crates/victory-shader/src/emit/lower.rs), Lower::write_i currently rejects any instruction with v.clamp set outside SATURATING = ["v_add_nc_u32", "v_sub_nc_u32", "v_subrev_nc_u32"] as "integer output modifiers" (still refusing 3 compute shaders in the #4 corpus sweep), while valu16 and packed16 reject clamped unsigned 16-bit and packed 16-bit integer operations:

  1. 16-bit float / normalized packs routed through write_i: v_cvt_f16_f32 (when opsel == 0 in valu_op, unlike valu16), v_cvt_pkrtz_f16_f32, and v_cvt_pknorm_{u16,i16}_f32 call write_i, so VOP3 clamp ([0.0, 1.0] float clamp on v_cvt_f16_f32 / v_cvt_pkrtz_f16_f32, already clamped on v_cvt_pknorm_*) is refused as "integer output modifiers".
  2. Clamped 32-bit integer arithmetic & carry-out/MAD/SAD ops:
    • v_add_co_u32, v_sub_co_u32, v_subrev_co_u32, v_add_co_ci_u32, v_sub_co_ci_u32, v_subrev_co_ci_u32 with clamp (saturating vdst at 0xffffffff or 0 while keeping the un-clamped carry/borrow mask in sdst).
    • v_add_nc_i32 and v_sub_nc_i32 (sadd.sat.i32 / ssub.sat.i32 with clamp, wrapping without clamp), plus v_cvt_pk_{u16_u32,i16_i32} and v_mad_i32_i16.
    • v_mad_{u32_u24,i32_i24,u32_u16,i32_i16}, v_mul_{u32_u24,i32_i24}, and v_sad_{u8,hi_u8,u16,u32} / v_msad_u8 with clamp.
  3. 16-bit and packed 16-bit integer clamp: v_add_nc_u16, v_sub_nc_u16, v_subrev_nc_u16, v_mul_lo_u16, and v_pk_{add,sub,mad}_{u16,i16} / v_pk_mul_lo_u16 with clamp.

Proposed Fix

  • Follow RDNA2 ISA CLAMP semantics across these 32-bit, 16-bit, and VOP3P packed integer and float-to-half packing instructions while keeping omod != 0 and non-saturating bitwise/shift clamp refused.
  • Add unit tests (including negative refusal checks) and a Metal GPU value test verifying exact boundary/overflow saturation results.

Refs #4, #258

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6656091762 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/515). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T23:30:25Z. --- ## Problem In `victory-shader` (`crates/victory-shader/src/emit/lower.rs`), `Lower::write_i` currently rejects any instruction with `v.clamp` set outside `SATURATING = ["v_add_nc_u32", "v_sub_nc_u32", "v_subrev_nc_u32"]` as `"integer output modifiers"` (still refusing 3 compute shaders in the #4 corpus sweep), while `valu16` and `packed16` reject clamped unsigned 16-bit and packed 16-bit integer operations: 1. **16-bit float / normalized packs routed through `write_i`:** `v_cvt_f16_f32` (when `opsel == 0` in `valu_op`, unlike `valu16`), `v_cvt_pkrtz_f16_f32`, and `v_cvt_pknorm_{u16,i16}_f32` call `write_i`, so VOP3 `clamp` (`[0.0, 1.0]` float clamp on `v_cvt_f16_f32` / `v_cvt_pkrtz_f16_f32`, already clamped on `v_cvt_pknorm_*`) is refused as `"integer output modifiers"`. 2. **Clamped 32-bit integer arithmetic & carry-out/MAD/SAD ops:** - `v_add_co_u32`, `v_sub_co_u32`, `v_subrev_co_u32`, `v_add_co_ci_u32`, `v_sub_co_ci_u32`, `v_subrev_co_ci_u32` with `clamp` (saturating `vdst` at `0xffffffff` or `0` while keeping the un-clamped carry/borrow mask in `sdst`). - `v_add_nc_i32` and `v_sub_nc_i32` (`sadd.sat.i32` / `ssub.sat.i32` with `clamp`, wrapping without `clamp`), plus `v_cvt_pk_{u16_u32,i16_i32}` and `v_mad_i32_i16`. - `v_mad_{u32_u24,i32_i24,u32_u16,i32_i16}`, `v_mul_{u32_u24,i32_i24}`, and `v_sad_{u8,hi_u8,u16,u32}` / `v_msad_u8` with `clamp`. 3. **16-bit and packed 16-bit integer `clamp`:** `v_add_nc_u16`, `v_sub_nc_u16`, `v_subrev_nc_u16`, `v_mul_lo_u16`, and `v_pk_{add,sub,mad}_{u16,i16}` / `v_pk_mul_lo_u16` with `clamp`. ## Proposed Fix - Follow RDNA2 ISA `CLAMP` semantics across these 32-bit, 16-bit, and VOP3P packed integer and float-to-half packing instructions while keeping `omod != 0` and non-saturating bitwise/shift `clamp` refused. - Add unit tests (including negative refusal checks) and a Metal GPU value test verifying exact boundary/overflow saturation results. Refs #4, #258
Sheffy007 commented 2026-11-27 23:30:26 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T23:30:26Z.


/claim

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7230193451 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/515#issuecomment-7230193451). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T23:30:26Z. --- /claim
github-actions[bot] commented 2026-11-27 23:30:35 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by github-actions[bot] on 2026-11-27T23:30:35Z.


Claimed by @Sheffy007. Open a draft pull request with Refs #515 in its description within 24 hours, or the claim expires.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7230195422 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/515#issuecomment-7230195422). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-27T23:30:35Z. --- Claimed by @Sheffy007. Open a draft pull request with `Refs #515` in its description within 24 hours, or the claim expires.
Sign in to join this conversation.
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
LakshanSivakumar/Native-Victory-macOS#515
No description provided.