Skip to content

Commit 593758e

Browse files
committed
Implement TypeSystemSwiftTypeRef::GetTypeBitAlign() (NFC)
This also fixes a pretty serious bit/byte bug in SwiftASTContext::GetTypeBitAlign() that go uncovered by the new validation assertion. <rdar://problem/68171432>
1 parent eaf9f9d commit 593758e

File tree

7 files changed

+81
-32
lines changed

7 files changed

+81
-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
@@ -6054,20 +6054,22 @@ SwiftASTContext::GetTypeBitAlign(opaque_compiler_type_t type,
60546054
exe_scope->CalculateExecutionContext(exe_ctx);
60556055
auto swift_scratch_ctx_lock = SwiftASTContextLock(&exe_ctx);
60566056
CompilerType bound_type = BindGenericTypeParameters({this, type}, exe_scope);
6057+
if (bound_type.GetOpaqueQualType() == type)
6058+
return {};
60576059
// Note thay the bound type may be in a different AST context.
60586060
return bound_type.GetTypeBitAlign(exe_scope);
60596061
}
60606062

60616063
const swift::irgen::FixedTypeInfo *fixed_type_info =
60626064
GetSwiftFixedTypeInfo(type);
60636065
if (fixed_type_info)
6064-
return fixed_type_info->getFixedAlignment().getValue();
6066+
return fixed_type_info->getFixedAlignment().getValue() * 8;
60656067

60666068
// Ask the dynamic type system.
60676069
if (!exe_scope)
60686070
return {};
60696071
if (auto *runtime = SwiftLanguageRuntime::Get(exe_scope->CalculateProcess()))
6070-
return runtime->GetBitAlignment({this, type});
6072+
return runtime->GetBitAlignment({this, type}, exe_scope);
60716073
return {};
60726074
}
60736075

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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,9 +2181,10 @@ SwiftLanguageRuntimeImpl::GetByteStride(CompilerType type) {
21812181
}
21822182

21832183
llvm::Optional<size_t>
2184-
SwiftLanguageRuntimeImpl::GetBitAlignment(CompilerType type) {
2185-
if (auto *type_info = GetTypeInfo(type, nullptr))
2186-
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;
21872188
return {};
21882189
}
21892190

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);

0 commit comments

Comments
 (0)