[Mlir-commits] [mlir] [mlir][arith] Fold min/max with absorption and redundancy (PR #160224)

Jakub Kuderski llvmlistbot at llvm.org
Tue Sep 23 06:56:15 PDT 2025


================
@@ -1156,6 +1156,30 @@ OpFoldResult MaxSIOp::fold(FoldAdaptor adaptor) {
       return getLhs();
   }
 
+  // max(max(a, b), b) -> max(a, b)
+  // max(max(a, b), a) -> max(a, b)
+  if (auto max = getLhs().getDefiningOp<MaxSIOp>())
+    if (getRhs() == max.getRhs() || getRhs() == max.getLhs())
+      return getLhs();
+
+  // max(a, max(a, b)) -> max(a, b)
+  // max(b, max(a, b)) -> max(a, b)
+  if (auto max = getRhs().getDefiningOp<MaxSIOp>())
+    if (getLhs() == max.getRhs() || getLhs() == max.getLhs())
+      return getRhs();
+
+  // max(min(a, b), a) -> a
+  // max(min(b, a), a) -> a
+  if (auto min = getLhs().getDefiningOp<MinSIOp>())
+    if (getRhs() == min.getRhs() || getRhs() == min.getLhs())
+      return getRhs();
+
+  // max(a, min(a, b)) -> a
+  // max(a, min(b, a)) -> a
----------------
kuhar wrote:

I was wondering if this is correct when one of the values is `poison` and gets discarded, but this should be fine since the optimization makes the result more defined: https://alive2.llvm.org/ce/z/cdoy8x

https://github.com/llvm/llvm-project/pull/160224


More information about the Mlir-commits mailing list