Skip to content

[Serialization] Minor ModuleFile/ModuleFileSharedCore improvements #33789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions include/swift/Serialization/Validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,8 @@ ValidationInfo validateSerializedAST(
/// - \p ModuleName is the name used to refer to the module in diagnostics.
void diagnoseSerializedASTLoadFailure(
ASTContext &Ctx, SourceLoc diagLoc, const ValidationInfo &loadInfo,
const ExtendedValidationInfo &extendedInfo, StringRef moduleBufferID,
StringRef moduleDocBufferID, ModuleFile *loadedModuleFile,
Identifier ModuleName);
StringRef moduleBufferID, StringRef moduleDocBufferID,
ModuleFile *loadedModuleFile, Identifier ModuleName);

} // end namespace serialization
} // end namespace swift
Expand Down
4 changes: 1 addition & 3 deletions lib/Serialization/ModuleFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,13 @@ ModuleFile::getModuleName(ASTContext &Ctx, StringRef modulePath,
llvm::MemoryBuffer::getMemBuffer(llvm::MemoryBufferRef(*moduleBuf.get()),
/*RequiresNullTerminator=*/false);
std::shared_ptr<const ModuleFileSharedCore> loadedModuleFile;
ExtendedValidationInfo ExtInfo;
bool isFramework = false;
serialization::ValidationInfo loadInfo =
ModuleFileSharedCore::load(modulePath.str(),
std::move(newBuf),
nullptr,
nullptr,
/*isFramework*/isFramework, loadedModuleFile,
&ExtInfo);
/*isFramework*/isFramework, loadedModuleFile);
Name = loadedModuleFile->Name.str();
return std::move(moduleBuf.get());
}
Expand Down
22 changes: 21 additions & 1 deletion lib/Serialization/ModuleFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,31 @@ class ModuleFile
return Core->CompatibilityVersion;
}

/// Whether this module is compiled with `-enable-private-imports`.
bool arePrivateImportsEnabled() const {
return Core->Bits.ArePrivateImportsEnabled;
}

/// Is this module file actually a .sib file? .sib files are serialized SIL at
/// arbitrary granularity and arbitrary stage; unlike serialized Swift
/// modules, which are assumed to contain canonical SIL for an entire module.
bool isSIB() const {
return Core->IsSIB;
return Core->Bits.IsSIB;
}

/// Whether this module file is compiled with '-enable-testing'.
bool isTestable() const {
return Core->Bits.IsTestable;
}

/// Whether the module is resilient. ('-enable-library-evolution')
ResilienceStrategy getResilienceStrategy() const {
return ResilienceStrategy(Core->Bits.ResilienceStrategy);
}

/// Whether this module is compiled with implicit dynamic.
bool isImplicitDynamicEnabled() const {
return Core->Bits.IsImplicitDynamicEnabled;
}

/// Associates this module file with the AST node representing it.
Expand Down
12 changes: 8 additions & 4 deletions lib/Serialization/ModuleFileSharedCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1087,8 +1087,7 @@ ModuleFileSharedCore::ModuleFileSharedCore(
std::unique_ptr<llvm::MemoryBuffer> moduleInputBuffer,
std::unique_ptr<llvm::MemoryBuffer> moduleDocInputBuffer,
std::unique_ptr<llvm::MemoryBuffer> moduleSourceInfoInputBuffer,
bool isFramework, serialization::ValidationInfo &info,
serialization::ExtendedValidationInfo *extInfo)
bool isFramework, serialization::ValidationInfo &info)
: ModuleInputBuffer(std::move(moduleInputBuffer)),
ModuleDocInputBuffer(std::move(moduleDocInputBuffer)),
ModuleSourceInfoInputBuffer(std::move(moduleSourceInfoInputBuffer)) {
Expand Down Expand Up @@ -1134,18 +1133,23 @@ ModuleFileSharedCore::ModuleFileSharedCore(
return;
}

ExtendedValidationInfo extInfo;
info = validateControlBlock(cursor, scratch,
{SWIFTMODULE_VERSION_MAJOR,
SWIFTMODULE_VERSION_MINOR},
extInfo);
&extInfo);
if (info.status != Status::Valid) {
error(info.status);
return;
}
Name = info.name;
TargetTriple = info.targetTriple;
CompatibilityVersion = info.compatibilityVersion;
IsSIB = extInfo->isSIB();
Bits.ArePrivateImportsEnabled = extInfo.arePrivateImportsEnabled();
Bits.IsSIB = extInfo.isSIB();
Bits.IsTestable = extInfo.isTestable();
Bits.ResilienceStrategy = unsigned(extInfo.getResilienceStrategy());
Bits.IsImplicitDynamicEnabled = extInfo.isImplicitDynamicEnabled();
MiscVersion = info.miscVersion;

hasValidControlBlock = true;
Expand Down
29 changes: 19 additions & 10 deletions lib/Serialization/ModuleFileSharedCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ class ModuleFileSharedCore {
/// The data blob containing all of the module's identifiers.
StringRef IdentifierData;

/// Is this module file actually a .sib file? .sib files are serialized SIL at
/// arbitrary granularity and arbitrary stage; unlike serialized Swift
/// modules, which are assumed to contain canonical SIL for an entire module.
bool IsSIB = false;

// Full blob from the misc. version field of the metadata block. This should
// include the version string of the compiler that built the module.
StringRef MiscVersion;
Expand Down Expand Up @@ -307,6 +302,21 @@ class ModuleFileSharedCore {
/// Whether an error has been detected setting up this module file.
unsigned HasError : 1;

/// Whether this module is `-enable-private-imports`.
unsigned ArePrivateImportsEnabled : 1;

/// Whether this module file is actually a .sib file.
unsigned IsSIB: 1;

/// Whether this module file is compiled with '-enable-testing'.
unsigned IsTestable : 1;

/// Discriminator for resilience strategy.
unsigned ResilienceStrategy : 2;

/// Whether this module is compiled with implicit dynamic.
unsigned IsImplicitDynamicEnabled: 1;

// Explicitly pad out to the next word boundary.
unsigned : 0;
} Bits = {};
Expand All @@ -326,8 +336,7 @@ class ModuleFileSharedCore {
ModuleFileSharedCore(std::unique_ptr<llvm::MemoryBuffer> moduleInputBuffer,
std::unique_ptr<llvm::MemoryBuffer> moduleDocInputBuffer,
std::unique_ptr<llvm::MemoryBuffer> moduleSourceInfoInputBuffer,
bool isFramework, serialization::ValidationInfo &info,
serialization::ExtendedValidationInfo *extInfo);
bool isFramework, serialization::ValidationInfo &info);

/// Change the status of the current module.
Status error(Status issue) {
Expand Down Expand Up @@ -455,12 +464,12 @@ class ModuleFileSharedCore {
std::unique_ptr<llvm::MemoryBuffer> moduleInputBuffer,
std::unique_ptr<llvm::MemoryBuffer> moduleDocInputBuffer,
std::unique_ptr<llvm::MemoryBuffer> moduleSourceInfoInputBuffer,
bool isFramework, std::shared_ptr<const ModuleFileSharedCore> &theModule,
serialization::ExtendedValidationInfo *extInfo = nullptr) {
bool isFramework,
std::shared_ptr<const ModuleFileSharedCore> &theModule) {
serialization::ValidationInfo info;
auto *core = new ModuleFileSharedCore(
std::move(moduleInputBuffer), std::move(moduleDocInputBuffer),
std::move(moduleSourceInfoInputBuffer), isFramework, info, extInfo);
std::move(moduleSourceInfoInputBuffer), isFramework, info);
if (!moduleInterfacePath.empty()) {
ArrayRef<char> path;
core->allocateBuffer(path, moduleInterfacePath);
Expand Down
19 changes: 7 additions & 12 deletions lib/Serialization/SerializedModuleLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,12 @@ llvm::ErrorOr<ModuleDependencies> SerializedModuleLoaderBase::scanModuleFile(
// Load the module file without validation.
std::shared_ptr<const ModuleFileSharedCore> loadedModuleFile;
bool isFramework = false;
serialization::ExtendedValidationInfo extInfo;
serialization::ValidationInfo loadInfo =
ModuleFileSharedCore::load(modulePath.str(),
std::move(moduleBuf.get()),
nullptr,
nullptr,
isFramework, loadedModuleFile,
&extInfo);
isFramework, loadedModuleFile);

// Map the set of dependencies over to the "module dependencies".
auto dependencies = ModuleDependencies::forSwiftModule(modulePath.str(), isFramework);
Expand Down Expand Up @@ -695,28 +693,26 @@ FileUnit *SerializedModuleLoaderBase::loadAST(
return nullptr;
}

serialization::ExtendedValidationInfo extendedInfo;
std::unique_ptr<ModuleFile> loadedModuleFile;
std::shared_ptr<const ModuleFileSharedCore> loadedModuleFileCore;
serialization::ValidationInfo loadInfo =
ModuleFileSharedCore::load(moduleInterfacePath,
std::move(moduleInputBuffer),
std::move(moduleDocInputBuffer),
std::move(moduleSourceInfoInputBuffer),
isFramework, loadedModuleFileCore,
&extendedInfo);
isFramework, loadedModuleFileCore);
if (loadInfo.status == serialization::Status::Valid) {
loadedModuleFile =
std::make_unique<ModuleFile>(std::move(loadedModuleFileCore));
M.setResilienceStrategy(extendedInfo.getResilienceStrategy());
M.setResilienceStrategy(loadedModuleFile->getResilienceStrategy());

// We've loaded the file. Now try to bring it into the AST.
auto fileUnit = new (Ctx) SerializedASTFile(M, *loadedModuleFile);
if (extendedInfo.isTestable())
if (loadedModuleFile->isTestable())
M.setTestingEnabled();
if (extendedInfo.arePrivateImportsEnabled())
if (loadedModuleFile->arePrivateImportsEnabled())
M.setPrivateImportsEnabled();
if (extendedInfo.isImplicitDynamicEnabled())
if (loadedModuleFile->isImplicitDynamicEnabled())
M.setImplicitDynamicEnabled();

auto diagLocOrInvalid = diagLoc.getValueOr(SourceLoc());
Expand Down Expand Up @@ -744,7 +740,7 @@ FileUnit *SerializedModuleLoaderBase::loadAST(

if (diagLoc)
serialization::diagnoseSerializedASTLoadFailure(
Ctx, *diagLoc, loadInfo, extendedInfo, moduleBufferID,
Ctx, *diagLoc, loadInfo, moduleBufferID,
moduleDocBufferID, loadedModuleFile.get(), M.getName());

// Even though the module failed to load, it's possible its contents include
Expand All @@ -761,7 +757,6 @@ FileUnit *SerializedModuleLoaderBase::loadAST(
void swift::serialization::diagnoseSerializedASTLoadFailure(
ASTContext &Ctx, SourceLoc diagLoc,
const serialization::ValidationInfo &loadInfo,
const serialization::ExtendedValidationInfo &extendedInfo,
StringRef moduleBufferID, StringRef moduleDocBufferID,
ModuleFile *loadedModuleFile, Identifier ModuleName) {
auto diagnoseDifferentLanguageVersion = [&](StringRef shortVersion) -> bool {
Expand Down