Skip to content

Commit 5ac4629

Browse files
Merge pull request #3896 from adrian-prantl/88458070
Sink unconditional scratch context initialization in GetDynamicTypeAn
2 parents 70cb106 + 95bbc4f commit 5ac4629

File tree

6 files changed

+199
-202
lines changed

6 files changed

+199
-202
lines changed

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeDynamicTypeResolution.cpp

Lines changed: 38 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,9 +1561,8 @@ static bool IsPrivateNSClass(NodePointer node) {
15611561
}
15621562

15631563
bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Class(
1564-
ValueObject &in_value, SwiftASTContextForExpressions &scratch_ctx,
1565-
lldb::DynamicValueType use_dynamic, TypeAndOrName &class_type_or_name,
1566-
Address &address) {
1564+
ValueObject &in_value, lldb::DynamicValueType use_dynamic,
1565+
TypeAndOrName &class_type_or_name, Address &address) {
15671566
AddressType address_type;
15681567
lldb::addr_t instance_ptr = in_value.GetPointerValue(&address_type);
15691568
if (instance_ptr == LLDB_INVALID_ADDRESS || instance_ptr == 0)
@@ -1612,7 +1611,17 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Class(
16121611
class_type_or_name.SetCompilerType(ts.RemangleAsType(dem, node));
16131612

16141613
#ifndef NDEBUG
1615-
auto &remote_ast = GetRemoteASTContext(scratch_ctx);
1614+
// Dynamic type resolution in RemoteAST might pull in other Swift modules, so
1615+
// use the scratch context where such operations are legal and safe.
1616+
llvm::Optional<SwiftScratchContextReader> maybe_scratch_ctx =
1617+
in_value.GetSwiftScratchContext();
1618+
if (!maybe_scratch_ctx)
1619+
return false;
1620+
SwiftASTContextForExpressions *scratch_ctx = maybe_scratch_ctx->get();
1621+
if (!scratch_ctx)
1622+
return true;
1623+
1624+
auto &remote_ast = GetRemoteASTContext(*scratch_ctx);
16161625
auto remote_ast_metadata_address = remote_ast.getHeapMetadataForObject(
16171626
swift::remote::RemoteAddress(instance_ptr));
16181627
if (remote_ast_metadata_address) {
@@ -1705,14 +1714,24 @@ bool SwiftLanguageRuntimeImpl::IsValidErrorValue(ValueObject &in_value) {
17051714

17061715
bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Protocol(
17071716
ValueObject &in_value, CompilerType protocol_type,
1708-
SwiftASTContextForExpressions &scratch_ctx,
17091717
lldb::DynamicValueType use_dynamic, TypeAndOrName &class_type_or_name,
17101718
Address &address) {
17111719
auto remote_ast_impl = [&](bool use_local_buffer,
17121720
lldb::addr_t existential_address)
17131721
-> llvm::Optional<std::pair<CompilerType, Address>> {
1722+
// Dynamic type resolution in RemoteAST might pull in other Swift modules,
1723+
// so
1724+
// use the scratch context where such operations are legal and safe.
1725+
llvm::Optional<SwiftScratchContextReader> maybe_scratch_ctx =
1726+
in_value.GetSwiftScratchContext();
1727+
if (!maybe_scratch_ctx)
1728+
return {};
1729+
SwiftASTContextForExpressions *scratch_ctx = maybe_scratch_ctx->get();
1730+
if (!scratch_ctx)
1731+
return {};
1732+
17141733
swift::remote::RemoteAddress remote_existential(existential_address);
1715-
auto &remote_ast = GetRemoteASTContext(scratch_ctx);
1734+
auto &remote_ast = GetRemoteASTContext(*scratch_ctx);
17161735
auto swift_type = GetSwiftType(protocol_type);
17171736
if (!swift_type)
17181737
return {};
@@ -1737,11 +1756,6 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Protocol(
17371756
};
17381757

17391758
Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES));
1740-
1741-
auto &target = m_process.GetTarget();
1742-
assert(IsScratchContextLocked(target) &&
1743-
"Swift scratch context not locked ahead");
1744-
17451759
auto *tss =
17461760
llvm::dyn_cast_or_null<TypeSystemSwift>(protocol_type.GetTypeSystem());
17471761
if (!tss) {
@@ -2017,9 +2031,6 @@ CompilerType
20172031
SwiftLanguageRuntimeImpl::BindGenericTypeParameters(StackFrame &stack_frame,
20182032
CompilerType base_type) {
20192033
LLDB_SCOPED_TIMER();
2020-
auto &target = m_process.GetTarget();
2021-
assert(IsScratchContextLocked(target) &&
2022-
"Swift scratch context not locked ahead of archetype binding");
20232034

20242035
// If this is a TypeRef type, bind that.
20252036
auto sc = stack_frame.GetSymbolContext(lldb::eSymbolContextEverything);
@@ -2029,6 +2040,10 @@ SwiftLanguageRuntimeImpl::BindGenericTypeParameters(StackFrame &stack_frame,
20292040
base_type.GetMangledTypeName());
20302041

20312042
Status error;
2043+
auto &target = m_process.GetTarget();
2044+
assert(IsScratchContextLocked(target) &&
2045+
"Swift scratch context not locked ahead of archetype binding");
2046+
20322047
// A failing Clang import in a module context permanently damages
20332048
// that module context. Binding archetypes can trigger an import of
20342049
// another module, so switch to a scratch context where such an
@@ -2545,59 +2560,26 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress(
25452560
if (!CouldHaveDynamicValue(in_value))
25462561
return false;
25472562

2548-
// Dynamic type resolution in RemoteAST might pull in other Swift modules, so
2549-
// use the scratch context where such operations are legal and safe.
2550-
assert(IsScratchContextLocked(in_value.GetTargetSP()) &&
2551-
"Swift scratch context not locked ahead of dynamic type resolution");
2552-
llvm::Optional<SwiftScratchContextReader> maybe_scratch_ctx =
2553-
in_value.GetSwiftScratchContext();
2554-
if (!maybe_scratch_ctx)
2555-
return false;
2556-
SwiftASTContextForExpressions *scratch_ctx = maybe_scratch_ctx->get();
2557-
2558-
auto retry_once = [&]() {
2559-
// Retry exactly once using the per-module fallback scratch context.
2560-
auto &target = m_process.GetTarget();
2561-
if (!target.UseScratchTypesystemPerModule()) {
2562-
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES));
2563-
if (log)
2564-
log->Printf("Dynamic type resolution detected fatal errors in "
2565-
"shared Swift state. Falling back to per-module "
2566-
"scratch context.\n");
2567-
target.SetUseScratchTypesystemPerModule(true);
2568-
return GetDynamicTypeAndAddress(in_value, use_dynamic, class_type_or_name,
2569-
address, value_type);
2570-
}
2571-
return false;
2572-
};
2573-
2574-
if (scratch_ctx->HasFatalErrors())
2575-
return retry_once();
2576-
2577-
// Import the type into the scratch context. Any form of dynamic
2578-
// type resolution may trigger a cross-module import.
25792563
CompilerType val_type(in_value.GetCompilerType());
25802564
Flags type_info(val_type.GetTypeInfo());
25812565
if (!type_info.AnySet(eTypeIsSwift))
25822566
return false;
25832567

25842568
bool success = false;
25852569
bool is_indirect_enum_case = IsIndirectEnumCase(in_value);
2586-
// Type kinds with metadata don't need archetype binding.
2570+
// Type kinds with instance metadata don't need generic type resolution.
25872571
if (is_indirect_enum_case)
2588-
// ..._IndirectEnumCase() recurses, no need to bind archetypes.
25892572
success = GetDynamicTypeAndAddress_IndirectEnumCase(
25902573
in_value, use_dynamic, class_type_or_name, address);
25912574
else if (type_info.AnySet(eTypeIsClass) ||
25922575
type_info.AllSet(eTypeIsBuiltIn | eTypeIsPointer | eTypeHasValue))
2593-
success = GetDynamicTypeAndAddress_Class(
2594-
in_value, *scratch_ctx, use_dynamic, class_type_or_name, address);
2576+
success = GetDynamicTypeAndAddress_Class(in_value, use_dynamic,
2577+
class_type_or_name, address);
25952578
else if (type_info.AnySet(eTypeIsProtocol))
2596-
success = GetDynamicTypeAndAddress_Protocol(in_value, val_type,
2597-
*scratch_ctx, use_dynamic,
2579+
success = GetDynamicTypeAndAddress_Protocol(in_value, val_type, use_dynamic,
25982580
class_type_or_name, address);
25992581
else {
2600-
// Perform archetype binding in the scratch context.
2582+
// Perform generic type resolution.
26012583
StackFrameSP frame = in_value.GetExecutionContextRef().GetFrameSP();
26022584
if (!frame)
26032585
return false;
@@ -2608,12 +2590,11 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress(
26082590

26092591
Flags subst_type_info(bound_type.GetTypeInfo());
26102592
if (subst_type_info.AnySet(eTypeIsClass)) {
2611-
success = GetDynamicTypeAndAddress_Class(
2612-
in_value, *scratch_ctx, use_dynamic, class_type_or_name, address);
2593+
success = GetDynamicTypeAndAddress_Class(in_value, use_dynamic,
2594+
class_type_or_name, address);
26132595
} else if (subst_type_info.AnySet(eTypeIsProtocol)) {
2614-
success = GetDynamicTypeAndAddress_Protocol(in_value, bound_type,
2615-
*scratch_ctx, use_dynamic,
2616-
class_type_or_name, address);
2596+
success = GetDynamicTypeAndAddress_Protocol(
2597+
in_value, bound_type, use_dynamic, class_type_or_name, address);
26172598
} else {
26182599
success = GetDynamicTypeAndAddress_Value(
26192600
in_value, bound_type, use_dynamic, class_type_or_name, address);
@@ -2623,8 +2604,6 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress(
26232604
if (success)
26242605
value_type = GetValueType(in_value, class_type_or_name.GetCompilerType(),
26252606
is_indirect_enum_case);
2626-
else if (scratch_ctx->HasFatalErrors())
2627-
return retry_once();
26282607
return success;
26292608
}
26302609

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeImpl.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,16 @@ class SwiftLanguageRuntimeImpl {
257257
CompilerType dynamic_type,
258258
bool is_indirect_enum_case);
259259

260-
bool GetDynamicTypeAndAddress_Class(
261-
ValueObject &in_value, SwiftASTContextForExpressions &scratch_ctx,
262-
lldb::DynamicValueType use_dynamic, TypeAndOrName &class_type_or_name,
263-
Address &address);
264-
265-
bool GetDynamicTypeAndAddress_Protocol(
266-
ValueObject &in_value, CompilerType protocol_type,
267-
SwiftASTContextForExpressions &scratch_ctx,
268-
lldb::DynamicValueType use_dynamic, TypeAndOrName &class_type_or_name,
269-
Address &address);
260+
bool GetDynamicTypeAndAddress_Class(ValueObject &in_value,
261+
lldb::DynamicValueType use_dynamic,
262+
TypeAndOrName &class_type_or_name,
263+
Address &address);
264+
265+
bool GetDynamicTypeAndAddress_Protocol(ValueObject &in_value,
266+
CompilerType protocol_type,
267+
lldb::DynamicValueType use_dynamic,
268+
TypeAndOrName &class_type_or_name,
269+
Address &address);
270270

271271
bool GetDynamicTypeAndAddress_Value(ValueObject &in_value,
272272
CompilerType &bound_type,

0 commit comments

Comments
 (0)