shader: lower v_min3/v_max3/v_med3 on 16-bit integers #65

Open
github-bot wants to merge 1 commit from github-bot/gh-65 into main AGit
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 21:32 UTC · original
lcg007xd-c:shader-int16-min3-max3-med3 → main · +115 −0 · 2 files · 1 commits · open

What is broken

VOP3 0x352–0x359 decode, but none of them translates:

Opcode Instruction
0x352 v_min3_i16
0x353 v_min3_u16
0x355 v_max3_i16
0x356 v_max3_u16
0x358 v_med3_i16
0x359 v_med3_u16

A program using any of them fails with v_min3_i16 is not supported (and likewise for the others). I found these by translating a one-instruction program for every VOP3 opcode the decoder names. The 32-bit integer and f16 forms already lower; only the 16-bit integer forms were missing.

The change (emit/lower.rs, in valu16_op beside the other 16-bit integer operations)

It follows AMD's RDNA2 ISA (§12.12, opcodes 850–857):

  • Sources: the three sources are their low halves, or the high halves op_sel picks. For _i16 they are sign-extended.
  • min3/max3: two min/max steps.
  • med3: the middle value, computed as max(min(a, b), min(max(a, b), c)). The ISA's form (find the max of the three, then take the max of the other two) gives the same value for integers.
  • Result: written to the destination's low half, or its high half with op_sel[3]. The other half is kept, as for every 16-bit VOP3 result on GFX10 (write_half).
  • Modifiers: clamp and the other modifiers are refused, as for the other 16-bit integer operations.

Tests

  • emit::lower::tests::int16_min3_max3_med3_translate (every host): each encoding decodes to its name, and a program that stores the result translates. The store keeps the instruction from being dropped as dead code. Without the lower.rs change it fails at v_min3_i16.
  • valu_tests::v_min3_max3_med3_16bit_integer_forms_match_rdna2 (Windows/GPU): three hand-computed cases per opcode. They include 0x8000 as both −32768 and 32768, 0xFFFF as −1 and 65535, and a tie. The inputs' high halves are non-zero so a lowering that reads them would fail.
  • CPU run: I ran the same cases on Linux through the CPU harness of #62 (LLVM opt + lli running the emitted DXIL), plus one op_sel case (src0 and src2 from the high halves, result to the high half). All match.

Run here (Linux, Rust 1.98.1):

  • cargo test -p victory-shader --lib --locked: 188 passed, 0 failed, 2 ignored.
  • Portable clippy and CARGO_FEATURE_PURE=1 cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings: clean.

Not run here: the GPU case, since I have no Windows machine.

🤖 Generated with Claude Code

**@⁠lcg007xd-c** on GitHub · 2026-11-25 21:32 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/65) `lcg007xd-c:shader-int16-min3-max3-med3` → `main` · +115 −0 · 2 files · 1 commits · open ## What is broken VOP3 **0x352–0x359** decode, but none of them translates: | Opcode | Instruction | |---|---| | 0x352 | `v_min3_i16` | | 0x353 | `v_min3_u16` | | 0x355 | `v_max3_i16` | | 0x356 | `v_max3_u16` | | 0x358 | `v_med3_i16` | | 0x359 | `v_med3_u16` | A program using any of them fails with `v_min3_i16 is not supported` (and likewise for the others). I found these by translating a one-instruction program for every VOP3 opcode the decoder names. The 32-bit integer and f16 forms already lower; only the 16-bit integer forms were missing. ## The change (`emit/lower.rs`, in `valu16_op` beside the other 16-bit integer operations) It follows AMD's RDNA2 ISA (§12.12, opcodes 850–857): - **Sources:** the three sources are their low halves, or the high halves `op_sel` picks. For `_i16` they are sign-extended. - **`min3`/`max3`:** two min/max steps. - **`med3`:** the middle value, computed as `max(min(a, b), min(max(a, b), c))`. The ISA's form (find the max of the three, then take the max of the other two) gives the same value for integers. - **Result:** written to the destination's low half, or its high half with `op_sel[3]`. The other half is kept, as for every 16-bit VOP3 result on GFX10 (`write_half`). - **Modifiers:** clamp and the other modifiers are refused, as for the other 16-bit integer operations. ## Tests - **`emit::lower::tests::int16_min3_max3_med3_translate`** (every host): each encoding decodes to its name, and a program that **stores** the result translates. The store keeps the instruction from being dropped as dead code. Without the `lower.rs` change it fails at `v_min3_i16`. - **`valu_tests::v_min3_max3_med3_16bit_integer_forms_match_rdna2`** (Windows/GPU): three hand-computed cases per opcode. They include `0x8000` as both −32768 and 32768, `0xFFFF` as −1 and 65535, and a tie. The inputs' high halves are non-zero so a lowering that reads them would fail. - **CPU run:** I ran the same cases on Linux through the CPU harness of #62 (LLVM `opt` + `lli` running the emitted DXIL), plus one `op_sel` case (src0 and src2 from the high halves, result to the high half). All match. **Run here** (Linux, Rust 1.98.1): - `cargo test -p victory-shader --lib --locked`: 188 passed, 0 failed, 2 ignored. - Portable clippy and `CARGO_FEATURE_PURE=1 cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings`: clean. **Not run here:** the GPU case, since I have no Windows machine. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
VOP3 0x352..=0x359 (v_min3_i16, v_min3_u16, v_max3_i16, v_max3_u16,
v_med3_i16, v_med3_u16) decoded but failed to translate ("v_min3_i16 is
not supported"). They now lower beside the other 16-bit integer
operations: the three low (or op_sel-selected) halves, sign-extended for
_i16, compared as the RDNA2 ISA defines (med3 is the middle value), and
the result written to the destination's selected half, the other half
kept.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:42:55 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/65/head:github-bot/gh-65'
git switch 'github-bot/gh-65'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-65'
git switch 'github-bot/gh-65'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-65'
git switch 'github-bot/gh-65'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-65'
git switch 'main'
git merge --squash 'github-bot/gh-65'
git switch 'main'
git merge --ff-only 'github-bot/gh-65'
git switch 'main'
git merge 'github-bot/gh-65'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.