[Mlir-commits] [mlir] ac66d87 - [mlir][Math] Add constant folder for RoundEvenOp.

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Sep 6 20:13:25 PDT 2022


Author: jacquesguan
Date: 2022-09-07T11:13:00+08:00
New Revision: ac66d87c4b964af9edff3667dd17776f7206cc6b

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

LOG: [mlir][Math] Add constant folder for RoundEvenOp.

This patch uses roundeven/roundevenf of libm to fold RoundEvenOp of constant.

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

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/Math/IR/MathOps.td
    mlir/lib/Dialect/Math/IR/MathOps.cpp
    mlir/test/Dialect/Math/canonicalize.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/Math/IR/MathOps.td b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
index 5923f8c412d7e..eeadf9526a9b3 100644
--- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td
+++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
@@ -769,6 +769,7 @@ def Math_RoundEvenOp : Math_FloatUnaryOp<"roundeven"> {
     %a = math.roundeven %b : f64
     ```
   }];
+  let hasFolder = 1;
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index 4d6e97a47e3b4..667c0e66bcd63 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -415,6 +415,24 @@ OpFoldResult math::TanhOp::fold(ArrayRef<Attribute> operands) {
       });
 }
 
+//===----------------------------------------------------------------------===//
+// RoundEvenOp folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::RoundEvenOp::fold(ArrayRef<Attribute> operands) {
+  return constFoldUnaryOpConditional<FloatAttr>(
+      operands, [](const APFloat &a) -> Optional<APFloat> {
+        switch (a.getSizeInBits(a.getSemantics())) {
+        case 64:
+          return APFloat(roundeven(a.convertToDouble()));
+        case 32:
+          return APFloat(roundevenf(a.convertToFloat()));
+        default:
+          return {};
+        }
+      });
+}
+
 /// Materialize an integer or floating point constant.
 Operation *math::MathDialect::materializeConstant(OpBuilder &builder,
                                                   Attribute value, Type type,

diff  --git a/mlir/test/Dialect/Math/canonicalize.mlir b/mlir/test/Dialect/Math/canonicalize.mlir
index e74e76a035ef9..e2e751b6745f7 100644
--- a/mlir/test/Dialect/Math/canonicalize.mlir
+++ b/mlir/test/Dialect/Math/canonicalize.mlir
@@ -375,3 +375,21 @@ func.func @cos_fold_vec() -> (vector<4xf32>) {
   %0 = math.cos %v1 : vector<4xf32>
   return %0 : vector<4xf32>
 }
+
+// CHECK-LABEL: @roundeven_fold
+// CHECK-NEXT: %[[cst:.+]] = arith.constant 2.000000e+00 : f32
+// CHECK-NEXT:   return %[[cst]]
+func.func @roundeven_fold() -> f32 {
+  %c = arith.constant 1.5 : f32
+  %r = math.roundeven %c : f32
+  return %r : f32
+}
+
+// CHECK-LABEL: @roundeven_fold_vec
+// CHECK-NEXT: %[[cst:.+]] = arith.constant dense<[0.000000e+00, -0.000000e+00, 2.000000e+00, -2.000000e+00]> : vector<4xf32>
+// CHECK-NEXT:   return %[[cst]]
+func.func @roundeven_fold_vec() -> (vector<4xf32>) {
+  %v1 = arith.constant dense<[0.5, -0.5, 1.5, -1.5]> : vector<4xf32>
+  %0 = math.roundeven %v1 : vector<4xf32>
+  return %0 : vector<4xf32>
+}


        


More information about the Mlir-commits mailing list