[mlir][Math] Add constant folder for AtanOp.
authorjacquesguan <Jianjian.Guan@streamcomputing.com>
Tue, 2 Aug 2022 11:49:11 +0000 (19:49 +0800)
committerjacquesguan <Jianjian.Guan@streamcomputing.com>
Wed, 3 Aug 2022 06:10:02 +0000 (14:10 +0800)
This patch adds constant folder for AtanOp which only supports single and double precision floating-point.

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

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

index 0b6023d..9ad9c54 100644 (file)
@@ -110,6 +110,7 @@ def Math_AtanOp : Math_FloatUnaryOp<"atan">{
     %a = math.atan %b : f64
     ```
   }];
+  let hasFolder = 1;
 }
 
 //===----------------------------------------------------------------------===//
index 5451d89..e45db0a 100644 (file)
@@ -33,6 +33,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
 //===----------------------------------------------------------------------===//
 
index bca8ce9..e2f7b91 100644 (file)
@@ -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>
+}
index 4514abd..7ebb86a 100644 (file)
@@ -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