shader: lower v_mul_lo_i32 and v_mad_i64_i32 #82

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

@⁠lcg007xd-c on GitHub · 2026-11-25 22:07 UTC · original
lcg007xd-c:shader-signed-mul → main · +119 −1 · 2 files · 1 commits · open

What is broken

v_mul_lo_i32 (VOP3 0x16B) and v_mad_i64_i32 (VOP3B 0x177) decode, but a program using either fails to translate with v_mul_lo_i32 is not supported (or v_mad_i64_i32 …). They are the signed counterparts of v_mul_lo_u32 and v_mad_u64_u32, which already lower. I found them by translating a one-instruction program for every VOP3 opcode the decoder names. LLVM emits v_mad_i64_i32 for 32×32→64 signed multiplies, so it appears in any 64-bit index math on signed values.

Semantics (AMD's RDNA2 ISA, §12.12, opcodes 363 and 375)

  • v_mul_lo_i32: D.i = S0.i * S1.i, low 32 bits. These are the same bits as v_mul_lo_u32.
  • v_mad_i64_i32: {vcc_out, D.i64} = S0.i32 * S1.i32 + S2.i64. The product is sign-extended, and S2 is a signed 64-bit operand.

The change (emit/lower.rs)

  • v_mul_lo_i32: joins v_mul_lo_u32's arm in integer.
  • v_mad_i64_i32 (in valu_op): the operands are sign-extended to i64, multiplied, and added to S2. The result is written to the VGPR pair.
    • Literals: a 32-bit literal S2 is sign-extended, as #41 does for the _i64 forms. src64's name test (ends_with("_i64")) does not match v_mad_i64_i32, so this arm does it.
    • The scalar carry-out is left unwritten. The ISA does not define what it means for the signed form. A program that reads it is refused (a lane mask from a carry is not supported) rather than given a guess. Compilers put null there.

Tests

  • emit::lower::tests::signed_multiplies_translate (every host):

    • both decode, and a program that stores the result translates, with the carry-out to null and to an SGPR pair nobody reads;
    • reading v_mad_i64_i32's carry-out is refused with a carry message, while the same read of v_mad_u64_u32's still translates.

    Without the lower.rs change it fails: v_mul_lo_i32 [d56b0000, 00020501]: GFX10 code at 0x24: v_mul_lo_i32 is not supported.

  • valu_tests::signed_multiplies_match_rdna2 (Windows/GPU): v_mul_lo_i32 on −3×7 and 0x10001². v_mad_i64_i32 is checked on both result dwords: −1×5, i32::MIN² = 2⁶², i32::MAX×−1 + 10, and 3×4 + 0xFFFFFFFF carrying into the high dword.

  • CPU run: I ran these on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's lli. I compared against Rust's wrapping_mul/wrapping_add on all 81 pairs of 9 edge values, times 5 accumulators for v_mad_i64_i32, on both dwords. I also checked a literal −2 accumulator (sign-extended: 1·1 − 2 = 0xFFFFFFFF_FFFFFFFF). 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 22:07 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/82) `lcg007xd-c:shader-signed-mul` → `main` · +119 −1 · 2 files · 1 commits · open ## What is broken `v_mul_lo_i32` (VOP3 **0x16B**) and `v_mad_i64_i32` (VOP3B **0x177**) decode, but a program using either fails to translate with `v_mul_lo_i32 is not supported` (or `v_mad_i64_i32 …`). They are the signed counterparts of `v_mul_lo_u32` and `v_mad_u64_u32`, which already lower. I found them by translating a one-instruction program for every VOP3 opcode the decoder names. LLVM emits `v_mad_i64_i32` for 32×32→64 signed multiplies, so it appears in any 64-bit index math on signed values. ## Semantics (AMD's RDNA2 ISA, §12.12, opcodes 363 and 375) - **`v_mul_lo_i32`:** `D.i = S0.i * S1.i`, low 32 bits. These are the same bits as `v_mul_lo_u32`. - **`v_mad_i64_i32`:** `{vcc_out, D.i64} = S0.i32 * S1.i32 + S2.i64`. The product is sign-extended, and S2 is a signed 64-bit operand. ## The change (`emit/lower.rs`) - **`v_mul_lo_i32`:** joins `v_mul_lo_u32`'s arm in `integer`. - **`v_mad_i64_i32`** (in `valu_op`): the operands are sign-extended to i64, multiplied, and added to S2. The result is written to the VGPR pair. - **Literals:** a 32-bit literal S2 is **sign**-extended, as #41 does for the `_i64` forms. `src64`'s name test (`ends_with("_i64")`) does not match `v_mad_i64_i32`, so this arm does it. - **The scalar carry-out is left unwritten.** The ISA does not define what it means for the signed form. A program that reads it is refused (`a lane mask from a carry is not supported`) rather than given a guess. Compilers put `null` there. ## Tests - **`emit::lower::tests::signed_multiplies_translate`** (every host): - both decode, and a program that **stores** the result translates, with the carry-out to `null` and to an SGPR pair nobody reads; - reading `v_mad_i64_i32`'s carry-out is refused with a carry message, while the same read of `v_mad_u64_u32`'s still translates. Without the `lower.rs` change it fails: `v_mul_lo_i32 [d56b0000, 00020501]: GFX10 code at 0x24: v_mul_lo_i32 is not supported`. - **`valu_tests::signed_multiplies_match_rdna2`** (Windows/GPU): `v_mul_lo_i32` on −3×7 and 0x10001². `v_mad_i64_i32` is checked on both result dwords: −1×5, `i32::MIN²` = 2⁶², `i32::MAX`×−1 + 10, and 3×4 + `0xFFFFFFFF` carrying into the high dword. - **CPU run:** I ran these on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's `lli`. I compared against Rust's `wrapping_mul`/`wrapping_add` on all 81 pairs of 9 edge values, times 5 accumulators for `v_mad_i64_i32`, on both dwords. I also checked a literal `−2` accumulator (sign-extended: `1·1 − 2 = 0xFFFFFFFF_FFFFFFFF`). 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 0x16B and VOP3B 0x177 decoded but failed to translate
("v_mul_lo_i32 is not supported"): the signed counterparts of
v_mul_lo_u32 and v_mad_u64_u32, which already lower.

v_mul_lo_i32's low 32 bits are v_mul_lo_u32's. v_mad_i64_i32 is
S0.i32 * S1.i32 + S2.i64 (AMD's RDNA2 ISA) with the product
sign-extended; S2 is a signed 64-bit operand, so a 32-bit literal
sign-extends. Its scalar carry-out is left unwritten, so a program that
reads it is refused ("a lane mask from a carry") instead of reading a
guess; compilers name null there.

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:29 +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/82/head:github-bot/gh-82'
git switch 'github-bot/gh-82'

Merge

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