diff --git a/mlir/include/mlir/Dialect/Math/IR/MathOps.td b/mlir/include/mlir/Dialect/Math/IR/MathOps.td --- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td +++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td @@ -258,6 +258,7 @@ %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 --- a/mlir/lib/Dialect/Math/IR/MathOps.cpp +++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp @@ -104,6 +104,24 @@ }); } +//===----------------------------------------------------------------------===// +// CosOp folder +//===----------------------------------------------------------------------===// + +OpFoldResult math::CosOp::fold(ArrayRef operands) { + return constFoldUnaryOpConditional( + operands, [](const APFloat &a) -> Optional { + 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 --- a/mlir/test/Dialect/Math/canonicalize.mlir +++ b/mlir/test/Dialect/Math/canonicalize.mlir @@ -358,3 +358,20 @@ 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 --- a/mlir/test/mlir-cpu-runner/math-polynomial-approx.mlir +++ b/mlir/test/mlir-cpu-runner/math-polynomial-approx.mlir @@ -346,38 +346,31 @@ // 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 @@ 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