19#define DEBUG_TYPE "orc"
26 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
27 assert(Disconnected &&
"Destroyed without disconnection");
50 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
51 SeqNo = getNextSeqNo();
52 assert(!PendingCallWrapperResults.count(SeqNo) &&
"SeqNo already in use");
53 PendingCallWrapperResults[SeqNo] = std::move(OnComplete);
57 WrapperFnAddr, ArgBuffer)) {
66 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
67 auto I = PendingCallWrapperResults.find(SeqNo);
68 if (
I != PendingCallWrapperResults.end()) {
69 H = std::move(
I->second);
70 PendingCallWrapperResults.erase(
I);
100 bool SendHangup =
false;
102 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
103 SendHangup = !LocalHangup && !RemoteHangup;
115 {Payload.data(), Payload.size()}))
121 std::unique_lock<std::mutex> Lock(SimpleRemoteEPCMutex);
122 DisconnectCV.wait(Lock, [
this] {
return Disconnected; });
123 return std::move(DisconnectErr);
132 dbgs() <<
"SimpleRemoteEPC::handleMessage: opc = ";
136 assert(SeqNo == 0 &&
"Non-zero SeqNo for Setup?");
137 assert(!TagAddr &&
"Non-zero TagAddr for Setup?");
141 assert(SeqNo == 0 &&
"Non-zero SeqNo for Hangup?");
142 assert(!TagAddr &&
"Non-zero TagAddr for Hangup?");
148 dbgs() <<
"CallWrapper";
151 dbgs() <<
", seqno = " << SeqNo <<
", tag-addr = " << TagAddr
152 <<
", arg-buffer = " <<
formatv(
"{0:x}", ArgBytes.
size())
156 using UT = std::underlying_type_t<SimpleRemoteEPCOpcode>;
163 if (
auto Err = handleSetup(SeqNo, TagAddr, std::move(ArgBytes)))
164 return std::move(Err);
169 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
172 if (
auto Err = handleHangup(std::move(ArgBytes)))
173 return std::move(Err);
176 if (
auto Err = handleResult(SeqNo, TagAddr, std::move(ArgBytes)))
177 return std::move(Err);
180 handleCallWrapper(SeqNo, TagAddr, std::move(ArgBytes));
188 dbgs() <<
"SimpleRemoteEPC::handleDisconnect: "
189 << (Err ?
"failure" :
"success") <<
"\n";
192 PendingCallWrapperResultsMap TmpPending;
195 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
196 std::swap(TmpPending, PendingCallWrapperResults);
199 for (
auto &KV : TmpPending)
203 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
216 Error DisconnectReason =
217 (!Err && !LocalHangup && !RemoteHangup)
223 joinErrors(std::move(DisconnectErr), std::move(DisconnectReason));
225 DisconnectCV.notify_all();
232 "SimpleRemoteEPC sending Setup message? That's the wrong direction.");
235 dbgs() <<
"SimpleRemoteEPC::sendMessage: opc = ";
239 assert(SeqNo == 0 &&
"Non-zero SeqNo for Hangup?");
240 assert(!TagAddr &&
"Non-zero TagAddr for Hangup?");
246 dbgs() <<
"CallWrapper";
251 dbgs() <<
", seqno = " << SeqNo <<
", tag-addr = " << TagAddr
252 <<
", arg-buffer = " <<
formatv(
"{0:x}", ArgBytes.
size())
255 auto Err =
T->sendMessage(OpC, SeqNo, TagAddr, ArgBytes);
258 dbgs() <<
" \\--> SimpleRemoteEPC::sendMessage failed\n";
273 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
274 auto I = PendingCallWrapperResults.find(0);
275 assert(PendingCallWrapperResults.size() == 1 &&
276 I != PendingCallWrapperResults.end() &&
277 "Setup message handler not connectly set up");
278 auto SetupMsgHandler = std::move(
I->second);
279 PendingCallWrapperResults.erase(
I);
283 SetupMsgHandler(std::move(WFR));
287Error SimpleRemoteEPC::setup() {
288 using namespace SimpleRemoteEPCDefaultBootstrapSymbolNames;
290 std::promise<MSVCPExpected<SimpleRemoteEPCExecutorInfo>> EIP;
291 auto EIF = EIP.get_future();
294 PendingCallWrapperResults[0] =
296 [&](shared::WrapperFunctionBuffer SetupMsgBytes) {
297 if (
const char *ErrMsg = SetupMsgBytes.getOutOfBandError()) {
303 shared::SPSArgList<shared::SPSSimpleRemoteEPCExecutorInfo>;
304 shared::SPSInputBuffer
IB(SetupMsgBytes.data(), SetupMsgBytes.size());
305 SimpleRemoteEPCExecutorInfo EI;
306 if (SPSSerialize::deserialize(IB, EI))
314 if (
auto Err = T->start())
321 return EI.takeError();
325 dbgs() <<
"SimpleRemoteEPC received setup message:\n"
326 <<
" Triple: " << EI->TargetTriple <<
"\n"
327 <<
" Page size: " << EI->PageSize <<
"\n"
328 <<
" Bootstrap map" << (EI->BootstrapMap.empty() ?
" empty" :
":")
330 for (
const auto &KV : EI->BootstrapMap)
331 dbgs() <<
" " << KV.first() <<
": " << KV.second.size()
332 <<
"-byte SPS encoded buffer\n";
333 dbgs() <<
" Bootstrap symbols"
334 << (EI->BootstrapSymbols.empty() ?
" empty" :
":") <<
"\n";
335 for (
const auto &KV : EI->BootstrapSymbols)
336 dbgs() <<
" " << KV.first() <<
": " << KV.second <<
"\n";
356 return WFR.takeError();
359 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
360 auto I = PendingCallWrapperResults.find(SeqNo);
361 if (
I == PendingCallWrapperResults.end())
365 SendResult = std::move(
I->second);
366 PendingCallWrapperResults.erase(
I);
370 SendResult(std::move(*WFR));
374void SimpleRemoteEPC::handleCallWrapper(
377 assert(
ES &&
"No ExecutionSession attached");
379 [
this, RemoteSeqNo, TagAddr, ArgBytes = std::move(ArgBytes)]()
mutable {
380 ES->runJITDispatchHandler(
381 [
this, RemoteSeqNo](shared::WrapperFunctionBuffer WFR) {
385 ResultTag, {Payload.data(), Payload.size()}))
388 TagAddr, std::move(ArgBytes));
390 "callWrapper task"));
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
Get the array size.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
void reportError(Error Err)
Report a error for this execution session.
Represents an address in the executor process.
A handler or incoming WrapperFunctionBuffers – either return values from callWrapper* calls,...
Constructs an IncomingWFRHandler from a function object that is callable as void(shared::WrapperFunct...
std::unique_ptr< TaskDispatcher > D
StringMap< ExecutorAddr > BootstrapSymbols
StringMap< std::vector< char > > BootstrapMap
ExecutionSession & getExecutionSession()
Return the ExecutionSession associated with this instance.
void handleDisconnect(Error Err) override
Handle a disconnection from the underlying transport.
Expected< std::unique_ptr< MemoryAccess > > createDefaultMemoryAccess() override
Create a default MemoryAccess for the target process.
Expected< int32_t > runAsMain(ExecutorAddr MainFnAddr, ArrayRef< std::string > Args) override
Run function with a main-like signature.
Expected< std::unique_ptr< jitlink::JITLinkMemoryManager > > createDefaultMemoryManager() override
Create a default JITLinkMemoryManager for the target process.
Expected< HandleMessageAction > handleMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo, ExecutorAddr TagAddr, shared::WrapperFunctionBuffer ArgBytes) override
Handle receipt of a message.
~SimpleRemoteEPC() override
Expected< std::unique_ptr< DylibManager > > createDefaultDylibMgr() override
Create a default DylibManager for the target process.
Error disconnect() override
Disconnect from the target process.
void callWrapperAsync(ExecutorAddr WrapperFnAddr, IncomingWFRHandler OnComplete, ArrayRef< char > ArgBuffer) override
Run a wrapper function in the executor.
C++ wrapper function buffer: Same as CWrapperFunctionBuffer but auto-releases memory.
static WrapperFunctionBuffer copyFrom(const char *Source, size_t Size)
Copy from the given char range.
size_t size() const
Returns the size of the data contained in this instance.
static WrapperFunctionBuffer createOutOfBandError(const char *Msg)
Create an out-of-band error by copying the given string.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI const char * DispatchFnName
LLVM_ABI const char * ExecutorSessionObjectName
LLVM_ABI const char *const DispatchCtxName
LLVM_ABI const char *const DispatchName
LLVM_ABI Expected< std::unique_ptr< EPCGenericJITLinkMemoryManager > > createEPCGenericJITLinkMemoryManager(JITDylib &JD)
Create an EPCGenericJITLinkMemoryManager for the ORC runtime's SimpleNativeMemoryMap interface,...
LLVM_ABI Expected< std::unique_ptr< EPCGenericDylibManager > > createEPCGenericDylibManager(JITDylib &JD)
Create an EPCGenericDylibManager for the ORC runtime's NativeDylibManager interface,...
LLVM_ABI Expected< std::unique_ptr< EPCGenericMemoryAccess > > createEPCGenericMemoryAccess(JITDylib &JD)
Create an EPCGenericMemoryAccess that reaches the memory-access wrappers in the given JITDylib via th...
LLVM_ABI shared::WrapperFunctionBuffer encodeHangupPayload(Error Err)
Encode an Error as the payload of a Hangup message.
LLVM_ABI std::pair< ExecutorAddr, shared::WrapperFunctionBuffer > encodeResultMessage(shared::WrapperFunctionBuffer ResultBytes)
Encode a wrapper function result as the TagAddr and payload of a Result message.
std::unique_ptr< GenericNamedTask > makeGenericNamedTask(FnT &&Fn, std::string Desc)
Create a generic named task from a std::string description.
LLVM_ABI void lookupAndApply(unique_function< void(Error)> OnApplied, LookupKind K, const JITDylibSearchOrder &SearchOrder, ArrayRef< LookupPrepareFn > PrepareFns)
Resolve the symbols contributed by every prepare function with a single lookup, then let each of thei...
LookupPrepareFn recordProxy(Proxy< FnT > *P, typename Proxy< FnT >::DispatchFn Dispatch, StringRef Name, SymbolLookupFlags LF=SymbolLookupFlags::RequiredSymbol)
Builds P over the symbol with the given name, dispatching through Dispatch.
LLVM_ABI Error decodeHangupPayload(shared::WrapperFunctionBuffer Payload)
Decode a Hangup payload produced by encodeHangupPayload.
LLVM_ABI Expected< shared::WrapperFunctionBuffer > decodeResultMessage(ExecutorAddr TagAddr, shared::WrapperFunctionBuffer Payload)
Decode a Result message produced by encodeResultMessage, returning the result to complete the pending...
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
Error joinErrors(Error E1, Error E2)
Concatenate errors.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
void consumeError(Error Err)
Consume a Error without doing anything.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.