[Mlir-commits] [mlir] 8b1e3aa - [mlir][arith] Allow folding all +/- 0 under fastmath nsz (#217031)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Tue Aug 18 09:59:41 PDT 2026
Author: Ivan R. Ivanov
Date: 2026-08-18T18:59:36+02:00
New Revision: 8b1e3aa6b661bae5b4155905796ed5c37e2a1a82
URL: https://github.com/llvm/llvm-project/commit/8b1e3aa6b661bae5b4155905796ed5c37e2a1a82
DIFF: https://github.com/llvm/llvm-project/commit/8b1e3aa6b661bae5b4155905796ed5c37e2a1a82.diff
LOG: [mlir][arith] Allow folding all +/- 0 under fastmath nsz (#217031)
The fastmath `nsz` flag means the sign bit of 0.0 or -0.0 input operands
can be non-deterministically flipped. This allows us to fold the
following:
(x + (-0.0))
(x - (+0.0))
to x.
Added:
Modified:
mlir/lib/Dialect/Arith/IR/ArithOps.cpp
mlir/test/Dialect/Arith/canonicalize.mlir
Removed:
################################################################################
diff --git a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
index 07c7f99999424..7157e29800db0 100644
--- a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
+++ b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
@@ -1295,6 +1295,9 @@ OpFoldResult arith::AddFOp::fold(FoldAdaptor adaptor) {
// addf(x, -0) -> x
if (matchPattern(adaptor.getRhs(), m_NegZeroFloat()))
return getLhs();
+ if (matchPattern(adaptor.getRhs(), m_PosZeroFloat()) &&
+ bitEnumContainsAll(adaptor.getFastmath(), FastMathFlags::nsz))
+ return getLhs();
auto rm = getRoundingmode();
return constFoldBinaryOp<FloatAttr>(
@@ -1318,6 +1321,9 @@ OpFoldResult arith::SubFOp::fold(FoldAdaptor adaptor) {
// subf(x, +0) -> x
if (matchPattern(adaptor.getRhs(), m_PosZeroFloat()))
return getLhs();
+ if (matchPattern(adaptor.getRhs(), m_NegZeroFloat()) &&
+ bitEnumContainsAll(adaptor.getFastmath(), FastMathFlags::nsz))
+ return getLhs();
auto rm = getRoundingmode();
return constFoldBinaryOp<FloatAttr>(
diff --git a/mlir/test/Dialect/Arith/canonicalize.mlir b/mlir/test/Dialect/Arith/canonicalize.mlir
index efee81bf09959..b2d38152dca6e 100644
--- a/mlir/test/Dialect/Arith/canonicalize.mlir
+++ b/mlir/test/Dialect/Arith/canonicalize.mlir
@@ -3127,11 +3127,11 @@ func.func @test_maxnumf(%arg0 : f32) -> (f32, f32, f32, f32) {
// -----
// CHECK-LABEL: @test_addf(
-func.func @test_addf(%arg0 : f32) -> (f32, f32, f32, f32) {
+func.func @test_addf(%arg0 : f32) -> (f32, f32, f32, f32, f32) {
// CHECK-DAG: %[[C2:.+]] = arith.constant 2.0
// CHECK-DAG: %[[C0:.+]] = arith.constant 0.0
// CHECK-NEXT: %[[X:.+]] = arith.addf %arg0, %[[C0]]
- // CHECK-NEXT: return %[[X]], %arg0, %arg0, %[[C2]]
+ // CHECK-NEXT: return %[[X]], %arg0, %arg0, %[[C2]], %arg0
%c0 = arith.constant 0.0 : f32
%c-0 = arith.constant -0.0 : f32
%c1 = arith.constant 1.0 : f32
@@ -3139,24 +3139,26 @@ func.func @test_addf(%arg0 : f32) -> (f32, f32, f32, f32) {
%1 = arith.addf %arg0, %c-0 : f32
%2 = arith.addf %c-0, %arg0 : f32
%3 = arith.addf %c1, %c1 : f32
- return %0, %1, %2, %3 : f32, f32, f32, f32
+ %4 = arith.addf %c0, %arg0 fastmath<nsz> : f32
+ return %0, %1, %2, %3, %4 : f32, f32, f32, f32, f32
}
// -----
// CHECK-LABEL: @test_subf(
-func.func @test_subf(%arg0 : f16) -> (f16, f16, f16) {
+func.func @test_subf(%arg0 : f16) -> (f16, f16, f16, f16) {
// CHECK-DAG: %[[C1:.+]] = arith.constant -1.0
// CHECK-DAG: %[[C0:.+]] = arith.constant -0.0
// CHECK-NEXT: %[[X:.+]] = arith.subf %arg0, %[[C0]]
- // CHECK-NEXT: return %arg0, %[[X]], %[[C1]]
+ // CHECK-NEXT: return %arg0, %[[X]], %[[C1]], %arg0
%c0 = arith.constant 0.0 : f16
%c-0 = arith.constant -0.0 : f16
%c1 = arith.constant 1.0 : f16
%0 = arith.subf %arg0, %c0 : f16
%1 = arith.subf %arg0, %c-0 : f16
%2 = arith.subf %c0, %c1 : f16
- return %0, %1, %2 : f16, f16, f16
+ %3 = arith.subf %arg0, %c-0 fastmath<nsz> : f16
+ return %0, %1, %2, %3 : f16, f16, f16, f16
}
// CHECK-LABEL: @test_subf_negzero(
More information about the Mlir-commits
mailing list