LLVM 23.0.0git
EPCDynamicLibrarySearchGenerator.cpp
Go to the documentation of this file.
1//===---------------- EPCDynamicLibrarySearchGenerator.cpp ----------------===//
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
10
13#include "llvm/Support/Error.h"
14
15#define DEBUG_TYPE "orc"
16
17namespace llvm {
18namespace orc {
19
20Expected<std::unique_ptr<EPCDynamicLibrarySearchGenerator>>
22 ExecutionSession &ES, DylibManager &DylibMgr, const char *LibraryPath,
23 SymbolPredicate Allow, AddAbsoluteSymbolsFn AddAbsoluteSymbols) {
24 auto Handle = DylibMgr.loadDylib(LibraryPath);
25 if (!Handle)
26 return Handle.takeError();
27
28 return std::make_unique<EPCDynamicLibrarySearchGenerator>(
29 ES, DylibMgr, *Handle, std::move(Allow), std::move(AddAbsoluteSymbols));
30}
31
34 JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &Symbols) {
35
36 if (Symbols.empty())
37 return Error::success();
38
40 dbgs() << "EPCDynamicLibrarySearchGenerator trying to generate "
41 << Symbols << "\n";
42 });
43
44 // If there's no handle then resolve all requested symbols to null.
45 if (!H) {
46 assert(Allow && "No handle or filter?");
47 SymbolMap Nulls;
48 for (auto &[Name, LookupFlags] : Symbols) {
49 if (Allow(Name))
50 Nulls[Name] = {};
51 }
52 return addAbsolutes(JD, std::move(Nulls));
53 }
54
55 // Otherwise proceed with lookup in the remote.
56 SymbolLookupSet LookupSymbols;
57
58 for (auto &KV : Symbols) {
59 // Skip symbols that don't match the filter.
60 if (Allow && !Allow(KV.first))
61 continue;
62 LookupSymbols.add(KV.first, SymbolLookupFlags::WeaklyReferencedSymbol);
63 }
64
65 DylibMgr.lookupSymbolsAsync(
66 *H, LookupSymbols,
67 [this, &JD, LS = std::move(LS), LookupSymbols](auto Result) mutable {
68 if (!Result) {
70 dbgs() << "EPCDynamicLibrarySearchGenerator lookup failed due to "
71 "error";
72 });
73 return LS.continueLookup(Result.takeError());
74 }
75
76 assert(Result->size() == LookupSymbols.size() &&
77 "Result has incorrect number of elements");
78
79 auto SymsIt = Result->begin();
80 SymbolNameSet MissingSymbols;
81 SymbolMap NewSymbols;
82 for (auto &[Name, Flags] : LookupSymbols) {
83 const auto &Sym = *SymsIt++;
84 if (Sym && Sym->getAddress())
85 NewSymbols[Name] = *Sym;
86 else if (LLVM_UNLIKELY(!Sym &&
88 MissingSymbols.insert(Name);
89 }
90
92 dbgs() << "EPCDynamicLibrarySearchGenerator lookup returned "
93 << NewSymbols << "\n";
94 });
95
96 // If there were no resolved symbols bail out.
97 if (NewSymbols.empty())
98 return LS.continueLookup(Error::success());
99
100 if (LLVM_UNLIKELY(!MissingSymbols.empty()))
101 return LS.continueLookup(make_error<SymbolsNotFound>(
102 this->ES.getSymbolStringPool(), std::move(MissingSymbols)));
103
104 // Define resolved symbols.
105 Error Err = addAbsolutes(JD, std::move(NewSymbols));
106
107 LS.continueLookup(std::move(Err));
108 });
109
110 return Error::success();
111}
112
113Error EPCDynamicLibrarySearchGenerator::addAbsolutes(JITDylib &JD,
114 SymbolMap Symbols) {
115 return AddAbsoluteSymbols ? AddAbsoluteSymbols(JD, std::move(Symbols))
116 : JD.define(absoluteSymbols(std::move(Symbols)));
117}
118
119} // end namespace orc
120} // end namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:336
#define LLVM_DEBUG(...)
Definition Debug.h:119
bool empty() const
Definition DenseMap.h:109
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
friend class ExecutionSession
Definition Core.h:877
unique_function< bool(const SymbolStringPtr &)> SymbolPredicate
static Expected< std::unique_ptr< EPCDynamicLibrarySearchGenerator > > Load(ExecutionSession &ES, DylibManager &DylibMgr, const char *LibraryPath, SymbolPredicate Allow=SymbolPredicate(), AddAbsoluteSymbolsFn AddAbsoluteSymbols=nullptr)
Permanently loads the library at the given path and, on success, returns an EPCDynamicLibrarySearchGe...
unique_function< Error(JITDylib &, SymbolMap)> AddAbsoluteSymbolsFn
Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD, JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &Symbols) override
DefinitionGenerators should override this method to insert new definitions into the parent JITDylib.
Represents a JIT'd dynamic library.
Definition Core.h:919
Wraps state for a lookup-in-progress.
Definition Core.h:851
A set of symbols to look up, each associated with a SymbolLookupFlags value.
Definition Core.h:199
UnderlyingVector::size_type size() const
Definition Core.h:280
SymbolLookupSet & add(SymbolStringPtr Name, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Add an element to the set.
Definition Core.h:265
std::unique_ptr< AbsoluteSymbolsMaterializationUnit > absoluteSymbols(SymbolMap Symbols)
Create an AbsoluteSymbolsMaterializationUnit with the given symbols.
JITDylibLookupFlags
Lookup flags that apply to each dylib in the search order for a lookup.
Definition Core.h:151
DenseMap< SymbolStringPtr, ExecutorSymbolDef > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
LookupKind
Describes the kind of lookup being performed.
Definition Core.h:173
DenseSet< SymbolStringPtr > SymbolNameSet
A set of symbol names (represented by SymbolStringPtrs for.
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.
Definition Debug.cpp:209
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1916
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:874