Skip to content

Commit db28df2

Browse files
Merge pull request #1901 from adrian-prantl/68171432
Implement TypeSystemSwiftTypeRef::GetTypeBitAlign()
2 parents 9631b3c + 593758e commit db28df2

File tree

8 files changed

+105
-32
lines changed

8 files changed

+105
-32
lines changed

lldb/include/lldb/Target/SwiftLanguageRuntime.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ class SwiftLanguageRuntime : public LanguageRuntime {
260260
llvm::Optional<uint64_t> GetByteStride(CompilerType type);
261261

262262
/// Ask Remote mirrors for the alignment of a Swift type.
263-
llvm::Optional<size_t> GetBitAlignment(CompilerType type);
263+
llvm::Optional<size_t> GetBitAlignment(CompilerType type,
264+
ExecutionContextScope *exe_scope);
264265

265266
/// Release the RemoteASTContext associated with the given swift::ASTContext.
266267
/// Note that a RemoteASTContext must be destroyed before its associated

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6062,20 +6062,22 @@ SwiftASTContext::GetTypeBitAlign(opaque_compiler_type_t type,
60626062
exe_scope->CalculateExecutionContext(exe_ctx);
60636063
auto swift_scratch_ctx_lock = SwiftASTContextLock(&exe_ctx);
60646064
CompilerType bound_type = BindGenericTypeParameters({this, type}, exe_scope);
6065+
if (bound_type.GetOpaqueQualType() == type)
6066+
return {};
60656067
// Note thay the bound type may be in a different AST context.
60666068
return bound_type.GetTypeBitAlign(exe_scope);
60676069
}
60686070

60696071
const swift::irgen::FixedTypeInfo *fixed_type_info =
60706072
GetSwiftFixedTypeInfo(type);
60716073
if (fixed_type_info)
6072-
return fixed_type_info->getFixedAlignment().getValue();
6074+
return fixed_type_info->getFixedAlignment().getValue() * 8;
60736075

60746076
// Ask the dynamic type system.
60756077
if (!exe_scope)
60766078
return {};
60776079
if (auto *runtime = SwiftLanguageRuntime::Get(exe_scope->CalculateProcess()))
6078-
return runtime->GetBitAlignment({this, type});
6080+
return runtime->GetBitAlignment({this, type}, exe_scope);
60796081
return {};
60806082
}
60816083

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

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,30 @@ DWARFASTParser *TypeSystemSwiftTypeRef::GetDWARFParser() {
10331033
return m_swift_ast_context->GetDWARFParser();
10341034
}
10351035

1036+
TypeSP TypeSystemSwiftTypeRef::LookupTypeInModule(
1037+
lldb::opaque_compiler_type_t opaque_type) {
1038+
auto *M = GetModule();
1039+
if (!M)
1040+
return {};
1041+
swift::Demangle::Demangler dem;
1042+
auto *node = GetDemangledType(dem, AsMangledName(opaque_type));
1043+
auto module_type = GetNominal(dem, node);
1044+
if (!module_type)
1045+
return {};
1046+
// DW_AT_linkage_name is not part of the accelerator table, so
1047+
// we need to search by module+name.
1048+
ConstString module(module_type->first);
1049+
ConstString type(module_type->second);
1050+
llvm::SmallVector<CompilerContext, 2> decl_context;
1051+
decl_context.push_back({CompilerContextKind::Module, module});
1052+
decl_context.push_back({CompilerContextKind::AnyType, type});
1053+
llvm::DenseSet<SymbolFile *> searched_symbol_files;
1054+
TypeMap types;
1055+
M->FindTypes(decl_context, TypeSystemSwift::GetSupportedLanguagesForTypes(),
1056+
searched_symbol_files, types);
1057+
return types.Empty() ? TypeSP() : types.GetTypeAtIndex(0);
1058+
}
1059+
10361060
// Tests
10371061

10381062
#ifndef NDEBUG
@@ -1681,27 +1705,8 @@ TypeSystemSwiftTypeRef::GetBitSize(opaque_compiler_type_t type,
16811705
// If there is no process, we can still try to get the static size
16821706
// information out of DWARF. Because it is stored in the Type
16831707
// object we need to look that up by name again.
1684-
if (auto *M = GetModule()) {
1685-
swift::Demangle::Demangler dem;
1686-
auto node = GetDemangledType(dem, AsMangledName(type));
1687-
if (auto module_type = GetNominal(dem, node)) {
1688-
// DW_AT_linkage_name is not part of the accelerator table, so
1689-
// we need to search by module+name.
1690-
ConstString module(module_type->first);
1691-
ConstString type(module_type->second);
1692-
llvm::SmallVector<CompilerContext, 2> decl_context;
1693-
decl_context.push_back({CompilerContextKind::Module, module});
1694-
decl_context.push_back({CompilerContextKind::AnyType, type});
1695-
llvm::DenseSet<SymbolFile *> searched_symbol_files;
1696-
TypeMap types;
1697-
M->FindTypes(decl_context,
1698-
TypeSystemSwift::GetSupportedLanguagesForTypes(),
1699-
searched_symbol_files, types);
1700-
if (!types.Empty())
1701-
if (auto type = types.GetTypeAtIndex(0))
1702-
return type->GetByteSize(nullptr);
1703-
}
1704-
}
1708+
if (TypeSP type_sp = LookupTypeInModule(type))
1709+
return type_sp->GetByteSize(exe_scope);
17051710
LLDB_LOGF(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES),
17061711
"Couldn't compute size of type %s without a process.",
17071712
AsMangledName(type));
@@ -1956,7 +1961,41 @@ bool TypeSystemSwiftTypeRef::IsPointerOrReferenceType(
19561961
llvm::Optional<size_t>
19571962
TypeSystemSwiftTypeRef::GetTypeBitAlign(opaque_compiler_type_t type,
19581963
ExecutionContextScope *exe_scope) {
1959-
return m_swift_ast_context->GetTypeBitAlign(ReconstructType(type), exe_scope);
1964+
auto impl = [&]() -> llvm::Optional<size_t> {
1965+
// Clang types can be resolved even without a process.
1966+
if (CompilerType clang_type = GetAsClangTypeOrNull(type)) {
1967+
// Swift doesn't know pointers: return the size alignment of the
1968+
// object pointer instead of the underlying object.
1969+
if (Flags(clang_type.GetTypeInfo()).AllSet(eTypeIsObjC | eTypeIsClass))
1970+
return GetPointerByteSize() * 8;
1971+
return clang_type.GetTypeBitAlign(exe_scope);
1972+
}
1973+
if (!exe_scope) {
1974+
LLDB_LOGF(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES),
1975+
"Couldn't compute alignment of type %s without an execution "
1976+
"context.",
1977+
AsMangledName(type));
1978+
return {};
1979+
}
1980+
if (auto *runtime =
1981+
SwiftLanguageRuntime::Get(exe_scope->CalculateProcess()))
1982+
return runtime->GetBitAlignment({this, type}, exe_scope);
1983+
1984+
// If there is no process, we can still try to get the static
1985+
// alignment information out of DWARF. Because it is stored in the
1986+
// Type object we need to look that up by name again.
1987+
if (TypeSP type_sp = LookupTypeInModule(type))
1988+
return type_sp->GetLayoutCompilerType().GetTypeBitAlign(exe_scope);
1989+
LLDB_LOGF(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES),
1990+
"Couldn't compute alignment of type %s without a process.",
1991+
AsMangledName(type));
1992+
return {};
1993+
};
1994+
if (exe_scope && exe_scope->CalculateProcess())
1995+
VALIDATE_AND_RETURN(impl, GetTypeBitAlign, type,
1996+
(ReconstructType(type), exe_scope));
1997+
else
1998+
return impl();
19601999
}
19612000
bool TypeSystemSwiftTypeRef::IsTypedefType(opaque_compiler_type_t type) {
19622001
return m_swift_ast_context->IsTypedefType(ReconstructType(type));

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
278278
/// Cast \p opaque_type as a mangled name.
279279
const char *AsMangledName(lldb::opaque_compiler_type_t type);
280280

281+
/// Lookup a type in the debug info.
282+
lldb::TypeSP LookupTypeInModule(lldb::opaque_compiler_type_t type);
283+
281284
/// Demangle the mangled name of the canonical type of \p type and
282285
/// drill into the Global(TypeMangling(Type())).
283286
///

lldb/source/Target/SwiftLanguageRuntime.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ class SwiftLanguageRuntimeStub {
270270
return {};
271271
}
272272

273-
llvm::Optional<size_t> GetBitAlignment(CompilerType type) {
273+
llvm::Optional<size_t> GetBitAlignment(CompilerType type,
274+
ExecutionContextScope *exe_scope) {
274275
STUB_LOG();
275276
return {};
276277
}
@@ -2131,8 +2132,9 @@ SwiftLanguageRuntime::GetByteStride(CompilerType type) {
21312132
}
21322133

21332134
llvm::Optional<size_t>
2134-
SwiftLanguageRuntime::GetBitAlignment(CompilerType type) {
2135-
FORWARD(GetBitAlignment, type);
2135+
SwiftLanguageRuntime::GetBitAlignment(CompilerType type,
2136+
ExecutionContextScope *exe_scope) {
2137+
FORWARD(GetBitAlignment, type, exe_scope);
21362138
}
21372139

21382140
bool SwiftLanguageRuntime::IsValidErrorValue(ValueObject &in_value) {

lldb/source/Target/SwiftLanguageRuntimeDynamicTypeResolution.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,21 @@ SwiftLanguageRuntimeImpl::BindGenericTypeParameters(StackFrame &stack_frame,
14341434
return type;
14351435
});
14361436

1437+
// Thicken generic metatypes. Once substituted, they should always
1438+
// be thick. TypeRef::subst() does the same transformation.
1439+
target_swift_type =
1440+
target_swift_type.transform([](swift::Type type) -> swift::Type {
1441+
using namespace swift;
1442+
const auto thin = MetatypeRepresentation::Thin;
1443+
const auto thick = MetatypeRepresentation::Thick;
1444+
if (auto *metatype = dyn_cast<AnyMetatypeType>(type.getPointer()))
1445+
if (metatype->hasRepresentation() &&
1446+
metatype->getRepresentation() == thin &&
1447+
metatype->getInstanceType()->hasTypeParameter())
1448+
return MetatypeType::get(metatype->getInstanceType(), thick);
1449+
return type;
1450+
});
1451+
14371452
while (target_swift_type->hasOpaqueArchetype()) {
14381453
auto old_type = target_swift_type;
14391454
target_swift_type = target_swift_type.subst(
@@ -2166,9 +2181,10 @@ SwiftLanguageRuntimeImpl::GetByteStride(CompilerType type) {
21662181
}
21672182

21682183
llvm::Optional<size_t>
2169-
SwiftLanguageRuntimeImpl::GetBitAlignment(CompilerType type) {
2170-
if (auto *type_info = GetTypeInfo(type, nullptr))
2171-
return type_info->getAlignment();
2184+
SwiftLanguageRuntimeImpl::GetBitAlignment(CompilerType type,
2185+
ExecutionContextScope *exe_scope) {
2186+
if (auto *type_info = GetTypeInfo(type, exe_scope))
2187+
return type_info->getAlignment() * 8;
21722188
return {};
21732189
}
21742190

lldb/source/Target/SwiftLanguageRuntimeImpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ class SwiftLanguageRuntimeImpl {
9292
llvm::Optional<uint64_t> GetByteStride(CompilerType type);
9393

9494
/// Ask Remote mirrors for the alignment of a Swift type.
95-
llvm::Optional<size_t> GetBitAlignment(CompilerType type);
95+
llvm::Optional<size_t> GetBitAlignment(CompilerType type,
96+
ExecutionContextScope *exe_scope);
9697

9798
SwiftLanguageRuntime::MetadataPromiseSP
9899
GetMetadataPromise(lldb::addr_t addr, ValueObject &for_object);

lldb/test/API/lang/swift/expression/static/TestSwiftExpressionsInClassFunctions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,14 @@ def test_expressions_in_class_functions(self):
7676
self.check_expression("i", str(i), False)
7777
if i == 6:
7878
self.check_expression("self", "a.H<Int>")
79+
frame = threads[0].GetFrameAtIndex(0)
80+
lldbutil.check_variable(self, frame.FindVariable("self"),
81+
use_dynamic=True,
82+
# FIXME: This should be '@thick a.H<Swift.Int>.Type'
83+
# but valobj.GetDynamicValue(lldb.eDynamicCanRunTarget)
84+
# doesn't seem to do its job.
85+
# rdar://problem/69889462
86+
typename='@thin a.H<τ_0_0>.Type')
87+
7988
self.runCmd("continue")
8089

0 commit comments

Comments
 (0)