Shaders: v_mad_i64_i32 sign-extends a negative literal addend #2378

Closed
Melow-code wants to merge 1 commit from upstream-pr/2380 into main
Melow-code commented 2026-11-25 12:19:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Melow-code on 2026-11-25T12:19:19Z.


The addend (src2) of v_mad_i64_i32 is S2.i64 (RDNA 2 ISA §12.12), so its 32-bit literal is sign-extended to 64 bits (§6.2.1). The existing guard in src64 matched only names that end in _i64, and v_mad_i64_i32 ends in _i32 (the multiplied inputs), so the literal fell to the zero-extending arm. With src2 = -100, 0 * 0 + src2 gave 0x00000000_ffffff9c instead of -100.

The guard now also matches v_mad_i64_i32 when the operand is src2 (k == 2). v_mad_u64_u32 keeps its zero-extending arm unchanged.

Verification

  • cargo test -p victory-shader --lib on Linux: 145 passed, 0 failed, 2 ignored.
  • New test: v_mad_i64_i32_sign_extends_a_negative_literal_addend (VOP3B with literal -100 as the addend).
  • The GPU readback needs a Windows host.

Fixes #2253.

🤖 Generated with Claude Code
Co-Authored-By: Claude Opus 4.6 [email protected]

<!-- greyfinch-sync:Marvisak/VIctory:issue:6604322279 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2380). Originally posted by [Melow-code](https://github.com/Melow-code) on 2026-11-25T12:19:19Z. --- The addend (src2) of `v_mad_i64_i32` is `S2.i64` (RDNA 2 ISA §12.12), so its 32-bit literal is sign-extended to 64 bits (§6.2.1). The existing guard in `src64` matched only names that end in `_i64`, and `v_mad_i64_i32` ends in `_i32` (the multiplied inputs), so the literal fell to the zero-extending arm. With `src2 = -100`, `0 * 0 + src2` gave `0x00000000_ffffff9c` instead of `-100`. The guard now also matches `v_mad_i64_i32` when the operand is src2 (`k == 2`). `v_mad_u64_u32` keeps its zero-extending arm unchanged. ## Verification - `cargo test -p victory-shader --lib` on Linux: 145 passed, 0 failed, 2 ignored. - New test: `v_mad_i64_i32_sign_extends_a_negative_literal_addend` (VOP3B with literal -100 as the addend). - The GPU readback needs a Windows host. Fixes #2253. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.6 <[email protected]>
DenRakEiw commented 2026-11-25 13:02:29 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T12:30:01Z.

Review: APPROVED


Verified against origin/main (940d32f) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP:

  • Sign-extending negative 32-bit literal addends for v_mad_i64_i32 is clean and passes dxil.dll validation (note: overlaps in scope with #2254).
  • cargo test -p victory-shader emit::lower::tests: 22/22 passed on RTX 5090, 22/22 passed on WARP (including v_mad_i64_i32_sign_extends_a_negative_literal_addend).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 183 files ok.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6057616537 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2380#pullrequestreview-6057616537). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T12:30:01Z. **Review:** APPROVED --- Verified against `origin/main` (`940d32f`) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP: - Sign-extending negative 32-bit literal addends for `v_mad_i64_i32` is clean and passes `dxil.dll` validation (note: overlaps in scope with `#2254`). - `cargo test -p victory-shader emit::lower::tests`: 22/22 passed on RTX 5090, 22/22 passed on WARP (including `v_mad_i64_i32_sign_extends_a_negative_literal_addend`). - `cargo clippy -p victory-shader --all-targets -- -D warnings`: 0 warnings. - `cargo xtask policy`: 183 files ok. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-25 13:02:29 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T12:41:42Z.

Review: APPROVED


Tested locally: merged into main 940d32f69b, cargo test -p victory-shader --locked on macOS arm64 — 145 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — third PR fixing v_mad_i64_i32 literal sign-extension (after #2254 and #2261), but the most precise of the three: the k == 2 condition restricts sign-extension to the addend (src2, S2.i64) instead of any operand of the instruction. All three conflict on the same lower.rs lines — if only one survives, I'd pick this one.

<!-- greyfinch-sync:Marvisak/VIctory:review:6057784498 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2380#pullrequestreview-6057784498). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T12:41:42Z. **Review:** APPROVED --- Tested locally: merged into main 940d32f69b, `cargo test -p victory-shader --locked` on macOS arm64 — 145 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — third PR fixing v_mad_i64_i32 literal sign-extension (after #2254 and #2261), but the most precise of the three: the `k == 2` condition restricts sign-extension to the addend (src2, S2.i64) instead of any operand of the instruction. All three conflict on the same lower.rs lines — if only one survives, I'd pick this one.
steven-seagull27 commented 2026-11-25 14:07:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by steven-seagull27 on 2026-11-25T13:30:41Z.


I ran a one-thread compute shader against the parent 940d32f and PR head d5dd05f. It reads both result VGPRs and compares them with CPU signed and unsigned 64-bit arithmetic. Each revision ran 32 operations per adapter, giving 64 dword readbacks.

Adapter Parent mismatches PR mismatches
RTX 5080, driver 621.40 8/64 0/64
Windows WARP, 10.0.26100.10068 8/64 0/64

All eight parent mismatches were in the high dword of the signed instruction, with literal 0xffffff9c or 0x80000000. For 0 * 0 + literal(-100), both adapters returned 0x00000000_ffffff9c on the parent and 0xffffffff_ffffff9c on the PR. The unsigned instruction returned 0x00000000_ffffff9c on both revisions. Positive literal controls and all low dwords also matched throughout.

The test commit also corrects the destination encoding in the translation-only fixture. VOP3B puts the vector destination in bits 7:0 and the scalar destination in bits 14:8, so v[0:1], vcc needs 0xd5776a00. Cherry-pickable test commit

git fetch https://github.com/steven-seagull27/VIctory.git refs/heads/codex/pr2380-mad-literal-readback
git cherry-pick 63a66f2c480358429614acc0a1d19d8a451c7f01
cargo test --locked -p victory-shader --lib emit::valu_tests::mad_64bit_literal_addends_match_signed_and_unsigned_arithmetic -- --exact --nocapture
<!-- greyfinch-sync:Marvisak/VIctory:comment:7163881468 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2380#issuecomment-7163881468). Originally posted by [steven-seagull27](https://github.com/steven-seagull27) on 2026-11-25T13:30:41Z. --- I ran a one-thread compute shader against the parent `940d32f` and PR head `d5dd05f`. It reads both result VGPRs and compares them with CPU signed and unsigned 64-bit arithmetic. Each revision ran 32 operations per adapter, giving 64 dword readbacks. | Adapter | Parent mismatches | PR mismatches | |---|---:|---:| | RTX 5080, driver 621.40 | 8/64 | 0/64 | | Windows WARP, 10.0.26100.10068 | 8/64 | 0/64 | All eight parent mismatches were in the high dword of the signed instruction, with literal `0xffffff9c` or `0x80000000`. For `0 * 0 + literal(-100)`, both adapters returned `0x00000000_ffffff9c` on the parent and `0xffffffff_ffffff9c` on the PR. The unsigned instruction returned `0x00000000_ffffff9c` on both revisions. Positive literal controls and all low dwords also matched throughout. The test commit also corrects the destination encoding in the translation-only fixture. VOP3B puts the vector destination in bits 7:0 and the scalar destination in bits 14:8, so `v[0:1], vcc` needs `0xd5776a00`. [Cherry-pickable test commit](https://github.com/steven-seagull27/VIctory/commit/63a66f2c480358429614acc0a1d19d8a451c7f01) ```sh git fetch https://github.com/steven-seagull27/VIctory.git refs/heads/codex/pr2380-mad-literal-readback git cherry-pick 63a66f2c480358429614acc0a1d19d8a451c7f01 cargo test --locked -p victory-shader --lib emit::valu_tests::mad_64bit_literal_addends_match_signed_and_unsigned_arithmetic -- --exact --nocapture ```
rminzao commented 2026-11-25 15:04:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by rminzao on 2026-11-25T14:53:52Z.


This is the same src64 change as #2254, which was opened earlier and is triaged as a merge candidate, so one of the two can be closed.

The difference is in the test. v_mad_i64_i32_sign_extends_a_negative_literal_addend only checks that the program lowers, and main already lowers it: with the fix hunk removed, the test still passes. #2254's mad_i64_i32_sign_extends_literal_addend reads the high dword back from the GPU, and it passes with this PR's fix on an Intel UHD (16-lane waves) and on WARP.

The k == 2 guard is narrower than #2254's name check, but the result is the same: v_mad_i64_i32 reads src0 and src1 as 32-bit sources and only src2 goes through src64 (lower.rs, the v_mad_u64_u32 | v_mad_i64_i32 arm).

Checked on Windows 11 against head d5dd05f: Intel UHD Graphics (31.0.101.4146) and WARP. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7165985935 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2380#issuecomment-7165985935). Originally posted by [rminzao](https://github.com/rminzao) on 2026-11-25T14:53:52Z. --- This is the same `src64` change as #2254, which was opened earlier and is triaged as a merge candidate, so one of the two can be closed. The difference is in the test. `v_mad_i64_i32_sign_extends_a_negative_literal_addend` only checks that the program lowers, and `main` already lowers it: with the fix hunk removed, the test still passes. #2254's `mad_i64_i32_sign_extends_literal_addend` reads the high dword back from the GPU, and it passes with this PR's fix on an Intel UHD (16-lane waves) and on WARP. The `k == 2` guard is narrower than #2254's name check, but the result is the same: `v_mad_i64_i32` reads src0 and src1 as 32-bit sources and only src2 goes through `src64` (`lower.rs`, the `v_mad_u64_u32 | v_mad_i64_i32` arm). _Checked on Windows 11 against head d5dd05f: Intel UHD Graphics (31.0.101.4146) and WARP. AI-assisted._
Marvisak commented 2026-11-25 15:04:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T14:54:08Z.


Automated triage for the maintainers.

Needs work (small). The fix itself is right. In src64 (crates/victory-shader/src/emit/lower.rs), v_mad_i64_i32's src2 is a signed 64-bit operand (public RDNA2 ISA: S2.i64), so its literal should sign-extend like the _i64 operands already do, and v_mad_u64_u32 stays zero-extended. But the new test v_mad_i64_i32_sign_extends_a_negative_literal_addend only checks that lowering succeeds, not the value, so it passes on main too. Its first word 0xD577_006A also puts 0x6A in the vector destination field (bits 7:0) and 0 in the scalar destination (bits 14:8); v[0:1], vcc is 0xD577_6A00.

What would change the verdict: fix the encoding and assert the result on the GPU. The cherry-pickable readback test linked in the thread (63a66f2, signed and unsigned against CPU arithmetic; 8/64 mismatches on main, 0 with this PR on RTX 5080 and WARP) does both.

CI: check passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7165993043 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2380#issuecomment-7165993043). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T14:54:08Z. --- <!-- victory-triage v1 head=d5dd05fed7ea484b455eb42598193ab48b15dada verdict=needs-work at=2026-11-25T14:53:54Z --> Automated triage for the maintainers. **Needs work (small).** The fix itself is right. In `src64` (`crates/victory-shader/src/emit/lower.rs`), `v_mad_i64_i32`'s src2 is a signed 64-bit operand (public RDNA2 ISA: `S2.i64`), so its literal should sign-extend like the `_i64` operands already do, and `v_mad_u64_u32` stays zero-extended. But the new test `v_mad_i64_i32_sign_extends_a_negative_literal_addend` only checks that lowering succeeds, not the value, so it passes on `main` too. Its first word `0xD577_006A` also puts `0x6A` in the vector destination field (bits 7:0) and 0 in the scalar destination (bits 14:8); `v[0:1], vcc` is `0xD577_6A00`. What would change the verdict: fix the encoding and assert the result on the GPU. The cherry-pickable readback test linked in the thread (`63a66f2`, signed and unsigned against CPU arithmetic; 8/64 mismatches on `main`, 0 with this PR on RTX 5080 and WARP) does both. CI: `check` passing. --- _Generated by [Claude Code](https://claude.ai/code)_
fyernald commented 2026-11-25 18:01:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fyernald on 2026-11-25T17:32:54Z.

Review: COMMENTED


Tested on RTX 3080 (617.42), Windows 10 (19045) plus WARP, main 67e8ae1 + #486 + #2596 (the suite crashes on NVIDIA without #2596, #2571). cargo test -p victory-shader --lib --locked -- --test-threads=1: 268 passed, 3 failed (the #1835 ones, also on the base). The change is correct.

But it's the same fix as #2254 (opened about three hours earlier), and the two behave the same: the literal arm is only reached for a 64-bit operand, which here is src2 alone. The test also doesn't check the fix. lowers_valu(...).unwrap() passes on main too, where the literal zero-extends and still lowers. #2254's test runs the instruction on the GPU and checks the high dword for both v_mad_i64_i32 and v_mad_u64_u32. I'd suggest closing this in favour of #2254.

<!-- greyfinch-sync:Marvisak/VIctory:review:6062130288 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2380#pullrequestreview-6062130288). Originally posted by [fyernald](https://github.com/fyernald) on 2026-11-25T17:32:54Z. **Review:** COMMENTED --- Tested on **RTX 3080 (617.42), Windows 10 (19045)** plus WARP, `main` `67e8ae1` + #486 + #2596 (the suite crashes on NVIDIA without #2596, #2571). `cargo test -p victory-shader --lib --locked -- --test-threads=1`: 268 passed, 3 failed (the #1835 ones, also on the base). The change is correct. But it's the same fix as #2254 (opened about three hours earlier), and the two behave the same: the literal arm is only reached for a 64-bit operand, which here is `src2` alone. The test also doesn't check the fix. `lowers_valu(...).unwrap()` passes on `main` too, where the literal zero-extends and still lowers. #2254's test runs the instruction on the GPU and checks the high dword for both `v_mad_i64_i32` and `v_mad_u64_u32`. I'd suggest closing this in favour of #2254.
upstream-sync closed this pull request 2026-11-25 21:02:27 +00:00

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
Marvisak/VIctory!2378
No description provided.