Skip to content

Commit 3f04710

Browse files
authored
Merge pull request #72937 from apple/elsh/pkg-sil-verify
Add a package serialization bit to Module for SIL verifier in Package CMO mode.
2 parents a354528 + 0c0d8c0 commit 3f04710

File tree

13 files changed

+312
-62
lines changed

13 files changed

+312
-62
lines changed

include/swift/AST/Decl.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
697697
HasAnyUnavailableDuringLoweringValues : 1
698698
);
699699

700-
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1,
700+
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1,
701701
/// If the module is compiled as static library.
702702
StaticLibrary : 1,
703703

@@ -756,7 +756,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
756756
HasCxxInteroperability : 1,
757757

758758
/// Whether this module has been built with -experimental-allow-non-resilient-access.
759-
AllowNonResilientAccess : 1
759+
AllowNonResilientAccess : 1,
760+
761+
/// Whether this module has been built with -experimental-package-cmo.
762+
SerializePackageEnabled : 1
760763
);
761764

762765
SWIFT_INLINE_BITFIELD(PrecedenceGroupDecl, Decl, 1+2,

include/swift/AST/Module.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,16 @@ class ModuleDecl
721721
Bits.ModuleDecl.AllowNonResilientAccess = flag;
722722
}
723723

724+
/// Returns true if -experimental-package-cmo was passed, which
725+
/// enables serialization of package, public, and inlinable decls in a
726+
/// package. This requires -experimental-allow-non-resilient-access.
727+
bool serializePackageEnabled() const {
728+
return Bits.ModuleDecl.SerializePackageEnabled;
729+
}
730+
void setSerializePackageEnabled(bool flag = true) {
731+
Bits.ModuleDecl.SerializePackageEnabled = flag;
732+
}
733+
724734
/// Returns true if this module is a non-Swift module that was imported into
725735
/// Swift.
726736
///

include/swift/Serialization/Validation.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class ExtendedValidationInfo {
141141
unsigned IsConcurrencyChecked : 1;
142142
unsigned HasCxxInteroperability : 1;
143143
unsigned AllowNonResilientAccess: 1;
144+
unsigned SerializePackageEnabled: 1;
144145
} Bits;
145146
public:
146147
ExtendedValidationInfo() : Bits() {}
@@ -209,6 +210,10 @@ class ExtendedValidationInfo {
209210
void setAllowNonResilientAccess(bool val) {
210211
Bits.AllowNonResilientAccess = val;
211212
}
213+
bool serializePackageEnabled() const { return Bits.SerializePackageEnabled; }
214+
void setSerializePackageEnabled(bool val) {
215+
Bits.SerializePackageEnabled = val;
216+
}
212217
bool isAllowModuleWithCompilerErrorsEnabled() {
213218
return Bits.IsAllowModuleWithCompilerErrorsEnabled;
214219
}

lib/AST/Module.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ ModuleDecl::ModuleDecl(Identifier name, ASTContext &ctx,
723723
Bits.ModuleDecl.ObjCNameLookupCachePopulated = 0;
724724
Bits.ModuleDecl.HasCxxInteroperability = 0;
725725
Bits.ModuleDecl.AllowNonResilientAccess = 0;
726+
Bits.ModuleDecl.SerializePackageEnabled = 0;
726727
}
727728

728729
void ModuleDecl::setIsSystemModule(bool flag) {

lib/Frontend/Frontend.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,8 @@ ModuleDecl *CompilerInstance::getMainModule() const {
13821382
MainModule->setHasCxxInteroperability();
13831383
if (Invocation.getLangOptions().AllowNonResilientAccess)
13841384
MainModule->setAllowNonResilientAccess();
1385+
if (Invocation.getSILOptions().EnableSerializePackage)
1386+
MainModule->setSerializePackageEnabled();
13851387

13861388
// Register the main module with the AST context.
13871389
Context->addLoadedModule(MainModule);

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,28 @@ static bool isArchetypeValidInFunction(ArchetypeType *A, const SILFunction *F) {
153153

154154
namespace {
155155

156-
/// When resilience is bypassed, direct access is legal, but the decls are still
157-
/// resilient.
156+
/// When resilience is bypassed for debugging or package serialization is enabled,
157+
/// direct access is legal, but the decls are still resilient.
158158
template <typename DeclType>
159159
bool checkResilience(DeclType *D, ModuleDecl *M,
160160
ResilienceExpansion expansion) {
161-
return !D->getModuleContext()->getBypassResilience() &&
162-
D->isResilient(M, expansion);
161+
auto refDeclModule = D->getModuleContext();
162+
// Explicitly bypassed for debugging with `bypass-resilience-checks`
163+
if (refDeclModule->getBypassResilience())
164+
return false;
165+
166+
// If package serialization is enabled with `experimental-package-cmo`,
167+
// decls can be serialized in a resiliently built module. In such case,
168+
// a direct access should be allowed.
169+
auto packageSerialized = expansion == ResilienceExpansion::Minimal &&
170+
refDeclModule->isResilient() &&
171+
refDeclModule->allowNonResilientAccess() &&
172+
refDeclModule->serializePackageEnabled() &&
173+
refDeclModule->inSamePackage(M);
174+
if (packageSerialized)
175+
return false;
176+
177+
return D->isResilient(M, expansion);
163178
}
164179

165180
bool checkTypeABIAccessible(SILFunction const &F, SILType ty) {

lib/Serialization/ModuleFile.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,11 @@ class ModuleFile
659659
return Core->Bits.AllowNonResilientAccess;
660660
}
661661

662+
/// Whether this module was built with -experimental-package-cmo.
663+
bool serializePackageEnabled() const {
664+
return Core->Bits.SerializePackageEnabled;
665+
}
666+
662667
/// Whether this module is compiled with implicit dynamic.
663668
bool isImplicitDynamicEnabled() const {
664669
return Core->Bits.IsImplicitDynamicEnabled;

lib/Serialization/ModuleFileSharedCore.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ static bool readOptionsBlock(llvm::BitstreamCursor &cursor,
200200
case options_block::ALLOW_NON_RESILIENT_ACCESS:
201201
extendedInfo.setAllowNonResilientAccess(true);
202202
break;
203+
case options_block::SERIALIZE_PACKAGE_ENABLED:
204+
extendedInfo.setSerializePackageEnabled(true);
205+
break;
203206
default:
204207
// Unknown options record, possibly for use by a future version of the
205208
// module format.
@@ -1448,6 +1451,7 @@ ModuleFileSharedCore::ModuleFileSharedCore(
14481451
Bits.IsConcurrencyChecked = extInfo.isConcurrencyChecked();
14491452
Bits.HasCxxInteroperability = extInfo.hasCxxInteroperability();
14501453
Bits.AllowNonResilientAccess = extInfo.allowNonResilientAccess();
1454+
Bits.SerializePackageEnabled = extInfo.serializePackageEnabled();
14511455
MiscVersion = info.miscVersion;
14521456
ModuleABIName = extInfo.getModuleABIName();
14531457
ModulePackageName = extInfo.getModulePackageName();

lib/Serialization/ModuleFileSharedCore.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,9 @@ class ModuleFileSharedCore {
391391
/// Whether this module is built with -experimental-allow-non-resilient-access.
392392
unsigned AllowNonResilientAccess : 1;
393393

394+
/// Whether this module is built with -experimental-package-cmo.
395+
unsigned SerializePackageEnabled : 1;
396+
394397
// Explicitly pad out to the next word boundary.
395398
unsigned : 3;
396399
} Bits = {};

lib/Serialization/ModuleFormat.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5858
/// describe what change you made. The content of this comment isn't important;
5959
/// it just ensures a conflict if two people change the module format.
6060
/// Don't worry about adhering to the 80-column limit for this line.
61-
const uint16_t SWIFTMODULE_VERSION_MINOR = 871; // ~ for suppression
61+
const uint16_t SWIFTMODULE_VERSION_MINOR = 872; // SerializePackageEnabled
6262

6363
/// A standard hash seed used for all string hashes in a serialized module.
6464
///
@@ -941,6 +941,7 @@ namespace options_block {
941941
PLUGIN_SEARCH_OPTION,
942942
HAS_CXX_INTEROPERABILITY_ENABLED,
943943
ALLOW_NON_RESILIENT_ACCESS,
944+
SERIALIZE_PACKAGE_ENABLED,
944945
};
945946

946947
using SDKPathLayout = BCRecordLayout<
@@ -1027,6 +1028,10 @@ namespace options_block {
10271028
using AllowNonResilientAccess = BCRecordLayout<
10281029
ALLOW_NON_RESILIENT_ACCESS
10291030
>;
1031+
1032+
using SerializePackageEnabled = BCRecordLayout<
1033+
SERIALIZE_PACKAGE_ENABLED
1034+
>;
10301035
}
10311036

10321037
/// The record types within the input block.

lib/Serialization/Serialization.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,7 @@ void Serializer::writeBlockInfoBlock() {
855855
BLOCK_RECORD(options_block, MODULE_EXPORT_AS_NAME);
856856
BLOCK_RECORD(options_block, PLUGIN_SEARCH_OPTION);
857857
BLOCK_RECORD(options_block, ALLOW_NON_RESILIENT_ACCESS);
858+
BLOCK_RECORD(options_block, SERIALIZE_PACKAGE_ENABLED);
858859

859860
BLOCK(INPUT_BLOCK);
860861
BLOCK_RECORD(input_block, IMPORTED_MODULE);
@@ -1092,6 +1093,11 @@ void Serializer::writeHeader() {
10921093
AllowNonResAcess.emit(ScratchRecord);
10931094
}
10941095

1096+
if (M->serializePackageEnabled()) {
1097+
options_block::SerializePackageEnabled SerializePkgEnabled(Out);
1098+
SerializePkgEnabled.emit(ScratchRecord);
1099+
}
1100+
10951101
if (allowCompilerErrors()) {
10961102
options_block::IsAllowModuleWithCompilerErrorsEnabledLayout
10971103
AllowErrors(Out);

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,8 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
918918
M.setIsBuiltFromInterface();
919919
if (loadedModuleFile->allowNonResilientAccess())
920920
M.setAllowNonResilientAccess();
921+
if (loadedModuleFile->serializePackageEnabled())
922+
M.setSerializePackageEnabled();
921923
if (!loadedModuleFile->getModuleABIName().empty())
922924
M.setABIName(Ctx.getIdentifier(loadedModuleFile->getModuleABIName()));
923925
if (loadedModuleFile->isConcurrencyChecked())

0 commit comments

Comments
 (0)