Skip to content

Commit f976719

Browse files
committed
Revert "[ADT][StringMap] Add ability to precompute and reuse the string hash"
Crash identified internally in lld's use of StringMap in `compareSections`. Will investigate offline before recommitting. This reverts commit 67c631d.
1 parent 5bc1adf commit f976719

File tree

3 files changed

+12
-63
lines changed

3 files changed

+12
-63
lines changed

llvm/include/llvm/ADT/StringMap.h

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "llvm/ADT/StringMapEntry.h"
1818
#include "llvm/ADT/iterator.h"
1919
#include "llvm/Support/AllocatorBase.h"
20-
#include "llvm/Support/DJB.h"
2120
#include "llvm/Support/PointerLikeTypeTraits.h"
2221
#include <initializer_list>
2322
#include <iterator>
@@ -61,20 +60,12 @@ class StringMapImpl {
6160
/// specified bucket will be non-null. Otherwise, it will be null. In either
6261
/// case, the FullHashValue field of the bucket will be set to the hash value
6362
/// of the string.
64-
unsigned LookupBucketFor(StringRef Key) {
65-
return LookupBucketFor(Key, hash(Key));
66-
}
67-
68-
/// Overload that explicitly takes precomputed hash(Key).
69-
unsigned LookupBucketFor(StringRef Key, uint32_t FullHashValue);
63+
unsigned LookupBucketFor(StringRef Key);
7064

7165
/// FindKey - Look up the bucket that contains the specified key. If it exists
7266
/// in the map, return the bucket number of the key. Otherwise return -1.
7367
/// This does not modify the map.
74-
int FindKey(StringRef Key) const { return FindKey(Key, hash(Key)); }
75-
76-
/// Overload that explicitly takes precomputed hash(Key).
77-
int FindKey(StringRef Key, uint32_t FullHashValue) const;
68+
int FindKey(StringRef Key) const;
7869

7970
/// RemoveKey - Remove the specified StringMapEntry from the table, but do not
8071
/// delete it. This aborts if the value isn't in the table.
@@ -103,13 +94,6 @@ class StringMapImpl {
10394
bool empty() const { return NumItems == 0; }
10495
unsigned size() const { return NumItems; }
10596

106-
/// Returns the hash value that will be used for the given string.
107-
/// This allows precomputing the value and passing it explicitly
108-
/// to some of the functions.
109-
/// The implementation of this function is not guaranteed to be stable
110-
/// and may change.
111-
static uint32_t hash(StringRef Key);
112-
11397
void swap(StringMapImpl &Other) {
11498
std::swap(TheTable, Other.TheTable);
11599
std::swap(NumBuckets, Other.NumBuckets);
@@ -231,19 +215,15 @@ class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
231215
StringMapKeyIterator<ValueTy>(end()));
232216
}
233217

234-
iterator find(StringRef Key) { return find(Key, hash(Key)); }
235-
236-
iterator find(StringRef Key, uint32_t FullHashValue) {
237-
int Bucket = FindKey(Key, FullHashValue);
218+
iterator find(StringRef Key) {
219+
int Bucket = FindKey(Key);
238220
if (Bucket == -1)
239221
return end();
240222
return iterator(TheTable + Bucket, true);
241223
}
242224

243-
const_iterator find(StringRef Key) const { return find(Key, hash(Key)); }
244-
245-
const_iterator find(StringRef Key, uint32_t FullHashValue) const {
246-
int Bucket = FindKey(Key, FullHashValue);
225+
const_iterator find(StringRef Key) const {
226+
int Bucket = FindKey(Key);
247227
if (Bucket == -1)
248228
return end();
249229
return const_iterator(TheTable + Bucket, true);
@@ -325,13 +305,7 @@ class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
325305
/// if and only if the insertion takes place, and the iterator component of
326306
/// the pair points to the element with key equivalent to the key of the pair.
327307
std::pair<iterator, bool> insert(std::pair<StringRef, ValueTy> KV) {
328-
return try_emplace_with_hash(KV.first, hash(KV.first),
329-
std::move(KV.second));
330-
}
331-
332-
std::pair<iterator, bool> insert(std::pair<StringRef, ValueTy> KV,
333-
uint32_t FullHashValue) {
334-
return try_emplace_with_hash(KV.first, FullHashValue, std::move(KV.second));
308+
return try_emplace(KV.first, std::move(KV.second));
335309
}
336310

337311
/// Inserts elements from range [first, last). If multiple elements in the
@@ -365,14 +339,7 @@ class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
365339
/// the pair points to the element with key equivalent to the key of the pair.
366340
template <typename... ArgsTy>
367341
std::pair<iterator, bool> try_emplace(StringRef Key, ArgsTy &&...Args) {
368-
return try_emplace_with_hash(Key, hash(Key), std::forward<ArgsTy>(Args)...);
369-
}
370-
371-
template <typename... ArgsTy>
372-
std::pair<iterator, bool> try_emplace_with_hash(StringRef Key,
373-
uint32_t FullHashValue,
374-
ArgsTy &&...Args) {
375-
unsigned BucketNo = LookupBucketFor(Key, FullHashValue);
342+
unsigned BucketNo = LookupBucketFor(Key);
376343
StringMapEntryBase *&Bucket = TheTable[BucketNo];
377344
if (Bucket && Bucket != getTombstoneVal())
378345
return std::make_pair(iterator(TheTable + BucketNo, false),

llvm/lib/Support/StringMap.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ static inline unsigned *getHashTable(StringMapEntryBase **TheTable,
4343
return reinterpret_cast<unsigned *>(TheTable + NumBuckets + 1);
4444
}
4545

46-
uint32_t StringMapImpl::hash(StringRef Key) { return xxh3_64bits(Key); }
47-
4846
StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) {
4947
ItemSize = itemSize;
5048

@@ -83,14 +81,11 @@ void StringMapImpl::init(unsigned InitSize) {
8381
/// specified bucket will be non-null. Otherwise, it will be null. In either
8482
/// case, the FullHashValue field of the bucket will be set to the hash value
8583
/// of the string.
86-
unsigned StringMapImpl::LookupBucketFor(StringRef Name,
87-
uint32_t FullHashValue) {
88-
#ifdef EXPENSIVE_CHECKS
89-
assert(FullHashValue == hash(Name));
90-
#endif
84+
unsigned StringMapImpl::LookupBucketFor(StringRef Name) {
9185
// Hash table unallocated so far?
9286
if (NumBuckets == 0)
9387
init(16);
88+
unsigned FullHashValue = xxh3_64bits(Name);
9489
if (shouldReverseIterate())
9590
FullHashValue = ~FullHashValue;
9691
unsigned BucketNo = FullHashValue & (NumBuckets - 1);
@@ -144,12 +139,10 @@ unsigned StringMapImpl::LookupBucketFor(StringRef Name,
144139
/// FindKey - Look up the bucket that contains the specified key. If it exists
145140
/// in the map, return the bucket number of the key. Otherwise return -1.
146141
/// This does not modify the map.
147-
int StringMapImpl::FindKey(StringRef Key, uint32_t FullHashValue) const {
142+
int StringMapImpl::FindKey(StringRef Key) const {
148143
if (NumBuckets == 0)
149144
return -1; // Really empty table?
150-
#ifdef EXPENSIVE_CHECKS
151-
assert(FullHashValue == hash(Key);
152-
#endif
145+
unsigned FullHashValue = xxh3_64bits(Key);
153146
if (shouldReverseIterate())
154147
FullHashValue = ~FullHashValue;
155148
unsigned BucketNo = FullHashValue & (NumBuckets - 1);

llvm/unittests/ADT/StringMapTest.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -487,17 +487,6 @@ TEST_F(StringMapTest, NotEqualWithDifferentValues) {
487487
ASSERT_TRUE(B != A);
488488
}
489489

490-
TEST_F(StringMapTest, PrecomputedHash) {
491-
StringMap<int> A;
492-
StringRef Key = "foo";
493-
int Value = 42;
494-
uint64_t Hash = StringMap<int>::hash(Key);
495-
A.insert({"foo", Value}, Hash);
496-
auto I = A.find(Key, Hash);
497-
ASSERT_NE(I, A.end());
498-
ASSERT_EQ(I->second, Value);
499-
}
500-
501490
struct Countable {
502491
int &InstanceCount;
503492
int Number;

0 commit comments

Comments
 (0)