shader: lower v_sat_pk_u8_i16 #499

Open
NolanCotter wants to merge 3 commits from upstream-pr/499 into main
NolanCotter commented 2026-11-24 04:11:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by NolanCotter on 2026-11-24T04:11:39Z.


Depends on #498 (uses its lowers_lds test helper; rebases to one commit once it lands).

Summary

  • Lowers v_sat_pk_u8_i16 (VOP1 0x62 / VOP3 0x1E2), refused at integer()'s fallthrough on main.
  • Each signed i16 half of src0 is sign-extended and saturated to an unsigned byte, packed into the destination's low 16 bits; the register's high half is kept — the GFX10 partial write, as v_cvt_f16_f32 (LLVM models the def as VOP1Inst_t16; Mesa ACO instr_is_16bit). Uses the existing low_half helper.
  • src() already refuses abs/neg, write_i refuses clamp/omod; a nonzero op_sel is refused.
  • Smoke test sat_pk_u8_packs_the_clamped_halves: 0x0100_7fff (halves 256 and 32767) saturates to 0xffff in the low 16 while 0xdead survives in the high half, consumed by an LDS store. Refused on main, lowers now.

Test plan

  • cargo test -p victory-shader — 59 passed
  • cargo clippy -p victory-shader — clean
  • cargo fmt --check — clean

AI-assisted (Devin). Semantics from the AMD RDNA2 ISA opcode description and the LLVM/ACO 16-bit-write tables; no game files used.

Generated with Devin

<!-- greyfinch-sync:Marvisak/VIctory:issue:6574432070 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/499). Originally posted by [NolanCotter](https://github.com/NolanCotter) on 2026-11-24T04:11:39Z. --- Depends on #498 (uses its `lowers_lds` test helper; rebases to one commit once it lands). ## Summary - Lowers `v_sat_pk_u8_i16` (VOP1 0x62 / VOP3 0x1E2), refused at `integer()`'s fallthrough on main. - Each signed i16 half of src0 is sign-extended and saturated to an unsigned byte, packed into the destination's low 16 bits; the register's high half is kept — the GFX10 partial write, as `v_cvt_f16_f32` (LLVM models the def as `VOP1Inst_t16`; Mesa ACO `instr_is_16bit`). Uses the existing `low_half` helper. - `src()` already refuses `abs`/`neg`, `write_i` refuses `clamp`/`omod`; a nonzero `op_sel` is refused. - Smoke test `sat_pk_u8_packs_the_clamped_halves`: `0x0100_7fff` (halves 256 and 32767) saturates to `0xffff` in the low 16 while `0xdead` survives in the high half, consumed by an LDS store. Refused on main, lowers now. #### Test plan - [x] `cargo test -p victory-shader` — 59 passed - [x] `cargo clippy -p victory-shader` — clean - [x] `cargo fmt --check` — clean AI-assisted (Devin). Semantics from the AMD RDNA2 ISA opcode description and the LLVM/ACO 16-bit-write tables; no game files used. Generated with [Devin](https://devin.ai)
LAKSHYA-cmd-tech commented 2026-11-24 06:53:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by LAKSHYA-cmd-tech on 2026-11-24T04:50:19Z.

Review: APPROVED


Verified on Windows 11 D3D12:

  • All 72 tests in \ ictory-shader\ pass, including \sat_pk_u8_packs_the_clamped_halves.
  • Signed i16 saturation to u8 byte packing into destination low 16 bits with preserved high half accurately reflects GFX10 partial write semantics.
  • \cargo clippy -p victory-shader\ clean with zero warnings.
  • \cargo xtask policy\ passes (151 files checked, zero asset policy violations).
<!-- greyfinch-sync:Marvisak/VIctory:review:6033806464 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/499#pullrequestreview-6033806464). Originally posted by [LAKSHYA-cmd-tech](https://github.com/LAKSHYA-cmd-tech) on 2026-11-24T04:50:19Z. **Review:** APPROVED --- Verified on Windows 11 D3D12: - All 72 tests in \ ictory-shader\ pass, including \sat_pk_u8_packs_the_clamped_halves\. - Signed i16 saturation to u8 byte packing into destination low 16 bits with preserved high half accurately reflects GFX10 partial write semantics. - \cargo clippy -p victory-shader\ clean with zero warnings. - \cargo xtask policy\ passes (151 files checked, zero asset policy violations).
Lyrnc commented 2026-11-24 06:56:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-24T05:53:50Z.


RDNA2 hardware measurement: the saturation is right, but the high half is zeroed on the hardware, not kept.

Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. A HIP kernel (assembled by the AMD driver's amd_comgr) sets the destination to 0xdeadbeef, then runs v_sat_pk_u8_i16 v10, v4 (VOP1) and, separately, v_sat_pk_u8_i16_e64 v11, v4 (VOP3) on 72 packed inputs: every pair of the halves 0x0000 0x0001 0x007f 0x0080 0x00ff 0x0100 0x7fff 0x8000 0xffff 0xff80 0x0123 0xfedc against the first six. RDNA2 desktop silicon, not the console's. No game data.

src0 VOP1 result VOP3 result
0x00ff0001 0x0000ff01 0x0000ff01
0x00000100 0x000000ff 0x000000ff
0x01000100 0x0000ffff 0x0000ffff
0x00007fff 0x000000ff 0x000000ff
0x00008000 0x00000000 0x00000000
0x00808000 0x00008000 0x00008000
0x0000ffff 0x00000000 0x00000000
0x0000ff80 0x00000000 0x00000000
0x01000123 0x0000ffff 0x0000ffff
  • Low 16 bits: each signed half saturated to [0, 255], src0[15:0] into bits 7:0 and src0[31:16] into bits 15:8. This matches your lowering on all 72 rows.
  • High 16 bits: 0x0000 on all 72 rows in both encodings. The 0xdeadbeef that was there is gone. Your test expects 0xdead to survive in the high half, so on this hardware it would be the wrong value.

For contrast, the same kernel shape with v_cvt_f16_f32 does keep the high half: 0xdead0000 stays 0xdead…. on 34 of 34 inputs. So the v_cvt_f16_f32 partial write you modelled this on does not carry over to v_sat_pk_u8_i16 on RDNA2. The fix would be to write the 16-bit result zero-extended, as a full 32-bit write, instead of through low_half.

AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7123377192 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/499#issuecomment-7123377192). Originally posted by [Lyrnc](https://github.com/Lyrnc) on 2026-11-24T05:53:50Z. --- RDNA2 hardware measurement: **the saturation is right, but the high half is zeroed on the hardware, not kept.** **Setup.** AMD Radeon iGPU of a Ryzen 7 9800X3D, **gfx1036 (RDNA2)**, AMD driver 32.0.21042.62, Windows 11 Pro 26200. A HIP kernel (assembled by the AMD driver's amd_comgr) sets the destination to `0xdeadbeef`, then runs `v_sat_pk_u8_i16 v10, v4` (VOP1) and, separately, `v_sat_pk_u8_i16_e64 v11, v4` (VOP3) on 72 packed inputs: every pair of the halves `0x0000 0x0001 0x007f 0x0080 0x00ff 0x0100 0x7fff 0x8000 0xffff 0xff80 0x0123 0xfedc` against the first six. RDNA2 desktop silicon, not the console's. No game data. | src0 | VOP1 result | VOP3 result | |---|---|---| | `0x00ff0001` | `0x0000ff01` | `0x0000ff01` | | `0x00000100` | `0x000000ff` | `0x000000ff` | | `0x01000100` | `0x0000ffff` | `0x0000ffff` | | `0x00007fff` | `0x000000ff` | `0x000000ff` | | `0x00008000` | `0x00000000` | `0x00000000` | | `0x00808000` | `0x00008000` | `0x00008000` | | `0x0000ffff` | `0x00000000` | `0x00000000` | | `0x0000ff80` | `0x00000000` | `0x00000000` | | `0x01000123` | `0x0000ffff` | `0x0000ffff` | - **Low 16 bits:** each signed half saturated to `[0, 255]`, src0[15:0] into bits 7:0 and src0[31:16] into bits 15:8. This matches your lowering on all 72 rows. - **High 16 bits:** `0x0000` on all 72 rows in both encodings. The `0xdeadbeef` that was there is gone. Your test expects `0xdead` to survive in the high half, so on this hardware it would be the wrong value. For contrast, the same kernel shape with `v_cvt_f16_f32` does keep the high half: `0xdead0000` stays `0xdead….` on 34 of 34 inputs. So the `v_cvt_f16_f32` partial write you modelled this on does not carry over to `v_sat_pk_u8_i16` on RDNA2. The fix would be to write the 16-bit result zero-extended, as a full 32-bit write, instead of through `low_half`. AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.
slint6614-dev commented 2026-11-24 11:04:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by slint6614-dev on 2026-11-24T10:42:31Z.


Measured on the full retail shader pack (26,503 containers) with victory translate --validate --pipelines (pipelines on an AMD RX 9070, Windows 11, dxil.dll from SDK 10.0.26100). Counts are emitted / validated / pipelines. Merged locally with current main c7a5f49 (main: CS 1440 / 1417).

CS 1441 / 1418 (+1 validated); VS/PS and MS unchanged.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7129138842 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/499#issuecomment-7129138842). Originally posted by [slint6614-dev](https://github.com/slint6614-dev) on 2026-11-24T10:42:31Z. --- Measured on the full retail shader pack (26,503 containers) with `victory translate --validate --pipelines` (pipelines on an AMD RX 9070, Windows 11, dxil.dll from SDK 10.0.26100). Counts are emitted / validated / pipelines. Merged locally with current main c7a5f49 (main: CS 1440 / 1417). CS 1441 / **1418** (+1 validated); VS/PS and MS unchanged.
mercuryVM commented 2026-11-24 13:01:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T12:23:06Z.

Review: APPROVED


Tested 5c211d0 on Windows 11, RTX 5070, MSVC: cargo test -p victory-shader -- --test-threads=1 passes as part of the #576 stack (72 passed), and cargo clippy -p victory-shader --all-targets is clean. victory translate --validate --pipelines on the local title executable still reports 12 of 12 programs. This commit is on mercuryVM/VIctory main via #576.

  • v_sat_pk_u8_i16 matches the ISA: sat8 of the low i16 in byte 0, sat8 of the high i16 in byte 1, and the register's high half kept as a GFX10 16-bit write. op_sel is refused instead of guessed.
  • sat_pk_u8_packs_the_clamped_halves only checks that the program lowers. A run_cases value check with -1, 0x7FFF and 0x0100 per half would pin the clamps.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6039118048 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/499#pullrequestreview-6039118048). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T12:23:06Z. **Review:** APPROVED --- Tested 5c211d0 on Windows 11, RTX 5070, MSVC: `cargo test -p victory-shader -- --test-threads=1` passes as part of the #576 stack (72 passed), and `cargo clippy -p victory-shader --all-targets` is clean. `victory translate --validate --pipelines` on the local title executable still reports 12 of 12 programs. This commit is on mercuryVM/VIctory main via #576. - `v_sat_pk_u8_i16` matches the ISA: sat8 of the low i16 in byte 0, sat8 of the high i16 in byte 1, and the register's high half kept as a GFX10 16-bit write. `op_sel` is refused instead of guessed. - `sat_pk_u8_packs_the_clamped_halves` only checks that the program lowers. A `run_cases` value check with -1, 0x7FFF and 0x0100 per half would pin the clamps. AI-assisted (Claude Code).
catminou4 commented 2026-11-24 13:01:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T12:38:30Z.

Review: COMMENTED


Test-merge with current main (ffe310e) conflicts — needs a rebase. Main moved recently (PR #566 waterfall loops touched lower.rs/resource.rs/valu_tests.rs), so shader PRs opened before it need rebasing. I'll re-review once it merges cleanly.

<!-- greyfinch-sync:Marvisak/VIctory:review:6039322764 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/499#pullrequestreview-6039322764). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T12:38:30Z. **Review:** COMMENTED --- Test-merge with current main (`ffe310e`) **conflicts** — needs a rebase. Main moved recently (PR #566 waterfall loops touched `lower.rs`/`resource.rs`/`valu_tests.rs`), so shader PRs opened before it need rebasing. I'll re-review once it merges cleanly.
Marvisak commented 2026-11-25 01:07:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:21:45Z.


Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-shader/src/emit/lower.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148429640 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/499#issuecomment-7148429640). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:21:45Z. --- <!-- victory-triage v1 head=5c211d04035c11fa47a267dbb9ded2f12d62a47c verdict=needs-rebase at=2026-11-25T00:13:20Z --> Automated triage for the maintainers. **Needs rebase.** This PR does not merge cleanly into current `main` (ac2525d). Conflicting files: `crates/victory-shader/src/emit/lower.rs`. The content has not been reviewed yet; it will be once the branch is rebased onto `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
andrewpozdnakov7-jpg commented 2026-11-25 12:03:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-25T11:35:12Z.


I separated the existing v_sat_pk_u8_i16 lowering from the #498 stack and replayed it unchanged on 940d32f. bd0df4c retains the original Git author.

The original smoke fixture encodes 0x7e08c501, which writes v4, while the LDS store reads v2. 24cbda9 uses the VOP1/VOP3 encoders and reads back the instruction's actual destination. The test was Unsupported on 940d32f. On the rebase, all ten Windows/WARP cases pass, checking both encodings, signed clamps at 0 and 255, and preservation of the upper 16 bits. All 29 VALU tests passed with Microsoft DXIL validation. Strict shader Clippy and the staged asset policy passed.

If you fold the readback test into a different commit, please retain Co-authored-by: andrewpozdnakov7-jpg <[email protected]>. Claim

<!-- greyfinch-sync:Marvisak/VIctory:comment:7161238562 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/499#issuecomment-7161238562). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-25T11:35:12Z. --- I separated the existing `v_sat_pk_u8_i16` lowering from the #498 stack and replayed it unchanged on `940d32f`. [`bd0df4c`](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/bd0df4c8417fc6b567e5772b95233fb856224d66) retains the original Git author. The original smoke fixture encodes `0x7e08c501`, which writes `v4`, while the LDS store reads `v2`. [`24cbda9`](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/24cbda9372833870373784114264d6cdb69d386e) uses the VOP1/VOP3 encoders and reads back the instruction's actual destination. The test was `Unsupported` on `940d32f`. On the rebase, all ten Windows/WARP cases pass, checking both encodings, signed clamps at 0 and 255, and preservation of the upper 16 bits. All 29 VALU tests passed with Microsoft DXIL validation. Strict shader Clippy and the staged asset policy passed. If you fold the readback test into a different commit, please retain `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>`. [Claim](https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7161174286)
andrewpozdnakov7-jpg commented 2026-11-26 11:04:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-26T10:28:00Z.


I corrected the upper-half expectation from my earlier WARP handoff using Lyrnc's RDNA2 hardware report. The new replay packs the two saturated bytes and writes a full dword with bits 31:16 cleared.

Branch on current main 63bcca4. d69b7ac retains the original Git author and a31a726 adds the readback test. On WARP, all 81 pairs passed in both VOP1 and VOP3 with a nonzero destination prefill. All 36 VALU tests, strict shader Clippy, and the asset policy passed.

Please retain the Git authors when taking the commits. If folding in the readback test, please add Co-authored-by: andrewpozdnakov7-jpg <[email protected]>. AI-assisted with Codex.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7189403072 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/499#issuecomment-7189403072). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-26T10:28:00Z. --- I corrected the upper-half expectation from my earlier WARP handoff using [Lyrnc's RDNA2 hardware report](https://github.com/Marvisak/VIctory/pull/499#issuecomment-7123377192). The new replay packs the two saturated bytes and writes a full dword with bits 31:16 cleared. [Branch on current main `63bcca4`](https://github.com/andrewpozdnakov7-jpg/VIctory/tree/followup/499-sat-pack-u8). [`d69b7ac`](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/d69b7ac33e89cd42207572b1444eeeceff72ee6c) retains the original Git author and [`a31a726`](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/a31a726eca6ccffdf29d9d3f51ffda7e0ce4dbdf) adds the readback test. On WARP, all 81 pairs passed in both VOP1 and VOP3 with a nonzero destination prefill. All 36 VALU tests, strict shader Clippy, and the asset policy passed. Please retain the Git authors when taking the commits. If folding in the readback test, please add `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>`. AI-assisted with Codex.
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/499:upstream-pr/499'
git switch 'upstream-pr/499'

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/499'
git switch 'upstream-pr/499'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/499'
git switch 'upstream-pr/499'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/499'
git switch 'main'
git merge --squash 'upstream-pr/499'
git switch 'main'
git merge --ff-only 'upstream-pr/499'
git switch 'main'
git merge 'upstream-pr/499'
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
Marvisak/VIctory!499
No description provided.