37 lcvTypes.push_back(forOp.getInductionVar().getType());
38 lcvLocs.push_back(forOp.getInductionVar().getLoc());
40 lcvTypes.push_back(
value.getType());
41 lcvLocs.push_back(
value.getLoc());
46 initArgs.push_back(forOp.getLowerBound());
47 llvm::append_range(initArgs, forOp.getInitArgs());
48 auto whileOp = rewriter.
create<WhileOp>(forOp.getLoc(), lcvTypes, initArgs,
54 &whileOp.getBefore(), whileOp.getBefore().begin(), lcvTypes, lcvLocs);
56 auto cmpOp = rewriter.
create<arith::CmpIOp>(
57 whileOp.getLoc(), arith::CmpIPredicate::slt,
58 beforeBlock->getArgument(0), forOp.getUpperBound());
59 rewriter.
create<scf::ConditionOp>(whileOp.getLoc(), cmpOp.getResult(),
60 beforeBlock->getArguments());
66 &whileOp.getAfter(), whileOp.getAfter().begin(), lcvTypes, lcvLocs);
70 auto ivIncOp = rewriter.
create<arith::AddIOp>(
71 whileOp.getLoc(), afterBlock->getArgument(0), forOp.getStep());
75 for (
const auto &barg :
enumerate(forOp.getBody(0)->getArguments()))
79 for (
auto &arg : llvm::make_early_inc_range(*forOp.getBody()))
80 arg.moveBefore(afterBlock, afterBlock->end());
83 for (
auto yieldOp : afterBlock->getOps<scf::YieldOp>()) {
85 yieldOperands.insert(yieldOperands.begin(), ivIncOp.getResult());
86 yieldOp->setOperands(yieldOperands);
94 arg.value().replaceAllUsesWith(whileOp.getResult(arg.index() + 1));
101 struct ForToWhileLoop :
public SCFForToWhileLoopBase<ForToWhileLoop> {
102 void runOnOperation()
override {
103 auto *parentOp = getOperation();
106 patterns.add<ForLoopLoweringPattern>(ctx);
113 return std::make_unique<ForToWhileLoop>();
Include the generated interface declarations.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
The OpAsmOpInterface, see OpAsmInterface.td for more details.
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
static constexpr const bool value
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
This class represents an efficient way to signal success or failure.
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
void replaceAllUsesWith(ValuesT &&values)
Replace all uses of results of this operation with the provided 'values'.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
std::unique_ptr< Pass > createForToWhileLoopPass()
MLIRContext is the top-level object for a collection of MLIR operations.
void setInsertionPointToEnd(Block *block)
Sets the insertion point to the end of the specified block.
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes=llvm::None, ArrayRef< Location > locs=llvm::None)
Add new block with 'argTypes' arguments and set the insertion point to the end of it...
LogicalResult applyPatternsAndFoldGreedily(MutableArrayRef< Region > regions, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig())
Rewrite the regions of the specified operation, which must be isolated from above, by repeatedly applying the highest benefit patterns in a greedy work-list driven manner.