Skip to content

Commit a6a75f0

Browse files
committed
GenericSpecializer: fix a crash with -cross-module-optimization
If a thunk needs to be generated for a partial_apply specialization, but the specialization doesn't have a body, bail. rdar://107165121
1 parent 5ef25e7 commit a6a75f0

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/SILOptimizer/Utils/Generics.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,6 +3119,12 @@ void swift::trySpecializeApplyOfGeneric(
31193119
<< SpecializedF->getLoweredFunctionType() << "\n");
31203120
NewFunctions.push_back(SpecializedF.getFunction());
31213121
}
3122+
if (replacePartialApplyWithoutReabstraction &&
3123+
SpecializedF.getFunction()->isExternalDeclaration()) {
3124+
// Cannot create a tunk without having the body of the function.
3125+
return;
3126+
}
3127+
31223128
if (F->isSerialized() && !SpecializedF->hasValidLinkageForFragileInline()) {
31233129
// If the specialized function already exists as a "IsNotSerialized" function,
31243130
// but now it's called from a "IsSerialized" function, we need to mark it as

test/SILOptimizer/specialize.sil

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,3 +726,34 @@ bb0:
726726
return %t : $()
727727

728728
}
729+
730+
sil shared @closure : $@convention(thin) <T> (@in_guaranteed T) -> () {
731+
bb0(%0 : $*T):
732+
%5 = tuple ()
733+
return %5 : $()
734+
}
735+
736+
sil @getExistingExternalSpecialization : $@convention(thin) <T> (@in_guaranteed T) -> @owned @callee_owned () -> () {
737+
bb0(%0 : $*T):
738+
%2 = function_ref @closure : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> ()
739+
%5 = partial_apply %2<T>(%0) : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> ()
740+
return %5 : $@callee_owned () -> ()
741+
}
742+
743+
// Make sure we don't crash here
744+
// CHECK-LABEL: sil @testExistingExternalSpecialization :
745+
// CHECK: function_ref @$s33getExistingExternalSpecializations5UInt8V_Tg5
746+
// CHECK: } // end sil function 'testExistingExternalSpecialization'
747+
sil @testExistingExternalSpecialization : $@convention(thin) () -> () {
748+
bb0:
749+
%0 = alloc_stack $UInt8
750+
%1 = integer_literal $Builtin.Int8, 5
751+
%2 = struct $UInt8 (%1 : $Builtin.Int8)
752+
store %2 to %0 : $*UInt8
753+
%5 = function_ref @getExistingExternalSpecialization : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> @owned @callee_owned () -> ()
754+
%8 = apply %5<UInt8>(%0) : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> @owned @callee_owned () -> ()
755+
dealloc_stack %0 : $*UInt8
756+
%r = tuple ()
757+
return %r : $()
758+
}
759+

0 commit comments

Comments
 (0)