[mlir][Math] Add constant folder for SinOp.

This patch adds constant folder for SinOp by using sin/sinf of libm.

Reviewed By: ftynse

Differential Revision: https://reviews.llvm.org/D133915
This commit is contained in:
jacquesguan 2022-09-15 14:33:17 +08:00
parent 8704281c56
commit 71e52a125c
3 changed files with 37 additions and 0 deletions

View File

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

View File

@ -122,6 +122,24 @@ OpFoldResult math::CosOp::fold(ArrayRef<Attribute> operands) {
}); });
} }
//===----------------------------------------------------------------------===//
// SinOp folder
//===----------------------------------------------------------------------===//
OpFoldResult math::SinOp::fold(ArrayRef<Attribute> operands) {
return constFoldUnaryOpConditional<FloatAttr>(
operands, [](const APFloat &a) -> Optional<APFloat> {
switch (a.getSizeInBits(a.getSemantics())) {
case 64:
return APFloat(sin(a.convertToDouble()));
case 32:
return APFloat(sinf(a.convertToFloat()));
default:
return {};
}
});
}
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// CountLeadingZerosOp folder // CountLeadingZerosOp folder
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//

View File

@ -447,3 +447,21 @@ func.func @trunc_fold_vec() -> (vector<4xf32>) {
%0 = math.trunc %v : vector<4xf32> %0 = math.trunc %v : vector<4xf32>
return %0 : vector<4xf32> return %0 : vector<4xf32>
} }
// CHECK-LABEL: @sin_fold
// CHECK-NEXT: %[[cst:.+]] = arith.constant 0.84{{[0-9]+}} : f32
// CHECK-NEXT: return %[[cst]]
func.func @sin_fold() -> f32 {
%c = arith.constant 1.0 : f32
%r = math.sin %c : f32
return %r : f32
}
// CHECK-LABEL: @sin_fold_vec
// CHECK-NEXT: %[[cst:.+]] = arith.constant dense<[0.000000e+00, 0.84{{[0-9]+}}, 0.000000e+00, 0.84{{[0-9]+}}]> : vector<4xf32>
// CHECK-NEXT: return %[[cst]]
func.func @sin_fold_vec() -> (vector<4xf32>) {
%v1 = arith.constant dense<[0.0, 1.0, 0.0, 1.0]> : vector<4xf32>
%0 = math.sin %v1 : vector<4xf32>
return %0 : vector<4xf32>
}