Skip to content

Commit da34dda

Browse files
committed
[Serialization] Add control over adding a loaded module to the in-memory cache
1 parent fd95b3a commit da34dda

File tree

8 files changed

+30
-12
lines changed

8 files changed

+30
-12
lines changed

include/swift/AST/ModuleLoader.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,14 @@ class ModuleLoader {
249249
/// \param path A sequence of (identifier, location) pairs that denote
250250
/// the dotted module name to load, e.g., AppKit.NSWindow.
251251
///
252+
/// \param AllowMemoryCache Enables preserving the loaded module in the
253+
/// in-memory cache for the next loading attempt.
254+
///
252255
/// \returns the module referenced, if it could be loaded. Otherwise,
253256
/// emits a diagnostic and returns NULL.
254257
virtual
255-
ModuleDecl *loadModule(SourceLoc importLoc, ImportPath::Module path) = 0;
258+
ModuleDecl *loadModule(SourceLoc importLoc, ImportPath::Module path,
259+
bool AllowMemoryCache = true) = 0;
256260

257261
/// Load extensions to the given nominal type.
258262
///

include/swift/ClangImporter/ClangImporter.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,15 @@ class ClangImporter final : public ClangModuleLoader {
220220
/// \param path A sequence of (identifier, location) pairs that denote
221221
/// the dotted module name to load, e.g., AppKit.NSWindow.
222222
///
223+
/// \param AllowMemoryCache Enables preserving the loaded module in the
224+
/// in-memory cache for the next loading attempt.
225+
///
223226
/// \returns the module referenced, if it could be loaded. Otherwise,
224227
/// emits a diagnostic and returns NULL.
225228
virtual ModuleDecl *loadModule(
226229
SourceLoc importLoc,
227-
ImportPath::Module path)
230+
ImportPath::Module path,
231+
bool AllowMemoryCache = true)
228232
override;
229233

230234
/// Determine whether \c overlayDC is within an overlay module for the

include/swift/Sema/SourceLoader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ class SourceLoader : public ModuleLoader {
7373
/// returns NULL.
7474
virtual ModuleDecl *
7575
loadModule(SourceLoc importLoc,
76-
ImportPath::Module path) override;
76+
ImportPath::Module path,
77+
bool AllowMemoryCache) override;
7778

7879
/// Load extensions to the given nominal type.
7980
///

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ class SerializedModuleLoaderBase : public ModuleLoader {
185185
/// emits a diagnostic and returns a FailedImportModule object.
186186
virtual ModuleDecl *
187187
loadModule(SourceLoc importLoc,
188-
ImportPath::Module path) override;
188+
ImportPath::Module path,
189+
bool AllowMemoryCache) override;
189190

190191

191192
virtual void loadExtensions(NominalTypeDecl *nominal,
@@ -294,7 +295,8 @@ class MemoryBufferSerializedModuleLoader : public SerializedModuleLoaderBase {
294295

295296
ModuleDecl *
296297
loadModule(SourceLoc importLoc,
297-
ImportPath::Module path) override;
298+
ImportPath::Module path,
299+
bool AllowMemoryCache = true) override;
298300

299301
/// Register a memory buffer that contains the serialized module for the given
300302
/// access path. This API is intended to be used by LLDB to add swiftmodules

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2456,7 +2456,8 @@ ASTContext::getModule(ImportPath::Module ModulePath, bool AllowMemoryCached) {
24562456
if (PreModuleImportCallback)
24572457
PreModuleImportCallback(moduleID.Item.str(), false /*=IsOverlay*/);
24582458
for (auto &importer : getImpl().ModuleLoaders) {
2459-
if (ModuleDecl *M = importer->loadModule(moduleID.Loc, ModulePath)) {
2459+
if (ModuleDecl *M = importer->loadModule(moduleID.Loc, ModulePath,
2460+
AllowMemoryCached)) {
24602461
if (LangOpts.EnableModuleLoadingRemarks) {
24612462
Diags.diagnose(ModulePath.getSourceRange().Start,
24622463
diag::module_loaded,

lib/ClangImporter/ClangImporter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2018,7 +2018,8 @@ ModuleDecl *ClangImporter::Implementation::loadModuleClang(
20182018

20192019
ModuleDecl *
20202020
ClangImporter::loadModule(SourceLoc importLoc,
2021-
ImportPath::Module path) {
2021+
ImportPath::Module path,
2022+
bool AllowMemoryCache) {
20222023
return Impl.loadModule(importLoc, path);
20232024
}
20242025

lib/Sema/SourceLoader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ bool SourceLoader::canImportModule(ImportPath::Module path,
9494
}
9595

9696
ModuleDecl *SourceLoader::loadModule(SourceLoc importLoc,
97-
ImportPath::Module path) {
97+
ImportPath::Module path,
98+
bool AllowMemoryCache) {
9899
// FIXME: Swift submodules?
99100
if (path.size() > 1)
100101
return nullptr;

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,8 @@ bool MemoryBufferSerializedModuleLoader::canImportModule(
12091209

12101210
ModuleDecl *
12111211
SerializedModuleLoaderBase::loadModule(SourceLoc importLoc,
1212-
ImportPath::Module path) {
1212+
ImportPath::Module path,
1213+
bool AllowMemoryCache) {
12131214
// FIXME: Swift submodules?
12141215
if (path.size() > 1)
12151216
return nullptr;
@@ -1234,7 +1235,8 @@ SerializedModuleLoaderBase::loadModule(SourceLoc importLoc,
12341235

12351236
auto M = ModuleDecl::create(moduleID.Item, Ctx);
12361237
M->setIsSystemModule(isSystemModule);
1237-
Ctx.addLoadedModule(M);
1238+
if (AllowMemoryCache)
1239+
Ctx.addLoadedModule(M);
12381240
SWIFT_DEFER { M->setHasResolvedImports(); };
12391241

12401242
llvm::sys::path::native(moduleInterfacePath);
@@ -1265,7 +1267,8 @@ SerializedModuleLoaderBase::loadModule(SourceLoc importLoc,
12651267

12661268
ModuleDecl *
12671269
MemoryBufferSerializedModuleLoader::loadModule(SourceLoc importLoc,
1268-
ImportPath::Module path) {
1270+
ImportPath::Module path,
1271+
bool AllowMemoryCache) {
12691272
// FIXME: Swift submodules?
12701273
if (path.size() > 1)
12711274
return nullptr;
@@ -1301,7 +1304,8 @@ MemoryBufferSerializedModuleLoader::loadModule(SourceLoc importLoc,
13011304
if (BypassResilience)
13021305
M->setBypassResilience();
13031306
M->addFile(*file);
1304-
Ctx.addLoadedModule(M);
1307+
if (AllowMemoryCache)
1308+
Ctx.addLoadedModule(M);
13051309
return M;
13061310
}
13071311

0 commit comments

Comments
 (0)