Skip to content

[MLIR][Math] Add floating point value folders #127947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mlir/include/mlir/Dialect/Math/IR/MathOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ def Math_IsFiniteOp : Math_FloatClassificationOp<"isfinite"> {
%f = math.isfinite %a : f32
```
}];
let hasFolder = 1;
}

//===----------------------------------------------------------------------===//
Expand All @@ -754,6 +755,7 @@ def Math_IsInfOp : Math_FloatClassificationOp<"isinf"> {
%f = math.isinf %a : f32
```
}];
let hasFolder = 1;
}

//===----------------------------------------------------------------------===//
Expand All @@ -772,6 +774,7 @@ def Math_IsNaNOp : Math_FloatClassificationOp<"isnan"> {
%f = math.isnan %a : f32
```
}];
let hasFolder = 1;
}


Expand All @@ -791,6 +794,7 @@ def Math_IsNormalOp : Math_FloatClassificationOp<"isnormal"> {
%f = math.isnormal %a : f32
```
}];
let hasFolder = 1;
}

//===----------------------------------------------------------------------===//
Expand Down
64 changes: 64 additions & 0 deletions mlir/lib/Dialect/Math/IR/MathOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,70 @@ OpFoldResult math::ExpM1Op::fold(FoldAdaptor adaptor) {
});
}

//===----------------------------------------------------------------------===//
// IsFiniteOp folder
//===----------------------------------------------------------------------===//

OpFoldResult math::IsFiniteOp::fold(FoldAdaptor adaptor) {
if (auto val = dyn_cast_or_null<FloatAttr>(adaptor.getOperand())) {
return BoolAttr::get(val.getContext(), val.getValue().isFinite());
}
if (auto splat = dyn_cast_or_null<SplatElementsAttr>(adaptor.getOperand())) {
return DenseElementsAttr::get(
cast<ShapedType>(getType()),
APInt(1, splat.getSplatValue<APFloat>().isFinite()));
}
return {};
}

//===----------------------------------------------------------------------===//
// IsInfOp folder
//===----------------------------------------------------------------------===//

OpFoldResult math::IsInfOp::fold(FoldAdaptor adaptor) {
if (auto val = dyn_cast_or_null<FloatAttr>(adaptor.getOperand())) {
return BoolAttr::get(val.getContext(), val.getValue().isInfinity());
}
if (auto splat = dyn_cast_or_null<SplatElementsAttr>(adaptor.getOperand())) {
return DenseElementsAttr::get(
cast<ShapedType>(getType()),
APInt(1, splat.getSplatValue<APFloat>().isInfinity()));
}
return {};
}

//===----------------------------------------------------------------------===//
// IsNaNOp folder
//===----------------------------------------------------------------------===//

OpFoldResult math::IsNaNOp::fold(FoldAdaptor adaptor) {
if (auto val = dyn_cast_or_null<FloatAttr>(adaptor.getOperand())) {
return BoolAttr::get(val.getContext(), val.getValue().isNaN());
}
if (auto splat = dyn_cast_or_null<SplatElementsAttr>(adaptor.getOperand())) {
return DenseElementsAttr::get(
cast<ShapedType>(getType()),
APInt(1, splat.getSplatValue<APFloat>().isNaN()));
}
return {};
}

//===----------------------------------------------------------------------===//
// IsNormalOp folder
//===----------------------------------------------------------------------===//

OpFoldResult math::IsNormalOp::fold(FoldAdaptor adaptor) {
if (auto val = dyn_cast_or_null<FloatAttr>(adaptor.getOperand())) {
return BoolAttr::get(val.getContext(), val.getValue().isNormal());
}
if (auto splat = dyn_cast_or_null<SplatElementsAttr>(adaptor.getOperand())) {
return DenseElementsAttr::get(
cast<ShapedType>(getType()),
APInt(1, splat.getSplatValue<APFloat>().isNormal()));
}
return {};
}

//===----------------------------------------------------------------------===//
// TanOp folder
//===----------------------------------------------------------------------===//
Expand Down
72 changes: 72 additions & 0 deletions mlir/test/Dialect/Math/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -492,3 +492,75 @@ func.func @abs_poison() -> f32 {
%1 = math.absf %0 : f32
return %1 : f32
}

// CHECK-LABEL: @isfinite_fold
// CHECK: %[[cst:.+]] = arith.constant true
// CHECK: return %[[cst]]
func.func @isfinite_fold() -> i1 {
%c = arith.constant 2.0 : f32
%r = math.isfinite %c : f32
return %r : i1
}

// CHECK-LABEL: @isfinite_fold_vec
// CHECK: %[[cst:.+]] = arith.constant dense<true> : vector<4xi1>
// CHECK: return %[[cst]]
func.func @isfinite_fold_vec() -> (vector<4xi1>) {
%v1 = arith.constant dense<2.0> : vector<4xf32>
%0 = math.isfinite %v1 : vector<4xf32>
return %0 : vector<4xi1>
}

// CHECK-LABEL: @isinf_fold
// CHECK: %[[cst:.+]] = arith.constant false
// CHECK: return %[[cst]]
func.func @isinf_fold() -> i1 {
%c = arith.constant 2.0 : f32
%r = math.isinf %c : f32
return %r : i1
}

// CHECK-LABEL: @isinf_fold_vec
// CHECK: %[[cst:.+]] = arith.constant dense<false> : vector<4xi1>
// CHECK: return %[[cst]]
func.func @isinf_fold_vec() -> (vector<4xi1>) {
%v1 = arith.constant dense<2.0> : vector<4xf32>
%0 = math.isinf %v1 : vector<4xf32>
return %0 : vector<4xi1>
}

// CHECK-LABEL: @isnan_fold
// CHECK: %[[cst:.+]] = arith.constant false
// CHECK: return %[[cst]]
func.func @isnan_fold() -> i1 {
%c = arith.constant 2.0 : f32
%r = math.isnan %c : f32
return %r : i1
}

// CHECK-LABEL: @isnan_fold_vec
// CHECK: %[[cst:.+]] = arith.constant dense<false> : vector<4xi1>
// CHECK: return %[[cst]]
func.func @isnan_fold_vec() -> (vector<4xi1>) {
%v1 = arith.constant dense<2.0> : vector<4xf32>
%0 = math.isnan %v1 : vector<4xf32>
return %0 : vector<4xi1>
}

// CHECK-LABEL: @isnormal_fold
// CHECK: %[[cst:.+]] = arith.constant true
// CHECK: return %[[cst]]
func.func @isnormal_fold() -> i1 {
%c = arith.constant 2.0 : f32
%r = math.isnormal %c : f32
return %r : i1
}

// CHECK-LABEL: @isnormal_fold_vec
// CHECK: %[[cst:.+]] = arith.constant dense<true> : vector<4xi1>
// CHECK: return %[[cst]]
func.func @isnormal_fold_vec() -> (vector<4xi1>) {
%v1 = arith.constant dense<2.0> : vector<4xf32>
%0 = math.isnormal %v1 : vector<4xf32>
return %0 : vector<4xi1>
}