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

Mehdi Amini llvmlistbot at llvm.org
Mon Sep 29 03:46:18 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)
----------------
joker-eph wrote:

> An alternative would be to normalize operands inside the fold itself (e.g., always move the nested max to the RHS)

Yes, I suggested this in the other revision: doing this in the trait would align all the commutative operation and then in-turn simplify all the folders (to avoid checking redundant forms)

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


More information about the Mlir-commits mailing list