Skip to content

Commit 88069cf

Browse files
committed
[lldb][progress] Improve Swift progress reporting (llvm#7769)
LLDB's progress reporting infrastructure has had the ability to add details to an overall progress report (added in https://reviews.llvm.org/D143690) but this had yet to be implemented in existing progress reports. This commit adds this functionality to progress reports made for Swift operations (such as caching user imports) so that instead of sending several individual progress reports for each step in these operations, one progress report is created and then incrementally updated. Importing Swift modules takes place in the Swift compiler which uses a callback function so that LLDB can perform its progress report. This originally called into a function in `lldb_private::SwiftASTContext` but will instead use a lambda function to increment a locally created progress report. Related to: swiftlang/swift#69730 rdar://105286354 (cherry picked from commit 4106480)
1 parent 4371e92 commit 88069cf

File tree

4 files changed

+76
-34
lines changed

4 files changed

+76
-34
lines changed

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,18 +504,15 @@ void SwiftLanguageRuntimeImpl::ProcessModulesToAdd() {
504504

505505
auto &target = m_process.GetTarget();
506506
auto exe_module = target.GetExecutableModule();
507-
Progress progress(
508-
llvm::formatv("Setting up Swift reflection for '{0}'",
509-
exe_module->GetFileSpec().GetFilename().AsCString()),
510-
modules_to_add_snapshot.GetSize());
511-
507+
Progress progress("Setting up Swift reflection");
512508
size_t completion = 0;
513509

514510
// Add all defered modules to reflection context that were added to
515511
// the target since this SwiftLanguageRuntime was created.
516512
modules_to_add_snapshot.ForEach([&](const ModuleSP &module_sp) -> bool {
517513
AddModuleToReflectionContext(module_sp);
518-
progress.Increment(++completion);
514+
progress.Increment(++completion,
515+
module_sp->GetFileSpec().GetFilename().AsCString());
519516
return true;
520517
});
521518
}

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

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
122122
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
123123

124+
#include <memory>
124125
#include <mutex>
125126
#include <queue>
126127
#include <set>
@@ -2018,6 +2019,25 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
20182019
{
20192020
LLDB_SCOPED_TIMERF("%s (getStdlibModule)", m_description.c_str());
20202021
const bool can_create = true;
2022+
2023+
// Report progress on module importing by using a callback function in
2024+
// swift::ASTContext
2025+
Progress progress("Importing Swift standard library");
2026+
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
2027+
[&progress](llvm::StringRef module_name, bool is_overlay) {
2028+
progress.Increment(1, (is_overlay ? module_name.str() + " (overlay)"
2029+
: module_name.str()));
2030+
});
2031+
2032+
// Clear the callback function on scope exit to prevent an out-of-scope
2033+
// access of the progress local variable
2034+
auto on_exit = llvm::make_scope_exit([&]() {
2035+
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
2036+
[](llvm::StringRef module_name, bool is_overlay) {
2037+
Progress("Importing Swift modules");
2038+
});
2039+
});
2040+
20212041
swift::ModuleDecl *stdlib =
20222042
swift_ast_sp->m_ast_context_ap->getStdlibModule(can_create);
20232043
if (!stdlib || IsDWARFImported(*stdlib)) {
@@ -2511,6 +2531,25 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
25112531
{
25122532
LLDB_SCOPED_TIMERF("%s (getStdlibModule)", m_description.c_str());
25132533
const bool can_create = true;
2534+
2535+
// Report progress on module importing by using a callback function in
2536+
// swift::ASTContext
2537+
Progress progress("Importing Swift standard library");
2538+
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
2539+
[&progress](llvm::StringRef module_name, bool is_overlay) {
2540+
progress.Increment(1, (is_overlay ? module_name.str() + " (overlay)"
2541+
: module_name.str()));
2542+
});
2543+
2544+
// Clear the callback function on scope exit to prevent an out-of-scope
2545+
// access of the progress local variable
2546+
auto on_exit = llvm::make_scope_exit([&]() {
2547+
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
2548+
[](llvm::StringRef module_name, bool is_overlay) {
2549+
Progress("Importing Swift modules");
2550+
});
2551+
});
2552+
25142553
swift::ModuleDecl *stdlib =
25152554
swift_ast_sp->m_ast_context_ap->getStdlibModule(can_create);
25162555
if (!stdlib || IsDWARFImported(*stdlib)) {
@@ -3217,7 +3256,7 @@ swift::ASTContext *SwiftASTContext::GetASTContext() {
32173256
GetLanguageOptions(), GetTypeCheckerOptions(), GetSILOptions(),
32183257
GetSearchPathOptions(), GetClangImporterOptions(),
32193258
GetSymbolGraphOptions(), GetSourceManager(), GetDiagnosticEngine(),
3220-
/*OutputBackend=*/nullptr, ReportModuleLoadingProgress));
3259+
/*OutputBackend=*/nullptr));
32213260

32223261
if (getenv("LLDB_SWIFT_DUMP_DIAGS")) {
32233262
// NOTE: leaking a swift::PrintingDiagnosticConsumer() here, but
@@ -3501,15 +3540,6 @@ void SwiftASTContext::CacheModule(swift::ModuleDecl *module) {
35013540
m_swift_module_cache.insert({ID, module});
35023541
}
35033542

3504-
bool SwiftASTContext::ReportModuleLoadingProgress(llvm::StringRef module_name,
3505-
bool is_overlay) {
3506-
Progress progress(llvm::formatv(is_overlay ? "Importing overlay module {0}"
3507-
: "Importing module {0}",
3508-
module_name)
3509-
.str());
3510-
return true;
3511-
}
3512-
35133543
swift::ModuleDecl *SwiftASTContext::GetModule(const SourceModule &module,
35143544
Status &error, bool *cached) {
35153545
if (cached)
@@ -3560,6 +3590,24 @@ swift::ModuleDecl *SwiftASTContext::GetModule(const SourceModule &module,
35603590
// Create a diagnostic consumer for the diagnostics produced by the import.
35613591
auto import_diags = getScopedDiagnosticConsumer();
35623592

3593+
// Report progress on module importing by using a callback function in
3594+
// swift::ASTContext
3595+
Progress progress("Importing Swift modules");
3596+
ast->SetPreModuleImportCallback([&progress](llvm::StringRef module_name,
3597+
bool is_overlay) {
3598+
progress.Increment(
3599+
1, (is_overlay ? module_name.str() + " (overlay)" : module_name.str()));
3600+
});
3601+
3602+
// Clear the callback function on scope exit to prevent an out-of-scope access
3603+
// of the progress local variable
3604+
auto on_exit = llvm::make_scope_exit([&]() {
3605+
ast->SetPreModuleImportCallback(
3606+
[](llvm::StringRef module_name, bool is_overlay) {
3607+
Progress("Importing Swift modules");
3608+
});
3609+
});
3610+
35633611
// Perform the import.
35643612
swift::ModuleDecl *module_decl = ast->getModuleByName(module_basename_sref);
35653613

@@ -4167,10 +4215,9 @@ void SwiftASTContext::ValidateSectionModules(
41674215
Status error;
41684216

41694217
Progress progress(
4170-
llvm::formatv("Loading Swift module {0}",
4218+
llvm::formatv("Loading Swift module '{0}' dependencies",
41714219
module.GetFileSpec().GetFilename().AsCString()),
41724220
module_names.size());
4173-
41744221
size_t completion = 0;
41754222

41764223
for (const std::string &module_name : module_names) {
@@ -4179,7 +4226,7 @@ void SwiftASTContext::ValidateSectionModules(
41794226

41804227
// We have to increment the completion value even if we can't get the module
41814228
// object to stay in-sync with the total progress reporting.
4182-
progress.Increment(++completion);
4229+
progress.Increment(++completion, module_name);
41834230
if (!GetModule(module_info, error))
41844231
module.ReportWarning("unable to load swift module \"{0}\" ({1})",
41854232
module_name.c_str(), error.AsCString());
@@ -8611,10 +8658,7 @@ bool SwiftASTContextForExpressions::CacheUserImports(
86118658

86128659
auto src_file_imports = source_file.getImports();
86138660

8614-
Progress progress(llvm::formatv("Caching Swift user imports from '{0}'",
8615-
source_file.getFilename().data()),
8616-
src_file_imports.size());
8617-
8661+
Progress progress("Importing modules used in expression");
86188662
size_t completion = 0;
86198663

86208664
/// Find all explicit imports in the expression.
@@ -8632,7 +8676,9 @@ bool SwiftASTContextForExpressions::CacheUserImports(
86328676
source_file.walk(import_finder);
86338677

86348678
for (const auto &attributed_import : src_file_imports) {
8635-
progress.Increment(++completion);
8679+
progress.Increment(
8680+
++completion,
8681+
attributed_import.module.importedModule->getModuleFilename().str());
86368682
swift::ModuleDecl *module = attributed_import.module.importedModule;
86378683
if (module && import_finder.imports.count(module)) {
86388684
std::string module_name;
@@ -8752,10 +8798,9 @@ bool SwiftASTContext::GetCompileUnitImportsImpl(
87528798
llvm::formatv("Getting Swift compile unit imports for '{0}'",
87538799
compile_unit->GetPrimaryFile().GetFilename()),
87548800
cu_imports.size());
8755-
87568801
size_t completion = 0;
87578802
for (const SourceModule &module : cu_imports) {
8758-
progress.Increment(++completion);
8803+
progress.Increment(++completion, module.path.back().GetStringRef().str());
87598804
// When building the Swift stdlib with debug info these will
87608805
// show up in "Swift.o", but we already imported them and
87618806
// manually importing them will fail.

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,6 @@ class SwiftASTContext : public TypeSystemSwift {
301301
swift::ModuleDecl *CreateModule(const SourceModule &module, Status &error,
302302
swift::ImplicitImportInfo importInfo);
303303

304-
static bool ReportModuleLoadingProgress(llvm::StringRef module_name,
305-
bool is_overlay);
306-
307304
// This function should only be called when all search paths
308305
// for all items in a swift::ASTContext have been setup to
309306
// allow for imports to happen correctly. Use with caution,

lldb/test/API/functionalities/progress_reporting/swift_progress_reporting/TestSwiftProgressReporting.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ def test_swift_progress_report(self):
3737
self.runCmd("expr boo")
3838
self.runCmd("v s")
3939

40-
beacons = [ "Loading Swift module",
41-
"Caching Swift user imports from",
42-
"Setting up Swift reflection for",
43-
"Getting Swift compile unit imports for",
44-
"Importing module", "Importing overlay module"]
40+
beacons = [
41+
"Loading Swift module",
42+
"Importing modules used in expression",
43+
"Setting up Swift reflection",
44+
"Getting Swift compile unit imports for",
45+
"Importing Swift modules",
46+
"Importing Swift standard library",
47+
]
4548

4649
while len(beacons):
4750
event = lldbutil.fetch_next_event(self, self.listener, self.broadcaster)

0 commit comments

Comments
 (0)