|
14 | 14 | #include "flang/Optimizer/Support/InternalNames.h"
|
15 | 15 | #include "flang/Optimizer/Support/TypeCode.h"
|
16 | 16 | #include "flang/Optimizer/Transforms/Passes.h"
|
17 |
| -#include "flang/Optimizer/Transforms/Utils.h" |
18 | 17 | #include "flang/Runtime/derived-api.h"
|
19 | 18 | #include "mlir/Dialect/Affine/IR/AffineOps.h"
|
20 | 19 | #include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
|
@@ -51,7 +50,103 @@ class CfgLoopConv : public mlir::OpRewritePattern<fir::DoLoopOp> {
|
51 | 50 | llvm::LogicalResult
|
52 | 51 | matchAndRewrite(DoLoopOp loop,
|
53 | 52 | mlir::PatternRewriter &rewriter) const override {
|
54 |
| - convertDoLoopToCFG(loop, rewriter, setNSW, forceLoopToExecuteOnce); |
| 53 | + auto loc = loop.getLoc(); |
| 54 | + mlir::arith::IntegerOverflowFlags flags{}; |
| 55 | + if (setNSW) |
| 56 | + flags = bitEnumSet(flags, mlir::arith::IntegerOverflowFlags::nsw); |
| 57 | + auto iofAttr = mlir::arith::IntegerOverflowFlagsAttr::get( |
| 58 | + rewriter.getContext(), flags); |
| 59 | + |
| 60 | + // Create the start and end blocks that will wrap the DoLoopOp with an |
| 61 | + // initalizer and an end point |
| 62 | + auto *initBlock = rewriter.getInsertionBlock(); |
| 63 | + auto initPos = rewriter.getInsertionPoint(); |
| 64 | + auto *endBlock = rewriter.splitBlock(initBlock, initPos); |
| 65 | + |
| 66 | + // Split the first DoLoopOp block in two parts. The part before will be the |
| 67 | + // conditional block since it already has the induction variable and |
| 68 | + // loop-carried values as arguments. |
| 69 | + auto *conditionalBlock = &loop.getRegion().front(); |
| 70 | + conditionalBlock->addArgument(rewriter.getIndexType(), loc); |
| 71 | + auto *firstBlock = |
| 72 | + rewriter.splitBlock(conditionalBlock, conditionalBlock->begin()); |
| 73 | + auto *lastBlock = &loop.getRegion().back(); |
| 74 | + |
| 75 | + // Move the blocks from the DoLoopOp between initBlock and endBlock |
| 76 | + rewriter.inlineRegionBefore(loop.getRegion(), endBlock); |
| 77 | + |
| 78 | + // Get loop values from the DoLoopOp |
| 79 | + auto low = loop.getLowerBound(); |
| 80 | + auto high = loop.getUpperBound(); |
| 81 | + assert(low && high && "must be a Value"); |
| 82 | + auto step = loop.getStep(); |
| 83 | + |
| 84 | + // Initalization block |
| 85 | + rewriter.setInsertionPointToEnd(initBlock); |
| 86 | + auto diff = mlir::arith::SubIOp::create(rewriter, loc, high, low); |
| 87 | + auto distance = mlir::arith::AddIOp::create(rewriter, loc, diff, step); |
| 88 | + mlir::Value iters = |
| 89 | + mlir::arith::DivSIOp::create(rewriter, loc, distance, step); |
| 90 | + |
| 91 | + if (forceLoopToExecuteOnce) { |
| 92 | + auto zero = mlir::arith::ConstantIndexOp::create(rewriter, loc, 0); |
| 93 | + auto cond = mlir::arith::CmpIOp::create( |
| 94 | + rewriter, loc, arith::CmpIPredicate::sle, iters, zero); |
| 95 | + auto one = mlir::arith::ConstantIndexOp::create(rewriter, loc, 1); |
| 96 | + iters = mlir::arith::SelectOp::create(rewriter, loc, cond, one, iters); |
| 97 | + } |
| 98 | + |
| 99 | + llvm::SmallVector<mlir::Value> loopOperands; |
| 100 | + loopOperands.push_back(low); |
| 101 | + auto operands = loop.getIterOperands(); |
| 102 | + loopOperands.append(operands.begin(), operands.end()); |
| 103 | + loopOperands.push_back(iters); |
| 104 | + |
| 105 | + mlir::cf::BranchOp::create(rewriter, loc, conditionalBlock, loopOperands); |
| 106 | + |
| 107 | + // Last loop block |
| 108 | + auto *terminator = lastBlock->getTerminator(); |
| 109 | + rewriter.setInsertionPointToEnd(lastBlock); |
| 110 | + auto iv = conditionalBlock->getArgument(0); |
| 111 | + mlir::Value steppedIndex = |
| 112 | + mlir::arith::AddIOp::create(rewriter, loc, iv, step, iofAttr); |
| 113 | + assert(steppedIndex && "must be a Value"); |
| 114 | + auto lastArg = conditionalBlock->getNumArguments() - 1; |
| 115 | + auto itersLeft = conditionalBlock->getArgument(lastArg); |
| 116 | + auto one = mlir::arith::ConstantIndexOp::create(rewriter, loc, 1); |
| 117 | + mlir::Value itersMinusOne = |
| 118 | + mlir::arith::SubIOp::create(rewriter, loc, itersLeft, one); |
| 119 | + |
| 120 | + llvm::SmallVector<mlir::Value> loopCarried; |
| 121 | + loopCarried.push_back(steppedIndex); |
| 122 | + auto begin = loop.getFinalValue() ? std::next(terminator->operand_begin()) |
| 123 | + : terminator->operand_begin(); |
| 124 | + loopCarried.append(begin, terminator->operand_end()); |
| 125 | + loopCarried.push_back(itersMinusOne); |
| 126 | + auto backEdge = mlir::cf::BranchOp::create(rewriter, loc, conditionalBlock, |
| 127 | + loopCarried); |
| 128 | + rewriter.eraseOp(terminator); |
| 129 | + |
| 130 | + // Copy loop annotations from the do loop to the loop back edge. |
| 131 | + if (auto ann = loop.getLoopAnnotation()) |
| 132 | + backEdge->setAttr("loop_annotation", *ann); |
| 133 | + |
| 134 | + // Conditional block |
| 135 | + rewriter.setInsertionPointToEnd(conditionalBlock); |
| 136 | + auto zero = mlir::arith::ConstantIndexOp::create(rewriter, loc, 0); |
| 137 | + auto comparison = mlir::arith::CmpIOp::create( |
| 138 | + rewriter, loc, arith::CmpIPredicate::sgt, itersLeft, zero); |
| 139 | + |
| 140 | + mlir::cf::CondBranchOp::create(rewriter, loc, comparison, firstBlock, |
| 141 | + llvm::ArrayRef<mlir::Value>(), endBlock, |
| 142 | + llvm::ArrayRef<mlir::Value>()); |
| 143 | + |
| 144 | + // The result of the loop operation is the values of the condition block |
| 145 | + // arguments except the induction variable on the last iteration. |
| 146 | + auto args = loop.getFinalValue() |
| 147 | + ? conditionalBlock->getArguments() |
| 148 | + : conditionalBlock->getArguments().drop_front(); |
| 149 | + rewriter.replaceOp(loop, args.drop_back()); |
55 | 150 | return success();
|
56 | 151 | }
|
57 | 152 |
|
|
0 commit comments