[Mlir-commits] [mlir] [mlir][IR] Do not trigger `notifyOperationInserted` for unlinked ops (PR #80278)

Matthias Springer llvmlistbot at llvm.org
Thu Feb 1 03:46:25 PST 2024


https://github.com/matthias-springer created https://github.com/llvm/llvm-project/pull/80278

This commit changes `OpBuilder::create` and `OpBuilder::createOrFold` such that `notifyOperationInserted` is no longer triggered if no insertion point is set. In such a case, an unlinked operation is created but not inserted, so `notifyOperationInserted` should not be triggered.

Note: Inserting another op into a block that belongs to an unlinked op (e.g., by the builder of the unlinked op) will trigger a notification.

>From 0051e5ca04fe89c0b4a46a799769305c8ccb2861 Mon Sep 17 00:00:00 2001
From: Matthias Springer <springerm at google.com>
Date: Thu, 1 Feb 2024 11:44:57 +0000
Subject: [PATCH] [mlir][IR] Do not trigger `notifyOperationInserted` for
 unlinked ops

This commit changes `OpBuilder::create` and `OpBuilder::createOrFold` such that `notifyOperationInserted` is no longer triggered if no insertion point is set. In such a case, an unlinked operation is created but not inserted, so `notifyOperationInserted` should not be triggered.

Note: Inserting another op into a block that belongs to an unlinked op (e.g., by the builder of the unlinked op) will trigger a notification.
---
 mlir/include/mlir/IR/Builders.h | 2 +-
 mlir/lib/IR/Builders.cpp        | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/mlir/include/mlir/IR/Builders.h b/mlir/include/mlir/IR/Builders.h
index 8c25a1aa2fad1..4fc29c65f2e68 100644
--- a/mlir/include/mlir/IR/Builders.h
+++ b/mlir/include/mlir/IR/Builders.h
@@ -530,7 +530,7 @@ class OpBuilder : public Builder {
     // Fold the operation. If successful erase it, otherwise notify.
     if (succeeded(tryFold(op, results)))
       op->erase();
-    else if (listener)
+    else if (block && listener)
       listener->notifyOperationInserted(op, /*previous=*/{});
   }
 
diff --git a/mlir/lib/IR/Builders.cpp b/mlir/lib/IR/Builders.cpp
index 7acef1073c6de..60068620af0b3 100644
--- a/mlir/lib/IR/Builders.cpp
+++ b/mlir/lib/IR/Builders.cpp
@@ -408,11 +408,11 @@ AffineMap Builder::getShiftedAffineMap(AffineMap map, int64_t shift) {
 
 /// Insert the given operation at the current insertion point and return it.
 Operation *OpBuilder::insert(Operation *op) {
-  if (block)
+  if (block) {
     block->getOperations().insert(insertPoint, op);
-
-  if (listener)
-    listener->notifyOperationInserted(op, /*previous=*/{});
+    if (listener)
+      listener->notifyOperationInserted(op, /*previous=*/{});
+  }
   return op;
 }
 



More information about the Mlir-commits mailing list