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

Closed
lcg007xd-c wants to merge 0 commits from upstream-pr/65 into main
lcg007xd-c commented 2026-11-25 21:32:38 +00:00 (Migrated from github.com)

Imported from uiliannn/VItoriaBr. Originally posted by lcg007xd-c on 2026-11-25T21:32:38Z.


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

<!-- greyfinch-sync:uiliannn/VItoriaBr:issue:5499530193 --> Imported from [uiliannn/VItoriaBr](https://github.com/uiliannn/VitoriaBr2.0/pull/65). Originally posted by [lcg007xd-c](https://github.com/lcg007xd-c) on 2026-11-25T21:32:38Z. --- ## 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)

Pull request closed

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
uiliannn/VItoriaBr!65
No description provided.