Skip to content

[lldb][progress] Improve Swift progress reporting (#7769) #8098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -504,18 +504,15 @@ void SwiftLanguageRuntimeImpl::ProcessModulesToAdd() {

auto &target = m_process.GetTarget();
auto exe_module = target.GetExecutableModule();
Progress progress(
llvm::formatv("Setting up Swift reflection for '{0}'",
exe_module->GetFileSpec().GetFilename().AsCString()),
modules_to_add_snapshot.GetSize());

Progress progress("Setting up Swift reflection");
size_t completion = 0;

// Add all defered modules to reflection context that were added to
// the target since this SwiftLanguageRuntime was created.
modules_to_add_snapshot.ForEach([&](const ModuleSP &module_sp) -> bool {
AddModuleToReflectionContext(module_sp);
progress.Increment(++completion);
progress.Increment(++completion,
module_sp->GetFileSpec().GetFilename().AsCString());
return true;
});
}
Expand Down
83 changes: 66 additions & 17 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"

#include <memory>
#include <mutex>
#include <queue>
#include <set>
Expand Down Expand Up @@ -2018,6 +2019,25 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
{
LLDB_SCOPED_TIMERF("%s (getStdlibModule)", m_description.c_str());
const bool can_create = true;

// Report progress on module importing by using a callback function in
// swift::ASTContext
Progress progress("Importing Swift standard library");
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
[&progress](llvm::StringRef module_name, bool is_overlay) {
progress.Increment(1, (is_overlay ? module_name.str() + " (overlay)"
: module_name.str()));
});

// Clear the callback function on scope exit to prevent an out-of-scope
// access of the progress local variable
auto on_exit = llvm::make_scope_exit([&]() {
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
[](llvm::StringRef module_name, bool is_overlay) {
Progress("Importing Swift modules");
});
});

swift::ModuleDecl *stdlib =
swift_ast_sp->m_ast_context_ap->getStdlibModule(can_create);
if (!stdlib || IsDWARFImported(*stdlib)) {
Expand Down Expand Up @@ -2511,6 +2531,25 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
{
LLDB_SCOPED_TIMERF("%s (getStdlibModule)", m_description.c_str());
const bool can_create = true;

// Report progress on module importing by using a callback function in
// swift::ASTContext
Progress progress("Importing Swift standard library");
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
[&progress](llvm::StringRef module_name, bool is_overlay) {
progress.Increment(1, (is_overlay ? module_name.str() + " (overlay)"
: module_name.str()));
});

// Clear the callback function on scope exit to prevent an out-of-scope
// access of the progress local variable
auto on_exit = llvm::make_scope_exit([&]() {
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
[](llvm::StringRef module_name, bool is_overlay) {
Progress("Importing Swift modules");
});
});

swift::ModuleDecl *stdlib =
swift_ast_sp->m_ast_context_ap->getStdlibModule(can_create);
if (!stdlib || IsDWARFImported(*stdlib)) {
Expand Down Expand Up @@ -3217,7 +3256,7 @@ swift::ASTContext *SwiftASTContext::GetASTContext() {
GetLanguageOptions(), GetTypeCheckerOptions(), GetSILOptions(),
GetSearchPathOptions(), GetClangImporterOptions(),
GetSymbolGraphOptions(), GetSourceManager(), GetDiagnosticEngine(),
/*OutputBackend=*/nullptr, ReportModuleLoadingProgress));
/*OutputBackend=*/nullptr));

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

bool SwiftASTContext::ReportModuleLoadingProgress(llvm::StringRef module_name,
bool is_overlay) {
Progress progress(llvm::formatv(is_overlay ? "Importing overlay module {0}"
: "Importing module {0}",
module_name)
.str());
return true;
}

swift::ModuleDecl *SwiftASTContext::GetModule(const SourceModule &module,
Status &error, bool *cached) {
if (cached)
Expand Down Expand Up @@ -3560,6 +3590,24 @@ swift::ModuleDecl *SwiftASTContext::GetModule(const SourceModule &module,
// Create a diagnostic consumer for the diagnostics produced by the import.
auto import_diags = getScopedDiagnosticConsumer();

// Report progress on module importing by using a callback function in
// swift::ASTContext
Progress progress("Importing Swift modules");
ast->SetPreModuleImportCallback([&progress](llvm::StringRef module_name,
bool is_overlay) {
progress.Increment(
1, (is_overlay ? module_name.str() + " (overlay)" : module_name.str()));
});

// Clear the callback function on scope exit to prevent an out-of-scope access
// of the progress local variable
auto on_exit = llvm::make_scope_exit([&]() {
ast->SetPreModuleImportCallback(
[](llvm::StringRef module_name, bool is_overlay) {
Progress("Importing Swift modules");
});
});

// Perform the import.
swift::ModuleDecl *module_decl = ast->getModuleByName(module_basename_sref);

Expand Down Expand Up @@ -4169,6 +4217,7 @@ void SwiftASTContext::ValidateSectionModules(
Progress progress("Loading Swift module '{0}' dependencies",
module.GetFileSpec().GetFilename().AsCString(),
module_names.size());

size_t completion = 0;

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

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

auto src_file_imports = source_file.getImports();

Progress progress(llvm::formatv("Caching Swift user imports from '{0}'",
source_file.getFilename().data()),
src_file_imports.size());

Progress progress("Importing modules used in expression");
size_t completion = 0;

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

for (const auto &attributed_import : src_file_imports) {
progress.Increment(++completion);
progress.Increment(
++completion,
attributed_import.module.importedModule->getModuleFilename().str());
swift::ModuleDecl *module = attributed_import.module.importedModule;
if (module && import_finder.imports.count(module)) {
std::string module_name;
Expand Down Expand Up @@ -8749,9 +8797,10 @@ bool SwiftASTContext::GetCompileUnitImportsImpl(
Progress progress("Getting Swift compile unit imports",
compile_unit->GetPrimaryFile().GetFilename().GetCString(),
cu_imports.size());

size_t completion = 0;
for (const SourceModule &module : cu_imports) {
progress.Increment(++completion);
progress.Increment(++completion, module.path.back().GetStringRef().str());
// When building the Swift stdlib with debug info these will
// show up in "Swift.o", but we already imported them and
// manually importing them will fail.
Expand Down
3 changes: 0 additions & 3 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,6 @@ class SwiftASTContext : public TypeSystemSwift {
swift::ModuleDecl *CreateModule(const SourceModule &module, Status &error,
swift::ImplicitImportInfo importInfo);

static bool ReportModuleLoadingProgress(llvm::StringRef module_name,
bool is_overlay);

// This function should only be called when all search paths
// for all items in a swift::ASTContext have been setup to
// allow for imports to happen correctly. Use with caution,
Expand Down