LLVM 24.0.0git
TrieRawHashMap.h
Go to the documentation of this file.
1//===- TrieRawHashMap.h -----------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_ADT_TRIERAWHASHMAP_H
10#define LLVM_ADT_TRIERAWHASHMAP_H
11
12#include "llvm/ADT/ArrayRef.h"
14#include <atomic>
15#include <optional>
16
17namespace llvm {
18
19class raw_ostream;
20
21/// TrieRawHashMap - is a lock-free thread-safe trie that is can be used to
22/// store/index data based on a hash value. It can be customized to work with
23/// any hash algorithm or store any data.
24///
25/// Data structure:
26/// Data node stored in the Trie contains both hash and data:
27/// struct {
28/// HashT Hash;
29/// DataT Data;
30/// };
31///
32/// Data is stored/indexed via a prefix tree, where each node in the tree can be
33/// either the root, a sub-trie or a data node. Assuming a 4-bit hash and two
34/// data objects {0001, A} and {0100, B}, it can be stored in a trie
35/// (assuming Root has 2 bits, SubTrie has 1 bit):
36/// +--------+
37/// |Root[00]| -> {0001, A}
38/// | [01]| -> {0100, B}
39/// | [10]| (empty)
40/// | [11]| (empty)
41/// +--------+
42///
43/// Inserting a new object {0010, C} will result in:
44/// +--------+ +----------+
45/// |Root[00]| -> |SubTrie[0]| -> {0001, A}
46/// | | | [1]| -> {0010, C}
47/// | | +----------+
48/// | [01]| -> {0100, B}
49/// | [10]| (empty)
50/// | [11]| (empty)
51/// +--------+
52/// Note object A is sunk down to a sub-trie during the insertion. All the
53/// nodes are inserted through compare-exchange to ensure thread-safe and
54/// lock-free.
55///
56/// To find an object in the trie, walk the tree with prefix of the hash until
57/// the data node is found. Then the hash is compared with the hash stored in
58/// the data node to see if the is the same object.
59///
60/// Hash collision is not allowed so it is recommended to use trie with a
61/// "strong" hashing algorithm. A well-distributed hash can also result in
62/// better performance and memory usage.
63///
64/// It currently does not support iteration and deletion.
65
66/// Base class for a lock-free thread-safe hash-mapped trie.
68public:
69 static constexpr size_t TrieContentBaseSize = 4;
70 static constexpr size_t DefaultNumRootBits = 6;
71 static constexpr size_t DefaultNumSubtrieBits = 4;
72
73private:
74 template <class T> struct AllocValueType {
76 alignas(T) char Content[sizeof(T)];
77 };
78
79protected:
80 template <class T>
81 static constexpr size_t DefaultContentAllocSize = sizeof(AllocValueType<T>);
82
83 template <class T>
84 static constexpr size_t DefaultContentAllocAlign = alignof(AllocValueType<T>);
85
86 template <class T>
87 static constexpr size_t DefaultContentOffset =
88 offsetof(AllocValueType<T>, Content);
89
90public:
91 static void *operator new(size_t Size) { return ::operator new(Size); }
92 void operator delete(void *Ptr) { ::operator delete(Ptr); }
93
94#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
95 LLVM_DUMP_METHOD void dump() const;
96#endif
97
98 LLVM_ABI void print(raw_ostream &OS) const;
99
100protected:
101 /// Result of a lookup. Suitable for an insertion hint. Maybe could be
102 /// expanded into an iterator of sorts, but likely not useful (visiting
103 /// everything in the trie should probably be done some way other than
104 /// through an iterator pattern).
106 protected:
107 void *get() const { return I == -2u ? P : nullptr; }
108
109 public:
110 PointerBase() noexcept = default;
111
112 private:
114 explicit PointerBase(void *Content) : P(Content), I(-2u) {}
115 PointerBase(void *P, unsigned I, unsigned B) : P(P), I(I), B(B) {}
116
117 bool isHint() const { return I != -1u && I != -2u; }
118
119 void *P = nullptr;
120 unsigned I = -1u;
121 unsigned B = 0;
122 };
123
124 /// Find the stored content with hash.
125 LLVM_ABI PointerBase find(ArrayRef<uint8_t> Hash) const;
126
127 /// Insert and return the stored content.
129 insert(PointerBase Hint, ArrayRef<uint8_t> Hash,
130 function_ref<const uint8_t *(void *Mem, ArrayRef<uint8_t> Hash)>
131 Constructor);
132
134
136 size_t ContentAllocSize, size_t ContentAllocAlign, size_t ContentOffset,
137 std::optional<size_t> NumRootBits = std::nullopt,
138 std::optional<size_t> NumSubtrieBits = std::nullopt);
139
140 /// Destructor, which asserts if there's anything to do. Subclasses should
141 /// call \a destroyImpl().
142 ///
143 /// \pre \a destroyImpl() was already called.
145 LLVM_ABI void destroyImpl(function_ref<void(void *ValueMem)> Destructor);
146
148
149 // Move assignment is not supported as it is not thread-safe.
152
153 // No copy.
157
158 // Debug functions. Implementation details and not guaranteed to be
159 // thread-safe.
161 LLVM_ABI unsigned getStartBit(PointerBase P) const;
162 LLVM_ABI unsigned getNumBits(PointerBase P) const;
163 LLVM_ABI unsigned getNumSlotUsed(PointerBase P) const;
164 LLVM_ABI std::string getTriePrefixAsString(PointerBase P) const;
165 LLVM_ABI unsigned getNumTries() const;
166 // Visit next trie in the allocation chain.
168
169private:
171 const unsigned short ContentAllocSize;
172 const unsigned short ContentAllocAlign;
173 const unsigned short ContentOffset;
174 unsigned short NumRootBits;
175 unsigned short NumSubtrieBits;
176 class ImplType;
177 // ImplPtr is owned by ThreadSafeTrieRawHashMapBase and needs to be freed in
178 // destroyImpl.
179 std::atomic<ImplType *> ImplPtr;
180 ImplType &getOrCreateImpl();
181};
182
183/// Lock-free thread-safe hash-mapped trie.
184template <class T, size_t NumHashBytes>
186public:
187 using HashT = std::array<uint8_t, NumHashBytes>;
188
190 struct value_type {
191 const HashT Hash;
193
194 value_type(value_type &&) = default;
195 value_type(const value_type &) = default;
196
198 : Hash(makeHash(Hash)), Data(Data) {}
201
202 private:
204
205 struct EmplaceTag {};
206 template <class... ArgsT>
207 value_type(ArrayRef<uint8_t> Hash, EmplaceTag, ArgsT &&...Args)
208 : Hash(makeHash(Hash)), Data(std::forward<ArgsT>(Args)...) {}
209
210 static HashT makeHash(ArrayRef<uint8_t> HashRef) {
211 HashT Hash;
212 std::copy(HashRef.begin(), HashRef.end(), Hash.data());
213 return Hash;
214 }
215 };
216
217 using ThreadSafeTrieRawHashMapBase::operator delete;
219
220#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
222#endif
223
225
226private:
227 template <class ValueT> class PointerImpl : PointerBase {
228 friend class ThreadSafeTrieRawHashMap;
229
230 ValueT *get() const {
231 return reinterpret_cast<ValueT *>(PointerBase::get());
232 }
233
234 public:
235 ValueT &operator*() const {
236 assert(get());
237 return *get();
238 }
239 ValueT *operator->() const {
240 assert(get());
241 return get();
242 }
243 explicit operator bool() const { return get(); }
244
245 PointerImpl() = default;
246
247 protected:
248 PointerImpl(PointerBase Result) : PointerBase(Result) {}
249 };
250
251public:
252 class pointer;
253 class const_pointer;
254 class pointer : public PointerImpl<value_type> {
256 friend class const_pointer;
257
258 public:
259 pointer() = default;
260
261 private:
262 pointer(PointerBase Result) : pointer::PointerImpl(Result) {}
263 };
264
265 class const_pointer : public PointerImpl<const value_type> {
267
268 public:
269 const_pointer() = default;
270 const_pointer(const pointer &P) : const_pointer::PointerImpl(P) {}
271
272 private:
273 const_pointer(PointerBase Result) : const_pointer::PointerImpl(Result) {}
274 };
275
277 public:
279 assert(Mem && "Constructor already called, or moved away");
280 return assign(::new (Mem) value_type(Hash, std::move(RHS)));
281 }
283 assert(Mem && "Constructor already called, or moved away");
284 return assign(::new (Mem) value_type(Hash, RHS));
285 }
286 template <class... ArgsT> value_type &emplace(ArgsT &&...Args) {
287 assert(Mem && "Constructor already called, or moved away");
288 return assign(::new (Mem)
289 value_type(Hash, typename value_type::EmplaceTag{},
290 std::forward<ArgsT>(Args)...));
291 }
292
294 : Mem(RHS.Mem), Result(RHS.Result), Hash(RHS.Hash) {
295 RHS.Mem = nullptr; // Moved away, cannot call.
296 }
297 ~LazyValueConstructor() { assert(!Mem && "Constructor never called!"); }
298
299 private:
301 Mem = nullptr;
302 Result = V;
303 return *V;
304 }
306 LazyValueConstructor() = delete;
307 LazyValueConstructor(void *Mem, value_type *&Result, ArrayRef<uint8_t> Hash)
308 : Mem(Mem), Result(Result), Hash(Hash) {
309 assert(Hash.size() == sizeof(HashT) && "Invalid hash");
310 assert(Mem && "Invalid memory for construction");
311 }
312 void *Mem;
313 value_type *&Result;
315 };
316
317 /// Insert with a hint. Default-constructed hint will work, but it's
318 /// recommended to start with a lookup to avoid overhead in object creation
319 /// if it already exists.
320 pointer insertLazy(const_pointer Hint, ArrayRef<uint8_t> Hash,
321 function_ref<void(LazyValueConstructor)> OnConstruct) {
323 Hint, Hash, [&](void *Mem, ArrayRef<uint8_t> Hash) {
324 value_type *Result = nullptr;
325 OnConstruct(LazyValueConstructor(Mem, Result, Hash));
326 return Result->Hash.data();
327 }));
328 }
329
331 function_ref<void(LazyValueConstructor)> OnConstruct) {
332 return insertLazy(const_pointer(), Hash, OnConstruct);
333 }
334
335 pointer insert(const_pointer Hint, value_type &&HashedData) {
336 return insertLazy(Hint, HashedData.Hash, [&](LazyValueConstructor C) {
337 C(std::move(HashedData.Data));
338 });
339 }
340
341 pointer insert(const_pointer Hint, const value_type &HashedData) {
342 return insertLazy(Hint, HashedData.Hash,
343 [&](LazyValueConstructor C) { C(HashedData.Data); });
344 }
345
346 pointer find(ArrayRef<uint8_t> Hash) {
347 assert(Hash.size() == std::tuple_size<HashT>::value);
349 }
350
351 const_pointer find(ArrayRef<uint8_t> Hash) const {
352 assert(Hash.size() == std::tuple_size<HashT>::value);
354 }
355
356 ThreadSafeTrieRawHashMap(std::optional<size_t> NumRootBits = std::nullopt,
357 std::optional<size_t> NumSubtrieBits = std::nullopt)
359 DefaultContentAllocAlign<value_type>,
360 DefaultContentOffset<value_type>,
361 NumRootBits, NumSubtrieBits) {}
362
364 if constexpr (std::is_trivially_destructible<value_type>::value)
365 this->destroyImpl(nullptr);
366 else
367 this->destroyImpl(
368 [](void *P) { static_cast<value_type *>(P)->~value_type(); });
369 }
370
371 // Move constructor okay.
373
374 // No move assignment or any copy.
379};
380
381} // namespace llvm
382
383#endif // LLVM_ADT_TRIERAWHASHMAP_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:215
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:678
#define offsetof(TYPE, MEMBER)
#define I(x, y, z)
Definition MD5.cpp:57
#define T
#define P(N)
Value * RHS
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:130
size_t size() const
Get the array size.
Definition ArrayRef.h:141
iterator begin() const
Definition ArrayRef.h:129
LLVM_ABI PointerBase getNextTrie(PointerBase P) const
LLVM_ABI unsigned getNumTries() const
LLVM_ABI unsigned getNumBits(PointerBase P) const
ThreadSafeTrieRawHashMapBase & operator=(const ThreadSafeTrieRawHashMapBase &)=delete
LLVM_ABI unsigned getNumSlotUsed(PointerBase P) const
LLVM_ABI ~ThreadSafeTrieRawHashMapBase()
Destructor, which asserts if there's anything to do.
LLVM_ABI void destroyImpl(function_ref< void(void *ValueMem)> Destructor)
static constexpr size_t DefaultContentAllocSize
static constexpr size_t DefaultContentAllocAlign
static constexpr size_t DefaultContentOffset
static constexpr size_t DefaultNumSubtrieBits
LLVM_ABI PointerBase find(ArrayRef< uint8_t > Hash) const
Find the stored content with hash.
LLVM_DUMP_METHOD void dump() const
LLVM_ABI PointerBase getRoot() const
LLVM_ABI PointerBase insert(PointerBase Hint, ArrayRef< uint8_t > Hash, function_ref< const uint8_t *(void *Mem, ArrayRef< uint8_t > Hash)> Constructor)
Insert and return the stored content.
LLVM_ABI unsigned getStartBit(PointerBase P) const
LLVM_ABI void print(raw_ostream &OS) const
ThreadSafeTrieRawHashMapBase & operator=(ThreadSafeTrieRawHashMapBase &&RHS)=delete
LLVM_ABI std::string getTriePrefixAsString(PointerBase P) const
static constexpr size_t TrieContentBaseSize
static constexpr size_t DefaultNumRootBits
ThreadSafeTrieRawHashMapBase(const ThreadSafeTrieRawHashMapBase &)=delete
ThreadSafeTrieRawHashMap & operator=(const ThreadSafeTrieRawHashMap &)=delete
pointer insertLazy(ArrayRef< uint8_t > Hash, function_ref< void(LazyValueConstructor)> OnConstruct)
ThreadSafeTrieRawHashMap & operator=(ThreadSafeTrieRawHashMap &&)=delete
pointer find(ArrayRef< uint8_t > Hash)
std::array< uint8_t, NumHashBytes > HashT
pointer insertLazy(const_pointer Hint, ArrayRef< uint8_t > Hash, function_ref< void(LazyValueConstructor)> OnConstruct)
Insert with a hint.
const_pointer find(ArrayRef< uint8_t > Hash) const
pointer insert(const_pointer Hint, value_type &&HashedData)
pointer insert(const_pointer Hint, const value_type &HashedData)
ThreadSafeTrieRawHashMap(std::optional< size_t > NumRootBits=std::nullopt, std::optional< size_t > NumSubtrieBits=std::nullopt)
ThreadSafeTrieRawHashMap(const ThreadSafeTrieRawHashMap &)=delete
ThreadSafeTrieRawHashMap(ThreadSafeTrieRawHashMap &&)=default
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
APInt operator*(APInt a, uint64_t RHS)
Definition APInt.h:2261
static void assign(DXContainerYAML::SourceInfo::SectionHeader &Dst, const dxbc::SourceInfo::SectionHeader &Src)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1933
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
value_type(const value_type &)=default
value_type(ArrayRef< uint8_t > Hash, T &&Data)
value_type(ArrayRef< uint8_t > Hash, const T &Data)