Skip to content

[mlir][arith] Fix arith maxnumf/minnumf folder #114595

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
Nov 27, 2024
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
5 changes: 5 additions & 0 deletions mlir/include/mlir/IR/Matchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ inline detail::constant_float_predicate_matcher m_OneFloat() {
}};
}

/// Matches a constant scalar / vector splat / tensor splat float ones.
inline detail::constant_float_predicate_matcher m_NaNFloat() {
return {[](const APFloat &value) { return value.isNaN(); }};
}

/// Matches a constant scalar / vector splat / tensor splat float positive
/// infinity.
inline detail::constant_float_predicate_matcher m_PosInfFloat() {
Expand Down
12 changes: 5 additions & 7 deletions mlir/lib/Dialect/Arith/IR/ArithOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1014,13 +1014,11 @@ OpFoldResult arith::MaxNumFOp::fold(FoldAdaptor adaptor) {
if (getLhs() == getRhs())
return getRhs();

// maxnumf(x, -inf) -> x
if (matchPattern(adaptor.getRhs(), m_NegInfFloat()))
// maxnumf(x, NaN) -> x
if (matchPattern(adaptor.getRhs(), m_NaNFloat()))
return getLhs();

return constFoldBinaryOp<FloatAttr>(
adaptor.getOperands(),
[](const APFloat &a, const APFloat &b) { return llvm::maximum(a, b); });
return constFoldBinaryOp<FloatAttr>(adaptor.getOperands(), llvm::maxnum);
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -1100,8 +1098,8 @@ OpFoldResult arith::MinNumFOp::fold(FoldAdaptor adaptor) {
if (getLhs() == getRhs())
return getRhs();

// minnumf(x, +inf) -> x
if (matchPattern(adaptor.getRhs(), m_PosInfFloat()))
// minnumf(x, NaN) -> x
if (matchPattern(adaptor.getRhs(), m_NaNFloat()))
return getLhs();

return constFoldBinaryOp<FloatAttr>(
Expand Down
22 changes: 15 additions & 7 deletions mlir/test/Dialect/Arith/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1905,31 +1905,39 @@ func.func @test_maximumf(%arg0 : f32) -> (f32, f32, f32) {
// -----

// CHECK-LABEL: @test_minnumf(
func.func @test_minnumf(%arg0 : f32) -> (f32, f32, f32) {
func.func @test_minnumf(%arg0 : f32) -> (f32, f32, f32, f32) {
// CHECK-DAG: %[[C0:.+]] = arith.constant 0.0
// CHECK-DAG: %[[INF:.+]] = arith.constant
// CHECK-NEXT: %[[X:.+]] = arith.minnumf %arg0, %[[C0]]
// CHECK-NEXT: return %[[X]], %arg0, %arg0
// CHECK-NEXT: %[[Y:.+]] = arith.minnumf %arg0, %[[INF]]
// CHECK-NEXT: return %[[X]], %arg0, %[[Y]], %arg0
%c0 = arith.constant 0.0 : f32
%inf = arith.constant 0x7F800000 : f32
%nan = arith.constant 0x7FC00000 : f32
%0 = arith.minnumf %c0, %arg0 : f32
%1 = arith.minnumf %arg0, %arg0 : f32
%2 = arith.minnumf %inf, %arg0 : f32
return %0, %1, %2 : f32, f32, f32
%3 = arith.minnumf %nan, %arg0 : f32
return %0, %1, %2, %3 : f32, f32, f32, f32
}

// -----

// CHECK-LABEL: @test_maxnumf(
func.func @test_maxnumf(%arg0 : f32) -> (f32, f32, f32) {
// CHECK-DAG: %[[C0:.+]] = arith.constant
func.func @test_maxnumf(%arg0 : f32) -> (f32, f32, f32, f32) {
// CHECK-DAG: %[[C0:.+]] = arith.constant 0.0
// CHECK-DAG: %[[NINF:.+]] = arith.constant
// CHECK-NEXT: %[[X:.+]] = arith.maxnumf %arg0, %[[C0]]
// CHECK-NEXT: return %[[X]], %arg0, %arg0
// CHECK-NEXT: %[[Y:.+]] = arith.maxnumf %arg0, %[[NINF]]
// CHECK-NEXT: return %[[X]], %arg0, %[[Y]], %arg0
%c0 = arith.constant 0.0 : f32
%-inf = arith.constant 0xFF800000 : f32
%nan = arith.constant 0x7FC00000 : f32
%0 = arith.maxnumf %c0, %arg0 : f32
%1 = arith.maxnumf %arg0, %arg0 : f32
%2 = arith.maxnumf %-inf, %arg0 : f32
return %0, %1, %2 : f32, f32, f32
%3 = arith.maxnumf %nan, %arg0 : f32
return %0, %1, %2, %3 : f32, f32, f32, f32
}

// -----
Expand Down
Loading