Skip to content

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 into
fxceless:mainfrom
murillojndem:fix/amd-mad-u64-carry-dwords
Open

murillojndem wants to merge 1 commit into
fxceless:mainfrom
murillojndem:fix/amd-mad-u64-carry-dwords

Conversation

@murillojndem

@murillojndem murillojndem commented Nov 28, 2026 •

Copy link
Copy Markdown

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.

No in-game effect is claimed.

Generated with Claude Code

The carry out was an i64 icmp ult of the sum with the addend. On an
RX 7900 XT (driver 32.0.32015.7029) it is wrong where the addend's register
pair shares a register with the second factor: 124 of 324 cases of the new
test with the addend in v[2:3]. WARP, the separate pair v[3:4] and the
result dwords are right. The dwords are compared instead, as in the carry
part of Marvisak/VIctory#3082. The test checks the carry and both result
dwords against a * b + c.

Co-authored-by: RVZO6 <66844656+RVZO6@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@murillojndem
murillojndem force-pushed the fix/amd-mad-u64-carry-dwords branch from 4fb28d3 to 83879d2 Compare November 28, 2026 06:03
ors33 added a commit to ors33/victory-launcher that referenced this pull request Nov 28, 2026
…s (AMD) (fxceless#1506, fxceless#1507)

Backport of Marvisak/VIctory#1599: v_cmp_{lt,le,gt,ge,eq,ne}_{i64,u64}
lowered to one i64 icmp gave the wrong answer on AMD's D3D12 driver when
the operands share a dword. The high dwords decide (signed for i64); equal
high dwords leave it to the low dwords (unsigned). Adds src64_dwords
helper.

v_mad_u64_u32 carry is now computed on the dwords (r < c unsigned, high
then low) instead of as an i64 compare, which AMD's D3D12 compiler also
gets wrong where c shares a register with the second factor.

@Melow-code Melow-code left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

I merged 83879d2 onto main 368967f 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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants