[Mlir-commits] [mlir] [MLIR] Fix infinite loop in tryFold in Graph Regions (PR #189232)

Matthias Springer llvmlistbot at llvm.org
Fri Sep 11 00:30:58 PDT 2026


================
@@ -496,11 +496,17 @@ OpBuilder::tryFold(Operation *op, SmallVectorImpl<Value> &results,
   if (failed(op->fold(foldResults)))
     return cleanupFailure();
 
+  // Bound the number of in-place fold iterations. Legitimate chains are very
+  // short (e.g. foldCommutative swaps once, then the op folds to a value).
+  // Without a bound, circular SSA uses in graph regions can cause an infinite
+  // loop (e.g. addi(x, 0) where x is the op's own result).
+  constexpr int kMaxInPlaceFolds = 64;
   int count = 0;
   do {
     LDBG() << "Folded in place #" << count
            << " times: " << OpWithFlags(op, OpPrintingFlags().skipRegions());
-    count++;
+    if (++count >= kMaxInPlaceFolds)
----------------
matthias-springer wrote:

Maybe emit a warning or LDBG?

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


More information about the Mlir-commits mailing list