[Mlir-commits] [mlir] 008ea1c - [mlir][Math] Add constant folder for TanhOp.

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Aug 2 04:38:05 PDT 2022


Author: jacquesguan
Date: 2022-08-02T19:37:27+08:00
New Revision: 008ea1c20148008c5a4bd35ce3e35f3d04c3a603

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

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

This patch adds constant folder for TanhOp which only supports single and double precision floating-point.

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

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 70bdf09a85dda..0b6023d8e77a6 100644
--- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td
+++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
@@ -677,6 +677,7 @@ def Math_TanhOp : Math_FloatUnaryOp<"tanh"> {
     %a = math.tanh %b : f64
     ```
   }];
+  let hasFolder = 1;
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index ad3c1b031d369..5451d89601b6d 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -282,6 +282,24 @@ OpFoldResult math::TanOp::fold(ArrayRef<Attribute> operands) {
       });
 }
 
+//===----------------------------------------------------------------------===//
+// TanhOp folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::TanhOp::fold(ArrayRef<Attribute> operands) {
+  return constFoldUnaryOpConditional<FloatAttr>(
+      operands, [](const APFloat &a) -> Optional<APFloat> {
+        switch (a.getSizeInBits(a.getSemantics())) {
+        case 64:
+          return APFloat(tanh(a.convertToDouble()));
+        case 32:
+          return APFloat(tanhf(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 8ed20b7fb969e..bca8ce957277d 100644
--- a/mlir/test/Dialect/Math/canonicalize.mlir
+++ b/mlir/test/Dialect/Math/canonicalize.mlir
@@ -302,3 +302,21 @@ func.func @tan_fold_vec() -> (vector<4xf32>) {
   return %0 : vector<4xf32>
 }
 
+// CHECK-LABEL: @tanh_fold
+// CHECK-NEXT: %[[cst:.+]] = arith.constant 0.761594176 : f32
+// CHECK-NEXT:   return %[[cst]]
+func.func @tanh_fold() -> f32 {
+  %c = arith.constant 1.0 : f32
+  %r = math.tanh %c : f32
+  return %r : f32
+}
+
+// CHECK-LABEL: @tanh_fold_vec
+// CHECK-NEXT: %[[cst:.+]] = arith.constant dense<[0.000000e+00, 0.761594176, 0.000000e+00, 0.761594176]> : vector<4xf32>
+// CHECK-NEXT:   return %[[cst]]
+func.func @tanh_fold_vec() -> (vector<4xf32>) {
+  %v1 = arith.constant dense<[0.0, 1.0, 0.0, 1.0]> : vector<4xf32>
+  %0 = math.tanh %v1 : vector<4xf32>
+  return %0 : vector<4xf32>
+}
+


        


More information about the Mlir-commits mailing list