Skip to content

Commit f6c4f8e

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> (cherry picked from commit 593758e)
1 parent d9e38ae commit f6c4f8e

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
@@ -6039,20 +6039,22 @@ SwiftASTContext::GetTypeBitAlign(opaque_compiler_type_t type,
60396039
exe_scope->CalculateExecutionContext(exe_ctx);
60406040
auto swift_scratch_ctx_lock = SwiftASTContextLock(&exe_ctx);
60416041
CompilerType bound_type = BindGenericTypeParameters({this, type}, exe_scope);
6042+
if (bound_type.GetOpaqueQualType() == type)
6043+
return {};
60426044
// Note thay the bound type may be in a different AST context.
60436045
return bound_type.GetTypeBitAlign(exe_scope);
60446046
}
60456047

60466048
const swift::irgen::FixedTypeInfo *fixed_type_info =
60476049
GetSwiftFixedTypeInfo(type);
60486050
if (fixed_type_info)
6049-
return fixed_type_info->getFixedAlignment().getValue();
6051+
return fixed_type_info->getFixedAlignment().getValue() * 8;
60506052

60516053
// Ask the dynamic type system.
60526054
if (!exe_scope)
60536055
return {};
60546056
if (auto *runtime = SwiftLanguageRuntime::Get(exe_scope->CalculateProcess()))
6055-
return runtime->GetBitAlignment({this, type});
6057+
return runtime->GetBitAlignment({this, type}, exe_scope);
60566058
return {};
60576059
}
60586060

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

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

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)