1 //===-- RewriteLoop.cpp ---------------------------------------------------===//
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 #include "PassDetail.h"
10 #include "flang/Optimizer/Dialect/FIRDialect.h"
11 #include "flang/Optimizer/Dialect/FIROps.h"
12 #include "flang/Optimizer/Transforms/Passes.h"
13 #include "mlir/Dialect/Affine/IR/AffineOps.h"
14 #include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
15 #include "mlir/Dialect/Func/IR/FuncOps.h"
16 #include "mlir/Pass/Pass.h"
17 #include "mlir/Transforms/DialectConversion.h"
18 #include "llvm/Support/CommandLine.h"
19 
20 using namespace fir;
21 using namespace mlir;
22 
23 namespace {
24 
25 // Conversion of fir control ops to more primitive control-flow.
26 //
27 // FIR loops that cannot be converted to the affine dialect will remain as
28 // `fir.do_loop` operations.  These can be converted to control-flow operations.
29 
30 /// Convert `fir.do_loop` to CFG
31 class CfgLoopConv : public mlir::OpRewritePattern<fir::DoLoopOp> {
32 public:
33   using OpRewritePattern::OpRewritePattern;
34 
35   CfgLoopConv(mlir::MLIRContext *ctx, bool forceLoopToExecuteOnce)
36       : mlir::OpRewritePattern<fir::DoLoopOp>(ctx),
37         forceLoopToExecuteOnce(forceLoopToExecuteOnce) {}
38 
39   mlir::LogicalResult
40   matchAndRewrite(DoLoopOp loop,
41                   mlir::PatternRewriter &rewriter) const override {
42     auto loc = loop.getLoc();
43 
44     // Create the start and end blocks that will wrap the DoLoopOp with an
45     // initalizer and an end point
46     auto *initBlock = rewriter.getInsertionBlock();
47     auto initPos = rewriter.getInsertionPoint();
48     auto *endBlock = rewriter.splitBlock(initBlock, initPos);
49 
50     // Split the first DoLoopOp block in two parts. The part before will be the
51     // conditional block since it already has the induction variable and
52     // loop-carried values as arguments.
53     auto *conditionalBlock = &loop.getRegion().front();
54     conditionalBlock->addArgument(rewriter.getIndexType(), loc);
55     auto *firstBlock =
56         rewriter.splitBlock(conditionalBlock, conditionalBlock->begin());
57     auto *lastBlock = &loop.getRegion().back();
58 
59     // Move the blocks from the DoLoopOp between initBlock and endBlock
60     rewriter.inlineRegionBefore(loop.getRegion(), endBlock);
61 
62     // Get loop values from the DoLoopOp
63     auto low = loop.getLowerBound();
64     auto high = loop.getUpperBound();
65     assert(low && high && "must be a Value");
66     auto step = loop.getStep();
67 
68     // Initalization block
69     rewriter.setInsertionPointToEnd(initBlock);
70     auto diff = rewriter.create<mlir::arith::SubIOp>(loc, high, low);
71     auto distance = rewriter.create<mlir::arith::AddIOp>(loc, diff, step);
72     mlir::Value iters =
73         rewriter.create<mlir::arith::DivSIOp>(loc, distance, step);
74 
75     if (forceLoopToExecuteOnce) {
76       auto zero = rewriter.create<mlir::arith::ConstantIndexOp>(loc, 0);
77       auto cond = rewriter.create<mlir::arith::CmpIOp>(
78           loc, arith::CmpIPredicate::sle, iters, zero);
79       auto one = rewriter.create<mlir::arith::ConstantIndexOp>(loc, 1);
80       iters = rewriter.create<mlir::arith::SelectOp>(loc, cond, one, iters);
81     }
82 
83     llvm::SmallVector<mlir::Value> loopOperands;
84     loopOperands.push_back(low);
85     auto operands = loop.getIterOperands();
86     loopOperands.append(operands.begin(), operands.end());
87     loopOperands.push_back(iters);
88 
89     rewriter.create<mlir::cf::BranchOp>(loc, conditionalBlock, loopOperands);
90 
91     // Last loop block
92     auto *terminator = lastBlock->getTerminator();
93     rewriter.setInsertionPointToEnd(lastBlock);
94     auto iv = conditionalBlock->getArgument(0);
95     mlir::Value steppedIndex =
96         rewriter.create<mlir::arith::AddIOp>(loc, iv, step);
97     assert(steppedIndex && "must be a Value");
98     auto lastArg = conditionalBlock->getNumArguments() - 1;
99     auto itersLeft = conditionalBlock->getArgument(lastArg);
100     auto one = rewriter.create<mlir::arith::ConstantIndexOp>(loc, 1);
101     mlir::Value itersMinusOne =
102         rewriter.create<mlir::arith::SubIOp>(loc, itersLeft, one);
103 
104     llvm::SmallVector<mlir::Value> loopCarried;
105     loopCarried.push_back(steppedIndex);
106     auto begin = loop.getFinalValue() ? std::next(terminator->operand_begin())
107                                       : terminator->operand_begin();
108     loopCarried.append(begin, terminator->operand_end());
109     loopCarried.push_back(itersMinusOne);
110     rewriter.create<mlir::cf::BranchOp>(loc, conditionalBlock, loopCarried);
111     rewriter.eraseOp(terminator);
112 
113     // Conditional block
114     rewriter.setInsertionPointToEnd(conditionalBlock);
115     auto zero = rewriter.create<mlir::arith::ConstantIndexOp>(loc, 0);
116     auto comparison = rewriter.create<mlir::arith::CmpIOp>(
117         loc, arith::CmpIPredicate::sgt, itersLeft, zero);
118 
119     rewriter.create<mlir::cf::CondBranchOp>(
120         loc, comparison, firstBlock, llvm::ArrayRef<mlir::Value>(), endBlock,
121         llvm::ArrayRef<mlir::Value>());
122 
123     // The result of the loop operation is the values of the condition block
124     // arguments except the induction variable on the last iteration.
125     auto args = loop.getFinalValue()
126                     ? conditionalBlock->getArguments()
127                     : conditionalBlock->getArguments().drop_front();
128     rewriter.replaceOp(loop, args.drop_back());
129     return success();
130   }
131 
132 private:
133   bool forceLoopToExecuteOnce;
134 };
135 
136 /// Convert `fir.if` to control-flow
137 class CfgIfConv : public mlir::OpRewritePattern<fir::IfOp> {
138 public:
139   using OpRewritePattern::OpRewritePattern;
140 
141   CfgIfConv(mlir::MLIRContext *ctx, bool forceLoopToExecuteOnce)
142       : mlir::OpRewritePattern<fir::IfOp>(ctx) {}
143 
144   mlir::LogicalResult
145   matchAndRewrite(IfOp ifOp, mlir::PatternRewriter &rewriter) const override {
146     auto loc = ifOp.getLoc();
147 
148     // Split the block containing the 'fir.if' into two parts.  The part before
149     // will contain the condition, the part after will be the continuation
150     // point.
151     auto *condBlock = rewriter.getInsertionBlock();
152     auto opPosition = rewriter.getInsertionPoint();
153     auto *remainingOpsBlock = rewriter.splitBlock(condBlock, opPosition);
154     mlir::Block *continueBlock;
155     if (ifOp.getNumResults() == 0) {
156       continueBlock = remainingOpsBlock;
157     } else {
158       continueBlock = rewriter.createBlock(
159           remainingOpsBlock, ifOp.getResultTypes(),
160           llvm::SmallVector<mlir::Location>(ifOp.getNumResults(), loc));
161       rewriter.create<mlir::cf::BranchOp>(loc, remainingOpsBlock);
162     }
163 
164     // Move blocks from the "then" region to the region containing 'fir.if',
165     // place it before the continuation block, and branch to it.
166     auto &ifOpRegion = ifOp.getThenRegion();
167     auto *ifOpBlock = &ifOpRegion.front();
168     auto *ifOpTerminator = ifOpRegion.back().getTerminator();
169     auto ifOpTerminatorOperands = ifOpTerminator->getOperands();
170     rewriter.setInsertionPointToEnd(&ifOpRegion.back());
171     rewriter.create<mlir::cf::BranchOp>(loc, continueBlock,
172                                         ifOpTerminatorOperands);
173     rewriter.eraseOp(ifOpTerminator);
174     rewriter.inlineRegionBefore(ifOpRegion, continueBlock);
175 
176     // Move blocks from the "else" region (if present) to the region containing
177     // 'fir.if', place it before the continuation block and branch to it.  It
178     // will be placed after the "then" regions.
179     auto *otherwiseBlock = continueBlock;
180     auto &otherwiseRegion = ifOp.getElseRegion();
181     if (!otherwiseRegion.empty()) {
182       otherwiseBlock = &otherwiseRegion.front();
183       auto *otherwiseTerm = otherwiseRegion.back().getTerminator();
184       auto otherwiseTermOperands = otherwiseTerm->getOperands();
185       rewriter.setInsertionPointToEnd(&otherwiseRegion.back());
186       rewriter.create<mlir::cf::BranchOp>(loc, continueBlock,
187                                           otherwiseTermOperands);
188       rewriter.eraseOp(otherwiseTerm);
189       rewriter.inlineRegionBefore(otherwiseRegion, continueBlock);
190     }
191 
192     rewriter.setInsertionPointToEnd(condBlock);
193     rewriter.create<mlir::cf::CondBranchOp>(
194         loc, ifOp.getCondition(), ifOpBlock, llvm::ArrayRef<mlir::Value>(),
195         otherwiseBlock, llvm::ArrayRef<mlir::Value>());
196     rewriter.replaceOp(ifOp, continueBlock->getArguments());
197     return success();
198   }
199 };
200 
201 /// Convert `fir.iter_while` to control-flow.
202 class CfgIterWhileConv : public mlir::OpRewritePattern<fir::IterWhileOp> {
203 public:
204   using OpRewritePattern::OpRewritePattern;
205 
206   CfgIterWhileConv(mlir::MLIRContext *ctx, bool forceLoopToExecuteOnce)
207       : mlir::OpRewritePattern<fir::IterWhileOp>(ctx) {}
208 
209   mlir::LogicalResult
210   matchAndRewrite(fir::IterWhileOp whileOp,
211                   mlir::PatternRewriter &rewriter) const override {
212     auto loc = whileOp.getLoc();
213 
214     // Start by splitting the block containing the 'fir.do_loop' into two parts.
215     // The part before will get the init code, the part after will be the end
216     // point.
217     auto *initBlock = rewriter.getInsertionBlock();
218     auto initPosition = rewriter.getInsertionPoint();
219     auto *endBlock = rewriter.splitBlock(initBlock, initPosition);
220 
221     // Use the first block of the loop body as the condition block since it is
222     // the block that has the induction variable and loop-carried values as
223     // arguments. Split out all operations from the first block into a new
224     // block. Move all body blocks from the loop body region to the region
225     // containing the loop.
226     auto *conditionBlock = &whileOp.getRegion().front();
227     auto *firstBodyBlock =
228         rewriter.splitBlock(conditionBlock, conditionBlock->begin());
229     auto *lastBodyBlock = &whileOp.getRegion().back();
230     rewriter.inlineRegionBefore(whileOp.getRegion(), endBlock);
231     auto iv = conditionBlock->getArgument(0);
232     auto iterateVar = conditionBlock->getArgument(1);
233 
234     // Append the induction variable stepping logic to the last body block and
235     // branch back to the condition block. Loop-carried values are taken from
236     // operands of the loop terminator.
237     auto *terminator = lastBodyBlock->getTerminator();
238     rewriter.setInsertionPointToEnd(lastBodyBlock);
239     auto step = whileOp.getStep();
240     mlir::Value stepped = rewriter.create<mlir::arith::AddIOp>(loc, iv, step);
241     assert(stepped && "must be a Value");
242 
243     llvm::SmallVector<mlir::Value> loopCarried;
244     loopCarried.push_back(stepped);
245     auto begin = whileOp.getFinalValue()
246                      ? std::next(terminator->operand_begin())
247                      : terminator->operand_begin();
248     loopCarried.append(begin, terminator->operand_end());
249     rewriter.create<mlir::cf::BranchOp>(loc, conditionBlock, loopCarried);
250     rewriter.eraseOp(terminator);
251 
252     // Compute loop bounds before branching to the condition.
253     rewriter.setInsertionPointToEnd(initBlock);
254     auto lowerBound = whileOp.getLowerBound();
255     auto upperBound = whileOp.getUpperBound();
256     assert(lowerBound && upperBound && "must be a Value");
257 
258     // The initial values of loop-carried values is obtained from the operands
259     // of the loop operation.
260     llvm::SmallVector<mlir::Value> destOperands;
261     destOperands.push_back(lowerBound);
262     auto iterOperands = whileOp.getIterOperands();
263     destOperands.append(iterOperands.begin(), iterOperands.end());
264     rewriter.create<mlir::cf::BranchOp>(loc, conditionBlock, destOperands);
265 
266     // With the body block done, we can fill in the condition block.
267     rewriter.setInsertionPointToEnd(conditionBlock);
268     // The comparison depends on the sign of the step value. We fully expect
269     // this expression to be folded by the optimizer or LLVM. This expression
270     // is written this way so that `step == 0` always returns `false`.
271     auto zero = rewriter.create<mlir::arith::ConstantIndexOp>(loc, 0);
272     auto compl0 = rewriter.create<mlir::arith::CmpIOp>(
273         loc, arith::CmpIPredicate::slt, zero, step);
274     auto compl1 = rewriter.create<mlir::arith::CmpIOp>(
275         loc, arith::CmpIPredicate::sle, iv, upperBound);
276     auto compl2 = rewriter.create<mlir::arith::CmpIOp>(
277         loc, arith::CmpIPredicate::slt, step, zero);
278     auto compl3 = rewriter.create<mlir::arith::CmpIOp>(
279         loc, arith::CmpIPredicate::sle, upperBound, iv);
280     auto cmp0 = rewriter.create<mlir::arith::AndIOp>(loc, compl0, compl1);
281     auto cmp1 = rewriter.create<mlir::arith::AndIOp>(loc, compl2, compl3);
282     auto cmp2 = rewriter.create<mlir::arith::OrIOp>(loc, cmp0, cmp1);
283     // Remember to AND in the early-exit bool.
284     auto comparison =
285         rewriter.create<mlir::arith::AndIOp>(loc, iterateVar, cmp2);
286     rewriter.create<mlir::cf::CondBranchOp>(
287         loc, comparison, firstBodyBlock, llvm::ArrayRef<mlir::Value>(),
288         endBlock, llvm::ArrayRef<mlir::Value>());
289     // The result of the loop operation is the values of the condition block
290     // arguments except the induction variable on the last iteration.
291     auto args = whileOp.getFinalValue()
292                     ? conditionBlock->getArguments()
293                     : conditionBlock->getArguments().drop_front();
294     rewriter.replaceOp(whileOp, args);
295     return success();
296   }
297 };
298 
299 /// Convert FIR structured control flow ops to CFG ops.
300 class CfgConversion : public CFGConversionBase<CfgConversion> {
301 public:
302   void runOnOperation() override {
303     auto *context = &getContext();
304     mlir::RewritePatternSet patterns(context);
305     patterns.insert<CfgLoopConv, CfgIfConv, CfgIterWhileConv>(
306         context, forceLoopToExecuteOnce);
307     mlir::ConversionTarget target(*context);
308     target.addLegalDialect<mlir::AffineDialect, mlir::cf::ControlFlowDialect,
309                            FIROpsDialect, mlir::func::FuncDialect>();
310 
311     // apply the patterns
312     target.addIllegalOp<ResultOp, DoLoopOp, IfOp, IterWhileOp>();
313     target.markUnknownOpDynamicallyLegal([](Operation *) { return true; });
314     if (mlir::failed(mlir::applyPartialConversion(getOperation(), target,
315                                                   std::move(patterns)))) {
316       mlir::emitError(mlir::UnknownLoc::get(context),
317                       "error in converting to CFG\n");
318       signalPassFailure();
319     }
320   }
321 };
322 } // namespace
323 
324 /// Convert FIR's structured control flow ops to CFG ops.  This
325 /// conversion enables the `createLowerToCFGPass` to transform these to CFG
326 /// form.
327 std::unique_ptr<mlir::Pass> fir::createFirToCfgPass() {
328   return std::make_unique<CfgConversion>();
329 }
330