@@ -161,14 +161,26 @@ class InstructionVisitor : public SILCloner<InstructionVisitor> {
161161 }
162162};
163163
164+ static bool isVisible (SILLinkage linkage, SILOptions options) {
165+ if (options.EnableSerializePackage )
166+ return linkage == SILLinkage::Public || linkage == SILLinkage::Package;
167+ return linkage == SILLinkage::Public;
168+ }
169+ static bool isVisible (AccessLevel accessLevel, SILOptions options) {
170+ if (options.EnableSerializePackage )
171+ return accessLevel == AccessLevel::Package || accessLevel == AccessLevel::Public;
172+ return accessLevel == AccessLevel::Public;
173+ }
174+
164175// / Select functions in the module which should be serialized.
165176void CrossModuleOptimization::serializeFunctionsInModule () {
166177
167178 FunctionFlags canSerializeFlags;
168179
169180 // Start with public functions.
170181 for (SILFunction &F : M) {
171- if (F.getLinkage () == SILLinkage::Public || everything) {
182+ if (isVisible (F.getLinkage (), M.getOptions ()) ||
183+ everything) {
172184 if (canSerializeFunction (&F, canSerializeFlags, /* maxDepth*/ 64 )) {
173185 serializeFunction (&F, canSerializeFlags);
174186 }
@@ -185,7 +197,6 @@ bool CrossModuleOptimization::canSerializeFunction(
185197 FunctionFlags &canSerializeFlags,
186198 int maxDepth) {
187199 auto iter = canSerializeFlags.find (function);
188-
189200 // Avoid infinite recursion in case it's a cycle in the call graph.
190201 if (iter != canSerializeFlags.end ())
191202 return iter->second ;
@@ -270,7 +281,7 @@ bool CrossModuleOptimization::canSerializeInstruction(SILInstruction *inst,
270281 // function is completely inlined afterwards.
271282 // Also, when emitting TBD files, we cannot introduce a new public symbol.
272283 if ((conservative || M.getOptions ().emitTBD ) &&
273- !hasPublicVisibility (callee->getLinkage ())) {
284+ !hasPublicOrPackageVisibility (callee->getLinkage (), M. getOptions (). EnableSerializePackage )) {
274285 return false ;
275286 }
276287
@@ -290,13 +301,12 @@ bool CrossModuleOptimization::canSerializeInstruction(SILInstruction *inst,
290301 // inline.
291302 if (!canUseFromInline (callee))
292303 return false ;
293-
294304 return true ;
295305 }
296306 if (auto *GAI = dyn_cast<GlobalAddrInst>(inst)) {
297307 SILGlobalVariable *global = GAI->getReferencedGlobal ();
298308 if ((conservative || M.getOptions ().emitTBD ) &&
299- !hasPublicVisibility (global->getLinkage ())) {
309+ !hasPublicOrPackageVisibility (global->getLinkage (), M. getOptions (). EnableSerializePackage )) {
300310 return false ;
301311 }
302312
@@ -344,7 +354,7 @@ bool CrossModuleOptimization::canSerializeGlobal(SILGlobalVariable *global) {
344354 // function is completely inlined afterwards.
345355 // Also, when emitting TBD files, we cannot introduce a new public symbol.
346356 if ((conservative || M.getOptions ().emitTBD ) &&
347- !hasPublicVisibility (referencedFunc->getLinkage ())) {
357+ !hasPublicOrPackageVisibility (referencedFunc->getLinkage (), M. getOptions (). EnableSerializePackage )) {
348358 return false ;
349359 }
350360
@@ -368,7 +378,7 @@ bool CrossModuleOptimization::canSerializeType(SILType type) {
368378 if (conservative && subNT->getEffectiveAccess () < AccessLevel::Package) {
369379 return true ;
370380 }
371-
381+
372382 // Exclude types which are defined in an @_implementationOnly imported
373383 // module. Such modules are not transitively available.
374384 if (!canUseFromInline (subNT)) {
@@ -542,15 +552,15 @@ void CrossModuleOptimization::serializeInstruction(SILInstruction *inst,
542552 }
543553 }
544554 serializeFunction (callee, canSerializeFlags);
545- assert (callee->isSerialized () || callee->getLinkage () == SILLinkage::Public );
555+ assert (callee->isSerialized () || isVisible ( callee->getLinkage (), M. getOptions ()) );
546556 return ;
547557 }
548558 if (auto *GAI = dyn_cast<GlobalAddrInst>(inst)) {
549559 SILGlobalVariable *global = GAI->getReferencedGlobal ();
550560 if (canSerializeGlobal (global)) {
551561 serializeGlobal (global);
552562 }
553- if (!hasPublicVisibility (global->getLinkage ())) {
563+ if (!hasPublicOrPackageVisibility (global->getLinkage (), M. getOptions (). EnableSerializePackage )) {
554564 global->setLinkage (SILLinkage::Public);
555565 }
556566 return ;
@@ -606,7 +616,7 @@ void CrossModuleOptimization::makeDeclUsableFromInline(ValueDecl *decl) {
606616 if (M.getSwiftModule () != decl->getDeclContext ()->getParentModule ())
607617 return ;
608618
609- if (decl->getFormalAccess () < AccessLevel::Public &&
619+ if (! isVisible ( decl->getFormalAccess (), M. getOptions ()) &&
610620 !decl->isUsableFromInline ()) {
611621 // Mark the nominal type as "usableFromInline".
612622 // TODO: find a way to do this without modifying the AST. The AST should be
0 commit comments