Shaders: the carry of v_mad_u64_u32 on its dwords (wrong on AMD when the addend shares a register with a factor) #1507

Open
murillojndem wants to merge 1 commit from upstream-pr/1507 into main
murillojndem commented 2026-11-28 05:51:48 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by murillojndem on 2026-11-28T05:51:48Z.


v_mad_u64_u32 writes {carry, D} = a * b + c. The lowering takes the carry as r < c on two i64 values (icmp ult). On an RX 7900 XT that compare is wrong when the addend's register pair shares a register with the second factor: v_mad_u64_u32 v[5:6], s20, v1, v2, v[2:3] with v1 = 0, v2 = 1 and v3 = 0 (so c = 1) gives a carry of 1 for 0 * 1 + 1. WARP gives 0. With the addend in a pair of its own the card is right, and so are both result dwords in either form.

The carry is now compared on the dwords: the result's high dword against the addend's, then the low dwords when the high ones are equal. The addend's dwords come from split64 of the operand, so this change does not depend on the backport of Marvisak/VIctory#1599 for v_cmp_*_i64, which is #1506. The same dword compare is the carry part of Marvisak/VIctory#3082 (commit 079d8f9 by RVZO6), which upstream's triage asked to narrow to that change; it has been checked on WARP only. This PR adds the measurement on AMD hardware.

On other cards: the dword form gives the same answers wherever the i64 compare is right, as on WARP here. It was not run on an NVIDIA card; the RX 7900 XT and WARP are the two devices tested.

The test, v_mad_u64_u32_matches_its_definition, checks the carry and both result dwords against a * b + c computed in 128 bits, for 6 x 6 factors and 9 addends, with the addend in v[3:4] and in v[2:3]. It runs every case and counts the mismatches by device, form and output before it fails.

Results on an AMD Radeon RX 7900 XT (RDNA 3, driver 32.0.32015.7029), Windows 11, Rust 1.98.1, on 6b3ce6c (current main) and first on 9e90746; synthetic shaders only, no game files. With the test added and the old lowering, the card gets 124 of the 324 carries wrong with the addend in v[2:3], and none with it in v[3:4]; the result dwords are right in both forms, and WARP is right everywhere. With this PR the card and WARP have no mismatch.

  • rdna_forms_give_what_the_spelled_out_ones_give ran in both binaries without its "no RDNA card" line, so the hardware device was the Radeon.
  • With the high-dword compare made signed (a deliberate fault), the test fails on WARP: 78 of 324 carries wrong in v[3:4] and 69 in v[2:3].
  • None of three simple "dwords swapped" models of the compare gives the 124 seen on the card. What this shares with Marvisak/VIctory#1599 is the trigger: a register in both compared values.
  • victory-shader library on the card and WARP (--test-threads=1), on 6b3ce6c: with the test alone, 314 passed, 4 failed (three PSV0 mismatches with the SDK 10.0.22621 dxil.dll, and this test), 4 ignored; with this PR, 315 passed, 3 failed (the three PSV0 ones), 4 ignored. On 9e90746 the same pair was 291/4/4 and 292/3/4.
  • Clippy in both CI configurations, with --keep-going: the same 15 errors with and without this change on 6b3ce6c, none in the changed files. cargo fmt --check -p victory-shader reports no new difference. cargo xtask policy: 288 files ok. cargo test --workspace --locked --no-run builds. lower.rs has 634,001 bytes against the 655,360 limit.
  • #635 tests this instruction's result dwords (on an RTX 4070 and WARP), not its carry, and adds v_mad_i64_i32 next to the arm this PR changes. A three-way merge of #635 with this PR, and of each with main, is clean.

No in-game effect is claimed.

Generated with Claude Code

<!-- greyfinch-sync:fxceless/victory-fork:issue:5537221257 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1507). Originally posted by [murillojndem](https://github.com/murillojndem) on 2026-11-28T05:51:48Z. --- `v_mad_u64_u32` writes `{carry, D} = a * b + c`. The lowering takes the carry as `r < c` on two i64 values (`icmp ult`). On an RX 7900 XT that compare is wrong when the addend's register pair shares a register with the second factor: `v_mad_u64_u32 v[5:6], s20, v1, v2, v[2:3]` with v1 = 0, v2 = 1 and v3 = 0 (so c = 1) gives a carry of 1 for 0 * 1 + 1. WARP gives 0. With the addend in a pair of its own the card is right, and so are both result dwords in either form. The carry is now compared on the dwords: the result's high dword against the addend's, then the low dwords when the high ones are equal. The addend's dwords come from `split64` of the operand, so this change does not depend on the backport of Marvisak/VIctory#1599 for `v_cmp_*_i64`, which is #1506. The same dword compare is the carry part of Marvisak/VIctory#3082 (commit 079d8f9 by RVZO6), which upstream's triage asked to narrow to that change; it has been checked on WARP only. This PR adds the measurement on AMD hardware. On other cards: the dword form gives the same answers wherever the i64 compare is right, as on WARP here. It was not run on an NVIDIA card; the RX 7900 XT and WARP are the two devices tested. The test, `v_mad_u64_u32_matches_its_definition`, checks the carry and both result dwords against `a * b + c` computed in 128 bits, for 6 x 6 factors and 9 addends, with the addend in v[3:4] and in v[2:3]. It runs every case and counts the mismatches by device, form and output before it fails. Results on an AMD Radeon RX 7900 XT (RDNA 3, driver 32.0.32015.7029), Windows 11, Rust 1.98.1, on 6b3ce6c (current `main`) and first on 9e90746; synthetic shaders only, no game files. With the test added and the old lowering, the card gets 124 of the 324 carries wrong with the addend in v[2:3], and none with it in v[3:4]; the result dwords are right in both forms, and WARP is right everywhere. With this PR the card and WARP have no mismatch. - `rdna_forms_give_what_the_spelled_out_ones_give` ran in both binaries without its "no RDNA card" line, so the hardware device was the Radeon. - With the high-dword compare made signed (a deliberate fault), the test fails on WARP: 78 of 324 carries wrong in v[3:4] and 69 in v[2:3]. - None of three simple "dwords swapped" models of the compare gives the 124 seen on the card. What this shares with Marvisak/VIctory#1599 is the trigger: a register in both compared values. - `victory-shader` library on the card and WARP (`--test-threads=1`), on 6b3ce6c: with the test alone, 314 passed, 4 failed (three PSV0 mismatches with the SDK 10.0.22621 `dxil.dll`, and this test), 4 ignored; with this PR, 315 passed, 3 failed (the three PSV0 ones), 4 ignored. On 9e90746 the same pair was 291/4/4 and 292/3/4. - Clippy in both CI configurations, with `--keep-going`: the same 15 errors with and without this change on 6b3ce6c, none in the changed files. `cargo fmt --check -p victory-shader` reports no new difference. `cargo xtask policy`: 288 files ok. `cargo test --workspace --locked --no-run` builds. `lower.rs` has 634,001 bytes against the 655,360 limit. - #635 tests this instruction's result dwords (on an RTX 4070 and WARP), not its carry, and adds `v_mad_i64_i32` next to the arm this PR changes. A three-way merge of #635 with this PR, and of each with `main`, is clean. No in-game effect is claimed. Generated with [Claude Code](https://claude.com/claude-code)
Melow-code commented 2026-11-29 04:59:17 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Melow-code on 2026-11-29T04:59:17Z.

Review: COMMENTED


Checked that the carry on dwords is exact, independently of any driver: no objection.

I merged 83879d2 onto main 368967f8 on Linux, added an evaluator of the emitted module (test code), lowered v_mad_u64_u32 in 18 operand forms and evaluated the carry and both result dwords: addend in a VGPR pair of its own, in pairs sharing either factor's register, in SGPR pairs, and as inline integers (0, 1, 64, -1, -16). 10,000 random inputs per form, with zeros, ones and sign bits mixed in: 540,000 values against the sum in 128 bits, with the carry set 64,338 times, no mismatch.

The check is not vacuous: with the low-dword compare made <=, it fails at once.

cargo test -p victory-formats -p victory-shader --lib: 42 and 208 passed (one of them mine), 0 failed. The merge is clean.

Not run: any card. The AMD compiler fault itself, and the PR's own device test, are not checked here. Also not run: the Windows tests.

AI-assisted review (Claude Code).

<!-- greyfinch-sync:fxceless/victory-fork:review:6101501827 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1507#pullrequestreview-6101501827). Originally posted by [Melow-code](https://github.com/Melow-code) on 2026-11-29T04:59:17Z. **Review:** COMMENTED --- Checked that the carry on dwords is exact, independently of any driver: no objection. I merged 83879d2 onto main 368967f8 on Linux, added an evaluator of the emitted module (test code), lowered `v_mad_u64_u32` in 18 operand forms and evaluated the carry and both result dwords: addend in a VGPR pair of its own, in pairs sharing either factor's register, in SGPR pairs, and as inline integers (0, 1, 64, -1, -16). 10,000 random inputs per form, with zeros, ones and sign bits mixed in: 540,000 values against the sum in 128 bits, with the carry set 64,338 times, no mismatch. The check is not vacuous: with the low-dword compare made `<=`, it fails at once. `cargo test -p victory-formats -p victory-shader --lib`: 42 and 208 passed (one of them mine), 0 failed. The merge is clean. Not run: any card. The AMD compiler fault itself, and the PR's own device test, are not checked here. Also not run: the Windows tests. AI-assisted review (Claude Code).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1507:upstream-pr/1507'
git switch 'upstream-pr/1507'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1507'
git switch 'upstream-pr/1507'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1507'
git switch 'upstream-pr/1507'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1507'
git switch 'main'
git merge --squash 'upstream-pr/1507'
git switch 'main'
git merge --ff-only 'upstream-pr/1507'
git switch 'main'
git merge 'upstream-pr/1507'
git push origin 'main'
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
fxceless/victory-fork!1507
No description provided.