LLVM 23.0.0git
SymbolStringPool.h
Go to the documentation of this file.
1//===-- SymbolStringPool.h -- Thread-safe pool for JIT symbols --*- 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// Contains a thread-safe string pool suitable for use with ORC.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_ORC_SYMBOLSTRINGPOOL_H
14#define LLVM_EXECUTIONENGINE_ORC_SYMBOLSTRINGPOOL_H
15
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/Hashing.h"
18#include "llvm/ADT/StringMap.h"
20#include <atomic>
21#include <mutex>
22
23namespace llvm {
24
25class raw_ostream;
26
27namespace orc {
28
30class SymbolStringPtr;
32
33/// String pool for symbol names used by the JIT.
36 friend class SymbolStringPtrBase;
38
39 // Implemented in DebugUtils.h.
41 const SymbolStringPool &SSP);
42
43public:
44 /// Destroy a SymbolStringPool.
46
47 /// Create a symbol string pointer from the given string.
49
50 /// Remove from the pool any entries that are no longer referenced.
51 void clearDeadEntries();
52
53 /// Returns true if the pool is empty.
54 bool empty() const;
55
56private:
57 size_t getRefCount(const SymbolStringPtrBase &S) const;
58
59 using RefCountType = std::atomic<size_t>;
60 using PoolMap = StringMap<RefCountType>;
61 using PoolMapEntry = StringMapEntry<RefCountType>;
62 mutable std::mutex PoolMutex;
63 PoolMap Pool;
64};
65
66/// Base class for both owning and non-owning symbol-string ptrs.
67///
68/// All symbol-string ptrs are convertible to bool, dereferenceable and
69/// comparable.
70///
71/// SymbolStringPtrBases are default-constructible and constructible
72/// from nullptr to enable comparison with these values.
74 friend class SymbolStringPool;
76 friend struct DenseMapInfo<SymbolStringPtr>;
78
79public:
81 SymbolStringPtrBase(std::nullptr_t) {}
82
83 explicit operator bool() const { return S; }
84
85 StringRef operator*() const { return S->first(); }
86
88 return LHS.S == RHS.S;
89 }
90
92 return !(LHS == RHS);
93 }
94
96 return LHS.S < RHS.S;
97 }
98
100 const SymbolStringPtrBase &Sym);
101
102#ifndef NDEBUG
103 // Returns true if the pool entry's ref count is above zero (or if the entry
104 // is an empty or tombstone value). Useful for debugging and testing -- this
105 // method can be used to identify SymbolStringPtrs and
106 // NonOwningSymbolStringPtrs that are pointing to abandoned pool entries.
107 bool poolEntryIsAlive() const {
108 return isRealPoolEntry(S) ? S->getValue() != 0 : true;
109 }
110#endif
111
112protected:
113 using PoolEntry = SymbolStringPool::PoolMapEntry;
115
117
118 constexpr static uintptr_t InvalidPtrMask =
119 (std::numeric_limits<uintptr_t>::max() - 3)
121
122 // Returns false for null, empty, and tombstone values, true otherwise.
124 return ((reinterpret_cast<uintptr_t>(P) - 1) & InvalidPtrMask) !=
126 }
127
128 size_t getRefCount() const {
129 return isRealPoolEntry(S) ? size_t(S->getValue()) : size_t(0);
130 }
131
132 PoolEntryPtr S = nullptr;
133};
134
135/// Pointer to a pooled string representing a symbol name.
137 friend class SymbolStringPool;
139 friend struct DenseMapInfo<SymbolStringPtr>;
140
141public:
142 SymbolStringPtr() = default;
143 SymbolStringPtr(std::nullptr_t) {}
147
149
151 decRef();
152 S = Other.S;
153 incRef();
154 return *this;
155 }
156
158
160 decRef();
161 S = nullptr;
162 std::swap(S, Other.S);
163 return *this;
164 }
165
166 ~SymbolStringPtr() { decRef(); }
167
168private:
170
171 void incRef() {
172 if (isRealPoolEntry(S))
173 ++S->getValue();
174 }
175
176 void decRef() {
177 if (isRealPoolEntry(S)) {
178 assert(S->getValue() && "Releasing SymbolStringPtr with zero ref count");
179 --S->getValue();
180 }
181 }
182};
183
184/// Provides unsafe access to ownership operations on SymbolStringPtr.
185/// This class can be used to manage SymbolStringPtr instances from C.
187public:
188 using PoolEntry = SymbolStringPool::PoolMapEntry;
189
191
192 /// Create an unsafe pool entry ref without changing the ref-count.
194 return S.S;
195 }
196
197 /// Consumes the given SymbolStringPtr without releasing the pool entry.
199 PoolEntry *E = nullptr;
200 std::swap(E, S.S);
201 return E;
202 }
203
204 PoolEntry *rawPtr() { return E; }
205
206 /// Creates a SymbolStringPtr for this entry, with the SymbolStringPtr
207 /// retaining the entry as usual.
209
210 /// Creates a SymbolStringPtr for this entry *without* performing a retain
211 /// operation during construction.
214 std::swap(S.S, E);
215 return S;
216 }
217
218 void retain() { ++E->getValue(); }
219 void release() { --E->getValue(); }
220
221private:
222 PoolEntry *E = nullptr;
223};
224
225/// Non-owning SymbolStringPool entry pointer. Instances are comparable with
226/// SymbolStringPtr instances and guaranteed to have the same hash, but do not
227/// affect the ref-count of the pooled string (and are therefore cheaper to
228/// copy).
229///
230/// NonOwningSymbolStringPtrs are silently invalidated if the pool entry's
231/// ref-count drops to zero, so they should only be used in contexts where a
232/// corresponding SymbolStringPtr is known to exist (which will guarantee that
233/// the ref-count stays above zero). E.g. in a graph where nodes are
234/// represented by SymbolStringPtrs the edges can be represented by pairs of
235/// NonOwningSymbolStringPtrs and this will make the introduction of deletion
236/// of edges cheaper.
239
240public:
242 explicit NonOwningSymbolStringPtr(const SymbolStringPtr &S)
243 : SymbolStringPtrBase(S) {}
244
245 using SymbolStringPtrBase::operator=;
246
247private:
249};
250
254 "SymbolStringPtr constructed from invalid non-owning pointer.");
255
256 if (isRealPoolEntry(S))
257 ++S->getValue();
258}
259
261#ifndef NDEBUG
263 assert(Pool.empty() && "Dangling references at pool destruction time");
264#endif // NDEBUG
265}
266
268 std::lock_guard<std::mutex> Lock(PoolMutex);
270 bool Added;
271 std::tie(I, Added) = Pool.try_emplace(S, 0);
272 return SymbolStringPtr(&*I);
273}
274
276 std::lock_guard<std::mutex> Lock(PoolMutex);
277 Pool.remove_if([](PoolMapEntry &E) { return E.getValue() == 0; });
278}
279
280inline bool SymbolStringPool::empty() const {
281 std::lock_guard<std::mutex> Lock(PoolMutex);
282 return Pool.empty();
283}
284
285inline size_t
286SymbolStringPool::getRefCount(const SymbolStringPtrBase &S) const {
287 return S.getRefCount();
288}
289
291 const SymbolStringPtrBase &Sym);
292
296
297} // end namespace orc
298
299template <>
300struct DenseMapInfo<orc::SymbolStringPtr> {
301
305
308 return LHS.S == RHS.S;
309 }
310};
311
312template <> struct DenseMapInfo<orc::NonOwningSymbolStringPtr> {
313
318
321 return LHS.S == RHS.S;
322 }
323};
324
325} // end namespace llvm
326
327#endif // LLVM_EXECUTIONENGINE_ORC_SYMBOLSTRINGPOOL_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition Compiler.h:213
This file defines the DenseMap class.
#define I(x, y, z)
Definition MD5.cpp:57
#define P(N)
Value * RHS
Value * LHS
const ValueTy & getValue() const
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:128
StringMapIterBase< RefCountType, false > iterator
Definition StringMap.h:209
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
An opaque object representing a hash code.
Definition Hashing.h:77
Non-owning SymbolStringPool entry pointer.
SymbolStringPool::PoolMapEntry PoolEntry
SymbolStringPtr copyToSymbolStringPtr()
Creates a SymbolStringPtr for this entry, with the SymbolStringPtr retaining the entry as usual.
static SymbolStringPoolEntryUnsafe from(const SymbolStringPtrBase &S)
Create an unsafe pool entry ref without changing the ref-count.
static SymbolStringPoolEntryUnsafe take(SymbolStringPtr &&S)
Consumes the given SymbolStringPtr without releasing the pool entry.
SymbolStringPtr moveToSymbolStringPtr()
Creates a SymbolStringPtr for this entry without performing a retain operation during construction.
String pool for symbol names used by the JIT.
LLVM_ABI friend raw_ostream & operator<<(raw_ostream &OS, const SymbolStringPool &SSP)
Dump a SymbolStringPool. Useful for debugging dangling-pointer crashes.
SymbolStringPtr intern(StringRef S)
Create a symbol string pointer from the given string.
bool empty() const
Returns true if the pool is empty.
~SymbolStringPool()
Destroy a SymbolStringPool.
void clearDeadEntries()
Remove from the pool any entries that are no longer referenced.
Base class for both owning and non-owning symbol-string ptrs.
static bool isRealPoolEntry(PoolEntryPtr P)
friend bool operator!=(SymbolStringPtrBase LHS, SymbolStringPtrBase RHS)
friend bool operator<(SymbolStringPtrBase LHS, SymbolStringPtrBase RHS)
friend bool operator==(SymbolStringPtrBase LHS, SymbolStringPtrBase RHS)
static constexpr uintptr_t InvalidPtrMask
LLVM_ABI friend raw_ostream & operator<<(raw_ostream &OS, const SymbolStringPtrBase &Sym)
SymbolStringPool::PoolMapEntry PoolEntry
Pointer to a pooled string representing a symbol name.
SymbolStringPtr(const SymbolStringPtr &Other)
SymbolStringPtr & operator=(SymbolStringPtr &&Other)
SymbolStringPtr & operator=(const SymbolStringPtr &Other)
SymbolStringPtr(SymbolStringPtr &&Other)
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
hash_code hash_value(const orc::SymbolStringPtrBase &S)
LLVM_ABI raw_ostream & operator<<(raw_ostream &OS, const SymbolNameSet &Symbols)
Render a SymbolNameSet.
This is an optimization pass for GlobalISel generic memory operations.
@ Other
Any other memory.
Definition ModRef.h:68
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:862
static bool isEqual(const orc::SymbolStringPtrBase &LHS, const orc::SymbolStringPtrBase &RHS)
static unsigned getHashValue(const orc::SymbolStringPtrBase &V)
static bool isEqual(const orc::SymbolStringPtrBase &LHS, const orc::SymbolStringPtrBase &RHS)
static unsigned getHashValue(const orc::SymbolStringPtrBase &V)
An information struct used to provide DenseMap with the various necessary components for a given valu...
A traits type that is used to handle pointer types and things that are just wrappers for pointers as ...