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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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"
|
2019-04-18 03:18:37 +08:00
|
|
|
#include "mlir/Pass/Pass.h"
|
2019-10-16 19:28:13 +08:00
|
|
|
#include "mlir/Transforms/LoopLikeInterface.h"
|
|
|
|
#include "mlir/Transforms/SideEffectsInterface.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 {
|
|
|
|
|
2019-10-16 19:28:13 +08:00
|
|
|
using SideEffecting = SideEffectsInterface::SideEffecting;
|
|
|
|
|
2019-04-18 03:18:37 +08:00
|
|
|
/// Loop invariant code motion (LICM) pass.
|
2019-10-16 19:28:13 +08:00
|
|
|
struct LoopInvariantCodeMotion : public OperationPass<LoopInvariantCodeMotion> {
|
|
|
|
public:
|
|
|
|
void runOnOperation() override;
|
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,
|
2019-12-23 13:59:55 +08:00
|
|
|
function_ref<bool(ValuePtr)> definedOutside,
|
2019-10-16 19:28:13 +08:00
|
|
|
SideEffecting sideEffecting,
|
|
|
|
SideEffectsInterface &interface) {
|
|
|
|
// 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.
|
|
|
|
auto thisOpIsSideEffecting = sideEffecting;
|
|
|
|
if (thisOpIsSideEffecting != SideEffecting::Never) {
|
|
|
|
thisOpIsSideEffecting = interface.isSideEffecting(op);
|
|
|
|
// If the op always has sideeffects, we cannot hoist.
|
|
|
|
if (thisOpIsSideEffecting == SideEffecting::Always)
|
2019-06-01 04:56:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
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()) {
|
|
|
|
for (auto &block : region.getBlocks()) {
|
|
|
|
for (auto &innerOp : block) {
|
|
|
|
if (innerOp.isKnownTerminator())
|
|
|
|
continue;
|
|
|
|
if (!canBeHoisted(&innerOp, definedOutside, thisOpIsSideEffecting,
|
|
|
|
interface))
|
|
|
|
return false;
|
2019-06-01 04:56:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-16 19:28:13 +08:00
|
|
|
static LogicalResult moveLoopInvariantCode(LoopLikeOpInterface looplike,
|
|
|
|
SideEffectsInterface &interface) {
|
|
|
|
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-23 13:59:55 +08:00
|
|
|
auto isDefinedOutsideOfBody = [&](ValuePtr value) {
|
2019-10-16 19:28:13 +08:00
|
|
|
auto definingOp = value->getDefiningOp();
|
|
|
|
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()) {
|
|
|
|
if (canBeHoisted(&op, isDefinedOutsideOfBody,
|
|
|
|
mlir::SideEffectsDialectInterface::Recursive,
|
|
|
|
interface)) {
|
|
|
|
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
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
void LoopInvariantCodeMotion::runOnOperation() {
|
|
|
|
SideEffectsInterface interface(&getContext());
|
|
|
|
// 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.
|
2019-10-16 19:28:13 +08:00
|
|
|
getOperation()->walk([&](Operation *op) {
|
|
|
|
if (auto looplike = dyn_cast<LoopLikeOpInterface>(op)) {
|
|
|
|
LLVM_DEBUG(op->print(llvm::dbgs() << "\nOriginal loop\n"));
|
|
|
|
if (failed(moveLoopInvariantCode(looplike, interface)))
|
|
|
|
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
|
|
|
// Include the generated code for the loop-like interface here, as it otherwise
|
|
|
|
// has no compilation unit. This works as loop-invariant code motion is the
|
|
|
|
// only user of that interface.
|
|
|
|
#include "mlir/Transforms/LoopLikeInterface.cpp.inc"
|
|
|
|
|
|
|
|
std::unique_ptr<Pass> mlir::createLoopInvariantCodeMotionPass() {
|
|
|
|
return std::make_unique<LoopInvariantCodeMotion>();
|
|
|
|
}
|
|
|
|
|
2019-04-18 03:18:37 +08:00
|
|
|
static PassRegistration<LoopInvariantCodeMotion>
|
2019-10-16 19:28:13 +08:00
|
|
|
pass("loop-invariant-code-motion",
|
2019-04-18 03:18:37 +08:00
|
|
|
"Hoist loop invariant instructions outside of the loop");
|