Skip to content

Commit 16fc9e1

Browse files
Merge pull request #39517 from nate-chandler/lexical_lifetimes/inliner-fixes
[SILInliner] Fixed some arg lexical lifetimes.
2 parents 7643373 + e1e750a commit 16fc9e1

File tree

2 files changed

+170
-6
lines changed

2 files changed

+170
-6
lines changed

lib/SILOptimizer/Utils/SILInliner.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,10 @@ SILInlineCloner::cloneInline(ArrayRef<SILValue> AppliedArgs) {
436436
SILValue callArg = p.value();
437437
unsigned idx = p.index();
438438
if (idx >= calleeConv.getSILArgIndexOfFirstParam()) {
439-
if (Apply.getFunction()->hasOwnership() && enableLexicalLifetimes) {
440-
if (!callArg->getType().isTrivial(*Apply.getFunction())) {
439+
if (enableLexicalLifetimes) {
440+
if (Apply.getFunction()->hasOwnership() &&
441+
!callArg->getType().isTrivial(*Apply.getFunction()) &&
442+
!callArg->getType().isAddress()) {
441443
SILBuilderWithScope builder(Apply.getInstruction(), getBuilder());
442444
if (calleeConv.getParamInfoForSILArg(idx).isGuaranteed()) {
443445
callArg = builder.createBeginBorrow(Apply.getLoc(), callArg,
@@ -448,8 +450,8 @@ SILInlineCloner::cloneInline(ArrayRef<SILValue> AppliedArgs) {
448450
callArg = builder.createCopyValue(Apply.getLoc(), bbi);
449451
copiedArgs[idx] = true;
450452
}
453+
borrowedArgs[idx] = true;
451454
}
452-
borrowedArgs[idx] = true;
453455
} else {
454456
// Insert begin/end borrow for guaranteed arguments.
455457
if (calleeConv.getParamInfoForSILArg(idx).isGuaranteed()) {

test/SILOptimizer/inline_lifetime.sil

Lines changed: 165 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import Swift
44

55
class C {}
66

7+
struct S {}
8+
79
////////////////////////////////////////////////////////////////////////////////
810
// apply
911
////////////////////////////////////////////////////////////////////////////////
@@ -23,6 +25,18 @@ entry(%instance : @guaranteed $C):
2325
return %retval : $()
2426
}
2527

28+
sil [ossa] [always_inline] @callee_trivial : $@convention(thin) (S) -> () {
29+
entry(%instance : $S):
30+
%retval = tuple ()
31+
return %retval : $()
32+
}
33+
34+
sil [ossa] [always_inline] @callee_address : $@convention(thin) (@in S) -> () {
35+
entry(%instance : $*S):
36+
%retval = tuple ()
37+
return %retval : $()
38+
}
39+
2640
// tests
2741

2842
// CHECK-LABEL: sil [ossa] @caller_owned_callee_owned : $@convention(thin) (@owned C) -> () {
@@ -91,6 +105,30 @@ entry(%instance : @guaranteed $C):
91105
return %result : $()
92106
}
93107

108+
// CHECK-LABEL: sil [ossa] @caller_trivial_callee_trivial : $@convention(thin) (S) -> () {
109+
// CHECK: {{bb[^,]+}}({{%[^,]+}} : $S):
110+
// CHECK: [[RETVAL:%[^,]+]] = tuple ()
111+
// CHECK: return [[RETVAL]]
112+
// CHECK-LABEL: } // end sil function 'caller_trivial_callee_trivial'
113+
sil [ossa] @caller_trivial_callee_trivial : $@convention(thin) (S) -> () {
114+
entry(%instance : $S):
115+
%callee_trivial = function_ref @callee_trivial : $@convention(thin) (S) -> ()
116+
%result = apply %callee_trivial(%instance) : $@convention(thin) (S) -> ()
117+
return %result : $()
118+
}
119+
120+
// CHECK-LABEL: sil [ossa] @caller_address_callee_address : $@convention(thin) (@in S) -> () {
121+
// CHECK: {{bb[^,]+}}({{%[^,]+}} : $*S):
122+
// CHECK: [[RETVAL:%[^,]+]] = tuple ()
123+
// CHECK: return [[RETVAL]]
124+
// CHECK-LABEL: } // end sil function 'caller_address_callee_address'
125+
sil [ossa] @caller_address_callee_address : $@convention(thin) (@in S) -> () {
126+
entry(%instance : $*S):
127+
%callee_address = function_ref @callee_address : $@convention(thin) (@in S) -> ()
128+
%result = apply %callee_address(%instance) : $@convention(thin) (@in S) -> ()
129+
return %result : $()
130+
}
131+
94132
////////////////////////////////////////////////////////////////////////////////
95133
// begin_apply
96134
////////////////////////////////////////////////////////////////////////////////
@@ -130,6 +168,30 @@ bb2:
130168
unwind
131169
}
132170

171+
sil hidden [ossa] [always_inline] @callee_coro_trivial : $@yield_once @convention(method) (S) -> @yields @inout S {
172+
bb0(%instance : $S):
173+
%addr = alloc_stack $S
174+
store %instance to [trivial] %addr : $*S
175+
yield %addr : $*S, resume bb1, unwind bb2
176+
bb1:
177+
dealloc_stack %addr : $*S
178+
%result = tuple ()
179+
return %result : $()
180+
bb2:
181+
dealloc_stack %addr : $*S
182+
unwind
183+
}
184+
185+
sil hidden [ossa] [always_inline] @callee_coro_address : $@yield_once @convention(method) (@in S) -> @yields @inout S {
186+
bb0(%instance : $*S):
187+
yield %instance : $*S, resume bb1, unwind bb2
188+
bb1:
189+
%result = tuple ()
190+
return %result : $()
191+
bb2:
192+
unwind
193+
}
194+
133195
// tests
134196

135197
// CHECK-LABEL: sil [ossa] @caller_owned_callee_coro_owned : $@convention(method) (@owned C) -> () {
@@ -242,6 +304,44 @@ bb0(%instance : @guaranteed $C):
242304
return %retval : $()
243305
}
244306

307+
// CHECK-LABEL: sil hidden [ossa] @caller_trivial_callee_coro_trivial : $@convention(method) (S) -> () {
308+
// CHECK: {{bb[0-9]+}}([[REGISTER_0:%[^,]+]] : $S):
309+
// CHECK: [[REGISTER_1:%[^,]+]] = alloc_stack $S
310+
// CHECK: store [[REGISTER_0]] to [trivial] [[REGISTER_1]] : $*S
311+
// CHECK: dealloc_stack [[REGISTER_1]] : $*S
312+
// CHECK: [[REGISTER_4:%[^,]+]] = tuple ()
313+
// CHECK: [[REGISTER_5:%[^,]+]] = tuple ()
314+
// CHECK: return [[REGISTER_5]] : $()
315+
// CHECK: {{bb[0-9]+}}:
316+
// CHECK: dealloc_stack [[REGISTER_1]] : $*S
317+
// CHECK: unreachable
318+
// CHECK-LABEL: } // end sil function 'caller_trivial_callee_coro_trivial'
319+
sil hidden [ossa] @caller_trivial_callee_coro_trivial : $@convention(method) (S) -> () {
320+
bb0(%instance : $S):
321+
%callee_coro_trivial = function_ref @callee_coro_trivial : $@yield_once @convention(method) (S) -> @yields @inout S
322+
(%addr, %continuation) = begin_apply %callee_coro_trivial(%instance) : $@yield_once @convention(method) (S) -> @yields @inout S
323+
end_apply %continuation
324+
%retval = tuple ()
325+
return %retval : $()
326+
}
327+
328+
// CHECK-LABEL: sil hidden [ossa] @caller_address_callee_coro_address : $@convention(method) (@in S) -> () {
329+
// CHECK: {{bb[^,]+}}({{%[^,]+}} : $*S):
330+
// CHECK: {{%[^,]+}} = tuple ()
331+
// CHECK: [[RETVAL:%[^,]+]] = tuple ()
332+
// CHECK: return [[RETVAL]]
333+
// CHECK: {{bb[^,]+}}:
334+
// CHECK: unreachable
335+
// CHECK-LABEL: } // end sil function 'caller_address_callee_coro_address'
336+
sil hidden [ossa] @caller_address_callee_coro_address : $@convention(method) (@in S) -> () {
337+
bb0(%instance : $*S):
338+
%callee_coro_address = function_ref @callee_coro_address : $@yield_once @convention(method) (@in S) -> @yields @inout S
339+
(%addr, %continuation) = begin_apply %callee_coro_address(%instance) : $@yield_once @convention(method) (@in S) -> @yields @inout S
340+
end_apply %continuation
341+
%retval = tuple ()
342+
return %retval : $()
343+
}
344+
245345
////////////////////////////////////////////////////////////////////////////////
246346
// try_apply
247347
////////////////////////////////////////////////////////////////////////////////
@@ -270,6 +370,26 @@ bb2:
270370
return %18 : $()
271371
}
272372

373+
sil [ossa] @callee_error_trivial : $@convention(thin) (S) -> @error Error {
374+
bb0(%0 : $S):
375+
cond_br undef, bb1, bb2
376+
bb1:
377+
throw undef : $Error
378+
bb2:
379+
%18 = tuple ()
380+
return %18 : $()
381+
}
382+
383+
sil [ossa] @callee_error_address : $@convention(thin) (@in S) -> @error Error {
384+
bb0(%0 : $*S):
385+
cond_br undef, bb1, bb2
386+
bb1:
387+
throw undef : $Error
388+
bb2:
389+
%18 = tuple ()
390+
return %18 : $()
391+
}
392+
273393
// tests
274394

275395
// CHECK-LABEL: sil [ossa] @callee_owned_callee_error_owned : $@convention(thin) (@owned C) -> @error Error {
@@ -364,11 +484,11 @@ bb2(%12 : @owned $Error):
364484
// CHECK-LABEL: sil [ossa] @callee_guaranteed_callee_error_guaranteed : $@convention(thin) (@guaranteed C) -> @error Error {
365485
// CHECK: {{bb[^,]+}}([[INSTANCE:%[^,]+]] : @guaranteed $C):
366486
// CHECK: [[LIFETIME:%[^,]+]] = begin_borrow [lexical] [[INSTANCE]]
367-
// CHECK: cond_br undef, [[BASIC_BLOCK1:bb[^,]+]], [[BASIC_BLOCK2:bb[0-9]+]]
368-
// CHECK: [[BASIC_BLOCK1]]:
487+
// CHECK: cond_br undef, [[THROW_BLOCK:bb[^,]+]], [[REGULAR_BLOCK:bb[0-9]+]]
488+
// CHECK: [[THROW_BLOCK]]:
369489
// CHECK: end_borrow [[LIFETIME]]
370490
// CHECK: throw undef
371-
// CHECK: [[BASIC_BLOCK2]]:
491+
// CHECK: [[REGULAR_BLOCK]]:
372492
// CHECK: [[ORIGINAL_RETVAL:%[^,]+]] = tuple ()
373493
// CHECK: end_borrow [[LIFETIME]]
374494
// CHECK: [[RETVAL:%[^,]+]] = tuple ()
@@ -384,3 +504,45 @@ bb1(%9 : $()):
384504
bb2(%12 : @owned $Error):
385505
throw %12 : $Error
386506
}
507+
508+
// CHECK-LABEL: sil hidden [ossa] @callee_trivial_callee_error_trivial : $@convention(thin) (S) -> @error Error {
509+
// CHECK: {{bb[^,]+}}({{%[^,]+}} : $S):
510+
// CHECK: cond_br undef, [[THROW_BLOCK:bb[^,]+]], [[REGULAR_BLOCK:bb[0-9]+]]
511+
// CHECK: [[THROW_BLOCK]]:
512+
// CHECK: throw undef
513+
// CHECK: [[REGULAR_BLOCK]]:
514+
// CHECK: [[ORIGINAL_RETVAL:%[^,]+]] = tuple ()
515+
// CHECK: [[RETVAL:%[^,]+]] = tuple ()
516+
// CHECK: return [[RETVAL]]
517+
// CHECK-LABEL: } // end sil function 'callee_trivial_callee_error_trivial'
518+
sil hidden [ossa] @callee_trivial_callee_error_trivial : $@convention(thin) (S) -> @error Error {
519+
bb0(%instance : $S):
520+
%callee_error_trivial = function_ref @callee_error_trivial : $@convention(thin) (S) -> @error Error
521+
try_apply %callee_error_trivial(%instance) : $@convention(thin) (S) -> @error Error, normal bb1, error bb2
522+
bb1(%9 : $()):
523+
%10 = tuple ()
524+
return %10 : $()
525+
bb2(%12 : @owned $Error):
526+
throw %12 : $Error
527+
}
528+
529+
// CHECK-LABEL: sil hidden [ossa] @callee_address_callee_error_address : $@convention(thin) (@in S) -> @error Error {
530+
// CHECK: {{bb[^,]+}}({{%[^,]+}} : $*S):
531+
// CHECK: cond_br undef, [[THROW_BLOCK:bb[0-9]+]], [[REGULAR_BLOCK:bb[0-9]+]]
532+
// CHECK: [[THROW_BLOCK]]:
533+
// CHECK: throw undef
534+
// CHECK: [[REGULAR_BLOCK]]:
535+
// CHECK: {{%[^,]+}} = tuple ()
536+
// CHECK: [[RETVAL:%[^,]+]] = tuple ()
537+
// CHECK: return [[RETVAL]]
538+
// CHECK-LABEL: } // end sil function 'callee_address_callee_error_address'
539+
sil hidden [ossa] @callee_address_callee_error_address : $@convention(thin) (@in S) -> @error Error {
540+
bb0(%instance : $*S):
541+
%callee_error_address = function_ref @callee_error_address : $@convention(thin) (@in S) -> @error Error
542+
try_apply %callee_error_address(%instance) : $@convention(thin) (@in S) -> @error Error, normal bb1, error bb2
543+
bb1(%9 : $()):
544+
%10 = tuple ()
545+
return %10 : $()
546+
bb2(%12 : @owned $Error):
547+
throw %12 : $Error
548+
}

0 commit comments

Comments
 (0)