Skip to content

Commit 29067a5

Browse files
committed
[AddressLowering] Handle store_borrow.
Thanks to the invariants of store_borrow, rewriting a store_borrow is a simple matter of replacing its (non end_borrow) uses with uses of the underlying address-only value whose use was stored.
1 parent 9642080 commit 29067a5

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3101,6 +3101,18 @@ class UseRewriter : SILInstructionVisitor<UseRewriter> {
31013101
llvm::report_fatal_error("Unimplemented SelectValue use.");
31023102
}
31033103

3104+
void visitStoreBorrowInst(StoreBorrowInst *sbi) {
3105+
auto addr = addrMat.materializeAddress(use->get());
3106+
SmallVector<Operand *, 4> uses(sbi->getUses());
3107+
for (auto *use : uses) {
3108+
if (auto *ebi = dyn_cast<EndBorrowInst>(use->getUser())) {
3109+
pass.deleter.forceDelete(ebi);
3110+
}
3111+
}
3112+
sbi->replaceAllUsesWith(addr);
3113+
pass.deleter.forceDelete(sbi);
3114+
}
3115+
31043116
void rewriteStore(SILValue srcVal, SILValue destAddr,
31053117
IsInitialization_t isInit);
31063118

test/SILOptimizer/address_lowering.sil

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,36 @@ bb0:
21492149
return %retval : $()
21502150
}
21512151

2152+
// CHECK-LABEL: sil [ossa] @test_store_borrow_1_copy_addr : {{.*}} {
2153+
// CHECK: [[ADDR:%[^,]+]] = alloc_stack
2154+
// CHECK: apply undef<T>([[ADDR]])
2155+
// CHECK: [[COPY:%[^,]+]] = alloc_stack
2156+
// CHECK: copy_addr [[ADDR]] to [init] [[COPY]]
2157+
// CHECK: apply undef<T>([[COPY]])
2158+
// CHECK: destroy_addr [[COPY]]
2159+
// CHECK: dealloc_stack [[COPY]]
2160+
// CHECK-NOT: end_borrow [[ADDR]]
2161+
// CHECK: destroy_addr [[ADDR]]
2162+
// CHECK-LABEL: } // end sil function 'test_store_borrow_1_copy_addr'
2163+
sil [ossa] @test_store_borrow_1_copy_addr : $@convention(thin) <T> () -> () {
2164+
entry:
2165+
%addr = alloc_stack $T
2166+
%instance = apply undef<T>() : $@convention(thin) <τ> () -> (@out τ)
2167+
%lifetime = begin_borrow %instance : $T
2168+
%borrow = store_borrow %lifetime to %addr : $*T
2169+
%copy = alloc_stack $T
2170+
copy_addr %borrow to [init] %copy : $*T
2171+
apply undef<T>(%copy) : $@convention(thin) <τ> (@inout τ) -> ()
2172+
destroy_addr %copy : $*T
2173+
dealloc_stack %copy : $*T
2174+
end_borrow %borrow : $*T
2175+
end_borrow %lifetime : $T
2176+
dealloc_stack %addr : $*T
2177+
destroy_value %instance : $T
2178+
%retval = tuple ()
2179+
return %retval : $()
2180+
}
2181+
21522182
// CHECK-LABEL: sil hidden [ossa] @test_unchecked_bitwise_cast :
21532183
// CHECK: bb0(%0 : $*U, %1 : $*T, %2 : $@thick U.Type):
21542184
// CHECK: [[STK:%.*]] = alloc_stack $T

0 commit comments

Comments
 (0)