1//===-- SymbolStringPool.h -- Thread-safe pool for JIT symbols --*- C++ -*-===//
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
7//===----------------------------------------------------------------------===//
9// Contains a thread-safe string pool suitable for use with ORC.
11//===----------------------------------------------------------------------===//
13#ifndef LLVM_EXECUTIONENGINE_ORC_SYMBOLSTRINGPOOL_H
14#define LLVM_EXECUTIONENGINE_ORC_SYMBOLSTRINGPOOL_H
33/// String pool for symbol names used by the JIT.
39 // Implemented in DebugUtils.h.
44 /// Destroy a SymbolStringPool.
47 /// Create a symbol string pointer from the given string.
50 /// Remove from the pool any entries that are no longer referenced.
53 /// Returns true if the pool is empty.
59 using RefCountType = std::atomic<size_t>;
62 mutable std::mutex PoolMutex;
66/// Base class for both owning and non-owning symbol-string ptrs.
68/// All symbol-string ptrs are convertible to bool, dereferenceable and
71/// SymbolStringPtrBases are default-constructible and constructible
72/// from nullptr to enable comparison with these values.
83 explicit operator bool()
const {
return S; }
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.
119 std::numeric_limits<uintptr_t>::max()
123 (std::numeric_limits<uintptr_t>::max() - 1)
127 (std::numeric_limits<uintptr_t>::max() - 3)
130 // Returns false for null, empty, and tombstone values, true otherwise.
143/// Pointer to a pooled string representing a symbol name.
186 assert(
S->
getValue() &&
"Releasing SymbolStringPtr with zero ref count");
200/// Provides unsafe access to ownership operations on SymbolStringPtr.
201/// This class can be used to manage SymbolStringPtr instances from C.
208 /// Create an unsafe pool entry ref without changing the ref-count.
213 /// Consumes the given SymbolStringPtr without releasing the pool entry.
222 /// Creates a SymbolStringPtr for this entry, with the SymbolStringPtr
223 /// retaining the entry as usual.
226 /// Creates a SymbolStringPtr for this entry *without* performing a retain
227 /// operation during construction.
241/// Non-owning SymbolStringPool entry pointer. Instances are comparable with
242/// SymbolStringPtr instances and guaranteed to have the same hash, but do not
243/// affect the ref-count of the pooled string (and are therefore cheaper to
246/// NonOwningSymbolStringPtrs are silently invalidated if the pool entry's
247/// ref-count drops to zero, so they should only be used in contexts where a
248/// corresponding SymbolStringPtr is known to exist (which will guarantee that
249/// the ref-count stays above zero). E.g. in a graph where nodes are
250/// represented by SymbolStringPtrs the edges can be represented by pairs of
251/// NonOwningSymbolStringPtrs and this will make the introduction of deletion
261 using SymbolStringPtrBase::operator=;
280 "SymbolStringPtr constructed from invalid non-owning pointer.");
289 assert(Pool.empty() &&
"Dangling references at pool destruction time");
294 std::lock_guard<std::mutex> Lock(PoolMutex);
297 std::tie(
I, Added) = Pool.try_emplace(S, 0);
302 std::lock_guard<std::mutex> Lock(PoolMutex);
303 for (
auto I = Pool.begin(),
E = Pool.end();
I !=
E;) {
305 if (Tmp->second == 0)
311 std::lock_guard<std::mutex> Lock(PoolMutex);
321 const SymbolStringPtrBase &Sym);
327}
// end namespace orc
333 return orc::SymbolStringPtr::getEmptyVal();
337 return orc::SymbolStringPtr::getTombstoneVal();
353 return orc::NonOwningSymbolStringPtr::getEmptyVal();
357 return orc::NonOwningSymbolStringPtr::getTombstoneVal();
371}
// end namespace llvm
373#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")
This file defines the DenseMap class.
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",...
StringMapIterBase< RefCountType, false > iterator
StringRef - Represent a constant reference to a string, i.e.
An opaque object representing a hash code.
Non-owning SymbolStringPool entry pointer.
NonOwningSymbolStringPtr()=default
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.
SymbolStringPoolEntryUnsafe(PoolEntry *E)
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.
friend class SymbolStringPoolEntryUnsafe
friend class SymbolStringPtrBase
friend class SymbolStringPoolTest
~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)
bool poolEntryIsAlive() const
SymbolStringPtrBase()=default
friend class SymbolStringPool
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)
size_t getRefCount() const
StringRef operator*() const
static constexpr uintptr_t EmptyBitPattern
static constexpr uintptr_t TombstoneBitPattern
friend class SymbolStringPoolEntryUnsafe
SymbolStringPtrBase(PoolEntryPtr S)
SymbolStringPool::PoolMapEntry PoolEntry
SymbolStringPtrBase(std::nullptr_t)
Pointer to a pooled string representing a symbol name.
friend class SymbolStringPool
SymbolStringPtr()=default
SymbolStringPtr(const SymbolStringPtr &Other)
SymbolStringPtr(std::nullptr_t)
friend class SymbolStringPoolEntryUnsafe
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.
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.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
static orc::NonOwningSymbolStringPtr getEmptyKey()
static bool isEqual(const orc::SymbolStringPtrBase &LHS, const orc::SymbolStringPtrBase &RHS)
static unsigned getHashValue(const orc::SymbolStringPtrBase &V)
static orc::NonOwningSymbolStringPtr getTombstoneKey()
static orc::SymbolStringPtr getEmptyKey()
static bool isEqual(const orc::SymbolStringPtrBase &LHS, const orc::SymbolStringPtrBase &RHS)
static unsigned getHashValue(const orc::SymbolStringPtrBase &V)
static orc::SymbolStringPtr getTombstoneKey()
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 ...