LLVM  8.0.1
SymbolStringPool.h
Go to the documentation of this file.
1 //===- SymbolStringPool.h - Multi-threaded pool for JIT symbols -*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Contains a multi-threaded string pool suitable for use with ORC.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_SYMBOLSTRINGPOOL_H
15 #define LLVM_EXECUTIONENGINE_ORC_SYMBOLSTRINGPOOL_H
16 
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/StringMap.h"
19 #include <atomic>
20 #include <mutex>
21 
22 namespace llvm {
23 namespace orc {
24 
25 class SymbolStringPtr;
26 
27 /// String pool for symbol names used by the JIT.
29  friend class SymbolStringPtr;
30 public:
31  /// Destroy a SymbolStringPool.
33 
34  /// Create a symbol string pointer from the given string.
36 
37  /// Remove from the pool any entries that are no longer referenced.
38  void clearDeadEntries();
39 
40  /// Returns true if the pool is empty.
41  bool empty() const;
42 private:
43  using RefCountType = std::atomic<size_t>;
46  mutable std::mutex PoolMutex;
47  PoolMap Pool;
48 };
49 
50 /// Pointer to a pooled string representing a symbol name.
52  friend class SymbolStringPool;
53  friend struct DenseMapInfo<SymbolStringPtr>;
54  friend bool operator==(const SymbolStringPtr &LHS,
55  const SymbolStringPtr &RHS);
56  friend bool operator<(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS);
57 
58  static SymbolStringPool::PoolMapEntry Tombstone;
59 
60 public:
61  SymbolStringPtr() = default;
63  : S(Other.S) {
64  if (S)
65  ++S->getValue();
66  }
67 
69  if (S)
70  --S->getValue();
71  S = Other.S;
72  if (S)
73  ++S->getValue();
74  return *this;
75  }
76 
78  std::swap(S, Other.S);
79  }
80 
82  if (S)
83  --S->getValue();
84  S = nullptr;
85  std::swap(S, Other.S);
86  return *this;
87  }
88 
90  if (S)
91  --S->getValue();
92  }
93 
94  StringRef operator*() const { return S->first(); }
95 
96 private:
97 
99  : S(S) {
100  if (S)
101  ++S->getValue();
102  }
103 
104  SymbolStringPool::PoolMapEntry *S = nullptr;
105 };
106 
107 inline bool operator==(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS) {
108  return LHS.S == RHS.S;
109 }
110 
111 inline bool operator!=(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS) {
112  return !(LHS == RHS);
113 }
114 
115 inline bool operator<(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS) {
116  return LHS.S < RHS.S;
117 }
118 
120 #ifndef NDEBUG
122  assert(Pool.empty() && "Dangling references at pool destruction time");
123 #endif // NDEBUG
124 }
125 
127  std::lock_guard<std::mutex> Lock(PoolMutex);
129  bool Added;
130  std::tie(I, Added) = Pool.try_emplace(S, 0);
131  return SymbolStringPtr(&*I);
132 }
133 
135  std::lock_guard<std::mutex> Lock(PoolMutex);
136  for (auto I = Pool.begin(), E = Pool.end(); I != E;) {
137  auto Tmp = I++;
138  if (Tmp->second == 0)
139  Pool.erase(Tmp);
140  }
141 }
142 
143 inline bool SymbolStringPool::empty() const {
144  std::lock_guard<std::mutex> Lock(PoolMutex);
145  return Pool.empty();
146 }
147 
148 } // end namespace orc
149 
150 template <>
152 
154  return orc::SymbolStringPtr();
155  }
156 
158  return orc::SymbolStringPtr(&orc::SymbolStringPtr::Tombstone);
159  }
160 
161  static unsigned getHashValue(orc::SymbolStringPtr V) {
162  uintptr_t IV = reinterpret_cast<uintptr_t>(V.S);
163  return unsigned(IV) ^ unsigned(IV >> 9);
164  }
165 
166  static bool isEqual(const orc::SymbolStringPtr &LHS,
167  const orc::SymbolStringPtr &RHS) {
168  return LHS.S == RHS.S;
169  }
170 };
171 
172 } // end namespace llvm
173 
174 #endif // LLVM_EXECUTIONENGINE_ORC_SYMBOLSTRINGPOOL_H
void clearDeadEntries()
Remove from the pool any entries that are no longer referenced.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
String pool for symbol names used by the JIT.
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMap.h:126
static sys::Mutex Lock
static orc::SymbolStringPtr getEmptyKey()
const ValueTy & getValue() const
Definition: StringMap.h:141
bool operator!=(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS)
static unsigned getHashValue(orc::SymbolStringPtr V)
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:784
~SymbolStringPool()
Destroy a SymbolStringPool.
bool operator==(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS)
Pointer to a pooled string representing a symbol name.
SymbolStringPtr intern(StringRef S)
Create a symbol string pointer from the given string.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
SymbolStringPtr(SymbolStringPtr &&Other)
SymbolStringPtr & operator=(SymbolStringPtr &&Other)
StringRef operator*() const
static orc::SymbolStringPtr getTombstoneKey()
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:941
static bool isEqual(const orc::SymbolStringPtr &LHS, const orc::SymbolStringPtr &RHS)
SymbolStringPtr & operator=(const SymbolStringPtr &Other)
bool operator<(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS)
SymbolStringPtr(const SymbolStringPtr &Other)
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool empty() const
Returns true if the pool is empty.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49