@@ -896,7 +896,7 @@ struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
896896 bool wasOpReplaced (Operation *op) const ;
897897
898898 // ===--------------------------------------------------------------------===//
899- // Type Conversion
899+ // IR Rewrites / Type Conversion
900900 // ===--------------------------------------------------------------------===//
901901
902902 // / Convert the types of block arguments within the given region.
@@ -916,6 +916,22 @@ struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
916916 const TypeConverter *converter,
917917 TypeConverter::SignatureConversion &signatureConversion);
918918
919+ // / Replace the results of the given operation with the given values and
920+ // / erase the operation.
921+ // /
922+ // / There can be multiple replacement values for each result (1:N
923+ // / replacement). If the replacement values are empty, the respective result
924+ // / is dropped and a source materialization is built if the result still has
925+ // / uses.
926+ void replaceOp (Operation *op, SmallVector<SmallVector<Value>> &&newValues);
927+
928+ // / Erase the given block and its contents.
929+ void eraseBlock (Block *block);
930+
931+ // / Inline the source block into the destination block before the given
932+ // / iterator.
933+ void inlineBlockBefore (Block *source, Block *dest, Block::iterator before);
934+
919935 // ===--------------------------------------------------------------------===//
920936 // Materializations
921937 // ===--------------------------------------------------------------------===//
@@ -952,21 +968,10 @@ struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
952968 void notifyOperationInserted (Operation *op,
953969 OpBuilder::InsertPoint previous) override ;
954970
955- // / Notifies that an op is about to be replaced with the given values.
956- void notifyOpReplaced (Operation *op,
957- SmallVector<SmallVector<Value>> &&newValues);
958-
959- // / Notifies that a block is about to be erased.
960- void notifyBlockIsBeingErased (Block *block);
961-
962971 // / Notifies that a block was inserted.
963972 void notifyBlockInserted (Block *block, Region *previous,
964973 Region::iterator previousIt) override ;
965974
966- // / Notifies that a block is being inlined into another block.
967- void notifyBlockBeingInlined (Block *block, Block *srcBlock,
968- Block::iterator before);
969-
970975 // / Notifies that a pattern match failed for the given reason.
971976 void
972977 notifyMatchFailure (Location loc,
@@ -1548,7 +1553,7 @@ void ConversionPatternRewriterImpl::notifyOperationInserted(
15481553 appendRewrite<MoveOperationRewrite>(op, previous.getBlock (), prevOp);
15491554}
15501555
1551- void ConversionPatternRewriterImpl::notifyOpReplaced (
1556+ void ConversionPatternRewriterImpl::replaceOp (
15521557 Operation *op, SmallVector<SmallVector<Value>> &&newValues) {
15531558 assert (newValues.size () == op->getNumResults ());
15541559 assert (!ignoredOps.contains (op) && " operation was already replaced" );
@@ -1599,8 +1604,14 @@ void ConversionPatternRewriterImpl::notifyOpReplaced(
15991604 op->walk ([&](Operation *op) { replacedOps.insert (op); });
16001605}
16011606
1602- void ConversionPatternRewriterImpl::notifyBlockIsBeingErased (Block *block) {
1607+ void ConversionPatternRewriterImpl::eraseBlock (Block *block) {
16031608 appendRewrite<EraseBlockRewrite>(block);
1609+
1610+ // Unlink the block from its parent region. The block is kept in the rewrite
1611+ // object and will be actually destroyed when rewrites are applied. This
1612+ // allows us to keep the operations in the block live and undo the removal by
1613+ // re-inserting the block.
1614+ block->getParent ()->getBlocks ().remove (block);
16041615}
16051616
16061617void ConversionPatternRewriterImpl::notifyBlockInserted (
@@ -1628,9 +1639,10 @@ void ConversionPatternRewriterImpl::notifyBlockInserted(
16281639 appendRewrite<MoveBlockRewrite>(block, previous, prevBlock);
16291640}
16301641
1631- void ConversionPatternRewriterImpl::notifyBlockBeingInlined (
1632- Block *block, Block *srcBlock, Block::iterator before) {
1633- appendRewrite<InlineBlockRewrite>(block, srcBlock, before);
1642+ void ConversionPatternRewriterImpl::inlineBlockBefore (Block *source,
1643+ Block *dest,
1644+ Block::iterator before) {
1645+ appendRewrite<InlineBlockRewrite>(dest, source, before);
16341646}
16351647
16361648void ConversionPatternRewriterImpl::notifyMatchFailure (
@@ -1673,7 +1685,7 @@ void ConversionPatternRewriter::replaceOp(Operation *op, ValueRange newValues) {
16731685 llvm::map_to_vector (newValues, [](Value v) -> SmallVector<Value> {
16741686 return v ? SmallVector<Value>{v} : SmallVector<Value>();
16751687 });
1676- impl->notifyOpReplaced (op, std::move (newVals));
1688+ impl->replaceOp (op, std::move (newVals));
16771689}
16781690
16791691void ConversionPatternRewriter::replaceOpWithMultiple (
@@ -1684,7 +1696,7 @@ void ConversionPatternRewriter::replaceOpWithMultiple(
16841696 impl->logger .startLine ()
16851697 << " ** Replace : '" << op->getName () << " '(" << op << " )\n " ;
16861698 });
1687- impl->notifyOpReplaced (op, std::move (newValues));
1699+ impl->replaceOp (op, std::move (newValues));
16881700}
16891701
16901702void ConversionPatternRewriter::eraseOp (Operation *op) {
@@ -1693,7 +1705,7 @@ void ConversionPatternRewriter::eraseOp(Operation *op) {
16931705 << " ** Erase : '" << op->getName () << " '(" << op << " )\n " ;
16941706 });
16951707 SmallVector<SmallVector<Value>> nullRepls (op->getNumResults (), {});
1696- impl->notifyOpReplaced (op, std::move (nullRepls));
1708+ impl->replaceOp (op, std::move (nullRepls));
16971709}
16981710
16991711void ConversionPatternRewriter::eraseBlock (Block *block) {
@@ -1704,12 +1716,7 @@ void ConversionPatternRewriter::eraseBlock(Block *block) {
17041716 for (Operation &op : *block)
17051717 eraseOp (&op);
17061718
1707- // Unlink the block from its parent region. The block is kept in the rewrite
1708- // object and will be actually destroyed when rewrites are applied. This
1709- // allows us to keep the operations in the block live and undo the removal by
1710- // re-inserting the block.
1711- impl->notifyBlockIsBeingErased (block);
1712- block->getParent ()->getBlocks ().remove (block);
1719+ impl->eraseBlock (block);
17131720}
17141721
17151722Block *ConversionPatternRewriter::applySignatureConversion (
@@ -1797,7 +1804,7 @@ void ConversionPatternRewriter::inlineBlockBefore(Block *source, Block *dest,
17971804 bool fastPath = !impl->config .listener ;
17981805
17991806 if (fastPath)
1800- impl->notifyBlockBeingInlined (dest, source , before);
1807+ impl->inlineBlockBefore (source, dest , before);
18011808
18021809 // Replace all uses of block arguments.
18031810 for (auto it : llvm::zip (source->getArguments (), argValues))
0 commit comments