Shaders: the carry of v_mad_u64_u32 on its dwords (wrong on AMD when the addend shares a register with a factor) #1507
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1507"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from fxceless/victory-fork. Originally posted by murillojndem on 2026-11-28T05:51:48Z.
v_mad_u64_u32writes{carry, D} = a * b + c. The lowering takes the carry asr < con 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
split64of the operand, so this change does not depend on the backport of Marvisak/VIctory#1599 forv_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 againsta * b + ccomputed 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(currentmain) and first on9e90746; 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_giveran in both binaries without its "no RDNA card" line, so the hardware device was the Radeon.victory-shaderlibrary on the card and WARP (--test-threads=1), on6b3ce6c: with the test alone, 314 passed, 4 failed (three PSV0 mismatches with the SDK 10.0.22621dxil.dll, and this test), 4 ignored; with this PR, 315 passed, 3 failed (the three PSV0 ones), 4 ignored. On9e90746the same pair was 291/4/4 and 292/3/4.--keep-going: the same 15 errors with and without this change on6b3ce6c, none in the changed files.cargo fmt --check -p victory-shaderreports no new difference.cargo xtask policy: 288 files ok.cargo test --workspace --locked --no-runbuilds.lower.rshas 634,001 bytes against the 655,360 limit.v_mad_i64_i32next to the arm this PR changes. A three-way merge of #635 with this PR, and of each withmain, is clean.No in-game effect is claimed.
Generated with Claude Code
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
83879d2onto main368967f8on Linux, added an evaluator of the emitted module (test code), loweredv_mad_u64_u32in 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).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.