diff --git a/mlir/include/mlir/IR/Operation.h b/mlir/include/mlir/IR/Operation.h --- a/mlir/include/mlir/IR/Operation.h +++ b/mlir/include/mlir/IR/Operation.h @@ -185,6 +185,15 @@ /// `iterator` in the specified block. void moveBefore(Block *block, llvm::iplist::iterator iterator); + /// Unlink this operation from its current block and insert it right after + /// `existingOp` which may be in the same or another block in the same + /// function. + void moveAfter(Operation *existingOp); + + /// Unlink this operation from its current block and insert it right after + /// `iterator` in the specified block. + void moveAfter(Block *block, llvm::iplist::iterator iterator); + /// Given an operation 'other' that is within the same parent block, return /// whether the current operation is before 'other' in the operation list /// of the parent block. diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp --- a/mlir/lib/IR/Operation.cpp +++ b/mlir/lib/IR/Operation.cpp @@ -492,6 +492,20 @@ getIterator()); } +/// Unlink this operation from its current block and insert it right after +/// `existingOp` which may be in the same or another block in the same function. +void Operation::moveAfter(Operation *existingOp) { + moveAfter(existingOp->getBlock(), existingOp->getIterator()); +} + +/// Unlink this operation from its current block and insert it right after +/// `iterator` in the specified block. +void Operation::moveAfter(Block *block, + llvm::iplist::iterator iterator) { + assert(iterator != block->end() && "cannot move after end of block"); + moveBefore(&*std::next(iterator)); +} + /// This drops all operand uses from this operation, which is an essential /// step in breaking cyclic dependences between references when they are to /// be deleted.