Skip to content

Commit 09dd182

Browse files
committed
[SILInliner] Only lifetime args with ownership.
Previously, after #39712, lexical lifetimes were added for arguments whose parameter convention was guaranteed. Those lexical borrow scopes were added regardless of the ownership of the values which were passed as those arguments. Here, the lifetimes are only added for argument values which have ownership.
1 parent fd1dbbe commit 09dd182

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

lib/SILOptimizer/Utils/SILInliner.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,18 @@ SILValue SILInlineCloner::borrowFunctionArgument(SILValue callArg,
601601
->getModule()
602602
.getASTContext()
603603
.SILOpts.EnableExperimentalLexicalLifetimes;
604-
if (!AI.getFunction()->hasOwnership() ||
605-
(callArg.getOwnershipKind() != OwnershipKind::Owned &&
606-
!enableLexicalLifetimes)) {
604+
auto argOwnershipRequiresBorrow = [&]() {
605+
auto kind = callArg.getOwnershipKind();
606+
if (enableLexicalLifetimes) {
607+
// At this point, we know that the function argument is @guaranteed.
608+
// If the value passed as that parameter has ownership, always add a
609+
// lexical borrow scope to ensure that the value stays alive for the
610+
// duration of the inlined callee.
611+
return kind != OwnershipKind::None;
612+
}
613+
return kind == OwnershipKind::Owned;
614+
};
615+
if (!AI.getFunction()->hasOwnership() || !argOwnershipRequiresBorrow()) {
607616
return SILValue();
608617
}
609618

test/SILOptimizer/inline_lifetime.sil

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ entry(%instance : $*S):
3737
return %retval : $()
3838
}
3939

40+
sil [always_inline] [ossa] @callee_inguaranteed : $@convention(thin) <T> (@in_guaranteed T) -> () {
41+
entry(%arg : $*T):
42+
%retval = tuple ()
43+
return %retval : $()
44+
}
45+
4046
// tests
4147

4248
// CHECK-LABEL: sil [ossa] @caller_owned_callee_owned : $@convention(thin) (@owned C) -> () {
@@ -114,6 +120,16 @@ entry(%instance : $*S):
114120
return %result : $()
115121
}
116122

123+
// CHECK-LABEL: sil [ossa] @caller_inguaranteed_callee_inguaranteed : $@convention(thin) <T> (@in_guaranteed T) -> () {
124+
// CHECK-NOT: begin_borrow [lexical]
125+
// CHECK-LABEL: } // end sil function 'caller_inguaranteed_callee_inguaranteed'
126+
sil [ossa] @caller_inguaranteed_callee_inguaranteed : $@convention(thin) <T> (@in_guaranteed T) -> () {
127+
bb0(%instance : $*T):
128+
%callee_inguaranteed = function_ref @callee_inguaranteed : $@convention(thin) <T> (@in_guaranteed T) -> ()
129+
%retval = apply %callee_inguaranteed<T>(%instance) : $@convention(thin) <T> (@in_guaranteed T) -> ()
130+
return %retval : $()
131+
}
132+
117133
////////////////////////////////////////////////////////////////////////////////
118134
// begin_apply
119135
////////////////////////////////////////////////////////////////////////////////
@@ -177,6 +193,22 @@ bb2:
177193
unwind
178194
}
179195

196+
sil hidden [ossa] [always_inline] @callee_coro_inguaranteed : $@yield_once @convention(thin) <T> (@in_guaranteed T) -> @yields @inout T {
197+
bb0(%instance : $*T):
198+
%addr = alloc_stack $T
199+
copy_addr %instance to [initialization] %addr : $*T
200+
yield %addr : $*T, resume bb1, unwind bb2
201+
bb1:
202+
destroy_addr %addr : $*T
203+
dealloc_stack %addr : $*T
204+
%result = tuple ()
205+
return %result : $()
206+
bb2:
207+
destroy_addr %addr : $*T
208+
dealloc_stack %addr : $*T
209+
unwind
210+
}
211+
180212
// tests
181213

182214
// CHECK-LABEL: sil [ossa] @caller_owned_callee_coro_owned : $@convention(thin) (@owned C) -> () {
@@ -296,6 +328,18 @@ bb0(%instance : $*S):
296328
return %retval : $()
297329
}
298330

331+
// CHECK-LABEL: sil hidden [ossa] @caller_inguaranteed_callee_coro_inguaranteed : $@convention(thin) <T> (@in_guaranteed T) -> () {
332+
// CHECK-NOTE: begin_borrow [lexical]
333+
// CHECK-LABEL: } // end sil function 'caller_inguaranteed_callee_coro_inguaranteed'
334+
sil hidden [ossa] @caller_inguaranteed_callee_coro_inguaranteed : $@convention(thin) <T> (@in_guaranteed T) -> () {
335+
bb0(%instance : $*T):
336+
%callee_coro_inguaranteed = function_ref @callee_coro_inguaranteed : $@yield_once @convention(thin) <T> (@in_guaranteed T) -> @yields @inout T
337+
(%addr_out, %continuation) = begin_apply %callee_coro_inguaranteed<T>(%instance) : $@yield_once @convention(thin) <T> (@in_guaranteed T) -> @yields @inout T
338+
end_apply %continuation
339+
%retval = tuple ()
340+
return %retval : $()
341+
}
342+
299343
////////////////////////////////////////////////////////////////////////////////
300344
// try_apply
301345
////////////////////////////////////////////////////////////////////////////////
@@ -344,6 +388,16 @@ bb2:
344388
return %18 : $()
345389
}
346390

391+
sil [ossa] @callee_error_inguaranteed : $@convention(thin) <T> (@in_guaranteed T) -> @error Error {
392+
bb0(%0 : $*T):
393+
cond_br undef, bb1, bb2
394+
bb1:
395+
throw undef : $Error
396+
bb2:
397+
%18 = tuple ()
398+
return %18 : $()
399+
}
400+
347401
// tests
348402

349403
// CHECK-LABEL: sil [ossa] @caller_owned_callee_error_owned : $@convention(thin) (@owned C) -> @error Error {
@@ -469,3 +523,17 @@ bb1(%9 : $()):
469523
bb2(%12 : @owned $Error):
470524
throw %12 : $Error
471525
}
526+
527+
// CHECK-LABEL: sil hidden [ossa] @caller_inguaranteed_callee_error_inguaranteed : $@convention(thin) <T> (@in_guaranteed T) -> @error Error {
528+
// CHECK-NOT: begin_borrow [lexical]
529+
// CHECK-LABEL: // end sil function 'caller_inguaranteed_callee_error_inguaranteed'
530+
sil hidden [ossa] @caller_inguaranteed_callee_error_inguaranteed : $@convention(thin) <T> (@in_guaranteed T) -> @error Error {
531+
bb0(%instance : $*T):
532+
%callee_error_inguaranteed = function_ref @callee_error_inguaranteed : $@convention(thin) <T> (@in_guaranteed T) -> @error Error
533+
try_apply %callee_error_inguaranteed<T>(%instance) : $@convention(thin) <T> (@in_guaranteed T) -> @error Error, normal bb1, error bb2
534+
bb1(%9 : $()):
535+
%10 = tuple ()
536+
return %10 : $()
537+
bb2(%12 : @owned $Error):
538+
throw %12 : $Error
539+
}

0 commit comments

Comments
 (0)