shader: lower v_mul_lo_i32 and v_mad_i64_i32 #82

Closed
lcg007xd-c wants to merge 0 commits from upstream-pr/82 into main
lcg007xd-c commented 2026-11-25 22:07:45 +00:00 (Migrated from github.com)

Imported from uiliannn/VItoriaBr. Originally posted by lcg007xd-c on 2026-11-25T22:07:45Z.


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

<!-- greyfinch-sync:uiliannn/VItoriaBr:issue:5500004370 --> Imported from [uiliannn/VItoriaBr](https://github.com/uiliannn/VitoriaBr2.0/pull/82). Originally posted by [lcg007xd-c](https://github.com/lcg007xd-c) on 2026-11-25T22:07:45Z. --- ## 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)
lmouz823-lang commented 2026-11-28 08:26:20 +00:00 (Migrated from github.com)

Imported from uiliannn/VItoriaBr. Originally posted by lmouz823-lang on 2026-11-28T08:26:20Z.


I can take native Windows signed-multiply validation at exact head f4242ab1cd48428ac3f2cdf5bd42f840247ad30c. I will run the existing translation/refusal and GPU regressions, then add focused fixed-expectation coverage for signed literal accumulators and result-dword boundaries. The signed carry-read refusal and unsigned control will remain part of acceptance. A targeted negative control, fresh restored-source runs, SDK validation evidence and explicit skipped/native AMD/game scope will accompany the result. AI-assisted with Codex.

<!-- greyfinch-sync:uiliannn/VItoriaBr:comment:7236583000 --> Imported from [uiliannn/VItoriaBr](https://github.com/uiliannn/VItoriaBr/pull/82#issuecomment-7236583000). Originally posted by [lmouz823-lang](https://github.com/lmouz823-lang) on 2026-11-28T08:26:20Z. --- I can take native Windows signed-multiply validation at exact head `f4242ab1cd48428ac3f2cdf5bd42f840247ad30c`. I will run the existing translation/refusal and GPU regressions, then add focused fixed-expectation coverage for signed literal accumulators and result-dword boundaries. The signed carry-read refusal and unsigned control will remain part of acceptance. A targeted negative control, fresh restored-source runs, SDK validation evidence and explicit skipped/native AMD/game scope will accompany the result. AI-assisted with Codex.
lmouz823-lang commented 2026-11-28 08:54:20 +00:00 (Migrated from github.com)

Imported from uiliannn/VItoriaBr. Originally posted by lmouz823-lang on 2026-11-28T08:54:20Z.


Windows validation of f4242ab

I checked head f4242ab1cd48428ac3f2cdf5bd42f840247ad30c against base 081a89d2e5767beb38c94e2f4adc7eeca0ad69db and prepared a small additional regression test.

Environment and execution

  • Windows 11 Pro build 26200, Rust 1.98.1 x86_64-pc-windows-msvc, release profile, locked dependencies.
  • NVIDIA GeForce RTX 4060 Laptop GPU 8 GB, driver 610.88, plus Microsoft Basic Render Driver through WARP. Both adapters actually executed the selected GPU cases.
  • VICTORY_STRICT_TESTS=1, explicit VICTORY_DXIL pointing to Windows SDK 10.0.26100.0 x64 dxil.dll, file version 1.8.2502.11. A temporary diagnostic run recorded successful validator load, validation, and container-hash checks for all 3 original and 4 added shader modules. The submitted patch contains the regression test.
Exact test Final result Dispatches across both adapters
emit::lower::tests::signed_multiplies_translate 1 passed, 0 failed, 0 ignored CPU translation
emit::valu_tests::signed_multiplies_match_rdna2 1 passed, 0 failed, 0 ignored 20
emit::valu_tests::signed_mad_wraps_and_sign_extends_literal_accumulators 1 passed, 0 failed, 0 ignored 40

Each command used cargo test --release --locked -p victory-shader --lib <exact-name> -- --exact --nocapture --test-threads=1. Each selected exactly one test. No selected final case reported a skip.

The existing translation test continues to accept null and unread signed carry destinations, refuse a read of signed MAD carry-out, and accept the unsigned MAD carry-read control.

Additional coverage and negative control

The attached 55-line test adds fixed expected bit patterns for signed 64-bit wraparound, register accumulator sign boundaries, and literal 0xFFFFFFFE sign extension. It reads both destination dwords through v[5:6], preserving v1..v4 inputs. Register cases account for 2 dwords × 6 inputs × 2 backends = 24 dispatches, and literal cases for 2 × 4 × 2 = 16.

The result-dword calculations follow signed 32-bit multiplication and a signed 64-bit accumulator. The i64 literal conversion is also documented as sign extension from 32 bits. AMD RDNA2 ISA, V_MAD_I64_I32, LLVM AMDGPU integer operand conversion

For a targeted negative control, I temporarily changed only the signed MAD literal accumulator conversion from i64::from(l as i32) to i64::from(l). The original GPU test still passed on both adapters. The added test failed on NVIDIA at literal -2, input [1, 1, 0, 0], high result dword, with actual 0x00000000 and expected 0xFFFFFFFF and cargo exit 101. This establishes that the added coverage detects that specific regression. The deliberate failure stopped later negative cases and the WARP negative run.

I then restored lower.rs byte-for-byte, removed diagnostic logging, cleaned only the isolated victory-shader release outputs, and rebuilt the final test binary in 1m41s. All three final exact tests above passed after that real rebuild.

Static checks and scope

  • Release Clippy with --all-targets -- -D warnings, the 195-file asset policy check, and git diff --check passed.
  • The added function passes the repository-configured rustfmt check. Whole-file rustfmt exits 1 on both the untouched head and final file, with the same 20 pre-existing diff hunks after path/line headers are removed.
  • Native AMD execution, actual gameplay, and the full shader/workspace suites remain untested. Numeric signed carry-out semantics remain unverified. This run checks the PR's existing refusal boundary for reading it.

The patch was prepared with AI assistance and checked locally. Patch SHA-256 d3f8cd7a50d44f9fe2efc307f7779d931c792425b5e44385afd56f55768da994.

Complete regression-test patch
diff --git a/crates/victory-shader/src/emit/valu_tests.rs b/crates/victory-shader/src/emit/valu_tests.rs
index 92de739..a8e7424 100644
--- a/crates/victory-shader/src/emit/valu_tests.rs
+++ b/crates/victory-shader/src/emit/valu_tests.rs
@@ -1019,6 +1019,61 @@ fn signed_multiplies_match_rdna2() {
     }
 }
 
+#[test]
+fn signed_mad_wraps_and_sign_extends_literal_accumulators() {
+    // v_mad_i64_i32 v[5:6], null, v1, v2, v[3:4].
+    let mad = [
+        0x35 << 26 | 0x177 << 16 | 125 << 8 | 5,
+        0x101 | 0x102 << 9 | 0x103 << 18,
+    ];
+    let register_cases: [([u32; 4], [u32; 2]); 6] = [
+        (
+            [0x8000_0000, u32::MAX, u32::MAX, 0x7FFF_FFFF],
+            [0x7FFF_FFFF, 0x8000_0000],
+        ),
+        ([0x8000_0000, 0x8000_0000, 0, 0x4000_0000], [0, 0x8000_0000]),
+        ([0x7FFF_FFFF, 0x7FFF_FFFF, u32::MAX, 0xC000_0000], [0, 0]),
+        ([u32::MAX, 1, 0, 0x8000_0000], [u32::MAX, 0x7FFF_FFFF]),
+        ([0, 0x7FFF_FFFF, u32::MAX, u32::MAX], [u32::MAX, u32::MAX]),
+        ([u32::MAX, u32::MAX, u32::MAX, u32::MAX], [0, 0]),
+    ];
+    let inputs = register_cases.map(|(input, _)| input);
+    for (half, read) in [(0, 5), (1, 6)] {
+        let shader = build_valu_cs(&[mad[0], mad[1], vop1(0x001, 0, vgpr(read))]);
+        run_cases(&shader, &inputs, |backend, i, input, got| {
+            let want = register_cases[i].1[half];
+            assert_eq!(
+                got, want,
+                "{backend}: register accumulator, dword {half}, input {input:x?}"
+            );
+        });
+    }
+
+    // The 32-bit literal -2 sign-extends to a signed 64-bit accumulator.
+    let literal_cases: [([u32; 4], [u32; 2]); 4] = [
+        ([1, 1, 0, 0], [u32::MAX, u32::MAX]),
+        ([u32::MAX, 5, 0, 0], [0xFFFF_FFF9, u32::MAX]),
+        ([0x8000_0000, 0x8000_0000, 0, 0], [0xFFFF_FFFE, 0x3FFF_FFFF]),
+        ([0x7FFF_FFFF, u32::MAX, 0, 0], [0x7FFF_FFFF, u32::MAX]),
+    ];
+    let inputs = literal_cases.map(|(input, _)| input);
+    for (half, read) in [(0, 5), (1, 6)] {
+        let shader = build_valu_cs(&[
+            mad[0],
+            0x101 | 0x102 << 9 | 0xFF << 18,
+            0xFFFF_FFFE,
+            vop1(0x001, 0, vgpr(read)),
+        ]);
+        run_cases(&shader, &inputs, |backend, i, input, got| {
+            let want = literal_cases[i].1[half];
+            assert_eq!(
+                got, want,
+                "{backend}: literal -2, dword {half}, input {input:x?}"
+            );
+        });
+    }
+}
+
 #[test]
 fn bfe_signed_and_unsigned_match_rdna2() {
     // v_bfe_i32 v0, v1, v2, v3 (VOP3 opcode 0x149)
<!-- greyfinch-sync:uiliannn/VItoriaBr:comment:7236887180 --> Imported from [uiliannn/VItoriaBr](https://github.com/uiliannn/VItoriaBr/pull/82#issuecomment-7236887180). Originally posted by [lmouz823-lang](https://github.com/lmouz823-lang) on 2026-11-28T08:54:20Z. --- ## Windows validation of f4242ab I checked head `f4242ab1cd48428ac3f2cdf5bd42f840247ad30c` against base `081a89d2e5767beb38c94e2f4adc7eeca0ad69db` and prepared a small additional regression test. ### Environment and execution - Windows 11 Pro build 26200, Rust 1.98.1 `x86_64-pc-windows-msvc`, release profile, locked dependencies. - NVIDIA GeForce RTX 4060 Laptop GPU 8 GB, driver 610.88, plus Microsoft Basic Render Driver through WARP. Both adapters actually executed the selected GPU cases. - `VICTORY_STRICT_TESTS=1`, explicit `VICTORY_DXIL` pointing to Windows SDK 10.0.26100.0 x64 `dxil.dll`, file version 1.8.2502.11. A temporary diagnostic run recorded successful validator load, validation, and container-hash checks for all 3 original and 4 added shader modules. The submitted patch contains the regression test. | Exact test | Final result | Dispatches across both adapters | | --- | --- | --- | | `emit::lower::tests::signed_multiplies_translate` | 1 passed, 0 failed, 0 ignored | CPU translation | | `emit::valu_tests::signed_multiplies_match_rdna2` | 1 passed, 0 failed, 0 ignored | 20 | | `emit::valu_tests::signed_mad_wraps_and_sign_extends_literal_accumulators` | 1 passed, 0 failed, 0 ignored | 40 | Each command used `cargo test --release --locked -p victory-shader --lib <exact-name> -- --exact --nocapture --test-threads=1`. Each selected exactly one test. No selected final case reported a skip. The existing translation test continues to accept null and unread signed carry destinations, refuse a read of signed MAD carry-out, and accept the unsigned MAD carry-read control. ### Additional coverage and negative control The attached 55-line test adds fixed expected bit patterns for signed 64-bit wraparound, register accumulator sign boundaries, and literal `0xFFFFFFFE` sign extension. It reads both destination dwords through v[5:6], preserving v1..v4 inputs. Register cases account for `2 dwords × 6 inputs × 2 backends = 24` dispatches, and literal cases for `2 × 4 × 2 = 16`. The result-dword calculations follow signed 32-bit multiplication and a signed 64-bit accumulator. The i64 literal conversion is also documented as sign extension from 32 bits. [AMD RDNA2 ISA, V_MAD_I64_I32](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture), [LLVM AMDGPU integer operand conversion](https://rocm.docs.amd.com/projects/llvm-project/en/docs-6.3.2/LLVM/llvm/html/AMDGPUOperandSyntax.html#conversion-of-integer-values) For a targeted negative control, I temporarily changed only the signed MAD literal accumulator conversion from `i64::from(l as i32)` to `i64::from(l)`. The original GPU test still passed on both adapters. The added test failed on NVIDIA at literal `-2`, input `[1, 1, 0, 0]`, high result dword, with actual `0x00000000` and expected `0xFFFFFFFF` and cargo exit 101. This establishes that the added coverage detects that specific regression. The deliberate failure stopped later negative cases and the WARP negative run. I then restored `lower.rs` byte-for-byte, removed diagnostic logging, cleaned only the isolated `victory-shader` release outputs, and rebuilt the final test binary in 1m41s. All three final exact tests above passed after that real rebuild. ### Static checks and scope - Release Clippy with `--all-targets -- -D warnings`, the 195-file asset policy check, and `git diff --check` passed. - The added function passes the repository-configured rustfmt check. Whole-file rustfmt exits 1 on both the untouched head and final file, with the same 20 pre-existing diff hunks after path/line headers are removed. - Native AMD execution, actual gameplay, and the full shader/workspace suites remain untested. Numeric signed carry-out semantics remain unverified. This run checks the PR's existing refusal boundary for reading it. The patch was prepared with AI assistance and checked locally. Patch SHA-256 `d3f8cd7a50d44f9fe2efc307f7779d931c792425b5e44385afd56f55768da994`. <details> <summary>Complete regression-test patch</summary> ```diff diff --git a/crates/victory-shader/src/emit/valu_tests.rs b/crates/victory-shader/src/emit/valu_tests.rs index 92de739..a8e7424 100644 --- a/crates/victory-shader/src/emit/valu_tests.rs +++ b/crates/victory-shader/src/emit/valu_tests.rs @@ -1019,6 +1019,61 @@ fn signed_multiplies_match_rdna2() { } } +#[test] +fn signed_mad_wraps_and_sign_extends_literal_accumulators() { + // v_mad_i64_i32 v[5:6], null, v1, v2, v[3:4]. + let mad = [ + 0x35 << 26 | 0x177 << 16 | 125 << 8 | 5, + 0x101 | 0x102 << 9 | 0x103 << 18, + ]; + let register_cases: [([u32; 4], [u32; 2]); 6] = [ + ( + [0x8000_0000, u32::MAX, u32::MAX, 0x7FFF_FFFF], + [0x7FFF_FFFF, 0x8000_0000], + ), + ([0x8000_0000, 0x8000_0000, 0, 0x4000_0000], [0, 0x8000_0000]), + ([0x7FFF_FFFF, 0x7FFF_FFFF, u32::MAX, 0xC000_0000], [0, 0]), + ([u32::MAX, 1, 0, 0x8000_0000], [u32::MAX, 0x7FFF_FFFF]), + ([0, 0x7FFF_FFFF, u32::MAX, u32::MAX], [u32::MAX, u32::MAX]), + ([u32::MAX, u32::MAX, u32::MAX, u32::MAX], [0, 0]), + ]; + let inputs = register_cases.map(|(input, _)| input); + for (half, read) in [(0, 5), (1, 6)] { + let shader = build_valu_cs(&[mad[0], mad[1], vop1(0x001, 0, vgpr(read))]); + run_cases(&shader, &inputs, |backend, i, input, got| { + let want = register_cases[i].1[half]; + assert_eq!( + got, want, + "{backend}: register accumulator, dword {half}, input {input:x?}" + ); + }); + } + + // The 32-bit literal -2 sign-extends to a signed 64-bit accumulator. + let literal_cases: [([u32; 4], [u32; 2]); 4] = [ + ([1, 1, 0, 0], [u32::MAX, u32::MAX]), + ([u32::MAX, 5, 0, 0], [0xFFFF_FFF9, u32::MAX]), + ([0x8000_0000, 0x8000_0000, 0, 0], [0xFFFF_FFFE, 0x3FFF_FFFF]), + ([0x7FFF_FFFF, u32::MAX, 0, 0], [0x7FFF_FFFF, u32::MAX]), + ]; + let inputs = literal_cases.map(|(input, _)| input); + for (half, read) in [(0, 5), (1, 6)] { + let shader = build_valu_cs(&[ + mad[0], + 0x101 | 0x102 << 9 | 0xFF << 18, + 0xFFFF_FFFE, + vop1(0x001, 0, vgpr(read)), + ]); + run_cases(&shader, &inputs, |backend, i, input, got| { + let want = literal_cases[i].1[half]; + assert_eq!( + got, want, + "{backend}: literal -2, dword {half}, input {input:x?}" + ); + }); + } +} + #[test] fn bfe_signed_and_unsigned_match_rdna2() { // v_bfe_i32 v0, v1, v2, v3 (VOP3 opcode 0x149) ``` </details>

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!82
No description provided.