2019-04-18 03:18:37 +08:00
|
|
|
//===- LoopInvariantCodeMotion.cpp - Code to perform loop fusion-----------===//
|
|
|
|
//
|
|
|
|
// Copyright 2019 The MLIR Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
// =============================================================================
|
|
|
|
//
|
|
|
|
// This file implements loop invariant code motion.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "mlir/Analysis/AffineAnalysis.h"
|
|
|
|
#include "mlir/Analysis/AffineStructures.h"
|
|
|
|
#include "mlir/Analysis/LoopAnalysis.h"
|
|
|
|
#include "mlir/Analysis/SliceAnalysis.h"
|
|
|
|
#include "mlir/Analysis/Utils.h"
|
2019-08-21 06:36:08 +08:00
|
|
|
#include "mlir/Dialect/AffineOps/AffineOps.h"
|
2019-08-20 02:00:47 +08:00
|
|
|
#include "mlir/Dialect/StandardOps/Ops.h"
|
2019-04-18 03:18:37 +08:00
|
|
|
#include "mlir/IR/AffineExpr.h"
|
|
|
|
#include "mlir/IR/AffineMap.h"
|
|
|
|
#include "mlir/IR/Builders.h"
|
|
|
|
#include "mlir/Pass/Pass.h"
|
|
|
|
#include "mlir/Transforms/LoopUtils.h"
|
|
|
|
#include "mlir/Transforms/Passes.h"
|
|
|
|
#include "mlir/Transforms/Utils.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2019-06-01 04:56:47 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2019-04-18 03:18:37 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "licm"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// Loop invariant code motion (LICM) pass.
|
|
|
|
/// TODO(asabne) : The pass is missing zero-trip tests.
|
|
|
|
/// TODO(asabne) : Check for the presence of side effects before hoisting.
|
|
|
|
struct LoopInvariantCodeMotion : public FunctionPass<LoopInvariantCodeMotion> {
|
|
|
|
void runOnFunction() override;
|
|
|
|
void runOnAffineForOp(AffineForOp forOp);
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2019-06-01 04:56:47 +08:00
|
|
|
static bool
|
|
|
|
checkInvarianceOfNestedIfOps(Operation *op, Value *indVar,
|
|
|
|
SmallPtrSetImpl<Operation *> &definedOps,
|
|
|
|
SmallPtrSetImpl<Operation *> &opsToHoist);
|
|
|
|
static bool isOpLoopInvariant(Operation &op, Value *indVar,
|
|
|
|
SmallPtrSetImpl<Operation *> &definedOps,
|
|
|
|
SmallPtrSetImpl<Operation *> &opsToHoist);
|
|
|
|
|
|
|
|
static bool
|
|
|
|
areAllOpsInTheBlockListInvariant(Region &blockList, Value *indVar,
|
|
|
|
SmallPtrSetImpl<Operation *> &definedOps,
|
|
|
|
SmallPtrSetImpl<Operation *> &opsToHoist);
|
|
|
|
|
|
|
|
static bool isMemRefDereferencingOp(Operation &op) {
|
|
|
|
// TODO(asabne): Support DMA Ops.
|
2019-07-04 01:35:03 +08:00
|
|
|
if (isa<AffineLoadOp>(op) || isa<AffineStoreOp>(op)) {
|
2019-06-01 04:56:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-14 04:33:46 +08:00
|
|
|
std::unique_ptr<OpPassBase<FuncOp>> mlir::createLoopInvariantCodeMotionPass() {
|
2019-08-18 02:05:35 +08:00
|
|
|
return std::make_unique<LoopInvariantCodeMotion>();
|
2019-04-18 03:18:37 +08:00
|
|
|
}
|
|
|
|
|
2019-06-01 04:56:47 +08:00
|
|
|
// Returns true if the individual op is loop invariant.
|
|
|
|
bool isOpLoopInvariant(Operation &op, Value *indVar,
|
|
|
|
SmallPtrSetImpl<Operation *> &definedOps,
|
|
|
|
SmallPtrSetImpl<Operation *> &opsToHoist) {
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "iterating on op: " << op;);
|
|
|
|
|
|
|
|
if (isa<AffineIfOp>(op)) {
|
|
|
|
if (!checkInvarianceOfNestedIfOps(&op, indVar, definedOps, opsToHoist)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (isa<AffineForOp>(op)) {
|
|
|
|
// If the body of a predicated region has a for loop, we don't hoist the
|
|
|
|
// 'affine.if'.
|
|
|
|
return false;
|
2019-07-04 01:35:03 +08:00
|
|
|
} else if (isa<AffineDmaStartOp>(op) || isa<AffineDmaWaitOp>(op)) {
|
2019-06-01 04:56:47 +08:00
|
|
|
// TODO(asabne): Support DMA ops.
|
|
|
|
return false;
|
|
|
|
} else if (!isa<ConstantOp>(op)) {
|
|
|
|
if (isMemRefDereferencingOp(op)) {
|
2019-07-04 01:35:03 +08:00
|
|
|
Value *memref = isa<AffineLoadOp>(op)
|
|
|
|
? cast<AffineLoadOp>(op).getMemRef()
|
|
|
|
: cast<AffineStoreOp>(op).getMemRef();
|
2019-06-01 04:56:47 +08:00
|
|
|
for (auto *user : memref->getUsers()) {
|
|
|
|
// If this memref has a user that is a DMA, give up because these
|
|
|
|
// operations write to this memref.
|
2019-07-04 01:35:03 +08:00
|
|
|
if (isa<AffineDmaStartOp>(op) || isa<AffineDmaWaitOp>(op)) {
|
2019-06-01 04:56:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// If the memref used by the load/store is used in a store elsewhere in
|
|
|
|
// the loop nest, we do not hoist. Similarly, if the memref used in a
|
|
|
|
// load is also being stored too, we do not hoist the load.
|
2019-07-04 01:35:03 +08:00
|
|
|
if (isa<AffineStoreOp>(user) ||
|
|
|
|
(isa<AffineLoadOp>(user) && isa<AffineStoreOp>(op))) {
|
2019-06-01 04:56:47 +08:00
|
|
|
if (&op != user) {
|
|
|
|
SmallVector<AffineForOp, 8> userIVs;
|
|
|
|
getLoopIVs(*user, &userIVs);
|
|
|
|
// Check that userIVs don't contain the for loop around the op.
|
|
|
|
if (llvm::is_contained(userIVs, getForInductionVarOwner(indVar))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert this op in the defined ops list.
|
|
|
|
definedOps.insert(&op);
|
|
|
|
|
|
|
|
if (op.getNumOperands() == 0 && !isa<AffineTerminatorOp>(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 the loop IV is the operand, this op isn't loop invariant.
|
|
|
|
if (indVar == op.getOperand(i)) {
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "\nLoop IV is the operand\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operandSrc != nullptr) {
|
|
|
|
LLVM_DEBUG(llvm::dbgs()
|
|
|
|
<< *operandSrc << "\nIterating on operand src\n");
|
|
|
|
|
|
|
|
// If the value was defined in the loop (outside of the
|
|
|
|
// if/else region), and that operation itself wasn't meant to
|
|
|
|
// be hoisted, then mark this operation loop dependent.
|
|
|
|
if (definedOps.count(operandSrc) && opsToHoist.count(operandSrc) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no operand was loop variant, mark this op for motion.
|
|
|
|
opsToHoist.insert(&op);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if all ops in a region (i.e. list of blocks) are loop invariant.
|
|
|
|
bool areAllOpsInTheBlockListInvariant(
|
|
|
|
Region &blockList, Value *indVar, SmallPtrSetImpl<Operation *> &definedOps,
|
|
|
|
SmallPtrSetImpl<Operation *> &opsToHoist) {
|
|
|
|
|
|
|
|
for (auto &b : blockList) {
|
|
|
|
for (auto &op : b) {
|
|
|
|
if (!isOpLoopInvariant(op, indVar, definedOps, opsToHoist)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the affine.if op can be hoisted.
|
|
|
|
bool checkInvarianceOfNestedIfOps(Operation *op, Value *indVar,
|
|
|
|
SmallPtrSetImpl<Operation *> &definedOps,
|
|
|
|
SmallPtrSetImpl<Operation *> &opsToHoist) {
|
|
|
|
assert(isa<AffineIfOp>(op));
|
|
|
|
auto ifOp = cast<AffineIfOp>(op);
|
|
|
|
|
2019-07-17 03:20:15 +08:00
|
|
|
if (!areAllOpsInTheBlockListInvariant(ifOp.thenRegion(), indVar, definedOps,
|
|
|
|
opsToHoist)) {
|
2019-06-01 04:56:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-17 03:20:15 +08:00
|
|
|
if (!areAllOpsInTheBlockListInvariant(ifOp.elseRegion(), indVar, definedOps,
|
|
|
|
opsToHoist)) {
|
2019-06-01 04:56:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-18 03:18:37 +08:00
|
|
|
void LoopInvariantCodeMotion::runOnAffineForOp(AffineForOp forOp) {
|
|
|
|
auto *loopBody = forOp.getBody();
|
2019-06-01 04:56:47 +08:00
|
|
|
auto *indVar = forOp.getInductionVar();
|
2019-04-18 03:18:37 +08:00
|
|
|
|
2019-06-01 04:56:47 +08:00
|
|
|
SmallPtrSet<Operation *, 8> definedOps;
|
2019-04-18 03:18:37 +08:00
|
|
|
// This is the place where hoisted instructions would reside.
|
2019-06-05 10:18:23 +08:00
|
|
|
OpBuilder b(forOp.getOperation());
|
2019-04-18 03:18:37 +08:00
|
|
|
|
2019-06-01 04:56:47 +08:00
|
|
|
SmallPtrSet<Operation *, 8> opsToHoist;
|
2019-04-18 03:18:37 +08:00
|
|
|
SmallVector<Operation *, 8> opsToMove;
|
|
|
|
|
|
|
|
for (auto &op : *loopBody) {
|
2019-06-01 04:56:47 +08:00
|
|
|
// We don't hoist for loops.
|
|
|
|
if (!isa<AffineForOp>(op)) {
|
|
|
|
if (!isa<AffineTerminatorOp>(op)) {
|
|
|
|
if (isOpLoopInvariant(op, indVar, definedOps, opsToHoist)) {
|
|
|
|
opsToMove.push_back(&op);
|
|
|
|
}
|
|
|
|
}
|
2019-04-18 03:18:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-01 04:56:47 +08:00
|
|
|
// For all instructions that we found to be invariant, place sequentially
|
2019-04-18 03:18:37 +08:00
|
|
|
// right before the for loop.
|
|
|
|
for (auto *op : opsToMove) {
|
|
|
|
op->moveBefore(forOp);
|
|
|
|
}
|
|
|
|
|
2019-06-01 04:56:47 +08:00
|
|
|
LLVM_DEBUG(forOp.getOperation()->print(llvm::dbgs() << "Modified loop\n"));
|
2019-04-18 03:18:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LoopInvariantCodeMotion::runOnFunction() {
|
2019-05-11 23:28:15 +08:00
|
|
|
// Walk through all loops in a function in innermost-loop-first order. This
|
|
|
|
// way, we first LICM from the inner loop, and place the ops in
|
|
|
|
// the outer loop, which in turn can be further LICM'ed.
|
2019-08-30 04:04:22 +08:00
|
|
|
getFunction().walk([&](AffineForOp op) {
|
2019-04-18 04:39:41 +08:00
|
|
|
LLVM_DEBUG(op.getOperation()->print(llvm::dbgs() << "\nOriginal loop\n"));
|
|
|
|
runOnAffineForOp(op);
|
2019-05-11 23:28:15 +08:00
|
|
|
});
|
2019-04-18 03:18:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PassRegistration<LoopInvariantCodeMotion>
|
2019-05-04 02:07:37 +08:00
|
|
|
pass("affine-loop-invariant-code-motion",
|
2019-04-18 03:18:37 +08:00
|
|
|
"Hoist loop invariant instructions outside of the loop");
|