diff --git a/mlir/lib/Interfaces/SideEffectInterfaces.cpp b/mlir/lib/Interfaces/SideEffectInterfaces.cpp index c9feb001a1984..59fd19310cea5 100644 --- a/mlir/lib/Interfaces/SideEffectInterfaces.cpp +++ b/mlir/lib/Interfaces/SideEffectInterfaces.cpp @@ -10,6 +10,7 @@ #include "mlir/IR/SymbolTable.h" #include "llvm/ADT/SmallPtrSet.h" +#include using namespace mlir; @@ -41,10 +42,18 @@ bool mlir::isOpTriviallyDead(Operation *op) { /// allows for marking region operations as trivially dead without always being /// conservative of terminators. static bool wouldOpBeTriviallyDeadImpl(Operation *rootOp) { - // The set of operations to consider when checking for side effects. - SmallVector effectingOps(1, rootOp); + // The set of operation intervals (end-exclusive) to consider when checking + // for side effects. + SmallVector, 1> effectingOps = { + std::make_pair(Block::iterator(rootOp), ++Block::iterator(rootOp))}; while (!effectingOps.empty()) { - Operation *op = effectingOps.pop_back_val(); + Block::iterator &it = effectingOps.back().first; + Block::iterator end = effectingOps.back().second; + if (it == end) { + effectingOps.pop_back(); + continue; + } + mlir::Operation *op = &*(it++); // If the operation has recursive effects, push all of the nested operations // on to the stack to consider. @@ -53,8 +62,7 @@ static bool wouldOpBeTriviallyDeadImpl(Operation *rootOp) { if (hasRecursiveEffects) { for (Region ®ion : op->getRegions()) { for (auto &block : region) { - for (auto &nestedOp : block) - effectingOps.push_back(&nestedOp); + effectingOps.push_back(std::make_pair(block.begin(), block.end())); } } } @@ -86,10 +94,9 @@ static bool wouldOpBeTriviallyDeadImpl(Operation *rootOp) { return false; } continue; - - // Otherwise, if the op has recursive side effects we can treat the - // operation itself as having no effects. } + // Otherwise, if the op only has recursive side effects we can treat the + // operation itself as having no effects. We will visit its children next. if (hasRecursiveEffects) continue;