Skip to content

[mlir][core] Add an MLIR "pattern catalog" generator #146228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 17, 2025
19 changes: 19 additions & 0 deletions mlir/include/mlir/IR/PatternMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,25 @@ class RewriterBase : public OpBuilder {
RewriterBase::Listener *rewriteListener;
};

/// A listener that logs notification events to llvm::dbgs() before
/// forwarding to the base listener.
struct PatternLoggingListener : public RewriterBase::ForwardingListener {
PatternLoggingListener(OpBuilder::Listener *listener, StringRef patternName)
: RewriterBase::ForwardingListener(listener), patternName(patternName) {
}

void notifyOperationInserted(Operation *op, InsertPoint previous) override;
void notifyOperationModified(Operation *op) override;
void notifyOperationReplaced(Operation *op, Operation *newOp) override;
void notifyOperationReplaced(Operation *op,
ValueRange replacement) override;
void notifyOperationErased(Operation *op) override;
void notifyPatternBegin(const Pattern &pattern, Operation *op) override;

private:
StringRef patternName;
};

/// Move the blocks that belong to "region" before the given position in
/// another region "parent". The two regions must be different. The caller
/// is responsible for creating or updating the operation transferring flow
Expand Down
1 change: 1 addition & 0 deletions mlir/lib/IR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_mlir_library(MLIRIR
ODSSupport.cpp
Operation.cpp
OperationSupport.cpp
PatternLoggingListener.cpp
PatternMatch.cpp
Region.cpp
RegionKindInterface.cpp
Expand Down
50 changes: 50 additions & 0 deletions mlir/lib/IR/PatternLoggingListener.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "mlir/IR/PatternMatch.h"
#include "llvm/Support/Debug.h"

#define DEBUG_TYPE "pattern-logging-listener"
#define DBGS() (llvm::dbgs() << "[" << DEBUG_TYPE << "] ")
#define LDBG(X) LLVM_DEBUG(DBGS() << X << "\n")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, funnily was working on a general utility that does such logging as I ended up writing it often :)


using namespace mlir;

void RewriterBase::PatternLoggingListener::notifyOperationInserted(
Operation *op, InsertPoint previous) {
LDBG(patternName << " | notifyOperationInserted"
<< " | " << op->getName());
ForwardingListener::notifyOperationInserted(op, previous);
}

void RewriterBase::PatternLoggingListener::notifyOperationModified(
Operation *op) {
LDBG(patternName << " | notifyOperationModified"
<< " | " << op->getName());
ForwardingListener::notifyOperationModified(op);
}

void RewriterBase::PatternLoggingListener::notifyOperationReplaced(
Operation *op, Operation *newOp) {
LDBG(patternName << " | notifyOperationReplaced (with op)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OOC why does with op vs value matter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping originally to be able to query for a source and target op, but then I found that almost all upstream patterns replace an op with values rather than calling replaceOpWithNewOp (even when replaceOp is given an operation, it is cast to the results and the notification hook is treated as replacing with values).

I'm not sure what to do with this in the end: keep it and expose it in the search, or just treat "operation replaced" as if it is "operation erased"

<< " | " << op->getName() << " | " << newOp->getName());
ForwardingListener::notifyOperationReplaced(op, newOp);
}

void RewriterBase::PatternLoggingListener::notifyOperationReplaced(
Operation *op, ValueRange replacement) {
LDBG(patternName << " | notifyOperationReplaced (with values)"
<< " | " << op->getName());
ForwardingListener::notifyOperationReplaced(op, replacement);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So all of these may end up with duplicate notifications (e.g., a pattern could insert the same op 10 times and this would report 10 times), and so that would be handled by post-processing? (I think also conditional emission, would just be squashed right, so would end up with union of all that pattern can do)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the postprocessing includes sort | uniq, and I don't have any immediate plans to support a more structured sort of query.

}

void RewriterBase::PatternLoggingListener::notifyOperationErased(
Operation *op) {
LDBG(patternName << " | notifyOperationErased"
<< " | " << op->getName());
ForwardingListener::notifyOperationErased(op);
}

void RewriterBase::PatternLoggingListener::notifyPatternBegin(
const Pattern &pattern, Operation *op) {
LDBG(patternName << " | notifyPatternBegin"
<< " | " << op->getName());
ForwardingListener::notifyPatternBegin(pattern, op);
}
16 changes: 14 additions & 2 deletions mlir/lib/Rewrite/PatternApplicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#include "ByteCode.h"
#include "llvm/Support/Debug.h"

#ifndef NDEBUG
#include "llvm/ADT/ScopeExit.h"
#endif

#define DEBUG_TYPE "pattern-application"

using namespace mlir;
Expand Down Expand Up @@ -206,11 +210,19 @@ LogicalResult PatternApplicator::matchAndRewrite(
} else {
LLVM_DEBUG(llvm::dbgs() << "Trying to match \""
<< bestPattern->getDebugName() << "\"\n");

const auto *pattern =
static_cast<const RewritePattern *>(bestPattern);
result = pattern->matchAndRewrite(op, rewriter);

#ifndef NDEBUG
OpBuilder::Listener *oldListener = rewriter.getListener();
auto loggingListener =
std::make_unique<RewriterBase::PatternLoggingListener>(
oldListener, pattern->getDebugName());
rewriter.setListener(loggingListener.get());
auto resetListenerCallback = llvm::make_scope_exit(
[&] { rewriter.setListener(oldListener); });
#endif
result = pattern->matchAndRewrite(op, rewriter);
LLVM_DEBUG(llvm::dbgs()
<< "\"" << bestPattern->getDebugName() << "\" result "
<< succeeded(result) << "\n");
Expand Down
17 changes: 17 additions & 0 deletions mlir/test/IR/test-pattern-logging-listener.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: mlir-opt %s --test-walk-pattern-rewrite-driver \
// RUN: --allow-unregistered-dialect --debug-only=pattern-logging-listener 2>&1 | FileCheck %s

// Check that when replacing an op with a new op, we get appropriate
// pattern-logging lines. The regex is because the anonymous namespace is
// printed differently on different platforms.

// CHECK: [pattern-logging-listener] {{.anonymous.namespace.}}::ReplaceWithNewOp | notifyOperationInserted | test.new_op
// CHECK: [pattern-logging-listener] {{.anonymous.namespace.}}::ReplaceWithNewOp | notifyOperationReplaced (with values) | test.replace_with_new_op
// CHECK: [pattern-logging-listener] {{.anonymous.namespace.}}::ReplaceWithNewOp | notifyOperationModified | arith.addi
// CHECK: [pattern-logging-listener] {{.anonymous.namespace.}}::ReplaceWithNewOp | notifyOperationModified | arith.addi
// CHECK: [pattern-logging-listener] {{.anonymous.namespace.}}::ReplaceWithNewOp | notifyOperationErased | test.replace_with_new_op
func.func @replace_with_new_op() -> i32 {
%a = "test.replace_with_new_op"() : () -> (i32)
%res = arith.addi %a, %a : i32
return %res : i32
}
11 changes: 11 additions & 0 deletions mlir/test/lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,17 @@ def find_real_python_interpreter():
ToolSubst("mlir-opt", "mlir-opt --verify-roundtrip", unresolved="fatal"),
]
)
elif "MLIR_GENERATE_PATTERN_CATALOG" in os.environ:
tools.extend(
[
ToolSubst(
"mlir-opt",
"mlir-opt --debug-only=pattern-logging-listener --mlir-disable-threading",
unresolved="fatal",
),
ToolSubst("FileCheck", "FileCheck --dump-input=always", unresolved="fatal"),
]
)
else:
tools.extend(["mlir-opt"])

Expand Down