Skip to content

Shaders: 64-bit integer compares on their dwords (backport of Marvisak/VIctory#1599, reproduced on an RX 7900 XT) - #1506

Open
murillojndem wants to merge 1 commit into
fxceless:mainfrom
murillojndem:fix/amd-i64-compare-dwords
Open

murillojndem wants to merge 1 commit into
fxceless:mainfrom
murillojndem:fix/amd-i64-compare-dwords

Conversation

@murillojndem

@murillojndem murillojndem commented Nov 28, 2026 •

Copy link
Copy Markdown

v_cmp_{lt,le,gt,ge,eq,ne}_{i64,u64} (and the v_cmpx_ forms, which reach the same code) are lowered to one icmp on 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_pair here and before split64 upstream). 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 touch lower.rs or valu_tests.rs. Synthetic shaders only; no game files.

  • Without the lowering change, v_cmp_u64_and_i64_order_by_the_high_dword fails 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.
  • A census over the same values with the lowering of 9e90746 (a local probe, not in this PR) runs every case: on the card only the shared form v[1:2], v[2:3] is wrong, and only for the ordered compares. lt, le, gt and ge get 28 or 29 of 100 cases wrong for _i64 and 49 to 56 of 100 for _u64. eq, ne and the disjoint form v[1:2], v[3:4] have no mismatch. WARP has none.
  • With the lowering change, the test passes on the card and on WARP. rdna_forms_give_what_the_spelled_out_ones_give ran in the same binary without its "no RDNA card" line, so the hardware device was the Radeon.
  • On 9e90746, cargo test -p victory-shader --lib with VICTORY_WARP=1: 288 passed, 7 failed, 4 ignored. The same 7 fail there without the change (287 passed). Three are PSV0 mismatches with the dxil.dll of SDK 10.0.22621 (101.6.2112.13); emit::rect tests fail with the Windows SDK 22621 DXIL validator (PSV0 mismatch) #1337 reports the two rect ones with that validator, and barycentric_tests::first_half_values_render_on_hardware_and_warp gives the same error. The other four fail only under VICTORY_WARP=1, which puts WARP in the hardware device's place; on the card they pass.
  • victory-shader library 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.
  • Clippy in both CI configurations, with --keep-going: the same 15 errors with and without this change on 6b3ce6c, none in the changed files. (On main the CI job stops at its Clippy step, so its Tests step does not run.) 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 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), and s_cmp_*_u64 tests 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

…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>
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 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).

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