Skip to content

Commit e1d4da0

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

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
@@ -1207,7 +1207,8 @@ bool MemoryBufferSerializedModuleLoader::canImportModule(
12071207

12081208
ModuleDecl *
12091209
SerializedModuleLoaderBase::loadModule(SourceLoc importLoc,
1210-
ImportPath::Module path) {
1210+
ImportPath::Module path,
1211+
bool AllowMemoryCache) {
12111212
// FIXME: Swift submodules?
12121213
if (path.size() > 1)
12131214
return nullptr;
@@ -1232,7 +1233,8 @@ SerializedModuleLoaderBase::loadModule(SourceLoc importLoc,
12321233

12331234
auto M = ModuleDecl::create(moduleID.Item, Ctx);
12341235
M->setIsSystemModule(isSystemModule);
1235-
Ctx.addLoadedModule(M);
1236+
if (AllowMemoryCache)
1237+
Ctx.addLoadedModule(M);
12361238
SWIFT_DEFER { M->setHasResolvedImports(); };
12371239

12381240
llvm::sys::path::native(moduleInterfacePath);
@@ -1263,7 +1265,8 @@ SerializedModuleLoaderBase::loadModule(SourceLoc importLoc,
12631265

12641266
ModuleDecl *
12651267
MemoryBufferSerializedModuleLoader::loadModule(SourceLoc importLoc,
1266-
ImportPath::Module path) {
1268+
ImportPath::Module path,
1269+
bool AllowMemoryCache) {
12671270
// FIXME: Swift submodules?
12681271
if (path.size() > 1)
12691272
return nullptr;
@@ -1299,7 +1302,8 @@ MemoryBufferSerializedModuleLoader::loadModule(SourceLoc importLoc,
12991302
if (BypassResilience)
13001303
M->setBypassResilience();
13011304
M->addFile(*file);
1302-
Ctx.addLoadedModule(M);
1305+
if (AllowMemoryCache)
1306+
Ctx.addLoadedModule(M);
13031307
return M;
13041308
}
13051309

0 commit comments

Comments
 (0)