[Mlir-commits] [mlir] [mlir][Transforms] Greedy pattern rewriter: fix infinite folding loop (PR #161145)

Matthias Springer llvmlistbot at llvm.org
Mon Sep 29 09:03:13 PDT 2025


matthias-springer wrote:

This PR does not fix the problem yet. I'm seeing an infinite loop again, not sure why the CI has a different error.

The problem is here:
```
OpFoldResult arith::AddIOp::fold(FoldAdaptor adaptor) {
  // addi(x, 0) -> x
  if (matchPattern(adaptor.getRhs(), m_Zero())) {

    // THIS CHECK IS NEEDED:
    if (getLhs() == *this) return {};
    
    return getLhs();
  }
```

Without the above check, we return `lhs`, which is the `addi` op itself. Then, in `foldSingleResultHook`, this is interpreted as "operation folded in-place", triggering a `notifyOperationModified` and, therefore, infinite looping.

Fixing the folder itself it not enough, the next pattern that causes infinite looping is `AddIAddConstant`.

I'm not sure what's the right fix here. We likely have many folders/patterns that run into infinite loops in such cases. Should we fix all these? Are they even broken? I am using the `addi` op in a graph region, and in a way that maybe should be disallowed. (What does `%0 = addi %0, %c1` even mean?) Maybe such IR should be disallowed by extra verification? 

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


More information about the Mlir-commits mailing list