Skip to content

Commit 9781e99

Browse files
committed
DeadObjectElimination: handle begin_borrow/end_borrow when deleting dead arrays
This is part of fixing regressions when enabling OSSA modules: rdar://140229560
1 parent 0bbaa35 commit 9781e99

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lib/SILOptimizer/Transforms/DeadObjectElimination.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,8 @@ recursivelyCollectInteriorUses(ValueBase *DefInst,
604604

605605
// Lifetime endpoints that don't allow the address to escape.
606606
if (isa<RefCountingInst>(User) || isa<DebugValueInst>(User) ||
607-
isa<FixLifetimeInst>(User) || isa<DestroyValueInst>(User)) {
607+
isa<FixLifetimeInst>(User) || isa<DestroyValueInst>(User) ||
608+
isa<EndBorrowInst>(User)) {
608609
AllUsers.insert(User);
609610
continue;
610611
}
@@ -632,6 +633,13 @@ recursivelyCollectInteriorUses(ValueBase *DefInst,
632633
}
633634
continue;
634635
}
636+
if (auto *bb = dyn_cast<BeginBorrowInst>(User)) {
637+
if (!recursivelyCollectInteriorUses(bb, AddressNode,
638+
IsInteriorAddress)) {
639+
return false;
640+
}
641+
continue;
642+
}
635643
if (auto PTAI = dyn_cast<PointerToAddressInst>(User)) {
636644
// Only one pointer-to-address is allowed for safety.
637645
if (SeenPtrToAddr)

test/SILOptimizer/dead_array_elim_ossa.sil

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct NonTrivialStruct {
3131
sil [_semantics "array.uninitialized_intrinsic"] @allocArray : $@convention(thin) <τ_0_0> (Builtin.Word) -> @owned (Array<τ_0_0>, Builtin.RawPointer)
3232

3333
sil [_semantics "array.uninitialized"] @adoptStorageSpecializedForInt : $@convention(method) (@guaranteed _ContiguousArrayStorage<Int>, Builtin.Word, @thin Array<Int>.Type) -> (@owned Array<Int>, UnsafeMutablePointer<Int>)
34+
sil [_semantics "array.uninitialized"] @allocUninitialized : $@convention(thin) (Int) -> (@owned Array<Int>, UnsafeMutablePointer<Int>)
3435

3536
sil [_semantics "array.finalize_intrinsic"] @finalize : $@convention(thin) (@owned Array<Int>) -> @owned Array<Int>
3637

@@ -190,3 +191,23 @@ bb0(%0 : @owned $Klass):
190191
return %t : $()
191192
}
192193

194+
195+
// CHECK-LABEL: sil [ossa] @dead_array_with_borrow :
196+
// CHECK-NOT: apply
197+
// CHECK: %1 = tuple
198+
// CHECK-NEXT: return
199+
// CHECK-LABEL: } // end sil function 'dead_array_with_borrow'
200+
sil [ossa] @dead_array_with_borrow : $@convention(thin) (Int) -> () {
201+
bb0(%0 : $Int):
202+
%2 = function_ref @allocUninitialized : $@convention(thin) (Int) -> (@owned Array<Int>, UnsafeMutablePointer<Int>)
203+
%3 = apply %2(%0) : $@convention(thin) (Int) -> (@owned Array<Int>, UnsafeMutablePointer<Int>)
204+
(%4, %5) = destructure_tuple %3
205+
debug_value %4, let, name "a"
206+
%7 = begin_borrow [lexical] %4
207+
fix_lifetime %7
208+
end_borrow %7
209+
destroy_value %4
210+
%11 = tuple ()
211+
return %11
212+
}
213+

0 commit comments

Comments
 (0)