LLVM  7.0.0
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/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 bool operator==(const SymbolStringPtr &LHS,
53  const SymbolStringPtr &RHS);
54  friend bool operator<(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS);
55 
56 public:
57  SymbolStringPtr() = default;
59  : S(Other.S) {
60  if (S)
61  ++S->getValue();
62  }
63 
65  if (S)
66  --S->getValue();
67  S = Other.S;
68  if (S)
69  ++S->getValue();
70  return *this;
71  }
72 
74  std::swap(S, Other.S);
75  }
76 
78  if (S)
79  --S->getValue();
80  S = nullptr;
81  std::swap(S, Other.S);
82  return *this;
83  }
84 
86  if (S)
87  --S->getValue();
88  }
89 
90  StringRef operator*() const { return S->first(); }
91 
92 private:
93 
95  : S(S) {
96  if (S)
97  ++S->getValue();
98  }
99 
100  SymbolStringPool::PoolMapEntry *S = nullptr;
101 };
102 
103 inline bool operator==(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS) {
104  return LHS.S == RHS.S;
105 }
106 
107 inline bool operator!=(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS) {
108  return !(LHS == RHS);
109 }
110 
111 inline bool operator<(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS) {
112  return LHS.S < RHS.S;
113 }
114 
116 #ifndef NDEBUG
118  assert(Pool.empty() && "Dangling references at pool destruction time");
119 #endif // NDEBUG
120 }
121 
123  std::lock_guard<std::mutex> Lock(PoolMutex);
125  bool Added;
126  std::tie(I, Added) = Pool.try_emplace(S, 0);
127  return SymbolStringPtr(&*I);
128 }
129 
131  std::lock_guard<std::mutex> Lock(PoolMutex);
132  for (auto I = Pool.begin(), E = Pool.end(); I != E;) {
133  auto Tmp = I++;
134  if (Tmp->second == 0)
135  Pool.erase(Tmp);
136  }
137 }
138 
139 inline bool SymbolStringPool::empty() const {
140  std::lock_guard<std::mutex> Lock(PoolMutex);
141  return Pool.empty();
142 }
143 
144 } // end namespace orc
145 } // end namespace llvm
146 
147 #endif // LLVM_EXECUTIONENGINE_ORC_SYMBOLSTRINGPOOL_H
void clearDeadEntries()
Remove from the pool any entries that are no longer referenced.
Compute iterated dominance frontiers using a linear time algorithm.
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
const ValueTy & getValue() const
Definition: StringMap.h:141
bool operator!=(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS)
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:773
~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
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:924
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