[Mlir-commits] [mlir] [mlir][SCF] Add support for peeling the first iteration out of the loop (PR #74015)

Lei Zhang llvmlistbot at llvm.org
Fri Dec 8 22:07:34 PST 2023


================
@@ -205,32 +222,84 @@ LogicalResult mlir::scf::peelForLoopAndSimplifyBounds(RewriterBase &rewriter,
   return success();
 }
 
+LogicalResult mlir::scf::peelFirstIterationForLoop(RewriterBase &b, ForOp forOp,
+                                                   ForOp &firstIteration) {
+  RewriterBase::InsertionGuard guard(b);
+  auto lbInt = getConstantIntValue(forOp.getLowerBound());
+  auto ubInt = getConstantIntValue(forOp.getUpperBound());
+  auto stepInt = getConstantIntValue(forOp.getStep());
+
+  // Peeling is not needed if there is one or less iteration.
+  if (lbInt && ubInt && stepInt && (*ubInt - *lbInt) / *stepInt <= 1)
+    return success();
----------------
antiagainst wrote:

This should return `failure` to indicate failing matching the pattern? Otherwise the framework will think the patten applies successfully and put the op again in the worklist to be processed by the pattern again later. So it will go infinite. In reality we have a limit of 10 to stop if not converging. If you use a test case of this you probably will see a warning of pattern stopped application after 10 times.

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


More information about the Mlir-commits mailing list