Repository navigation
Shaders: 64-bit integer compares on their dwords (backport of Marvisak/VIctory#1599, reproduced on an RX 7900 XT) - #1506
Conversation
…k/VIctory#1599) v_cmp_{lt,le,gt,ge}_{i64,u64} lowered to one i64 icmp give the wrong answer on AMD's D3D12 driver when the operands share a dword: v_cmp_lt_i64 s20, v[1:2], v[2:3] with v1 = v2 = 0, v3 = 1 is false on an RX 7900 XT (driver 32.0.32015.7029); WARP gets it right. The compare now runs on the dwords, as in upstream's cc7ec2a by alexdeliu: the same lines and the same test. Without the lowering change the test fails on the card. Co-authored-by: alexdeliu <240865807+alexdeliu@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…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 dword form is exact, independently of any driver: no objection.
I merged de6370a onto main 368967f on Linux, added an evaluator of the emitted module (test code), lowered the twelve compares in 22 operand forms and evaluated each result: VGPR pairs apart, the same and sharing a register, SGPR pairs, inline integers (0, 1, 64, -1, -16) and a literal on either side. 2,000 random inputs per form, with zeros, ones, sign bits and equal high dwords mixed in: 528,000 compares against Rust's u64/i64, no mismatch. Literals are sign-extended for _i64 and zero-extended for _u64.
The check is not vacuous: with the low dwords of lt_i64 compared signed, 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: v_cmpx, DPP and SDWA forms, inline floats, the Windows tests.
AI-assisted review (Claude Code).
v_cmp_{lt,le,gt,ge,eq,ne}_{i64,u64}(and thev_cmpx_forms, which reach the same code) are lowered to oneicmpon an i64 built from two dwords. AMD's D3D12 compiler gets the order wrong when the two operands share a dword:v_cmp_lt_i64 s20, v[1:2], v[2:3]with v1 = v2 = 0 and v3 = 1 asks whether 0 < 0x1_00000000, and the card answers false. WARP answers true.This is a backport of Marvisak/VIctory#1599, merged upstream on 25 November as cc7ec2a (by alexdeliu). This fork's #135 is the same change by the same author; it conflicts since the tree moved to the VIctory-NVIDIA base. The compare now runs on the dwords: the high dwords decide (signed for i64), and equal high dwords leave it to the low dwords (unsigned). It no longer needs INT64. The added and removed lines are the same as in cc7ec2a; only the offsets differ (the helper sits before
f64_pairhere and beforesplit64upstream). The test is upstream's: all twelve compares over values whose dword orders disagree, on disjoint and on shared register pairs.On other cards: upstream merged these lines for every host, and #135 cites an RTX 4070 Ti that agrees with WARP on this compare. The dword form gives the same answers wherever the i64 compare is right. It was not run on an NVIDIA card here; the RX 7900 XT and WARP are the two devices tested.
Tested on an AMD Radeon RX 7900 XT (RDNA 3, 20 GB, driver 32.0.32015.7029), Windows 11, Rust 1.98.1, on 6b3ce6c (current
main) and first on 9e90746; the two commits between them do not touchlower.rsorvalu_tests.rs. Synthetic shaders only; no game files.v_cmp_u64_and_i64_order_by_the_high_dwordfails on the card:hardware: v_cmp_lt_i64(0x0000000000000000, 0x0000000100000000), 0 where 1 is right. That is the first failure reported on an RX 9070, a gfx1036 iGPU and an RX 7700.v[1:2], v[2:3]is wrong, and only for the ordered compares.lt,le,gtandgeget 28 or 29 of 100 cases wrong for_i64and 49 to 56 of 100 for_u64.eq,neand the disjoint formv[1:2], v[3:4]have no mismatch. WARP has none.rdna_forms_give_what_the_spelled_out_ones_giveran in the same binary without its "no RDNA card" line, so the hardware device was the Radeon.cargo test -p victory-shader --libwithVICTORY_WARP=1: 288 passed, 7 failed, 4 ignored. The same 7 fail there without the change (287 passed). Three are PSV0 mismatches with thedxil.dllof SDK 10.0.22621 (101.6.2112.13); emit::rect tests fail with the Windows SDK 22621 DXIL validator (PSV0 mismatch) #1337 reports the tworectones with that validator, andbarycentric_tests::first_half_values_render_on_hardware_and_warpgives the same error. The other four fail only underVICTORY_WARP=1, which puts WARP in the hardware device's place; on the card they pass.victory-shaderlibrary on the card and WARP (--test-threads=1), on 6b3ce6c: with the test alone, 314 passed, 4 failed (the three PSV0 ones 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. (Onmainthe CI job stops at its Clippy step, so its Tests step does not run.)cargo fmt --check -p victory-shaderreports no new difference.cargo xtask policy: 288 files ok.cargo test --workspace --locked --no-runbuilds.lower.rshas 636,765 bytes against the 655,360 limit.The translator has one more ordered 64-bit compare, the carry out of
v_mad_u64_u32. It is wrong on the same card and fixed in #1507, which does not depend on this one. No other one was found: the rest of the 64-bit code is arithmetic (mul_hi, the BVH node address, shifts), ands_cmp_*_u64tests equality only.No in-game effect is claimed: without the title I cannot say which of its programs reach these compares.
Generated with Claude Code