LLVM  9.0.0svn
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 SymbolStringPool;
52  friend struct DenseMapInfo<SymbolStringPtr>;
53  friend bool operator==(const SymbolStringPtr &LHS,
54  const SymbolStringPtr &RHS);
55  friend bool operator<(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS);
56 
57  static SymbolStringPool::PoolMapEntry Tombstone;
58 
59 public:
60  SymbolStringPtr() = default;
62  : S(Other.S) {
63  if (S)
64  ++S->getValue();
65  }
66 
68  if (S)
69  --S->getValue();
70  S = Other.S;
71  if (S)
72  ++S->getValue();
73  return *this;
74  }
75 
77  std::swap(S, Other.S);
78  }
79 
81  if (S)
82  --S->getValue();
83  S = nullptr;
84  std::swap(S, Other.S);
85  return *this;
86  }
87 
89  if (S)
90  --S->getValue();
91  }
92 
93  StringRef operator*() const { return S->first(); }
94 
95 private:
96 
98  : S(S) {
99  if (S)
100  ++S->getValue();
101  }
102 
103  SymbolStringPool::PoolMapEntry *S = nullptr;
104 };
105 
106 inline bool operator==(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS) {
107  return LHS.S == RHS.S;
108 }
109 
110 inline bool operator!=(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS) {
111  return !(LHS == RHS);
112 }
113 
114 inline bool operator<(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS) {
115  return LHS.S < RHS.S;
116 }
117 
119 #ifndef NDEBUG
121  assert(Pool.empty() && "Dangling references at pool destruction time");
122 #endif // NDEBUG
123 }
124 
126  std::lock_guard<std::mutex> Lock(PoolMutex);
128  bool Added;
129  std::tie(I, Added) = Pool.try_emplace(S, 0);
130  return SymbolStringPtr(&*I);
131 }
132 
134  std::lock_guard<std::mutex> Lock(PoolMutex);
135  for (auto I = Pool.begin(), E = Pool.end(); I != E;) {
136  auto Tmp = I++;
137  if (Tmp->second == 0)
138  Pool.erase(Tmp);
139  }
140 }
141 
142 inline bool SymbolStringPool::empty() const {
143  std::lock_guard<std::mutex> Lock(PoolMutex);
144  return Pool.empty();
145 }
146 
147 } // end namespace orc
148 
149 template <>
151 
153  return orc::SymbolStringPtr();
154  }
155 
157  return orc::SymbolStringPtr(&orc::SymbolStringPtr::Tombstone);
158  }
159 
160  static unsigned getHashValue(orc::SymbolStringPtr V) {
161  uintptr_t IV = reinterpret_cast<uintptr_t>(V.S);
162  return unsigned(IV) ^ unsigned(IV >> 9);
163  }
164 
165  static bool isEqual(const orc::SymbolStringPtr &LHS,
166  const orc::SymbolStringPtr &RHS) {
167  return LHS.S == RHS.S;
168  }
169 };
170 
171 } // end namespace llvm
172 
173 #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:23
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:125
static sys::Mutex Lock
static orc::SymbolStringPtr getEmptyKey()
const ValueTy & getValue() const
Definition: StringMap.h:140
bool operator!=(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS)
static unsigned getHashValue(orc::SymbolStringPtr V)
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:849
~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:940
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:48