Skip to content

Commit 3bd6391

Browse files
committed
[lldb] Retire the per-module Swift scratch context fallback
Now that all scratch contexts are using precise per-CU compiler invocations, the infrastructure for the per-module fallback scratch context is no longer needed, since it is actually less precise than the "normal" contexts. This patch removes the notion of a fallback SwiftASTContext entirely.
1 parent 5232c25 commit 3bd6391

File tree

9 files changed

+14
-158
lines changed

9 files changed

+14
-158
lines changed

lldb/include/lldb/Target/Target.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,14 +1587,6 @@ class Target : public std::enable_shared_from_this<Target>,
15871587

15881588
void SetREPL(lldb::LanguageType language, lldb::REPLSP repl_sp);
15891589

1590-
/// Enable the use of a separate sscratch type system per lldb::Module.
1591-
void SetUseScratchTypesystemPerModule(bool value) {
1592-
m_use_scratch_typesystem_per_module = value;
1593-
}
1594-
bool UseScratchTypesystemPerModule() const {
1595-
return m_use_scratch_typesystem_per_module;
1596-
}
1597-
15981590
public:
15991591
StackFrameRecognizerManager &GetFrameRecognizerManager() {
16001592
return *m_frame_recognizer_manager_up;

lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,6 @@ struct ModuleImportError : public llvm::ErrorInfo<ModuleImportError> {
10841084
ModuleImportError(llvm::Twine message, bool is_new_dylib = false)
10851085
: msg(message.str()), is_new_dylib(is_new_dylib) {}
10861086
void log(llvm::raw_ostream &OS) const override {
1087-
OS << "error while processing module import: ";
10881087
OS << msg;
10891088
}
10901089
std::error_code convertToErrorCode() const override {
@@ -1453,7 +1452,8 @@ SwiftExpressionParser::ParseAndImport(
14531452
// fatal error state. One way this can happen is if the import
14541453
// triggered a dylib import, in which case the context is
14551454
// purposefully poisoned.
1456-
msg = "import may have triggered a dylib import";
1455+
msg = "import may have triggered a dylib import, "
1456+
"resetting compiler state";
14571457
}
14581458
return make_error<ModuleImportError>(msg, /*is_new_dylib=*/true);
14591459
}
@@ -1722,14 +1722,8 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
17221722
// There are no fallback contexts in REPL and playgrounds.
17231723
if (repl || playground)
17241724
return;
1725-
if (!m_sc.target_sp->UseScratchTypesystemPerModule()) {
1726-
// This, together with the fatal error forces
1727-
// a per-module scratch to be instantiated on
1728-
// retry.
1729-
m_sc.target_sp->SetUseScratchTypesystemPerModule(true);
1730-
m_swift_ast_ctx.RaiseFatalError(MIE.message());
1731-
retry = true;
1732-
}
1725+
// The fatal error causes a new compiler to be instantiated on retry.
1726+
m_swift_ast_ctx.RaiseFatalError(MIE.message());
17331727
},
17341728
[&](const SwiftASTContextError &SACE) {
17351729
DiagnoseSwiftASTContextError();
@@ -1745,9 +1739,7 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
17451739
});
17461740

17471741
// Signal that we want to retry the expression exactly once with a
1748-
// fresh SwiftASTContext initialized with the flags from the
1749-
// current lldb::Module / Swift dylib to avoid header search
1750-
// mismatches.
1742+
// fresh SwiftASTContext.
17511743
if (retry)
17521744
return ParseResult::retry_fresh_context;
17531745

lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class SwiftExpressionParser : public ExpressionParser {
5050
public:
5151
enum class ParseResult {
5252
success,
53-
retry_fresh_context,
53+
retry_fresh_context,
5454
retry_no_bind_generic_params,
5555
unrecoverable_error
5656
};

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,8 +2347,7 @@ ProcessModule(Module &module, std::string m_description,
23472347

23482348
lldb::TypeSystemSP
23492349
SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
2350-
TypeSystemSwiftTypeRef &typeref_typesystem,
2351-
bool fallback) {
2350+
TypeSystemSwiftTypeRef &typeref_typesystem) {
23522351
TargetSP target = typeref_typesystem.GetTargetWP().lock();
23532352
if (!SwiftASTContextSupportsLanguage(language))
23542353
return lldb::TypeSystemSP();
@@ -2361,10 +2360,7 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
23612360
{
23622361
llvm::raw_string_ostream ss(m_description);
23632362
ss << "SwiftASTContext";
2364-
if (fallback)
2365-
ss << "ForExpressions";
2366-
else
2367-
ss << "ForModule";
2363+
ss << "ForModule";
23682364
ss << '(' << '"';
23692365
module.GetDescription(ss, eDescriptionLevelBrief);
23702366
ss << '"' << ')';
@@ -2424,20 +2420,15 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
24242420
}
24252421

24262422
// If there is a target this may be a fallback scratch context.
2427-
assert((!fallback || target) && "fallback context must specify a target");
2428-
std::shared_ptr<SwiftASTContext> swift_ast_sp(
2429-
fallback
2430-
? static_cast<SwiftASTContext *>(new SwiftASTContextForExpressions(
2431-
m_description, typeref_typesystem))
2432-
: static_cast<SwiftASTContext *>(new SwiftASTContextForModule(
2433-
m_description, typeref_typesystem)));
2423+
std::shared_ptr<SwiftASTContext> swift_ast_sp(static_cast<SwiftASTContext *>(
2424+
new SwiftASTContextForModule(m_description, typeref_typesystem)));
24342425
bool suppress_config_log = false;
24352426
auto defer_log =
2436-
llvm::make_scope_exit([swift_ast_sp, &suppress_config_log, fallback] {
2427+
llvm::make_scope_exit([swift_ast_sp, &suppress_config_log] {
24372428
// To avoid spamming the log with useless info, we don't log the
24382429
// configuration if everything went fine and the current module
24392430
// doesn't have any Swift contents (i.e., the shared cache dylibs).
2440-
if (!suppress_config_log || fallback)
2431+
if (!suppress_config_log)
24412432
swift_ast_sp->LogConfiguration();
24422433
});
24432434

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,10 @@ class SwiftASTContext : public TypeSystemSwift {
198198
/// Create a SwiftASTContext from a Module. This context is used
199199
/// for frame variable and uses ClangImporter options specific to
200200
/// this lldb::Module. The optional target is necessary when
201-
/// creating a module-specific scratch context. If \p fallback is
202-
/// true, then a SwiftASTContextForExpressions is created.
201+
/// creating a module-specific scratch context.
203202
static lldb::TypeSystemSP
204203
CreateInstance(lldb::LanguageType language, Module &module,
205-
TypeSystemSwiftTypeRef &typeref_typesystem,
206-
bool fallback = false);
204+
TypeSystemSwiftTypeRef &typeref_typesystem);
207205
/// Create a SwiftASTContextForExpressions taylored to a specific symbol
208206
/// context.
209207
static lldb::TypeSystemSP

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,21 +1659,6 @@ TypeSystemSwiftTypeRef::TypeSystemSwiftTypeRef(Module &module) {
16591659
m_description.c_str());
16601660
}
16611661

1662-
TypeSystemSwiftTypeRefForExpressions::TypeSystemSwiftTypeRefForExpressions(
1663-
lldb::LanguageType language, Target &target, Module &module)
1664-
: TypeSystemSwiftTypeRef(module), m_target_wp(target.shared_from_this()),
1665-
m_persistent_state_up(new SwiftPersistentExpressionState) {
1666-
m_description = "TypeSystemSwiftTypeRefForExpressions(PerModuleFallback)";
1667-
LLDB_LOGF(GetLog(LLDBLog::Types),
1668-
"%s::TypeSystemSwiftTypeRefForExpressions()",
1669-
m_description.c_str());
1670-
m_swift_ast_context_map.insert(
1671-
{nullptr,
1672-
SwiftASTContext::CreateInstance(
1673-
LanguageType::eLanguageTypeSwift, module,
1674-
*const_cast<TypeSystemSwiftTypeRefForExpressions *>(this), true)});
1675-
}
1676-
16771662
TypeSystemSwiftTypeRefForExpressions::TypeSystemSwiftTypeRefForExpressions(
16781663
lldb::LanguageType language, Target &target, const char *extra_options)
16791664
: m_target_wp(target.shared_from_this()),

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -520,10 +520,6 @@ class TypeSystemSwiftTypeRefForExpressions : public TypeSystemSwiftTypeRef {
520520
Target &target,
521521
const char *extra_options);
522522

523-
/// For per-module fallback contexts.
524-
TypeSystemSwiftTypeRefForExpressions(lldb::LanguageType language,
525-
Target &target, Module &module);
526-
527523
SwiftASTContext *GetSwiftASTContext(const SymbolContext &sc) const override;
528524
SwiftASTContext *
529525
GetSwiftASTContextOrNull(const SymbolContext &sc) const override;

lldb/source/Target/Target.cpp

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2932,96 +2932,7 @@ Target::GetSwiftScratchContext(Status &error, ExecutionContextScope &exe_scope,
29322932
return nullptr;
29332933
};
29342934

2935-
auto maybe_create_fallback_context = [&]() {
2936-
ModuleLanguage key = {lldb_module, lldb::eLanguageTypeSwift};
2937-
if (auto *cached_ts = get_cached_module_ts(lldb_module)) {
2938-
auto *cached_ast_ctx =
2939-
llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
2940-
cached_ts->GetSwiftASTContext(sc));
2941-
if (cached_ast_ctx && cached_ast_ctx->HasFatalErrors() &&
2942-
!m_cant_make_scratch_type_system.count(lldb::eLanguageTypeSwift)) {
2943-
DisplayFallbackSwiftContextErrors(cached_ast_ctx);
2944-
// Try again.
2945-
// FIXME: Shouldn't this continue rather than return?
2946-
auto &lock = GetSwiftScratchContextLock();
2947-
if (!lock.try_lock()) {
2948-
if (log)
2949-
log->Printf("module scratch context has errors but couldn't "
2950-
"acquire scratch context lock\n");
2951-
return;
2952-
}
2953-
std::lock_guard<std::shared_mutex> unlock(lock, std::adopt_lock);
2954-
m_scratch_typesystem_for_module.erase(key);
2955-
if (log)
2956-
log->Printf("erased module-wide scratch context with errors\n");
2957-
return;
2958-
}
2959-
if (log)
2960-
log->PutCString("returned cached module-wide scratch context");
2961-
return;
2962-
}
2963-
2964-
if (!create_on_demand) {
2965-
if (log)
2966-
log->PutCString("not allowed to create a new context");
2967-
return;
2968-
}
2969-
2970-
// Call for its side effects of establishing the Swift scratch type
2971-
// system.
2972-
auto type_system_or_err =
2973-
GetScratchTypeSystemForLanguage(eLanguageTypeSwift, false);
2974-
if (!type_system_or_err) {
2975-
llvm::consumeError(type_system_or_err.takeError());
2976-
return;
2977-
}
2978-
2979-
auto &lock = GetSwiftScratchContextLock();
2980-
if (!lock.try_lock()) {
2981-
if (log)
2982-
log->PutCString("couldn't acquire scratch context lock");
2983-
return;
2984-
}
2985-
std::lock_guard<std::shared_mutex> unlock(lock, std::adopt_lock);
2986-
2987-
// With the lock held, get the current scratch type system. This ensures
2988-
// the current instance is used even in the unlikely event it was changed
2989-
// during the brief window between the call to
2990-
// `GetScratchTypeSystemForLanguage` and taking the lock.
2991-
type_system_or_err = m_scratch_type_system_map.GetTypeSystemForLanguage(
2992-
eLanguageTypeSwift, this, false);
2993-
if (!type_system_or_err) {
2994-
llvm::consumeError(type_system_or_err.takeError());
2995-
return;
2996-
}
2997-
2998-
if (auto *global_scratch_ctx =
2999-
llvm::cast_or_null<TypeSystemSwiftTypeRefForExpressions>(
3000-
type_system_or_err->get()))
3001-
if (auto *swift_ast_ctx =
3002-
llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
3003-
global_scratch_ctx->GetSwiftASTContext(sc)))
3004-
DisplayFallbackSwiftContextErrors(swift_ast_ctx);
3005-
3006-
auto typesystem_sp = std::make_shared<TypeSystemSwiftTypeRefForExpressions>(
3007-
lldb::eLanguageTypeSwift, *this, *lldb_module);
3008-
typesystem_sp->GetSwiftASTContext(sc);
3009-
m_scratch_typesystem_for_module.insert({key, typesystem_sp});
3010-
if (log)
3011-
log->PutCString("created module-wide scratch context");
3012-
return;
3013-
};
3014-
30152935
std::optional<SwiftScratchContextReader> reader;
3016-
if (lldb_module && m_use_scratch_typesystem_per_module) {
3017-
maybe_create_fallback_context();
3018-
std::shared_lock<std::shared_mutex> lock(GetSwiftScratchContextLock());
3019-
if (auto *cached_ts = get_cached_module_ts(lldb_module)) {
3020-
reader = SwiftScratchContextReader(std::move(lock), *cached_ts);
3021-
if (log)
3022-
log->PutCString("returned project-wide scratch context");
3023-
}
3024-
}
30252936
// FIXME: Don't return the project-wide context after requesting the
30262937
// module-wide one.
30272938
if (!reader) {

lldb/test/API/lang/swift/clangimporter/hard_macro_conflict/TestSwiftHardMacroConflict.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,3 @@ def test(self):
3333
threads = lldbutil.get_threads_stopped_at_breakpoint(
3434
process, b_breakpoint)
3535
self.expect("expression foo", error=True)
36-
37-
per_module_fallback = 0
38-
import io
39-
with open(log, "r", encoding='utf-8') as logfile:
40-
for line in logfile:
41-
if 'SwiftASTContextForExpressions("Framework")' in line:
42-
per_module_fallback += 1
43-
self.assertGreater(per_module_fallback, 0,
44-
"failed to create per-module scratch context")

0 commit comments

Comments
 (0)