llvm-project
3d6ae4bd - [MLIR] Fix infinite loop in tryFold in Graph Regions (#189232)

Commit
4 days ago
[MLIR] Fix infinite loop in tryFold in Graph Regions (#189232) In a graph region (e.g. the module body), an op may use its own result as an operand, creating a circular SSA dependency. When such an op was passed to OpBuilder::tryFold, the do-while loop could spin forever: 1. foldCommutative moved the constant operand to the RHS (in-place fold, foldResults still empty). 2. The next fold call returned the op's own result, signalling an in-place fold, but foldSingleResultHook re-ran trait folds, found nothing to swap, and returned success with empty foldResults. 3. Step 2 repeated without end. Fix: cap the number of in-place fold iterations at 64. Legitimate chains are very short in practice (foldCommutative swaps once, then the op folds to a value or constant), so the cap is never reached for correct IR. For the circular-SSA case the loop now terminates and tryFold returns failure, letting dialect conversion fall through to a conversion pattern instead of looping. An alternative could be for each iteration of the loop, snapshot the operand list and attributes/properties. If a fold returns empty results but the operands are unchanged, we have reached a fixpoint. Stop the loop and return failure. However this is more expensive for the common case, while fixing an edge case. Fixes #159675 Assisted-by: Claude Code
Author
Parents
Loading