[Mlir-commits] [flang] [mlir] [mlir][IR] Add rewriter API for moving operations (PR #78988)

Matthias Springer llvmlistbot at llvm.org
Tue Jan 23 00:07:13 PST 2024


================
@@ -366,3 +366,31 @@ void RewriterBase::cloneRegionBefore(Region &region, Region &parent,
 void RewriterBase::cloneRegionBefore(Region &region, Block *before) {
   cloneRegionBefore(region, *before->getParent(), before->getIterator());
 }
+
+void RewriterBase::moveOpBefore(Operation *op, Operation *existingOp) {
+  moveOpBefore(op, existingOp->getBlock(), existingOp->getIterator());
+}
+
+void RewriterBase::moveOpBefore(Operation *op, Block *block,
+                                Block::iterator iterator) {
+  Block *currentBlock = op->getBlock();
+  Block::iterator currentIterator = op->getIterator();
+  op->moveBefore(block, iterator);
+  if (listener)
+    listener->notifyOperationInserted(
+        op, /*previous=*/InsertPoint(currentBlock, currentIterator));
----------------
matthias-springer wrote:

Are you referring to the fact that "moving ops" is not supported by the dialect conversion? The `notifyOperationInserted` callback is triggered after moving the op. The current location can be queried from the op itself. The previous location is passed as a parameter. That should be enough information to implement the rollback mechanism in the future.

"Moving an op" is a form of "inserting an op". Until now, we used `notifyOperationInserted` only for newly created ops. But in both cases we are inserting an op, only the previous location is different (moving an op: has a previous location, inserting a newly created op: was previously unlinked). (The callback is called `notifyOperationInserted`, not `notifyOperationCreated`.)


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


More information about the Mlir-commits mailing list