Repository navigation
Shaders: the carry of v_mad_u64_u32 on its dwords (wrong on AMD when the addend shares a register with a factor) - #1507
Conversation
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>
4fb28d3 to
83879d2
Compare
…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
left a comment
There was a problem hiding this comment.
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).
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 (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_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), on 6b3ce6c: 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. On 9e90746 the same pair was 291/4/4 and 292/3/4.--keep-going: the same 15 errors with and without this change on 6b3ce6c, 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 Shaders: lower v_mad_i64_i32; test the 64-bit integer vector operations against their definitions #635 with this PR, and of each withmain, is clean.No in-game effect is claimed.
Generated with Claude Code