@@ -896,7 +896,7 @@ struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
896
896
bool wasOpReplaced (Operation *op) const ;
897
897
898
898
// ===--------------------------------------------------------------------===//
899
- // Type Conversion
899
+ // IR Rewrites / Type Conversion
900
900
// ===--------------------------------------------------------------------===//
901
901
902
902
// / Convert the types of block arguments within the given region.
@@ -916,6 +916,22 @@ struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
916
916
const TypeConverter *converter,
917
917
TypeConverter::SignatureConversion &signatureConversion);
918
918
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
+
919
935
// ===--------------------------------------------------------------------===//
920
936
// Materializations
921
937
// ===--------------------------------------------------------------------===//
@@ -952,21 +968,10 @@ struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
952
968
void notifyOperationInserted (Operation *op,
953
969
OpBuilder::InsertPoint previous) override ;
954
970
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
-
962
971
// / Notifies that a block was inserted.
963
972
void notifyBlockInserted (Block *block, Region *previous,
964
973
Region::iterator previousIt) override ;
965
974
966
- // / Notifies that a block is being inlined into another block.
967
- void notifyBlockBeingInlined (Block *block, Block *srcBlock,
968
- Block::iterator before);
969
-
970
975
// / Notifies that a pattern match failed for the given reason.
971
976
void
972
977
notifyMatchFailure (Location loc,
@@ -1548,7 +1553,7 @@ void ConversionPatternRewriterImpl::notifyOperationInserted(
1548
1553
appendRewrite<MoveOperationRewrite>(op, previous.getBlock (), prevOp);
1549
1554
}
1550
1555
1551
- void ConversionPatternRewriterImpl::notifyOpReplaced (
1556
+ void ConversionPatternRewriterImpl::replaceOp (
1552
1557
Operation *op, SmallVector<SmallVector<Value>> &&newValues) {
1553
1558
assert (newValues.size () == op->getNumResults ());
1554
1559
assert (!ignoredOps.contains (op) && " operation was already replaced" );
@@ -1599,8 +1604,14 @@ void ConversionPatternRewriterImpl::notifyOpReplaced(
1599
1604
op->walk ([&](Operation *op) { replacedOps.insert (op); });
1600
1605
}
1601
1606
1602
- void ConversionPatternRewriterImpl::notifyBlockIsBeingErased (Block *block) {
1607
+ void ConversionPatternRewriterImpl::eraseBlock (Block *block) {
1603
1608
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);
1604
1615
}
1605
1616
1606
1617
void ConversionPatternRewriterImpl::notifyBlockInserted (
@@ -1628,9 +1639,10 @@ void ConversionPatternRewriterImpl::notifyBlockInserted(
1628
1639
appendRewrite<MoveBlockRewrite>(block, previous, prevBlock);
1629
1640
}
1630
1641
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);
1634
1646
}
1635
1647
1636
1648
void ConversionPatternRewriterImpl::notifyMatchFailure (
@@ -1673,7 +1685,7 @@ void ConversionPatternRewriter::replaceOp(Operation *op, ValueRange newValues) {
1673
1685
llvm::map_to_vector (newValues, [](Value v) -> SmallVector<Value> {
1674
1686
return v ? SmallVector<Value>{v} : SmallVector<Value>();
1675
1687
});
1676
- impl->notifyOpReplaced (op, std::move (newVals));
1688
+ impl->replaceOp (op, std::move (newVals));
1677
1689
}
1678
1690
1679
1691
void ConversionPatternRewriter::replaceOpWithMultiple (
@@ -1684,7 +1696,7 @@ void ConversionPatternRewriter::replaceOpWithMultiple(
1684
1696
impl->logger .startLine ()
1685
1697
<< " ** Replace : '" << op->getName () << " '(" << op << " )\n " ;
1686
1698
});
1687
- impl->notifyOpReplaced (op, std::move (newValues));
1699
+ impl->replaceOp (op, std::move (newValues));
1688
1700
}
1689
1701
1690
1702
void ConversionPatternRewriter::eraseOp (Operation *op) {
@@ -1693,7 +1705,7 @@ void ConversionPatternRewriter::eraseOp(Operation *op) {
1693
1705
<< " ** Erase : '" << op->getName () << " '(" << op << " )\n " ;
1694
1706
});
1695
1707
SmallVector<SmallVector<Value>> nullRepls (op->getNumResults (), {});
1696
- impl->notifyOpReplaced (op, std::move (nullRepls));
1708
+ impl->replaceOp (op, std::move (nullRepls));
1697
1709
}
1698
1710
1699
1711
void ConversionPatternRewriter::eraseBlock (Block *block) {
@@ -1704,12 +1716,7 @@ void ConversionPatternRewriter::eraseBlock(Block *block) {
1704
1716
for (Operation &op : *block)
1705
1717
eraseOp (&op);
1706
1718
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);
1713
1720
}
1714
1721
1715
1722
Block *ConversionPatternRewriter::applySignatureConversion (
@@ -1797,7 +1804,7 @@ void ConversionPatternRewriter::inlineBlockBefore(Block *source, Block *dest,
1797
1804
bool fastPath = !impl->config .listener ;
1798
1805
1799
1806
if (fastPath)
1800
- impl->notifyBlockBeingInlined (dest, source , before);
1807
+ impl->inlineBlockBefore (source, dest , before);
1801
1808
1802
1809
// Replace all uses of block arguments.
1803
1810
for (auto it : llvm::zip (source->getArguments (), argValues))
0 commit comments