Skip to content

Commit db21bd4

Browse files
committed
[ORC] Move EPC load-dylib and lookup operations into their own class.
This keeps common operations together, and should make it easier to write re-usable dylib managers in the future (e.g. a DylibManager that uses the EPC's remote-execution APIs to implement load and lookup).
1 parent 8bbd079 commit db21bd4

File tree

9 files changed

+54
-84
lines changed

9 files changed

+54
-84
lines changed

llvm/include/llvm/ExecutionEngine/Orc/ExecutorProcessControl.h

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "llvm/ADT/StringRef.h"
1717
#include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
18+
#include "llvm/ExecutionEngine/Orc/DylibManager.h"
1819
#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
1920
#include "llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h"
2021
#include "llvm/ExecutionEngine/Orc/Shared/WrapperFunctionUtils.h"
@@ -32,7 +33,6 @@ namespace llvm {
3233
namespace orc {
3334

3435
class ExecutionSession;
35-
class SymbolLookupSet;
3636

3737
/// ExecutorProcessControl supports interaction with a JIT target process.
3838
class ExecutorProcessControl {
@@ -172,14 +172,6 @@ class ExecutorProcessControl {
172172
}
173173
};
174174

175-
/// A pair of a dylib and a set of symbols to be looked up.
176-
struct LookupRequest {
177-
LookupRequest(tpctypes::DylibHandle Handle, const SymbolLookupSet &Symbols)
178-
: Handle(Handle), Symbols(Symbols) {}
179-
tpctypes::DylibHandle Handle;
180-
const SymbolLookupSet &Symbols;
181-
};
182-
183175
/// Contains the address of the dispatch function and context that the ORC
184176
/// runtime can use to call functions in the JIT.
185177
struct JITDispatchInfo {
@@ -229,6 +221,12 @@ class ExecutorProcessControl {
229221
return *MemMgr;
230222
}
231223

224+
/// Return the DylibManager for the target process.
225+
DylibManager &getDylibMgr() const {
226+
assert(DylibMgr && "No DylibMgr object set");
227+
return *DylibMgr;
228+
}
229+
232230
/// Returns the bootstrap map.
233231
const StringMap<std::vector<char>> &getBootstrapMap() const {
234232
return BootstrapMap;
@@ -277,38 +275,6 @@ class ExecutorProcessControl {
277275
return Error::success();
278276
}
279277

280-
/// Load the dynamic library at the given path and return a handle to it.
281-
/// If LibraryPath is null this function will return the global handle for
282-
/// the target process.
283-
virtual Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) = 0;
284-
285-
/// Search for symbols in the target process.
286-
///
287-
/// The result of the lookup is a 2-dimensional array of target addresses
288-
/// that correspond to the lookup order. If a required symbol is not
289-
/// found then this method will return an error. If a weakly referenced
290-
/// symbol is not found then it be assigned a '0' value.
291-
Expected<std::vector<tpctypes::LookupResult>>
292-
lookupSymbols(ArrayRef<LookupRequest> Request) {
293-
std::promise<MSVCPExpected<std::vector<tpctypes::LookupResult>>> RP;
294-
auto RF = RP.get_future();
295-
lookupSymbolsAsync(Request,
296-
[&RP](auto Result) { RP.set_value(std::move(Result)); });
297-
return RF.get();
298-
}
299-
300-
using SymbolLookupCompleteFn =
301-
unique_function<void(Expected<std::vector<tpctypes::LookupResult>>)>;
302-
303-
/// Search for symbols in the target process.
304-
///
305-
/// The result of the lookup is a 2-dimensional array of target addresses
306-
/// that correspond to the lookup order. If a required symbol is not
307-
/// found then this method will return an error. If a weakly referenced
308-
/// symbol is not found then it be assigned a '0' value.
309-
virtual void lookupSymbolsAsync(ArrayRef<LookupRequest> Request,
310-
SymbolLookupCompleteFn F) = 0;
311-
312278
/// Run function with a main-like signature.
313279
virtual Expected<int32_t> runAsMain(ExecutorAddr MainFnAddr,
314280
ArrayRef<std::string> Args) = 0;
@@ -426,6 +392,7 @@ class ExecutorProcessControl {
426392
JITDispatchInfo JDI;
427393
MemoryAccess *MemAccess = nullptr;
428394
jitlink::JITLinkMemoryManager *MemMgr = nullptr;
395+
DylibManager *DylibMgr = nullptr;
429396
StringMap<std::vector<char>> BootstrapMap;
430397
StringMap<ExecutorAddr> BootstrapSymbols;
431398
};
@@ -474,15 +441,6 @@ class UnsupportedExecutorProcessControl : public ExecutorProcessControl,
474441
this->MemAccess = this;
475442
}
476443

477-
Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override {
478-
llvm_unreachable("Unsupported");
479-
}
480-
481-
void lookupSymbolsAsync(ArrayRef<LookupRequest> Request,
482-
SymbolLookupCompleteFn F) override {
483-
llvm_unreachable("Unsupported");
484-
}
485-
486444
Expected<int32_t> runAsMain(ExecutorAddr MainFnAddr,
487445
ArrayRef<std::string> Args) override {
488446
llvm_unreachable("Unsupported");
@@ -507,7 +465,8 @@ class UnsupportedExecutorProcessControl : public ExecutorProcessControl,
507465

508466
/// A ExecutorProcessControl implementation targeting the current process.
509467
class SelfExecutorProcessControl : public ExecutorProcessControl,
510-
private InProcessMemoryAccess {
468+
private InProcessMemoryAccess,
469+
private DylibManager {
511470
public:
512471
SelfExecutorProcessControl(
513472
std::shared_ptr<SymbolStringPool> SSP, std::unique_ptr<TaskDispatcher> D,
@@ -524,11 +483,6 @@ class SelfExecutorProcessControl : public ExecutorProcessControl,
524483
std::unique_ptr<TaskDispatcher> D = nullptr,
525484
std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr = nullptr);
526485

527-
Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override;
528-
529-
void lookupSymbolsAsync(ArrayRef<LookupRequest> Request,
530-
SymbolLookupCompleteFn F) override;
531-
532486
Expected<int32_t> runAsMain(ExecutorAddr MainFnAddr,
533487
ArrayRef<std::string> Args) override;
534488

@@ -547,6 +501,11 @@ class SelfExecutorProcessControl : public ExecutorProcessControl,
547501
jitDispatchViaWrapperFunctionManager(void *Ctx, const void *FnTag,
548502
const char *Data, size_t Size);
549503

504+
Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override;
505+
506+
void lookupSymbolsAsync(ArrayRef<LookupRequest> Request,
507+
SymbolLookupCompleteFn F) override;
508+
550509
std::unique_ptr<jitlink::JITLinkMemoryManager> OwnedMemMgr;
551510
char GlobalManglingPrefix = 0;
552511
};

llvm/include/llvm/ExecutionEngine/Orc/SimpleRemoteEPC.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ namespace llvm {
2929
namespace orc {
3030

3131
class SimpleRemoteEPC : public ExecutorProcessControl,
32-
public SimpleRemoteEPCTransportClient {
32+
public SimpleRemoteEPCTransportClient,
33+
private DylibManager {
3334
public:
3435
/// A setup object containing callbacks to construct a memory manager and
3536
/// memory access object. Both are optional. If not specified,
@@ -69,11 +70,6 @@ class SimpleRemoteEPC : public ExecutorProcessControl,
6970
SimpleRemoteEPC &operator=(SimpleRemoteEPC &&) = delete;
7071
~SimpleRemoteEPC();
7172

72-
Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override;
73-
74-
void lookupSymbolsAsync(ArrayRef<LookupRequest> Request,
75-
SymbolLookupCompleteFn F) override;
76-
7773
Expected<int32_t> runAsMain(ExecutorAddr MainFnAddr,
7874
ArrayRef<std::string> Args) override;
7975

@@ -96,7 +92,9 @@ class SimpleRemoteEPC : public ExecutorProcessControl,
9692
private:
9793
SimpleRemoteEPC(std::shared_ptr<SymbolStringPool> SSP,
9894
std::unique_ptr<TaskDispatcher> D)
99-
: ExecutorProcessControl(std::move(SSP), std::move(D)) {}
95+
: ExecutorProcessControl(std::move(SSP), std::move(D)) {
96+
this->DylibMgr = this;
97+
}
10098

10199
static Expected<std::unique_ptr<jitlink::JITLinkMemoryManager>>
102100
createDefaultMemoryManager(SimpleRemoteEPC &SREPC);
@@ -119,6 +117,11 @@ class SimpleRemoteEPC : public ExecutorProcessControl,
119117
uint64_t getNextSeqNo() { return NextSeqNo++; }
120118
void releaseSeqNo(uint64_t SeqNo) {}
121119

120+
Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override;
121+
122+
void lookupSymbolsAsync(ArrayRef<LookupRequest> Request,
123+
SymbolLookupCompleteFn F) override;
124+
122125
using PendingCallWrapperResultsMap =
123126
DenseMap<uint64_t, IncomingWFRHandler>;
124127

@@ -131,7 +134,7 @@ class SimpleRemoteEPC : public ExecutorProcessControl,
131134
std::unique_ptr<jitlink::JITLinkMemoryManager> OwnedMemMgr;
132135
std::unique_ptr<MemoryAccess> OwnedMemAccess;
133136

134-
std::unique_ptr<EPCGenericDylibManager> DylibMgr;
137+
std::unique_ptr<EPCGenericDylibManager> EPCDylibMgr;
135138
ExecutorAddr RunAsMainAddr;
136139
ExecutorAddr RunAsVoidFunctionAddr;
137140
ExecutorAddr RunAsIntFunctionAddr;

llvm/lib/ExecutionEngine/Orc/EPCDebugObjectRegistrar.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Expected<std::unique_ptr<EPCDebugObjectRegistrar>> createJITLoaderGDBRegistrar(
2222
auto &EPC = ES.getExecutorProcessControl();
2323

2424
if (!RegistrationFunctionDylib) {
25-
if (auto D = EPC.loadDylib(nullptr))
25+
if (auto D = EPC.getDylibMgr().loadDylib(nullptr))
2626
RegistrationFunctionDylib = *D;
2727
else
2828
return D.takeError();
@@ -36,8 +36,8 @@ Expected<std::unique_ptr<EPCDebugObjectRegistrar>> createJITLoaderGDBRegistrar(
3636
SymbolLookupSet RegistrationSymbols;
3737
RegistrationSymbols.add(RegisterFn);
3838

39-
auto Result =
40-
EPC.lookupSymbols({{*RegistrationFunctionDylib, RegistrationSymbols}});
39+
auto Result = EPC.getDylibMgr().lookupSymbols(
40+
{{*RegistrationFunctionDylib, RegistrationSymbols}});
4141
if (!Result)
4242
return Result.takeError();
4343

llvm/lib/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ Expected<std::unique_ptr<EPCDynamicLibrarySearchGenerator>>
1919
EPCDynamicLibrarySearchGenerator::Load(
2020
ExecutionSession &ES, const char *LibraryPath, SymbolPredicate Allow,
2121
AddAbsoluteSymbolsFn AddAbsoluteSymbols) {
22-
auto Handle = ES.getExecutorProcessControl().loadDylib(LibraryPath);
22+
auto Handle =
23+
ES.getExecutorProcessControl().getDylibMgr().loadDylib(LibraryPath);
2324
if (!Handle)
2425
return Handle.takeError();
2526

@@ -48,10 +49,11 @@ Error EPCDynamicLibrarySearchGenerator::tryToGenerate(
4849
LookupSymbols.add(KV.first, SymbolLookupFlags::WeaklyReferencedSymbol);
4950
}
5051

51-
ExecutorProcessControl::LookupRequest Request(H, LookupSymbols);
52+
DylibManager::LookupRequest Request(H, LookupSymbols);
5253
// Copy-capture LookupSymbols, since LookupRequest keeps a reference.
53-
EPC.lookupSymbolsAsync(Request, [this, &JD, LS = std::move(LS),
54-
LookupSymbols](auto Result) mutable {
54+
EPC.getDylibMgr().lookupSymbolsAsync(Request, [this, &JD, LS = std::move(LS),
55+
LookupSymbols](
56+
auto Result) mutable {
5557
if (!Result) {
5658
LLVM_DEBUG({
5759
dbgs() << "EPCDynamicLibrarySearchGenerator lookup failed due to error";

llvm/lib/ExecutionEngine/Orc/EPCGenericDylibManager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ class TrivialSPSSequenceSerialization<SPSRemoteSymbolLookupSetElement,
4141

4242
template <>
4343
class SPSSerializationTraits<SPSRemoteSymbolLookup,
44-
ExecutorProcessControl::LookupRequest> {
44+
DylibManager::LookupRequest> {
4545
using MemberSerialization =
4646
SPSArgList<SPSExecutorAddr, SPSRemoteSymbolLookupSet>;
4747

4848
public:
49-
static size_t size(const ExecutorProcessControl::LookupRequest &LR) {
49+
static size_t size(const DylibManager::LookupRequest &LR) {
5050
return MemberSerialization::size(ExecutorAddr(LR.Handle), LR.Symbols);
5151
}
5252

5353
static bool serialize(SPSOutputBuffer &OB,
54-
const ExecutorProcessControl::LookupRequest &LR) {
54+
const DylibManager::LookupRequest &LR) {
5555
return MemberSerialization::serialize(OB, ExecutorAddr(LR.Handle),
5656
LR.Symbols);
5757
}

llvm/lib/ExecutionEngine/Orc/ExecutorProcessControl.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
namespace llvm {
2222
namespace orc {
2323

24+
DylibManager::~DylibManager() = default;
25+
2426
ExecutorProcessControl::MemoryAccess::~MemoryAccess() = default;
2527

2628
ExecutorProcessControl::~ExecutorProcessControl() = default;
@@ -41,6 +43,7 @@ SelfExecutorProcessControl::SelfExecutorProcessControl(
4143
this->PageSize = PageSize;
4244
this->MemMgr = OwnedMemMgr.get();
4345
this->MemAccess = this;
46+
this->DylibMgr = this;
4447
this->JDI = {ExecutorAddr::fromPtr(jitDispatchViaWrapperFunctionManager),
4548
ExecutorAddr::fromPtr(this)};
4649
if (this->TargetTriple.isOSBinFormatMachO())
@@ -86,7 +89,7 @@ SelfExecutorProcessControl::loadDylib(const char *DylibPath) {
8689

8790
void SelfExecutorProcessControl::lookupSymbolsAsync(
8891
ArrayRef<LookupRequest> Request,
89-
ExecutorProcessControl::SymbolLookupCompleteFn Complete) {
92+
DylibManager::SymbolLookupCompleteFn Complete) {
9093
std::vector<tpctypes::LookupResult> R;
9194

9295
for (auto &Elem : Request) {

llvm/lib/ExecutionEngine/Orc/LookupAndRecordAddrs.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ Error lookupAndRecordAddrs(
6060
for (auto &KV : Pairs)
6161
Symbols.add(KV.first, LookupFlags);
6262

63-
ExecutorProcessControl::LookupRequest LR(H, Symbols);
64-
auto Result = EPC.lookupSymbols(LR);
63+
DylibManager::LookupRequest LR(H, Symbols);
64+
auto Result = EPC.getDylibMgr().lookupSymbols(LR);
6565
if (!Result)
6666
return Result.takeError();
6767

llvm/lib/ExecutionEngine/Orc/SimpleRemoteEPC.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ SimpleRemoteEPC::~SimpleRemoteEPC() {
2626

2727
Expected<tpctypes::DylibHandle>
2828
SimpleRemoteEPC::loadDylib(const char *DylibPath) {
29-
return DylibMgr->open(DylibPath, 0);
29+
return EPCDylibMgr->open(DylibPath, 0);
3030
}
3131

3232
/// Async helper to chain together calls to DylibMgr::lookupAsync to fulfill all
3333
/// all the requests.
3434
/// FIXME: The dylib manager should support multiple LookupRequests natively.
3535
static void
3636
lookupSymbolsAsyncHelper(EPCGenericDylibManager &DylibMgr,
37-
ArrayRef<SimpleRemoteEPC::LookupRequest> Request,
37+
ArrayRef<DylibManager::LookupRequest> Request,
3838
std::vector<tpctypes::LookupResult> Result,
39-
SimpleRemoteEPC::SymbolLookupCompleteFn Complete) {
39+
DylibManager::SymbolLookupCompleteFn Complete) {
4040
if (Request.empty())
4141
return Complete(std::move(Result));
4242

@@ -59,7 +59,7 @@ lookupSymbolsAsyncHelper(EPCGenericDylibManager &DylibMgr,
5959

6060
void SimpleRemoteEPC::lookupSymbolsAsync(ArrayRef<LookupRequest> Request,
6161
SymbolLookupCompleteFn Complete) {
62-
lookupSymbolsAsyncHelper(*DylibMgr, Request, {}, std::move(Complete));
62+
lookupSymbolsAsyncHelper(*EPCDylibMgr, Request, {}, std::move(Complete));
6363
}
6464

6565
Expected<int32_t> SimpleRemoteEPC::runAsMain(ExecutorAddr MainFnAddr,
@@ -357,7 +357,7 @@ Error SimpleRemoteEPC::setup(Setup S) {
357357

358358
if (auto DM =
359359
EPCGenericDylibManager::CreateWithDefaultBootstrapSymbols(*this))
360-
DylibMgr = std::make_unique<EPCGenericDylibManager>(std::move(*DM));
360+
EPCDylibMgr = std::make_unique<EPCGenericDylibManager>(std::move(*DM));
361361
else
362362
return DM.takeError();
363363

llvm/unittests/ExecutionEngine/Orc/ObjectLinkingLayerTest.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,14 @@ TEST_F(ObjectLinkingLayerTest, AddAndRemovePlugins) {
255255
}
256256

257257
TEST(ObjectLinkingLayerSearchGeneratorTest, AbsoluteSymbolsObjectLayer) {
258-
class TestEPC : public UnsupportedExecutorProcessControl {
258+
class TestEPC : public UnsupportedExecutorProcessControl,
259+
public DylibManager {
259260
public:
260261
TestEPC()
261262
: UnsupportedExecutorProcessControl(nullptr, nullptr,
262-
"x86_64-apple-darwin") {}
263+
"x86_64-apple-darwin") {
264+
this->DylibMgr = this;
265+
}
263266

264267
Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override {
265268
return ExecutorAddr::fromPtr((void *)nullptr);

0 commit comments

Comments
 (0)