1 //===- LoopSpecialization.cpp - scf.parallel/SCR.for specialization -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Specializes parallel loops and for loops for easier unrolling and
10 // vectorization.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "PassDetail.h"
15 #include "mlir/Analysis/AffineStructures.h"
16 #include "mlir/Dialect/Affine/IR/AffineOps.h"
17 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
18 #include "mlir/Dialect/SCF/Passes.h"
19 #include "mlir/Dialect/SCF/SCF.h"
20 #include "mlir/Dialect/SCF/Transforms.h"
21 #include "mlir/Dialect/StandardOps/IR/Ops.h"
22 #include "mlir/Dialect/Utils/StaticValueUtils.h"
23 #include "mlir/IR/AffineExpr.h"
24 #include "mlir/IR/BlockAndValueMapping.h"
25 #include "mlir/IR/PatternMatch.h"
26 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
27 #include "llvm/ADT/DenseMap.h"
28 
29 using namespace mlir;
30 using scf::ForOp;
31 using scf::ParallelOp;
32 
33 /// Rewrite a parallel loop with bounds defined by an affine.min with a constant
34 /// into 2 loops after checking if the bounds are equal to that constant. This
35 /// is beneficial if the loop will almost always have the constant bound and
36 /// that version can be fully unrolled and vectorized.
37 static void specializeParallelLoopForUnrolling(ParallelOp op) {
38   SmallVector<int64_t, 2> constantIndices;
39   constantIndices.reserve(op.upperBound().size());
40   for (auto bound : op.upperBound()) {
41     auto minOp = bound.getDefiningOp<AffineMinOp>();
42     if (!minOp)
43       return;
44     int64_t minConstant = std::numeric_limits<int64_t>::max();
45     for (AffineExpr expr : minOp.map().getResults()) {
46       if (auto constantIndex = expr.dyn_cast<AffineConstantExpr>())
47         minConstant = std::min(minConstant, constantIndex.getValue());
48     }
49     if (minConstant == std::numeric_limits<int64_t>::max())
50       return;
51     constantIndices.push_back(minConstant);
52   }
53 
54   OpBuilder b(op);
55   BlockAndValueMapping map;
56   Value cond;
57   for (auto bound : llvm::zip(op.upperBound(), constantIndices)) {
58     Value constant =
59         b.create<arith::ConstantIndexOp>(op.getLoc(), std::get<1>(bound));
60     Value cmp = b.create<arith::CmpIOp>(op.getLoc(), arith::CmpIPredicate::eq,
61                                         std::get<0>(bound), constant);
62     cond = cond ? b.create<arith::AndIOp>(op.getLoc(), cond, cmp) : cmp;
63     map.map(std::get<0>(bound), constant);
64   }
65   auto ifOp = b.create<scf::IfOp>(op.getLoc(), cond, /*withElseRegion=*/true);
66   ifOp.getThenBodyBuilder().clone(*op.getOperation(), map);
67   ifOp.getElseBodyBuilder().clone(*op.getOperation());
68   op.erase();
69 }
70 
71 /// Rewrite a for loop with bounds defined by an affine.min with a constant into
72 /// 2 loops after checking if the bounds are equal to that constant. This is
73 /// beneficial if the loop will almost always have the constant bound and that
74 /// version can be fully unrolled and vectorized.
75 static void specializeForLoopForUnrolling(ForOp op) {
76   auto bound = op.upperBound();
77   auto minOp = bound.getDefiningOp<AffineMinOp>();
78   if (!minOp)
79     return;
80   int64_t minConstant = std::numeric_limits<int64_t>::max();
81   for (AffineExpr expr : minOp.map().getResults()) {
82     if (auto constantIndex = expr.dyn_cast<AffineConstantExpr>())
83       minConstant = std::min(minConstant, constantIndex.getValue());
84   }
85   if (minConstant == std::numeric_limits<int64_t>::max())
86     return;
87 
88   OpBuilder b(op);
89   BlockAndValueMapping map;
90   Value constant = b.create<arith::ConstantIndexOp>(op.getLoc(), minConstant);
91   Value cond = b.create<arith::CmpIOp>(op.getLoc(), arith::CmpIPredicate::eq,
92                                        bound, constant);
93   map.map(bound, constant);
94   auto ifOp = b.create<scf::IfOp>(op.getLoc(), cond, /*withElseRegion=*/true);
95   ifOp.getThenBodyBuilder().clone(*op.getOperation(), map);
96   ifOp.getElseBodyBuilder().clone(*op.getOperation());
97   op.erase();
98 }
99 
100 /// Rewrite a for loop with bounds/step that potentially do not divide evenly
101 /// into a for loop where the step divides the iteration space evenly, followed
102 /// by an scf.if for the last (partial) iteration (if any).
103 ///
104 /// This function rewrites the given scf.for loop in-place and creates a new
105 /// scf.if operation for the last iteration. It replaces all uses of the
106 /// unpeeled loop with the results of the newly generated scf.if.
107 ///
108 /// The newly generated scf.if operation is returned via `ifOp`. The boundary
109 /// at which the loop is split (new upper bound) is returned via `splitBound`.
110 /// The return value indicates whether the loop was rewritten or not.
111 static LogicalResult peelForLoop(RewriterBase &b, ForOp forOp,
112                                  ForOp &partialIteration, Value &splitBound) {
113   RewriterBase::InsertionGuard guard(b);
114   auto lbInt = getConstantIntValue(forOp.lowerBound());
115   auto ubInt = getConstantIntValue(forOp.upperBound());
116   auto stepInt = getConstantIntValue(forOp.step());
117 
118   // No specialization necessary if step already divides upper bound evenly.
119   if (lbInt && ubInt && stepInt && (*ubInt - *lbInt) % *stepInt == 0)
120     return failure();
121   // No specialization necessary if step size is 1.
122   if (stepInt == static_cast<int64_t>(1))
123     return failure();
124 
125   auto loc = forOp.getLoc();
126   AffineExpr sym0, sym1, sym2;
127   bindSymbols(b.getContext(), sym0, sym1, sym2);
128   // New upper bound: %ub - (%ub - %lb) mod %step
129   auto modMap = AffineMap::get(0, 3, {sym1 - ((sym1 - sym0) % sym2)});
130   b.setInsertionPoint(forOp);
131   splitBound = b.createOrFold<AffineApplyOp>(
132       loc, modMap,
133       ValueRange{forOp.lowerBound(), forOp.upperBound(), forOp.step()});
134 
135   // Create ForOp for partial iteration.
136   b.setInsertionPointAfter(forOp);
137   partialIteration = cast<ForOp>(b.clone(*forOp.getOperation()));
138   partialIteration.lowerBoundMutable().assign(splitBound);
139   forOp.replaceAllUsesWith(partialIteration->getResults());
140   partialIteration.initArgsMutable().assign(forOp->getResults());
141 
142   // Set new upper loop bound.
143   b.updateRootInPlace(forOp,
144                       [&]() { forOp.upperBoundMutable().assign(splitBound); });
145 
146   return success();
147 }
148 
149 static void unpackOptionalValues(ArrayRef<Optional<Value>> source,
150                                  SmallVector<Value> &target) {
151   target = llvm::to_vector<4>(llvm::map_range(source, [](Optional<Value> val) {
152     return val.hasValue() ? *val : Value();
153   }));
154 }
155 
156 /// Bound an identifier `pos` in a given FlatAffineValueConstraints with
157 /// constraints drawn from an affine map. Before adding the constraint, the
158 /// dimensions/symbols of the affine map are aligned with `constraints`.
159 /// `operands` are the SSA Value operands used with the affine map.
160 /// Note: This function adds a new symbol column to the `constraints` for each
161 /// dimension/symbol that exists in the affine map but not in `constraints`.
162 static LogicalResult alignAndAddBound(FlatAffineValueConstraints &constraints,
163                                       FlatAffineConstraints::BoundType type,
164                                       unsigned pos, AffineMap map,
165                                       ValueRange operands) {
166   SmallVector<Value> dims, syms, newSyms;
167   unpackOptionalValues(constraints.getMaybeDimValues(), dims);
168   unpackOptionalValues(constraints.getMaybeSymbolValues(), syms);
169 
170   AffineMap alignedMap =
171       alignAffineMapWithValues(map, operands, dims, syms, &newSyms);
172   for (unsigned i = syms.size(); i < newSyms.size(); ++i)
173     constraints.appendSymbolId(newSyms[i]);
174   return constraints.addBound(type, pos, alignedMap);
175 }
176 
177 /// Add `val` to each result of `map`.
178 static AffineMap addConstToResults(AffineMap map, int64_t val) {
179   SmallVector<AffineExpr> newResults;
180   for (AffineExpr r : map.getResults())
181     newResults.push_back(r + val);
182   return AffineMap::get(map.getNumDims(), map.getNumSymbols(), newResults,
183                         map.getContext());
184 }
185 
186 /// This function tries to canonicalize min/max operations by proving that their
187 /// value is bounded by the same lower and upper bound. In that case, the
188 /// operation can be folded away.
189 ///
190 /// Bounds are computed by FlatAffineValueConstraints. Invariants required for
191 /// finding/proving bounds should be supplied via `constraints`.
192 ///
193 /// 1. Add dimensions for `op` and `opBound` (lower or upper bound of `op`).
194 /// 2. Compute an upper bound of `op` (in case of `isMin`) or a lower bound (in
195 ///    case of `!isMin`) and bind it to `opBound`. SSA values that are used in
196 ///    `op` but are not part of `constraints`, are added as extra symbols.
197 /// 3. For each result of `op`: Add result as a dimension `r_i`. Prove that:
198 ///    * If `isMin`: r_i >= opBound
199 ///    * If `isMax`: r_i <= opBound
200 ///    If this is the case, ub(op) == lb(op).
201 /// 4. Replace `op` with `opBound`.
202 ///
203 /// In summary, the following constraints are added throughout this function.
204 /// Note: `invar` are dimensions added by the caller to express the invariants.
205 /// (Showing only the case where `isMin`.)
206 ///
207 ///  invar |    op | opBound | r_i | extra syms... | const |           eq/ineq
208 ///  ------+-------+---------+-----+---------------+-------+-------------------
209 ///   (various eq./ineq. constraining `invar`, added by the caller)
210 ///    ... |     0 |       0 |   0 |             0 |   ... |               ...
211 ///  ------+-------+---------+-----+---------------+-------+-------------------
212 ///   (various ineq. constraining `op` in terms of `op` operands (`invar` and
213 ///    extra `op` operands "extra syms" that are not in `invar`)).
214 ///    ... |    -1 |       0 |   0 |           ... |   ... |              >= 0
215 ///  ------+-------+---------+-----+---------------+-------+-------------------
216 ///   (set `opBound` to `op` upper bound in terms of `invar` and "extra syms")
217 ///    ... |     0 |      -1 |   0 |           ... |   ... |               = 0
218 ///  ------+-------+---------+-----+---------------+-------+-------------------
219 ///   (for each `op` map result r_i: set r_i to corresponding map result,
220 ///    prove that r_i >= minOpUb via contradiction)
221 ///    ... |     0 |       0 |  -1 |           ... |   ... |               = 0
222 ///      0 |     0 |       1 |  -1 |             0 |    -1 |              >= 0
223 ///
224 static LogicalResult
225 canonicalizeMinMaxOp(RewriterBase &rewriter, Operation *op, AffineMap map,
226                      ValueRange operands, bool isMin,
227                      FlatAffineValueConstraints constraints) {
228   RewriterBase::InsertionGuard guard(rewriter);
229   unsigned numResults = map.getNumResults();
230 
231   // Add a few extra dimensions.
232   unsigned dimOp = constraints.appendDimId();      // `op`
233   unsigned dimOpBound = constraints.appendDimId(); // `op` lower/upper bound
234   unsigned resultDimStart = constraints.appendDimId(/*num=*/numResults);
235 
236   // Add an inequality for each result expr_i of map:
237   // isMin: op <= expr_i, !isMin: op >= expr_i
238   auto boundType =
239       isMin ? FlatAffineConstraints::UB : FlatAffineConstraints::LB;
240   // Upper bounds are exclusive, so add 1. (`affine.min` ops are inclusive.)
241   AffineMap mapLbUb = isMin ? addConstToResults(map, 1) : map;
242   if (failed(
243           alignAndAddBound(constraints, boundType, dimOp, mapLbUb, operands)))
244     return failure();
245 
246   // Try to compute a lower/upper bound for op, expressed in terms of the other
247   // `dims` and extra symbols.
248   SmallVector<AffineMap> opLb(1), opUb(1);
249   constraints.getSliceBounds(dimOp, 1, rewriter.getContext(), &opLb, &opUb);
250   AffineMap sliceBound = isMin ? opUb[0] : opLb[0];
251   // TODO: `getSliceBounds` may return multiple bounds at the moment. This is
252   // a TODO of `getSliceBounds` and not handled here.
253   if (!sliceBound || sliceBound.getNumResults() != 1)
254     return failure(); // No or multiple bounds found.
255   // Recover the inclusive UB in the case of an `affine.min`.
256   AffineMap boundMap = isMin ? addConstToResults(sliceBound, -1) : sliceBound;
257 
258   // Add an equality: Set dimOpBound to computed bound.
259   // Add back dimension for op. (Was removed by `getSliceBounds`.)
260   AffineMap alignedBoundMap = boundMap.shiftDims(/*shift=*/1, /*offset=*/dimOp);
261   if (failed(constraints.addBound(FlatAffineConstraints::EQ, dimOpBound,
262                                   alignedBoundMap)))
263     return failure();
264 
265   // If the constraint system is empty, there is an inconsistency. (E.g., this
266   // can happen if loop lb > ub.)
267   if (constraints.isEmpty())
268     return failure();
269 
270   // In the case of `isMin` (`!isMin` is inversed):
271   // Prove that each result of `map` has a lower bound that is equal to (or
272   // greater than) the upper bound of `op` (`dimOpBound`). In that case, `op`
273   // can be replaced with the bound. I.e., prove that for each result
274   // expr_i (represented by dimension r_i):
275   //
276   // r_i >= opBound
277   //
278   // To prove this inequality, add its negation to the constraint set and prove
279   // that the constraint set is empty.
280   for (unsigned i = resultDimStart; i < resultDimStart + numResults; ++i) {
281     FlatAffineValueConstraints newConstr(constraints);
282 
283     // Add an equality: r_i = expr_i
284     // Note: These equalities could have been added earlier and used to express
285     // minOp <= expr_i. However, then we run the risk that `getSliceBounds`
286     // computes minOpUb in terms of r_i dims, which is not desired.
287     if (failed(alignAndAddBound(newConstr, FlatAffineConstraints::EQ, i,
288                                 map.getSubMap({i - resultDimStart}), operands)))
289       return failure();
290 
291     // If `isMin`:  Add inequality: r_i < opBound
292     //              equiv.: opBound - r_i - 1 >= 0
293     // If `!isMin`: Add inequality: r_i > opBound
294     //              equiv.: -opBound + r_i - 1 >= 0
295     SmallVector<int64_t> ineq(newConstr.getNumCols(), 0);
296     ineq[dimOpBound] = isMin ? 1 : -1;
297     ineq[i] = isMin ? -1 : 1;
298     ineq[newConstr.getNumCols() - 1] = -1;
299     newConstr.addInequality(ineq);
300     if (!newConstr.isEmpty())
301       return failure();
302   }
303 
304   // Lower and upper bound of `op` are equal. Replace `minOp` with its bound.
305   AffineMap newMap = alignedBoundMap;
306   SmallVector<Value> newOperands;
307   unpackOptionalValues(constraints.getMaybeDimAndSymbolValues(), newOperands);
308   mlir::canonicalizeMapAndOperands(&newMap, &newOperands);
309   rewriter.setInsertionPoint(op);
310   rewriter.replaceOpWithNewOp<AffineApplyOp>(op, newMap, newOperands);
311   return success();
312 }
313 
314 /// Try to simplify a min/max operation `op` after loop peeling. This function
315 /// can simplify min/max operations such as (ub is the previous upper bound of
316 /// the unpeeled loop):
317 /// ```
318 /// #map = affine_map<(d0)[s0, s1] -> (s0, -d0 + s1)>
319 /// %r = affine.min #affine.min #map(%iv)[%step, %ub]
320 /// ```
321 /// and rewrites them into (in the case the peeled loop):
322 /// ```
323 /// %r = %step
324 /// ```
325 /// min/max operations inside the partial iteration are rewritten in a similar
326 /// way.
327 ///
328 /// This function builds up a set of constraints, capable of proving that:
329 /// * Inside the peeled loop: min(step, ub - iv) == step
330 /// * Inside the partial iteration: min(step, ub - iv) == ub - iv
331 ///
332 /// Returns `success` if the given operation was replaced by a new operation;
333 /// `failure` otherwise.
334 ///
335 /// Note: `ub` is the previous upper bound of the loop (before peeling).
336 /// `insideLoop` must be true for min/max ops inside the loop and false for
337 /// affine.min ops inside the partial iteration. For an explanation of the other
338 /// parameters, see comment of `canonicalizeMinMaxOpInLoop`.
339 LogicalResult mlir::scf::rewritePeeledMinMaxOp(RewriterBase &rewriter,
340                                                Operation *op, AffineMap map,
341                                                ValueRange operands, bool isMin,
342                                                Value iv, Value ub, Value step,
343                                                bool insideLoop) {
344   FlatAffineValueConstraints constraints;
345   constraints.appendDimId({iv, ub, step});
346   if (auto constUb = getConstantIntValue(ub))
347     constraints.addBound(FlatAffineConstraints::EQ, 1, *constUb);
348   if (auto constStep = getConstantIntValue(step))
349     constraints.addBound(FlatAffineConstraints::EQ, 2, *constStep);
350 
351   // Add loop peeling invariant. This is the main piece of knowledge that
352   // enables AffineMinOp simplification.
353   if (insideLoop) {
354     // ub - iv >= step (equiv.: -iv + ub - step + 0 >= 0)
355     // Intuitively: Inside the peeled loop, every iteration is a "full"
356     // iteration, i.e., step divides the iteration space `ub - lb` evenly.
357     constraints.addInequality({-1, 1, -1, 0});
358   } else {
359     // ub - iv < step (equiv.: iv + -ub + step - 1 >= 0)
360     // Intuitively: `iv` is the split bound here, i.e., the iteration variable
361     // value of the very last iteration (in the unpeeled loop). At that point,
362     // there are less than `step` elements remaining. (Otherwise, the peeled
363     // loop would run for at least one more iteration.)
364     constraints.addInequality({1, -1, 1, -1});
365   }
366 
367   return canonicalizeMinMaxOp(rewriter, op, map, operands, isMin, constraints);
368 }
369 
370 template <typename OpTy, bool IsMin>
371 static void rewriteAffineOpAfterPeeling(RewriterBase &rewriter, ForOp forOp,
372                                         ForOp partialIteration,
373                                         Value previousUb) {
374   Value mainIv = forOp.getInductionVar();
375   Value partialIv = partialIteration.getInductionVar();
376   assert(forOp.step() == partialIteration.step() &&
377          "expected same step in main and partial loop");
378   Value step = forOp.step();
379 
380   forOp.walk([&](OpTy affineOp) {
381     AffineMap map = affineOp.getAffineMap();
382     (void)scf::rewritePeeledMinMaxOp(rewriter, affineOp, map,
383                                      affineOp.operands(), IsMin, mainIv,
384                                      previousUb, step,
385                                      /*insideLoop=*/true);
386   });
387   partialIteration.walk([&](OpTy affineOp) {
388     AffineMap map = affineOp.getAffineMap();
389     (void)scf::rewritePeeledMinMaxOp(rewriter, affineOp, map,
390                                      affineOp.operands(), IsMin, partialIv,
391                                      previousUb, step, /*insideLoop=*/false);
392   });
393 }
394 
395 LogicalResult mlir::scf::peelAndCanonicalizeForLoop(RewriterBase &rewriter,
396                                                     ForOp forOp,
397                                                     ForOp &partialIteration) {
398   Value previousUb = forOp.upperBound();
399   Value splitBound;
400   if (failed(peelForLoop(rewriter, forOp, partialIteration, splitBound)))
401     return failure();
402 
403   // Rewrite affine.min and affine.max ops.
404   rewriteAffineOpAfterPeeling<AffineMinOp, /*IsMin=*/true>(
405       rewriter, forOp, partialIteration, previousUb);
406   rewriteAffineOpAfterPeeling<AffineMaxOp, /*IsMin=*/false>(
407       rewriter, forOp, partialIteration, previousUb);
408 
409   return success();
410 }
411 
412 /// Canonicalize min/max operations in the context of for loops with a known
413 /// range. Call `canonicalizeMinMaxOp` and add the following constraints to
414 /// the constraint system (along with the missing dimensions):
415 ///
416 /// * iv >= lb
417 /// * iv < lb + step * ((ub - lb - 1) floorDiv step) + 1
418 ///
419 /// Note: Due to limitations of FlatAffineConstraints, only constant step sizes
420 /// are currently supported.
421 LogicalResult
422 mlir::scf::canonicalizeMinMaxOpInLoop(RewriterBase &rewriter, Operation *op,
423                                       AffineMap map, ValueRange operands,
424                                       bool isMin, LoopMatcherFn loopMatcher) {
425   FlatAffineValueConstraints constraints;
426   DenseSet<Value> allIvs;
427 
428   // Find all iteration variables among `minOp`'s operands add constrain them.
429   for (Value operand : operands) {
430     // Skip duplicate ivs.
431     if (llvm::find(allIvs, operand) != allIvs.end())
432       continue;
433 
434     // If `operand` is an iteration variable: Find corresponding loop
435     // bounds and step.
436     Value iv = operand;
437     Value lb, ub, step;
438     if (failed(loopMatcher(operand, lb, ub, step)))
439       continue;
440     allIvs.insert(iv);
441 
442     // FlatAffineConstraints does not support semi-affine expressions.
443     // Therefore, only constant step values are supported.
444     auto stepInt = getConstantIntValue(step);
445     if (!stepInt)
446       continue;
447 
448     unsigned dimIv = constraints.appendDimId(iv);
449     unsigned dimLb = constraints.appendDimId(lb);
450     unsigned dimUb = constraints.appendDimId(ub);
451 
452     // If loop lower/upper bounds are constant: Add EQ constraint.
453     Optional<int64_t> lbInt = getConstantIntValue(lb);
454     Optional<int64_t> ubInt = getConstantIntValue(ub);
455     if (lbInt)
456       constraints.addBound(FlatAffineConstraints::EQ, dimLb, *lbInt);
457     if (ubInt)
458       constraints.addBound(FlatAffineConstraints::EQ, dimUb, *ubInt);
459 
460     // iv >= lb (equiv.: iv - lb >= 0)
461     SmallVector<int64_t> ineqLb(constraints.getNumCols(), 0);
462     ineqLb[dimIv] = 1;
463     ineqLb[dimLb] = -1;
464     constraints.addInequality(ineqLb);
465 
466     // iv < lb + step * ((ub - lb - 1) floorDiv step) + 1
467     AffineExpr exprLb = lbInt ? rewriter.getAffineConstantExpr(*lbInt)
468                               : rewriter.getAffineDimExpr(dimLb);
469     AffineExpr exprUb = ubInt ? rewriter.getAffineConstantExpr(*ubInt)
470                               : rewriter.getAffineDimExpr(dimUb);
471     AffineExpr ivUb =
472         exprLb + 1 + (*stepInt * ((exprUb - exprLb - 1).floorDiv(*stepInt)));
473     auto map = AffineMap::get(
474         /*dimCount=*/constraints.getNumDimIds(),
475         /*symbolCount=*/constraints.getNumSymbolIds(), /*result=*/ivUb);
476 
477     if (failed(constraints.addBound(FlatAffineConstraints::UB, dimIv, map)))
478       return failure();
479   }
480 
481   return canonicalizeMinMaxOp(rewriter, op, map, operands, isMin, constraints);
482 }
483 
484 static constexpr char kPeeledLoopLabel[] = "__peeled_loop__";
485 static constexpr char kPartialIterationLabel[] = "__partial_iteration__";
486 
487 namespace {
488 struct ForLoopPeelingPattern : public OpRewritePattern<ForOp> {
489   ForLoopPeelingPattern(MLIRContext *ctx, bool skipPartial)
490       : OpRewritePattern<ForOp>(ctx), skipPartial(skipPartial) {}
491 
492   LogicalResult matchAndRewrite(ForOp forOp,
493                                 PatternRewriter &rewriter) const override {
494     // Do not peel already peeled loops.
495     if (forOp->hasAttr(kPeeledLoopLabel))
496       return failure();
497     if (skipPartial) {
498       // No peeling of loops inside the partial iteration of another peeled
499       // loop.
500       Operation *op = forOp.getOperation();
501       while ((op = op->getParentOfType<scf::ForOp>())) {
502         if (op->hasAttr(kPartialIterationLabel))
503           return failure();
504       }
505     }
506     // Apply loop peeling.
507     scf::ForOp partialIteration;
508     if (failed(peelAndCanonicalizeForLoop(rewriter, forOp, partialIteration)))
509       return failure();
510     // Apply label, so that the same loop is not rewritten a second time.
511     partialIteration->setAttr(kPeeledLoopLabel, rewriter.getUnitAttr());
512     rewriter.updateRootInPlace(forOp, [&]() {
513       forOp->setAttr(kPeeledLoopLabel, rewriter.getUnitAttr());
514     });
515     partialIteration->setAttr(kPartialIterationLabel, rewriter.getUnitAttr());
516     return success();
517   }
518 
519   /// If set to true, loops inside partial iterations of another peeled loop
520   /// are not peeled. This reduces the size of the generated code. Partial
521   /// iterations are not usually performance critical.
522   /// Note: Takes into account the entire chain of parent operations, not just
523   /// the direct parent.
524   bool skipPartial;
525 };
526 } // namespace
527 
528 namespace {
529 struct ParallelLoopSpecialization
530     : public SCFParallelLoopSpecializationBase<ParallelLoopSpecialization> {
531   void runOnFunction() override {
532     getFunction().walk(
533         [](ParallelOp op) { specializeParallelLoopForUnrolling(op); });
534   }
535 };
536 
537 struct ForLoopSpecialization
538     : public SCFForLoopSpecializationBase<ForLoopSpecialization> {
539   void runOnFunction() override {
540     getFunction().walk([](ForOp op) { specializeForLoopForUnrolling(op); });
541   }
542 };
543 
544 struct ForLoopPeeling : public SCFForLoopPeelingBase<ForLoopPeeling> {
545   void runOnFunction() override {
546     FuncOp funcOp = getFunction();
547     MLIRContext *ctx = funcOp.getContext();
548     RewritePatternSet patterns(ctx);
549     patterns.add<ForLoopPeelingPattern>(ctx, skipPartial);
550     (void)applyPatternsAndFoldGreedily(funcOp, std::move(patterns));
551 
552     // Drop the markers.
553     funcOp.walk([](Operation *op) {
554       op->removeAttr(kPeeledLoopLabel);
555       op->removeAttr(kPartialIterationLabel);
556     });
557   }
558 };
559 } // namespace
560 
561 std::unique_ptr<Pass> mlir::createParallelLoopSpecializationPass() {
562   return std::make_unique<ParallelLoopSpecialization>();
563 }
564 
565 std::unique_ptr<Pass> mlir::createForLoopSpecializationPass() {
566   return std::make_unique<ForLoopSpecialization>();
567 }
568 
569 std::unique_ptr<Pass> mlir::createForLoopPeelingPass() {
570   return std::make_unique<ForLoopPeeling>();
571 }
572