Skip to content

Commit edb08da

Browse files
committed
GenericSpecializer: fix the function convention if specialization removes the self parameter of a method
When dropping the self metatype parameter of a method, it must become a "thin" function. Fixes a crash when using performance annotations. rdar://107202455
1 parent 3e5347c commit edb08da

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

lib/SILOptimizer/Utils/Generics.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,12 +1025,16 @@ createSpecializedType(CanSILFunctionType SubstFTy, SILModule &M) const {
10251025
SpecializedResults.push_back(RI);
10261026
}
10271027
unsigned idx = 0;
1028+
bool removedSelfParam = false;
10281029
for (SILParameterInfo PI : SubstFTy->getParameters()) {
10291030
unsigned paramIdx = idx++;
10301031
PI = PI.getUnsubstituted(M, SubstFTy, context);
10311032

1032-
if (isDroppedMetatypeArg(param2ArgIndex(paramIdx)))
1033+
if (isDroppedMetatypeArg(param2ArgIndex(paramIdx))) {
1034+
if (SubstFTy->hasSelfParam() && paramIdx == SubstFTy->getParameters().size() - 1)
1035+
removedSelfParam = true;
10331036
continue;
1037+
}
10341038

10351039
bool isTrivial = TrivialArgs.test(param2ArgIndex(paramIdx));
10361040
if (!isParamConverted(paramIdx)) {
@@ -1061,8 +1065,15 @@ createSpecializedType(CanSILFunctionType SubstFTy, SILModule &M) const {
10611065
auto Signature = SubstFTy->isPolymorphic()
10621066
? SubstFTy->getInvocationGenericSignature()
10631067
: CanGenericSignature();
1068+
1069+
SILFunctionType::ExtInfo extInfo = SubstFTy->getExtInfo();
1070+
if (extInfo.hasSelfParam() && removedSelfParam) {
1071+
extInfo = extInfo.withRepresentation(SILFunctionTypeRepresentation::Thin);
1072+
assert(!extInfo.hasSelfParam());
1073+
}
1074+
10641075
return SILFunctionType::get(
1065-
Signature, SubstFTy->getExtInfo(),
1076+
Signature, extInfo,
10661077
SubstFTy->getCoroutineKind(), SubstFTy->getCalleeConvention(),
10671078
SpecializedParams, SpecializedYields, SpecializedResults,
10681079
SubstFTy->getOptionalErrorResult(), SubstitutionMap(), SubstitutionMap(),
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %target-swift-frontend -experimental-performance-annotations %s -sil-verify-all -module-name=test -emit-sil | %FileCheck %s
2+
3+
// REQUIRES: swift_in_compiler,swift_stdlib_no_asserts,optimized_stdlib
4+
// UNSUPPORTED: swift_test_mode_optimize
5+
6+
public struct Stack<T> {
7+
var size = 42
8+
}
9+
10+
// CHECK-LABEL: sil [no_allocation] @$s4test11createStackyyF :
11+
// CHECK: [[F:%[0-9]+]] = function_ref @$s4test5StackVACyxGycfCSi_Tgm5
12+
// CHECK: [[S:%[0-9]+]] = apply [[F]]()
13+
// CHECK: debug_value [[S]]
14+
// CHECK: } // end sil function '$s4test11createStackyyF'
15+
@_noAllocation
16+
public func createStack() {
17+
let s = Stack<Int>()
18+
_ = s.size
19+
}
20+

0 commit comments

Comments
 (0)