Shaders: v_mad_i64_i32 zero-extends a negative literal addend #2253

Open
opened 2026-11-26 04:59:55 +00:00 by github-bot · 4 comments
Owner

@⁠BastienRodz on GitHub · 2026-11-25 08:59 UTC · original

What happens

v_mad_i64_i32 with a 32-bit literal as its 64-bit addend (src2) zero-extends the literal. Lower::src64 sign-extends a literal only for operations whose name ends in _i64 (added in #447):

Operand::Literal(x) if inst.name().ends_with("_i64") => { /* sign-extend */ }

v_mad_i64_i32 ends in _i32, so its addend takes the zero-extending arm. With src2 = -100 (0xffffff9c), 0 * 0 + src2 gives 0x00000000_ffffff9c instead of -100, and 10 * 10 + src2 gives a high dword of 1 instead of 0.

#447 noted v_mad_i64_i32 was handled in #163, which was closed without merging.

Expected

RDNA 2 ISA, §6.2.1 "Literal Expansion to 64 bits": "Literal constants are 32-bits, but they can be used as sources which normally require 64-bit data. They are expanded to 64 bits following these rules: … 64-bit signed integer: sign extended to 64 bits". §12.12, opcode 375 V_MAD_I64_I32: "{vcc_out,D.i64} = S0.i32 * S1.i32 + S2.i64." So the literal in src2 is sign-extended; v_mad_u64_u32 (S2.u64) keeps zero-extending.

Where

crates/victory-shader/src/emit/lower.rs, Lower::src64.

AI-assisted (Gemini 4 Neon found it, Claude Code reviewed and verified it); synthetic test only, no game data.

**@⁠BastienRodz** on GitHub · 2026-11-25 08:59 UTC · [original](https://github.com/Marvisak/VIctory/issues/2253) ### What happens `v_mad_i64_i32` with a 32-bit literal as its 64-bit addend (`src2`) zero-extends the literal. `Lower::src64` sign-extends a literal only for operations whose name ends in `_i64` (added in #447): ```rust Operand::Literal(x) if inst.name().ends_with("_i64") => { /* sign-extend */ } ``` `v_mad_i64_i32` ends in `_i32`, so its addend takes the zero-extending arm. With `src2 = -100` (`0xffffff9c`), `0 * 0 + src2` gives `0x00000000_ffffff9c` instead of `-100`, and `10 * 10 + src2` gives a high dword of 1 instead of 0. #447 noted `v_mad_i64_i32` was handled in #163, which was closed without merging. ### Expected RDNA 2 ISA, §6.2.1 "Literal Expansion to 64 bits": "Literal constants are 32-bits, but they can be used as sources which normally require 64-bit data. They are expanded to 64 bits following these rules: … 64-bit signed integer: sign extended to 64 bits". §12.12, opcode 375 `V_MAD_I64_I32`: "{vcc_out,D.i64} = S0.i32 * S1.i32 + S2.i64." So the literal in `src2` is sign-extended; `v_mad_u64_u32` (`S2.u64`) keeps zero-extending. ### Where `crates/victory-shader/src/emit/lower.rs`, `Lower::src64`. AI-assisted (Gemini 4 Neon found it, Claude Code reviewed and verified it); synthetic test only, no game data.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:59:55 +00:00
Author
Owner

@⁠Dathinel on GitHub · 2026-11-25 09:20 UTC · original

Compared #2254 (5a8a792) and #2261 (440da6e) against their common parent 940d32f. Their entire lower.rs matches after removing comment-only lines. Either production fix covers this issue.

I ran #2261's two synthetic regressions on Windows. Restoring only the parent's lower.rs made both fail: the DXIL check missed the signed literal, and the hardware returned high dword 0 for 0 * 0 + (-100) (expected 0xffffffff). Restoring the fix passed both. Reproduce with cargo test --locked -p victory-shader --lib mad_64_bit_addend -- --nocapture.

For choosing test coverage: #2254 also checks a negative product and unsigned carry across bit 32. #2261 adds a GPU-independent DXIL check on Windows when dxc is available. These tests can be selected independently of the identical production change.

AI-assisted comparison and local verification.

**@⁠Dathinel** on GitHub · 2026-11-25 09:20 UTC · [original](https://github.com/Marvisak/VIctory/issues/2253#issuecomment-7158206466) Compared #2254 (5a8a792) and #2261 (440da6e) against their common parent 940d32f. Their entire `lower.rs` matches after removing comment-only lines. Either production fix covers this issue. I ran #2261's two synthetic regressions on Windows. Restoring only the parent's `lower.rs` made both fail: the DXIL check missed the signed literal, and the hardware returned high dword 0 for `0 * 0 + (-100)` (expected 0xffffffff). Restoring the fix passed both. Reproduce with `cargo test --locked -p victory-shader --lib mad_64_bit_addend -- --nocapture`. For choosing test coverage: #2254 also checks a negative product and unsigned carry across bit 32. #2261 adds a GPU-independent DXIL check on Windows when dxc is available. These tests can be selected independently of the identical production change. AI-assisted comparison and local verification.
Author
Owner

@⁠studiostephe on GitHub · 2026-11-25 10:21 UTC · original

Lower::src64 sign-extends a 32-bit literal only for operations whose name ends in _i64 (added in #447). v_mad_i64_i32 ends in _i32, so its 64-bit addend (src2) takes the zero-extending arm. With src2 = -100 (0xffffff9c), 0 * 0 + src2 gives 0x00000000_ffffff9c instead of -100 (0xffffffff_ffffff9c), and the high dword of the result is 1 instead of 0.

RDNA 2 ISA §6.2.1 "Literal Expansion to 64 bits": "64-bit signed integer: sign extended to 64 bits." §12.12, opcode 375 V_MAD_I64_I32: "{vcc_out,D.i64} = S0.i32 * S1.i32 + S2.i64." The addend is a signed 64-bit operand; its literal sign-extends. v_mad_u64_u32 (opcode 374, S2.u64) keeps zero-extending.

Where: crates/victory-shader/src/emit/lower.rs, Lower::src64 (line 6162 in current main):

Operand::Literal(x) if inst.name().ends_with("_i64") => {

Fix: also sign-extend for v_mad_i64_i32:

-            Operand::Literal(x) if inst.name().ends_with("_i64") => {
+            // `v_mad_i64_i32` ends in `_i32` but its addend (src2) is a
+            // signed 64-bit operand (RDNA 2 ISA §6.2.1, opcode 375).
+            Operand::Literal(x)
+                if inst.name().ends_with("_i64") || inst.name() == "v_mad_i64_i32" =>
+            {

Test: valu_tests.rs — a Windows-only hardware/WARP test (same harness as the existing signed_64_bit_operands_sign_extend_their_literal test, which covers the _i64 names). The shader runs v_mad_i64_i32 v[0:1], s0, s0, -100 (VOP3 0x177, literal src2) with s0 = 0, moves the high dword to v0, and the harness stores it. Patched: high dword = 0xFFFFFFFF (sign-extended −100). Unpatched: high dword = 0 (zero-extended 0xffffff9c).

#[test]
fn v_mad_i64_i32_sign_extends_its_literal_addend() {
    // … (full test in the diff below)
}

Verified:

  • cargo test -p victory-shader --locked — 145 passed, 0 failed, 2 ignored (native, Mac; the new test is #[cfg(all(test, windows))] and compiles for the Windows target).
  • cargo check -p victory-shader --profile test --target x86_64-pc-windows-msvc — clean.
  • rustfmt --edition 2024 --check — clean on the changed lines.
  • The test requires a SM 6.6 host (hardware GPU or WARP); it skips without one, like the rest of valu_tests.

Not covered:

  • Not run on a GPU here (no Windows/D3D12 on this Mac). The test will run in CI or on a Windows box.
  • v_mad_u64_u32 is unaffected (its addend is unsigned; zero-extension is correct).

Full diff:

diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs
index 2c06b01..e615b35 100644
--- a/crates/victory-shader/src/emit/lower.rs
+++ b/crates/victory-shader/src/emit/lower.rs
@@ -6159,7 +6159,11 @@ impl<'a> Lower<'a> {
             }
             // A signed 64-bit operand sign-extends its 32-bit literal, as
             // the hardware does (LLVM's AMDGPU operand rules).
-            Operand::Literal(x) if inst.name().ends_with("_i64") => {
+            // `v_mad_i64_i32` ends in `_i32` but its addend (src2) is a
+            // signed 64-bit operand (RDNA 2 ISA §6.2.1, opcode 375).
+            Operand::Literal(x)
+                if inst.name().ends_with("_i64") || inst.name() == "v_mad_i64_i32" =>
+            {
                 Ok(self.s.m.int_const(i64t, i64::from(x as i32)))
             }
             _ => {
diff --git a/crates/victory-shader/src/emit/valu_tests.rs b/crates/victory-shader/src/emit/valu_tests.rs
index 0e300b8..40cd441 100644
--- a/crates/victory-shader/src/emit/valu_tests.rs
+++ b/crates/victory-shader/src/emit/valu_tests.rs
@@ -1791,3 +1791,31 @@ fn relative_moves_reach_only_what_their_clamped_index_can() {
         refused.err()
     );
 }
+
+/// `v_mad_i64_i32` with a negative literal addend (src2): the hardware
+/// sign-extends the 32-bit literal to 64 bits (RDNA 2 ISA §6.2.1). The
+/// unpatched `Lower::src64` only sign-extends for names ending `_i64`, so
+/// `v_mad_i64_i32` (which ends `_i32`) zero-extended the addend.
+#[test]
+fn v_mad_i64_i32_sign_extends_its_literal_addend() {
+    const MINUS_100: u32 = -100_i32 as u32;
+    // v_mad_i64_i32 v[0:1], s0, s0, -100 (VOP3 0x177, literal src2);
+    // v_mov_b32 v0, v1 (move the high dword to v0 for the store).
+    let body = [
+        (0x35 << 26) | (0x177 << 16), // v_mad_i64_i32 v[0:1], s0, s0, lit
+        0xFF << 18,                   //   src0 = s0, src1 = s0, src2 = literal
+        MINUS_100,                    //   literal value = -100
+        vop1(0x001, 0, vgpr(1)),      // v_mov_b32 v0, v1 (the high dword)
+    ];
+    let shader = build_valu_cs(&body);
+    // s0 = 0: 0 * 0 + (-100) = -100 = 0xFFFFFFFF_FFFFFF9C; high dword = 0xFFFFFFFF.
+    // Unpatched: 0 * 0 + 0xFFFFFF9C (zero-extended) = 0x00000000_FFFFFF9C;
+    // high dword = 0.
+    run_cases(&shader, &[[0, 0, 0, 0]], |backend, _, _, got| {
+        assert_eq!(
+            got,
+            u32::MAX,
+            "{backend}: v_mad_i64_i32(0, 0, -100) high dword = {got:#x}, want 0xFFFFFFFF"
+        );
+    });
+}

qwen3.8_27b running locally on an M1 Mac
shout out to Digital Debris Los Angeles

**@⁠studiostephe** on GitHub · 2026-11-25 10:21 UTC · [original](https://github.com/Marvisak/VIctory/issues/2253#issuecomment-7159580898) `Lower::src64` sign-extends a 32-bit literal only for operations whose name ends in `_i64` (added in #447). `v_mad_i64_i32` ends in `_i32`, so its 64-bit addend (src2) takes the zero-extending arm. With `src2 = -100` (`0xffffff9c`), `0 * 0 + src2` gives `0x00000000_ffffff9c` instead of `-100` (`0xffffffff_ffffff9c`), and the high dword of the result is 1 instead of 0. RDNA 2 ISA §6.2.1 "Literal Expansion to 64 bits": "64-bit signed integer: sign extended to 64 bits." §12.12, opcode 375 `V_MAD_I64_I32`: "{vcc_out,D.i64} = S0.i32 * S1.i32 + S2.i64." The addend is a signed 64-bit operand; its literal sign-extends. `v_mad_u64_u32` (opcode 374, `S2.u64`) keeps zero-extending. **Where:** `crates/victory-shader/src/emit/lower.rs`, `Lower::src64` (line 6162 in current main): ```rust Operand::Literal(x) if inst.name().ends_with("_i64") => { ``` **Fix:** also sign-extend for `v_mad_i64_i32`: ```diff - Operand::Literal(x) if inst.name().ends_with("_i64") => { + // `v_mad_i64_i32` ends in `_i32` but its addend (src2) is a + // signed 64-bit operand (RDNA 2 ISA §6.2.1, opcode 375). + Operand::Literal(x) + if inst.name().ends_with("_i64") || inst.name() == "v_mad_i64_i32" => + { ``` **Test:** `valu_tests.rs` — a Windows-only hardware/WARP test (same harness as the existing `signed_64_bit_operands_sign_extend_their_literal` test, which covers the `_i64` names). The shader runs `v_mad_i64_i32 v[0:1], s0, s0, -100` (VOP3 0x177, literal src2) with `s0 = 0`, moves the high dword to v0, and the harness stores it. Patched: high dword = `0xFFFFFFFF` (sign-extended −100). Unpatched: high dword = `0` (zero-extended `0xffffff9c`). ```rust #[test] fn v_mad_i64_i32_sign_extends_its_literal_addend() { // … (full test in the diff below) } ``` **Verified:** - `cargo test -p victory-shader --locked` — 145 passed, 0 failed, 2 ignored (native, Mac; the new test is `#[cfg(all(test, windows))]` and compiles for the Windows target). - `cargo check -p victory-shader --profile test --target x86_64-pc-windows-msvc` — clean. - `rustfmt --edition 2024 --check` — clean on the changed lines. - The test requires a SM 6.6 host (hardware GPU or WARP); it skips without one, like the rest of `valu_tests`. **Not covered:** - Not run on a GPU here (no Windows/D3D12 on this Mac). The test will run in CI or on a Windows box. - `v_mad_u64_u32` is unaffected (its addend is unsigned; zero-extension is correct). Full diff: ```diff diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 2c06b01..e615b35 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -6159,7 +6159,11 @@ impl<'a> Lower<'a> { } // A signed 64-bit operand sign-extends its 32-bit literal, as // the hardware does (LLVM's AMDGPU operand rules). - Operand::Literal(x) if inst.name().ends_with("_i64") => { + // `v_mad_i64_i32` ends in `_i32` but its addend (src2) is a + // signed 64-bit operand (RDNA 2 ISA §6.2.1, opcode 375). + Operand::Literal(x) + if inst.name().ends_with("_i64") || inst.name() == "v_mad_i64_i32" => + { Ok(self.s.m.int_const(i64t, i64::from(x as i32))) } _ => { diff --git a/crates/victory-shader/src/emit/valu_tests.rs b/crates/victory-shader/src/emit/valu_tests.rs index 0e300b8..40cd441 100644 --- a/crates/victory-shader/src/emit/valu_tests.rs +++ b/crates/victory-shader/src/emit/valu_tests.rs @@ -1791,3 +1791,31 @@ fn relative_moves_reach_only_what_their_clamped_index_can() { refused.err() ); } + +/// `v_mad_i64_i32` with a negative literal addend (src2): the hardware +/// sign-extends the 32-bit literal to 64 bits (RDNA 2 ISA §6.2.1). The +/// unpatched `Lower::src64` only sign-extends for names ending `_i64`, so +/// `v_mad_i64_i32` (which ends `_i32`) zero-extended the addend. +#[test] +fn v_mad_i64_i32_sign_extends_its_literal_addend() { + const MINUS_100: u32 = -100_i32 as u32; + // v_mad_i64_i32 v[0:1], s0, s0, -100 (VOP3 0x177, literal src2); + // v_mov_b32 v0, v1 (move the high dword to v0 for the store). + let body = [ + (0x35 << 26) | (0x177 << 16), // v_mad_i64_i32 v[0:1], s0, s0, lit + 0xFF << 18, // src0 = s0, src1 = s0, src2 = literal + MINUS_100, // literal value = -100 + vop1(0x001, 0, vgpr(1)), // v_mov_b32 v0, v1 (the high dword) + ]; + let shader = build_valu_cs(&body); + // s0 = 0: 0 * 0 + (-100) = -100 = 0xFFFFFFFF_FFFFFF9C; high dword = 0xFFFFFFFF. + // Unpatched: 0 * 0 + 0xFFFFFF9C (zero-extended) = 0x00000000_FFFFFF9C; + // high dword = 0. + run_cases(&shader, &[[0, 0, 0, 0]], |backend, _, _, got| { + assert_eq!( + got, + u32::MAX, + "{backend}: v_mad_i64_i32(0, 0, -100) high dword = {got:#x}, want 0xFFFFFFFF" + ); + }); +} ``` qwen3.8_27b running locally on an M1 Mac shout out to Digital Debris Los Angeles
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 18:47 UTC · original

Automated triage for the maintainers.

Confirmed on main. In crates/victory-shader/src/emit/lower.rs, Lower::src64 sign-extends a literal only when the name ends in _i64. The v_mad_u64_u32 | v_mad_i64_i32 arm takes its addend from src64(inst, 2), so the addend of v_mad_i64_i32 is zero-extended.

Two open PRs carry the fix:

  • #2254, which adds tests for both signed and unsigned forms.
  • #2380.

#2261 was closed. The diff posted in the comment above is the same change as #2254, so no separate patch is needed.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 18:47 UTC · [original](https://github.com/Marvisak/VIctory/issues/2253#issuecomment-7171404849) <!-- victory-issue-triage v1 verdict=confirmed seen=2026-11-25T10:21:21Z at=2026-11-25T18:50:00Z --> Automated triage for the maintainers. Confirmed on main. In `crates/victory-shader/src/emit/lower.rs`, `Lower::src64` sign-extends a literal only when the name ends in `_i64`. The `v_mad_u64_u32 | v_mad_i64_i32` arm takes its addend from `src64(inst, 2)`, so the addend of `v_mad_i64_i32` is zero-extended. Two open PRs carry the fix: - #2254, which adds tests for both signed and unsigned forms. - #2380. #2261 was closed. The diff posted in the comment above is the same change as #2254, so no separate patch is needed. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-26 18:52 UTC · original

Hardware confirmation on gfx1036 (RDNA2), and a check of #2254 against it.

Setup. The Radeon iGPU of a Ryzen 7 9800X3D (gfx1036, AMD driver 32.0.21042.62, Windows 11). A HIP kernel (assembled by the driver's amd_comgr) ran v_mad_i64_i32 v[40:41], null, v4, v5, <src2> and v_mad_u64_u32 with the same operands, over 76 rows of signed and unsigned edge values and random words. The same words went through build_valu_cs on that Radeon and on an RTX 5080 (driver 610.88), one adapter at a time with a local switch. PC RDNA2 silicon, not the console's GPU. No game data.

gfx1036, all 76 rows of each:

instruction, src2 64-bit addend e.g. 0 * 0 + src2, high dword
v_mad_i64_i32, literal 0xffffff9c (-100) sign-extended 0xffffffff
v_mad_i64_i32, literal 0x80000000 sign-extended 0xffffffff
v_mad_u64_u32, literal 0xffffff9c zero-extended 0x00000000
v_mad_i64_i32, inline -1 -1 0xffffffff
v_mad_u64_u32, inline -1 -1 (all 64 bits set) 0xffffffff

So the literal follows the operand's type, as §6.2.1 says. An inline constant is the same 64-bit value for both opcodes.

Translator (high dword, rows that differ of 76; identical on the Radeon and the 5080):

case main 63bcca4 #2254 (5a8a792)
v_mad_i64_i32, literal -100 76 0
v_mad_i64_i32, literal 0x80000000 76 0
v_mad_u64_u32, literal -100 0 0
inline -1, both opcodes 0 0
v_mad_i64_i32, literal -100, low dword 0 0

#2254 ran on a local merge with PRs that touch other instructions; it doesn't conflict there.

AI use: Claude Code (Claude Opus 5.5) wrote the probe and ran it on the GPUs above; the numbers come from those runs.

**@⁠Lyrnc** on GitHub · 2026-11-26 18:52 UTC · [original](https://github.com/Marvisak/VIctory/issues/2253#issuecomment-7200950466) Hardware confirmation on gfx1036 (RDNA2), and a check of #2254 against it. **Setup.** The Radeon iGPU of a Ryzen 7 9800X3D (**gfx1036**, AMD driver 32.0.21042.62, Windows 11). A HIP kernel (assembled by the driver's amd_comgr) ran `v_mad_i64_i32 v[40:41], null, v4, v5, <src2>` and `v_mad_u64_u32` with the same operands, over 76 rows of signed and unsigned edge values and random words. The same words went through `build_valu_cs` on that Radeon and on an RTX 5080 (driver 610.88), one adapter at a time with a local switch. PC RDNA2 silicon, not the console's GPU. No game data. **gfx1036**, all 76 rows of each: | instruction, `src2` | 64-bit addend | e.g. `0 * 0 + src2`, high dword | |---|---|---| | `v_mad_i64_i32`, literal `0xffffff9c` (-100) | sign-extended | `0xffffffff` | | `v_mad_i64_i32`, literal `0x80000000` | sign-extended | `0xffffffff` | | `v_mad_u64_u32`, literal `0xffffff9c` | zero-extended | `0x00000000` | | `v_mad_i64_i32`, inline `-1` | `-1` | `0xffffffff` | | `v_mad_u64_u32`, inline `-1` | `-1` (all 64 bits set) | `0xffffffff` | So the literal follows the operand's type, as §6.2.1 says. An inline constant is the same 64-bit value for both opcodes. **Translator** (high dword, rows that differ of 76; identical on the Radeon and the 5080): | case | `main` `63bcca4` | #2254 (`5a8a792`) | |---|---|---| | `v_mad_i64_i32`, literal -100 | **76** | 0 | | `v_mad_i64_i32`, literal `0x80000000` | **76** | 0 | | `v_mad_u64_u32`, literal -100 | 0 | 0 | | inline `-1`, both opcodes | 0 | 0 | | `v_mad_i64_i32`, literal -100, low dword | 0 | 0 | #2254 ran on a local merge with PRs that touch other instructions; it doesn't conflict there. AI use: Claude Code (Claude Opus 5.5) wrote the probe and ran it on the GPUs above; the numbers come from those runs.
This discussion has been locked. Commenting is limited to contributors.
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory#2253
No description provided.