[llvm-branch-commits] [mlir] [mlir][IR] Add listener notifications for pattern begin/end (PR #84131)

Mehdi Amini via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Mar 7 23:24:43 PST 2024


================
@@ -572,20 +571,33 @@ bool GreedyPatternRewriteDriver::processWorklist() {
         logger.getOStream() << ")' {\n";
         logger.indent();
       });
+      if (config.listener)
+        config.listener->notifyPatternBegin(pattern, op);
       return true;
     };
-    auto onFailure = [&](const Pattern &pattern) {
-      LLVM_DEBUG(logResult("failure", "pattern failed to match"));
-    };
-    auto onSuccess = [&](const Pattern &pattern) {
-      LLVM_DEBUG(logResult("success", "pattern applied successfully"));
-      return success();
-    };
-#else
-    function_ref<bool(const Pattern &)> canApply = {};
-    function_ref<void(const Pattern &)> onFailure = {};
-    function_ref<LogicalResult(const Pattern &)> onSuccess = {};
-#endif
+    function_ref<void(const Pattern &)> onFailure =
+        [&](const Pattern &pattern) {
+          LLVM_DEBUG(logResult("failure", "pattern failed to match"));
+          if (config.listener)
+            config.listener->notifyPatternEnd(pattern, failure());
+        };
+    function_ref<LogicalResult(const Pattern &)> onSuccess =
+        [&](const Pattern &pattern) {
+          LLVM_DEBUG(logResult("success", "pattern applied successfully"));
+          if (config.listener)
+            config.listener->notifyPatternEnd(pattern, success());
+          return success();
+        };
+
+#ifdef NDEBUG
+    // Optimization: PatternApplicator callbacks are not needed when running in
+    // optimized mode and without a listener.
+    if (!config.listener) {
+      canApply = nullptr;
+      onFailure = nullptr;
+      onSuccess = nullptr;
+    }
+#endif // NDEBUG
----------------
joker-eph wrote:

Note: I didn't suggest changing this, what you had here was reasonable!

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


More information about the llvm-branch-commits mailing list