[Mlir-commits] [mlir] 752c9d0 - [mlir][Math] Add constant folder for AtanOp.

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Aug 2 23:17:23 PDT 2022


Author: jacquesguan
Date: 2022-08-03T14:10:02+08:00
New Revision: 752c9d0dab8b6111204182dcf29a27622ae94921

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

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

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

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

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/Math/IR/MathOps.td
    mlir/lib/Dialect/Math/IR/MathOps.cpp
    mlir/test/Dialect/Math/canonicalize.mlir
    mlir/test/mlir-cpu-runner/math-polynomial-approx.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/Math/IR/MathOps.td b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
index 0b6023d8e77a6..9ad9c542a4bae 100644
--- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td
+++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
@@ -110,6 +110,7 @@ def Math_AtanOp : Math_FloatUnaryOp<"atan">{
     %a = math.atan %b : f64
     ```
   }];
+  let hasFolder = 1;
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index 5451d89601b6d..e45db0ac958cf 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -32,6 +32,24 @@ OpFoldResult math::AbsOp::fold(ArrayRef<Attribute> operands) {
   });
 }
 
+//===----------------------------------------------------------------------===//
+// AtanOp folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::AtanOp::fold(ArrayRef<Attribute> operands) {
+  return constFoldUnaryOpConditional<FloatAttr>(
+      operands, [](const APFloat &a) -> Optional<APFloat> {
+        switch (a.getSizeInBits(a.getSemantics())) {
+        case 64:
+          return APFloat(atan(a.convertToDouble()));
+        case 32:
+          return APFloat(atanf(a.convertToFloat()));
+        default:
+          return {};
+        }
+      });
+}
+
 //===----------------------------------------------------------------------===//
 // CeilOp folder
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/test/Dialect/Math/canonicalize.mlir b/mlir/test/Dialect/Math/canonicalize.mlir
index bca8ce957277d..e2f7b91494471 100644
--- a/mlir/test/Dialect/Math/canonicalize.mlir
+++ b/mlir/test/Dialect/Math/canonicalize.mlir
@@ -320,3 +320,20 @@ func.func @tanh_fold_vec() -> (vector<4xf32>) {
   return %0 : vector<4xf32>
 }
 
+// CHECK-LABEL: @atan_fold
+// CHECK-NEXT: %[[cst:.+]] = arith.constant 0.785398185 : f32
+// CHECK-NEXT:   return %[[cst]]
+func.func @atan_fold() -> f32 {
+  %c = arith.constant 1.0 : f32
+  %r = math.atan %c : f32
+  return %r : f32
+}
+
+// CHECK-LABEL: @atan_fold_vec
+// CHECK-NEXT: %[[cst:.+]] = arith.constant dense<[0.000000e+00, 0.785398185, 0.000000e+00, 0.785398185]> : vector<4xf32>
+// CHECK-NEXT:   return %[[cst]]
+func.func @atan_fold_vec() -> (vector<4xf32>) {
+  %v1 = arith.constant dense<[0.0, 1.0, 0.0, 1.0]> : vector<4xf32>
+  %0 = math.atan %v1 : vector<4xf32>
+  return %0 : vector<4xf32>
+}

diff  --git a/mlir/test/mlir-cpu-runner/math-polynomial-approx.mlir b/mlir/test/mlir-cpu-runner/math-polynomial-approx.mlir
index 4514abd0ea850..7ebb86a97c8f7 100644
--- a/mlir/test/mlir-cpu-runner/math-polynomial-approx.mlir
+++ b/mlir/test/mlir-cpu-runner/math-polynomial-approx.mlir
@@ -386,22 +386,22 @@ func.func @cos() {
 // -------------------------------------------------------------------------- //
 
 func.func @atan() {
-  // CHECK: -0.785184
+  // CHECK: -0.785398
   %0 = arith.constant -1.0 : f32
   %atan_0 = math.atan %0 : f32
   vector.print %atan_0 : f32
 
-  // CHECK: 0.785184
+  // CHECK: 0.785398
   %1 = arith.constant 1.0 : f32
   %atan_1 = math.atan %1 : f32
   vector.print %atan_1 : f32
 
-  // CHECK: -0.463643
+  // CHECK: -0.463648
   %2 = arith.constant -0.5 : f32
   %atan_2 = math.atan %2 : f32
   vector.print %atan_2 : f32
 
-  // CHECK: 0.463643
+  // CHECK: 0.463648
   %3 = arith.constant 0.5 : f32
   %atan_3 = math.atan %3 : f32
   vector.print %atan_3 : f32


        


More information about the Mlir-commits mailing list