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

Jakub Kuderski llvmlistbot at llvm.org
Sun Sep 28 06:48:22 PDT 2025


================
@@ -1116,6 +1116,18 @@ OpFoldResult arith::MaximumFOp::fold(FoldAdaptor adaptor) {
   if (matchPattern(adaptor.getRhs(), m_NegInfFloat()))
     return getLhs();
 
+  // max(max(a, b), b) -> max(a, b)
+  // max(max(a, b), a) -> max(a, b)
+  if (auto max = getLhs().getDefiningOp<MaximumFOp>())
+    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)
----------------
kuhar wrote:

I wonder if instead of covering both cases here and for other ops, we should instead use the fact that these are commutative and first move the other `min`/`max` operand to the RHS?

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


More information about the Mlir-commits mailing list