Skip to content

Implement TypeSystemSwiftTypeRef::GetTypeBitAlign() #1901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lldb/include/lldb/Target/SwiftLanguageRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ class SwiftLanguageRuntime : public LanguageRuntime {
llvm::Optional<uint64_t> GetByteStride(CompilerType type);

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

/// Release the RemoteASTContext associated with the given swift::ASTContext.
/// Note that a RemoteASTContext must be destroyed before its associated
Expand Down
6 changes: 4 additions & 2 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6054,20 +6054,22 @@ SwiftASTContext::GetTypeBitAlign(opaque_compiler_type_t type,
exe_scope->CalculateExecutionContext(exe_ctx);
auto swift_scratch_ctx_lock = SwiftASTContextLock(&exe_ctx);
CompilerType bound_type = BindGenericTypeParameters({this, type}, exe_scope);
if (bound_type.GetOpaqueQualType() == type)
return {};
// Note thay the bound type may be in a different AST context.
return bound_type.GetTypeBitAlign(exe_scope);
}

const swift::irgen::FixedTypeInfo *fixed_type_info =
GetSwiftFixedTypeInfo(type);
if (fixed_type_info)
return fixed_type_info->getFixedAlignment().getValue();
return fixed_type_info->getFixedAlignment().getValue() * 8;

// Ask the dynamic type system.
if (!exe_scope)
return {};
if (auto *runtime = SwiftLanguageRuntime::Get(exe_scope->CalculateProcess()))
return runtime->GetBitAlignment({this, type});
return runtime->GetBitAlignment({this, type}, exe_scope);
return {};
}

Expand Down
83 changes: 61 additions & 22 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,30 @@ DWARFASTParser *TypeSystemSwiftTypeRef::GetDWARFParser() {
return m_swift_ast_context->GetDWARFParser();
}

TypeSP TypeSystemSwiftTypeRef::LookupTypeInModule(
lldb::opaque_compiler_type_t opaque_type) {
auto *M = GetModule();
if (!M)
return {};
swift::Demangle::Demangler dem;
auto *node = GetDemangledType(dem, AsMangledName(opaque_type));
auto module_type = GetNominal(dem, node);
if (!module_type)
return {};
// DW_AT_linkage_name is not part of the accelerator table, so
// we need to search by module+name.
ConstString module(module_type->first);
ConstString type(module_type->second);
llvm::SmallVector<CompilerContext, 2> decl_context;
decl_context.push_back({CompilerContextKind::Module, module});
decl_context.push_back({CompilerContextKind::AnyType, type});
llvm::DenseSet<SymbolFile *> searched_symbol_files;
TypeMap types;
M->FindTypes(decl_context, TypeSystemSwift::GetSupportedLanguagesForTypes(),
searched_symbol_files, types);
return types.Empty() ? TypeSP() : types.GetTypeAtIndex(0);
}

// Tests

#ifndef NDEBUG
Expand Down Expand Up @@ -1681,27 +1705,8 @@ TypeSystemSwiftTypeRef::GetBitSize(opaque_compiler_type_t type,
// If there is no process, we can still try to get the static size
// information out of DWARF. Because it is stored in the Type
// object we need to look that up by name again.
if (auto *M = GetModule()) {
swift::Demangle::Demangler dem;
auto node = GetDemangledType(dem, AsMangledName(type));
if (auto module_type = GetNominal(dem, node)) {
// DW_AT_linkage_name is not part of the accelerator table, so
// we need to search by module+name.
ConstString module(module_type->first);
ConstString type(module_type->second);
llvm::SmallVector<CompilerContext, 2> decl_context;
decl_context.push_back({CompilerContextKind::Module, module});
decl_context.push_back({CompilerContextKind::AnyType, type});
llvm::DenseSet<SymbolFile *> searched_symbol_files;
TypeMap types;
M->FindTypes(decl_context,
TypeSystemSwift::GetSupportedLanguagesForTypes(),
searched_symbol_files, types);
if (!types.Empty())
if (auto type = types.GetTypeAtIndex(0))
return type->GetByteSize(nullptr);
}
}
if (TypeSP type_sp = LookupTypeInModule(type))
return type_sp->GetByteSize(exe_scope);
LLDB_LOGF(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES),
"Couldn't compute size of type %s without a process.",
AsMangledName(type));
Expand Down Expand Up @@ -1956,7 +1961,41 @@ bool TypeSystemSwiftTypeRef::IsPointerOrReferenceType(
llvm::Optional<size_t>
TypeSystemSwiftTypeRef::GetTypeBitAlign(opaque_compiler_type_t type,
ExecutionContextScope *exe_scope) {
return m_swift_ast_context->GetTypeBitAlign(ReconstructType(type), exe_scope);
auto impl = [&]() -> llvm::Optional<size_t> {
// Clang types can be resolved even without a process.
if (CompilerType clang_type = GetAsClangTypeOrNull(type)) {
// Swift doesn't know pointers: return the size alignment of the
// object pointer instead of the underlying object.
if (Flags(clang_type.GetTypeInfo()).AllSet(eTypeIsObjC | eTypeIsClass))
return GetPointerByteSize() * 8;
return clang_type.GetTypeBitAlign(exe_scope);
}
if (!exe_scope) {
LLDB_LOGF(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES),
"Couldn't compute alignment of type %s without an execution "
"context.",
AsMangledName(type));
return {};
}
if (auto *runtime =
SwiftLanguageRuntime::Get(exe_scope->CalculateProcess()))
return runtime->GetBitAlignment({this, type}, exe_scope);

// If there is no process, we can still try to get the static
// alignment information out of DWARF. Because it is stored in the
// Type object we need to look that up by name again.
if (TypeSP type_sp = LookupTypeInModule(type))
return type_sp->GetLayoutCompilerType().GetTypeBitAlign(exe_scope);
LLDB_LOGF(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES),
"Couldn't compute alignment of type %s without a process.",
AsMangledName(type));
return {};
};
if (exe_scope && exe_scope->CalculateProcess())
VALIDATE_AND_RETURN(impl, GetTypeBitAlign, type,
(ReconstructType(type), exe_scope));
else
return impl();
}
bool TypeSystemSwiftTypeRef::IsTypedefType(opaque_compiler_type_t type) {
return m_swift_ast_context->IsTypedefType(ReconstructType(type));
Expand Down
3 changes: 3 additions & 0 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
/// Cast \p opaque_type as a mangled name.
const char *AsMangledName(lldb::opaque_compiler_type_t type);

/// Lookup a type in the debug info.
lldb::TypeSP LookupTypeInModule(lldb::opaque_compiler_type_t type);

/// Demangle the mangled name of the canonical type of \p type and
/// drill into the Global(TypeMangling(Type())).
///
Expand Down
8 changes: 5 additions & 3 deletions lldb/source/Target/SwiftLanguageRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ class SwiftLanguageRuntimeStub {
return {};
}

llvm::Optional<size_t> GetBitAlignment(CompilerType type) {
llvm::Optional<size_t> GetBitAlignment(CompilerType type,
ExecutionContextScope *exe_scope) {
STUB_LOG();
return {};
}
Expand Down Expand Up @@ -2131,8 +2132,9 @@ SwiftLanguageRuntime::GetByteStride(CompilerType type) {
}

llvm::Optional<size_t>
SwiftLanguageRuntime::GetBitAlignment(CompilerType type) {
FORWARD(GetBitAlignment, type);
SwiftLanguageRuntime::GetBitAlignment(CompilerType type,
ExecutionContextScope *exe_scope) {
FORWARD(GetBitAlignment, type, exe_scope);
}

bool SwiftLanguageRuntime::IsValidErrorValue(ValueObject &in_value) {
Expand Down
22 changes: 19 additions & 3 deletions lldb/source/Target/SwiftLanguageRuntimeDynamicTypeResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,21 @@ SwiftLanguageRuntimeImpl::BindGenericTypeParameters(StackFrame &stack_frame,
return type;
});

// Thicken generic metatypes. Once substituted, they should always
// be thick. TypeRef::subst() does the same transformation.
target_swift_type =
target_swift_type.transform([](swift::Type type) -> swift::Type {
using namespace swift;
const auto thin = MetatypeRepresentation::Thin;
const auto thick = MetatypeRepresentation::Thick;
if (auto *metatype = dyn_cast<AnyMetatypeType>(type.getPointer()))
if (metatype->hasRepresentation() &&
metatype->getRepresentation() == thin &&
metatype->getInstanceType()->hasTypeParameter())
return MetatypeType::get(metatype->getInstanceType(), thick);
return type;
});

while (target_swift_type->hasOpaqueArchetype()) {
auto old_type = target_swift_type;
target_swift_type = target_swift_type.subst(
Expand Down Expand Up @@ -2166,9 +2181,10 @@ SwiftLanguageRuntimeImpl::GetByteStride(CompilerType type) {
}

llvm::Optional<size_t>
SwiftLanguageRuntimeImpl::GetBitAlignment(CompilerType type) {
if (auto *type_info = GetTypeInfo(type, nullptr))
return type_info->getAlignment();
SwiftLanguageRuntimeImpl::GetBitAlignment(CompilerType type,
ExecutionContextScope *exe_scope) {
if (auto *type_info = GetTypeInfo(type, exe_scope))
return type_info->getAlignment() * 8;
return {};
}

Expand Down
3 changes: 2 additions & 1 deletion lldb/source/Target/SwiftLanguageRuntimeImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ class SwiftLanguageRuntimeImpl {
llvm::Optional<uint64_t> GetByteStride(CompilerType type);

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

SwiftLanguageRuntime::MetadataPromiseSP
GetMetadataPromise(lldb::addr_t addr, ValueObject &for_object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,14 @@ def test_expressions_in_class_functions(self):
self.check_expression("i", str(i), False)
if i == 6:
self.check_expression("self", "a.H<Int>")
frame = threads[0].GetFrameAtIndex(0)
lldbutil.check_variable(self, frame.FindVariable("self"),
use_dynamic=True,
# FIXME: This should be '@thick a.H<Swift.Int>.Type'
# but valobj.GetDynamicValue(lldb.eDynamicCanRunTarget)
# doesn't seem to do its job.
# rdar://problem/69889462
typename='@thin a.H<τ_0_0>.Type')

self.runCmd("continue")