From 261483f892bc2574cc78b329fc3e4d4ea43706ca Mon Sep 17 00:00:00 2001 From: ArtuArtaza <71021164+ArtuArtaza@users.noreply.github.com> Date: Mon, 30 Nov 2026 14:20:10 -0300 Subject: [PATCH] Recompiler: direct f32 compares test for NaN explicitly again a899ec90 (#3269) lowered the direct translator's f32 compares (f16 ones go through f32) to one `fcmp` each, without the explicit NaN tests of #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 5b0d5fa3. 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. #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 --- crates/xcl/src/gpu/dxil/lower_valu.rs | 60 ++++++++++++++++----------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/lower_valu.rs b/crates/xcl/src/gpu/dxil/lower_valu.rs index d98e6a369c..4e457019ed 100644 --- a/crates/xcl/src/gpu/dxil/lower_valu.rs +++ b/crates/xcl/src/gpu/dxil/lower_valu.rs @@ -1421,31 +1421,12 @@ impl Lw { }) } - /// Float compare: single-instruction IEEE-754 ordered/unordered `fcmp` for `f32`, - /// explicit `isnan_d` for `f64`. + /// The recompiler's `float_compare`: NaN tested explicitly, so the result does not depend on + /// what a driver makes of an `fcmp` of a NaN. AMD's (RX 6700 XT, 32.0.21045.29003) takes + /// `fcmp one` for `une`, and gives the second of two compares of the same operands that differ + /// only in ordered / unordered (`olt` and `ult`) the first one's result; WARP runs the same + /// DXIL right. pub fn float_compare(&mut self, opn: &str, a: Value, b: Value) -> Option { - let is_f32 = self.s.m.type_of(a) == self.s.m.f32(); - if is_f32 { - return Some(match opn { - "f" => self.b(false), - "t" | "tru" => self.b(true), - "lt" => self.fcmp(Pred::FOLt, a, b), - "eq" => self.fcmp(Pred::FOEq, a, b), - "le" => self.fcmp(Pred::FOLe, a, b), - "gt" => self.fcmp(Pred::FOGt, a, b), - "ge" => self.fcmp(Pred::FOGe, a, b), - "lg" => self.fcmp(Pred::FONe, a, b), - "o" => self.fcmp(Pred::FOrd, a, b), - "u" => self.fcmp(Pred::FUno, a, b), - "nlt" => self.fcmp(Pred::FUGe, a, b), - "neq" => self.fcmp(Pred::FUNe, a, b), - "nle" => self.fcmp(Pred::FUGt, a, b), - "ngt" => self.fcmp(Pred::FULe, a, b), - "nge" => self.fcmp(Pred::FULt, a, b), - "nlg" => self.fcmp(Pred::FUEq, a, b), - _ => return None, - }); - } let rel = |s: &mut Self, r: &str| -> Option { Some(match r { "lt" => s.fcmp(Pred::FOLt, a, b), @@ -1549,6 +1530,37 @@ impl Lw { } } +#[cfg(test)] +mod float_compare_tests { + use super::super::kernel::{translate, KernelOptions}; + use super::super::validate; + use crate::gpu::rdna2::encode; + use crate::gpu::recomp::{dxc, runner}; + + /// The predicates `float_compare` must leave out (its comment says why), without a GPU: + /// the GPU tests (`gpu_f32_compares_with_nan`) only fail on an AMD driver. + #[test] + fn f32_compares_use_no_predicate_that_amd_merges() { + if dxc::location().is_err() { + return runner::skip_test("no DXC"); + } + let mut code = Vec::new(); + for (i, op) in ["LT", "EQ", "LE", "GT", "LG", "GE", "O", "U", "NGE", "NLG", "NGT", "NLE", "NEQ", "NLT"].into_iter().enumerate() { + code.extend(encode(&format!("V_CMP_{op}_F32"), "ENC_VOP3", &[("VDST", 20 + 2 * i as u64), ("SRC0", 256), ("SRC1", 257)], None).unwrap()); + } + code.extend(encode("S_ENDPGM", "ENC_SOPP", &[], None).unwrap()); + for wave64 in [false, true] { + let dxil = translate(&code, KernelOptions { wave64, split: false, mem_windows: 1, sampler_base: 0 }).expect("translate f32 compares"); + validate::validate(&dxil).expect("validate f32 compares"); + let dis = validate::disassemble(&dxil).expect("disassemble f32 compares"); + assert!(dis.contains("fcmp olt float"), "{dis}"); + for p in ["one", "ueq", "ugt", "uge", "ult", "ule"] { + assert!(!dis.contains(&format!("fcmp {p} float")), "wave64 {wave64}: fcmp {p}\n{dis}"); + } + } + } +} + #[cfg(test)] mod valu_carry_tests { use super::super::kernel::{self, KernelOptions};