[Mlir-commits] [mlir] [mlir][arith] Fix integer-to-float comparison canonicalization (PR #233631)

Jakub Kuderski llvmlistbot at llvm.org
Mon Oct 19 18:06:10 PDT 2026


================
@@ -2840,6 +2840,33 @@ class CmpFIntToFPConst final : public OpRewritePattern<CmpFOp> {
     // Number of bits representing values, as opposed to the sign
     auto valueBits = isUnsigned ? intWidth : (intWidth - 1);
 
+    const llvm::fltSemantics &semantics = floatTy.getFloatSemantics();
+
+    // Conservatively require an exact representation of integer zero. For
+    // example, f8E8M0FNU converts zero to its smallest positive value.
+    if (!APFloat::semanticsHasZero(semantics))
+      return failure();
+
+    // Signed inputs may be negative, which e.g. f8E5M3FNU cannot represent.
+    // Do not try to convert the signed minimum below for such formats.
+    if (!isUnsigned && !APFloat::semanticsHasSignedRepr(semantics))
+      return failure();
+
+    if (!APFloat::semanticsHasInf(semantics)) {
+      // Overflow can produce NaN or saturate to a finite value:
+      // - sitofp(1000 : i32) to f8E4M3FN is NaN, so cmpf ogt against 0.0
+      //   is false, whereas cmpi sgt against integer zero would be true.
+      // - uitofp(8 : i4) to f6E2M3FN saturates to 7.5. Equality against 7.5
+      //   is true, but the fractional-constant logic below would fold it to
+      //   false. Its four bits of precision do not prevent overflow.
+      //
+      // Conservatively require the enclosing 2^valueBits (and its negative
+      // for signed inputs) to be representable, so rounding stays within
+      // finite bounds. Equality at the exponent bound is safe.
+      if (APFloat::semanticsMaxExponent(semantics) < (int)valueBits)
----------------
kuhar wrote:

use static_cast

https://github.com/llvm/llvm-project/pull/233631


More information about the Mlir-commits mailing list