Skip to content

Commit 6613f4a

Browse files
committed
[ORC] Use raw OS handle values, ExecutorAddr for EPC dylib handles.
Updates tpctypes::DylibHandle to be an ExecutorAddr (rather than a uint64_t), and SimpleExecutorDylibManager to hold and return raw OS handle values (as ExecutorAddrs) rather than index values into a map of DynamicLibrary instances. This will allow clients to use EPCGenericDylibManager in contexts where the existing DynamicLibrary interface is too limited to be used. (e.g. to look up JIT symbols in a dylib that was loaded with RTLD_LOCAL).
1 parent 5dcfc32 commit 6613f4a

10 files changed

+26
-29
lines changed

llvm/include/llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ extern const char *RunAsVoidFunctionWrapperName;
5151
extern const char *RunAsIntFunctionWrapperName;
5252

5353
using SPSSimpleExecutorDylibManagerOpenSignature =
54-
shared::SPSExpected<uint64_t>(shared::SPSExecutorAddr, shared::SPSString,
55-
uint64_t);
54+
shared::SPSExpected<shared::SPSExecutorAddr>(shared::SPSExecutorAddr,
55+
shared::SPSString, uint64_t);
5656

5757
using SPSSimpleExecutorDylibManagerLookupSignature =
5858
shared::SPSExpected<shared::SPSSequence<shared::SPSExecutorAddr>>(
59-
shared::SPSExecutorAddr, uint64_t, shared::SPSRemoteSymbolLookupSet);
59+
shared::SPSExecutorAddr, shared::SPSExecutorAddr,
60+
shared::SPSRemoteSymbolLookupSet);
6061

6162
using SPSSimpleExecutorMemoryManagerReserveSignature =
6263
shared::SPSExpected<shared::SPSExecutorAddr>(shared::SPSExecutorAddr,

llvm/include/llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ struct BufferWrite {
8585
};
8686

8787
/// A handle used to represent a loaded dylib in the target process.
88-
using DylibHandle = JITTargetAddress;
88+
using DylibHandle = ExecutorAddr;
8989

90-
using LookupResult = std::vector<JITTargetAddress>;
90+
using LookupResult = std::vector<ExecutorAddr>;
9191

9292
} // end namespace tpctypes
9393

llvm/include/llvm/ExecutionEngine/Orc/TargetProcess/SimpleExecutorDylibManager.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#ifndef LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_SIMPLEEXECUTORDYLIBMANAGER_H
1717
#define LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_SIMPLEEXECUTORDYLIBMANAGER_H
1818

19-
#include "llvm/ADT/DenseMap.h"
19+
#include "llvm/ADT/DenseSet.h"
2020
#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
2121
#include "llvm/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.h"
2222
#include "llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h"
@@ -44,7 +44,7 @@ class SimpleExecutorDylibManager : public ExecutorBootstrapService {
4444
void addBootstrapSymbols(StringMap<ExecutorAddr> &M) override;
4545

4646
private:
47-
using DylibsMap = DenseMap<uint64_t, sys::DynamicLibrary>;
47+
using DylibSet = DenseSet<void *>;
4848

4949
static llvm::orc::shared::CWrapperFunctionResult
5050
openWrapper(const char *ArgData, size_t ArgSize);
@@ -53,8 +53,7 @@ class SimpleExecutorDylibManager : public ExecutorBootstrapService {
5353
lookupWrapper(const char *ArgData, size_t ArgSize);
5454

5555
std::mutex M;
56-
uint64_t NextId = 0;
57-
DylibsMap Dylibs;
56+
DylibSet Dylibs;
5857
};
5958

6059
} // end namespace rt_bootstrap

llvm/include/llvm/Support/DynamicLibrary.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class DynamicLibrary {
4242
public:
4343
explicit DynamicLibrary(void *data = &Invalid) : Data(data) {}
4444

45+
/// Return the OS specific handle value.
46+
void *getOSSpecificHandle() const { return Data; }
47+
4548
/// Returns true if the object refers to a valid library.
4649
bool isValid() const { return Data != &Invalid; }
4750

llvm/lib/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Error EPCDynamicLibrarySearchGenerator::tryToGenerate(
5454
for (auto &KV : LookupSymbols) {
5555
if (*ResultI)
5656
NewSymbols[KV.first] =
57-
JITEvaluatedSymbol(*ResultI, JITSymbolFlags::Exported);
57+
JITEvaluatedSymbol(ResultI->getValue(), JITSymbolFlags::Exported);
5858
++ResultI;
5959
}
6060

llvm/lib/ExecutionEngine/Orc/EPCGenericDylibManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ EPCGenericDylibManager::CreateWithDefaultBootstrapSymbols(
7373

7474
Expected<tpctypes::DylibHandle> EPCGenericDylibManager::open(StringRef Path,
7575
uint64_t Mode) {
76-
Expected<tpctypes::DylibHandle> H(0);
76+
Expected<tpctypes::DylibHandle> H((ExecutorAddr()));
7777
if (auto Err =
7878
EPC.callSPSWrapper<rt::SPSSimpleExecutorDylibManagerOpenSignature>(
7979
SAs.Open, H, SAs.Instance, Path, Mode))

llvm/lib/ExecutionEngine/Orc/ExecutorProcessControl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,22 @@ SelfExecutorProcessControl::loadDylib(const char *DylibPath) {
8080
if (!Dylib->isValid())
8181
return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
8282
DynamicLibraries.push_back(std::move(Dylib));
83-
return pointerToJITTargetAddress(DynamicLibraries.back().get());
83+
return ExecutorAddr::fromPtr(DynamicLibraries.back().get());
8484
}
8585

8686
Expected<std::vector<tpctypes::LookupResult>>
8787
SelfExecutorProcessControl::lookupSymbols(ArrayRef<LookupRequest> Request) {
8888
std::vector<tpctypes::LookupResult> R;
8989

9090
for (auto &Elem : Request) {
91-
auto *Dylib = jitTargetAddressToPointer<sys::DynamicLibrary *>(Elem.Handle);
91+
auto *Dylib = Elem.Handle.toPtr<sys::DynamicLibrary *>();
9292
assert(llvm::any_of(DynamicLibraries,
9393
[=](const std::unique_ptr<sys::DynamicLibrary> &DL) {
9494
return DL.get() == Dylib;
9595
}) &&
9696
"Invalid handle");
9797

98-
R.push_back(std::vector<JITTargetAddress>());
98+
R.push_back(std::vector<ExecutorAddr>());
9999
for (auto &KV : Elem.Symbols) {
100100
auto &Sym = KV.first;
101101
std::string Tmp((*Sym).data() + !!GlobalManglingPrefix,
@@ -107,7 +107,7 @@ SelfExecutorProcessControl::lookupSymbols(ArrayRef<LookupRequest> Request) {
107107
MissingSymbols.push_back(Sym);
108108
return make_error<SymbolsNotFound>(SSP, std::move(MissingSymbols));
109109
}
110-
R.back().push_back(pointerToJITTargetAddress(Addr));
110+
R.back().push_back(ExecutorAddr::fromPtr(Addr));
111111
}
112112
}
113113

llvm/lib/ExecutionEngine/Orc/LookupAndRecordAddrs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Error lookupAndRecordAddrs(
7373
inconvertibleErrorCode());
7474

7575
for (unsigned I = 0; I != Pairs.size(); ++I)
76-
Pairs[I].second->setValue(Result->front()[I]);
76+
*Pairs[I].second = Result->front()[I];
7777

7878
return Error::success();
7979
}

llvm/lib/ExecutionEngine/Orc/SimpleRemoteEPC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ SimpleRemoteEPC::lookupSymbols(ArrayRef<LookupRequest> Request) {
3838
Result.push_back({});
3939
Result.back().reserve(R->size());
4040
for (auto Addr : *R)
41-
Result.back().push_back(Addr.getValue());
41+
Result.back().push_back(Addr);
4242
} else
4343
return R.takeError();
4444
}

llvm/lib/ExecutionEngine/Orc/TargetProcess/SimpleExecutorDylibManager.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,18 @@ SimpleExecutorDylibManager::open(const std::string &Path, uint64_t Mode) {
3535
return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
3636

3737
std::lock_guard<std::mutex> Lock(M);
38-
Dylibs[NextId] = std::move(DL);
39-
return NextId++;
38+
auto H = ExecutorAddr::fromPtr(DL.getOSSpecificHandle());
39+
Dylibs.insert(DL.getOSSpecificHandle());
40+
return H;
4041
}
4142

4243
Expected<std::vector<ExecutorAddr>>
4344
SimpleExecutorDylibManager::lookup(tpctypes::DylibHandle H,
4445
const RemoteSymbolLookupSet &L) {
4546
std::vector<ExecutorAddr> Result;
46-
47-
std::lock_guard<std::mutex> Lock(M);
48-
auto I = Dylibs.find(H);
49-
if (I == Dylibs.end())
50-
return make_error<StringError>("No dylib for handle " + formatv("{0:x}", H),
51-
inconvertibleErrorCode());
52-
auto &DL = I->second;
47+
auto DL = sys::DynamicLibrary(H.toPtr<void *>());
5348

5449
for (const auto &E : L) {
55-
5650
if (E.Name.empty()) {
5751
if (E.Required)
5852
return make_error<StringError>("Required address for empty symbol \"\"",
@@ -85,10 +79,10 @@ SimpleExecutorDylibManager::lookup(tpctypes::DylibHandle H,
8579

8680
Error SimpleExecutorDylibManager::shutdown() {
8781

88-
DylibsMap DM;
82+
DylibSet DS;
8983
{
9084
std::lock_guard<std::mutex> Lock(M);
91-
std::swap(DM, Dylibs);
85+
std::swap(DS, Dylibs);
9286
}
9387

9488
// There is no removal of dylibs at the moment, so nothing to do here.

0 commit comments

Comments
 (0)