Skip to content

Commit 0b835f0

Browse files
committed
Fix ownership verification error due to multi block mem2reg with store_borrows
@guaranteed stored values will never be added to a phi in mem2reg because any uses of store borrowed locations have to be accessed via the store borrow return address and since we ban address phis altogether we will never have input sil which necessitates this. But, the DJ-edges based algorithm for inserting phi blocks in mem2reg can insert unnecessary phis which are removed later. Ensure fixPhiPredBlock handles both owned and guaranteed stored values correctly for this reason.
1 parent 3f54e99 commit 0b835f0

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,13 +1328,12 @@ void StackAllocationPromoter::fixPhiPredBlock(BlockSetVector &phiBlocks,
13281328
LLVM_DEBUG(llvm::dbgs() << "*** Fixing the terminator " << *ti << ".\n");
13291329

13301330
LiveValues values = getEffectiveLiveOutValues(phiBlocks, predBlock);
1331-
auto ownedValues = values.getOwned();
13321331

13331332
LLVM_DEBUG(llvm::dbgs() << "*** Found the definition: "
1334-
<< ownedValues.stored);
1333+
<< values.getStored());
13351334

13361335
SmallVector<SILValue> vals;
1337-
vals.push_back(ownedValues.replacement(asi, nullptr));
1336+
vals.push_back(values.replacement(asi, nullptr));
13381337

13391338
addArgumentsToBranch(vals, destBlock, ti);
13401339
deleter.forceDelete(ti);

test/SILOptimizer/mem2reg_borrows.sil

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,39 @@ bb3:
372372
%retval = tuple ()
373373
return %retval : $()
374374
}
375+
376+
// Ensure we don't have an ownership verification error
377+
sil [ossa] @test_store_borrow_phi : $@convention(thin) () -> () {
378+
bb0:
379+
br bb1
380+
381+
bb1:
382+
%1 = apply undef() : $@convention(thin) () -> @owned Klass
383+
%2 = begin_borrow %1 : $Klass
384+
%3 = alloc_stack $Klass
385+
%4 = store_borrow %2 to %3 : $*Klass
386+
%5 = load_borrow %4 : $*Klass
387+
%6 = apply undef(%5) : $@convention(thin) (@guaranteed Klass) -> ()
388+
end_borrow %5 : $Klass
389+
end_borrow %4 : $*Klass
390+
cond_br undef, bb2, bb3
391+
392+
bb2:
393+
br bb4
394+
395+
bb3:
396+
br bb4
397+
398+
bb4:
399+
dealloc_stack %3 : $*Klass
400+
end_borrow %2 : $Klass
401+
destroy_value %1 : $Klass
402+
cond_br undef, bb5, bb6
403+
404+
bb5:
405+
br bb1
406+
407+
bb6:
408+
%17 = tuple ()
409+
return %17 : $()
410+
}

test/SILOptimizer/mem2reg_lifetime_borrows.sil

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,38 @@ bb0(%0 : @guaranteed $WrapperStruct):
526526
return %33 : $()
527527
}
528528

529+
// Ensure we don't have an ownership verification error
530+
sil [ossa] @test_store_borrow_phi : $@convention(thin) () -> () {
531+
bb0:
532+
br bb1
533+
534+
bb1:
535+
%1 = apply undef() : $@convention(thin) () -> @owned Klass
536+
%2 = begin_borrow %1 : $Klass
537+
%3 = alloc_stack [lexical] $Klass
538+
%4 = store_borrow %2 to %3 : $*Klass
539+
%5 = load_borrow %4 : $*Klass
540+
%6 = apply undef(%5) : $@convention(thin) (@guaranteed Klass) -> ()
541+
end_borrow %5 : $Klass
542+
end_borrow %4 : $*Klass
543+
cond_br undef, bb2, bb3
544+
545+
bb2:
546+
br bb4
547+
548+
bb3:
549+
br bb4
550+
551+
bb4:
552+
dealloc_stack %3 : $*Klass
553+
end_borrow %2 : $Klass
554+
destroy_value %1 : $Klass
555+
cond_br undef, bb5, bb6
556+
557+
bb5:
558+
br bb1
559+
560+
bb6:
561+
%17 = tuple ()
562+
return %17 : $()
563+
}

0 commit comments

Comments
 (0)