Skip to content

Commit 8f3d26d

Browse files
authored
Merge pull request #3257 from aschwaighofer/specialize_closure_with_indirect_results
2 parents 26ade95 + 4ab10c5 commit 8f3d26d

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

lib/SILOptimizer/IPO/ClosureSpecializer.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,12 +475,11 @@ static bool isSupportedClosure(const SILInstruction *Closure) {
475475
}
476476

477477
// Make sure that it is a simple partial apply (i.e. its callee is a
478-
// function_ref). We also do not handle indirect results currently in the
479-
// closure so make sure that does not happen at this point.
478+
// function_ref).
480479
//
481480
// TODO: We can probably handle other partial applies here.
482481
auto *FRI = dyn_cast<FunctionRefInst>(Callee);
483-
if (!FRI || FRI->getFunctionType()->hasIndirectResults())
482+
if (!FRI)
484483
return false;
485484

486485
// Otherwise, we do support specializing this closure.

test/SILOptimizer/closure_specialize_simple.sil

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,25 +141,34 @@ bb2:
141141
return %9999 : $()
142142
}
143143

144-
sil @indirect_parameter_partial_apply_caller6 : $@convention(thin) (@callee_owned () -> ()) -> @out Builtin.Int1 {
145-
bb0(%1 : $*Builtin.Int1, %0 : $@callee_owned () -> ()):
144+
sil @indirect_parameter_partial_apply_caller6 : $@convention(thin) (@callee_owned () -> @out Builtin.Int1) -> @out Builtin.Int1 {
145+
bb0(%1 : $*Builtin.Int1, %0 : $@callee_owned () -> @out Builtin.Int1):
146146
br bb1
147147

148148
bb1:
149-
apply %0() : $@callee_owned () -> ()
149+
apply %0(%1) : $@callee_owned () -> @out Builtin.Int1
150150
cond_br undef, bb1, bb2
151151

152152
bb2:
153153
%9999 = tuple()
154154
return %9999 : $()
155155
}
156156

157-
sil @indirect_parameter_partial_apply_caller7 : $@convention(thin) (@callee_owned () -> ()) -> @out (Builtin.Int1, Builtin.Int1) {
158-
bb0(%1 : $*(Builtin.Int1, Builtin.Int1), %0 : $@callee_owned () -> ()):
157+
sil @indirect_parameter_partial_apply_caller7 : $@convention(thin) (@callee_owned () -> @out Builtin.Int1) -> @out (Builtin.Int1, Builtin.Int1) {
158+
bb0(%1 : $*(Builtin.Int1, Builtin.Int1), %0 : $@callee_owned () -> @out Builtin.Int1):
159159
br bb1
160160

161161
bb1:
162-
apply %0() : $@callee_owned () -> ()
162+
%2 = alloc_stack $Builtin.Int1
163+
%3 = alloc_stack $Builtin.Int1
164+
apply %0(%2) : $@callee_owned () -> @out Builtin.Int1
165+
apply %0(%3) : $@callee_owned () -> @out Builtin.Int1
166+
%4 = load %2: $*Builtin.Int1
167+
%5 = load %3: $*Builtin.Int1
168+
%6 = tuple(%4 : $Builtin.Int1, %5: $Builtin.Int1)
169+
store %6 to %1 : $*(Builtin.Int1, Builtin.Int1)
170+
dealloc_stack %3: $*Builtin.Int1
171+
dealloc_stack %2: $*Builtin.Int1
163172
cond_br undef, bb1, bb2
164173

165174
bb2:
@@ -174,6 +183,34 @@ bb2:
174183
// We can't call this one b/c it is just a declaration.
175184
// CHECK: [[UNSPECIALIZED_FUN_DECL:%.*]] = function_ref @simple_partial_apply_caller_decl : $@convention(thin) (@owned @callee_owned (Builtin.Int1) -> Builtin.Int1) -> Builtin.Int1
176185
// CHECK: apply [[UNSPECIALIZED_FUN_DECL]]
186+
187+
// We handle closures with indirect results.
188+
// CHECK: [[CLOSUREFUN:%.*]] = function_ref @indirect_parameter_partial_apply_fun
189+
// CHECK-NOT: partial_apply [[CLOSUREFUN]]()
190+
// CHECK: [[INLINEDCLOSURE_CALLER1:%.*]] = function_ref @{{.*}}indirect_parameter_partial_apply_fun__indirect_parameter_partial_apply_caller1
191+
// CHECK-NOT: partial_apply [[CLOSUREFUN]]()
192+
193+
// We don't handle captured indirect parameters yet.
194+
// CHECK: [[CLOSURE2:%.*]] = partial_apply [[CLOSUREFUN]](%{{.*}})
195+
// CHECK: [[CLOSURE3:%.*]] = partial_apply [[CLOSUREFUN]](%{{.*}})
196+
// CHECK: [[CLOSURE4:%.*]] = partial_apply [[CLOSUREFUN]](%{{.*}})
197+
198+
// CHECK: [[CALLER1:%.*]] = function_ref @indirect_parameter_partial_apply_caller1
199+
// CHECK: [[CALLER2:%.*]] = function_ref @indirect_parameter_partial_apply_caller2
200+
// CHECK: [[CALLER3:%.*]] = function_ref @indirect_parameter_partial_apply_caller3
201+
// CHECK: [[CALLER4:%.*]] = function_ref @indirect_parameter_partial_apply_caller4
202+
203+
// Closure with indirect result but no captured indirect parameter.
204+
// CHECK-NOT: apply [[CALLER1]]
205+
// apply [[INLINEDCLOSURE_CALLER1]]()
206+
// CHECK-NOT: apply [[CALLER1]]
207+
208+
// Closures with captured indirect parameters.
209+
// apply [[CALLER2]]([[CLOSURE2]])
210+
// apply [[CALLER3]]([[CLOSURE3]])
211+
// apply [[CALLER4]]([[CLOSURE4]])
212+
213+
// CHECK: return
177214
sil @loop_driver : $@convention(thin) (Builtin.Int1, Builtin.Int1) -> () {
178215
bb0(%0 : $Builtin.Int1, %1 : $Builtin.Int1):
179216
%2 = function_ref @simple_partial_apply_fun : $@convention(thin) (Builtin.Int1, Builtin.Int1) -> Builtin.Int1
@@ -203,16 +240,15 @@ bb0(%0 : $Builtin.Int1, %1 : $Builtin.Int1):
203240
apply %17(%12) : $@convention(thin) (@callee_owned (@in Builtin.Int1, Builtin.Int1) -> @out Builtin.Int1) -> ()
204241
apply %18(%13) : $@convention(thin) (@callee_owned (@in Builtin.Int1) -> @out Builtin.Int1) -> ()
205242
apply %19(%14) : $@convention(thin) (@callee_owned () -> @out Builtin.Int1) -> ()
206-
//apply %20(%15) : $@convention(thin) (@callee_owned () -> ()) -> ()
207243

208244
// Make sure we handle when we already have an out parameter correctly.
209245
%21 = alloc_stack $(Builtin.Int1, Builtin.Int1)
210-
%22 = function_ref @indirect_parameter_partial_apply_caller6 : $@convention(thin) (@callee_owned () -> ()) -> @out Builtin.Int1
211-
%23 = function_ref @indirect_parameter_partial_apply_caller7 : $@convention(thin) (@callee_owned () -> ()) -> @out (Builtin.Int1, Builtin.Int1)
246+
%22 = function_ref @indirect_parameter_partial_apply_caller6 : $@convention(thin) (@callee_owned () -> @out Builtin.Int1) -> @out Builtin.Int1
247+
%23 = function_ref @indirect_parameter_partial_apply_caller7 : $@convention(thin) (@callee_owned () -> @out Builtin.Int1) -> @out (Builtin.Int1, Builtin.Int1)
212248
%24 = partial_apply %10(%9, %1, %9) : $@convention(thin) (@in Builtin.Int1, Builtin.Int1, @in Builtin.Int1) -> @out Builtin.Int1
213249
%25 = partial_apply %10(%9, %1, %9) : $@convention(thin) (@in Builtin.Int1, Builtin.Int1, @in Builtin.Int1) -> @out Builtin.Int1
214-
//apply %22(%9, %24) : $@convention(thin) (@callee_owned () -> ()) -> @out Builtin.Int1
215-
//apply %23(%21, %25) : $@convention(thin) (@callee_owned () -> ()) -> @out (Builtin.Int1, Builtin.Int1)
250+
apply %22(%9, %24) : $@convention(thin) (@callee_owned () -> @out Builtin.Int1) -> @out Builtin.Int1
251+
apply %23(%21, %25) : $@convention(thin) (@callee_owned () -> @out Builtin.Int1) -> @out (Builtin.Int1, Builtin.Int1)
216252

217253
dealloc_stack %21 : $*(Builtin.Int1, Builtin.Int1)
218254
dealloc_stack %9 : $*Builtin.Int1

0 commit comments

Comments
 (0)