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 @@ -110,6 +110,7 @@ %a = math.atan %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 @@ -32,6 +32,24 @@ }); } +//===----------------------------------------------------------------------===// +// AtanOp folder +//===----------------------------------------------------------------------===// + +OpFoldResult math::AtanOp::fold(ArrayRef operands) { + return constFoldUnaryOpConditional( + operands, [](const APFloat &a) -> Optional { + switch (a.getSizeInBits(a.getSemantics())) { + case 64: + return APFloat(atan(a.convertToDouble())); + case 32: + return APFloat(atanf(a.convertToFloat())); + default: + return {}; + } + }); +} + //===----------------------------------------------------------------------===// // CeilOp 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 @@ -320,3 +320,20 @@ 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> +} 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 @@ -386,22 +386,22 @@ // -------------------------------------------------------------------------- // 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