LLVM: include/llvm/ExecutionEngine/Orc/TargetProcess/LibraryResolver.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14#ifndef LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_LIBRARYRESOLVER_H
15#define LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_LIBRARYRESOLVER_H
16
21
22#include
23#include <shared_mutex>
24
25namespace llvm {
26namespace orc {
27
28
29
30
31
32
34public:
36
38 public:
41
43 std::optional Filter = std::nullopt)
44 : FilePath(std::move(FilePath)), S(S), K(K), Filter(std::move(Filter)) {
45 }
46
49
50 std::string getFullPath() const { return FilePath; }
51
53 std::lock_guardstd::shared\_mutex Lock(Mtx);
54 if (Filter)
55 return;
56 Filter.emplace(std::move(F));
57 }
58
61 std::lock_guardstd::shared\_mutex Lock(Mtx);
62 if (Filter)
63 return;
64 Filter.emplace(FB.build(Symbols));
65 }
66
69 std::shared_lockstd::shared\_mutex Lock(Mtx);
70 return Filter->mayContain(Symbol);
71 }
72
74 std::shared_lockstd::shared\_mutex Lock(Mtx);
75 return Filter.has_value();
76 }
77
80
82
84 return FilePath == other.FilePath;
85 }
86
87 private:
88 std::string FilePath;
89 std::atomic S;
91 std::optional Filter;
92 mutable std::shared_mutex Mtx;
93 };
94
95
96
97
98
100 public:
104 public:
106 : it(it_), end(end_), S(S), K(K) {
107 advance();
108 }
109
111 return it != other.it;
112 }
113
114 const std::shared_ptr &operator*() const {
115 return it->second;
116 }
117
119 ++it;
120 advance();
121 return *this;
122 }
123
124 private:
125 void advance() {
126 for (; it != end; ++it)
127 if (it->second->getState() == S && it->second->getKind() == K)
128 break;
129 }
134 };
136 : mapBegin(begin), mapEnd(end), state(s), kind(k) {}
137
141
145
146 private:
151 };
152
153private:
155 mutable std::shared_mutex Mtx;
156
157public:
159
162
164 std::optional Filter = std::nullopt) {
165 std::unique_lockstd::shared\_mutex Lock(Mtx);
166 if (Libraries.count(Path) > 0)
167 return false;
168 Libraries.insert({std::move(Path),
170 Kind, std::move(Filter))});
171 return true;
172 }
173
175 std::shared_lockstd::shared\_mutex Lock(Mtx);
176 if (Libraries.count(Path) > 0)
177 return true;
178 return false;
179 }
180
182 std::unique_lockstd::shared\_mutex Lock(Mtx);
183 auto I = Libraries.find(Path);
184 if (I == Libraries.end())
185 return;
186 Libraries.erase(I);
187 }
188
190 std::unique_lockstd::shared\_mutex Lock(Mtx);
191 if (auto It = Libraries.find(Path); It != Libraries.end())
193 }
194
196 std::unique_lockstd::shared\_mutex Lock(Mtx);
197 if (auto It = Libraries.find(Path); It != Libraries.end())
199 }
200
202 std::shared_lockstd::shared\_mutex Lock(Mtx);
203 if (auto It = Libraries.find(Path); It != Libraries.end())
204 return It->second;
205 return nullptr;
206 }
207
209 std::shared_lockstd::shared\_mutex Lock(Mtx);
210 return FilteredView(Libraries.begin(), Libraries.end(), S, K);
211 }
212
215 std::vector<std::shared_ptr> &Outs,
217 std::shared_lockstd::shared\_mutex Lock(Mtx);
218 for (const auto &[_, Entry] : Libraries) {
219 const auto &Info = *Entry;
220 if (Info.getKind() != K || Info.getState() != S)
221 continue;
223 continue;
224 Outs.push_back(Entry);
225 }
226 }
227
229 std::unique_lockstd::shared\_mutex Lock(Mtx);
230 for (const auto &[_, entry] : Libraries) {
231 if (!visitor(*entry))
232 break;
233 }
234 }
235
237 std::shared_lockstd::shared\_mutex Lock(Mtx);
238 if (auto It = Libraries.find(Path.str()); It != Libraries.end())
240 return false;
241 }
242
244 std::shared_lockstd::shared\_mutex Lock(Mtx);
245 if (auto It = Libraries.find(Path.str()); It != Libraries.end())
247 return false;
248 }
249
251 std::unique_lockstd::shared\_mutex Lock(Mtx);
252 Libraries.clear();
253 }
254};
255
257
262
265
274};
275
292
301
302
303
304
305
306
309
310public:
312 public:
314
316
319 };
320
321
322
323
324
325
327 public:
328
333
334 private:
335 mutable std::shared_mutex Mtx;
337 std::atomic<size_t> ResolvedCount = 0;
338
339 public:
340 explicit SymbolQuery(const std::vectorstd::string &Symbols) {
341 for (const auto &s : Symbols) {
342 if (!Results.contains(s))
343 Results.insert({s, Result{s, ""}});
344 }
345 }
346
349 std::shared_lockstd::shared\_mutex Lock(Mtx);
350 for (const auto &[name, res] : Results) {
351 if (res.ResolvedLibPath.empty())
353 }
354 return Unresolved;
355 }
356
358 std::unique_lockstd::shared\_mutex Lock(Mtx);
359 auto It = Results.find(Sym);
360 if (It != Results.end() && It->second.ResolvedLibPath.empty()) {
361 It->second.ResolvedLibPath = LibPath;
362 ResolvedCount.fetch_add(1, std::memory_order_relaxed);
363 }
364 }
365
367 return ResolvedCount.load(std::memory_order_relaxed) == Results.size();
368 }
369
371 return ResolvedCount.load(std::memory_order_relaxed) < Results.size();
372 }
373
375 std::shared_lockstd::shared\_mutex Lock(Mtx);
376 auto It = Results.find(Sym);
377 if (It != Results.end() && !It->second.ResolvedLibPath.empty())
378 return StringRef(It->second.ResolvedLibPath);
379 return std::nullopt;
380 }
381
383 std::shared_lockstd::shared\_mutex Lock(Mtx);
384 auto It = Results.find(Sym.str());
385 return It != Results.end() && !It->second.ResolvedLibPath.empty();
386 }
387
389 std::shared_lockstd::shared\_mutex Lock(Mtx);
390 std::vector<const Result *> Out;
391 Out.reserve(Results.size());
392 for (const auto &[_, res] : Results)
393 Out.push_back(&res);
394 return Out;
395 }
396 };
397
402
404
408
410
413 std::shared_ptr existingCache = nullptr,
414 std::shared_ptr existingResolver = nullptr,
418
420 existingCache ? existingCache : std::make_shared();
421
422 S.PResolver = existingResolver ? existingResolver
423 : std::make_shared(S.Cache);
424
425 if (customShouldScan)
427
428 return S;
429 }
430 };
431
435
437
439 int i = 0;
440 LibMgr.forEachLibrary([&](const LibraryInfo &Lib) -> bool {
441 dbgs() << ++i << ". Library Path : " << Lib.getFullPath() << " -> \n\t\t:"
442 << " ({Type : ("
443 << (Lib.getKind() == PathType::User ? "User" : "System")
444 << ") }, { State : "
446 ? "Loaded"
447 : "Unloaded")
448 << "})\n";
449 return true;
450 });
451 }
452
456
457private:
458 bool scanLibrariesIfNeeded(PathType K, size_t BatchSize = 0);
461 bool
463 std::vectorstd::string *MatchedSymbols = nullptr);
464
466 std::vectorstd::string *AllSymbols,
468
469 std::shared_ptr LibPathCache;
470 std::shared_ptr LibPathResolver;
475 size_t scanBatchSize;
476};
477
481
482class LibraryResolutionDriver {
483public:
484 static std::unique_ptr
486
491 return LR->LibMgr.isLoaded(Path);
492 }
493
495 LR->LibMgr.clear();
496 LR->ScanHelper.resetToScan();
497 LR->LibPathCache->clear();
498 }
499
501 LR->scanLibrariesIfNeeded(PathType::User, BatchSize);
502 LR->scanLibrariesIfNeeded(PathType::System, BatchSize);
503 }
504
506 LR->scanLibrariesIfNeeded(PK, BatchSize);
507 }
508
509 void resolveSymbols(std::vectorstd::string Symbols,
512
514
515private:
516 LibraryResolutionDriver(std::unique_ptr L)
518
519 std::unique_ptr LR;
520};
521
522}
523}
524
525#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Analysis containing CSE Info
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
StringMapIterBase< std::shared_ptr< LibraryInfo >, true > const_iterator
StringRef - Represent a constant reference to a string, i.e.
std::string str() const
str - Get the contents as an std::string.
BloomFilter build(ArrayRef< StringRef > Symbols) const
Definition LibraryResolver.h:103
bool operator!=(const FilterIterator &other) const
Definition LibraryResolver.h:110
const std::shared_ptr< LibraryInfo > & operator*() const
Definition LibraryResolver.h:114
FilterIterator & operator++()
Definition LibraryResolver.h:118
FilterIterator(Iterator it_, Iterator end_, LibState S, PathType K)
Definition LibraryResolver.h:105
A read-only view of libraries filtered by state and kind.
Definition LibraryResolver.h:99
Map::const_iterator Iterator
Definition LibraryResolver.h:102
FilterIterator begin() const
Definition LibraryResolver.h:138
StringMap< std::shared_ptr< LibraryInfo > > Map
Definition LibraryResolver.h:101
FilteredView(Iterator begin, Iterator end, LibState s, PathType k)
Definition LibraryResolver.h:135
FilterIterator end() const
Definition LibraryResolver.h:142
Definition LibraryResolver.h:37
std::string getFullPath() const
Definition LibraryResolver.h:50
LibState getState() const
Definition LibraryResolver.h:78
bool mayContain(StringRef Symbol) const
Definition LibraryResolver.h:67
PathType getKind() const
Definition LibraryResolver.h:79
LibraryInfo(const LibraryInfo &)=delete
void ensureFilterBuilt(const BloomFilterBuilder &FB, ArrayRef< StringRef > Symbols)
Definition LibraryResolver.h:59
void setState(LibState s)
Definition LibraryResolver.h:81
StringRef getBasePath() const
Definition LibraryResolver.h:47
bool hasFilter() const
Definition LibraryResolver.h:73
StringRef getFileName() const
Definition LibraryResolver.h:48
LibraryInfo & operator=(const LibraryInfo &)=delete
void setFilter(BloomFilter F)
Definition LibraryResolver.h:52
bool operator==(const LibraryInfo &other) const
Definition LibraryResolver.h:83
LibraryInfo(std::string FilePath, LibState S, PathType K, std::optional< BloomFilter > Filter=std::nullopt)
Definition LibraryResolver.h:42
Manages library metadata and state for symbol resolution.
Definition LibraryResolver.h:33
void getLibraries(LibState S, PathType K, std::vector< std::shared_ptr< LibraryInfo > > &Outs, LibraryFilterFn Filter=nullptr) const
Definition LibraryResolver.h:214
LibState
Definition LibraryResolver.h:35
@ Loaded
Definition LibraryResolver.h:35
@ Unloaded
Definition LibraryResolver.h:35
@ Queried
Definition LibraryResolver.h:35
std::shared_ptr< LibraryInfo > getLibrary(StringRef Path)
Definition LibraryResolver.h:201
bool hasLibrary(StringRef Path) const
Definition LibraryResolver.h:174
bool isLoaded(StringRef Path) const
Definition LibraryResolver.h:236
void markLoaded(StringRef Path)
Definition LibraryResolver.h:189
FilteredView getView(LibState S, PathType K) const
Definition LibraryResolver.h:208
void removeLibrary(StringRef Path)
Definition LibraryResolver.h:181
void forEachLibrary(const LibraryVisitor &visitor) const
Definition LibraryResolver.h:228
void clear()
Definition LibraryResolver.h:250
std::function< bool(const LibraryInfo &)> LibraryFilterFn
Definition LibraryResolver.h:213
std::function< bool(const LibraryInfo &)> LibraryVisitor
Definition LibraryResolver.h:158
bool addLibrary(std::string Path, PathType Kind, std::optional< BloomFilter > Filter=std::nullopt)
Definition LibraryResolver.h:163
bool isQueried(StringRef Path) const
Definition LibraryResolver.h:243
~LibraryManager()=default
void markQueried(StringRef Path)
Definition LibraryResolver.h:195
void resolveSymbols(std::vector< std::string > Symbols, LibraryResolver::OnSearchComplete OnCompletion, const SearchConfig &Config=SearchConfig())
void addScanPath(const std::string &Path, PathType Kind)
~LibraryResolutionDriver()=default
void scan(PathType PK, size_t BatchSize=0)
Definition LibraryResolver.h:505
void scanAll(size_t BatchSize=0)
Definition LibraryResolver.h:500
bool isLibraryLoaded(StringRef Path) const
Definition LibraryResolver.h:490
bool markLibraryLoaded(StringRef Path)
static std::unique_ptr< LibraryResolutionDriver > create(const LibraryResolver::Setup &S)
void resetAll()
Definition LibraryResolver.h:494
bool markLibraryUnLoaded(StringRef Path)
Definition LibraryResolver.h:311
EnumerateResult
Definition LibraryResolver.h:313
@ Stop
Definition LibraryResolver.h:313
@ Error
Definition LibraryResolver.h:313
@ Continue
Definition LibraryResolver.h:313
std::function< EnumerateResult(StringRef Sym)> OnEachSymbolFn
Definition LibraryResolver.h:315
static bool enumerateSymbols(StringRef Path, OnEachSymbolFn OnEach, const SymbolEnumeratorOptions &Opts)
Tracks a set of symbols and the libraries where they are resolved.
Definition LibraryResolver.h:326
SymbolQuery(const std::vector< std::string > &Symbols)
Definition LibraryResolver.h:340
std::optional< StringRef > getResolvedLib(StringRef Sym) const
Definition LibraryResolver.h:374
bool hasUnresolved() const
Definition LibraryResolver.h:370
bool isResolved(StringRef Sym) const
Definition LibraryResolver.h:382
SmallVector< StringRef > getUnresolvedSymbols() const
Definition LibraryResolver.h:347
bool allResolved() const
Definition LibraryResolver.h:366
std::vector< const Result * > getAllResults() const
Definition LibraryResolver.h:388
void resolve(StringRef Sym, const std::string &LibPath)
Definition LibraryResolver.h:357
~LibraryResolver()=default
unique_function< void(SymbolQuery &)> OnSearchComplete
Definition LibraryResolver.h:436
void dump()
Definition LibraryResolver.h:438
friend class LibraryResolutionDriver
Definition LibraryResolver.h:308
void searchSymbolsInLibraries(std::vector< std::string > &SymList, OnSearchComplete OnComplete, const SearchConfig &Config=SearchConfig())
Scans and tracks libraries for symbol resolution.
std::function< bool(StringRef)> ShouldScanFn
unique_function is a type-erasing functor similar to std::function.
LibraryManager::LibraryInfo LibraryInfo
Definition LibraryResolver.h:256
SymbolEnumerator::EnumerateResult EnumerateResult
Definition LibraryResolver.h:480
LibraryResolver::SymbolEnumerator SymbolEnumerator
Definition LibraryResolver.h:478
LibraryResolver::SymbolQuery SymbolQuery
Definition LibraryResolver.h:479
LLVM_ABI StringRef parent_path(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get parent path.
LLVM_ABI StringRef filename(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get filename.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Implement std::hash so that hash_code can be used in STL containers.
Definition LibraryResolver.h:398
size_t ScanBatchSize
Definition LibraryResolver.h:403
static Setup create(std::vector< std::string > BasePaths, std::shared_ptr< LibraryPathCache > existingCache=nullptr, std::shared_ptr< PathResolver > existingResolver=nullptr, LibraryScanner::ShouldScanFn customShouldScan=nullptr)
Definition LibraryResolver.h:412
BloomFilterBuilder FilterBuilder
Definition LibraryResolver.h:409
LibraryScanner::ShouldScanFn ShouldScanCall
Definition LibraryResolver.h:405
std::vector< std::string > BasePaths
Definition LibraryResolver.h:399
std::shared_ptr< PathResolver > PResolver
Definition LibraryResolver.h:401
std::shared_ptr< LibraryPathCache > Cache
Definition LibraryResolver.h:400
Holds the result for a single symbol.
Definition LibraryResolver.h:329
std::string ResolvedLibPath
Definition LibraryResolver.h:331
std::string Name
Definition LibraryResolver.h:330
Definition LibraryResolver.h:293
SearchPolicy Policy
Definition LibraryResolver.h:294
SymbolEnumeratorOptions Options
Definition LibraryResolver.h:295
SearchConfig()
Definition LibraryResolver.h:297
Definition LibraryResolver.h:258
LibraryManager::LibState State
Definition LibraryResolver.h:259
PathType Type
Definition LibraryResolver.h:260
Definition LibraryResolver.h:263
static SearchPolicy defaultPlan()
Definition LibraryResolver.h:266
std::vector< SearchPlanEntry > Plan
Definition LibraryResolver.h:264
Definition LibraryResolver.h:276
uint32_t FilterFlags
Definition LibraryResolver.h:290
Filter
Definition LibraryResolver.h:277
@ IgnoreHidden
Definition LibraryResolver.h:282
@ IgnoreNonGlobal
Definition LibraryResolver.h:283
@ None
Definition LibraryResolver.h:278
@ IgnoreWeak
Definition LibraryResolver.h:280
@ IgnoreUndefined
Definition LibraryResolver.h:279
@ IgnoreIndirect
Definition LibraryResolver.h:281
static SymbolEnumeratorOptions defaultOptions()
Definition LibraryResolver.h:286