[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
This commit is contained in:
jacquesguan 2022-08-02 19:49:11 +08:00
parent c44c71843f
commit 752c9d0dab
4 changed files with 40 additions and 4 deletions

View File

@ -110,6 +110,7 @@ def Math_AtanOp : Math_FloatUnaryOp<"atan">{
%a = math.atan %b : f64
```
}];
let hasFolder = 1;
}
//===----------------------------------------------------------------------===//

View File

@ -32,6 +32,24 @@ OpFoldResult math::AbsOp::fold(ArrayRef<Attribute> operands) {
});
}
//===----------------------------------------------------------------------===//
// AtanOp folder
//===----------------------------------------------------------------------===//
OpFoldResult math::AtanOp::fold(ArrayRef<Attribute> operands) {
return constFoldUnaryOpConditional<FloatAttr>(
operands, [](const APFloat &a) -> Optional<APFloat> {
switch (a.getSizeInBits(a.getSemantics())) {
case 64:
return APFloat(atan(a.convertToDouble()));
case 32:
return APFloat(atanf(a.convertToFloat()));
default:
return {};
}
});
}
//===----------------------------------------------------------------------===//
// CeilOp folder
//===----------------------------------------------------------------------===//

View File

@ -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>
}

View File

@ -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