This has been a major TODO for a very long time, and is necessary for establishing a proper dialect-free dependency layering for the Transforms library. Code was moved to effectively two main locations: * Affine/ There was quite a bit of affine dialect related code in Transforms/ do to historical reasons (of a time way into MLIR's past). The following headers were moved to: Transforms/LoopFusionUtils.h -> Dialect/Affine/LoopFusionUtils.h Transforms/LoopUtils.h -> Dialect/Affine/LoopUtils.h Transforms/Utils.h -> Dialect/Affine/Utils.h The following transforms were also moved: AffineLoopFusion, AffinePipelineDataTransfer, LoopCoalescing * SCF/ Only one SCF pass was in Transforms/ (likely accidentally placed here): ParallelLoopCollapsing The SCF specific utilities in LoopUtils have been moved to SCF/Utils.h * Misc: mlir::moveLoopInvariantCode was also moved to LoopLikeInterface.h given that it is a simple utility defined in terms of LoopLikeOpInterface. Differential Revision: https://reviews.llvm.org/D117848
73 lines
2.6 KiB
C++
73 lines
2.6 KiB
C++
//===- ControlFlowSink.cpp - Code to perform control-flow sinking ---------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file implements a basic control-flow sink pass. Control-flow sinking
|
|
// moves operations whose only uses are in conditionally-executed blocks in to
|
|
// those blocks so that they aren't executed on paths where their results are
|
|
// not needed.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "PassDetail.h"
|
|
#include "mlir/IR/Dominance.h"
|
|
#include "mlir/Interfaces/ControlFlowInterfaces.h"
|
|
#include "mlir/Transforms/ControlFlowSinkUtils.h"
|
|
#include "mlir/Transforms/Passes.h"
|
|
|
|
using namespace mlir;
|
|
|
|
namespace {
|
|
/// A control-flow sink pass.
|
|
struct ControlFlowSink : public ControlFlowSinkBase<ControlFlowSink> {
|
|
void runOnOperation() override;
|
|
};
|
|
} // end anonymous namespace
|
|
|
|
/// Returns true if the given operation is side-effect free as are all of its
|
|
/// nested operations.
|
|
static bool isSideEffectFree(Operation *op) {
|
|
if (auto memInterface = dyn_cast<MemoryEffectOpInterface>(op)) {
|
|
// If the op has side-effects, it cannot be moved.
|
|
if (!memInterface.hasNoEffect())
|
|
return false;
|
|
// If the op does not have recursive side effects, then it can be moved.
|
|
if (!op->hasTrait<OpTrait::HasRecursiveSideEffects>())
|
|
return true;
|
|
} else if (!op->hasTrait<OpTrait::HasRecursiveSideEffects>()) {
|
|
// Otherwise, if the op does not implement the memory effect interface and
|
|
// it does not have recursive side effects, then it cannot be known that the
|
|
// op is moveable.
|
|
return false;
|
|
}
|
|
|
|
// Recurse into the regions and ensure that all nested ops can also be moved.
|
|
for (Region ®ion : op->getRegions())
|
|
for (Operation &op : region.getOps())
|
|
if (!isSideEffectFree(&op))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
void ControlFlowSink::runOnOperation() {
|
|
auto &domInfo = getAnalysis<DominanceInfo>();
|
|
getOperation()->walk([&](RegionBranchOpInterface branch) {
|
|
SmallVector<Region *> regionsToSink;
|
|
// Get the regions are that known to be executed at most once.
|
|
getSinglyExecutedRegionsToSink(branch, regionsToSink);
|
|
// Sink side-effect free operations.
|
|
numSunk =
|
|
controlFlowSink(regionsToSink, domInfo, [](Operation *op, Region *) {
|
|
return isSideEffectFree(op);
|
|
});
|
|
});
|
|
}
|
|
|
|
std::unique_ptr<Pass> mlir::createControlFlowSinkPass() {
|
|
return std::make_unique<ControlFlowSink>();
|
|
}
|