Skip to content

SILCombine: We are not guaranteed to get a function_ref instruction here #25014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1362,7 +1362,11 @@ static bool knowHowToEmitReferenceCountInsts(ApplyInst *Call) {
if (Call->getNumArguments() != 1)
return false;

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

Expand Down
4 changes: 2 additions & 2 deletions lib/SILOptimizer/Utils/CastOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ CastOptimizer::optimizeBridgedObjCToSwiftCast(SILDynamicCastInst dynamicCast) {
// Now emit the a cast from the casted ObjC object into a target type.
// This is done by means of calling _forceBridgeFromObjectiveC or
// _conditionallyBridgeFromObjectiveC_bridgeable from the Target type.
auto *funcRef = Builder.createFunctionRef(Loc, bridgingFunc);
auto *funcRef = Builder.createFunctionRefFor(Loc, bridgingFunc);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createFunctionRefFor takes [dynamically_replacable] into account and generates either a function_ref or a dynamic_function_ref.

SubstitutionMap subMap = lookupBridgeToObjCProtocolSubs(mod, target);

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

SILBuilderWithScope Builder(Inst, builderContext);
auto FnRef = Builder.createFunctionRef(Loc, bridgedFunc);
auto FnRef = Builder.createFunctionRefFor(Loc, bridgedFunc);
auto ParamTypes = SubstFnTy.castTo<SILFunctionType>()->getParameters();
if (Src->getType().isAddress() && !substConv.isSILIndirect(ParamTypes[0])) {
// Create load
Expand Down
17 changes: 17 additions & 0 deletions test/SILOptimizer/sil_combine_objc_bridge.sil
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ bb0(%0 : $AnNSArray):
return %4 : $AnNSArray
}

sil [_semantics "convertFromObjectiveC"] [dynamically_replacable] @bridgeFromObjectiveC2 :
$@convention(thin) <τ_0_0> (@owned AnNSArray) -> @owned AnArray<τ_0_0>

sil [_semantics "convertToObjectiveC"] [dynamically_replacable] @bridgeToObjectiveC2:
$@convention(method) <τ_0_0> (@owned AnArray<τ_0_0>) -> @owned AnNSArray

// This should not crash.
sil @bridge_from_to_owned_dynamic : $@convention(thin) (@owned AnNSArray) -> @owned AnNSArray {
bb0(%0 : $AnNSArray):
%1 = dynamic_function_ref @bridgeFromObjectiveC2 : $@convention(thin) <AnyObject> (@owned AnNSArray) -> @owned AnArray<AnyObject>
%2 = apply %1<AnyObject>(%0) : $@convention(thin) <AnyObject> (@owned AnNSArray) -> @owned AnArray<AnyObject>
retain_value %2 : $AnArray<AnyObject>
release_value %2 : $AnArray<AnyObject>
%3 = dynamic_function_ref @bridgeToObjectiveC2 : $@convention(method) <AnyObject> (@owned AnArray<AnyObject>) -> @owned AnNSArray
%4 = apply %3<AnyObject>(%2) : $@convention(method) <AnyObject> (@owned AnArray<AnyObject>) -> @owned AnNSArray
return %4 : $AnNSArray
}
// CHECK-LABEL: sil @bridge_to_from_owned
// CHECK-NOT: apply
// CHECK: retain_value
Expand Down