22 #define GEN_PASS_DEF_SCFFORTOWHILELOOP
23 #include "mlir/Dialect/SCF/Transforms/Passes.h.inc"
36 LogicalResult matchAndRewrite(ForOp forOp,
42 lcvTypes.push_back(forOp.getInductionVar().getType());
43 lcvLocs.push_back(forOp.getInductionVar().getLoc());
44 for (
Value value : forOp.getInitArgs()) {
45 lcvTypes.push_back(value.getType());
46 lcvLocs.push_back(value.getLoc());
51 initArgs.push_back(forOp.getLowerBound());
52 llvm::append_range(initArgs, forOp.getInitArgs());
53 auto whileOp = rewriter.
create<WhileOp>(forOp.getLoc(), lcvTypes, initArgs,
59 &whileOp.getBefore(), whileOp.getBefore().begin(), lcvTypes, lcvLocs);
61 auto cmpOp = rewriter.
create<arith::CmpIOp>(
62 whileOp.getLoc(), arith::CmpIPredicate::slt,
63 beforeBlock->getArgument(0), forOp.getUpperBound());
64 rewriter.
create<scf::ConditionOp>(whileOp.getLoc(), cmpOp.getResult(),
65 beforeBlock->getArguments());
71 &whileOp.getAfter(), whileOp.getAfter().begin(), lcvTypes, lcvLocs);
75 auto ivIncOp = rewriter.
create<arith::AddIOp>(
76 whileOp.getLoc(), afterBlock->getArgument(0), forOp.getStep());
80 for (
const auto &barg :
enumerate(forOp.getBody(0)->getArguments()))
82 afterBlock->getArgument(barg.index()));
85 for (
auto &arg : llvm::make_early_inc_range(*forOp.getBody()))
86 rewriter.
moveOpBefore(&arg, afterBlock, afterBlock->end());
89 for (
auto yieldOp : afterBlock->getOps<scf::YieldOp>()) {
91 yieldOperands.insert(yieldOperands.begin(), ivIncOp.getResult());
93 [&]() { yieldOp->setOperands(yieldOperands); });
102 whileOp.getResult(arg.index() + 1));
109 struct ForToWhileLoop :
public impl::SCFForToWhileLoopBase<ForToWhileLoop> {
110 void runOnOperation()
override {
111 auto *parentOp = getOperation();
114 patterns.add<ForLoopLoweringPattern>(ctx);
121 return std::make_unique<ForToWhileLoop>();
MLIRContext is the top-level object for a collection of MLIR operations.
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
void setInsertionPointToEnd(Block *block)
Sets the insertion point to the end of the specified block.
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes=std::nullopt, ArrayRef< Location > locs=std::nullopt)
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
void replaceAllUsesWith(Value from, Value to)
Find uses of from and replace them with to.
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
void moveOpBefore(Operation *op, Operation *existingOp)
Unlink this operation from its current block and insert it right before existingOp which may be in th...
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
The OpAsmOpInterface, see OpAsmInterface.td for more details.
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
Include the generated interface declarations.
std::unique_ptr< Pass > createForToWhileLoopPass()
LogicalResult applyPatternsAndFoldGreedily(Region ®ion, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr)
Rewrite ops in the given region, which must be isolated from above, by repeatedly applying the highes...
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...