Skip to content

Commit 135d620

Browse files
committed
Fix DiagnoseLifetimeIssues handling of nonescaping arguments.
Function call arguments were not being treated as liveness uses. Unblocks SIL: Treat store_borrow as borrowing its source, and have the move-only checker account for borrow scopes. #69169 #69169
1 parent 80c89fc commit 135d620

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/SILOptimizer/Mandatory/DiagnoseLifetimeIssues.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ visitUses(SILValue def, bool updateLivenessAndWeakStores, int callDepth) {
189189
// Try to get information from the called function.
190190
switch (getArgumentState(ai, use, callDepth)) {
191191
case DoesNotEscape:
192+
if (updateLivenessAndWeakStores)
193+
liveness->updateForUse(user, /*lifetimeEnding*/ false);
192194
break;
193195
case CanEscape:
194196
return CanEscape;

test/SILOptimizer/diagnose_lifetime_issues.sil

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class WeakCycle {
2727
weak var c: WeakCycle?
2828
}
2929

30+
sil [ossa] @$s24diagnose_lifetime_issues8DelegateCACycfC : $@convention(method) (@thick Delegate.Type) -> @owned Delegate
3031
sil [ossa] @$s24diagnose_lifetime_issues10MyDelegateCACycfC : $@convention(method) (@thick MyDelegate.Type) -> @owned MyDelegate
3132
sil [ossa] @$s24diagnose_lifetime_issues14strongDelegate1dyAA0D0C_tF : $@convention(method) (@guaranteed Delegate, @guaranteed Container) -> ()
3233

@@ -119,3 +120,41 @@ bb0(%0 : $@thick WeakCycle.Type):
119120
%3 = apply %2(%1) : $@convention(method) (@owned WeakCycle) -> @owned WeakCycle
120121
return %3 : $WeakCycle
121122
}
123+
124+
// Helper
125+
sil private [ossa] @testBorrowInDefer$defer : $@convention(thin) (@guaranteed Delegate) -> () {
126+
bb0(%0 : @closureCapture @guaranteed $Delegate):
127+
debug_value %0 : $Delegate, let, name "delegate", argno 1
128+
fix_lifetime %0 : $Delegate
129+
%8 = tuple ()
130+
return %8 : $()
131+
}
132+
133+
// Test no warning for a value kept alive within a call which does not escape its argument.
134+
sil hidden [ossa] @testBorrowinDefer : $@convention(thin) (@guaranteed Container) -> () {
135+
bb0(%0 : @guaranteed $Container):
136+
debug_value %0 : $Container, let, name "container", argno 1
137+
%2 = metatype $@thick Delegate.Type
138+
// function_ref Delegate.__allocating_init()
139+
%3 = function_ref @$s24diagnose_lifetime_issues8DelegateCACycfC : $@convention(method) (@thick Delegate.Type) -> @owned Delegate
140+
141+
// This is the owned allocation.
142+
%4 = apply %3(%2) : $@convention(method) (@thick Delegate.Type) -> @owned Delegate
143+
%6 = copy_value %4 : $Delegate
144+
%7 = enum $Optional<Delegate>, #Optional.some!enumelt, %6 : $Delegate
145+
%8 = ref_element_addr %0 : $Container, #Container.delegate
146+
%9 = begin_access [modify] [dynamic] %8 : $*@sil_weak Optional<Delegate>
147+
148+
// This is the weak assignment.
149+
store_weak %7 to %9 : $*@sil_weak Optional<Delegate>
150+
destroy_value %7 : $Optional<Delegate>
151+
end_access %9 : $*@sil_weak Optional<Delegate>
152+
153+
// This call keeps the parent alive
154+
%15 = function_ref @testBorrowInDefer$defer : $@convention(thin) (@guaranteed Delegate) -> () // user: %16
155+
%16 = apply %15(%4) : $@convention(thin) (@guaranteed Delegate) -> ()
156+
157+
destroy_value %4 : $Delegate
158+
%18 = tuple ()
159+
return %18 : $()
160+
}

0 commit comments

Comments
 (0)