shader: lower v_mad_u32_u16, v_mad_i32_i16, v_mad_u16 and v_mad_i16 #73

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

@⁠lcg007xd-c on GitHub · 2026-11-25 21:41 UTC · original
lcg007xd-c:shader-mad-16bit → main · +144 −0 · 2 files · 1 commits · open

What is broken

The 16-bit multiply-adds decode but do not translate:

Opcode Instruction
VOP3 0x373 v_mad_u32_u16
VOP3 0x375 v_mad_i32_i16
VOP3 0x340 v_mad_u16
VOP3 0x35E v_mad_i16

A program using one fails with v_mad_u32_u16 is not supported (and likewise for the others). I found them by translating a one-instruction program for every VOP3 opcode the decoder names. The 24-bit forms (v_mad_u32_u24, v_mad_i32_i24) already lower.

Semantics (AMD's RDNA2 ISA, §12.12, opcodes 832, 862, 883, 885)

  • v_mad_u32_u16: D.u32 = S0.u16 * S1.u16 + S2.u32. v_mad_i32_i16 is the same with signed 16-bit factors and a 32-bit sum. op_sel picks which half of src0 and src1 is read.
  • v_mad_u16: D.u16 = S0.u16 * S1.u16 + S2.u16, which "supports saturation (… 16-bit integer domain)". v_mad_i16 is the same with signed values. The 16-bit result goes to the half op_sel[3] selects, and the other half is kept.

The change (emit/lower.rs)

  • v_mad_u32_u16 / v_mad_i32_i16 (in integer, beside the 24-bit forms): the two halves come from half_src, which honours op_sel, and are sign-extended for the signed form. The sum wraps. Clamp is still refused here, as for the other 32-bit integer operations.
  • v_mad_u16 / v_mad_i16 (in valu16_op): three halves, sign-extended for _i16. The exact sum fits in 32 bits: at most 0xFFFE0001 + 0xFFFF unsigned, and within ±2³⁰ + 2¹⁵ signed. So clamp compares the exact value with the 16-bit limits. Then write_half writes it.

Tests

  • emit::lower::tests::sixteen_bit_multiply_adds_translate (every host): each encoding decodes to its name, and a program that stores the result translates. The 16-bit-result forms are also tried with clamp. Without the lower.rs change it fails: v_mad_u32_u16 clamp 0: GFX10 code at 0x24: v_mad_u32_u16 is not supported.
  • valu_tests::sixteen_bit_multiply_adds_match_rdna2 (Windows/GPU): two hand-computed cases per opcode, the 16-bit-result ones with and without clamp. They cover 0xFFFF * 0xFFFF, a wrapping 32-bit sum, −1 × −32768, and saturation at both 16-bit ends. Every source has junk in the high half it must not read.
  • CPU run: I ran the same cases on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's lli. I added three op_sel cases: high halves of src0/src1 for both 32-bit forms, and v_mad_i16 reading src2's high half and writing v0's high half with clamp. I also checked clamped v_mad_i16/v_mad_u16 against a Rust reference on a 9 × 11 grid. 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. The test and the GPU case sit at different places in their files from those of #65 and #71, so the three merge in any order.

🤖 Generated with Claude Code

**@⁠lcg007xd-c** on GitHub · 2026-11-25 21:41 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/73) `lcg007xd-c:shader-mad-16bit` → `main` · +144 −0 · 2 files · 1 commits · open ## What is broken The 16-bit multiply-adds decode but do not translate: | Opcode | Instruction | |---|---| | VOP3 0x373 | `v_mad_u32_u16` | | VOP3 0x375 | `v_mad_i32_i16` | | VOP3 0x340 | `v_mad_u16` | | VOP3 0x35E | `v_mad_i16` | A program using one fails with `v_mad_u32_u16 is not supported` (and likewise for the others). I found them by translating a one-instruction program for every VOP3 opcode the decoder names. The 24-bit forms (`v_mad_u32_u24`, `v_mad_i32_i24`) already lower. ## Semantics (AMD's RDNA2 ISA, §12.12, opcodes 832, 862, 883, 885) - **`v_mad_u32_u16`:** `D.u32 = S0.u16 * S1.u16 + S2.u32`. `v_mad_i32_i16` is the same with signed 16-bit factors and a 32-bit sum. `op_sel` picks which half of src0 and src1 is read. - **`v_mad_u16`:** `D.u16 = S0.u16 * S1.u16 + S2.u16`, which "supports saturation (… 16-bit integer domain)". `v_mad_i16` is the same with signed values. The 16-bit result goes to the half `op_sel[3]` selects, and the other half is kept. ## The change (`emit/lower.rs`) - **`v_mad_u32_u16` / `v_mad_i32_i16`** (in `integer`, beside the 24-bit forms): the two halves come from `half_src`, which honours `op_sel`, and are sign-extended for the signed form. The sum wraps. Clamp is still refused here, as for the other 32-bit integer operations. - **`v_mad_u16` / `v_mad_i16`** (in `valu16_op`): three halves, sign-extended for `_i16`. The exact sum fits in 32 bits: at most `0xFFFE0001 + 0xFFFF` unsigned, and within ±2³⁰ + 2¹⁵ signed. So clamp compares the exact value with the 16-bit limits. Then `write_half` writes it. ## Tests - **`emit::lower::tests::sixteen_bit_multiply_adds_translate`** (every host): each encoding decodes to its name, and a program that **stores** the result translates. The 16-bit-result forms are also tried with clamp. Without the `lower.rs` change it fails: `v_mad_u32_u16 clamp 0: GFX10 code at 0x24: v_mad_u32_u16 is not supported`. - **`valu_tests::sixteen_bit_multiply_adds_match_rdna2`** (Windows/GPU): two hand-computed cases per opcode, the 16-bit-result ones with and without clamp. They cover `0xFFFF * 0xFFFF`, a wrapping 32-bit sum, −1 × −32768, and saturation at both 16-bit ends. Every source has junk in the high half it must not read. - **CPU run:** I ran the same cases on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's `lli`. I added three `op_sel` cases: high halves of src0/src1 for both 32-bit forms, and `v_mad_i16` reading src2's high half and writing v0's high half with clamp. I also checked clamped `v_mad_i16`/`v_mad_u16` against a Rust reference on a 9 × 11 grid. 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. The test and the GPU case sit at different places in their files from those of #65 and #71, so the three merge in any order. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
VOP3 0x373, 0x375, 0x340 and 0x35E decoded but failed to translate
("v_mad_u32_u16 is not supported"). As AMD's RDNA2 ISA defines them:
v_mad_u32_u16/v_mad_i32_i16 multiply two 16-bit halves (op_sel picks
them; sign-extended for the signed one) and add a 32-bit src2, wrapping.
v_mad_u16/v_mad_i16 add a 16-bit src2 and write a 16-bit half; with
clamp they saturate to the 16-bit range. The exact sums fit in 32 bits,
so the clamp compares the exact value.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:43:10 +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/73/head:github-bot/gh-73'
git switch 'github-bot/gh-73'

Merge

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