Skip to content

Commit d6b7ec9

Browse files
authored
Merge pull request swiftlang#60857 from meg-gupta/fixmem2regsbi2
Fix store_borrow multi block handling in mem2reg
2 parents d072191 + 17664c0 commit d6b7ec9

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,8 @@ beginLexicalLifetimeAfterStore(AllocStackInst *asi, SILInstruction *inst) {
420420
if (isGuaranteedLexicalValue(sbi->getSrc())) {
421421
return {{stored, SILValue(), SILValue()}, /*isStorageValid*/ true};
422422
}
423-
auto *borrow = SILBuilderWithScope(sbi).createBeginBorrow(
424-
loc, stored, /*isLexical*/ true);
423+
auto *borrow = SILBuilderWithScope(sbi->getNextInstruction())
424+
.createBeginBorrow(loc, stored, /*isLexical*/ true);
425425
return {{stored, borrow, SILValue()}, /*isStorageValid*/ true};
426426
}
427427
BeginBorrowInst *bbi = nullptr;
@@ -794,6 +794,11 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
794794
if (!sbi) {
795795
continue;
796796
}
797+
if (sbi->getDest() != asi) {
798+
continue;
799+
}
800+
assert(!deinitializationPoints[blockPromotingWithin]);
801+
deinitializationPoints[blockPromotingWithin] = inst;
797802
if (!runningVals.hasValue()) {
798803
continue;
799804
}
@@ -802,8 +807,6 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
802807
}
803808
// Mark storage as invalid and mark end_borrow as a deinit point.
804809
runningVals->isStorageValid = false;
805-
assert(!deinitializationPoints[blockPromotingWithin]);
806-
deinitializationPoints[blockPromotingWithin] = inst;
807810
if (!canEndLexicalLifetime(runningVals->value)) {
808811
continue;
809812
}

test/SILOptimizer/mem2reg_lifetime_borrows.sil

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,20 @@ bb3:
484484
return %r : $()
485485
}
486486

487+
// CHECK-NOT: alloc_stack [lexical]
488+
// CHECK-LABEL: } // end sil function 'storeborrow_multi_only_lexicalallocas'
489+
sil [ossa] @storeborrow_multi_only_lexicalallocas : $@convention(thin) (@in_guaranteed Klass) -> () {
490+
bb0(%0 : $*Klass):
491+
%l = load_borrow %0 : $*Klass
492+
%1 = alloc_stack [lexical] $Klass
493+
%sb = store_borrow %l to %1 : $*Klass
494+
br bb1
495+
496+
bb1:
497+
end_borrow %sb : $*Klass
498+
end_borrow %l : $Klass
499+
dealloc_stack %1 : $*Klass
500+
%6 = tuple ()
501+
return %6 : $()
502+
}
503+

0 commit comments

Comments
 (0)