diff --git a/mlir/include/mlir/IR/Operation.h b/mlir/include/mlir/IR/Operation.h index 1f4f5276dc9d..06108c60462e 100644 --- a/mlir/include/mlir/IR/Operation.h +++ b/mlir/include/mlir/IR/Operation.h @@ -120,6 +120,11 @@ public: /// Return true if there are no users of any results of this operation. bool use_empty() const; + /// Unlink this operation from its current block and insert it right before + /// `existingOp` which may be in the same or another block of the same + /// function. + void moveBefore(Operation *existingOp); + // Attributes. Operations may optionally carry a list of attributes that // associate constants to names. Attributes may be dynamically added and // removed over the lifetime of an operation. diff --git a/mlir/include/mlir/IR/Statements.h b/mlir/include/mlir/IR/Statements.h index a5f9c40f7cc7..48f9c540f07e 100644 --- a/mlir/include/mlir/IR/Statements.h +++ b/mlir/include/mlir/IR/Statements.h @@ -56,6 +56,7 @@ public: using Statement::emitNote; using Statement::emitWarning; using Statement::getLoc; + using Statement::moveBefore; using Statement::print; /// Check if this statement is a return statement. diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp index 48617e65f207..bdf38745b709 100644 --- a/mlir/lib/IR/Operation.cpp +++ b/mlir/lib/IR/Operation.cpp @@ -197,6 +197,13 @@ bool Operation::use_empty() const { return true; } +void Operation::moveBefore(Operation *existingOp) { + if (auto *inst = llvm::dyn_cast(this)) + return inst->moveBefore(llvm::cast(existingOp)); + return llvm::cast(this)->moveBefore( + llvm::cast(existingOp)); +} + ArrayRef Operation::getAttrs() const { if (!attrs) return {};