LLVM  14.0.0git
SymbolStringPool.h
Go to the documentation of this file.
1 //===- SymbolStringPool.h - Multi-threaded 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 multi-threaded 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/StringMap.h"
18 #include <atomic>
19 #include <mutex>
20 
21 namespace llvm {
22 namespace orc {
23 
24 class SymbolStringPtr;
25 
26 /// String pool for symbol names used by the JIT.
28  friend class SymbolStringPtr;
29 public:
30  /// Destroy a SymbolStringPool.
32 
33  /// Create a symbol string pointer from the given string.
35 
36  /// Remove from the pool any entries that are no longer referenced.
37  void clearDeadEntries();
38 
39  /// Returns true if the pool is empty.
40  bool empty() const;
41 private:
42  using RefCountType = std::atomic<size_t>;
45  mutable std::mutex PoolMutex;
46  PoolMap Pool;
47 };
48 
49 /// Pointer to a pooled string representing a symbol name.
51  friend class OrcV2CAPIHelper;
52  friend class SymbolStringPool;
53  friend struct DenseMapInfo<SymbolStringPtr>;
54 
55 public:
56  SymbolStringPtr() = default;
57  SymbolStringPtr(std::nullptr_t) {}
59  : S(Other.S) {
60  if (isRealPoolEntry(S))
61  ++S->getValue();
62  }
63 
65  if (isRealPoolEntry(S)) {
66  assert(S->getValue() && "Releasing SymbolStringPtr with zero ref count");
67  --S->getValue();
68  }
69  S = Other.S;
70  if (isRealPoolEntry(S))
71  ++S->getValue();
72  return *this;
73  }
74 
76  std::swap(S, Other.S);
77  }
78 
80  if (isRealPoolEntry(S)) {
81  assert(S->getValue() && "Releasing SymbolStringPtr with zero ref count");
82  --S->getValue();
83  }
84  S = nullptr;
85  std::swap(S, Other.S);
86  return *this;
87  }
88 
90  if (isRealPoolEntry(S)) {
91  assert(S->getValue() && "Releasing SymbolStringPtr with zero ref count");
92  --S->getValue();
93  }
94  }
95 
96  explicit operator bool() const { return S; }
97 
98  StringRef operator*() const { return S->first(); }
99 
100  friend bool operator==(const SymbolStringPtr &LHS,
101  const SymbolStringPtr &RHS) {
102  return LHS.S == RHS.S;
103  }
104 
105  friend bool operator!=(const SymbolStringPtr &LHS,
106  const SymbolStringPtr &RHS) {
107  return !(LHS == RHS);
108  }
109 
110  friend bool operator<(const SymbolStringPtr &LHS,
111  const SymbolStringPtr &RHS) {
112  return LHS.S < RHS.S;
113  }
114 
115 private:
116  using PoolEntry = SymbolStringPool::PoolMapEntry;
117  using PoolEntryPtr = PoolEntry *;
118 
120  : S(S) {
121  if (isRealPoolEntry(S))
122  ++S->getValue();
123  }
124 
125  // Returns false for null, empty, and tombstone values, true otherwise.
126  bool isRealPoolEntry(PoolEntryPtr P) {
127  return ((reinterpret_cast<uintptr_t>(P) - 1) & InvalidPtrMask) !=
128  InvalidPtrMask;
129  }
130 
131  static SymbolStringPtr getEmptyVal() {
132  return SymbolStringPtr(reinterpret_cast<PoolEntryPtr>(EmptyBitPattern));
133  }
134 
135  static SymbolStringPtr getTombstoneVal() {
136  return SymbolStringPtr(reinterpret_cast<PoolEntryPtr>(TombstoneBitPattern));
137  }
138 
139  constexpr static uintptr_t EmptyBitPattern =
141  << PointerLikeTypeTraits<PoolEntryPtr>::NumLowBitsAvailable;
142 
143  constexpr static uintptr_t TombstoneBitPattern =
145  << PointerLikeTypeTraits<PoolEntryPtr>::NumLowBitsAvailable;
146 
147  constexpr static uintptr_t InvalidPtrMask =
149  << PointerLikeTypeTraits<PoolEntryPtr>::NumLowBitsAvailable;
150 
151  PoolEntryPtr S = nullptr;
152 };
153 
155 #ifndef NDEBUG
157  assert(Pool.empty() && "Dangling references at pool destruction time");
158 #endif // NDEBUG
159 }
160 
162  std::lock_guard<std::mutex> Lock(PoolMutex);
164  bool Added;
165  std::tie(I, Added) = Pool.try_emplace(S, 0);
166  return SymbolStringPtr(&*I);
167 }
168 
170  std::lock_guard<std::mutex> Lock(PoolMutex);
171  for (auto I = Pool.begin(), E = Pool.end(); I != E;) {
172  auto Tmp = I++;
173  if (Tmp->second == 0)
174  Pool.erase(Tmp);
175  }
176 }
177 
178 inline bool SymbolStringPool::empty() const {
179  std::lock_guard<std::mutex> Lock(PoolMutex);
180  return Pool.empty();
181 }
182 
183 } // end namespace orc
184 
185 template <>
186 struct DenseMapInfo<orc::SymbolStringPtr> {
187 
189  return orc::SymbolStringPtr::getEmptyVal();
190  }
191 
193  return orc::SymbolStringPtr::getTombstoneVal();
194  }
195 
196  static unsigned getHashValue(const orc::SymbolStringPtr &V) {
198  }
199 
200  static bool isEqual(const orc::SymbolStringPtr &LHS,
201  const orc::SymbolStringPtr &RHS) {
202  return LHS.S == RHS.S;
203  }
204 };
205 
206 } // end namespace llvm
207 
208 #endif // LLVM_EXECUTIONENGINE_ORC_SYMBOLSTRINGPOOL_H
llvm::orc::SymbolStringPool::~SymbolStringPool
~SymbolStringPool()
Destroy a SymbolStringPool.
Definition: SymbolStringPool.h:154
llvm::orc::SymbolStringPtr::SymbolStringPtr
SymbolStringPtr(const SymbolStringPtr &Other)
Definition: SymbolStringPool.h:58
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::StringMapEntry
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMapEntry.h:98
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::orc::SymbolStringPtr::SymbolStringPtr
SymbolStringPtr(SymbolStringPtr &&Other)
Definition: SymbolStringPool.h:75
llvm::orc::SymbolStringPtr
Pointer to a pooled string representing a symbol name.
Definition: SymbolStringPool.h:50
llvm::StringMap::end
iterator end()
Definition: StringMap.h:203
DenseMap.h
llvm::DenseMapInfo< orc::SymbolStringPtr >::getHashValue
static unsigned getHashValue(const orc::SymbolStringPtr &V)
Definition: SymbolStringPool.h:196
llvm::orc::SymbolStringPtr::operator==
friend bool operator==(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS)
Definition: SymbolStringPool.h:100
llvm::orc::SymbolStringPool::empty
bool empty() const
Returns true if the pool is empty.
Definition: SymbolStringPool.h:178
llvm::Lock
static sys::Mutex Lock
Definition: NVPTXUtilities.cpp:39
llvm::DenseMapInfo
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: APInt.h:34
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::orc::SymbolStringPtr::SymbolStringPtr
SymbolStringPtr(std::nullptr_t)
Definition: SymbolStringPool.h:57
llvm::orc::SymbolStringPool
String pool for symbol names used by the JIT.
Definition: SymbolStringPool.h:27
llvm::DenseMapInfo< orc::SymbolStringPtr >::getTombstoneKey
static orc::SymbolStringPtr getTombstoneKey()
Definition: SymbolStringPool.h:192
llvm::DenseMapInfo< orc::SymbolStringPtr >::getEmptyKey
static orc::SymbolStringPtr getEmptyKey()
Definition: SymbolStringPool.h:188
StringMap.h
llvm::StringMap< RefCountType >
llvm::orc::SymbolStringPtr::~SymbolStringPtr
~SymbolStringPtr()
Definition: SymbolStringPool.h:89
llvm::orc::SymbolStringPtr::operator*
StringRef operator*() const
Definition: SymbolStringPool.h:98
llvm::StringMapIterator
Definition: StringMap.h:25
llvm::StringMap::erase
void erase(iterator I)
Definition: StringMap.h:368
llvm::orc::SymbolStringPtr::operator=
SymbolStringPtr & operator=(const SymbolStringPtr &Other)
Definition: SymbolStringPool.h:64
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::StringMap::begin
iterator begin()
Definition: StringMap.h:202
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::swap
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:840
llvm::orc::SymbolStringPtr::operator!=
friend bool operator!=(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS)
Definition: SymbolStringPool.h:105
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::orc::SymbolStringPool::SymbolStringPtr
friend class SymbolStringPtr
Definition: SymbolStringPool.h:28
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::orc::SymbolStringPtr::operator<
friend bool operator<(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS)
Definition: SymbolStringPool.h:110
llvm::orc::OrcV2CAPIHelper
Definition: OrcV2CBindings.cpp:28
llvm::orc::SymbolStringPool::intern
SymbolStringPtr intern(StringRef S)
Create a symbol string pointer from the given string.
Definition: SymbolStringPool.h:161
llvm::DenseMapInfo< orc::SymbolStringPtr >::isEqual
static bool isEqual(const orc::SymbolStringPtr &LHS, const orc::SymbolStringPtr &RHS)
Definition: SymbolStringPool.h:200
llvm::StringMapImpl::empty
bool empty() const
Definition: StringMap.h:92
llvm::max
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:340
llvm::orc::SymbolStringPool::clearDeadEntries
void clearDeadEntries()
Remove from the pool any entries that are no longer referenced.
Definition: SymbolStringPool.h:169
llvm::orc::SymbolStringPtr::operator=
SymbolStringPtr & operator=(SymbolStringPtr &&Other)
Definition: SymbolStringPool.h:79
llvm::orc::SymbolStringPtr::SymbolStringPtr
SymbolStringPtr()=default
llvm::StringMap::try_emplace
std::pair< iterator, bool > try_emplace(StringRef Key, ArgsTy &&... Args)
Emplace a new element for the specified key into the map if the key isn't already in the map.
Definition: StringMap.h:328
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1191