#ifndef LLVM_EXECUTIONENGINE_ORC_EXECUTORPROCESSCONTROL_H
#define LLVM_EXECUTIONENGINE_ORC_EXECUTORPROCESSCONTROL_H
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Triple.h"
#include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
#include "llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h"
#include "llvm/ExecutionEngine/Orc/Shared/WrapperFunctionUtils.h"
#include "llvm/ExecutionEngine/Orc/SymbolStringPool.h"
#include "llvm/ExecutionEngine/Orc/TaskDispatch.h"
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/MSVCErrorWorkarounds.h"
#include <future>
#include <mutex>
#include <vector>
namespace llvm {
namespace orc {
class ExecutionSession;
class SymbolLookupSet;
class ExecutorProcessControl {
friend class ExecutionSession;
public:
class IncomingWFRHandler {
friend class ExecutorProcessControl;
public:
IncomingWFRHandler() = default;
explicit operator bool() const { return !!H; }
void operator()(shared::WrapperFunctionResult WFR) { H(std::move(WFR)); }
private:
template <typename FnT> IncomingWFRHandler(FnT &&Fn)
: H(std::forward<FnT>(Fn)) {}
unique_function<void(shared::WrapperFunctionResult)> H;
};
class RunInPlace {
public:
template <typename FnT>
IncomingWFRHandler operator()(FnT &&Fn) {
return IncomingWFRHandler(std::forward<FnT>(Fn));
}
};
class RunAsTask {
public:
RunAsTask(TaskDispatcher &D) : D(D) {}
template <typename FnT>
IncomingWFRHandler operator()(FnT &&Fn) {
return IncomingWFRHandler(
[&D = this->D, Fn = std::move(Fn)]
(shared::WrapperFunctionResult WFR) mutable {
D.dispatch(
makeGenericNamedTask(
[Fn = std::move(Fn), WFR = std::move(WFR)]() mutable {
Fn(std::move(WFR));
}, "WFR handler task"));
});
}
private:
TaskDispatcher &D;
};
class MemoryAccess {
public:
using WriteResultFn = unique_function<void(Error)>;
virtual ~MemoryAccess();
virtual void writeUInt8sAsync(ArrayRef<tpctypes::UInt8Write> Ws,
WriteResultFn OnWriteComplete) = 0;
virtual void writeUInt16sAsync(ArrayRef<tpctypes::UInt16Write> Ws,
WriteResultFn OnWriteComplete) = 0;
virtual void writeUInt32sAsync(ArrayRef<tpctypes::UInt32Write> Ws,
WriteResultFn OnWriteComplete) = 0;
virtual void writeUInt64sAsync(ArrayRef<tpctypes::UInt64Write> Ws,
WriteResultFn OnWriteComplete) = 0;
virtual void writeBuffersAsync(ArrayRef<tpctypes::BufferWrite> Ws,
WriteResultFn OnWriteComplete) = 0;
Error writeUInt8s(ArrayRef<tpctypes::UInt8Write> Ws) {
std::promise<MSVCPError> ResultP;
auto ResultF = ResultP.get_future();
writeUInt8sAsync(Ws,
[&](Error Err) { ResultP.set_value(std::move(Err)); });
return ResultF.get();
}
Error writeUInt16s(ArrayRef<tpctypes::UInt16Write> Ws) {
std::promise<MSVCPError> ResultP;
auto ResultF = ResultP.get_future();
writeUInt16sAsync(Ws,
[&](Error Err) { ResultP.set_value(std::move(Err)); });
return ResultF.get();
}
Error writeUInt32s(ArrayRef<tpctypes::UInt32Write> Ws) {
std::promise<MSVCPError> ResultP;
auto ResultF = ResultP.get_future();
writeUInt32sAsync(Ws,
[&](Error Err) { ResultP.set_value(std::move(Err)); });
return ResultF.get();
}
Error writeUInt64s(ArrayRef<tpctypes::UInt64Write> Ws) {
std::promise<MSVCPError> ResultP;
auto ResultF = ResultP.get_future();
writeUInt64sAsync(Ws,
[&](Error Err) { ResultP.set_value(std::move(Err)); });
return ResultF.get();
}
Error writeBuffers(ArrayRef<tpctypes::BufferWrite> Ws) {
std::promise<MSVCPError> ResultP;
auto ResultF = ResultP.get_future();
writeBuffersAsync(Ws,
[&](Error Err) { ResultP.set_value(std::move(Err)); });
return ResultF.get();
}
};
struct LookupRequest {
LookupRequest(tpctypes::DylibHandle Handle, const SymbolLookupSet &Symbols)
: Handle(Handle), Symbols(Symbols) {}
tpctypes::DylibHandle Handle;
const SymbolLookupSet &Symbols;
};
struct JITDispatchInfo {
ExecutorAddr JITDispatchFunction;
ExecutorAddr JITDispatchContext;
};
ExecutorProcessControl(std::shared_ptr<SymbolStringPool> SSP,
std::unique_ptr<TaskDispatcher> D)
: SSP(std::move(SSP)), D(std::move(D)) {}
virtual ~ExecutorProcessControl();
ExecutionSession &getExecutionSession() {
assert(ES && "No ExecutionSession associated yet");
return *ES;
}
SymbolStringPtr intern(StringRef SymName) { return SSP->intern(SymName); }
std::shared_ptr<SymbolStringPool> getSymbolStringPool() const { return SSP; }
TaskDispatcher &getDispatcher() { return *D; }
const Triple &getTargetTriple() const { return TargetTriple; }
unsigned getPageSize() const { return PageSize; }
const JITDispatchInfo &getJITDispatchInfo() const { return JDI; }
MemoryAccess &getMemoryAccess() const {
assert(MemAccess && "No MemAccess object set.");
return *MemAccess;
}
jitlink::JITLinkMemoryManager &getMemMgr() const {
assert(MemMgr && "No MemMgr object set");
return *MemMgr;
}
const StringMap<ExecutorAddr> &getBootstrapSymbolsMap() const {
return BootstrapSymbols;
}
Error getBootstrapSymbols(
ArrayRef<std::pair<ExecutorAddr &, StringRef>> Pairs) const {
for (auto &KV : Pairs) {
auto I = BootstrapSymbols.find(KV.second);
if (I == BootstrapSymbols.end())
return make_error<StringError>("Symbol \"" + KV.second +
"\" not found "
"in bootstrap symbols map",
inconvertibleErrorCode());
KV.first = I->second;
}
return Error::success();
}
virtual Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) = 0;
virtual Expected<std::vector<tpctypes::LookupResult>>
lookupSymbols(ArrayRef<LookupRequest> Request) = 0;
virtual Expected<int32_t> runAsMain(ExecutorAddr MainFnAddr,
ArrayRef<std::string> Args) = 0;
virtual void callWrapperAsync(ExecutorAddr WrapperFnAddr,
IncomingWFRHandler OnComplete,
ArrayRef<char> ArgBuffer) = 0;
template <typename RunPolicyT, typename FnT>
void callWrapperAsync(RunPolicyT &&Runner, ExecutorAddr WrapperFnAddr,
FnT &&OnComplete, ArrayRef<char> ArgBuffer) {
callWrapperAsync(
WrapperFnAddr, Runner(std::forward<FnT>(OnComplete)), ArgBuffer);
}
template <typename FnT>
void callWrapperAsync(ExecutorAddr WrapperFnAddr, FnT &&OnComplete,
ArrayRef<char> ArgBuffer) {
callWrapperAsync(RunAsTask(*D), WrapperFnAddr,
std::forward<FnT>(OnComplete), ArgBuffer);
}
shared::WrapperFunctionResult callWrapper(ExecutorAddr WrapperFnAddr,
ArrayRef<char> ArgBuffer) {
std::promise<shared::WrapperFunctionResult> RP;
auto RF = RP.get_future();
callWrapperAsync(
RunInPlace(), WrapperFnAddr,
[&](shared::WrapperFunctionResult R) {
RP.set_value(std::move(R));
}, ArgBuffer);
return RF.get();
}
template <typename SPSSignature, typename RunPolicyT, typename SendResultT,
typename... ArgTs>
void callSPSWrapperAsync(RunPolicyT &&Runner, ExecutorAddr WrapperFnAddr,
SendResultT &&SendResult, const ArgTs &...Args) {
shared::WrapperFunction<SPSSignature>::callAsync(
[this, WrapperFnAddr, Runner = std::move(Runner)]
(auto &&SendResult, const char *ArgData, size_t ArgSize) mutable {
this->callWrapperAsync(std::move(Runner), WrapperFnAddr,
std::move(SendResult),
ArrayRef<char>(ArgData, ArgSize));
},
std::forward<SendResultT>(SendResult), Args...);
}
template <typename SPSSignature, typename SendResultT, typename... ArgTs>
void callSPSWrapperAsync(ExecutorAddr WrapperFnAddr, SendResultT &&SendResult,
const ArgTs &...Args) {
callSPSWrapperAsync<SPSSignature>(RunAsTask(*D), WrapperFnAddr,
std::forward<SendResultT>(SendResult),
Args...);
}
template <typename SPSSignature, typename... WrapperCallArgTs>
Error callSPSWrapper(ExecutorAddr WrapperFnAddr,
WrapperCallArgTs &&...WrapperCallArgs) {
return shared::WrapperFunction<SPSSignature>::call(
[this, WrapperFnAddr](const char *ArgData, size_t ArgSize) {
return callWrapper(WrapperFnAddr, ArrayRef<char>(ArgData, ArgSize));
},
std::forward<WrapperCallArgTs>(WrapperCallArgs)...);
}
virtual Error disconnect() = 0;
protected:
std::shared_ptr<SymbolStringPool> SSP;
std::unique_ptr<TaskDispatcher> D;
ExecutionSession *ES = nullptr;
Triple TargetTriple;
unsigned PageSize = 0;
JITDispatchInfo JDI;
MemoryAccess *MemAccess = nullptr;
jitlink::JITLinkMemoryManager *MemMgr = nullptr;
StringMap<ExecutorAddr> BootstrapSymbols;
};
class UnsupportedExecutorProcessControl : public ExecutorProcessControl {
public:
UnsupportedExecutorProcessControl(
std::shared_ptr<SymbolStringPool> SSP = nullptr,
std::unique_ptr<TaskDispatcher> D = nullptr,
const std::string &TT = "", unsigned PageSize = 0)
: ExecutorProcessControl(SSP ? std::move(SSP)
: std::make_shared<SymbolStringPool>(),
D ? std::move(D)
: std::make_unique<InPlaceTaskDispatcher>()) {
this->TargetTriple = Triple(TT);
this->PageSize = PageSize;
}
Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override {
llvm_unreachable("Unsupported");
}
Expected<std::vector<tpctypes::LookupResult>>
lookupSymbols(ArrayRef<LookupRequest> Request) override {
llvm_unreachable("Unsupported");
}
Expected<int32_t> runAsMain(ExecutorAddr MainFnAddr,
ArrayRef<std::string> Args) override {
llvm_unreachable("Unsupported");
}
void callWrapperAsync(ExecutorAddr WrapperFnAddr,
IncomingWFRHandler OnComplete,
ArrayRef<char> ArgBuffer) override {
llvm_unreachable("Unsupported");
}
Error disconnect() override { return Error::success(); }
};
class SelfExecutorProcessControl
: public ExecutorProcessControl,
private ExecutorProcessControl::MemoryAccess {
public:
SelfExecutorProcessControl(
std::shared_ptr<SymbolStringPool> SSP, std::unique_ptr<TaskDispatcher> D,
Triple TargetTriple, unsigned PageSize,
std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr);
static Expected<std::unique_ptr<SelfExecutorProcessControl>>
Create(std::shared_ptr<SymbolStringPool> SSP = nullptr,
std::unique_ptr<TaskDispatcher> D = nullptr,
std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr = nullptr);
Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override;
Expected<std::vector<tpctypes::LookupResult>>
lookupSymbols(ArrayRef<LookupRequest> Request) override;
Expected<int32_t> runAsMain(ExecutorAddr MainFnAddr,
ArrayRef<std::string> Args) override;
void callWrapperAsync(ExecutorAddr WrapperFnAddr,
IncomingWFRHandler OnComplete,
ArrayRef<char> ArgBuffer) override;
Error disconnect() override;
private:
void writeUInt8sAsync(ArrayRef<tpctypes::UInt8Write> Ws,
WriteResultFn OnWriteComplete) override;
void writeUInt16sAsync(ArrayRef<tpctypes::UInt16Write> Ws,
WriteResultFn OnWriteComplete) override;
void writeUInt32sAsync(ArrayRef<tpctypes::UInt32Write> Ws,
WriteResultFn OnWriteComplete) override;
void writeUInt64sAsync(ArrayRef<tpctypes::UInt64Write> Ws,
WriteResultFn OnWriteComplete) override;
void writeBuffersAsync(ArrayRef<tpctypes::BufferWrite> Ws,
WriteResultFn OnWriteComplete) override;
static shared::CWrapperFunctionResult
jitDispatchViaWrapperFunctionManager(void *Ctx, const void *FnTag,
const char *Data, size_t Size);
std::unique_ptr<jitlink::JITLinkMemoryManager> OwnedMemMgr;
char GlobalManglingPrefix = 0;
std::vector<std::unique_ptr<sys::DynamicLibrary>> DynamicLibraries;
};
} }
#endif