12#include "llvm/ADT/SmallPtrSet.h"
22#include "mlir/Interfaces/SideEffectInterfaces.cpp.inc"
29 return isa<Allocate, Free, Read, Write>(effect);
51 if (
auto effectInterface = dyn_cast<MemoryEffectOpInterface>(op)) {
53 effectInterface.getEffects(effects);
58 if (isa<MemoryEffects::Allocate>(effect.getEffect()) &&
59 effect.getValue() && effect.getValue().getDefiningOp() == op)
60 allocResults.insert(effect.getValue());
61 return llvm::all_of(effects,
66 return allocResults.contains(effect.
getValue()) ||
67 isa<MemoryEffects::Read>(effect.
getEffect());
70 return hasRecursiveEffects;
94 while (!worklist.empty()) {
95 WorkItem &item = worklist.back();
96 if (item.current == item.end) {
97 if (cache && item.complete)
98 (*cache)[item.complete] = State::Dead;
103 bool cachedAlive =
false;
105 auto cached = cache->find(op);
106 if (cached != cache->end()) {
107 if (cached->second == State::Dead)
118 (*cache)[ancestor] = State::Alive;
119 if (ancestor == root)
127 (*cache)[op] = State::Dead;
132 Operation *complete = cache ? op :
nullptr;
134 for (
Block &block : region) {
135 worklist.push_back({block.begin(), block.end(), complete});
140 (*cache)[complete] = State::Dead;
150 isa<SymbolOpInterface>(root))
153 if (!pending.empty())
154 flushInvalidations();
155 auto cachedRoot = cache.find(root);
156 if (cachedRoot != cache.end())
162void DeadnessCache::enqueue(Target
target, Request request) {
166 if (it->second == Request::Cancelled) {
169 it->second = request;
170 }
else if (request == Request::Subtree) {
171 it->second = Request::Subtree;
175void DeadnessCache::cancel(Target
target) {
176 auto it = pending.find(
target);
177 if (it == pending.end() || it->second == Request::Cancelled)
179 it->second = Request::Cancelled;
181 if (numCancelled >= pending.size() - numCancelled)
185void DeadnessCache::removeCancelled() {
189 [](
const auto &entry) {
return entry.second == Request::Cancelled; });
203 enqueue(op, Request::Subtree);
210 enqueue(op, Request::Ancestors);
216 enqueue(block, Request::Subtree);
234void DeadnessCache::flushInvalidations() {
239 for (
size_t i = 0; i < pending.size(); ++i) {
240 Target
target = pending.getArrayRef()[i].first;
241 if (
auto *block = dyn_cast<Block *>(
target)) {
243 enqueue(&op, Request::Subtree);
251 scheduleAncestorInvalidation(parent);
255 for (
auto [
target, request] : pending) {
256 Operation *root = dyn_cast<Operation *>(
target);
257 if (!root || request != Request::Subtree)
260 if (op != root && pending.lookup(op) == Request::Subtree)
269template <
typename EffectTy>
271 auto memOp = dyn_cast<MemoryEffectOpInterface>(op);
275 memOp.getEffects(effects);
276 bool hasSingleEffectOnVal =
false;
279 for (
auto &effect : effects) {
280 hasSingleEffectOnVal = isa<EffectTy>(effect.getEffect());
281 if (!hasSingleEffectOnVal)
284 return hasSingleEffectOnVal;
291template <
typename EffectTy>
293 auto memOp = dyn_cast<MemoryEffectOpInterface>(op);
297 memOp.getEffects(effects);
298 bool hasSingleEffectOnVal =
false;
301 for (
auto &effect : effects) {
302 if (effect.getValue() != value)
304 hasSingleEffectOnVal = isa<EffectTy>(effect.getEffect());
305 if (!hasSingleEffectOnVal)
308 return hasSingleEffectOnVal;
320template <
typename ValueTy,
typename EffectTy>
322 auto memOp = dyn_cast<MemoryEffectOpInterface>(op);
326 memOp.getEffects(effects);
327 bool hasSingleEffectOnVal =
false;
330 for (
auto &effect : effects) {
331 if (effect.getEffectValue<ValueTy>() != value)
333 hasSingleEffectOnVal = isa<EffectTy>(effect.getEffect());
334 if (!hasSingleEffectOnVal)
337 return hasSingleEffectOnVal;
373template <
typename... EffectTys>
375 auto memOp = dyn_cast<MemoryEffectOpInterface>(op);
379 memOp.getEffects(effects);
381 return isa<EffectTys...>(effect.
getEffect());
391template <
typename... EffectTys>
393 auto memOp = dyn_cast<MemoryEffectOpInterface>(op);
397 memOp.getEffects(effects);
401 return isa<EffectTys...>(effect.
getEffect());
413template <
typename ValueTy,
typename... EffectTys>
415 auto memOp = dyn_cast<MemoryEffectOpInterface>(op);
419 memOp.getEffects(effects);
423 return isa<EffectTys...>(effect.
getEffect());
465 return !isa<MemoryEffectOpInterface>(op) &&
472 if (isa<SymbolOpInterface>(op))
478 if (
auto memInterface = dyn_cast<MemoryEffectOpInterface>(op)) {
479 if (!memInterface.hasNoEffect())
502std::optional<llvm::SmallVector<MemoryEffects::EffectInstance>>
506 while (!effectingOps.empty()) {
507 Operation *op = effectingOps.pop_back_val();
511 bool hasRecursiveEffects =
513 if (hasRecursiveEffects) {
515 for (
Block &block : region) {
517 effectingOps.push_back(&nestedOp);
523 if (
auto effectInterface = dyn_cast<MemoryEffectOpInterface>(op)) {
524 effectInterface.getEffects(effects);
525 }
else if (!hasRecursiveEffects) {
535 auto conditionallySpeculatable = dyn_cast<ConditionallySpeculatable>(op);
536 if (!conditionallySpeculatable)
539 switch (conditionallySpeculatable.getSpeculatability()) {
555 llvm_unreachable(
"Unhandled enum in mlir::isSpeculatable!");
*if copies could not be generated due to yet unimplemented cases *copyInPlacementStart and copyOutPlacementStart in copyPlacementBlock *specify the insertion points where the incoming copies and outgoing should be inserted(the insertion happens right before the *insertion point). Since `begin` can itself be invalidated due to the memref *rewriting done from this method
static bool hasOnlyRemovableEffects(Operation *op, bool hasRecursiveEffects)
Return true if the op's own memory effects do not prevent erasure.
static bool wouldOpBeTriviallyDeadImpl(Operation *root, DenseMap< Operation *, DeadnessCache::State > *cache=nullptr)
Internal implementation of mlir::wouldOpBeTriviallyDead that also considers terminator operations as ...
This class represents an argument of a Block.
Block represents an ordered list of Operations.
OpListType::iterator iterator
Operation * getParentOp()
Returns the closest surrounding operation that contains this block.
void scheduleBlockInvalidation(Block *block)
Report a change to a block and its contents.
bool isOpTriviallyDead(Operation *op)
Return true if the operation is unused and can be erased.
void scheduleAncestorInvalidation(Operation *op)
Invalidate the cached effects of this operation and its ancestors.
void notifyOperationErased(Operation *op)
Call once for each erased operation before it is detached or destroyed.
void scheduleInvalidation(Operation *op)
Report a change to this operation or its nested IR.
void notifyBlockErased(Block *block)
Call before an empty block is detached and destroyed.
This class represents an operand of an operation.
This is a value defined by a result of an operation.
This trait indicates that the memory effects of an operation includes the effects of operations neste...
This class provides the API for ops that are known to be terminators.
Operation is the basic unit of execution within MLIR.
bool use_empty()
Returns true if this operation has no uses.
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
bool mightHaveTrait()
Returns true if the operation might have the provided trait.
unsigned getNumRegions()
Returns the number of regions held by this operation.
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
std::enable_if_t< llvm::function_traits< std::decay_t< FnT > >::num_args==1, RetT > walk(FnT &&callback)
Walk the operation by calling the callback for each nested operation (including this one),...
This class contains a list of basic blocks and a link to the parent operation it is attached to.
EffectT * getEffect() const
Return the effect being applied.
T getEffectValue() const
Returns the OpOperand effect is applied on, or nullptr if there isn't a known value being effected.
Value getValue() const
Return the value the effect is applied on, or nullptr if there isn't a known value being affected.
This class represents a base class for a specific effect type.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
static WalkResult advance()
SideEffects::EffectInstance< Effect > EffectInstance
constexpr auto RecursivelySpeculatable
constexpr auto Speculatable
constexpr auto NotSpeculatable
Include the generated interface declarations.
bool hasSingleEffect(Operation *op)
Returns "true" if op has only an effect of type EffectTy.
bool isPure(Operation *op)
Returns true if the given operation is pure, i.e., is speculatable that does not touch memory.
bool isMemoryEffectFree(Operation *op)
Returns true if the given operation is free of memory effects.
bool wouldOpBeTriviallyDead(Operation *op)
Return true if the given operation would be dead if unused, and has no side effects on memory that wo...
bool isSpeculatable(Operation *op)
Returns true if the given operation is speculatable, i.e.
bool isOpTriviallyDead(Operation *op)
Return true if the given operation is unused, and has no side effects on memory that prevent erasing.
std::optional< llvm::SmallVector< MemoryEffects::EffectInstance > > getEffectsRecursively(Operation *rootOp)
Returns the side effects of an operation.
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
bool hasEffect(Operation *op)
Returns "true" if op has an effect of type EffectTy.
bool hasUnknownEffects(Operation *op)
Return "true" if op has unknown effects.
static bool classof(const SideEffects::Effect *effect)
Include the definitions of the side effect interfaces.