1//===- ForToWhile.cpp - scf.for to scf.while loop conversion --------------===//
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// Transforms SCF.ForOp's into SCF.WhileOp's.
10//
11//===----------------------------------------------------------------------===//
12
13#include "mlir/Dialect/SCF/Transforms/Passes.h"
14
15#include "mlir/Dialect/Arith/IR/Arith.h"
16#include "mlir/Dialect/SCF/IR/SCF.h"
17#include "mlir/Dialect/SCF/Transforms/Transforms.h"
18#include "mlir/IR/PatternMatch.h"
19#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
20
21namespace mlir {
22#define GEN_PASS_DEF_SCFFORTOWHILELOOP
23#include "mlir/Dialect/SCF/Transforms/Passes.h.inc"
24} // namespace mlir
25
26using namespace mlir;
27using scf::ForOp;
28using scf::WhileOp;
29
30namespace {
31
32struct ForLoopLoweringPattern : public OpRewritePattern<ForOp> {
33 using OpRewritePattern<ForOp>::OpRewritePattern;
34
35 LogicalResult matchAndRewrite(ForOp forOp,
36 PatternRewriter &rewriter) const override {
37 // Generate type signature for the loop-carried values. The induction
38 // variable is placed first, followed by the forOp.iterArgs.
39 SmallVector<Type> lcvTypes;
40 SmallVector<Location> lcvLocs;
41 lcvTypes.push_back(Elt: forOp.getInductionVar().getType());
42 lcvLocs.push_back(Elt: forOp.getInductionVar().getLoc());
43 for (Value value : forOp.getInitArgs()) {
44 lcvTypes.push_back(Elt: value.getType());
45 lcvLocs.push_back(Elt: value.getLoc());
46 }
47
48 // Build scf.WhileOp
49 SmallVector<Value> initArgs;
50 initArgs.push_back(Elt: forOp.getLowerBound());
51 llvm::append_range(C&: initArgs, R: forOp.getInitArgs());
52 auto whileOp =
53 WhileOp::create(builder&: rewriter, location: forOp.getLoc(), resultTypes: lcvTypes, operands: initArgs,
54 attributes: forOp->getDiscardableAttrDictionary().getValue());
55
56 // 'before' region contains the loop condition and forwarding of iteration
57 // arguments to the 'after' region.
58 auto *beforeBlock = rewriter.createBlock(
59 parent: &whileOp.getBefore(), insertPt: whileOp.getBefore().begin(), argTypes: lcvTypes, locs: lcvLocs);
60 rewriter.setInsertionPointToStart(whileOp.getBeforeBody());
61 arith::CmpIPredicate predicate = forOp.getUnsignedCmp()
62 ? arith::CmpIPredicate::ult
63 : arith::CmpIPredicate::slt;
64 auto cmpOp = arith::CmpIOp::create(builder&: rewriter, location: whileOp.getLoc(), predicate,
65 lhs: beforeBlock->getArgument(i: 0),
66 rhs: forOp.getUpperBound());
67 scf::ConditionOp::create(builder&: rewriter, location: whileOp.getLoc(), condition: cmpOp.getResult(),
68 args: beforeBlock->getArguments());
69
70 // Inline for-loop body into an executeRegion operation in the "after"
71 // region. The return type of the execRegionOp does not contain the
72 // iv - yields in the source for-loop contain only iterArgs.
73 auto *afterBlock = rewriter.createBlock(
74 parent: &whileOp.getAfter(), insertPt: whileOp.getAfter().begin(), argTypes: lcvTypes, locs: lcvLocs);
75
76 // Add induction variable incrementation
77 rewriter.setInsertionPointToEnd(afterBlock);
78 auto ivIncOp =
79 arith::AddIOp::create(builder&: rewriter, location: whileOp.getLoc(),
80 lhs: afterBlock->getArgument(i: 0), rhs: forOp.getStep());
81
82 // Rewrite uses of the for-loop block arguments to the new while-loop
83 // "after" arguments
84 for (const auto &barg : enumerate(First: forOp.getBody(idx: 0)->getArguments()))
85 rewriter.replaceAllUsesWith(from: barg.value(),
86 to: afterBlock->getArgument(i: barg.index()));
87
88 // Inline for-loop body operations into 'after' region.
89 for (auto &arg : llvm::make_early_inc_range(Range&: *forOp.getBody()))
90 rewriter.moveOpBefore(op: &arg, block: afterBlock, iterator: afterBlock->end());
91
92 // Add incremented IV to yield operations
93 for (auto yieldOp : afterBlock->getOps<scf::YieldOp>()) {
94 SmallVector<Value> yieldOperands = yieldOp.getOperands();
95 yieldOperands.insert(I: yieldOperands.begin(), Elt: ivIncOp.getResult());
96 rewriter.modifyOpInPlace(root: yieldOp,
97 callable: [&]() { yieldOp->setOperands(yieldOperands); });
98 }
99
100 // We cannot do a direct replacement of the forOp since the while op returns
101 // an extra value (the induction variable escapes the loop through being
102 // carried in the set of iterargs). Instead, rewrite uses of the forOp
103 // results.
104 for (const auto &arg : llvm::enumerate(First: forOp.getResults()))
105 rewriter.replaceAllUsesWith(from: arg.value(),
106 to: whileOp.getResult(i: arg.index() + 1));
107
108 rewriter.eraseOp(op: forOp);
109 return success();
110 }
111};
112
113struct ForToWhileLoop : public impl::SCFForToWhileLoopBase<ForToWhileLoop> {
114 void runOnOperation() override {
115 auto *parentOp = getOperation();
116 MLIRContext *ctx = parentOp->getContext();
117 RewritePatternSet patterns(ctx);
118 patterns.add<ForLoopLoweringPattern>(arg&: ctx);
119 (void)applyPatternsGreedily(op: parentOp, patterns: std::move(patterns));
120 }
121};
122} // namespace
123
124std::unique_ptr<Pass> mlir::createForToWhileLoopPass() {
125 return std::make_unique<ForToWhileLoop>();
126}
127