Skip to content

Commit 01e1b01

Browse files
Merge pull request #3168 from aschwaighofer/capture_promotion_conservative_fix
Capture promotion: The pass does not handle indirect results correctly
2 parents f89a263 + 5396651 commit 01e1b01

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

lib/SILOptimizer/IPO/CapturePromotion.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,11 @@ examineAllocBoxInst(AllocBoxInst *ABI, ReachabilityInfo &RI,
747747
if (signatureHasDependentTypes(*CalleeTy))
748748
return false;
749749

750+
// The code currently does not handle indirect results correctly.
751+
// Conservatively, bail
752+
if (CalleeTy->hasIndirectResults())
753+
return false;
754+
750755
auto closureType = PAI->getType().castTo<SILFunctionType>();
751756

752757
// Calculate the index into the closure's argument list of the captured

test/SILOptimizer/capture_promotion.sil

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,47 @@ bb0:
8282
strong_release %11 : $@box Int
8383
strong_release %6 : $@box Baz
8484
strong_release %1 : $@box Foo
85+
8586
return %21 : $@callee_owned () -> Int
8687
}
8788

89+
// CHECK-LABEL: sil @test_capture_promotion_indirect
90+
sil @test_capture_promotion_indirect : $@convention(thin) () -> @owned @callee_owned () -> @out Int {
91+
bb0:
92+
%0 = tuple ()
93+
%1 = alloc_box $Foo
94+
%1a = project_box %1 : $@box Foo
95+
%2 = function_ref @foo_allocating_init : $@convention(thin) (@thick Foo.Type) -> @owned Foo
96+
%3 = metatype $@thick Foo.Type
97+
%4 = apply %2(%3) : $@convention(thin) (@thick Foo.Type) -> @owned Foo
98+
store %4 to %1a : $*Foo
99+
%6 = alloc_box $Baz
100+
%6a = project_box %6 : $@box Baz
101+
%7 = function_ref @baz_init : $@convention(thin) (@thin Baz.Type) -> @owned Baz
102+
%8 = metatype $@thin Baz.Type
103+
%9 = apply %7(%8) : $@convention(thin) (@thin Baz.Type) -> @owned Baz
104+
store %9 to %6a : $*Baz
105+
%11 = alloc_box $Int
106+
%11a = project_box %11 : $@box Int
107+
%12 = function_ref @convert_from_integer_literal : $@convention(thin) (Builtin.Word, @thin Int.Type) -> Int
108+
%13 = metatype $@thin Int.Type
109+
%14 = integer_literal $Builtin.Word, 3
110+
%15 = apply %12(%14, %13) : $@convention(thin) (Builtin.Word, @thin Int.Type) -> Int
111+
store %15 to %11a : $*Int
112+
113+
%17 = function_ref @closure_indirect_result : $@convention(thin) (@owned @box Foo, @owned @box Baz, @owned @box Int) -> @out Int
114+
strong_retain %1 : $@box Foo
115+
strong_retain %6 : $@box Baz
116+
strong_retain %11 : $@box Int
117+
%21 = partial_apply %17(%1, %6, %11) : $@convention(thin) (@owned @box Foo, @owned @box Baz, @owned @box Int) -> @out Int
118+
119+
strong_release %11 : $@box Int
120+
strong_release %6 : $@box Baz
121+
strong_release %1 : $@box Foo
122+
123+
return %21 : $@callee_owned () -> @out Int
124+
}
125+
88126
// CHECK-LABEL: sil private @_TTSf2i_i_i__closure0 : $@convention(thin) (@owned Foo, @owned Baz, Int) -> Int
89127
// CHECK: [[DUMMY_FUNC:%.*]] = function_ref @dummy_func : $@convention(thin) (Int, Int, Int) -> Int
90128

@@ -215,3 +253,30 @@ bb0(%0 : $@box Int, %2 : $@box Int):
215253
}
216254

217255
sil [transparent] [fragile] @_TFsoi1pFTSiSi_Si : $@convention(thin) (Int, Int) -> Int
256+
257+
258+
sil private @closure_indirect_result : $@convention(thin) (@owned @box Foo, @owned @box Baz, @owned @box Int) -> @out Int {
259+
bb0(%0: $*Int, %1 : $@box Foo, %2 : $@box Baz, %4 : $@box Int):
260+
%17 = project_box %1 : $@box Foo
261+
%3 = project_box %2 : $@box Baz
262+
%5 = project_box %4 : $@box Int
263+
%6 = tuple ()
264+
// function_ref test14.plus (a : Swift.Int, b : Swift.Int, c : Swift.Int) -> Swift.Int
265+
%7 = function_ref @dummy_func : $@convention(thin) (Int, Int, Int) -> Int
266+
%8 = load %17 : $*Foo
267+
strong_retain %8 : $Foo
268+
%10 = class_method %8 : $Foo, #Foo.foo!1 : (Foo) -> () -> Int, $@convention(method) (@guaranteed Foo) -> Int
269+
%11 = apply %10(%8) : $@convention(method) (@guaranteed Foo) -> Int
270+
%12 = struct_element_addr %3 : $*Baz, #Baz.x
271+
%13 = load %12 : $*Int
272+
%14 = load %5 : $*Int
273+
%15 = apply %7(%11, %13, %14) : $@convention(thin) (Int, Int, Int) -> Int
274+
strong_release %4 : $@box Int
275+
strong_release %2 : $@box Baz
276+
strong_release %1 : $@box Foo
277+
store %15 to %0 : $*Int
278+
%16 = tuple()
279+
return %16 : $()
280+
}
281+
282+

0 commit comments

Comments
 (0)