diff --git a/mlir/include/mlir/IR/Builders.h b/mlir/include/mlir/IR/Builders.h --- a/mlir/include/mlir/IR/Builders.h +++ b/mlir/include/mlir/IR/Builders.h @@ -228,6 +228,8 @@ setInsertionPoint(block, insertPoint); } + virtual ~OpBuilder(); + /// Create a builder and set the insertion point to before the first operation /// in the block but still inside the block. static OpBuilder atBlockBegin(Block *block, Listener *listener = nullptr) { @@ -421,6 +423,16 @@ Block *createBlock(Block *insertBefore, TypeRange argTypes = std::nullopt, ArrayRef locs = std::nullopt); + /// Clone the blocks that belong to "region" before the given position in + /// another region "parent". The two regions must be different. The caller is + /// responsible for creating or updating the operation transferring flow of + /// control to the region and passing it the correct block arguments. + virtual void cloneRegionBefore(Region ®ion, Region &parent, + Region::iterator before, IRMapping &mapping); + void cloneRegionBefore(Region ®ion, Region &parent, + Region::iterator before); + void cloneRegionBefore(Region ®ion, Block *before); + //===--------------------------------------------------------------------===// // Operation Creation //===--------------------------------------------------------------------===// diff --git a/mlir/include/mlir/IR/PatternMatch.h b/mlir/include/mlir/IR/PatternMatch.h --- a/mlir/include/mlir/IR/PatternMatch.h +++ b/mlir/include/mlir/IR/PatternMatch.h @@ -406,16 +406,6 @@ Region::iterator before); void inlineRegionBefore(Region ®ion, Block *before); - /// Clone the blocks that belong to "region" before the given position in - /// another region "parent". The two regions must be different. The caller is - /// responsible for creating or updating the operation transferring flow of - /// control to the region and passing it the correct block arguments. - virtual void cloneRegionBefore(Region ®ion, Region &parent, - Region::iterator before, IRMapping &mapping); - void cloneRegionBefore(Region ®ion, Region &parent, - Region::iterator before); - void cloneRegionBefore(Region ®ion, Block *before); - /// This method replaces the uses of the results of `op` with the values in /// `newValues` when the provided `functor` returns true for a specific use. /// The number of values in `newValues` is required to match the number of diff --git a/mlir/lib/IR/Builders.cpp b/mlir/lib/IR/Builders.cpp --- a/mlir/lib/IR/Builders.cpp +++ b/mlir/lib/IR/Builders.cpp @@ -390,6 +390,8 @@ OpBuilder::Listener::~Listener() = default; +OpBuilder::~OpBuilder() = default; + /// Insert the given operation at the current insertion point and return it. Operation *OpBuilder::insert(Operation *op) { if (block) @@ -535,3 +537,24 @@ IRMapping mapper; return clone(op, mapper); } + +void OpBuilder::cloneRegionBefore(Region ®ion, Region &parent, + Region::iterator before, IRMapping &mapping) { + region.cloneInto(&parent, before, mapping); + if (listener) { + Region::iterator it = mapping.lookup(®ion.front())->getIterator(); + while (it != before) { + // TODO: Notify listener of nested blocks. + it->walk([&](Operation *op) { listener->notifyOperationInserted(op); }); + it++; + } + } +} +void OpBuilder::cloneRegionBefore(Region ®ion, Region &parent, + Region::iterator before) { + IRMapping mapping; + cloneRegionBefore(region, parent, before, mapping); +} +void OpBuilder::cloneRegionBefore(Region ®ion, Block *before) { + cloneRegionBefore(region, *before->getParent(), before->getIterator()); +} diff --git a/mlir/lib/IR/PatternMatch.cpp b/mlir/lib/IR/PatternMatch.cpp --- a/mlir/lib/IR/PatternMatch.cpp +++ b/mlir/lib/IR/PatternMatch.cpp @@ -371,29 +371,3 @@ void RewriterBase::inlineRegionBefore(Region ®ion, Block *before) { inlineRegionBefore(region, *before->getParent(), before->getIterator()); } - -/// Clone the blocks that belong to "region" before the given position in -/// another region "parent". The two regions must be different. The caller is -/// responsible for creating or updating the operation transferring flow of -/// control to the region and passing it the correct block arguments. -void RewriterBase::cloneRegionBefore(Region ®ion, Region &parent, - Region::iterator before, - IRMapping &mapping) { - region.cloneInto(&parent, before, mapping); - if (Listener *listener = getListener()) { - Region::iterator it = mapping.lookup(®ion.front())->getIterator(); - while (it != before) { - // TODO: Notify listener of nested blocks. - it->walk([&](Operation *op) { listener->notifyOperationInserted(op); }); - it++; - } - } -} -void RewriterBase::cloneRegionBefore(Region ®ion, Region &parent, - Region::iterator before) { - IRMapping mapping; - cloneRegionBefore(region, parent, before, mapping); -} -void RewriterBase::cloneRegionBefore(Region ®ion, Block *before) { - cloneRegionBefore(region, *before->getParent(), before->getIterator()); -} diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp b/mlir/lib/Transforms/Utils/DialectConversion.cpp --- a/mlir/lib/Transforms/Utils/DialectConversion.cpp +++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp @@ -1625,9 +1625,9 @@ Region &parent, Region::iterator before, IRMapping &mapping) { - // Note: Do not call PatternRewriter::cloneRegionBefore. We cannot rely on its - // "op created" notifications because we need to populate the list of created - // ops in a certain order. This is done in `notifyRegionWasClonedBefore`. + // Note: Do not call OpBuilder::cloneRegionBefore. We cannot rely on its "op + // created" notifications because we need to populate the list of created ops + // in a certain order. This is done in `notifyRegionWasClonedBefore`. if (region.empty()) return;