Skip to content

Commit 60a2b99

Browse files
committed
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.
1 parent b71e1f6 commit 60a2b99

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

llvm/include/llvm/CodeGen/AccelTable.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define LLVM_CODEGEN_ACCELTABLE_H
1515

1616
#include "llvm/ADT/ArrayRef.h"
17+
#include "llvm/ADT/MapVector.h"
1718
#include "llvm/ADT/STLFunctionalExtras.h"
1819
#include "llvm/ADT/StringMap.h"
1920
#include "llvm/ADT/StringRef.h"
@@ -142,9 +143,6 @@ class AccelTableBase {
142143
std::vector<AccelTableData *> Values;
143144
MCSymbol *Sym;
144145

145-
HashData(DwarfStringPoolEntryRef Name, HashFn *Hash)
146-
: Name(Name), HashValue(Hash(Name.getString())) {}
147-
148146
#ifndef NDEBUG
149147
void print(raw_ostream &OS) const;
150148
void dump() const { print(dbgs()); }
@@ -157,7 +155,7 @@ class AccelTableBase {
157155
/// Allocator for HashData and Values.
158156
BumpPtrAllocator Allocator;
159157

160-
using StringEntries = StringMap<HashData, BumpPtrAllocator &>;
158+
using StringEntries = MapVector<StringRef, HashData>;
161159
StringEntries Entries;
162160

163161
HashFn *Hash;
@@ -169,7 +167,7 @@ class AccelTableBase {
169167

170168
void computeBucketCount();
171169

172-
AccelTableBase(HashFn *Hash) : Entries(Allocator), Hash(Hash) {}
170+
AccelTableBase(HashFn *Hash) : Hash(Hash) {}
173171

174172
public:
175173
void finalize(AsmPrinter *Asm, StringRef Prefix);
@@ -207,10 +205,13 @@ void AccelTable<AccelTableDataT>::addName(DwarfStringPoolEntryRef Name,
207205
assert(Buckets.empty() && "Already finalized!");
208206
// If the string is in the list already then add this die to the list
209207
// otherwise add a new one.
210-
auto Iter = Entries.try_emplace(Name.getString(), Name, Hash).first;
211-
assert(Iter->second.Name == Name);
212-
Iter->second.Values.push_back(
213-
new (Allocator) AccelTableDataT(std::forward<Types>(Args)...));
208+
auto &It = Entries[Name.getString()];
209+
if (It.Values.empty()) {
210+
It.Name = Name;
211+
It.HashValue = Hash(Name.getString());
212+
}
213+
It.Values.push_back(new (Allocator)
214+
AccelTableDataT(std::forward<Types>(Args)...));
214215
}
215216

216217
/// A base class for different implementations of Data classes for Apple

llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -664,9 +664,9 @@ void AccelTableBase::HashData::print(raw_ostream &OS) const {
664664
void AccelTableBase::print(raw_ostream &OS) const {
665665
// Print Content.
666666
OS << "Entries: \n";
667-
for (const auto &Entry : Entries) {
668-
OS << "Name: " << Entry.first() << "\n";
669-
for (auto *V : Entry.second.Values)
667+
for (const auto &[Name, Data] : Entries) {
668+
OS << "Name: " << Name << "\n";
669+
for (auto *V : Data.Values)
670670
V->print(OS);
671671
}
672672

llvm/test/DebugInfo/Generic/accel-table-hash-collisions.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
; CHECK: String: 0x{{[0-9a-f]*}} "is"
3333

3434
; CHECK: Hash 0xa4b42a1e
35-
; CHECK: String: 0x{{[0-9a-f]*}} "_ZN4llvm16DenseMapIteratorIPNS_10MDLocationENS_6detail13DenseSetEmptyENS_10MDNodeInfoIS1_EENS3_12DenseSetPairIS2_EELb0EE23AdvancePastEmptyBucketsEv"
3635
; CHECK: String: 0x{{[0-9a-f]*}} "_ZN5clang23DataRecursiveASTVisitorIN12_GLOBAL__N_124UnusedBackingIvarCheckerEE26TraverseCUDAKernelCallExprEPNS_18CUDAKernelCallExprE"
36+
; CHECK: String: 0x{{[0-9a-f]*}} "_ZN4llvm16DenseMapIteratorIPNS_10MDLocationENS_6detail13DenseSetEmptyENS_10MDNodeInfoIS1_EENS3_12DenseSetPairIS2_EELb0EE23AdvancePastEmptyBucketsEv"
3737

3838
; CHECK: Hash 0xeee7c0b2
3939
; CHECK: String: 0x{{[0-9a-f]*}} "_ZNK4llvm12LivePhysRegs5printERNS_11raw_ostreamE"

llvm/test/DebugInfo/Generic/debug-names-hash-collisions.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@
2929
; Check that all the names are present in the output
3030
; CHECK: Bucket 0
3131
; CHECK: Hash: 0xF8CF70D
32-
; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZN4lldb7SBBlockC1ERKS0_"
33-
; CHECK: Hash: 0xF8CF70D
3432
; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZN4lldb7SBBlockaSERKS0_"
35-
; CHECK: Hash: 0x135A482C
36-
; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZN4lldb7SBErrorC1ERKS0_"
33+
; CHECK: Hash: 0xF8CF70D
34+
; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZN4lldb7SBBlockC1ERKS0_"
3735
; CHECK: Hash: 0x135A482C
3836
; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZN4lldb7SBErroraSERKS0_"
37+
; CHECK: Hash: 0x135A482C
38+
; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZN4lldb7SBErrorC1ERKS0_"
3939
; CHECK-NOT: String:
4040
; CHECK: Bucket 1
4141
; CHECK-NEXT: EMPTY
4242
; CHECK: Bucket 2
4343
; CHECK: Hash: 0x2841B989
44-
; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZL11NumCommutes"
45-
; CHECK: Hash: 0x2841B989
4644
; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZL11numCommutes"
45+
; CHECK: Hash: 0x2841B989
46+
; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZL11NumCommutes"
4747
; CHECK: Hash: 0x3E190F5F
4848
; CHECK-NEXT:String: 0x{{[0-9a-f]*}} "_ZL9NumRemats"
4949
; CHECK: Hash: 0x3E190F5F

0 commit comments

Comments
 (0)