Skip to content

Commit 4198cc0

Browse files
committed
[Mem2Reg] Fix store_borrow user block omission.
Previously, when blocks were added to the worklist, only blocks which were users of the `alloc_stack` instruction were considered. For "guaranteed alloc_stacks" (`store_borrow` locations), that resulted in not processing blocks which contained uses of the `store_borrow` but not the `alloc_stack`. When such a user was an `end_borrow`, the effect was that no `end_borrow` was created for the newly introduced `begin_borrow [lexical]`. Fix this by adding blocks with users of the `store_borrow` to the worklist.
1 parent 25bceb1 commit 4198cc0

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,12 @@ void StackAllocationPromoter::pruneAllocStackUsage() {
14751475
for (auto *use : asi->getUses())
14761476
functionBlocks.insert(use->getUser()->getParent());
14771477

1478+
for (auto *sbi : asi->getUsersOfType<StoreBorrowInst>()) {
1479+
for (auto *use : sbi->getUses()) {
1480+
functionBlocks.insert(use->getUser()->getParent());
1481+
}
1482+
}
1483+
14781484
for (auto block : functionBlocks)
14791485
if (auto si = promoteAllocationInBlock(block)) {
14801486
// There was a final store/store_borrow instruction which was not

test/SILOptimizer/mem2reg_borrows.sil

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,38 @@ bb3:
337337
return %8 : $()
338338
}
339339

340+
// CHECK-LABEL: sil [ossa] @test_store_borrow_use_in_block_without_alloc_stack_use : {{.*}} {
341+
// CHECK: [[OWNED:%[^,]+]] = apply
342+
// CHECK: [[GUARANTEED:%[^,]+]] = begin_borrow [[OWNED]]
343+
// CHECK: [[LIFETIME:%[^,]+]] = begin_borrow [lexical] [[GUARANTEED]]
344+
// CHECK: apply undef([[LIFETIME]]) : $@convention(thin) (@guaranteed Klass) -> ()
345+
// CHECK: br [[MIDDLE:bb[0-9]+]]
346+
// CHECK: [[MIDDLE]]:
347+
// CHECK: end_borrow [[LIFETIME]]
348+
// CHECK: br [[EXIT:bb[0-9]+]]
349+
// CHECK: [[EXIT]]:
350+
// CHECK: end_borrow [[GUARANTEED]]
351+
// CHECK: destroy_value [[OWNED]]
352+
// CHECK-LABEL: } // end sil function 'test_store_borrow_use_in_block_without_alloc_stack_use'
353+
sil [ossa] @test_store_borrow_use_in_block_without_alloc_stack_use : $@convention(thin) () -> () {
354+
bb0:
355+
%owned = apply undef() : $@convention(thin) () -> (@owned Klass)
356+
%guaranteed = begin_borrow %owned : $Klass
357+
%stack = alloc_stack [lexical] $Klass
358+
%stored = store_borrow %guaranteed to %stack : $*Klass
359+
%load = load_borrow %stored : $*Klass
360+
apply undef(%load) : $@convention(thin) (@guaranteed Klass) -> ()
361+
end_borrow %load : $Klass
362+
br bb1
363+
364+
bb1:
365+
end_borrow %stored : $*Klass
366+
br bb3
367+
368+
bb3:
369+
dealloc_stack %stack : $*Klass
370+
end_borrow %guaranteed : $Klass
371+
destroy_value %owned : $Klass
372+
%retval = tuple ()
373+
return %retval : $()
374+
}

0 commit comments

Comments
 (0)