@@ -250,10 +250,10 @@ enum class BlockActionKind {
250
250
};
251
251
252
252
// / Original position of the given block in its parent region. During undo
253
- // / actions, the block needs to be placed after `insertAfterBlock `.
253
+ // / actions, the block needs to be placed before `insertBeforeBlock `.
254
254
struct BlockPosition {
255
255
Region *region;
256
- Block *insertAfterBlock ;
256
+ Block *insertBeforeBlock ;
257
257
};
258
258
259
259
// / Information needed to undo inlining actions.
@@ -910,7 +910,8 @@ struct ConversionPatternRewriterImpl {
910
910
void notifyBlockIsBeingErased (Block *block);
911
911
912
912
// / Notifies that a block was created.
913
- void notifyCreatedBlock (Block *block);
913
+ void notifyInsertedBlock (Block *block, Region *previous,
914
+ Region::iterator previousIt);
914
915
915
916
// / Notifies that a block was split.
916
917
void notifySplitBlock (Block *block, Block *continuation);
@@ -919,10 +920,6 @@ struct ConversionPatternRewriterImpl {
919
920
void notifyBlockBeingInlined (Block *block, Block *srcBlock,
920
921
Block::iterator before);
921
922
922
- // / Notifies that the blocks of a region are about to be moved.
923
- void notifyRegionIsBeingInlinedBefore (Region ®ion, Region &parent,
924
- Region::iterator before);
925
-
926
923
// / Notifies that a pattern match failed for the given reason.
927
924
LogicalResult
928
925
notifyMatchFailure (Location loc,
@@ -1173,10 +1170,9 @@ void ConversionPatternRewriterImpl::undoBlockActions(
1173
1170
// Put the block (owned by action) back into its original position.
1174
1171
case BlockActionKind::Erase: {
1175
1172
auto &blockList = action.originalPosition .region ->getBlocks ();
1176
- Block *insertAfterBlock = action.originalPosition .insertAfterBlock ;
1177
- blockList.insert ((insertAfterBlock
1178
- ? std::next (Region::iterator (insertAfterBlock))
1179
- : blockList.begin ()),
1173
+ Block *insertBeforeBlock = action.originalPosition .insertBeforeBlock ;
1174
+ blockList.insert ((insertBeforeBlock ? Region::iterator (insertBeforeBlock)
1175
+ : blockList.end ()),
1180
1176
action.block );
1181
1177
break ;
1182
1178
}
@@ -1196,10 +1192,10 @@ void ConversionPatternRewriterImpl::undoBlockActions(
1196
1192
// Move the block back to its original position.
1197
1193
case BlockActionKind::Move: {
1198
1194
Region *originalRegion = action.originalPosition .region ;
1199
- Block *insertAfterBlock = action.originalPosition .insertAfterBlock ;
1195
+ Block *insertBeforeBlock = action.originalPosition .insertBeforeBlock ;
1200
1196
originalRegion->getBlocks ().splice (
1201
- (insertAfterBlock ? std::next ( Region::iterator (insertAfterBlock) )
1202
- : originalRegion->end ()),
1197
+ (insertBeforeBlock ? Region::iterator (insertBeforeBlock )
1198
+ : originalRegion->end ()),
1203
1199
action.block ->getParent ()->getBlocks (), action.block );
1204
1200
break ;
1205
1201
}
@@ -1398,12 +1394,19 @@ void ConversionPatternRewriterImpl::notifyOpReplaced(Operation *op,
1398
1394
1399
1395
void ConversionPatternRewriterImpl::notifyBlockIsBeingErased (Block *block) {
1400
1396
Region *region = block->getParent ();
1401
- Block *origPrevBlock = block->getPrevNode ();
1402
- blockActions.push_back (BlockAction::getErase (block, {region, origPrevBlock }));
1397
+ Block *origNextBlock = block->getNextNode ();
1398
+ blockActions.push_back (BlockAction::getErase (block, {region, origNextBlock }));
1403
1399
}
1404
1400
1405
- void ConversionPatternRewriterImpl::notifyCreatedBlock (Block *block) {
1406
- blockActions.push_back (BlockAction::getCreate (block));
1401
+ void ConversionPatternRewriterImpl::notifyInsertedBlock (
1402
+ Block *block, Region *previous, Region::iterator previousIt) {
1403
+ if (!previous) {
1404
+ // This is a newly created block.
1405
+ blockActions.push_back (BlockAction::getCreate (block));
1406
+ return ;
1407
+ }
1408
+ Block *prevBlock = previousIt == previous->end () ? nullptr : &*previousIt;
1409
+ blockActions.push_back (BlockAction::getMove (block, {previous, prevBlock}));
1407
1410
}
1408
1411
1409
1412
void ConversionPatternRewriterImpl::notifySplitBlock (Block *block,
@@ -1416,19 +1419,6 @@ void ConversionPatternRewriterImpl::notifyBlockBeingInlined(
1416
1419
blockActions.push_back (BlockAction::getInline (block, srcBlock, before));
1417
1420
}
1418
1421
1419
- void ConversionPatternRewriterImpl::notifyRegionIsBeingInlinedBefore (
1420
- Region ®ion, Region &parent, Region::iterator before) {
1421
- if (region.empty ())
1422
- return ;
1423
- Block *laterBlock = ®ion.back ();
1424
- for (auto &earlierBlock : llvm::drop_begin (llvm::reverse (region), 1 )) {
1425
- blockActions.push_back (
1426
- BlockAction::getMove (laterBlock, {®ion, &earlierBlock}));
1427
- laterBlock = &earlierBlock;
1428
- }
1429
- blockActions.push_back (BlockAction::getMove (laterBlock, {®ion, nullptr }));
1430
- }
1431
-
1432
1422
LogicalResult ConversionPatternRewriterImpl::notifyMatchFailure (
1433
1423
Location loc, function_ref<void (Diagnostic &)> reasonCallback) {
1434
1424
LLVM_DEBUG ({
@@ -1551,8 +1541,9 @@ ConversionPatternRewriter::getRemappedValues(ValueRange keys,
1551
1541
results);
1552
1542
}
1553
1543
1554
- void ConversionPatternRewriter::notifyBlockCreated (Block *block) {
1555
- impl->notifyCreatedBlock (block);
1544
+ void ConversionPatternRewriter::notifyBlockInserted (
1545
+ Block *block, Region *previous, Region::iterator previousIt) {
1546
+ impl->notifyInsertedBlock (block, previous, previousIt);
1556
1547
}
1557
1548
1558
1549
Block *ConversionPatternRewriter::splitBlock (Block *block,
@@ -1582,13 +1573,6 @@ void ConversionPatternRewriter::inlineBlockBefore(Block *source, Block *dest,
1582
1573
eraseBlock (source);
1583
1574
}
1584
1575
1585
- void ConversionPatternRewriter::inlineRegionBefore (Region ®ion,
1586
- Region &parent,
1587
- Region::iterator before) {
1588
- impl->notifyRegionIsBeingInlinedBefore (region, parent, before);
1589
- PatternRewriter::inlineRegionBefore (region, parent, before);
1590
- }
1591
-
1592
1576
void ConversionPatternRewriter::cloneRegionBefore (Region ®ion,
1593
1577
Region &parent,
1594
1578
Region::iterator before,
@@ -1600,7 +1584,7 @@ void ConversionPatternRewriter::cloneRegionBefore(Region ®ion,
1600
1584
1601
1585
for (Block &b : ForwardDominanceIterator<>::makeIterable (region)) {
1602
1586
Block *cloned = mapping.lookup (&b);
1603
- impl->notifyCreatedBlock (cloned);
1587
+ impl->notifyInsertedBlock (cloned, /* previous= */ nullptr , /* previousIt= */ {} );
1604
1588
cloned->walk <WalkOrder::PreOrder, ForwardDominanceIterator<>>(
1605
1589
[&](Operation *op) { notifyOperationInserted (op, /* previous=*/ {}); });
1606
1590
}
0 commit comments