LLVM 20.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
11#include "llvm/Support/Error.h"
12
13#define DEBUG_TYPE "orc"
14
15namespace llvm {
16namespace orc {
17
18Expected<std::unique_ptr<EPCDynamicLibrarySearchGenerator>>
20 ExecutionSession &ES, const char *LibraryPath, SymbolPredicate Allow,
21 AddAbsoluteSymbolsFn AddAbsoluteSymbols) {
22 auto Handle = ES.getExecutorProcessControl().loadDylib(LibraryPath);
23 if (!Handle)
24 return Handle.takeError();
25
26 return std::make_unique<EPCDynamicLibrarySearchGenerator>(
27 ES, *Handle, std::move(Allow), std::move(AddAbsoluteSymbols));
28}
29
32 JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &Symbols) {
33
34 if (Symbols.empty())
35 return Error::success();
36
38 dbgs() << "EPCDynamicLibrarySearchGenerator trying to generate "
39 << Symbols << "\n";
40 });
41
42 SymbolLookupSet LookupSymbols;
43
44 for (auto &KV : Symbols) {
45 // Skip symbols that don't match the filter.
46 if (Allow && !Allow(KV.first))
47 continue;
48 LookupSymbols.add(KV.first, SymbolLookupFlags::WeaklyReferencedSymbol);
49 }
50
51 ExecutorProcessControl::LookupRequest Request(H, LookupSymbols);
52 // Copy-capture LookupSymbols, since LookupRequest keeps a reference.
53 EPC.lookupSymbolsAsync(Request, [this, &JD, LS = std::move(LS),
54 LookupSymbols](auto Result) mutable {
55 if (!Result) {
57 dbgs() << "EPCDynamicLibrarySearchGenerator lookup failed due to error";
58 });
59 return LS.continueLookup(Result.takeError());
60 }
61
62 assert(Result->size() == 1 && "Results for more than one library returned");
63 assert(Result->front().size() == LookupSymbols.size() &&
64 "Result has incorrect number of elements");
65
66 SymbolMap NewSymbols;
67 auto ResultI = Result->front().begin();
68 for (auto &KV : LookupSymbols) {
69 if (ResultI->getAddress())
70 NewSymbols[KV.first] = *ResultI;
71 ++ResultI;
72 }
73
75 dbgs() << "EPCDynamicLibrarySearchGenerator lookup returned "
76 << NewSymbols << "\n";
77 });
78
79 // If there were no resolved symbols bail out.
80 if (NewSymbols.empty())
81 return LS.continueLookup(Error::success());
82
83 // Define resolved symbols.
84 Error Err = AddAbsoluteSymbols
85 ? AddAbsoluteSymbols(JD, std::move(NewSymbols))
86 : JD.define(absoluteSymbols(std::move(NewSymbols)));
87
88 LS.continueLookup(std::move(Err));
89 });
90
91 return Error::success();
92}
93
94} // end namespace orc
95} // end namespace llvm
#define LLVM_DEBUG(X)
Definition: Debug.h:101
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool empty() const
Definition: DenseMap.h:98
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
static Expected< std::unique_ptr< EPCDynamicLibrarySearchGenerator > > Load(ExecutionSession &ES, const char *LibraryPath, SymbolPredicate Allow=SymbolPredicate(), AddAbsoluteSymbolsFn AddAbsoluteSymbols=nullptr)
Permanently loads the library at the given path and, on success, returns a DynamicLibrarySearchGenera...
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.
An ExecutionSession represents a running JIT program.
Definition: Core.h:1431
ExecutorProcessControl & getExecutorProcessControl()
Get the ExecutorProcessControl object associated with this ExecutionSession.
Definition: Core.h:1471
virtual Expected< tpctypes::DylibHandle > loadDylib(const char *DylibPath)=0
Load the dynamic library at the given path and return a handle to it.
virtual void lookupSymbolsAsync(ArrayRef< LookupRequest > Request, SymbolLookupCompleteFn F)=0
Search for symbols in the target process.
Represents a JIT'd dynamic library.
Definition: Core.h:989
Error define(std::unique_ptr< MaterializationUnitType > &&MU, ResourceTrackerSP RT=nullptr)
Define all symbols provided by the materialization unit to be part of this JITDylib.
Definition: Core.h:1910
Wraps state for a lookup-in-progress.
Definition: Core.h:921
A set of symbols to look up, each associated with a SymbolLookupFlags value.
Definition: Core.h:183
SymbolLookupSet & add(SymbolStringPtr Name, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Add an element to the set.
Definition: Core.h:244
std::unique_ptr< AbsoluteSymbolsMaterializationUnit > absoluteSymbols(SymbolMap Symbols)
Create an AbsoluteSymbolsMaterializationUnit with the given symbols.
Definition: Core.h:791
JITDylibLookupFlags
Lookup flags that apply to each dylib in the search order for a lookup.
Definition: Core.h:135
LookupKind
Describes the kind of lookup being performed.
Definition: Core.h:157
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
A pair of a dylib and a set of symbols to be looked up.