Skip to content

Commit 534c35b

Browse files
committed
[PackageCMO] Optimize witness thunks.
- Keep witness thunk linkage private for a package protocol member in SILGen. - Optimize private/hidden functions during Package CMO; if they don't contain references that have private/hidden symbols, serialize them and set the linkage to shared. For unserialized witness thunks, set the linkage to package, so the witness table itself can be serialized. - Update witness table and vtable serialization. Resolves rdar://129976582
1 parent 5e810ea commit 534c35b

8 files changed

+394
-244
lines changed

lib/SIL/IR/Linker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void SILLinkerVisitor::maybeAddFunctionToWorklist(
113113
if (callerSerializedKind == IsSerialized &&
114114
hasSharedVisibility(linkage) &&
115115
!Mod.isSerialized() &&
116-
!F->isSerialized()) {
116+
!F->isAnySerialized()) {
117117
F->setSerializedKind(IsSerialized);
118118

119119
// Push the function to the worklist so that all referenced shared functions

lib/SIL/IR/SILWitnessTable.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ SILWitnessTable::SILWitnessTable(
108108
ArrayRef<ConditionalConformance> conditionalConformances)
109109
: Mod(M), Name(N), Linkage(Linkage), Conformance(Conformance), Entries(),
110110
ConditionalConformances(), IsDeclaration(true),
111-
SerializedKind(IsNotSerialized) {
111+
SerializedKind(SerializedKind) {
112112
convertToDefinition(entries, conditionalConformances, SerializedKind);
113113
}
114114

@@ -169,21 +169,15 @@ void SILWitnessTable::convertToDefinition(
169169

170170
SerializedKind_t SILWitnessTable::conformanceSerializedKind(
171171
const RootProtocolConformance *conformance) {
172-
// Allow serializing conformance with package or public access level
173-
// if package serialization is enabled.
174-
auto optInPackage = conformance->getDeclContext()->getParentModule()->serializePackageEnabled();
175-
auto accessLevelToCheck =
176-
optInPackage ? AccessLevel::Package : AccessLevel::Public;
177-
178172
auto normalConformance = dyn_cast<NormalProtocolConformance>(conformance);
179-
if (normalConformance && normalConformance->isResilient() && !optInPackage)
173+
if (normalConformance && normalConformance->isResilient())
180174
return IsNotSerialized;
181175

182-
if (conformance->getProtocol()->getEffectiveAccess() < accessLevelToCheck)
176+
if (conformance->getProtocol()->getEffectiveAccess() < AccessLevel::Public)
183177
return IsNotSerialized;
184178

185179
auto *nominal = conformance->getDeclContext()->getSelfNominalTypeDecl();
186-
if (nominal->getEffectiveAccess() >= accessLevelToCheck)
180+
if (nominal->getEffectiveAccess() >= AccessLevel::Public)
187181
return IsSerialized;
188182

189183
return IsNotSerialized;

lib/SILOptimizer/IPO/CrossModuleOptimization.cpp

Lines changed: 105 additions & 88 deletions
Large diffs are not rendered by default.

test/SILOptimizer/package-cmo-non-resilient-mode.swift

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,34 @@
1111
import Module
1212
import ModuleTBD
1313

14+
// static ModuleStruct.privateFunctionPointer
1415
// CHECK-LABEL: sil_global public_external @$s6Module0A6StructV22privateFunctionPointeryS2icvpZ : $@callee_guaranteed (Int) -> Int{{$}}
16+
17+
// static ModuleStruct.publicFunctionPointer
18+
// CHECK-LABEL: sil_global public_external [serialized] @$s6Module0A6StructV21publicFunctionPointeryS2icvpZ : $@callee_guaranteed (Int) -> Int
19+
20+
21+
// CHECK-LABEL: sil @$s4Main26callPrivateFunctionPointeryS2iF : $@convention(thin) (Int) -> Int {
22+
// CHECK: global_addr @$s6Module0A6StructV22privateFunctionPointeryS2icvpZ
23+
// CHECK: load
24+
// CHECK: apply
1525
public func callPrivateFunctionPointer(_ x: Int) -> Int {
1626
return Module.ModuleStruct.privateFunctionPointer(x)
1727
}
1828

19-
// CHECK-LABEL: sil_global package_external @$s6Module03PkgA6StructV14closurePointeryS2icvpZ : $@callee_guaranteed (Int) -> Int{{$}}
29+
// CHECK-LABEL: sil package @$s4Main27callStaticPkgClosurePointeryS2iF : $@convention(thin) (Int) -> Int {
30+
// CHECK: function_ref @$s6Module03PkgA6StructV14closurePointeryS2icvau
31+
// CHECK: apply
32+
// CHECK: pointer_to_address
33+
// CHECK: load
34+
// CHECK: apply
35+
36+
// PkgModuleStruct.closurePointer.unsafeMutableAddressor
37+
// CHECK: sil package_external [global_init] @$s6Module03PkgA6StructV14closurePointeryS2icvau : $@convention(thin) () -> Builtin.RawPointer
2038
package func callStaticPkgClosurePointer(_ x: Int) -> Int {
2139
return Module.PkgModuleStruct.closurePointer(x)
2240
}
2341

24-
// static ModuleStruct.publicFunctionPointer
25-
// CHECK-LABEL: sil_global public_external [serialized] @$s6Module0A6StructV21publicFunctionPointeryS2icvpZ : $@callee_guaranteed (Int) -> Int
26-
// static PkgModuleStruct.funcPointer
27-
// CHECK-LABEL: sil_global package_external [serialized] @$s6Module03PkgA6StructV11funcPointeryS2icvpZ : $@callee_guaranteed (Int) -> Int
28-
29-
3042
// CHECK-LABEL: sil @$s4Main25callPublicFunctionPointeryS2iF : $@convention(thin) (Int) -> Int {
3143
// CHECK: global_addr @$s6Module0A6StructV21publicFunctionPointeryS2icvpZ : $*@callee_guaranteed (Int) -> Int
3244
// CHECK: load
@@ -37,10 +49,14 @@ public func callPublicFunctionPointer(_ x: Int) -> Int {
3749
}
3850

3951
// CHECK-LABEL: sil package @$s4Main28callStaticPkgFunctionPointeryS2iF : $@convention(thin) (Int) -> Int {
40-
// CHECK: global_addr @$s6Module03PkgA6StructV11funcPointeryS2icvpZ : $*@callee_guaranteed (Int) -> Int
52+
// CHECK: function_ref @$s6Module03PkgA6StructV11funcPointeryS2icvau
53+
// CHECK: pointer_to_address
4154
// CHECK: load
4255
// CHECK: apply
4356
// CHECK: } // end sil function '$s4Main28callStaticPkgFunctionPointeryS2iF'
57+
58+
// PkgModuleStruct.funcPointer.unsafeMutableAddressor
59+
// CHECK-LABEL: sil package_external [global_init] @$s6Module03PkgA6StructV11funcPointeryS2icvau : $@convention(thin) () -> Builtin.RawPointer
4460
package func callStaticPkgFunctionPointer(_ x: Int) -> Int {
4561
return Module.PkgModuleStruct.funcPointer(x)
4662
}
@@ -68,9 +84,12 @@ public func doIncrement(_ x: Int) -> Int {
6884
}
6985

7086
// CHECK-LABEL: sil package @$s4Main11callPkgFuncyS2iF : $@convention(thin) (Int) -> Int {
71-
// CHECK-NOT: function_ref
72-
// CHECK-NOT: apply
87+
// CHECK: function_ref @$s6Module7pkgFuncyS2iF
88+
// CHECK: apply
7389
// CHECK: } // end sil function '$s4Main11callPkgFuncyS2iF'
90+
91+
// pkgFunc(_:)
92+
// CHECK-LABEL: sil package_external @$s6Module7pkgFuncyS2iF : $@convention(thin) (Int) -> Int
7493
package func callPkgFunc(_ x: Int) -> Int {
7594
return Module.pkgFunc(x)
7695
}
@@ -83,8 +102,10 @@ public func doIncrementWithCall(_ x: Int) -> Int {
83102
}
84103

85104
// CHECK-LABEL: sil package @$s4Main16callPkgFuncNoCMOyS2iF : $@convention(thin) (Int) -> Int {
86-
// CHECK: function_ref @$s9Submodule15subPkgFuncNoCMOyS2iF
105+
// CHECK: function_ref @$s6Module12pkgFuncNoCMOyS2iF
87106
// CHECK: } // end sil function '$s4Main16callPkgFuncNoCMOyS2iF'
107+
// pkgFuncNoCMO(_:)
108+
// CHECK-LABEL: sil package_external @$s6Module12pkgFuncNoCMOyS2iF : $@convention(thin) (Int) -> Int
88109
package func callPkgFuncNoCMO(_ x: Int) -> Int {
89110
return Module.pkgFuncNoCMO(x)
90111
}
@@ -98,9 +119,11 @@ public func doIncrementTBD(_ x: Int) -> Int {
98119
}
99120

100121
// CHECK-LABEL: sil package @$s4Main14callPkgFuncTBDyS2iF : $@convention(thin) (Int) -> Int {
101-
// CHECK-NOT: function_ref
102-
// CHECK-NOT: apply
122+
// CHECK: function_ref @$s9ModuleTBD7pkgFuncyS2iF
123+
// CHECK: apply
103124
// CHECK: } // end sil function '$s4Main14callPkgFuncTBDyS2iF'
125+
// pkgFunc(_:)
126+
// CHECK-LABEL: sil package_external @$s9ModuleTBD7pkgFuncyS2iF : $@convention(thin) (Int) -> Int
104127
package func callPkgFuncTBD(_ x: Int) -> Int {
105128
return ModuleTBD.pkgFunc(x)
106129
}
@@ -130,9 +153,11 @@ public func getSubmoduleKlassMember() -> Int {
130153
}
131154

132155
// CHECK-LABEL: sil package @$s4Main26getPkgSubmoduleKlassMemberSiyF : $@convention(thin) () -> Int {
133-
// CHECK-NOT: function_ref
134-
// CHECK-NOT: apply
156+
// CHECK: function_ref @$s6Module23pkgSubmoduleKlassMemberSiyF
157+
// CHECK: apply
135158
// CHECK: } // end sil function '$s4Main26getPkgSubmoduleKlassMemberSiyF'
159+
// pkgSubmoduleKlassMember()
160+
// CHECK-LABEL: sil package_external @$s6Module23pkgSubmoduleKlassMemberSiyF : $@convention(thin) () -> Int
136161
package func getPkgSubmoduleKlassMember() -> Int {
137162
return Module.pkgSubmoduleKlassMember()
138163
}
@@ -146,9 +171,12 @@ public func getSubmoduleKlassMemberTBD() -> Int {
146171
}
147172

148173
// CHECK-LABEL: sil package @$s4Main29getPkgSubmoduleKlassMemberTBDSiyF : $@convention(thin) () -> Int {
149-
// CHECK-NOT: function_ref
150-
// CHECK-NOT: apply
174+
// CHECK: function_ref @$s9ModuleTBD23pkgSubmoduleKlassMemberSiyF
175+
// CHECK: apply
151176
// CHECK: } // end sil function '$s4Main29getPkgSubmoduleKlassMemberTBDSiyF'
177+
178+
// pkgSubmoduleKlassMember()
179+
// CHECK-LABEL:sil package_external @$s9ModuleTBD23pkgSubmoduleKlassMemberSiyF : $@convention(thin) () -> Int
152180
package func getPkgSubmoduleKlassMemberTBD() -> Int {
153181
return ModuleTBD.pkgSubmoduleKlassMember()
154182
}
@@ -162,9 +190,11 @@ public func getModuleKlassMember() -> Int {
162190
}
163191

164192
// CHECK-LABEL: sil package @$s4Main23getPkgModuleKlassMemberSiyF : $@convention(thin) () -> Int {
165-
// CHECK-NOT: function_ref
166-
// CHECK-NOT: apply
193+
// CHECK: function_ref @$s6Module03pkgA11KlassMemberSiyF
194+
// CHECK: apply
167195
// CHECK: } // end sil function '$s4Main23getPkgModuleKlassMemberSiyF'
196+
// pkgModuleKlassMember()
197+
// CHECK-LABEL: sil package_external @$s6Module03pkgA11KlassMemberSiyF : $@convention(thin) () -> Int
168198
package func getPkgModuleKlassMember() -> Int {
169199
return Module.pkgModuleKlassMember()
170200
}
@@ -178,14 +208,14 @@ public func getModuleKlassMemberTBD() -> Int {
178208
}
179209

180210
// CHECK-LABEL: sil package @$s4Main26getPkgModuleKlassMemberTBDSiyF : $@convention(thin) () -> Int {
181-
// CHECK-NOT: function_ref
182-
// CHECK-NOT: apply
211+
// CHECK: function_ref @$s9ModuleTBD03pkgA11KlassMemberSiyF
212+
// CHECK: apply
183213
// CHECK: } // end sil function '$s4Main26getPkgModuleKlassMemberTBDSiyF'
214+
// pkgModuleKlassMember()
215+
// CHECK-LABEL: sil package_external @$s9ModuleTBD03pkgA11KlassMemberSiyF : $@convention(thin) () -> Int
184216
package func getPkgModuleKlassMemberTBD() -> Int {
185217
return ModuleTBD.pkgModuleKlassMember()
186218
}
187219

188220

189221
// CHECK-LABEL: sil [_semantics "optimize.no.crossmodule"] @$s9Submodule19incrementByOneNoCMOyS2iF : $@convention(thin) (Int) -> Int
190-
191-
// CHECK-LABEL: sil package_external [_semantics "optimize.no.crossmodule"] @$s9Submodule15subPkgFuncNoCMOyS2iF : $@convention(thin) (Int) -> Int

0 commit comments

Comments
 (0)