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 @@ -226,6 +226,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 @@ -97,6 +97,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 @@ -357,7 +357,7 @@ %cos_pi_over_4 = math.cos %pi_over_4 : f32 vector.print %cos_pi_over_4 : f32 - //// CHECK: 0 + //// CHECK: -4.37114e-08 %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 @@ -367,12 +367,12 @@ %cos_pi = math.cos %pi : f32 vector.print %cos_pi : f32 - // CHECK: 0 - %pi_3_over_2 = arith.constant 4.71238898038 : f32 + // CHECK: 1.19249e-08 + %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 + // CHECK: -1, -0.5, -4.37114e-08 %vec_x = arith.constant dense<[9.42477796077, 2.09439510239, -1.57079632679]> : vector<3xf32> %cos_vec_x = math.cos %vec_x : vector<3xf32> vector.print %cos_vec_x : vector<3xf32>