Skip to content

Commit 4985e0f

Browse files
committed
SILCombine: We are not guaranteed to get a function_ref instruction here
Also fix an issue in that cast optimizer and objc bridging thunks that are marked dynamic. Same test case tests both issues. rdar://51068786
1 parent 9d134a8 commit 4985e0f

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,11 @@ static bool knowHowToEmitReferenceCountInsts(ApplyInst *Call) {
13621362
if (Call->getNumArguments() != 1)
13631363
return false;
13641364

1365-
FunctionRefInst *FRI = cast<FunctionRefInst>(Call->getCallee());
1365+
// FIXME: We could handle dynamic_function_ref instructions here because the
1366+
// code only looks at the function type.
1367+
FunctionRefInst *FRI = dyn_cast<FunctionRefInst>(Call->getCallee());
1368+
if (!FRI)
1369+
return false;
13661370
SILFunction *F = FRI->getReferencedFunction();
13671371
auto FnTy = F->getLoweredFunctionType();
13681372

lib/SILOptimizer/Utils/CastOptimizer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ CastOptimizer::optimizeBridgedObjCToSwiftCast(SILDynamicCastInst dynamicCast) {
271271
// Now emit the a cast from the casted ObjC object into a target type.
272272
// This is done by means of calling _forceBridgeFromObjectiveC or
273273
// _conditionallyBridgeFromObjectiveC_bridgeable from the Target type.
274-
auto *funcRef = Builder.createFunctionRef(Loc, bridgingFunc);
274+
auto *funcRef = Builder.createFunctionRefFor(Loc, bridgingFunc);
275275
SubstitutionMap subMap = lookupBridgeToObjCProtocolSubs(mod, target);
276276

277277
auto MetaTy = MetatypeType::get(target, MetatypeRepresentation::Thick);
@@ -588,7 +588,7 @@ CastOptimizer::optimizeBridgedSwiftToObjCCast(SILDynamicCastInst dynamicCast) {
588588
}
589589

590590
SILBuilderWithScope Builder(Inst, builderContext);
591-
auto FnRef = Builder.createFunctionRef(Loc, bridgedFunc);
591+
auto FnRef = Builder.createFunctionRefFor(Loc, bridgedFunc);
592592
auto ParamTypes = SubstFnTy.castTo<SILFunctionType>()->getParameters();
593593
if (Src->getType().isAddress() && !substConv.isSILIndirect(ParamTypes[0])) {
594594
// Create load

test/SILOptimizer/sil_combine_objc_bridge.sil

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ bb0(%0 : $AnNSArray):
6464
return %4 : $AnNSArray
6565
}
6666

67+
sil [_semantics "convertFromObjectiveC"] [dynamically_replacable] @bridgeFromObjectiveC2 :
68+
$@convention(thin) <τ_0_0> (@owned AnNSArray) -> @owned AnArray<τ_0_0>
69+
70+
sil [_semantics "convertToObjectiveC"] [dynamically_replacable] @bridgeToObjectiveC2:
71+
$@convention(method) <τ_0_0> (@owned AnArray<τ_0_0>) -> @owned AnNSArray
72+
73+
// This should not crash.
74+
sil @bridge_from_to_owned_dynamic : $@convention(thin) (@owned AnNSArray) -> @owned AnNSArray {
75+
bb0(%0 : $AnNSArray):
76+
%1 = dynamic_function_ref @bridgeFromObjectiveC2 : $@convention(thin) <AnyObject> (@owned AnNSArray) -> @owned AnArray<AnyObject>
77+
%2 = apply %1<AnyObject>(%0) : $@convention(thin) <AnyObject> (@owned AnNSArray) -> @owned AnArray<AnyObject>
78+
retain_value %2 : $AnArray<AnyObject>
79+
release_value %2 : $AnArray<AnyObject>
80+
%3 = dynamic_function_ref @bridgeToObjectiveC2 : $@convention(method) <AnyObject> (@owned AnArray<AnyObject>) -> @owned AnNSArray
81+
%4 = apply %3<AnyObject>(%2) : $@convention(method) <AnyObject> (@owned AnArray<AnyObject>) -> @owned AnNSArray
82+
return %4 : $AnNSArray
83+
}
6784
// CHECK-LABEL: sil @bridge_to_from_owned
6885
// CHECK-NOT: apply
6986
// CHECK: retain_value

0 commit comments

Comments
 (0)