[llvm] [mlir] [mlir][core] Add an MLIR "pattern catalog" generator (PR #146228)

Jeremy Kun via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 28 20:29:40 PDT 2025


================
@@ -206,14 +227,38 @@ 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);
 
+#ifdef MLIR_ENABLE_CATALOG_GENERATOR
+            OpBuilder::Listener *oldListener = rewriter.getListener();
+            int status;
+            const char *mangledPatternName = typeid(*pattern).name();
+            char *demangled = abi::__cxa_demangle(mangledPatternName, nullptr,
+                                                  nullptr, &status);
+            std::string demangledPatternName;
+            if (status == 0 && demangled) {
+              demangledPatternName = demangled;
+              free(demangled);
+            } else {
+              // Fallback in case demangling fails.
+              demangledPatternName = mangledPatternName;
+            }
----------------
j2kun wrote:

Some of the other (minor) differences I see:

- cxx demangle includes default template attributes (e.g., mlir::AttrConvertPassThrough in VectorConvertToLLVMPattern) while debugName omits them.
- debugName gives the name of an enum, while cxx demangle only has the ordinal (e.g., `mlir::ReshapeOpKind::kCollapse` vs `(mlir::ReshapeOpKind)1`)
- debugName properly demangles the instances mentioned in the PR description that fail under cxx demangle.

Overall debugName seems better, so the main thing I want to figure out is how to get it to handle those canonicalize methods. Maybe the solution is simply to rewrite those canonicalize methods as proper patterns?

https://github.com/llvm/llvm-project/pull/146228


More information about the llvm-commits mailing list