2019-04-18 03:18:37 +08:00
|
|
|
//===- LoopInvariantCodeMotion.cpp - Code to perform loop fusion-----------===//
|
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-04-18 03:18:37 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-04-18 03:18:37 +08:00
|
|
|
//
|
|
|
|
// This file implements loop invariant code motion.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-04-08 04:58:12 +08:00
|
|
|
#include "PassDetail.h"
|
2019-10-16 19:28:13 +08:00
|
|
|
#include "mlir/Transforms/Passes.h"
|
|
|
|
|
2019-04-18 03:18:37 +08:00
|
|
|
#include "mlir/IR/Builders.h"
|
2019-10-16 19:28:13 +08:00
|
|
|
#include "mlir/IR/Function.h"
|
2020-03-15 04:36:42 +08:00
|
|
|
#include "mlir/Interfaces/LoopLikeInterface.h"
|
2020-05-14 01:27:19 +08:00
|
|
|
#include "mlir/Interfaces/SideEffectInterfaces.h"
|
2020-06-05 18:35:46 +08:00
|
|
|
#include "mlir/Transforms/LoopUtils.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"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "licm"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// Loop invariant code motion (LICM) pass.
|
2020-04-08 04:56:16 +08:00
|
|
|
struct LoopInvariantCodeMotion
|
2020-04-08 04:58:12 +08:00
|
|
|
: public LoopInvariantCodeMotionBase<LoopInvariantCodeMotion> {
|
2019-10-16 19:28:13 +08:00
|
|
|
void runOnOperation() override;
|
2019-04-18 03:18:37 +08:00
|
|
|
};
|
2020-03-15 04:36:42 +08:00
|
|
|
} // end anonymous namespace
|
2019-04-18 03:18:37 +08:00
|
|
|
|
2019-10-16 19:28:13 +08:00
|
|
|
// Checks whether the given op can be hoisted by checking that
|
|
|
|
// - the op and any of its contained operations do not depend on SSA values
|
|
|
|
// defined inside of the loop (by means of calling definedOutside).
|
|
|
|
// - the op has no side-effects. If sideEffecting is Never, sideeffects of this
|
|
|
|
// op and its nested ops are ignored.
|
|
|
|
static bool canBeHoisted(Operation *op,
|
2020-03-10 07:01:41 +08:00
|
|
|
function_ref<bool(Value)> definedOutside) {
|
2019-10-16 19:28:13 +08:00
|
|
|
// Check that dependencies are defined outside of loop.
|
|
|
|
if (!llvm::all_of(op->getOperands(), definedOutside))
|
2019-06-01 04:56:47 +08:00
|
|
|
return false;
|
2019-10-16 19:28:13 +08:00
|
|
|
// Check whether this op is side-effect free. If we already know that there
|
|
|
|
// can be no side-effects because the surrounding op has claimed so, we can
|
|
|
|
// (and have to) skip this step.
|
2020-03-10 07:01:41 +08:00
|
|
|
if (auto memInterface = dyn_cast<MemoryEffectOpInterface>(op)) {
|
|
|
|
if (!memInterface.hasNoEffect())
|
2019-06-01 04:56:47 +08:00
|
|
|
return false;
|
2020-03-13 05:06:41 +08:00
|
|
|
// If the operation doesn't have side effects and it doesn't recursively
|
|
|
|
// have side effects, it can always be hoisted.
|
|
|
|
if (!op->hasTrait<OpTrait::HasRecursiveSideEffects>())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Otherwise, if the operation doesn't provide the memory effect interface
|
|
|
|
// and it doesn't have recursive side effects we treat it conservatively as
|
|
|
|
// side-effecting.
|
|
|
|
} else if (!op->hasTrait<OpTrait::HasRecursiveSideEffects>()) {
|
2020-03-10 07:01:41 +08:00
|
|
|
return false;
|
2019-06-01 04:56:47 +08:00
|
|
|
}
|
2020-03-10 07:01:41 +08:00
|
|
|
|
2019-10-16 19:28:13 +08:00
|
|
|
// Recurse into the regions for this op and check whether the contained ops
|
|
|
|
// can be hoisted.
|
|
|
|
for (auto ®ion : op->getRegions()) {
|
2020-06-20 03:33:21 +08:00
|
|
|
for (auto &block : region) {
|
2020-03-10 07:01:41 +08:00
|
|
|
for (auto &innerOp : block.without_terminator())
|
|
|
|
if (!canBeHoisted(&innerOp, definedOutside))
|
2019-10-16 19:28:13 +08:00
|
|
|
return false;
|
2019-06-01 04:56:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-05 18:35:46 +08:00
|
|
|
LogicalResult mlir::moveLoopInvariantCode(LoopLikeOpInterface looplike) {
|
2019-10-16 19:28:13 +08:00
|
|
|
auto &loopBody = looplike.getLoopBody();
|
2019-04-18 03:18:37 +08:00
|
|
|
|
2019-10-16 19:28:13 +08:00
|
|
|
// We use two collections here as we need to preserve the order for insertion
|
|
|
|
// and this is easiest.
|
|
|
|
SmallPtrSet<Operation *, 8> willBeMovedSet;
|
2019-04-18 03:18:37 +08:00
|
|
|
SmallVector<Operation *, 8> opsToMove;
|
|
|
|
|
2019-10-16 19:28:13 +08:00
|
|
|
// Helper to check whether an operation is loop invariant wrt. SSA properties.
|
2019-12-24 06:45:01 +08:00
|
|
|
auto isDefinedOutsideOfBody = [&](Value value) {
|
2020-01-12 00:54:04 +08:00
|
|
|
auto definingOp = value.getDefiningOp();
|
2019-10-16 19:28:13 +08:00
|
|
|
return (definingOp && !!willBeMovedSet.count(definingOp)) ||
|
|
|
|
looplike.isDefinedOutsideOfLoop(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Do not use walk here, as we do not want to go into nested regions and hoist
|
|
|
|
// operations from there. These regions might have semantics unknown to this
|
|
|
|
// rewriting. If the nested regions are loops, they will have been processed.
|
|
|
|
for (auto &block : loopBody) {
|
|
|
|
for (auto &op : block.without_terminator()) {
|
2020-03-10 07:01:41 +08:00
|
|
|
if (canBeHoisted(&op, isDefinedOutsideOfBody)) {
|
2019-10-16 19:28:13 +08:00
|
|
|
opsToMove.push_back(&op);
|
|
|
|
willBeMovedSet.insert(&op);
|
2019-06-01 04:56:47 +08:00
|
|
|
}
|
2019-04-18 03:18:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-16 19:28:13 +08:00
|
|
|
// For all instructions that we found to be invariant, move outside of the
|
|
|
|
// loop.
|
|
|
|
auto result = looplike.moveOutOfLoop(opsToMove);
|
|
|
|
LLVM_DEBUG(looplike.print(llvm::dbgs() << "Modified loop\n"));
|
|
|
|
return result;
|
2019-04-18 03:18:37 +08:00
|
|
|
}
|
|
|
|
|
2019-10-16 19:28:13 +08:00
|
|
|
void LoopInvariantCodeMotion::runOnOperation() {
|
|
|
|
// Walk through all loops in a function in innermost-loop-first order. This
|
2019-05-11 23:28:15 +08:00
|
|
|
// way, we first LICM from the inner loop, and place the ops in
|
|
|
|
// the outer loop, which in turn can be further LICM'ed.
|
2020-03-10 07:01:41 +08:00
|
|
|
getOperation()->walk([&](LoopLikeOpInterface loopLike) {
|
|
|
|
LLVM_DEBUG(loopLike.print(llvm::dbgs() << "\nOriginal loop\n"));
|
|
|
|
if (failed(moveLoopInvariantCode(loopLike)))
|
|
|
|
signalPassFailure();
|
2019-05-11 23:28:15 +08:00
|
|
|
});
|
2019-04-18 03:18:37 +08:00
|
|
|
}
|
|
|
|
|
2019-10-16 19:28:13 +08:00
|
|
|
std::unique_ptr<Pass> mlir::createLoopInvariantCodeMotionPass() {
|
|
|
|
return std::make_unique<LoopInvariantCodeMotion>();
|
|
|
|
}
|