67 scf::WhileOp whileOp,
RewriterBase &rewriter,
bool forceCreateCheck) {
70 if (!forceCreateCheck && isa<scf::YieldOp>(whileOp.getAfterBody()->front())) {
77 Block *beforeBlock = whileOp.getBeforeBody();
79 for (
auto [arg, init] :
80 llvm::zip_equal(beforeBlock->
getArguments(), whileOp.getInits())) {
81 mapper.
map(arg, init);
84 for (
auto &op : *beforeBlock) {
85 if (isa<scf::ConditionOp>(op)) {
90 rewriter.
insert(op.clone(mapper));
93 scf::ConditionOp condOp = whileOp.getConditionOp();
96 condOp.getArgs(), [&](
Value arg) { return mapper.lookupOrDefault(arg); });
99 auto newLoopOp = rewriter.
create<scf::WhileOp>(
100 whileOp.getLoc(), whileOp.getResultTypes(), clonedCondArgs,
103 auto newBlock = builder.getBlock();
104 rewriter.
mergeBlocks(whileOp.getAfterBody(), newBlock, args);
105 auto yieldOp = cast<scf::YieldOp>(newBlock->getTerminator());
106 rewriter.
mergeBlocks(whileOp.getBeforeBody(), newBlock,
107 yieldOp.getResults());
112 builder.
create<scf::YieldOp>(loc, args);
116 auto ifOp = rewriter.
create<scf::IfOp>(
117 whileOp.getLoc(), clonedCondition,
120 rewriter.
moveOpBefore(newLoopOp, builder.getInsertionBlock(),
121 builder.getInsertionPoint());
122 builder.create<scf::YieldOp>(loc, newLoopOp.getResults());
126 builder.
create<scf::YieldOp>(loc, clonedCondArgs);
Block represents an ordered list of Operations.
BlockArgListType getArguments()
This is a utility class for mapping one set of IR entities to another.
auto lookupOrDefault(T from) const
Lookup a mapped value within the map.
void map(Value from, Value to)
Inserts a new mapping for 'from' to 'to'.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
RAII guard to reset the insertion point of the builder when destroyed.
This class helps build Operations.
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Operation * insert(Operation *op)
Insert the given operation at the current insertion point and return it.
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
void mergeBlocks(Block *source, Block *dest, ValueRange argValues=std::nullopt)
Inline the operations of block 'source' into the end of block 'dest'.
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...
This class provides an abstraction over the different types of ranges over Values.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
FailureOr< WhileOp > wrapWhileLoopInZeroTripCheck(WhileOp whileOp, RewriterBase &rewriter, bool forceCreateCheck=false)
Create zero-trip-check around a while op and return the new loop op in the check.
Include the generated interface declarations.