Skip to content

Commit 697fd96

Browse files
authored
Merge pull request #7672 from eeckstein/capture-prop
CapturePropagation: handle convert_function and try_apply.
2 parents b023a48 + 465e343 commit 697fd96

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

lib/SILOptimizer/IPO/CapturePropagation.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class CapturePropagation : public SILFunctionTransform
5252
static LiteralInst *getConstant(SILValue V) {
5353
if (auto I = dyn_cast<ThinToThickFunctionInst>(V))
5454
return getConstant(I->getOperand());
55+
if (auto I = dyn_cast<ConvertFunctionInst>(V))
56+
return getConstant(I->getOperand());
5557
return dyn_cast<LiteralInst>(V);
5658
}
5759

@@ -282,8 +284,8 @@ static bool isProfitable(SILFunction *Callee) {
282284
SILBasicBlock *EntryBB = &*Callee->begin();
283285
for (auto *Arg : EntryBB->getArguments()) {
284286
for (auto *Operand : Arg->getUses()) {
285-
if (auto *AI = dyn_cast<ApplyInst>(Operand->getUser())) {
286-
if (AI->getCallee() == Operand->get())
287+
if (FullApplySite FAS = FullApplySite::isa(Operand->getUser())) {
288+
if (FAS.getCallee() == Operand->get())
287289
return true;
288290
}
289291
}

test/SILOptimizer/capture_propagation.sil

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,42 @@ bb0:
163163
return %2 : $@callee_owned (Int32, Int32) -> (Bool, @error Error)
164164
}
165165

166+
// Check if we can handle a non-throwing closure which is passed to a thunk accepting a throwing closure.
167+
168+
sil @simple_nonthrowing_closure : $@convention(thin) (Int32) -> Bool
169+
170+
// CHECK-LABEL: sil shared @{{.*}}throwing_thunk{{.*}} : $@convention(thin) (Int32) -> (Bool, @error Error) {
171+
// CHECK: = function_ref @simple_nonthrowing_closure
172+
// CHECK-NEXT: thin_to_thick_function
173+
// CHECK-NEXT: convert_function
174+
// CHECK-NEXT: try_apply
175+
// CHECK: return
176+
// CHECK: throw
177+
sil @throwing_thunk : $@convention(thin) (Int32, @owned @callee_owned (Int32) -> (Bool, @error Error)) -> (Bool, @error Error) {
178+
bb0(%0 : $Int32, %1 : $@callee_owned (Int32) -> (Bool, @error Error)):
179+
try_apply %1(%0) : $@callee_owned (Int32) -> (Bool, @error Error), normal bb1, error bb2
180+
181+
bb1(%5 : $Bool):
182+
return %5 : $Bool
183+
184+
bb2(%7 : $Error):
185+
throw %7 : $Error
186+
}
187+
188+
// CHECK-LABEL: sil @return_throwing_thunk_closure
189+
// CHECK: [[F:%[0-9]+]] = function_ref @{{.*}}throwing_thunk{{.*}} : $@convention(thin) (Int32) -> (Bool, @error Error)
190+
// CHECK: [[T:%[0-9]+]] = thin_to_thick_function [[F]]
191+
// CHECK: return [[T]]
192+
sil @return_throwing_thunk_closure : $@convention(thin) () -> @owned @callee_owned (Int32) -> (Bool, @error Error) {
193+
bb0:
194+
%c1 = function_ref @simple_nonthrowing_closure : $@convention(thin) (Int32) -> Bool
195+
%c2 = thin_to_thick_function %c1 : $@convention(thin) (Int32) -> Bool to $@callee_owned (Int32) -> Bool
196+
%c3 = convert_function %c2 : $@callee_owned (Int32) -> Bool to $@callee_owned (Int32) -> (Bool, @error Error)
197+
%t1 = function_ref @throwing_thunk : $@convention(thin) (Int32, @owned @callee_owned (Int32) -> (Bool, @error Error)) -> (Bool, @error Error)
198+
%2 = partial_apply %t1(%c3) : $@convention(thin) (Int32, @owned @callee_owned (Int32) -> (Bool, @error Error)) -> (Bool, @error Error)
199+
return %2 : $@callee_owned (Int32) -> (Bool, @error Error)
200+
}
201+
166202
// Negative tests
167203

168204
sil @swapped_arguments : $@convention(method) (Int32, Int32, @thin Int32.Type) -> Bool {

0 commit comments

Comments
 (0)