Skip to content

Commit 75a7e4d

Browse files
Merge pull request #25015 from aschwaighofer/fix_combine_dynamic_function_ref-5.1
[5.1] SILCombine: We are not guaranteed to get a function_ref instruc…
2 parents 2d27db6 + 13e9e01 commit 75a7e4d

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
@@ -1176,7 +1176,11 @@ static bool knowHowToEmitReferenceCountInsts(ApplyInst *Call) {
11761176
if (Call->getNumArguments() != 1)
11771177
return false;
11781178

1179-
FunctionRefInst *FRI = cast<FunctionRefInst>(Call->getCallee());
1179+
// FIXME: We could handle dynamic_function_ref instructions here because the
1180+
// code only looks at the function type.
1181+
FunctionRefInst *FRI = dyn_cast<FunctionRefInst>(Call->getCallee());
1182+
if (!FRI)
1183+
return false;
11801184
SILFunction *F = FRI->getReferencedFunction();
11811185
auto FnTy = F->getLoweredFunctionType();
11821186

lib/SILOptimizer/Utils/CastOptimizer.cpp

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

196196
auto MetaTy = MetatypeType::get(target, MetatypeRepresentation::Thick);
@@ -430,7 +430,7 @@ CastOptimizer::optimizeBridgedSwiftToObjCCast(SILDynamicCastInst dynamicCast) {
430430
}
431431

432432
SILBuilderWithScope Builder(Inst, BuilderContext);
433-
auto FnRef = Builder.createFunctionRef(Loc, bridgedFunc);
433+
auto FnRef = Builder.createFunctionRefFor(Loc, bridgedFunc);
434434
auto ParamTypes = SubstFnTy.castTo<SILFunctionType>()->getParameters();
435435
if (Src->getType().isAddress() && !substConv.isSILIndirect(ParamTypes[0])) {
436436
// 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)