Skip to content

Commit d376aac

Browse files
[lldb][progress] Improve Swift progress reporting (#7769) (#8098)
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 e6c1e69 commit d376aac

File tree

3 files changed

+69
-26
lines changed

3 files changed

+69
-26
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: 66 additions & 17 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

@@ -4169,6 +4217,7 @@ void SwiftASTContext::ValidateSectionModules(
41694217
Progress progress("Loading Swift module '{0}' dependencies",
41704218
module.GetFileSpec().GetFilename().AsCString(),
41714219
module_names.size());
4220+
41724221
size_t completion = 0;
41734222

41744223
for (const std::string &module_name : module_names) {
@@ -4177,7 +4226,7 @@ void SwiftASTContext::ValidateSectionModules(
41774226

41784227
// We have to increment the completion value even if we can't get the module
41794228
// object to stay in-sync with the total progress reporting.
4180-
progress.Increment(++completion);
4229+
progress.Increment(++completion, module_name);
41814230
if (!GetModule(module_info, error))
41824231
module.ReportWarning("unable to load swift module \"{0}\" ({1})",
41834232
module_name.c_str(), error.AsCString());
@@ -8609,10 +8658,7 @@ bool SwiftASTContextForExpressions::CacheUserImports(
86098658

86108659
auto src_file_imports = source_file.getImports();
86118660

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

86188664
/// Find all explicit imports in the expression.
@@ -8630,7 +8676,9 @@ bool SwiftASTContextForExpressions::CacheUserImports(
86308676
source_file.walk(import_finder);
86318677

86328678
for (const auto &attributed_import : src_file_imports) {
8633-
progress.Increment(++completion);
8679+
progress.Increment(
8680+
++completion,
8681+
attributed_import.module.importedModule->getModuleFilename().str());
86348682
swift::ModuleDecl *module = attributed_import.module.importedModule;
86358683
if (module && import_finder.imports.count(module)) {
86368684
std::string module_name;
@@ -8749,9 +8797,10 @@ bool SwiftASTContext::GetCompileUnitImportsImpl(
87498797
Progress progress("Getting Swift compile unit imports",
87508798
compile_unit->GetPrimaryFile().GetFilename().GetCString(),
87518799
cu_imports.size());
8800+
87528801
size_t completion = 0;
87538802
for (const SourceModule &module : cu_imports) {
8754-
progress.Increment(++completion);
8803+
progress.Increment(++completion, module.path.back().GetStringRef().str());
87558804
// When building the Swift stdlib with debug info these will
87568805
// show up in "Swift.o", but we already imported them and
87578806
// 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,

0 commit comments

Comments
 (0)