Skip to content

Commit a62112b

Browse files
authored
Merge pull request swiftlang#78200 from meg-gupta/fixdce
Look through borrowed from instructions before calling lifetime completion in DCE
2 parents 33e8c2f + 50bde82 commit a62112b

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

lib/SILOptimizer/Transforms/DeadCodeElimination.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -637,17 +637,20 @@ void DCE::endLifetimeOfLiveValue(Operand *op, SILInstruction *insertPt) {
637637
// If DCE is going to delete the block in which we have to insert a
638638
// compensating lifetime end, let complete lifetimes utility handle it.
639639
if (!LiveBlocks.contains(insertPt->getParent())) {
640-
valuesToComplete.push_back(value);
640+
valuesToComplete.push_back(lookThroughBorrowedFromDef(value));
641641
return;
642642
}
643643

644644
SILBuilderWithScope builder(insertPt);
645645
if (value->getOwnershipKind() == OwnershipKind::Owned) {
646-
builder.createDestroyValue(RegularLocation::getAutoGeneratedLocation(),
647-
value);
646+
auto *destroy = builder.createDestroyValue(
647+
RegularLocation::getAutoGeneratedLocation(), value);
648+
markInstructionLive(destroy);
648649
}
649650
if (value->getOwnershipKind() == OwnershipKind::Guaranteed) {
650-
builder.createEndBorrow(RegularLocation::getAutoGeneratedLocation(), value);
651+
auto *endBorrow = builder.createEndBorrow(
652+
RegularLocation::getAutoGeneratedLocation(), value);
653+
markInstructionLive(endBorrow);
651654
}
652655
}
653656

test/SILOptimizer/dead_code_elimination_nontrivial_ossa.sil

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,3 +1376,38 @@ bb3:
13761376
%res = tuple ()
13771377
return %res : $()
13781378
}
1379+
1380+
// Ensure no verification error
1381+
sil [ossa] @dce_borrowedfromuser : $@convention(thin) (@guaranteed FakeOptional<Klass>) -> () {
1382+
bb0(%0 : @guaranteed $FakeOptional<Klass>):
1383+
%1 = copy_value %0
1384+
%2 = begin_borrow %1
1385+
%3 = begin_borrow %1
1386+
br bb1(%2, %3)
1387+
1388+
bb1(%5 : @reborrow $FakeOptional<Klass>, %6 : @reborrow $FakeOptional<Klass>):
1389+
%7 = borrowed %6 from (%1)
1390+
%8 = borrowed %5 from (%1)
1391+
switch_enum %7, case #FakeOptional.some!enumelt: bb2, case #FakeOptional.none!enumelt: bb3
1392+
1393+
bb2(%10 : @guaranteed $Klass):
1394+
%11 = function_ref @$use_klass2 : $@convention(thin) (@guaranteed Klass) -> ()
1395+
%12 = apply %11(%10) : $@convention(thin) (@guaranteed Klass) -> ()
1396+
br bb4
1397+
1398+
bb3:
1399+
br bb4
1400+
1401+
bb4:
1402+
br bb5(%8, %7)
1403+
1404+
bb5(%16 : @reborrow $FakeOptional<Klass>, %17 : @reborrow $FakeOptional<Klass>):
1405+
%18 = borrowed %17 from (%1)
1406+
%19 = borrowed %16 from (%1)
1407+
end_borrow %19
1408+
end_borrow %18
1409+
destroy_value %1
1410+
%23 = tuple ()
1411+
return %23
1412+
}
1413+

0 commit comments

Comments
 (0)