Skip to content

Commit

Permalink
[DWARF] Refactor .debug_names bucket count computation (llvm#88087)
Browse files Browse the repository at this point in the history
`getDebugNamesBucketAndHashCount` lures users to provide an array to
compute the bucket count using an O(n log n) sort. This is inefficient
as hash table based uniquifying is faster.

The performance issue matters less for Clang as the number of names is
relatively small. For `ld.lld --debug-names`, I plan to compute the
unique hash count as a side product of parallel entry pool computation,
and I just need a function to suggest a bucket count.
  • Loading branch information
MaskRay authored Apr 9, 2024
1 parent a454d92 commit d3016aa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
22 changes: 6 additions & 16 deletions llvm/include/llvm/BinaryFormat/Dwarf.h
Original file line number Diff line number Diff line change
Expand Up @@ -613,23 +613,13 @@ enum AcceleratorTable {
DW_hash_function_djb = 0u
};

// Uniquify the string hashes and calculate the bucket count for the
// DWARF v5 Accelerator Table. NOTE: This function effectively consumes the
// 'Hashes' input parameter.
inline std::pair<uint32_t, uint32_t>
getDebugNamesBucketAndHashCount(MutableArrayRef<uint32_t> Hashes) {
uint32_t BucketCount = 0;

sort(Hashes);
uint32_t UniqueHashCount = llvm::unique(Hashes) - Hashes.begin();
// Return a suggested bucket count for the DWARF v5 Accelerator Table.
inline uint32_t getDebugNamesBucketCount(uint32_t UniqueHashCount) {
if (UniqueHashCount > 1024)
BucketCount = UniqueHashCount / 4;
else if (UniqueHashCount > 16)
BucketCount = UniqueHashCount / 2;
else
BucketCount = std::max<uint32_t>(UniqueHashCount, 1);

return {BucketCount, UniqueHashCount};
return UniqueHashCount / 4;
if (UniqueHashCount > 16)
return UniqueHashCount / 2;
return std::max<uint32_t>(UniqueHashCount, 1);
}

// Constants for the GNU pubnames/pubtypes extensions supporting gdb index.
Expand Down
7 changes: 3 additions & 4 deletions llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@
using namespace llvm;

void AccelTableBase::computeBucketCount() {
// First get the number of unique hashes.
SmallVector<uint32_t, 0> Uniques;
Uniques.reserve(Entries.size());
for (const auto &E : Entries)
Uniques.push_back(E.second.HashValue);

std::tie(BucketCount, UniqueHashCount) =
llvm::dwarf::getDebugNamesBucketAndHashCount(Uniques);
llvm::sort(Uniques);
UniqueHashCount = llvm::unique(Uniques) - Uniques.begin();
BucketCount = dwarf::getDebugNamesBucketCount(UniqueHashCount);
}

void AccelTableBase::finalize(AsmPrinter *Asm, StringRef Prefix) {
Expand Down

0 comments on commit d3016aa

Please sign in to comment.