[llvm] [LLVM][DWARF] Refactor code for generating DWARF V5 .debug_names acce… (PR #82264)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 19 20:37:46 PST 2024
https://github.com/cmtice updated https://github.com/llvm/llvm-project/pull/82264
>From c1d522c2cfbcf49a487eb7035a2bbf9cdad62b38 Mon Sep 17 00:00:00 2001
From: Caroline Tice <cmtice at google.com>
Date: Sun, 18 Feb 2024 10:00:03 -0800
Subject: [PATCH 1/2] [LLVM][DWARF] Refactor code for generating DWARF V5
.debug_names accelerator table.
Refactor the code that uniques the entries and computes the bucket count
for the DWARF V5 .debug_names accelerator table. This will make it easier to
share this bit of code wtih other tools such as LLD.
---
llvm/include/llvm/ADT/GenericUniformityImpl.h | 5 +----
llvm/include/llvm/ADT/STLExtras.h | 4 ++++
llvm/include/llvm/BinaryFormat/Dwarf.h | 18 ++++++++++++++++++
llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp | 14 ++------------
llvm/unittests/ADT/STLExtrasTest.cpp | 13 +++++++++++++
5 files changed, 38 insertions(+), 16 deletions(-)
diff --git a/llvm/include/llvm/ADT/GenericUniformityImpl.h b/llvm/include/llvm/ADT/GenericUniformityImpl.h
index d397b937d78ccc..6b744384051b56 100644
--- a/llvm/include/llvm/ADT/GenericUniformityImpl.h
+++ b/llvm/include/llvm/ADT/GenericUniformityImpl.h
@@ -46,6 +46,7 @@
#include "llvm/ADT/GenericUniformityInfo.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SparseBitVector.h"
#include "llvm/ADT/StringExtras.h"
@@ -57,10 +58,6 @@
namespace llvm {
-template <typename Range> auto unique(Range &&R) {
- return std::unique(adl_begin(R), adl_end(R));
-}
-
/// Construct a specially modified post-order traversal of cycles.
///
/// The ModifiedPO is contructed using a virtually modified CFG as follows:
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index a136eeb0ff1bd7..3d2f4bb40b6b79 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -1994,6 +1994,10 @@ auto unique(Range &&R, Predicate P) {
return std::unique(adl_begin(R), adl_end(R), P);
}
+template <typename Range> auto unique(Range &&R) {
+ return std::unique(adl_begin(R), adl_end(R));
+}
+
/// Wrapper function around std::equal to detect if pair-wise elements between
/// two ranges are the same.
template <typename L, typename R> bool equal(L &&LRange, R &&RRange) {
diff --git a/llvm/include/llvm/BinaryFormat/Dwarf.h b/llvm/include/llvm/BinaryFormat/Dwarf.h
index 869352b35e3235..3a245972a2aa93 100644
--- a/llvm/include/llvm/BinaryFormat/Dwarf.h
+++ b/llvm/include/llvm/BinaryFormat/Dwarf.h
@@ -613,6 +613,24 @@ enum AcceleratorTable {
DW_hash_function_djb = 0u
};
+// Uniquify the string hashes and calculate the bucket count for the
+// DWARF v5 Accelerator Table.
+inline uint32_t
+ComputeDebugNamesUniqueHashes(SmallVector<uint32_t, 0> &hashes) {
+ uint32_t BucketCount = 0;
+
+ llvm::sort(hashes);
+ uint32_t uniqueHashCount = llvm::unique(hashes) - hashes.begin();
+ if (uniqueHashCount > 1024)
+ BucketCount = uniqueHashCount / 4;
+ else if (uniqueHashCount > 16)
+ BucketCount = uniqueHashCount / 2;
+ else
+ BucketCount = std::max<uint32_t>(uniqueHashCount, 1);
+
+ return BucketCount;
+}
+
// Constants for the GNU pubnames/pubtypes extensions supporting gdb index.
enum GDBIndexEntryKind {
GIEK_NONE,
diff --git a/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp b/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp
index 22d995a9cc3c56..7dce65d668a6de 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp
@@ -33,22 +33,12 @@ using namespace llvm;
void AccelTableBase::computeBucketCount() {
// First get the number of unique hashes.
- std::vector<uint32_t> Uniques;
+ SmallVector<uint32_t, 0> Uniques;
Uniques.reserve(Entries.size());
for (const auto &E : Entries)
Uniques.push_back(E.second.HashValue);
- array_pod_sort(Uniques.begin(), Uniques.end());
- std::vector<uint32_t>::iterator P =
- std::unique(Uniques.begin(), Uniques.end());
- UniqueHashCount = std::distance(Uniques.begin(), P);
-
- if (UniqueHashCount > 1024)
- BucketCount = UniqueHashCount / 4;
- else if (UniqueHashCount > 16)
- BucketCount = UniqueHashCount / 2;
- else
- BucketCount = std::max<uint32_t>(UniqueHashCount, 1);
+ BucketCount = llvm::dwarf::ComputeDebugNamesUniqueHashes(Uniques);
}
void AccelTableBase::finalize(AsmPrinter *Asm, StringRef Prefix) {
diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index 7db339e4ef31cd..cafef5b5fad512 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -1004,6 +1004,19 @@ TEST(STLExtras, Unique) {
EXPECT_EQ(3, V[3]);
}
+TEST(STLExtras, UniqueNoPred) {
+ std::vector<uint32_t> V = {1, 5, 5, 4, 3, 3, 3};
+
+ auto I = llvm::unique(V);
+
+ EXPECT_EQ(I, V.begin() + 4);
+
+ EXPECT_EQ(1, V[0]);
+ EXPECT_EQ(5, V[1]);
+ EXPECT_EQ(4, V[2]);
+ EXPECT_EQ(3, V[3]);
+}
+
TEST(STLExtrasTest, MakeVisitorOneCallable) {
auto IdentityLambda = [](auto X) { return X; };
auto IdentityVisitor = makeVisitor(IdentityLambda);
>From cd40e59c2d04eee56eb08ad24d5a0846d5964264 Mon Sep 17 00:00:00 2001
From: Caroline Tice <cmtice at google.com>
Date: Mon, 19 Feb 2024 20:33:32 -0800
Subject: [PATCH 2/2] [LLVM][DWARF] Refactir code fir generating DWARF v5
.debug_names accelerator table.
Take the code for moving 'unique' from GenericUniformityImpl.h to
STLExtras.h out of the change (to put it into a separate change).
---
llvm/include/llvm/ADT/GenericUniformityImpl.h | 5 ++++-
llvm/include/llvm/ADT/STLExtras.h | 4 ----
llvm/unittests/ADT/STLExtrasTest.cpp | 13 -------------
3 files changed, 4 insertions(+), 18 deletions(-)
diff --git a/llvm/include/llvm/ADT/GenericUniformityImpl.h b/llvm/include/llvm/ADT/GenericUniformityImpl.h
index 6b744384051b56..d397b937d78ccc 100644
--- a/llvm/include/llvm/ADT/GenericUniformityImpl.h
+++ b/llvm/include/llvm/ADT/GenericUniformityImpl.h
@@ -46,7 +46,6 @@
#include "llvm/ADT/GenericUniformityInfo.h"
-#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SparseBitVector.h"
#include "llvm/ADT/StringExtras.h"
@@ -58,6 +57,10 @@
namespace llvm {
+template <typename Range> auto unique(Range &&R) {
+ return std::unique(adl_begin(R), adl_end(R));
+}
+
/// Construct a specially modified post-order traversal of cycles.
///
/// The ModifiedPO is contructed using a virtually modified CFG as follows:
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 3d2f4bb40b6b79..a136eeb0ff1bd7 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -1994,10 +1994,6 @@ auto unique(Range &&R, Predicate P) {
return std::unique(adl_begin(R), adl_end(R), P);
}
-template <typename Range> auto unique(Range &&R) {
- return std::unique(adl_begin(R), adl_end(R));
-}
-
/// Wrapper function around std::equal to detect if pair-wise elements between
/// two ranges are the same.
template <typename L, typename R> bool equal(L &&LRange, R &&RRange) {
diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index cafef5b5fad512..7db339e4ef31cd 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -1004,19 +1004,6 @@ TEST(STLExtras, Unique) {
EXPECT_EQ(3, V[3]);
}
-TEST(STLExtras, UniqueNoPred) {
- std::vector<uint32_t> V = {1, 5, 5, 4, 3, 3, 3};
-
- auto I = llvm::unique(V);
-
- EXPECT_EQ(I, V.begin() + 4);
-
- EXPECT_EQ(1, V[0]);
- EXPECT_EQ(5, V[1]);
- EXPECT_EQ(4, V[2]);
- EXPECT_EQ(3, V[3]);
-}
-
TEST(STLExtrasTest, MakeVisitorOneCallable) {
auto IdentityLambda = [](auto X) { return X; };
auto IdentityVisitor = makeVisitor(IdentityLambda);
More information about the llvm-commits
mailing list