Files
clang-p2996/lldb/source/Plugins/SymbolFile/DWARF/DWARFIndex.cpp
jeffreytan81 61a46780d1 Improve type lookup using .debug_names parent chain (#108907)
## Summary
This PR improves `SymbolFileDWARF::FindTypes()` by utilizing the newly
added parent chain `DW_IDX_parent` in `.debug_names`. The proposal was
originally discussed in [this
RFC](https://discourse.llvm.org/t/rfc-improve-dwarf-5-debug-names-type-lookup-parsing-speed/74151).

## Implementation
To leverage the parent chain for `SymbolFileDWARF::FindTypes()`, this PR
adds a new API: `GetTypesWithQuery` in `DWARFIndex` base class. The API
performs the same function as `GetTypes` with additional filtering using
`TypeQuery`. Since this only introduces filtering, the callback
mechanisms at all call sites remain unchanged. A default implementation
is given in `DWARFIndex` class which parses debug info and performs the
matching. In the `DebugNameDWARFIndex` override, the parent_contexts in
the `TypeQuery` is cross checked with parent chain in `.debug_names` for
for much faster filtering before fallback to base implementation for
final filtering.

Unlike the `GetFullyQualifiedType` API, which fully consumes the
`DW_IDX_parent` parent chain for exact matching, these new APIs perform
partial subset matching for type/namespace queries. This is necessary to
support queries involving anonymous or inline namespaces. For instance,
a user might request `NS1::NS2::NS3::Foo`, while the index table's
parent chain might contain `NS1::inline_NS2::NS3::Foo`, which would fail
exact matching.

## Performance Results
In one of our internal target using `.debug_names` + split dwarf.
Expanding a "this" pointer in locals view in VSCode:
94s => 48s. (Not sure why I got 94s this time instead of 70s last week).

---------

Co-authored-by: jeffreytan81 <jeffreytan@fb.com>
2024-10-09 16:29:39 -07:00

154 lines
5.5 KiB
C++

//===-- DWARFIndex.cpp ----------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "Plugins/SymbolFile/DWARF/DWARFIndex.h"
#include "DWARFDebugInfoEntry.h"
#include "DWARFDeclContext.h"
#include "Plugins/Language/ObjC/ObjCLanguage.h"
#include "Plugins/SymbolFile/DWARF/DWARFDIE.h"
#include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
#include "lldb/Core/Mangled.h"
#include "lldb/Core/Module.h"
#include "lldb/Target/Language.h"
using namespace lldb_private;
using namespace lldb;
using namespace lldb_private::plugin::dwarf;
DWARFIndex::~DWARFIndex() = default;
bool DWARFIndex::ProcessFunctionDIE(
const Module::LookupInfo &lookup_info, DWARFDIE die,
const CompilerDeclContext &parent_decl_ctx,
llvm::function_ref<bool(DWARFDIE die)> callback) {
llvm::StringRef name = lookup_info.GetLookupName().GetStringRef();
FunctionNameType name_type_mask = lookup_info.GetNameTypeMask();
if (!(name_type_mask & eFunctionNameTypeFull)) {
ConstString name_to_match_against;
if (const char *mangled_die_name = die.GetMangledName()) {
name_to_match_against = ConstString(mangled_die_name);
} else {
SymbolFileDWARF *symbols = die.GetDWARF();
if (ConstString demangled_die_name =
symbols->ConstructFunctionDemangledName(die))
name_to_match_against = demangled_die_name;
}
if (!lookup_info.NameMatchesLookupInfo(name_to_match_against,
lookup_info.GetLanguageType()))
return true;
}
// Exit early if we're searching exclusively for methods or selectors and
// we have a context specified (no methods in namespaces).
uint32_t looking_for_nonmethods =
name_type_mask & ~(eFunctionNameTypeMethod | eFunctionNameTypeSelector);
if (!looking_for_nonmethods && parent_decl_ctx.IsValid())
return true;
// Otherwise, we need to also check that the context matches. If it does not
// match, we do nothing.
if (!SymbolFileDWARF::DIEInDeclContext(parent_decl_ctx, die))
return true;
// In case of a full match, we just insert everything we find.
if (name_type_mask & eFunctionNameTypeFull && die.GetMangledName() == name)
return callback(die);
// If looking for ObjC selectors, we need to also check if the name is a
// possible selector.
if (name_type_mask & eFunctionNameTypeSelector &&
ObjCLanguage::IsPossibleObjCMethodName(die.GetName()))
return callback(die);
bool looking_for_methods = name_type_mask & lldb::eFunctionNameTypeMethod;
bool looking_for_functions = name_type_mask & lldb::eFunctionNameTypeBase;
if (looking_for_methods || looking_for_functions) {
// If we're looking for either methods or functions, we definitely want this
// die. Otherwise, only keep it if the die type matches what we are
// searching for.
if ((looking_for_methods && looking_for_functions) ||
looking_for_methods == die.IsMethod())
return callback(die);
}
return true;
}
DWARFIndex::DIERefCallbackImpl::DIERefCallbackImpl(
const DWARFIndex &index, llvm::function_ref<bool(DWARFDIE die)> callback,
llvm::StringRef name)
: m_index(index),
m_dwarf(*llvm::cast<SymbolFileDWARF>(
index.m_module.GetSymbolFile()->GetBackingSymbolFile())),
m_callback(callback), m_name(name) {}
bool DWARFIndex::DIERefCallbackImpl::operator()(DIERef ref) const {
if (DWARFDIE die = m_dwarf.GetDIE(ref))
return m_callback(die);
m_index.ReportInvalidDIERef(ref, m_name);
return true;
}
bool DWARFIndex::DIERefCallbackImpl::operator()(
const llvm::AppleAcceleratorTable::Entry &entry) const {
return this->operator()(DIERef(std::nullopt, DIERef::Section::DebugInfo,
*entry.getDIESectionOffset()));
}
void DWARFIndex::ReportInvalidDIERef(DIERef ref, llvm::StringRef name) const {
m_module.ReportErrorIfModifyDetected(
"the DWARF debug information has been modified (accelerator table had "
"bad die {0:x16} for '{1}')\n",
ref.die_offset(), name.str().c_str());
}
void DWARFIndex::GetFullyQualifiedType(
const DWARFDeclContext &context,
llvm::function_ref<bool(DWARFDIE die)> callback) {
GetTypes(context, [&](DWARFDIE die) {
return GetFullyQualifiedTypeImpl(context, die, callback);
});
}
bool DWARFIndex::GetFullyQualifiedTypeImpl(
const DWARFDeclContext &context, DWARFDIE die,
llvm::function_ref<bool(DWARFDIE die)> callback) {
DWARFDeclContext dwarf_decl_ctx = die.GetDWARFDeclContext();
if (dwarf_decl_ctx == context)
return callback(die);
return true;
}
void DWARFIndex::GetTypesWithQuery(
TypeQuery &query, llvm::function_ref<bool(DWARFDIE die)> callback) {
GetTypes(query.GetTypeBasename(), [&](DWARFDIE die) {
return ProcessTypeDIEMatchQuery(query, die, callback);
});
}
bool DWARFIndex::ProcessTypeDIEMatchQuery(
TypeQuery &query, DWARFDIE die,
llvm::function_ref<bool(DWARFDIE die)> callback) {
// Nothing to match from query
if (query.GetContextRef().size() <= 1)
return callback(die);
std::vector<lldb_private::CompilerContext> die_context;
if (query.GetModuleSearch())
die_context = die.GetDeclContext();
else
die_context = die.GetTypeLookupContext();
if (!query.ContextMatches(die_context))
return true;
return callback(die);
}