shader: lower clamped integer, carry-out, MAD/SAD, and f16 pack VALU ops #516

Open
Sheffy007 wants to merge 1 commit from upstream-pr/516 into main
Sheffy007 commented 2026-11-27 23:42:39 +00:00 (Migrated from github.com)

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


Summary

  • Follow RDNA2 ISA CLAMP semantics across VALU operations routed through Lower::write_i, valu16, and packed16 that previously failed as "integer output modifiers" or "modifiers on a 16-bit/packed integer operation" (while leaving non-clamped emission unchanged and keeping omod != 0 and bitwise/shift clamp refused):
    • v_add_nc_i32 and v_sub_nc_i32 (sadd.sat.i32 / ssub.sat.i32 with clamp, wrapping without clamp), v_cvt_pk_u16_u32, v_cvt_pk_i16_i32, and v_mad_i32_i16.
    • v_add_co_u32, v_sub_co_u32, v_subrev_co_u32, v_add_co_ci_u32, v_sub_co_ci_u32, and v_subrev_co_ci_u32 with clamp (saturating vdst at 0xffffffff or 0 while keeping the un-clamped carry/borrow mask in sdst).
    • v_mul_{u32_u24,i32_i24}, v_mad_{u32_u24,i32_i24,u32_u16,i32_i16}, v_mad_{u64_u32,i64_i32}, and v_sad_{u8,hi_u8,u16,u32} / v_msad_u8 with clamp.
    • v_cvt_f16_f32 (opsel == 0, matching valu16), v_cvt_pkrtz_f16_f32 ([0.0, 1.0] float clamp before RTZ conversion), and v_cvt_pknorm_{u16,i16}_f32 / v_cvt_norm_{u16,i16}_f16 with clamp.
    • 16-bit and VOP3P packed 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.

Verification

  • Unit and negative refusal tests in victory_shader::emit::lower::tests and victory_shader::emit::valu_tests
  • Metal GPU value test victory_gpu::native::tests::addr_tests::clamped_integer_carry_mad_sad_and_half_packs_saturate_on_the_gpu (16 boundary/saturation cases on Metal)
  • cargo test -p victory-hle -p victory -p victory-gpu -p victory-metal -p victory-shader -p xtask
  • cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo xtask policy

Closes #515
Refs #4, #258
cc @Olima34

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5534550389 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/516). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T23:42:39Z. --- ## Summary - Follow RDNA2 ISA `CLAMP` semantics across VALU operations routed through `Lower::write_i`, `valu16`, and `packed16` that previously failed as `"integer output modifiers"` or `"modifiers on a 16-bit/packed integer operation"` (while leaving non-clamped emission unchanged and keeping `omod != 0` and bitwise/shift `clamp` refused): - `v_add_nc_i32` and `v_sub_nc_i32` (`sadd.sat.i32` / `ssub.sat.i32` with `clamp`, wrapping without `clamp`), `v_cvt_pk_u16_u32`, `v_cvt_pk_i16_i32`, and `v_mad_i32_i16`. - `v_add_co_u32`, `v_sub_co_u32`, `v_subrev_co_u32`, `v_add_co_ci_u32`, `v_sub_co_ci_u32`, and `v_subrev_co_ci_u32` with `clamp` (saturating `vdst` at `0xffffffff` or `0` while keeping the un-clamped carry/borrow mask in `sdst`). - `v_mul_{u32_u24,i32_i24}`, `v_mad_{u32_u24,i32_i24,u32_u16,i32_i16}`, `v_mad_{u64_u32,i64_i32}`, and `v_sad_{u8,hi_u8,u16,u32}` / `v_msad_u8` with `clamp`. - `v_cvt_f16_f32` (`opsel == 0`, matching `valu16`), `v_cvt_pkrtz_f16_f32` (`[0.0, 1.0]` float clamp before RTZ conversion), and `v_cvt_pknorm_{u16,i16}_f32` / `v_cvt_norm_{u16,i16}_f16` with `clamp`. - 16-bit and VOP3P packed `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`. ## Verification - Unit and negative refusal tests in `victory_shader::emit::lower::tests` and `victory_shader::emit::valu_tests` - Metal GPU value test `victory_gpu::native::tests::addr_tests::clamped_integer_carry_mad_sad_and_half_packs_saturate_on_the_gpu` (16 boundary/saturation cases on Metal) - `cargo test -p victory-hle -p victory -p victory-gpu -p victory-metal -p victory-shader -p xtask` - `cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings` - `cargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warnings` - `cargo xtask policy` Closes #515 Refs #4, #258 cc @Olima34
Olima34 commented 2026-11-27 23:48:33 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T23:48:33Z.


Thanks @Sheffy007! Integer output modifiers are one of the refusal classes we see most often in world shaders, so this is welcome. Keeping omod != 0 and bitwise/shift clamp refused rather than guessing reads right to us.

Heads-up: main moved an hour ago to 86cefd22 (the maintainer merged the preview branch: page-crossing reads, mesh rules, the compute heap-index rule, LOD clamp and bias). We'll sweep #516 on that main over our two boot corpora (4,921 and 4,129 containers) and on the 21,600-container world corpus, then post newly translated / regressions / changed outputs per stage here, the same way as on #464. We'll also flag any overlap with your #448, #464 and #512 where they touch the same lowering.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7230424520 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/516#issuecomment-7230424520). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-27T23:48:33Z. --- Thanks @Sheffy007! Integer output modifiers are one of the refusal classes we see most often in world shaders, so this is welcome. Keeping `omod != 0` and bitwise/shift `clamp` refused rather than guessing reads right to us. Heads-up: main moved an hour ago to `86cefd22` (the maintainer merged the preview branch: page-crossing reads, mesh rules, the compute heap-index rule, LOD clamp and bias). We'll sweep #516 on that main over our two boot corpora (4,921 and 4,129 containers) and on the 21,600-container world corpus, then post newly translated / regressions / changed outputs per stage here, the same way as on #464. We'll also flag any overlap with your #448, #464 and #512 where they touch the same lowering.
Olima34 commented 2026-11-28 01:55:18 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-28T01:55:18Z.


Thanks for this PR! Here is the corpus sweep I promised, with no game run.

Corpus sweep (examples/sweep + tools/corpus/compare.sh), measured on main a6a58361. That main translates shaders the same way as 226645f9.

  • Head swept: 497baed6.
  • Your rebase to 65a04a4b only changes test context; the shader-side diff is the same, and main's newer victory-shader commits since a6a58361 only add tests.
set newly translated output changed regressions
A (4,921 boot shaders) 0 0 0
B (4,129 boot shaders) 0 0 0
W (21,600 world shaders) 0 0 0

The same holds at the earlier head 4dccd5ab, which still had the v_cvt_pk_u16_u32 part: 0 / 0 / 0 on all three sets.

No shader in these sets is refused first for "integer output modifiers" or "modifiers on a 16-bit/packed integer operation" on current main. So this is a no-regression check rather than a coverage measurement. Shaders whose first refusal is something else may still carry these ops further in.

Overlap with #448, #464 and #512: #516 merges cleanly with each of them, and with all three together, on a6a58361. Since #516 changes no output in these sets, its gains cannot overlap theirs.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7232006241 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/516#issuecomment-7232006241). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-28T01:55:18Z. --- Thanks for this PR! Here is the corpus sweep I promised, with no game run. **Corpus sweep** (`examples/sweep` + `tools/corpus/compare.sh`), measured on main a6a58361. That main translates shaders the same way as 226645f9. - Head swept: 497baed6. - Your rebase to 65a04a4b only changes test context; the shader-side diff is the same, and main's newer victory-shader commits since a6a58361 only add tests. | set | newly translated | output changed | regressions | |---|---|---|---| | A (4,921 boot shaders) | 0 | 0 | 0 | | B (4,129 boot shaders) | 0 | 0 | 0 | | W (21,600 world shaders) | 0 | 0 | 0 | The same holds at the earlier head 4dccd5ab, which still had the `v_cvt_pk_u16_u32` part: 0 / 0 / 0 on all three sets. No shader in these sets is refused first for "integer output modifiers" or "modifiers on a 16-bit/packed integer operation" on current main. So this is a no-regression check rather than a coverage measurement. Shaders whose first refusal is something else may still carry these ops further in. **Overlap with #448, #464 and #512:** #516 merges cleanly with each of them, and with all three together, on a6a58361. Since #516 changes no output in these sets, its gains cannot overlap theirs.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/516:upstream-pr/516'
git switch 'upstream-pr/516'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/516'
git switch 'upstream-pr/516'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/516'
git switch 'upstream-pr/516'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/516'
git switch 'main'
git merge --squash 'upstream-pr/516'
git switch 'main'
git merge --ff-only 'upstream-pr/516'
git switch 'main'
git merge 'upstream-pr/516'
git push origin 'main'
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
LakshanSivakumar/Native-Victory-macOS!516
No description provided.