10 #include "mlir/Config/mlir-config.h"
14 #include "llvm/ADT/SmallPtrSet.h"
23 assert(representation == benefit && benefit != ImpossibleToMatchSentinel &&
24 "This pattern match benefit is too large to represent");
29 return representation;
49 :
Pattern(nullptr, RootKind::Any, generatedNames, benefit, context) {}
57 :
Pattern(interfaceID.getAsOpaquePointer(), RootKind::InterfaceID,
58 generatedNames, benefit, context) {}
66 :
Pattern(traitID.getAsOpaquePointer(), RootKind::TraitID, generatedNames,
75 : rootValue(rootValue), rootKind(rootKind), benefit(benefit),
76 contextAndHasBoundedRecursion(context, false) {
77 if (generatedNames.empty())
79 generatedOps.reserve(generatedNames.size());
80 std::transform(generatedNames.begin(), generatedNames.end(),
81 std::back_inserter(generatedOps), [context](StringRef name) {
82 return OperationName(name, context);
91 llvm_unreachable(
"need to implement either matchAndRewrite or one of the "
92 "rewrite functions!");
96 llvm_unreachable(
"need to implement either match or matchAndRewrite!");
100 void RewritePattern::anchor() {}
116 if (
auto *rewriteListener = dyn_cast_if_present<Listener>(
listener))
117 rewriteListener->notifyOperationReplaced(from, to);
124 if (
auto *rewriteListener = dyn_cast_if_present<Listener>(
listener))
125 rewriteListener->notifyOperationReplaced(from, to);
135 "incorrect # of replacement values");
148 assert(op && newOp &&
"expected non-null op");
150 "ops have different number of results");
162 assert(op->
use_empty() &&
"expected 'op' to have no uses");
163 auto *rewriteListener = dyn_cast_if_present<Listener>(
listener);
166 if (!rewriteListener) {
172 auto eraseSingleOp = [&](
Operation *op) {
177 "expected empty regions");
182 "expected that op has no uses");
184 rewriteListener->notifyOperationErased(op);
207 for (
Block *b : llvm::post_order_ext(&r.front(), visited)) {
214 erasedBlocks.push_back(b);
216 for (
Block *b : erasedBlocks) {
234 assert(block->
use_empty() &&
"expected 'block' to have no uses");
236 for (
auto &op : llvm::make_early_inc_range(llvm::reverse(*block))) {
237 assert(op.
use_empty() &&
"expected 'op' to have no uses");
242 if (
auto *rewriteListener = dyn_cast_if_present<Listener>(
listener))
243 rewriteListener->notifyBlockErased(block);
250 if (
auto *rewriteListener = dyn_cast_if_present<Listener>(
listener))
251 rewriteListener->notifyOperationModified(op);
258 return !preservedUsers.contains(user);
264 bool *allUsesReplaced) {
265 bool allReplaced =
true;
267 bool replace = functor(operand);
270 allReplaced &= replace;
273 *allUsesReplaced = allReplaced;
278 bool *allUsesReplaced) {
279 assert(from.size() == to.size() &&
"incorrect number of replacements");
280 bool allReplaced =
true;
281 for (
auto it : llvm::zip_equal(from, to)) {
288 *allUsesReplaced = allReplaced;
295 "incorrect # of argument replacement values");
300 "expected 'source' to have no predecessors");
302 if (dest->
end() != before) {
307 "expected 'source' to have no successors");
312 assert(dest->
hasNoSuccessors() &&
"expected 'dest' to have no successors");
316 for (
auto it : llvm::zip(source->
getArguments(), argValues))
325 while (!source->
empty())
330 assert(source->
empty() &&
"expected 'source' to be empty");
357 if (before == block->
end())
362 while (before->getBlock() != newBlock)
381 while (!region.
empty())
390 anotherBlock->getIterator());
423 assert(iterator != block->
end() &&
"cannot move after end of block");
This class represents an argument of a Block.
Block represents an ordered list of Operations.
OpListType::iterator iterator
bool hasNoSuccessors()
Returns true if this blocks has no successors.
unsigned getNumArguments()
void erase()
Unlink this Block from its parent region and delete it.
Block * splitBlock(iterator splitBefore)
Split the block into two blocks before the specified operation or iterator.
Region * getParent() const
Provide a 'getParent' method for ilist_node_with_parent methods.
OpListType & getOperations()
BlockArgListType getArguments()
void moveBefore(Block *block)
Unlink this block from its current region and insert it right before the specific block.
bool hasNoPredecessors()
Return true if this block has no predecessors.
bool use_empty() const
Returns true if this value has no uses.
MLIRContext is the top-level object for a collection of MLIR operations.
This class represents a saved insertion point.
RAII guard to reset the insertion point of the builder when destroyed.
Listener * listener
The optional listener for events of this builder.
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes=std::nullopt, ArrayRef< Location > locs=std::nullopt)
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
This class represents an operand of an operation.
Operation is the basic unit of execution within MLIR.
bool use_empty()
Returns true if this operation has no uses.
void dropAllUses()
Drop all uses of results of this operation.
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
Block * getBlock()
Returns the operation block that contains this operation.
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
void moveBefore(Operation *existingOp)
Unlink this operation from its current block and insert it right before existingOp which may be in th...
Region * getParentRegion()
Returns the region to which the instruction belongs.
result_range getResults()
void erase()
Remove this operation from its parent block and delete it.
unsigned getNumResults()
Return the number of results held by this operation.
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
bool isImpossibleToMatch() const
unsigned short getBenefit() const
If the corresponding pattern can match, return its benefit. If the.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
This class contains all of the data related to a pattern, but does not contain any methods or logic f...
Pattern(StringRef rootName, PatternBenefit benefit, MLIRContext *context, ArrayRef< StringRef > generatedNames={})
Construct a pattern with a certain benefit that matches the operation with the given root name.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
BlockListType & getBlocks()
BlockListType::iterator iterator
virtual LogicalResult match(Operation *op) const
Attempt to match against code rooted at the specified operation, which is the same operation code as ...
virtual void rewrite(Operation *op, PatternRewriter &rewriter) const
Rewrite the IR rooted at the specified operation with the result of this pattern, generating any new ...
virtual void eraseBlock(Block *block)
This method erases all operations in a block.
Block * splitBlock(Block *block, Block::iterator before)
Split the operations starting at "before" (inclusive) out of the given block into a new block,...
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
void moveBlockBefore(Block *block, Block *anotherBlock)
Unlink this block and insert it right before existingBlock.
void replaceAllUsesWith(Value from, Value to)
Find uses of from and replace them with to.
void mergeBlocks(Block *source, Block *dest, ValueRange argValues=std::nullopt)
Inline the operations of block 'source' into the end of block 'dest'.
virtual void finalizeOpModification(Operation *op)
This method is used to signal the end of an in-place modification of the given operation.
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
void replaceUsesWithIf(Value from, Value to, function_ref< bool(OpOperand &)> functor, bool *allUsesReplaced=nullptr)
Find uses of from and replace them with to if the functor returns true.
void replaceAllUsesExcept(Value from, Value to, Operation *exceptedUser)
Find uses of from and replace them with to except if the user is exceptedUser.
void moveOpBefore(Operation *op, Operation *existingOp)
Unlink this operation from its current block and insert it right before existingOp which may be in th...
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
void moveOpAfter(Operation *op, Operation *existingOp)
Unlink this operation from its current block and insert it right after existingOp which may be in the...
void inlineRegionBefore(Region ®ion, Region &parent, Region::iterator before)
Move the blocks that belong to "region" before the given position in another region "parent".
virtual void inlineBlockBefore(Block *source, Block *dest, Block::iterator before, ValueRange argValues=std::nullopt)
Inline the operations of block 'source' into block 'dest' before the given position.
void replaceAllOpUsesWith(Operation *from, ValueRange to)
Find uses of from and replace them with to.
This class provides an efficient unique identifier for a specific C++ type.
This class provides an abstraction over the different types of ranges over Values.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
use_range getUses() const
Returns a range of all uses, which is useful for iterating over all uses.
Operation * getOwner() const
Return the owner of this operand.
Include the generated interface declarations.
bool mayBeGraphRegion(Region ®ion)
Return "true" if the given region may be a graph region without SSA dominance.
@ RewriterBaseListener
RewriterBase::Listener or user-derived class.
This class represents a listener that may be used to hook into various actions within an OpBuilder.
virtual void notifyBlockInserted(Block *block, Region *previous, Region::iterator previousIt)
Notify the listener that the specified block was inserted.
virtual void notifyOperationInserted(Operation *op, InsertPoint previous)
Notify the listener that the specified operation was inserted.
This class acts as a special tag that makes the desire to match "any" operation type explicit.
This class acts as a special tag that makes the desire to match any operation that implements a given...
This class acts as a special tag that makes the desire to match any operation that implements a given...
static constexpr auto makeIterable(RangeT &&range)
static bool classof(const OpBuilder::Listener *base)