Skip to content

Commit 4c46faa

Browse files
committed
Fix mem2reg for load_borrows with reborrows
StackAllocationPromoter::pruneAllocStackUsage substitutes loads/stores of alloc_stack with values. For some reason isLoadFromStack was bailing out for load_borrows with reborrows leaving them to be fixed up by fixBranchesAndUses which uses live in value from predecessors for substitution which is obviosly incorrect the block containing the load_borrow has a store before it. Fixes rdar://145834542
1 parent f6924ee commit 4c46faa

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -377,11 +377,6 @@ static bool isLoadFromStack(SILInstruction *i, AllocStackInst *asi) {
377377
if (!isa<LoadInst>(i) && !isa<LoadBorrowInst>(i))
378378
return false;
379379

380-
if (auto *lbi = dyn_cast<LoadBorrowInst>(i)) {
381-
if (BorrowedValue(lbi).hasReborrow())
382-
return false;
383-
}
384-
385380
// Skip struct and tuple address projections.
386381
ValueBase *op = i->getOperand(0);
387382
while (op != asi) {

test/SILOptimizer/mem2reg_borrows.sil

+27
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,30 @@ bb6:
408408
%17 = tuple ()
409409
return %17 : $()
410410
}
411+
412+
// CHECK-LABEL: sil [ossa] @test_load_borrow_with_reborrow :
413+
// CHECK-NOT: alloc_stack
414+
// CHECK-LABEL: } // end sil function 'test_load_borrow_with_reborrow'
415+
sil [ossa] @test_load_borrow_with_reborrow : $@convention(thin) () -> () {
416+
bb0:
417+
%owned = apply undef() : $@convention(thin) () -> (@owned Klass)
418+
%stack = alloc_stack [lexical] $Klass
419+
store %owned to [init] %stack : $*Klass
420+
br bb1
421+
422+
bb1:
423+
%ld = load [take] %stack : $*Klass
424+
destroy_value %ld
425+
%owned_other = apply undef() : $@convention(thin) () -> (@owned Klass)
426+
store %owned_other to [init] %stack : $*Klass
427+
%lb = load_borrow %stack
428+
br bb2(%lb)
429+
430+
bb2(%reborrow : @reborrow $Klass):
431+
end_borrow %reborrow : $Klass
432+
destroy_addr %stack : $*Klass
433+
dealloc_stack %stack : $*Klass
434+
%retval = tuple ()
435+
return %retval : $()
436+
}
437+

0 commit comments

Comments
 (0)