Skip to content

Commit 63ccd9c

Browse files
fixup! [ADT] Add TrieRawHashMap
1 parent 458e3af commit 63ccd9c

File tree

4 files changed

+25
-37
lines changed

4 files changed

+25
-37
lines changed

llvm/include/llvm/ADT/TrieRawHashMap.h

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@ class ThreadSafeTrieRawHashMapBase {
105105

106106
public:
107107
PointerBase() noexcept = default;
108-
PointerBase(PointerBase &&) = default;
109-
PointerBase(const PointerBase &) = default;
110-
PointerBase &operator=(PointerBase &&) = default;
111-
PointerBase &operator=(const PointerBase &) = default;
112108

113109
private:
114110
friend class ThreadSafeTrieRawHashMapBase;
@@ -228,9 +224,7 @@ class ThreadSafeTrieRawHashMap : public ThreadSafeTrieRawHashMapBase {
228224
friend class ThreadSafeTrieRawHashMap;
229225

230226
ValueT *get() const {
231-
if (void *B = PointerBase::get())
232-
return reinterpret_cast<ValueT *>(B);
233-
return nullptr;
227+
return reinterpret_cast<ValueT *>(PointerBase::get());
234228
}
235229

236230
public:
@@ -245,10 +239,6 @@ class ThreadSafeTrieRawHashMap : public ThreadSafeTrieRawHashMapBase {
245239
explicit operator bool() const { return get(); }
246240

247241
PointerImpl() = default;
248-
PointerImpl(PointerImpl &&) = default;
249-
PointerImpl(const PointerImpl &) = default;
250-
PointerImpl &operator=(PointerImpl &&) = default;
251-
PointerImpl &operator=(const PointerImpl &) = default;
252242

253243
protected:
254244
PointerImpl(PointerBase Result) : PointerBase(Result) {}
@@ -263,10 +253,6 @@ class ThreadSafeTrieRawHashMap : public ThreadSafeTrieRawHashMapBase {
263253

264254
public:
265255
pointer() = default;
266-
pointer(pointer &&) = default;
267-
pointer(const pointer &) = default;
268-
pointer &operator=(pointer &&) = default;
269-
pointer &operator=(const pointer &) = default;
270256

271257
private:
272258
pointer(PointerBase Result) : pointer::PointerImpl(Result) {}
@@ -277,11 +263,6 @@ class ThreadSafeTrieRawHashMap : public ThreadSafeTrieRawHashMapBase {
277263

278264
public:
279265
const_pointer() = default;
280-
const_pointer(const_pointer &&) = default;
281-
const_pointer(const const_pointer &) = default;
282-
const_pointer &operator=(const_pointer &&) = default;
283-
const_pointer &operator=(const const_pointer &) = default;
284-
285266
const_pointer(const pointer &P) : const_pointer::PointerImpl(P) {}
286267

287268
private:

llvm/lib/Support/TrieHashIndexGenerator.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ struct IndexGenerator {
4141
}
4242

4343
size_t hint(unsigned Index, unsigned Bit) {
44-
assert(Index >= 0);
4544
assert(Bit < Bytes.size() * 8);
4645
assert(Bit == 0 || (Bit - NumRootBits) % NumSubtrieBits == 0);
4746
StartBit = Bit;

llvm/lib/Support/TrieRawHashMap.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct TrieContent final : public TrieNode {
4949

5050
static bool classof(const TrieNode *TN) { return !TN->IsSubtrie; }
5151
};
52+
5253
static_assert(sizeof(TrieContent) ==
5354
ThreadSafeTrieRawHashMapBase::TrieContentBaseSize,
5455
"Check header assumption!");
@@ -165,7 +166,7 @@ struct ThreadSafeTrieRawHashMapBase::ImplType {
165166
while (!Root.Next.compare_exchange_weak(CurrentHead, S.get()))
166167
S->Next.exchange(CurrentHead);
167168

168-
// Ownership transferred to subtrie.
169+
// Ownership transferred to subtrie successfully. Release the unique_ptr.
169170
return S.release();
170171
}
171172

@@ -191,9 +192,13 @@ ThreadSafeTrieRawHashMapBase::getOrCreateImpl() {
191192
// If another thread wins this one is destroyed locally.
192193
std::unique_ptr<ImplType> Impl = ImplType::create(0, NumRootBits);
193194
ImplType *ExistingImpl = nullptr;
195+
196+
// If the ownership transferred succesfully, release unique_ptr and return
197+
// the pointer to the new ImplType.
194198
if (ImplPtr.compare_exchange_strong(ExistingImpl, Impl.get()))
195199
return *Impl.release();
196200

201+
// Already created, return the existing ImplType.
197202
return *ExistingImpl;
198203
}
199204

llvm/unittests/ADT/TrieRawHashMapTest.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class TrieRawHashMapTestHelper {
4949
} // namespace llvm
5050

5151
namespace {
52-
template <typename DataType, size_t HashSize>
52+
template <typename DataType, size_t HashSize = sizeof(uint64_t)>
5353
class SimpleTrieHashMapTest : public TrieRawHashMapTestHelper,
5454
public ::testing::Test {
5555
public:
@@ -64,11 +64,7 @@ class SimpleTrieHashMapTest : public TrieRawHashMapTestHelper,
6464
}
6565

6666
void destroyTrie() { Trie.reset(); }
67-
68-
~SimpleTrieHashMapTest() {
69-
if (Trie)
70-
Trie.reset();
71-
}
67+
~SimpleTrieHashMapTest() { destroyTrie(); }
7268

7369
// Use the number itself as hash to test the pathological case.
7470
static HashType hash(uint64_t Num) {
@@ -83,7 +79,7 @@ class SimpleTrieHashMapTest : public TrieRawHashMapTestHelper,
8379
std::optional<TrieType> Trie;
8480
};
8581

86-
using SmallNodeTrieTest = SimpleTrieHashMapTest<uint64_t, sizeof(uint64_t)>;
82+
using SmallNodeTrieTest = SimpleTrieHashMapTest<uint64_t>;
8783

8884
TEST_F(SmallNodeTrieTest, TrieAllocation) {
8985
NumType Numbers[] = {
@@ -209,9 +205,7 @@ TEST_F(SmallNodeTrieTest, TrieStructureSmallFinalSubtrie) {
209205
}
210206
for (NumType N : Numbers) {
211207
TrieType::pointer Lookup = Trie.find(hash(N));
212-
EXPECT_TRUE(Lookup);
213-
if (!Lookup)
214-
continue;
208+
ASSERT_TRUE(Lookup);
215209
EXPECT_EQ(hash(N), Lookup->Hash);
216210
EXPECT_EQ(N, Lookup->Data);
217211

@@ -273,11 +267,11 @@ TEST_F(SmallNodeTrieTest, TrieDestructionLoop) {
273267

274268
struct NumWithDestructorT {
275269
uint64_t Num;
276-
~NumWithDestructorT() {}
270+
llvm::function_ref<void()> DestructorCallback;
271+
~NumWithDestructorT() { DestructorCallback(); }
277272
};
278273

279-
using NodeWithDestructorTrieTest =
280-
SimpleTrieHashMapTest<NumWithDestructorT, sizeof(uint64_t)>;
274+
using NodeWithDestructorTrieTest = SimpleTrieHashMapTest<NumWithDestructorT>;
281275

282276
TEST_F(NodeWithDestructorTrieTest, TrieDestructionLoop) {
283277
// Test destroying large Trie. Make sure there is no recursion that can
@@ -289,17 +283,26 @@ TEST_F(NodeWithDestructorTrieTest, TrieDestructionLoop) {
289283
// Fill them up. Pick a MaxN high enough to cause a stack overflow in debug
290284
// builds.
291285
static constexpr uint64_t MaxN = 100000;
286+
287+
uint64_t DestructorCalled = 0;
288+
auto DtorCallback = [&DestructorCalled]() { ++DestructorCalled; };
292289
for (uint64_t N = 0; N != MaxN; ++N) {
293290
HashType Hash = hash(N);
294-
Trie.insert(TrieType::pointer(), TrieType::value_type(Hash, NumType{N}));
291+
Trie.insert(TrieType::pointer(),
292+
TrieType::value_type(Hash, NumType{N, DtorCallback}));
295293
}
294+
// Reset the count after all the temporaries get destroyed.
295+
DestructorCalled = 0;
296296

297297
// Destroy tries. If destruction is recursive and MaxN is high enough, these
298298
// will both fail.
299299
destroyTrie();
300+
301+
// Count the number of destructor calls during `destroyTrie()`.
302+
ASSERT_EQ(DestructorCalled, MaxN);
300303
}
301304

302-
using NumStrNodeTrieTest = SimpleTrieHashMapTest<std::string, sizeof(uint64_t)>;
305+
using NumStrNodeTrieTest = SimpleTrieHashMapTest<std::string>;
303306

304307
TEST_F(NumStrNodeTrieTest, TrieInsertLazy) {
305308
for (unsigned RootBits : {2, 3, 6, 10}) {

0 commit comments

Comments
 (0)