diff options
Diffstat (limited to 'mlir/lib/Transforms')
-rw-r--r-- | mlir/lib/Transforms/CSE.cpp | 16 | ||||
-rw-r--r-- | mlir/lib/Transforms/LoopInvariantCodeMotion.cpp | 16 | ||||
-rw-r--r-- | mlir/lib/Transforms/Utils/FoldUtils.cpp | 6 | ||||
-rw-r--r-- | mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp | 15 | ||||
-rw-r--r-- | mlir/lib/Transforms/Utils/LoopUtils.cpp | 2 | ||||
-rw-r--r-- | mlir/lib/Transforms/Utils/RegionUtils.cpp | 6 |
6 files changed, 34 insertions, 27 deletions
diff --git a/mlir/lib/Transforms/CSE.cpp b/mlir/lib/Transforms/CSE.cpp index 3a76594..42ba715 100644 --- a/mlir/lib/Transforms/CSE.cpp +++ b/mlir/lib/Transforms/CSE.cpp @@ -127,6 +127,13 @@ LogicalResult CSE::simplifyOperation(ScopedMapTy &knownValues, Operation *op) { if (op->isKnownTerminator()) return failure(); + // If the operation is already trivially dead just add it to the erase list. + if (isOpTriviallyDead(op)) { + opsToErase.push_back(op); + ++numDCE; + return success(); + } + // Don't simplify operations with nested blocks. We don't currently model // equality comparisons correctly among other things. It is also unclear // whether we would want to CSE such operations. @@ -135,16 +142,9 @@ LogicalResult CSE::simplifyOperation(ScopedMapTy &knownValues, Operation *op) { // TODO(riverriddle) We currently only eliminate non side-effecting // operations. - if (!op->hasNoSideEffect()) + if (!MemoryEffectOpInterface::hasNoEffect(op)) return failure(); - // If the operation is already trivially dead just add it to the erase list. - if (op->use_empty()) { - opsToErase.push_back(op); - ++numDCE; - return success(); - } - // Look for an existing definition for the operation. if (auto *existing = knownValues.lookup(op)) { // If we find one then replace all uses of the current operation with the diff --git a/mlir/lib/Transforms/LoopInvariantCodeMotion.cpp b/mlir/lib/Transforms/LoopInvariantCodeMotion.cpp index a452e33..7300948 100644 --- a/mlir/lib/Transforms/LoopInvariantCodeMotion.cpp +++ b/mlir/lib/Transforms/LoopInvariantCodeMotion.cpp @@ -49,16 +49,18 @@ static bool canBeHoisted(Operation *op, if (auto memInterface = dyn_cast<MemoryEffectOpInterface>(op)) { if (!memInterface.hasNoEffect()) return false; - } else if (!op->hasNoSideEffect() && - !op->hasTrait<OpTrait::HasRecursiveSideEffects>()) { + // 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>()) { return false; } - // 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; - // Recurse into the regions for this op and check whether the contained ops // can be hoisted. for (auto ®ion : op->getRegions()) { diff --git a/mlir/lib/Transforms/Utils/FoldUtils.cpp b/mlir/lib/Transforms/Utils/FoldUtils.cpp index f374d38..66535ec 100644 --- a/mlir/lib/Transforms/Utils/FoldUtils.cpp +++ b/mlir/lib/Transforms/Utils/FoldUtils.cpp @@ -126,6 +126,12 @@ void OperationFolder::notifyRemoval(Operation *op) { referencedDialects.erase(it); } +/// Clear out any constants cached inside of the folder. +void OperationFolder::clear() { + foldScopes.clear(); + referencedDialects.clear(); +} + /// Tries to perform folding on the given `op`. If successful, populates /// `results` with the results of the folding. LogicalResult OperationFolder::tryToFold( diff --git a/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp b/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp index f9a9be5..e40a4d9 100644 --- a/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp +++ b/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp @@ -10,9 +10,8 @@ // //===----------------------------------------------------------------------===// -#include "mlir/Dialect/StandardOps/IR/Ops.h" -#include "mlir/IR/Builders.h" #include "mlir/IR/PatternMatch.h" +#include "mlir/Interfaces/SideEffects.h" #include "mlir/Transforms/FoldUtils.h" #include "mlir/Transforms/RegionUtils.h" #include "llvm/ADT/DenseMap.h" @@ -162,11 +161,8 @@ bool GreedyPatternRewriteDriver::simplify(MutableArrayRef<Region> regions, if (op == nullptr) continue; - // If the operation has no side effects, and no users, then it is - // trivially dead - remove it. - if (op->isKnownNonTerminator() && op->hasNoSideEffect() && - op->use_empty()) { - // Be careful to update bookkeeping. + // If the operation is trivially dead - remove it. + if (isOpTriviallyDead(op)) { notifyOperationRemoved(op); op->erase(); continue; @@ -204,7 +200,10 @@ bool GreedyPatternRewriteDriver::simplify(MutableArrayRef<Region> regions, // After applying patterns, make sure that the CFG of each of the regions is // kept up to date. - changed |= succeeded(simplifyRegions(regions)); + if (succeeded(simplifyRegions(regions))) { + folder.clear(); + changed = true; + } } while (changed && ++i < maxIterations); // Whether the rewrite converges, i.e. wasn't changed in the last iteration. return !changed; diff --git a/mlir/lib/Transforms/Utils/LoopUtils.cpp b/mlir/lib/Transforms/Utils/LoopUtils.cpp index a02c71a..7095e55 100644 --- a/mlir/lib/Transforms/Utils/LoopUtils.cpp +++ b/mlir/lib/Transforms/Utils/LoopUtils.cpp @@ -887,7 +887,7 @@ static LogicalResult hoistOpsBetween(loop::ForOp outer, loop::ForOp inner) { } // Skip if op has side effects. // TODO(ntv): loads to immutable memory regions are ok. - if (!op.hasNoSideEffect()) { + if (!MemoryEffectOpInterface::hasNoEffect(&op)) { status = failure(); continue; } diff --git a/mlir/lib/Transforms/Utils/RegionUtils.cpp b/mlir/lib/Transforms/Utils/RegionUtils.cpp index 78bb609b..162091c 100644 --- a/mlir/lib/Transforms/Utils/RegionUtils.cpp +++ b/mlir/lib/Transforms/Utils/RegionUtils.cpp @@ -12,6 +12,7 @@ #include "mlir/IR/RegionGraphTraits.h" #include "mlir/IR/Value.h" #include "mlir/Interfaces/ControlFlowInterfaces.h" +#include "mlir/Interfaces/SideEffects.h" #include "llvm/ADT/DepthFirstIterator.h" #include "llvm/ADT/PostOrderIterator.h" @@ -196,9 +197,8 @@ static bool isOpIntrinsicallyLive(Operation *op) { if (!op->isKnownNonTerminator()) return true; // If the op has a side effect, we treat it as live. - if (!op->hasNoSideEffect()) - return true; - return false; + // TODO: Properly handle region side effects. + return !MemoryEffectOpInterface::hasNoEffect(op) || op->getNumRegions() != 0; } static void propagateLiveness(Region ®ion, LiveMap &liveMap); |