diff --git a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp index 43d71736b5ac11..b2ba746ce83def 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp +++ b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp @@ -1193,6 +1193,25 @@ static void printASTValidationError( LLDB_LOG(log, " -- {0}", ExtraOpt); } +std::optional SwiftASTContext::ReportStatistics() { + const auto &load_times = GetSwiftModuleLoadTimes(); + llvm::json::Array swift_module_load_times; + StatsDuration swift_module_total_load_time; + for (const auto &entry : load_times) { + llvm::json::Object obj; + obj.try_emplace("name", entry.first().str()); + obj.try_emplace("loadTime", entry.second.get().count()); + swift_module_total_load_time += entry.second; + swift_module_load_times.emplace_back(std::move(obj)); + } + + llvm::json::Object obj; + obj.try_emplace("swiftmodules", std::move(swift_module_load_times)); + obj.try_emplace("totalLoadTime", swift_module_total_load_time.get().count()); + llvm::json::Value ret = std::move(obj); + return ret; +} + void SwiftASTContext::DiagnoseWarnings(Process &process, const SymbolContext &sc) const { if (!sc.module_sp || !HasDiagnostics()) @@ -3892,6 +3911,10 @@ SwiftASTContext::GetModule(const SourceModule &module, bool *cached) { return *module_decl; } + auto &load_time_map = GetSwiftModuleLoadTimes(); + StatsDuration &load_time = load_time_map[module.path.front().GetStringRef()]; + ElapsedTime elapsed(load_time); + LLDB_SCOPED_TIMER(); ThreadSafeASTContext ast = GetASTContext(); if (!ast) { diff --git a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h index 99a59f1029a9b2..061513c700e649 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h +++ b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h @@ -21,6 +21,8 @@ #include "lldb/Core/ThreadSafeDenseSet.h" #include "lldb/Expression/DiagnosticManager.h" #include "lldb/Utility/Either.h" +#include "lldb/Target/Statistics.h" +#include "llvm/Support/JSON.h" #include "swift/AST/Import.h" #include "swift/AST/Module.h" @@ -553,6 +555,15 @@ class SwiftASTContext : public TypeSystemSwift { const SwiftModuleMap &GetModuleCache() { return m_swift_module_cache; } + + typedef llvm::StringMap SwiftModuleLoadTimeMap; + + SwiftModuleLoadTimeMap &GetSwiftModuleLoadTimes() { + return m_swift_module_load_time_map; + } + + std::optional ReportStatistics() override; + const swift::irgen::TypeInfo * GetSwiftTypeInfo(lldb::opaque_compiler_type_t type); @@ -942,6 +953,7 @@ class SwiftASTContext : public TypeSystemSwift { std::shared_ptr m_clangimporter_typesystem; std::unique_ptr m_dwarfimporter_delegate_up; SwiftModuleMap m_swift_module_cache; + SwiftModuleLoadTimeMap m_swift_module_load_time_map; SwiftTypeFromMangledNameMap m_mangled_name_to_type_map; SwiftMangledNameFromTypeMap m_type_to_mangled_name_map; uint32_t m_pointer_byte_size = 0; diff --git a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp index e37da7f8f3512f..fdee904e855d94 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp +++ b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp @@ -2061,6 +2061,15 @@ Status TypeSystemSwiftTypeRef::IsCompatible() { return {}; } +std::optional TypeSystemSwiftTypeRef::ReportStatistics() { + // This SymbolContext is not being used within the function GetSwiftASTContextOrNull + SymbolContext sc; + if (auto *swift_ast_context = GetSwiftASTContextOrNull(sc)) { + return swift_ast_context->ReportStatistics(); + } + return std::nullopt; +} + void TypeSystemSwiftTypeRef::DiagnoseWarnings(Process &process, const SymbolContext &sc) const { // This gets called only from Thread::FrameSelectedCallback(StackFrame). diff --git a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h index f8a0bb93641e38..9b2864cde1eff0 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h +++ b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h @@ -86,6 +86,8 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift { void ClearModuleDependentCaches() override; lldb::TargetWP GetTargetWP() const override { return {}; } + std::optional ReportStatistics() override; + /// Return a SwiftASTContext type for type. CompilerType ReconstructType(CompilerType type, const ExecutionContext *exe_ctx);