[llvm] 60a2b99 - AccelTable: Use MapVector to stabilize iteration order

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 19 19:50:41 PDT 2023


Author: Fangrui Song
Date: 2023-07-19T19:50:36-07:00
New Revision: 60a2b9912514eb94f4d53dae4a727f8079c4c402

URL: https://github.com/llvm/llvm-project/commit/60a2b9912514eb94f4d53dae4a727f8079c4c402
DIFF: https://github.com/llvm/llvm-project/commit/60a2b9912514eb94f4d53dae4a727f8079c4c402.diff

LOG: AccelTable: Use MapVector to stabilize iteration order

Entries of the same DJB hash are in the hash lookup table/name table are
ordered by the iteration order of `Entries` (a StringMap). Change
`Entries` to a MapVector to stabilize the order and simplify future
changes like D142862 that change the StringMap hash function.

Added: 
    

Modified: 
    llvm/include/llvm/CodeGen/AccelTable.h
    llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp
    llvm/test/DebugInfo/Generic/accel-table-hash-collisions.ll
    llvm/test/DebugInfo/Generic/debug-names-hash-collisions.ll

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/CodeGen/AccelTable.h b/llvm/include/llvm/CodeGen/AccelTable.h
index 46537c9cb68156..ec977b546f46f0 100644
--- a/llvm/include/llvm/CodeGen/AccelTable.h
+++ b/llvm/include/llvm/CodeGen/AccelTable.h
@@ -14,6 +14,7 @@
 #define LLVM_CODEGEN_ACCELTABLE_H
 
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/STLFunctionalExtras.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
@@ -142,9 +143,6 @@ class AccelTableBase {
     std::vector<AccelTableData *> Values;
     MCSymbol *Sym;
 
-    HashData(DwarfStringPoolEntryRef Name, HashFn *Hash)
-        : Name(Name), HashValue(Hash(Name.getString())) {}
-
 #ifndef NDEBUG
     void print(raw_ostream &OS) const;
     void dump() const { print(dbgs()); }
@@ -157,7 +155,7 @@ class AccelTableBase {
   /// Allocator for HashData and Values.
   BumpPtrAllocator Allocator;
 
-  using StringEntries = StringMap<HashData, BumpPtrAllocator &>;
+  using StringEntries = MapVector<StringRef, HashData>;
   StringEntries Entries;
 
   HashFn *Hash;
@@ -169,7 +167,7 @@ class AccelTableBase {
 
   void computeBucketCount();
 
-  AccelTableBase(HashFn *Hash) : Entries(Allocator), Hash(Hash) {}
+  AccelTableBase(HashFn *Hash) : Hash(Hash) {}
 
 public:
   void finalize(AsmPrinter *Asm, StringRef Prefix);
@@ -207,10 +205,13 @@ void AccelTable<AccelTableDataT>::addName(DwarfStringPoolEntryRef Name,
   assert(Buckets.empty() && "Already finalized!");
   // If the string is in the list already then add this die to the list
   // otherwise add a new one.
-  auto Iter = Entries.try_emplace(Name.getString(), Name, Hash).first;
-  assert(Iter->second.Name == Name);
-  Iter->second.Values.push_back(
-      new (Allocator) AccelTableDataT(std::forward<Types>(Args)...));
+  auto &It = Entries[Name.getString()];
+  if (It.Values.empty()) {
+    It.Name = Name;
+    It.HashValue = Hash(Name.getString());
+  }
+  It.Values.push_back(new (Allocator)
+                          AccelTableDataT(std::forward<Types>(Args)...));
 }
 
 /// A base class for 
diff erent implementations of Data classes for Apple

diff  --git a/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp b/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp
index 5f9c3ea8252740..aab3c268133925 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp
@@ -664,9 +664,9 @@ void AccelTableBase::HashData::print(raw_ostream &OS) const {
 void AccelTableBase::print(raw_ostream &OS) const {
   // Print Content.
   OS << "Entries: \n";
-  for (const auto &Entry : Entries) {
-    OS << "Name: " << Entry.first() << "\n";
-    for (auto *V : Entry.second.Values)
+  for (const auto &[Name, Data] : Entries) {
+    OS << "Name: " << Name << "\n";
+    for (auto *V : Data.Values)
       V->print(OS);
   }
 

diff  --git a/llvm/test/DebugInfo/Generic/accel-table-hash-collisions.ll b/llvm/test/DebugInfo/Generic/accel-table-hash-collisions.ll
index 1da5a5248d1d09..7ed21f76237594 100644
--- a/llvm/test/DebugInfo/Generic/accel-table-hash-collisions.ll
+++ b/llvm/test/DebugInfo/Generic/accel-table-hash-collisions.ll
@@ -32,8 +32,8 @@
 ; CHECK:    String: 0x{{[0-9a-f]*}} "is"
 
 ; CHECK: Hash 0xa4b42a1e
-; CHECK:    String: 0x{{[0-9a-f]*}} "_ZN4llvm16DenseMapIteratorIPNS_10MDLocationENS_6detail13DenseSetEmptyENS_10MDNodeInfoIS1_EENS3_12DenseSetPairIS2_EELb0EE23AdvancePastEmptyBucketsEv"
 ; CHECK:    String: 0x{{[0-9a-f]*}} "_ZN5clang23DataRecursiveASTVisitorIN12_GLOBAL__N_124UnusedBackingIvarCheckerEE26TraverseCUDAKernelCallExprEPNS_18CUDAKernelCallExprE"
+; CHECK:    String: 0x{{[0-9a-f]*}} "_ZN4llvm16DenseMapIteratorIPNS_10MDLocationENS_6detail13DenseSetEmptyENS_10MDNodeInfoIS1_EENS3_12DenseSetPairIS2_EELb0EE23AdvancePastEmptyBucketsEv"
 
 ; CHECK: Hash 0xeee7c0b2
 ; CHECK:    String: 0x{{[0-9a-f]*}} "_ZNK4llvm12LivePhysRegs5printERNS_11raw_ostreamE"

diff  --git a/llvm/test/DebugInfo/Generic/debug-names-hash-collisions.ll b/llvm/test/DebugInfo/Generic/debug-names-hash-collisions.ll
index 67294ba5ce827e..ee35d75eb78147 100644
--- a/llvm/test/DebugInfo/Generic/debug-names-hash-collisions.ll
+++ b/llvm/test/DebugInfo/Generic/debug-names-hash-collisions.ll
@@ -29,21 +29,21 @@
 ; Check that all the names are present in the output
 ; CHECK: Bucket 0
 ; CHECK:     Hash: 0xF8CF70D
-; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZN4lldb7SBBlockC1ERKS0_"
-; CHECK:     Hash: 0xF8CF70D
 ; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZN4lldb7SBBlockaSERKS0_"
-; CHECK:     Hash: 0x135A482C
-; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZN4lldb7SBErrorC1ERKS0_"
+; CHECK:     Hash: 0xF8CF70D
+; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZN4lldb7SBBlockC1ERKS0_"
 ; CHECK:     Hash: 0x135A482C
 ; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZN4lldb7SBErroraSERKS0_"
+; CHECK:     Hash: 0x135A482C
+; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZN4lldb7SBErrorC1ERKS0_"
 ; CHECK-NOT: String:
 ; CHECK: Bucket 1
 ; CHECK-NEXT: EMPTY
 ; CHECK: Bucket 2
 ; CHECK:     Hash: 0x2841B989
-; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZL11NumCommutes"
-; CHECK:     Hash: 0x2841B989
 ; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZL11numCommutes"
+; CHECK:     Hash: 0x2841B989
+; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZL11NumCommutes"
 ; CHECK:     Hash: 0x3E190F5F
 ; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZL9NumRemats"
 ; CHECK:     Hash: 0x3E190F5F


        


More information about the llvm-commits mailing list