Skip to content

Commit fc3baa8

Browse files
Merge pull request #10638 from adrian-prantl/mangling-flavor-speedup
[lldb] Avoid spinnning up a SwiftASTContext just to determine the man…
2 parents e49951f + 5aed050 commit fc3baa8

File tree

6 files changed

+57
-53
lines changed

6 files changed

+57
-53
lines changed

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

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2643,47 +2643,6 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
26432643
return swift_ast_sp;
26442644
}
26452645

2646-
bool SwiftASTContext::CheckFlagInCU(CompileUnit *cu, const char *flag) {
2647-
AutoBool interop_enabled =
2648-
ModuleList::GetGlobalModuleListProperties().GetSwiftEnableCxxInterop();
2649-
switch (interop_enabled) {
2650-
case AutoBool::True:
2651-
return true;
2652-
case AutoBool::False:
2653-
return false;
2654-
case AutoBool::Auto: {
2655-
if (!cu)
2656-
return false;
2657-
lldb::ModuleSP module = cu->CalculateSymbolContextModule();
2658-
if (!module)
2659-
return false;
2660-
auto *sym_file = module->GetSymbolFile();
2661-
if (!sym_file)
2662-
return false;
2663-
auto options = sym_file->GetCompileOptions();
2664-
for (auto &[unit, args] : options) {
2665-
if (unit.get() == cu) {
2666-
if (cu->GetLanguage() == eLanguageTypeSwift)
2667-
for (const char *arg : args.GetArgumentArrayRef())
2668-
if (strcmp(arg, flag) == 0)
2669-
return true;
2670-
return false;
2671-
}
2672-
}
2673-
}
2674-
}
2675-
return false;
2676-
}
2677-
2678-
/// Determine whether this CU was compiled with C++ interop enabled.
2679-
bool SwiftASTContext::ShouldEnableCXXInterop(CompileUnit *cu) {
2680-
return CheckFlagInCU(cu, "-enable-experimental-cxx-interop");
2681-
}
2682-
2683-
bool SwiftASTContext::ShouldEnableEmbeddedSwift(CompileUnit *cu) {
2684-
return CheckFlagInCU(cu, "-enable-embedded-swift");
2685-
}
2686-
26872646
static bool IsUnitTestExecutable(lldb_private::Module &module) {
26882647
static ConstString s_xctest("xctest");
26892648
static ConstString s_XCTRunner("XCTRunner");

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,6 @@ class SwiftASTContext : public TypeSystemSwift {
210210
TypeSystemSwiftTypeRef &typeref_typesystem,
211211
const char *extra_options = nullptr);
212212

213-
/// Returns true if the given flag is present in the given compile unit.
214-
static bool CheckFlagInCU(CompileUnit *cu, const char *flag);
215-
216-
static bool ShouldEnableCXXInterop(CompileUnit *cu);
217-
218-
static bool ShouldEnableEmbeddedSwift(CompileUnit *cu);
219-
220213
static void EnumerateSupportedLanguages(
221214
std::set<lldb::LanguageType> &languages_for_types,
222215
std::set<lldb::LanguageType> &languages_for_expressions);

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "Plugins/ExpressionParser/Swift/SwiftPersistentExpressionState.h"
1616
#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
1717
#include "lldb/Core/PluginManager.h"
18+
#include "lldb/Symbol/CompileUnit.h"
1819
#include <lldb/lldb-enumerations.h>
1920
#include <llvm/ADT/StringRef.h>
2021

@@ -71,6 +72,47 @@ void TypeSystemSwift::Terminate() {
7172

7273
/// \}
7374

75+
bool TypeSystemSwift::CheckFlagInCU(CompileUnit *cu, const char *flag) {
76+
AutoBool interop_enabled =
77+
ModuleList::GetGlobalModuleListProperties().GetSwiftEnableCxxInterop();
78+
switch (interop_enabled) {
79+
case AutoBool::True:
80+
return true;
81+
case AutoBool::False:
82+
return false;
83+
case AutoBool::Auto: {
84+
if (!cu)
85+
return false;
86+
lldb::ModuleSP module = cu->CalculateSymbolContextModule();
87+
if (!module)
88+
return false;
89+
auto *sym_file = module->GetSymbolFile();
90+
if (!sym_file)
91+
return false;
92+
auto options = sym_file->GetCompileOptions();
93+
for (auto &[unit, args] : options) {
94+
if (unit.get() == cu) {
95+
if (cu->GetLanguage() == eLanguageTypeSwift)
96+
for (const char *arg : args.GetArgumentArrayRef())
97+
if (strcmp(arg, flag) == 0)
98+
return true;
99+
return false;
100+
}
101+
}
102+
}
103+
}
104+
return false;
105+
}
106+
107+
/// Determine whether this CU was compiled with C++ interop enabled.
108+
bool TypeSystemSwift::ShouldEnableCXXInterop(CompileUnit *cu) {
109+
return CheckFlagInCU(cu, "-enable-experimental-cxx-interop");
110+
}
111+
112+
bool TypeSystemSwift::ShouldEnableEmbeddedSwift(CompileUnit *cu) {
113+
return CheckFlagInCU(cu, "-enable-embedded-swift");
114+
}
115+
74116
void TypeSystemSwift::Dump(llvm::raw_ostream &output) {
75117
// TODO: What to dump?
76118
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ class TypeSystemSwift : public TypeSystem {
127127

128128
const std::string &GetDescription() const { return m_description; }
129129
static LanguageSet GetSupportedLanguagesForTypes();
130+
/// Returns true if the given flag is present in the given compile unit.
131+
static bool CheckFlagInCU(CompileUnit *cu, const char *flag);
132+
static bool ShouldEnableCXXInterop(CompileUnit *cu);
133+
static bool ShouldEnableEmbeddedSwift(CompileUnit *cu);
134+
130135
virtual SwiftASTContextSP
131136
GetSwiftASTContext(const SymbolContext &sc) const = 0;
132137
virtual TypeSystemSwiftTypeRefSP GetTypeSystemSwiftTypeRef() = 0;

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5333,11 +5333,13 @@ TypeSystemSwiftTypeRef::GetDependentGenericParamListForType(
53335333
swift::Mangle::ManglingFlavor
53345334
TypeSystemSwiftTypeRef::GetManglingFlavor(ExecutionContext *exe_ctx) {
53355335
auto sc = GetSymbolContext(exe_ctx);
5336-
if (auto ast_ctx = GetSwiftASTContext(sc))
5337-
return ast_ctx->GetManglingFlavor();
5338-
LLDB_LOG(GetLog(LLDBLog::Types),
5339-
"GetManglingFlavor failed to acquire a SwiftASTContext");
5340-
return swift::Mangle::ManglingFlavor::Default;
5336+
auto *cu = sc.comp_unit;
5337+
// Cache the result for the last recently used CU.
5338+
if (cu != m_lru_is_embedded.first)
5339+
m_lru_is_embedded = {cu, ShouldEnableEmbeddedSwift(sc.comp_unit)
5340+
? swift::Mangle::ManglingFlavor::Embedded
5341+
: swift::Mangle::ManglingFlavor::Default};
5342+
return m_lru_is_embedded.second;
53415343
}
53425344

53435345
#ifndef NDEBUG

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,9 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
597597

598598
/// All lldb::Type pointers produced by DWARFASTParser Swift go here.
599599
ThreadSafeDenseMap<const char *, lldb::TypeSP> m_swift_type_map;
600+
/// An LRU cache for \ref GetManglingFlavor().
601+
std::pair<CompileUnit *, swift::Mangle::ManglingFlavor> m_lru_is_embedded = {
602+
nullptr, swift::Mangle::ManglingFlavor::Default};
600603
};
601604

602605
/// This one owns a SwiftASTContextForExpressions.

0 commit comments

Comments
 (0)