21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorHandling.h"
24 #define DEBUG_TYPE "walk-rewriter"
29 struct WalkAndApplyPatternsAction final
30 : tracing::ActionImpl<WalkAndApplyPatternsAction> {
32 using ActionImpl::ActionImpl;
33 static constexpr StringLiteral tag =
"walk-and-apply-patterns";
34 void print(raw_ostream &os)
const override { os << tag; }
37 #if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
42 struct ErasedOpsListener final : RewriterBase::ForwardingListener {
45 void notifyOperationErased(Operation *op)
override {
47 ForwardingListener::notifyOperationErased(op);
50 void notifyBlockErased(Block *block)
override {
51 checkErasure(block->getParentOp());
52 ForwardingListener::notifyBlockErased(block);
55 void checkErasure(Operation *op)
const {
56 Operation *ancestorOp = op;
57 while (ancestorOp && ancestorOp != visitedOp)
58 ancestorOp = ancestorOp->getParentOp();
60 if (ancestorOp != visitedOp)
61 llvm::report_fatal_error(
62 "unsupported erasure in WalkPatternRewriter; "
63 "erasure is only supported for matched ops and their descendants");
66 Operation *visitedOp =
nullptr;
74 #if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
76 llvm::report_fatal_error(
"walk pattern rewriter input IR failed to verify");
81 #if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
82 ErasedOpsListener erasedListener(listener);
95 LLVM_DEBUG(llvm::dbgs() <<
"Visiting op: "; visitedOp->
print(
97 llvm::dbgs() <<
"\n";);
98 #if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
99 erasedListener.visitedOp = visitedOp;
102 LLVM_DEBUG(llvm::dbgs() <<
"\tOp matched and rewritten\n";);
109 #if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
111 llvm::report_fatal_error(
112 "walk pattern rewriter result IR failed to verify");
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
#define MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(CLASS_NAME)
This class represents a frozen set of patterns that can be processed by a pattern applicator.
MLIRContext is the top-level object for a collection of MLIR operations.
void executeAction(function_ref< void()> actionFn, const tracing::Action &action)
Dispatch the provided action to the handler if any, or just execute it.
void setListener(Listener *newListener)
Sets the listener of this builder to the one provided.
Set of flags used to control the behavior of the various IR print methods (e.g.
Operation is the basic unit of execution within MLIR.
void print(raw_ostream &os, const OpPrintingFlags &flags=std::nullopt)
MLIRContext * getContext()
Return the context this operation is associated with.
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
This class manages the application of a group of rewrite patterns, with a user-provided cost model.
LogicalResult matchAndRewrite(Operation *op, PatternRewriter &rewriter, function_ref< bool(const Pattern &)> canApply={}, function_ref< void(const Pattern &)> onFailure={}, function_ref< LogicalResult(const Pattern &)> onSuccess={})
Attempt to match and rewrite the given op with any pattern, allowing a predicate to decide if a patte...
void applyDefaultCostModel()
Apply the default cost model that solely uses the pattern's static benefit.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Include the generated interface declarations.
void walkAndApplyPatterns(Operation *op, const FrozenRewritePatternSet &patterns, RewriterBase::Listener *listener=nullptr)
A fast walk-based pattern rewrite driver.
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...
ForwardingListener(OpBuilder::Listener *listener)