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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Feb 1 03:46:54 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir-core

@llvm/pr-subscribers-mlir

Author: Matthias Springer (matthias-springer)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/80278.diff


2 Files Affected:

- (modified) mlir/include/mlir/IR/Builders.h (+1-1) 
- (modified) mlir/lib/IR/Builders.cpp (+4-4) 


``````````diff
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;
 }
 

``````````

</details>


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


More information about the Mlir-commits mailing list