From 752c9d0dab8b6111204182dcf29a27622ae94921 Mon Sep 17 00:00:00 2001 From: jacquesguan Date: Tue, 2 Aug 2022 19:49:11 +0800 Subject: [PATCH] [mlir][Math] Add constant folder for AtanOp. This patch adds constant folder for AtanOp which only supports single and double precision floating-point. Differential Revision: https://reviews.llvm.org/D130983 --- mlir/include/mlir/Dialect/Math/IR/MathOps.td | 1 + mlir/lib/Dialect/Math/IR/MathOps.cpp | 18 ++++++++++++++++++ mlir/test/Dialect/Math/canonicalize.mlir | 17 +++++++++++++++++ .../math-polynomial-approx.mlir | 8 ++++---- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/mlir/include/mlir/Dialect/Math/IR/MathOps.td b/mlir/include/mlir/Dialect/Math/IR/MathOps.td index 0b6023d8e77a..9ad9c542a4ba 100644 --- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td +++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td @@ -110,6 +110,7 @@ def Math_AtanOp : Math_FloatUnaryOp<"atan">{ %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 index 5451d89601b6..e45db0ac958c 100644 --- a/mlir/lib/Dialect/Math/IR/MathOps.cpp +++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp @@ -32,6 +32,24 @@ OpFoldResult math::AbsOp::fold(ArrayRef operands) { }); } +//===----------------------------------------------------------------------===// +// 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 index bca8ce957277..e2f7b9149447 100644 --- a/mlir/test/Dialect/Math/canonicalize.mlir +++ b/mlir/test/Dialect/Math/canonicalize.mlir @@ -320,3 +320,20 @@ func.func @tanh_fold_vec() -> (vector<4xf32>) { 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 index 4514abd0ea85..7ebb86a97c8f 100644 --- 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 @cos() { // -------------------------------------------------------------------------- // 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