[Mlir-commits] [mlir] f6fab68 - Fold arith.cmpf when at least one operand is known to be NaN.

Christian Sigg llvmlistbot at llvm.org
Wed Jan 12 11:49:00 PST 2022


Author: Christian Sigg
Date: 2022-01-12T20:48:52+01:00
New Revision: f6fab68c30622b7c97b0d21b5ea02809210444eb

URL: https://github.com/llvm/llvm-project/commit/f6fab68c30622b7c97b0d21b5ea02809210444eb
DIFF: https://github.com/llvm/llvm-project/commit/f6fab68c30622b7c97b0d21b5ea02809210444eb.diff

LOG: Fold arith.cmpf when at least one operand is known to be NaN.

Reviewed By: herhut

Differential Revision: https://reviews.llvm.org/D117101

Added: 
    

Modified: 
    mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
    mlir/test/Dialect/Arithmetic/canonicalize.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp b/mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
index 188d6e56543a4..9beecb6f66d41 100644
--- a/mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
+++ b/mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
@@ -1299,6 +1299,12 @@ OpFoldResult arith::CmpFOp::fold(ArrayRef<Attribute> operands) {
   auto lhs = operands.front().dyn_cast_or_null<FloatAttr>();
   auto rhs = operands.back().dyn_cast_or_null<FloatAttr>();
 
+  // If one operand is NaN, making them both NaN does not change the result.
+  if (lhs && lhs.getValue().isNaN())
+    rhs = lhs;
+  if (rhs && rhs.getValue().isNaN())
+    lhs = rhs;
+
   if (!lhs || !rhs)
     return {};
 

diff  --git a/mlir/test/Dialect/Arithmetic/canonicalize.mlir b/mlir/test/Dialect/Arithmetic/canonicalize.mlir
index 54259741b7a64..22a88d165d300 100644
--- a/mlir/test/Dialect/Arithmetic/canonicalize.mlir
+++ b/mlir/test/Dialect/Arithmetic/canonicalize.mlir
@@ -691,3 +691,17 @@ func @constant_MinMax(%arg0 : f32) -> f32 {
   %res = arith.maxf %const, %min : f32
   return %res : f32
 }
+
+// -----
+// CHECK-LABEL: @cmpf_nan(
+func @cmpf_nan(%arg0 : f32) -> (i1, i1, i1, i1) {
+//   CHECK-DAG:   %[[T:.*]] = arith.constant true
+//   CHECK-DAG:   %[[F:.*]] = arith.constant false
+//       CHECK:   return %[[F]], %[[F]], %[[T]], %[[T]]
+  %nan = arith.constant 0x7fffffff : f32
+  %0 = arith.cmpf olt, %nan, %arg0 : f32
+  %1 = arith.cmpf olt, %arg0, %nan : f32
+  %2 = arith.cmpf ugt, %nan, %arg0 : f32
+  %3 = arith.cmpf ugt, %arg0, %nan : f32
+  return %0, %1, %2, %3 : i1, i1, i1, i1
+}


        


More information about the Mlir-commits mailing list