Skip to content

Shaders: v_cmp_lg is ordered on AMD hosts too (olt | ogt, not fcmp one) - #706

Merged
Marvisak merged 1 commit into
Marvisak:mainfrom
Lyrnc:fix/cmp-lg-ordered
Nov 24, 2026
Merged

Marvisak merged 1 commit into
Marvisak:mainfrom
Lyrnc:fix/cmp-lg-ordered

Conversation

@Lyrnc

@Lyrnc Lyrnc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

What

v_cmp_lg_f32 / v_cmp_lg_f16 ("less or greater") are ordered not-equal compares. They are false when either operand is a NaN, on RDNA2 hardware as in the ISA. compare lowered them to fcmp one, which means the same thing in LLVM. The AMD D3D12 driver runs fcmp one as unordered and returns true for a NaN. NVIDIA and WARP run it correctly. So on AMD hosts every v_cmp_lg with a NaN operand took the wrong side.

lg now lowers as fcmp olt | fcmp ogt, which gives the same answer on all three. pred::ONE is no longer emitted. It stays in the table with a note saying why.

How it was found and measured

A differential check of the translator against RDNA2 hardware: the same raw VOP3 words run on a gfx1036 (RDNA2) iGPU through a HIP kernel, and through build_valu_cs on a D3D12 host, 240 inputs per opcode. Out of 70 VOPC compares, v_cmp_lg_f32 and v_cmp_lg_f16 were the only ones whose results differ on an AMD host: 40 and 33 of 240, all NaN rows. With the same DXIL on other hosts:

Host main this PR
AMD Radeon iGPU (gfx1036), driver 32.0.21042.62 fails: v_cmp_lg_f32(0x7fc00000, 0x3f800000) = 1, want 0 passes
NVIDIA RTX 5080, driver 610.88 passes passes
WARP passes passes

The AMD row needs #94's VICTORY_ADAPTER=amd to put the test on the iGPU. I merged #94 locally for that run only; it is not part of this PR. olt, ogt, ord, une, ueq and the other float predicates matched the hardware on the AMD host in the same check, so only one is affected.

Test

v_cmp_lg_is_false_for_nan in valu_tests.rs builds v_cmp_lg_{f32,f16}_e64 s20, v1, v2 + v_cndmask_b32_e64 v0, 0, 1, s20 from hand-written words and runs it on the hardware device and WARP. It covers ordered pairs both ways, equal values, +0 against -0, inf against inf and -inf, NaN on either side and on both, a signalling NaN, and for f16, junk in the unread high halves. The expected values are the ISA's, and the same on the gfx1036 hardware.

  • cargo test --workspace -j 4 (Windows 11, RTX 5080 as the default adapter): 265 passed, 0 failed, 4 ignored (264 on main plus the new test).
  • cargo clippy -p victory-shader --all-targets: no warnings. rustfmt --edition 2024 --check on the three files: clean.
  • cargo xtask policy: 151 files ok.

No game files were used.

AI use: Claude Code (Claude Opus 5.5) found this with the differential check, wrote the change and the test, and ran everything above. I reviewed the diff and the results.

🤖 Generated with Claude Code

v_cmp_lg_{f32,f16} ("less or greater") is ordered and not equal: false
when either operand is a NaN, on RDNA2 hardware as in the ISA. It was
lowered to `fcmp one`, which says the same, but the AMD D3D12 driver runs
`fcmp one` as unordered and returns true for a NaN. NVIDIA and WARP get it
right. It now lowers as `olt | ogt`, which all three run alike.

New GPU test v_cmp_lg_is_false_for_nan covers both widths: NaN on either
side, both NaN, a signalling NaN, +0 against -0, inf against inf and
against -inf, and junk in the unread high halves for f16.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@slint6614-dev

Copy link
Copy Markdown

AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and the Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the D3D12 device for cargo test -p victory-shader --release (local adapter-selection patch, not part of this PR).

  • Whole victory-shader suite: 70 passed, 0 failed on both GPUs.
  • Negative control: with this PR's lower.rs/bitcode.rs reverted to main and only the new test kept, v_cmp_lg_is_false_for_nan fails on both AMD GPUs (left: 1, right: 0). So main's fcmp one lowering really returns true for NaN on AMD hardware, and this PR fixes it there.

@LAKSHYA-cmd-tech LAKSHYA-cmd-tech 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.

Verified on Windows 11 MSVC (Rust 1.98.1). \�_cmp_lg_is_false_for_nan\ passes cleanly across all f32 and f16 test vectors (signalling NaN, quiet NaN, infinities, signed zeroes, high-half masks). Lowering \�_cmp_lg\ as \olt | ogt\ avoids the AMD D3D12 driver bug where \ cmp one\ was treated as unordered (\UNE). Excellent fix.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

cargo test -p victory-shader --locked on macOS 26 arm64, rustc 1.98.1: merges cleanly with current main (7374bfb), 64 passed / 0 failed / 1 ignored — same as main's baseline (the new valu_tests coverage is #[cfg(all(test, windows))], needs the Windows host backend). The bitcode/lower changes compile and don't regress anything here. Approving; the AMD ordering behavior itself needs the Windows shader run.

@sambagarandawertanda

Copy link
Copy Markdown

I ran v_cmp_lg_is_false_for_nan on the original PR head 51f1627 with an RTX 3080 (driver 621.14) and WARP. All 20 input cases passed on each backend. This used Windows 11 and Rust 1.98.1 MSVC.

On the merged commit 58fed370, the shader tests fail to compile with E0061 in scalar_bit_tests_lower. The same call is already present on the merge's first parent, a51bdfa. The old helper used rsrc2 = 0, so I restored that value at the call site.

-            lowers(&words).unwrap();
+            lowers(&words, 0).unwrap();

With this one-line test change, both v_cmp_lg_is_false_for_nan and scalar_bit_tests_lower passed on 58fed370. The former again exercised all 40 comparisons across both backends. Tests used synthetic inputs. AI assistance with Codex.

MfDev0 pushed a commit to MfDev0/victory that referenced this pull request Nov 24, 2026
v_cmp_lg_{f32,f16} ("less or greater") is ordered and not equal: false
when either operand is a NaN, on RDNA2 hardware as in the ISA. It was
lowered to `fcmp one`, which says the same, but the AMD D3D12 driver runs
`fcmp one` as unordered and returns true for a NaN. NVIDIA and WARP get it
right. It now lowers as `olt | ogt`, which all three run alike.

New GPU test v_cmp_lg_is_false_for_nan covers both widths: NaN on either
side, both NaN, a signalling NaN, +0 against -0, inf against inf and
against -inf, and junk in the unread high halves for f16.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

(cherry picked from commit 51f1627, upstream Marvisak#706)

Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>
@slint6614-dev slint6614-dev mentioned this pull request Nov 24, 2026
suirentakamura referenced this pull request in suirentakamura/victory-suiren Nov 24, 2026
Shaders: v_cmp_lg is ordered on AMD hosts too (olt | ogt, not fcmp one)
itsonlysimbo pushed a commit to itsonlysimbo/VIctory that referenced this pull request Nov 24, 2026
Shaders: v_cmp_lg is ordered on AMD hosts too (olt | ogt, not fcmp one)
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.

6 participants