Skip to content

Recompiler: direct f32 compares test for NaN explicitly again - #3539

Open
ArtuArtaza wants to merge 1 commit into
xcl-dev:mainfrom
ArtuArtaza:fix/f32-compare-nan
Open

ArtuArtaza wants to merge 1 commit into
xcl-dev:mainfrom
ArtuArtaza:fix/f32-compare-nan

Conversation

@ArtuArtaza

Copy link
Copy Markdown

Since a899ec90 (#3269) three GPU tests fail on my RX 6700 XT (driver 32.0.21045.29003): gpu_f32_compares_with_nan, gpu_float_compares_random and f32_programs_on_the_gpu. That commit lowered the direct translator's f32 compares (f16 ones go through f32) to one fcmp each and dropped the explicit NaN tests, which the translators have because of an AMD driver bug (#46). This PR puts the explicit form back for f32. Related: #46, #3269, #2258, #3023, #42.

The DXIL of a899ec90 is right; the driver does two wrong things with it:

  • it takes fcmp one (what v_cmp_lg became) for une: a NaN operand gives true instead of false;
  • it gives the second of two compares of the same operands that differ only in ordered / unordered (olt and ult, oeq and ueq, ...) the first one's result, in either order, also when one of them is written with its operands swapped (olt a, b and ugt b, a). v_cmp_lt_f32 then v_cmp_nge_f32 is such a pair.

How it was narrowed (local probes, not in this PR):

Other forms tried on this card:

form pairs wrong (of 182) the three tests DXIL of 567 sampled title programs
main (one fcmp) 36 fail 24,387,884 B
lg as olt or ogt, the rest one fcmp (amd-dev's form) 16 fail not measured
ordered predicates only, nX = xor (oX), true, lg = olt or ogt 0 pass 24,403,784 B (+0.07 %)
explicit NaN tests (this PR) 0 pass 24,789,936 B (+1.65 %)

The third form is much smaller, but it relies on the driver not folding the xor back into an unordered compare, and I can only measure one driver. The explicit form does not depend on what the driver makes of an fcmp of a NaN, and it is what main emitted until a899ec90. If the third form also passes on RDNA3 / RDNA4 drivers, it could replace this one.

What is in it

  • crates/xcl/src/gpu/dxil/lower_valu.rs: float_compare loses its f32 branch, so f32 takes the explicit form that f64 already takes; the comment gives the reason.
  • Same file, test f32_compares_use_no_predicate_that_amd_merges: the 14 f32 compares, wave32 and wave64, must not disassemble to one, ueq, ugt, uge, ult or ule. It needs no GPU: Recompiler: direct DXIL optimizer runs to a fixed point with new passes #3269's NVIDIA runs of the GPU tests passed, so they did not show this.
  • No TRANSLATOR_VERSION bump: a gpu/dxil edit changes the direct translations' cache identity by itself.

Tested

  • AMD Radeon RX 6700 XT (driver 32.0.21045.29003), Ryzen 5 5600X, 48 GB, Windows 11 build 26200.9974, rustc 1.100.0; tests in debug, title runs in release (both without debug info: CARGO_PROFILE_DEV_DEBUG=0, CARGO_PROFILE_RELEASE_DEBUG=0).
  • cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4 on e921d6a9 (on main 9dfbe3ea): 2884 passed, 4 failed, 51 ignored (44 test binaries). The 4 are 3 tests, and all 3 fail with the same values on unmodified main 9dfbe3ea on this card (ran them there alone): gpu_compact_form_with_micro_blocks_and_literal_table_runs_as_the_plain_form (what Recompiler: compact literal indices are read from one table word on AMD #3473 fixes), gpu_direct_image_get_resinfo_d16 (see Tests: name the trailing VGPRs in the D16 resinfo kernel test #3513) and two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs (parent and child run). 4 SKIPPED (2 no heap windows, 1 no GPU_UPLOAD heaps, 1 cannot disable the driver cache).
  • The three tests of this PR (cargo test -p xcl --lib -- gpu_f32_compares_with_nan gpu_float_compares_random f32_programs_on_the_gpu --test-threads=1): pass on cfb9753a and 5b0d5fa3, fail on a899ec90, pass here. The new test fails with main's float_compare (fcmp one) and passes with this one.
  • Size: 567 programs of a retail title (xcl shaders <archive> --direct --sample 600 --seed 3 --max-bytes 40000 --split 0 --timeout 120, summed DXIL, both forms from one local build of 9dfbe3ea with a switch): 24,387,884 B on main, 24,789,936 B here (+1.65 %).
  • How common the compare is in that title, counted in the optimized IR of main's form by a local counter, 3,826 translated programs (--sample 4000 --seed 11 --max-bytes 60000 --split 0): 1,925 programs hold an fcmp one (15,852 of 929,962 f32 fcmps), 5 hold a pair the driver would merge. It only matters in lanes where an operand is NaN; whether the picture changes was not measured.

Regression run

60 s (menus), scripts/smoke.ps1, XCL_DEVICE_TYPE=lockhart XCL_PREPARE=0, A pressed every 15 s from 30 s: main 9dfbe3ea, then this PR's head (which ran first).

xcl-smoke v1
commit: 9dfbe3ea6653a9c76ef3ea0d66187a4b5407483b
gpu: AMD Radeon RX 6700 XT
title: 2c508222
seconds: 60
exit: 0x0
frames shown: 1551 (PresentX 1551)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13647 MB
xcl-smoke v1
commit: e921d6a91e440d96ea1e3a650bde8955074f2002
gpu: AMD Radeon RX 6700 XT
title: 2c508222
seconds: 60
exit: 0x0
frames shown: 1550 (PresentX 1550)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13623 MB

Not tested

  • Any other GPU or driver. NVIDIA and Intel get the same change (the form they had until a899ec90).
  • RDNA3 and RDNA4: the reports cited above are other people's, on other forms of this change.
  • Driver compile time of the larger DXIL, and frame time in the world: the 60 s runs do not reach it.
  • The image of the title before and after.

AI use

  • Claude Code with Claude Opus 5.5 found the commit, wrote the probes and the fix, ran everything above on my machine and wrote this description.

🤖 Generated with Claude Code

@github-actions github-actions Bot added area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Nov 30, 2026
@ArtuArtaza
ArtuArtaza marked this pull request as ready for review November 30, 2026 18:33

@Zippp69 Zippp69 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.

Skip the predicate regression test when DXC is unavailable

The new predicate regression test fails on hosts without DXC before it can check the predicates. Check dxc::location() up front and use skip_test("no DXC") if the compiler is unavailable. Keep validation and disassembly failures visible when DXC is installed.

Local verification on Windows 11, DXC 1.9.2602.17. I applied the PR diff to 9dfbe3e, verified that the changed source file matches e921d6a, and ran the original and guarded variants separately with cargo test --offline --locked -p xcl --lib <test> -- --exact --nocapture.

  • With DXC available, both variants passed (1 passed each).
  • With compiler discovery disabled by a test-only hook, the original failed during translation with dxcompiler.dll not found. The guarded variant reported SKIPPED: no DXC and exited successfully.

Codex assisted with the review, patch, and targeted test runs.

@Gevroska

Copy link
Copy Markdown

@ArtuArtaza I ran your predicate test unchanged on parent 9dfbe3ea and PR head e921d6a9. On the parent, DXIL validation succeeded, but the assertion caught fcmp one in the wave32 disassembly (wave64 false: fcmp one). On the head, the assertion passed in both wave32 and wave64 emission modes.

I also ran the three existing numerical tests on my RTX 3080 (driver 616.92, DXC 1.9.2609.5, XCL_DIRECT_DXIL=1). All three passed on both revisions. The random test checked 1,600 lanes × 33 comparisons per precision with zero distinct failures. The program test passed the same six wave32 cases and skipped the same six native-wave64 cases on both builds.

The regression test catches the old instruction on the parent. The numerical results showed no regression on this NVIDIA adapter. I didn’t test the reported AMD driver behavior.

I used Codex GPT-6.2 Sol to run the comparison. A separate GPT-6.2 Sol agent reviewed the source and test evidence. Codex GPT-6.2 Astra drafted this comment.

I reviewed and approved this comment before posting.

@andresd-cerebras andresd-cerebras 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.

Withdrawn.

a899ec9 (xcl-dev#3269) lowered the direct translator's f32 compares (f16 ones go
through f32) to one `fcmp` each, without the explicit NaN tests of xcl-dev#46. Three
GPU tests fail on an RX 6700 XT (32.0.21045.29003) since then:
gpu_f32_compares_with_nan, gpu_float_compares_random and
f32_programs_on_the_gpu. They pass on its parent 5b0d5fa.

The DXIL is right, the driver is not. It takes `fcmp one` (v_cmp_lg) for `une`,
so a NaN operand gives true, and it gives the second of two compares of the
same operands that differ only in ordered / unordered (`olt` and `ult`) the
first one's result, in either order. Two plain fcmps in a kernel without any
wave operation show it: 36 of the 182 ordered pairs of predicates are wrong on
that card and 0 on WARP, the same with XCL_DXIL_NO_OPT=all, so no optimizer
pass takes part. xcl-dev#46 had found the second form on RDNA3 and an RDNA2 iGPU.

Splitting only `lg` into `olt | ogt` leaves 16 pairs wrong and the three tests
failing. The explicit tests do not depend on what the driver makes of an fcmp
of a NaN. They cost 1.65 % more DXIL over 567 sampled programs of a retail
title.

The new test reads the predicates from the disassembly, so it fails without an
AMD card too.

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

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review (head 261483f, diff read, not a merge-check: the PR description check fails, so not built or run here yet):

  • The test counts and both smoke blocks name e921d6a, not the head 261483f, which is why PR description fails. The two differ only in the new test's DXC skip_test guard, so rerunning scripts/smoke.ps1 on the head and updating the counts should do.
  • Outside this PR: lower_image.rs ~473 still emits fcmp une.

Fine: removing the f32 branch brings back exactly the explicit NaN form (isnan through isSpecialFloat plus ordered compares) main had before a899ec9 (#3269); f16 compares become f32 first and f64 already used this form (lower_wide.rs ~889); the fcmp the translator emits carries no fast-math flags (lower.rs ~722); f32_compares_use_no_predicate_that_amd_merges fails on main's fcmp one, needs no GPU and skips without DXC, and its positive fcmp olt float check keeps the other assertions from passing vacuously. The cost is about two isnan and one or two logic ops per compare (+1.65 % DXIL as measured by the author, not checked here).

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head 261483f

No other open PR restores the explicit NaN tests for f32 compares. #2258 goes the other way: one fcmp per compare, for f64 too. a899ec9 (#3269) brought that form to main for f32, and this PR reverts it for f32.
No problems found in the diff: f32 now goes through the same rel/isnan_any path as f64. f32_compares_use_no_predicate_that_amd_merges checks, after the optimizer, that no one/ueq/ugt/uge/ult/ule comes back in wave32 or wave64.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants