#include "PassDetail.h"
#include "mlir/Analysis/SliceAnalysis.h"
#include "mlir/Dialect/Affine/Analysis/AffineAnalysis.h"
#include "mlir/Dialect/Affine/Analysis/AffineStructures.h"
#include "mlir/Dialect/Affine/Analysis/LoopAnalysis.h"
#include "mlir/Dialect/Affine/Analysis/Utils.h"
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Affine/LoopUtils.h"
#include "mlir/Dialect/Affine/Passes.h"
#include "mlir/Dialect/Affine/Utils.h"
#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
#include "mlir/IR/AffineExpr.h"
#include "mlir/IR/AffineMap.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/Matchers.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#define DEBUG_TYPE "licm"
using namespace mlir;
namespace {
struct LoopInvariantCodeMotion
: public AffineLoopInvariantCodeMotionBase<LoopInvariantCodeMotion> {
void runOnOperation() override;
void runOnAffineForOp(AffineForOp forOp);
};
}
static bool
checkInvarianceOfNestedIfOps(Operation *op, Value indVar, ValueRange iterArgs,
SmallPtrSetImpl<Operation *> &opsWithUsers,
SmallPtrSetImpl<Operation *> &opsToHoist);
static bool isOpLoopInvariant(Operation &op, Value indVar, ValueRange iterArgs,
SmallPtrSetImpl<Operation *> &opsWithUsers,
SmallPtrSetImpl<Operation *> &opsToHoist);
static bool
areAllOpsInTheBlockListInvariant(Region &blockList, Value indVar,
ValueRange iterArgs,
SmallPtrSetImpl<Operation *> &opsWithUsers,
SmallPtrSetImpl<Operation *> &opsToHoist);
bool isOpLoopInvariant(Operation &op, Value indVar, ValueRange iterArgs,
SmallPtrSetImpl<Operation *> &opsWithUsers,
SmallPtrSetImpl<Operation *> &opsToHoist) {
LLVM_DEBUG(llvm::dbgs() << "iterating on op: " << op;);
if (isa<AffineIfOp>(op)) {
if (!checkInvarianceOfNestedIfOps(&op, indVar, iterArgs, opsWithUsers,
opsToHoist)) {
return false;
}
} else if (auto forOp = dyn_cast<AffineForOp>(op)) {
if (!areAllOpsInTheBlockListInvariant(forOp.getLoopBody(), indVar, iterArgs,
opsWithUsers, opsToHoist)) {
return false;
}
} else if (isa<AffineDmaStartOp, AffineDmaWaitOp>(op)) {
return false;
} else if (!matchPattern(&op, m_Constant())) {
opsWithUsers.insert(&op);
if (isa<AffineMapAccessInterface>(op)) {
Value memref = isa<AffineReadOpInterface>(op)
? cast<AffineReadOpInterface>(op).getMemRef()
: cast<AffineWriteOpInterface>(op).getMemRef();
for (auto *user : memref.getUsers()) {
if (isa<AffineDmaStartOp, AffineDmaWaitOp>(op)) {
return false;
}
if (isa<AffineWriteOpInterface>(user) ||
(isa<AffineReadOpInterface>(user) &&
isa<AffineWriteOpInterface>(op))) {
if (&op != user) {
SmallVector<AffineForOp, 8> userIVs;
getLoopIVs(*user, &userIVs);
if (llvm::is_contained(userIVs, getForInductionVarOwner(indVar))) {
return false;
}
}
}
}
}
if (op.getNumOperands() == 0 && !isa<AffineYieldOp>(op)) {
LLVM_DEBUG(llvm::dbgs() << "\nNon-constant op with 0 operands\n");
return false;
}
}
for (unsigned int i = 0; i < op.getNumOperands(); ++i) {
auto *operandSrc = op.getOperand(i).getDefiningOp();
LLVM_DEBUG(
op.getOperand(i).print(llvm::dbgs() << "\nIterating on operand\n"));
if (indVar == op.getOperand(i)) {
LLVM_DEBUG(llvm::dbgs() << "\nLoop IV is the operand\n");
return false;
}
if (llvm::is_contained(iterArgs, op.getOperand(i))) {
LLVM_DEBUG(llvm::dbgs() << "\nOne of the iter_args is the operand\n");
return false;
}
if (operandSrc != nullptr) {
LLVM_DEBUG(llvm::dbgs() << *operandSrc << "\nIterating on operand src\n");
if (opsWithUsers.count(operandSrc) && opsToHoist.count(operandSrc) == 0) {
return false;
}
}
}
opsToHoist.insert(&op);
return true;
}
bool areAllOpsInTheBlockListInvariant(
Region &blockList, Value indVar, ValueRange iterArgs,
SmallPtrSetImpl<Operation *> &opsWithUsers,
SmallPtrSetImpl<Operation *> &opsToHoist) {
for (auto &b : blockList) {
for (auto &op : b) {
if (!isOpLoopInvariant(op, indVar, iterArgs, opsWithUsers, opsToHoist)) {
return false;
}
}
}
return true;
}
bool checkInvarianceOfNestedIfOps(Operation *op, Value indVar,
ValueRange iterArgs,
SmallPtrSetImpl<Operation *> &opsWithUsers,
SmallPtrSetImpl<Operation *> &opsToHoist) {
assert(isa<AffineIfOp>(op));
auto ifOp = cast<AffineIfOp>(op);
if (!areAllOpsInTheBlockListInvariant(ifOp.getThenRegion(), indVar, iterArgs,
opsWithUsers, opsToHoist)) {
return false;
}
if (!areAllOpsInTheBlockListInvariant(ifOp.getElseRegion(), indVar, iterArgs,
opsWithUsers, opsToHoist)) {
return false;
}
return true;
}
void LoopInvariantCodeMotion::runOnAffineForOp(AffineForOp forOp) {
auto *loopBody = forOp.getBody();
auto indVar = forOp.getInductionVar();
ValueRange iterArgs = forOp.getRegionIterArgs();
OpBuilder b(forOp.getOperation());
SmallPtrSet<Operation *, 8> opsToHoist;
SmallVector<Operation *, 8> opsToMove;
SmallPtrSet<Operation *, 8> opsWithUsers;
for (auto &op : *loopBody) {
if (!op.use_empty())
opsWithUsers.insert(&op);
if (!isa<AffineYieldOp>(op)) {
if (isOpLoopInvariant(op, indVar, iterArgs, opsWithUsers, opsToHoist)) {
opsToMove.push_back(&op);
}
}
}
for (auto *op : opsToMove) {
op->moveBefore(forOp);
}
LLVM_DEBUG(forOp->print(llvm::dbgs() << "Modified loop\n"));
}
void LoopInvariantCodeMotion::runOnOperation() {
getOperation().walk([&](AffineForOp op) {
LLVM_DEBUG(op->print(llvm::dbgs() << "\nOriginal loop\n"));
runOnAffineForOp(op);
});
}
std::unique_ptr<OperationPass<func::FuncOp>>
mlir::createAffineLoopInvariantCodeMotionPass() {
return std::make_unique<LoopInvariantCodeMotion>();
}