17#define DEBUG_TYPE "orc"
24 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
25 assert(Disconnected &&
"Destroyed without disconnection");
33 RunAsMainAddr,
Result, MainFnAddr, Args))
34 return std::move(Err);
43 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
44 SeqNo = getNextSeqNo();
45 assert(!PendingCallWrapperResults.count(SeqNo) &&
"SeqNo already in use");
46 PendingCallWrapperResults[SeqNo] = std::move(OnComplete);
50 WrapperFnAddr, ArgBuffer)) {
59 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
60 auto I = PendingCallWrapperResults.find(SeqNo);
61 if (
I != PendingCallWrapperResults.end()) {
62 H = std::move(
I->second);
63 PendingCallWrapperResults.erase(
I);
92 std::unique_lock<std::mutex> Lock(SimpleRemoteEPCMutex);
93 DisconnectCV.wait(Lock, [
this] {
return Disconnected; });
94 return std::move(DisconnectErr);
103 dbgs() <<
"SimpleRemoteEPC::handleMessage: opc = ";
107 assert(SeqNo == 0 &&
"Non-zero SeqNo for Setup?");
108 assert(!TagAddr &&
"Non-zero TagAddr for Setup?");
112 assert(SeqNo == 0 &&
"Non-zero SeqNo for Hangup?");
113 assert(!TagAddr &&
"Non-zero TagAddr for Hangup?");
117 assert(!TagAddr &&
"Non-zero TagAddr for Result?");
120 dbgs() <<
"CallWrapper";
123 dbgs() <<
", seqno = " << SeqNo <<
", tag-addr = " << TagAddr
124 <<
", arg-buffer = " <<
formatv(
"{0:x}", ArgBytes.
size())
128 using UT = std::underlying_type_t<SimpleRemoteEPCOpcode>;
135 if (
auto Err = handleSetup(SeqNo, TagAddr, std::move(ArgBytes)))
136 return std::move(Err);
140 if (
auto Err = handleHangup(std::move(ArgBytes)))
141 return std::move(Err);
144 if (
auto Err = handleResult(SeqNo, TagAddr, std::move(ArgBytes)))
145 return std::move(Err);
148 handleCallWrapper(SeqNo, TagAddr, std::move(ArgBytes));
156 dbgs() <<
"SimpleRemoteEPC::handleDisconnect: "
157 << (Err ?
"failure" :
"success") <<
"\n";
160 PendingCallWrapperResultsMap TmpPending;
163 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
164 std::swap(TmpPending, PendingCallWrapperResults);
167 for (
auto &KV : TmpPending)
171 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
172 DisconnectErr =
joinErrors(std::move(DisconnectErr), std::move(Err));
174 DisconnectCV.notify_all();
181 "SimpleRemoteEPC sending Setup message? That's the wrong direction.");
184 dbgs() <<
"SimpleRemoteEPC::sendMessage: opc = ";
188 assert(SeqNo == 0 &&
"Non-zero SeqNo for Hangup?");
189 assert(!TagAddr &&
"Non-zero TagAddr for Hangup?");
193 assert(!TagAddr &&
"Non-zero TagAddr for Result?");
196 dbgs() <<
"CallWrapper";
201 dbgs() <<
", seqno = " << SeqNo <<
", tag-addr = " << TagAddr
202 <<
", arg-buffer = " <<
formatv(
"{0:x}", ArgBytes.
size())
205 auto Err =
T->sendMessage(OpC, SeqNo, TagAddr, ArgBytes);
208 dbgs() <<
" \\--> SimpleRemoteEPC::sendMessage failed\n";
223 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
224 auto I = PendingCallWrapperResults.find(0);
225 assert(PendingCallWrapperResults.size() == 1 &&
226 I != PendingCallWrapperResults.end() &&
227 "Setup message handler not connectly set up");
228 auto SetupMsgHandler = std::move(
I->second);
229 PendingCallWrapperResults.erase(
I);
233 SetupMsgHandler(std::move(WFR));
237Error SimpleRemoteEPC::setup() {
238 using namespace SimpleRemoteEPCDefaultBootstrapSymbolNames;
240 std::promise<MSVCPExpected<SimpleRemoteEPCExecutorInfo>> EIP;
241 auto EIF = EIP.get_future();
244 PendingCallWrapperResults[0] =
246 [&](shared::WrapperFunctionBuffer SetupMsgBytes) {
247 if (
const char *ErrMsg = SetupMsgBytes.getOutOfBandError()) {
253 shared::SPSArgList<shared::SPSSimpleRemoteEPCExecutorInfo>;
254 shared::SPSInputBuffer
IB(SetupMsgBytes.data(), SetupMsgBytes.size());
255 SimpleRemoteEPCExecutorInfo EI;
256 if (SPSSerialize::deserialize(IB, EI))
264 if (
auto Err = T->start())
271 return EI.takeError();
275 dbgs() <<
"SimpleRemoteEPC received setup message:\n"
276 <<
" Triple: " << EI->TargetTriple <<
"\n"
277 <<
" Page size: " << EI->PageSize <<
"\n"
278 <<
" Bootstrap map" << (EI->BootstrapMap.empty() ?
" empty" :
":")
280 for (
const auto &KV : EI->BootstrapMap)
281 dbgs() <<
" " << KV.first() <<
": " << KV.second.size()
282 <<
"-byte SPS encoded buffer\n";
283 dbgs() <<
" Bootstrap symbols"
284 << (EI->BootstrapSymbols.empty() ?
" empty" :
":") <<
"\n";
285 for (
const auto &KV : EI->BootstrapSymbols)
286 dbgs() <<
" " << KV.first() <<
": " << KV.second <<
"\n";
313 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
314 auto I = PendingCallWrapperResults.find(SeqNo);
315 if (
I == PendingCallWrapperResults.end())
319 SendResult = std::move(
I->second);
320 PendingCallWrapperResults.erase(
I);
326 SendResult(std::move(WFR));
330void SimpleRemoteEPC::handleCallWrapper(
333 assert(
ES &&
"No ExecutionSession attached");
335 [
this, RemoteSeqNo, TagAddr, ArgBytes = std::move(ArgBytes)]()
mutable {
336 ES->runJITDispatchHandler(
337 [
this, RemoteSeqNo](shared::WrapperFunctionBuffer WFR) {
340 ExecutorAddr(), {WFR.data(), WFR.size()}))
343 TagAddr, std::move(ArgBytes));
345 "callWrapper task"));
349 using namespace llvm::orc::shared;
350 auto WFR = WrapperFunctionBuffer::copyFrom(ArgBytes.data(), ArgBytes.size());
351 if (
const char *ErrMsg = WFR.getOutOfBandError())
354 orc::shared::detail::SPSSerializableError
Info;
355 SPSInputBuffer
IB(WFR.data(), WFR.size());
356 if (!SPSArgList<SPSError>::deserialize(IB, Info))
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
Error callSPSWrapper(ExecutorAddr WrapperFnAddr, WrapperCallArgTs &&...WrapperCallArgs)
Run a wrapper function using SPS to serialize the arguments and deserialize the results.
Error getBootstrapSymbols(ArrayRef< std::pair< ExecutorAddr &, StringRef > > Pairs) const
For each (ExecutorAddr&, StringRef) pair, looks up the string in the bootstrap symbols map and writes...
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
Error fromSPSSerializable(SPSSerializableError BSE)
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...
std::unique_ptr< GenericNamedTask > makeGenericNamedTask(FnT &&Fn, std::string Desc)
Create a generic named task from a std::string description.
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 swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
static constexpr char Name[]