23#define GEN_PASS_DEF_AFFINELOOPINVARIANTCODEMOTION
24#include "mlir/Dialect/Affine/Transforms/Passes.h.inc"
28#define DEBUG_TYPE "affine-licm"
42struct LoopInvariantCodeMotion
44 LoopInvariantCodeMotion> {
45 void runOnOperation()
override;
46 void runOnAffineForOp(AffineForOp forOp);
67 Value iv = loop.getInductionVar();
69 if (
auto ifOp = dyn_cast<AffineIfOp>(op)) {
72 }
else if (
auto forOp = dyn_cast<AffineForOp>(op)) {
76 }
else if (
auto parOp = dyn_cast<AffineParallelOp>(op)) {
81 !isa<AffineReadOpInterface, AffineWriteOpInterface>(&op)) {
85 }
else if (isa<AffineReadOpInterface, AffineWriteOpInterface>(op)) {
87 opsWithUsers.insert(&op);
89 auto read = dyn_cast<AffineReadOpInterface>(op);
91 read ? read.getMemRef() : cast<AffineWriteOpInterface>(op).getMemRef();
92 for (
auto *user :
memref.getUsers()) {
101 isa<AffineWriteOpInterface>(op))) {
105 if (llvm::is_contained(userIVs, loop))
112 ValueRange iterArgs = loop.getRegionIterArgs();
121 if (llvm::is_contained(iterArgs, op.
getOperand(i)))
128 if (opsWithUsers.count(operandSrc) && opsToHoist.count(operandSrc) == 0)
134 opsToHoist.insert(&op);
144 for (
auto &
b : blockList) {
160 opsWithUsers, opsToHoist))
164 opsWithUsers, opsToHoist))
170void LoopInvariantCodeMotion::runOnAffineForOp(AffineForOp forOp) {
172 OpBuilder
b(forOp.getOperation());
174 SmallPtrSet<Operation *, 8> opsToHoist;
175 SmallVector<Operation *, 8> opsToMove;
176 SmallPtrSet<Operation *, 8> opsWithUsers;
182 auto tripCount = forOp.getStaticTripCount();
183 bool guaranteedToExecute =
184 tripCount.has_value() && tripCount->getZExtValue() > 0;
186 for (Operation &op : *forOp.getBody()) {
191 opsWithUsers.insert(&op);
192 if (!isa<AffineYieldOp>(op)) {
195 if (!guaranteedToExecute && !
isPure(&op))
198 opsToMove.push_back(&op);
205 for (
auto *op : opsToMove) {
206 op->moveBefore(forOp);
213void LoopInvariantCodeMotion::runOnOperation() {
217 getOperation().walk([&](AffineForOp op) { runOnAffineForOp(op); });
220std::unique_ptr<OperationPass<func::FuncOp>>
222 return std::make_unique<LoopInvariantCodeMotion>();
static bool isOpLoopInvariant(Operation &op, AffineForOp loop, SmallPtrSetImpl< Operation * > &opsWithUsers, SmallPtrSetImpl< Operation * > &opsToHoist)
Returns true if op is invariant on loop.
static bool areAllOpsInTheBlockListInvariant(Region &blockList, AffineForOp loop, SmallPtrSetImpl< Operation * > &opsWithUsers, SmallPtrSetImpl< Operation * > &opsToHoist)
static bool checkInvarianceOfNestedIfOps(AffineIfOp ifOp, AffineForOp loop, SmallPtrSetImpl< Operation * > &opsWithUsers, SmallPtrSetImpl< Operation * > &opsToHoist)
Operation is the basic unit of execution within MLIR.
Value getOperand(unsigned idx)
unsigned getNumOperands()
This class contains a list of basic blocks and a link to the parent operation it is attached to.
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...
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
std::unique_ptr< OperationPass< func::FuncOp > > createAffineLoopInvariantCodeMotionPass()
Creates a loop invariant code motion pass that hoists loop invariant operations out of affine loops.
void getAffineForIVs(Operation &op, SmallVectorImpl< AffineForOp > *loops)
Populates 'loops' with IVs of the affine.for ops surrounding 'op' ordered from the outermost 'affine....
Include the generated interface declarations.
bool isPure(Operation *op)
Returns true if the given operation is pure, i.e., is speculatable that does not touch memory.
bool isMemoryEffectFree(Operation *op)
Returns true if the given operation is free of memory effects.
size_t moveLoopInvariantCodeFromNestedRegions(LoopLikeOpInterface loopLike)
Move loop invariant code out of the control-flow regions nested in the body of a loop-like op,...
bool mightHaveEffect(Operation *op)
Returns "true" if op might have an effect of type EffectTy.