[mlir][Math] Add constant folder for CosOp.
authorjacquesguan <Jianjian.Guan@streamcomputing.com>
Fri, 5 Aug 2022 03:03:20 +0000 (11:03 +0800)
committerjacquesguan <Jianjian.Guan@streamcomputing.com>
Wed, 7 Sep 2022 02:54:08 +0000 (10:54 +0800)
This patch adds constant folder for CosOp which only supports single and double precision floating-point.

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

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 21b5db8..5923f8c 100644 (file)
@@ -258,6 +258,7 @@ def Math_CosOp : Math_FloatUnaryOp<"cos"> {
     %a = math.cos %b : f64
     ```
   }];
+  let hasFolder = 1;
 }
 
 //===----------------------------------------------------------------------===//
index 7369110..4d6e97a 100644 (file)
@@ -105,6 +105,24 @@ OpFoldResult math::CopySignOp::fold(ArrayRef<Attribute> operands) {
 }
 
 //===----------------------------------------------------------------------===//
+// CosOp folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::CosOp::fold(ArrayRef<Attribute> operands) {
+  return constFoldUnaryOpConditional<FloatAttr>(
+      operands, [](const APFloat &a) -> Optional<APFloat> {
+        switch (a.getSizeInBits(a.getSemantics())) {
+        case 64:
+          return APFloat(cos(a.convertToDouble()));
+        case 32:
+          return APFloat(cosf(a.convertToFloat()));
+        default:
+          return {};
+        }
+      });
+}
+
+//===----------------------------------------------------------------------===//
 // CountLeadingZerosOp folder
 //===----------------------------------------------------------------------===//
 
index 5fde49e..e74e76a 100644 (file)
@@ -358,3 +358,20 @@ func.func @atan2_fold_vec() -> (vector<4xf32>) {
   return %0 : vector<4xf32>
 }
 
+// CHECK-LABEL: @cos_fold
+// CHECK-NEXT: %[[cst:.+]] = arith.constant 0.540302277 : f32
+// CHECK-NEXT:   return %[[cst]]
+func.func @cos_fold() -> f32 {
+  %c = arith.constant 1.0 : f32
+  %r = math.cos %c : f32
+  return %r : f32
+}
+
+// CHECK-LABEL: @cos_fold_vec
+// CHECK-NEXT: %[[cst:.+]] = arith.constant dense<[1.000000e+00, 0.540302277, 1.000000e+00, 0.540302277]> : vector<4xf32>
+// CHECK-NEXT:   return %[[cst]]
+func.func @cos_fold_vec() -> (vector<4xf32>) {
+  %v1 = arith.constant dense<[0.0, 1.0, 0.0, 1.0]> : vector<4xf32>
+  %0 = math.cos %v1 : vector<4xf32>
+  return %0 : vector<4xf32>
+}
index 242e6ea..2f0669b 100644 (file)
@@ -346,38 +346,31 @@ func.func @sin() {
 // cos.
 // -------------------------------------------------------------------------- //
 
-func.func @cos() {
+func.func @cos(%zero : f32, %pi_over_4 : f32, %pi_over_2 : f32, %pi : f32, %pi_3_over_2 : f32, %vec_cos : vector<3xf32>) {
   // CHECK: 1
-  %0 = arith.constant 0.0 : f32
-  %cos_0 = math.cos %0 : f32
+  %cos_0 = math.cos %zero : f32
   vector.print %cos_0 : f32
 
   // CHECK: 0.707107
-  %pi_over_4 = arith.constant 0.78539816339 : f32
   %cos_pi_over_4 = math.cos %pi_over_4 : f32
   vector.print %cos_pi_over_4 : f32
 
   //// CHECK: 0
-  %pi_over_2 = arith.constant 1.57079632679 : f32
   %cos_pi_over_2 = math.cos %pi_over_2 : f32
   vector.print %cos_pi_over_2 : f32
 
   /// CHECK: -1
-  %pi = arith.constant 3.14159265359 : f32
   %cos_pi = math.cos %pi : f32
   vector.print %cos_pi : f32
 
   // CHECK: 0
-  %pi_3_over_2 = arith.constant 4.71238898038 : f32
   %cos_pi_3_over_2 = math.cos %pi_3_over_2 : f32
   vector.print %cos_pi_3_over_2 : f32
 
   // CHECK: -1, -0.5, 0
-  %vec_x = arith.constant dense<[9.42477796077, 2.09439510239, -1.57079632679]> : vector<3xf32>
-  %cos_vec_x = math.cos %vec_x : vector<3xf32>
+  %cos_vec_x = math.cos %vec_cos : vector<3xf32>
   vector.print %cos_vec_x : vector<3xf32>
 
-
   return
 }
 
@@ -507,7 +500,13 @@ func.func @main() {
   call @exp(): () -> ()
   call @expm1(): () -> ()
   call @sin(): () -> ()
-  call @cos(): () -> ()
+  %zero = arith.constant 0.0 : f32
+  %pi_over_4 = arith.constant 0.78539816339 : f32
+  %pi_over_2 = arith.constant 1.57079632679 : f32
+  %pi = arith.constant 3.14159265359 : f32
+  %pi_3_over_2 = arith.constant 4.71238898038 : f32
+  %vec_cos = arith.constant dense<[9.42477796077, 2.09439510239, -1.57079632679]> : vector<3xf32>
+  call @cos(%zero, %pi_over_4, %pi_over_2, %pi, %pi_3_over_2, %vec_cos): (f32, f32, f32, f32, f32, vector<3xf32>) -> ()
   call @atan() : () -> ()
   call @atan2() : () -> ()
   return