[Mlir-commits] [mlir] e3434a8 - [mlir][Math] Add constant folder for CosOp.

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Sep 6 19:54:28 PDT 2022


Author: jacquesguan
Date: 2022-09-07T10:54:08+08:00
New Revision: e3434a862797e74b0b54559366eaaeb137fa2ff7

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

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

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

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

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 21b5db85f484c..5923f8c412d7e 100644
--- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td
+++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
@@ -258,6 +258,7 @@ def Math_CosOp : Math_FloatUnaryOp<"cos"> {
     %a = math.cos %b : f64
     ```
   }];
+  let hasFolder = 1;
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index 7369110e96acf..4d6e97a47e3b4 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -104,6 +104,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
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/test/Dialect/Math/canonicalize.mlir b/mlir/test/Dialect/Math/canonicalize.mlir
index 5fde49ed96db9..e74e76a035ef9 100644
--- a/mlir/test/Dialect/Math/canonicalize.mlir
+++ b/mlir/test/Dialect/Math/canonicalize.mlir
@@ -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>
+}

diff  --git a/mlir/test/mlir-cpu-runner/math-polynomial-approx.mlir b/mlir/test/mlir-cpu-runner/math-polynomial-approx.mlir
index 242e6ea2fdeaf..2f0669bc2daa2 100644
--- a/mlir/test/mlir-cpu-runner/math-polynomial-approx.mlir
+++ b/mlir/test/mlir-cpu-runner/math-polynomial-approx.mlir
@@ -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


        


More information about the Mlir-commits mailing list