Skip to content

Commit 162c9a8

Browse files
Merge pull request #2719 from adrian-prantl/75381959-1
Cache the results of SwiftASTContext::GetCompileUnitImports()
2 parents 799e84c + 7b1707a commit 162c9a8

File tree

3 files changed

+60
-17
lines changed

3 files changed

+60
-17
lines changed

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8315,8 +8315,8 @@ bool SwiftASTContext::GetImplicitImports(
83158315
llvm::SmallVectorImpl<swift::AttributedImport<swift::ImportedModule>>
83168316
&modules,
83178317
Status &error) {
8318-
if (!GetCompileUnitImports(swift_ast_context, sc, stack_frame_wp, modules,
8319-
error)) {
8318+
if (!swift_ast_context.GetCompileUnitImports(sc, stack_frame_wp, modules,
8319+
error)) {
83208320
return false;
83218321
}
83228322

@@ -8388,22 +8388,54 @@ bool SwiftASTContext::CacheUserImports(SwiftASTContext &swift_ast_context,
83888388
}
83898389

83908390
bool SwiftASTContext::GetCompileUnitImports(
8391-
SwiftASTContext &swift_ast_context, SymbolContext &sc,
8392-
lldb::StackFrameWP &stack_frame_wp,
8391+
SymbolContext &sc, lldb::StackFrameWP &stack_frame_wp,
83938392
llvm::SmallVectorImpl<swift::AttributedImport<swift::ImportedModule>>
83948393
&modules,
83958394
Status &error) {
8395+
return GetCompileUnitImportsImpl(sc, stack_frame_wp, &modules, error);
8396+
}
8397+
8398+
void SwiftASTContext::PerformCompileUnitImports(
8399+
SymbolContext &sc, lldb::StackFrameWP &stack_frame_wp, Status &error) {
8400+
GetCompileUnitImportsImpl(sc, stack_frame_wp, nullptr, error);
8401+
}
8402+
8403+
static std::pair<Module *, lldb::user_id_t>
8404+
GetCUSignature(CompileUnit &compile_unit) {
8405+
return {compile_unit.GetModule().get(), compile_unit.GetID()};
8406+
}
8407+
8408+
bool SwiftASTContext::GetCompileUnitImportsImpl(
8409+
SymbolContext &sc, lldb::StackFrameWP &stack_frame_wp,
8410+
llvm::SmallVectorImpl<swift::AttributedImport<swift::ImportedModule>>
8411+
*modules,
8412+
Status &error) {
8413+
CompileUnit *compile_unit = sc.comp_unit;
8414+
if (compile_unit)
8415+
// Check the cache if this compile unit's imports were previously
8416+
// requested. If the caller didn't request the list of imported
8417+
// modules then there is nothing left to do for subsequent
8418+
// GetCompileUnitImportsImpl() calls as the previously loaded
8419+
// modules should still be loaded. The fact the we
8420+
// unconditionally return true does not matter because the only
8421+
// way to get here is through void PerformCompileUnitImports(),
8422+
// which discards the return value.
8423+
if (!m_cu_imports.insert(GetCUSignature(*compile_unit)).second)
8424+
// List of imports isn't requested and we already processed this CU?
8425+
if (!modules)
8426+
return true;
8427+
83968428
// Import the Swift standard library and its dependencies.
83978429
SourceModule swift_module;
83988430
swift_module.path.emplace_back("Swift");
83998431
auto *stdlib =
8400-
LoadOneModule(swift_module, swift_ast_context, stack_frame_wp, error);
8432+
LoadOneModule(swift_module, *this, stack_frame_wp, error);
84018433
if (!stdlib)
84028434
return false;
84038435

8404-
modules.emplace_back(swift::ImportedModule(stdlib));
8436+
if (modules)
8437+
modules->emplace_back(swift::ImportedModule(stdlib));
84058438

8406-
CompileUnit *compile_unit = sc.comp_unit;
84078439
if (!compile_unit || compile_unit->GetLanguage() != lldb::eLanguageTypeSwift)
84088440
return true;
84098441

@@ -8418,11 +8450,12 @@ bool SwiftASTContext::GetCompileUnitImports(
84188450
continue;
84198451

84208452
auto *loaded_module =
8421-
LoadOneModule(module, swift_ast_context, stack_frame_wp, error);
8453+
LoadOneModule(module, *this, stack_frame_wp, error);
84228454
if (!loaded_module)
84238455
return false;
84248456

8425-
modules.emplace_back(swift::ImportedModule(loaded_module));
8457+
if (modules)
8458+
modules->emplace_back(swift::ImportedModule(loaded_module));
84268459
}
84278460
return true;
84288461
}

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -773,15 +773,26 @@ class SwiftASTContext : public TypeSystemSwift {
773773
lldb::StackFrameWP &stack_frame_wp,
774774
swift::SourceFile &source_file, Status &error);
775775

776-
/// Retrieve the modules imported by the compilation unit.
777-
static bool GetCompileUnitImports(
778-
SwiftASTContext &swift_ast_context, SymbolContext &sc,
779-
lldb::StackFrameWP &stack_frame_wp,
776+
/// Retrieve/import the modules imported by the compilation
777+
/// unit. Early-exists with false if there was an import failure.
778+
bool GetCompileUnitImports(
779+
SymbolContext &sc, lldb::StackFrameWP &stack_frame_wp,
780780
llvm::SmallVectorImpl<swift::AttributedImport<swift::ImportedModule>>
781781
&modules,
782782
Status &error);
783783

784+
/// Perform all the implicit imports for the current frame.
785+
void PerformCompileUnitImports(SymbolContext &sc,
786+
lldb::StackFrameWP &stack_frame_wp,
787+
Status &error);
788+
784789
protected:
790+
bool GetCompileUnitImportsImpl(
791+
SymbolContext &sc, lldb::StackFrameWP &stack_frame_wp,
792+
llvm::SmallVectorImpl<swift::AttributedImport<swift::ImportedModule>>
793+
*modules,
794+
Status &error);
795+
785796
/// This map uses the string value of ConstStrings as the key, and the
786797
/// TypeBase
787798
/// * as the value. Since the ConstString strings are uniqued, we can use
@@ -867,6 +878,8 @@ class SwiftASTContext : public TypeSystemSwift {
867878
/// respective result (true = loaded, false = failed to load).
868879
std::unordered_map<detail::SwiftLibraryLookupRequest, bool>
869880
library_load_cache;
881+
/// A cache for GetCompileUnitImports();
882+
llvm::DenseSet<std::pair<Module *, lldb::user_id_t>> m_cu_imports;
870883

871884
typedef std::map<Module *, std::vector<lldb::DataBufferSP>> ASTFileDataMap;
872885
ASTFileDataMap m_ast_file_data_map;

lldb/source/Target/Target.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,10 +2557,7 @@ llvm::Optional<SwiftASTContextReader> Target::GetScratchSwiftASTContext(
25572557
!swift_ast_ctx->HasFatalErrors()) {
25582558
StackFrameWP frame_wp(frame_sp);
25592559
SymbolContext sc = frame_sp->GetSymbolContext(lldb::eSymbolContextEverything);
2560-
llvm::SmallVector<swift::AttributedImport<swift::ImportedModule>, 16>
2561-
modules;
2562-
swift_ast_ctx->GetCompileUnitImports(*swift_ast_ctx, sc, frame_wp, modules,
2563-
error);
2560+
swift_ast_ctx->PerformCompileUnitImports(sc, frame_wp, error);
25642561
}
25652562

25662563
if (!swift_ast_ctx)

0 commit comments

Comments
 (0)