Skip to content

Commit f2ae567

Browse files
committed
Update global var serialization for package cmo
1 parent 7097ffb commit f2ae567

File tree

7 files changed

+38
-22
lines changed

7 files changed

+38
-22
lines changed

include/swift/SIL/SILGlobalVariable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ class SILGlobalVariable
154154
/// Check if this global variable is [serialized]. This does not check
155155
/// if it's [serialized_for_package].
156156
bool isSerialized() const;
157+
/// Check if this global variable is [serialized_for_package].
158+
bool isSerializedForPackage() const;
157159
/// Checks whether this global var is neither [serialized] nor
158160
/// [serialized_for_package].
159161
bool isNotSerialized() const;

lib/SIL/IR/SILGlobalVariable.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ bool SILGlobalVariable::shouldBePreservedForDebugger() const {
7878
bool SILGlobalVariable::isSerialized() const {
7979
return SerializedKind_t(Serialized) == IsSerialized;
8080
}
81+
bool SILGlobalVariable::isSerializedForPackage() const {
82+
return SerializedKind_t(Serialized) == IsSerializedForPackage;
83+
}
8184
bool SILGlobalVariable::isNotSerialized() const {
8285
return SerializedKind_t(Serialized) == IsNotSerialized;
8386
}

lib/SILGen/SILGenType.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,7 @@ class SILGenVTable : public SILVTableVisitor<SILGenVTable> {
256256

257257
SILGenVTable(SILGenModule &SGM, ClassDecl *theClass)
258258
: SGM(SGM), theClass(theClass) {
259-
isResilient = theClass->isResilient() &&
260-
!theClass->getModuleContext()->allowNonResilientAccess();
259+
isResilient = theClass->isResilient();
261260
}
262261

263262
void emitVTable() {

lib/SILOptimizer/IPO/CrossModuleOptimization.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,11 @@ static bool isSerializedWithRightKind(const SILModule &mod,
202202
return mod.getSwiftModule()->serializePackageEnabled() ?
203203
f->isSerializedForPackage() : f->isSerialized();
204204
}
205-
205+
static bool isSerializedWithRightKind(const SILModule &mod,
206+
SILGlobalVariable *g) {
207+
return mod.getSwiftModule()->serializePackageEnabled() ?
208+
g->isSerializedForPackage() : g->isSerialized();
209+
}
206210
static SerializedKind_t getRightSerializedKind(const SILModule &mod) {
207211
return mod.getSwiftModule()->serializePackageEnabled() ?
208212
IsSerializedForPackage : IsSerialized;
@@ -235,7 +239,7 @@ static bool isReferenceSerializeCandidate(SILFunction *F, SILOptions options) {
235239
static bool isReferenceSerializeCandidate(SILGlobalVariable *G,
236240
SILOptions options) {
237241
if (options.EnableSerializePackage) {
238-
if (G->isSerialized())
242+
if (isSerializedWithRightKind(G->getModule(), G))
239243
return true;
240244
return hasPublicOrPackageVisibility(G->getLinkage(),
241245
/*includePackage*/ true);
@@ -306,8 +310,8 @@ bool CrossModuleOptimization::canSerializeFunction(
306310
canSerializeFlags[function] = false;
307311

308312
if (everything) {
309-
canSerializeFlags[function] = true;
310-
return true;
313+
canSerializeFlags[function] = true;
314+
return true;
311315
}
312316

313317
if (DeclContext *funcCtxt = function->getDeclContext()) {
@@ -689,6 +693,8 @@ void CrossModuleOptimization::serializeInstruction(SILInstruction *inst,
689693
}
690694

691695
void CrossModuleOptimization::serializeGlobal(SILGlobalVariable *global) {
696+
if (isSerializedWithRightKind(M, global))
697+
return;
692698
for (const SILInstruction &initInst : *global) {
693699
if (auto *FRI = dyn_cast<FunctionRefInst>(&initInst)) {
694700
SILFunction *callee = FRI->getReferencedFunction();

lib/Serialization/DeserializeSIL.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3638,8 +3638,8 @@ SILGlobalVariable *SILDeserializer::readGlobalVar(StringRef Name) {
36383638

36393639
TypeID TyID;
36403640
DeclID dID;
3641-
unsigned rawLinkage, isSerialized, IsDeclaration, IsLet;
3642-
SILGlobalVarLayout::readRecord(scratch, rawLinkage, isSerialized,
3641+
unsigned rawLinkage, serializedKind, IsDeclaration, IsLet;
3642+
SILGlobalVarLayout::readRecord(scratch, rawLinkage, serializedKind,
36433643
IsDeclaration, IsLet, TyID, dID);
36443644
if (TyID == 0) {
36453645
LLVM_DEBUG(llvm::dbgs() << "SILGlobalVariable typeID is 0.\n");
@@ -3655,7 +3655,7 @@ SILGlobalVariable *SILDeserializer::readGlobalVar(StringRef Name) {
36553655

36563656
auto Ty = MF->getType(TyID);
36573657
SILGlobalVariable *v = SILGlobalVariable::create(
3658-
SILMod, linkage.value(), isSerialized ? IsSerialized : IsNotSerialized,
3658+
SILMod, linkage.value(), SerializedKind_t(serializedKind),
36593659
Name.str(), getSILType(Ty, SILValueCategory::Object, nullptr),
36603660
std::nullopt, dID ? cast<VarDecl>(MF->getDecl(dID)) : nullptr);
36613661
v->setLet(IsLet);
@@ -3932,7 +3932,7 @@ SILMoveOnlyDeinit *SILDeserializer::readMoveOnlyDeinit(DeclID tableID) {
39323932
rawSerialized = 0;
39333933

39343934
auto *deinit = SILMoveOnlyDeinit::create(
3935-
SILMod, theNomDecl, rawSerialized ? IsSerialized : IsNotSerialized,
3935+
SILMod, theNomDecl, SerializedKind_t(rawSerialized),
39363936
theFunc);
39373937
moveOnlyDeinitOrOffset.set(deinit, true /*isFullyDeserialized*/);
39383938

lib/Serialization/SerializeSIL.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,7 +2873,7 @@ void SILSerializer::writeSILGlobalVar(const SILGlobalVariable &g) {
28732873
SILGlobalVarLayout::emitRecord(Out, ScratchRecord,
28742874
SILAbbrCodes[SILGlobalVarLayout::Code],
28752875
toStableSILLinkage(g.getLinkage()),
2876-
g.getSerializedKind(),
2876+
(unsigned)g.getSerializedKind(),
28772877
(unsigned)!g.isDefinition(),
28782878
(unsigned)g.isLet(),
28792879
TyID, dID);
@@ -2919,13 +2919,14 @@ void SILSerializer::writeSILVTable(const SILVTable &vt) {
29192919
VTableOffset.push_back(Out.GetCurrentBitNo());
29202920
VTableLayout::emitRecord(Out, ScratchRecord, SILAbbrCodes[VTableLayout::Code],
29212921
S.addDeclRef(vt.getClass()),
2922-
vt.getSerializedKind());
2922+
(unsigned)vt.getSerializedKind());
29232923

29242924
for (auto &entry : vt.getEntries()) {
29252925
SmallVector<uint64_t, 4> ListOfValues;
29262926
SILFunction *impl = entry.getImplementation();
29272927

2928-
if (ShouldSerializeAll || impl->hasValidLinkageForFragileRef(vt.getSerializedKind())) {
2928+
if (ShouldSerializeAll ||
2929+
impl->hasValidLinkageForFragileRef(vt.getSerializedKind())) {
29292930
handleSILDeclRef(S, entry.getMethod(), ListOfValues);
29302931
addReferencedSILFunction(impl, true);
29312932
// Each entry is a pair of SILDeclRef and SILFunction.
@@ -3001,7 +3002,7 @@ void SILSerializer::writeSILWitnessTable(const SILWitnessTable &wt) {
30013002
SILAbbrCodes[WitnessTableLayout::Code],
30023003
toStableSILLinkage(wt.getLinkage()),
30033004
unsigned(wt.isDeclaration()),
3004-
wt.getSerializedKind(),
3005+
unsigned(wt.getSerializedKind()),
30053006
conformanceID);
30063007

30073008
// If we have a declaration, do not attempt to serialize entries.

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ public func mainPub() {
176176

177177
//--- Lib.swift
178178

179+
// FIXME: serialize closure pointers?
179180
// static PkgStruct.pkgStaticSimpleFuncPtr
180-
// CHECK-COMMON-DAG: sil_global package @$s3Lib9PkgStructV22pkgStaticSimpleFuncPtryS2icvpZ
181+
// CHECK-COMMON-DAG: sil_global package [serialized_for_package] @$s3Lib9PkgStructV22pkgStaticSimpleFuncPtryS2icvpZ
181182

182183
// static FrPubStruct.pubStaticClosurePtr
183184
// CHECK-COMMON-DAG: sil_global @$s3Lib11FrPubStructV19pubStaticClosurePtrySiAA0cD0VcvpZ
@@ -189,25 +190,25 @@ public func mainPub() {
189190
// CHECK-COMMON-DAG: sil_global package @$s3Lib9PkgStructV25pkgStaticSimpleClosurePtryS2icvpZ
190191

191192
// static PkgStruct.pkgStaticFuncPtr
192-
// CHECK-COMMON-DAG: sil_global package @$s3Lib9PkgStructV16pkgStaticFuncPtrySiACcvpZ
193+
// CHECK-COMMON-DAG: sil_global package [serialized_for_package] @$s3Lib9PkgStructV16pkgStaticFuncPtrySiACcvpZ
193194

194195
// static PubStruct.pubStaticClosurePtr
195196
// CHECK-COMMON-DAG: sil_global @$s3Lib9PubStructV19pubStaticClosurePtrySiACcvpZ
196197

197198
// static FrPubStruct.pubStaticSimpleFuncPtr
198-
// CHECK-COMMON-DAG: sil_global @$s3Lib11FrPubStructV22pubStaticSimpleFuncPtryS2icvpZ
199+
// CHECK-COMMON-DAG: sil_global [serialized_for_package] @$s3Lib11FrPubStructV22pubStaticSimpleFuncPtryS2icvpZ
199200

200201
// static FrPubStruct.pubStaticFuncPtr
201-
// CHECK-COMMON-DAG: sil_global @$s3Lib11FrPubStructV16pubStaticFuncPtrySiAA0cD0VcvpZ
202+
// CHECK-COMMON-DAG: sil_global [serialized_for_package] @$s3Lib11FrPubStructV16pubStaticFuncPtrySiAA0cD0VcvpZ
202203

203204
// static PubStruct.pubStaticSimpleClosurePtr
204205
// CHECK-COMMON-DAG: sil_global @$s3Lib9PubStructV25pubStaticSimpleClosurePtryS2icvpZ
205206

206207
// static PubStruct.pubStaticSimpleFuncPtr
207-
// CHECK-COMMON-DAG: sil_global @$s3Lib9PubStructV22pubStaticSimpleFuncPtryS2icvpZ
208+
// CHECK-COMMON-DAG: sil_global [serialized_for_package] @$s3Lib9PubStructV22pubStaticSimpleFuncPtryS2icvpZ
208209

209210
// static PubStruct.pubStaticFuncPtr
210-
// CHECK-COMMON-DAG: sil_global @$s3Lib9PubStructV16pubStaticFuncPtrySiACcvpZ
211+
// CHECK-COMMON-DAG: sil_global [serialized_for_package] @$s3Lib9PubStructV16pubStaticFuncPtrySiACcvpZ
211212

212213
// static FrPubStruct.pubStaticSimpleClosurePtr
213214
// CHECK-COMMON-DAG: sil_global @$s3Lib11FrPubStructV25pubStaticSimpleClosurePtryS2icvpZ
@@ -517,15 +518,19 @@ final package class FinalPkgKlass {
517518
}
518519
}
519520

520-
// CHECK-COMMON-LABEL: sil_vtable [serialized_for_package] PubKlass {
521+
// CHECK-RES-LABEL: sil_vtable [serialized_for_package] PubKlass {
522+
// FIXME: should be [serialized_for_package] in non-resilient mode as well?
523+
// CHECK-NONRES-LABEL: sil_vtable [serialized] PubKlass {
521524
// CHECK-COMMON-NEXT: #PubKlass.data!getter: (PubKlass) -> () -> Int : @$s3Lib8PubKlassC4dataSivg
522525
// CHECK-COMMON-NEXT: #PubKlass.data!setter: (PubKlass) -> (Int) -> () : @$s3Lib8PubKlassC4dataSivs
523526
// CHECK-COMMON-NEXT: #PubKlass.data!modify: (PubKlass) -> () -> () : @$s3Lib8PubKlassC4dataSivM
524527
// CHECK-COMMON-NEXT: #PubKlass.init!allocator: (PubKlass.Type) -> (Int) -> PubKlass : @$s3Lib8PubKlassCyACSicfC
525528
// CHECK-COMMON-NEXT: #PubKlass.pubfunc: (PubKlass) -> (Int) -> Int : @$s3Lib8PubKlassC7pubfuncyS2iF
526529
// CHECK-COMMON-NEXT: #PubKlass.deinit!deallocator: @$s3Lib8PubKlassCfD
527530

528-
// CHECK-COMMON-LABEL: sil_vtable [serialized_for_package] FinalPubKlass {
531+
// CHECK-RES-LABEL: sil_vtable [serialized_for_package] FinalPubKlass {
532+
// FIXME: should be [serialized_for_package] in non-resilient mode as well?
533+
// CHECK-NONRES-LABEL: sil_vtable [serialized] FinalPubKlass {
529534
// CHECK-COMMON-NEXT: #FinalPubKlass.init!allocator: (FinalPubKlass.Type) -> (Int) -> FinalPubKlass : @$s3Lib13FinalPubKlassCyACSicfC
530535
// CHECK-COMMON-NEXT: #FinalPubKlass.deinit!deallocator: @$s3Lib13FinalPubKlassCfD
531536

0 commit comments

Comments
 (0)