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);
83 return DM.takeError();
84 return std::make_unique<EPCGenericDylibManager>(std::move(*
DM));
95 std::unique_lock<std::mutex> Lock(SimpleRemoteEPCMutex);
96 DisconnectCV.wait(Lock, [
this] {
return Disconnected; });
97 return std::move(DisconnectErr);
106 dbgs() <<
"SimpleRemoteEPC::handleMessage: opc = ";
110 assert(SeqNo == 0 &&
"Non-zero SeqNo for Setup?");
111 assert(!TagAddr &&
"Non-zero TagAddr for Setup?");
115 assert(SeqNo == 0 &&
"Non-zero SeqNo for Hangup?");
116 assert(!TagAddr &&
"Non-zero TagAddr for Hangup?");
120 assert(!TagAddr &&
"Non-zero TagAddr for Result?");
123 dbgs() <<
"CallWrapper";
126 dbgs() <<
", seqno = " << SeqNo <<
", tag-addr = " << TagAddr
127 <<
", arg-buffer = " <<
formatv(
"{0:x}", ArgBytes.
size())
131 using UT = std::underlying_type_t<SimpleRemoteEPCOpcode>;
138 if (
auto Err = handleSetup(SeqNo, TagAddr, std::move(ArgBytes)))
139 return std::move(Err);
143 if (
auto Err = handleHangup(std::move(ArgBytes)))
144 return std::move(Err);
147 if (
auto Err = handleResult(SeqNo, TagAddr, std::move(ArgBytes)))
148 return std::move(Err);
151 handleCallWrapper(SeqNo, TagAddr, std::move(ArgBytes));
159 dbgs() <<
"SimpleRemoteEPC::handleDisconnect: "
160 << (Err ?
"failure" :
"success") <<
"\n";
163 PendingCallWrapperResultsMap TmpPending;
166 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
167 std::swap(TmpPending, PendingCallWrapperResults);
170 for (
auto &KV : TmpPending)
174 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
175 DisconnectErr =
joinErrors(std::move(DisconnectErr), std::move(Err));
177 DisconnectCV.notify_all();
184 "SimpleRemoteEPC sending Setup message? That's the wrong direction.");
187 dbgs() <<
"SimpleRemoteEPC::sendMessage: opc = ";
191 assert(SeqNo == 0 &&
"Non-zero SeqNo for Hangup?");
192 assert(!TagAddr &&
"Non-zero TagAddr for Hangup?");
196 assert(!TagAddr &&
"Non-zero TagAddr for Result?");
199 dbgs() <<
"CallWrapper";
204 dbgs() <<
", seqno = " << SeqNo <<
", tag-addr = " << TagAddr
205 <<
", arg-buffer = " <<
formatv(
"{0:x}", ArgBytes.
size())
208 auto Err =
T->sendMessage(OpC, SeqNo, TagAddr, ArgBytes);
211 dbgs() <<
" \\--> SimpleRemoteEPC::sendMessage failed\n";
226 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
227 auto I = PendingCallWrapperResults.find(0);
228 assert(PendingCallWrapperResults.size() == 1 &&
229 I != PendingCallWrapperResults.end() &&
230 "Setup message handler not connectly set up");
231 auto SetupMsgHandler = std::move(
I->second);
232 PendingCallWrapperResults.erase(
I);
236 SetupMsgHandler(std::move(WFR));
240Error SimpleRemoteEPC::setup() {
241 using namespace SimpleRemoteEPCDefaultBootstrapSymbolNames;
243 std::promise<MSVCPExpected<SimpleRemoteEPCExecutorInfo>> EIP;
244 auto EIF = EIP.get_future();
247 PendingCallWrapperResults[0] =
249 [&](shared::WrapperFunctionBuffer SetupMsgBytes) {
250 if (
const char *ErrMsg = SetupMsgBytes.getOutOfBandError()) {
256 shared::SPSArgList<shared::SPSSimpleRemoteEPCExecutorInfo>;
257 shared::SPSInputBuffer
IB(SetupMsgBytes.data(), SetupMsgBytes.size());
258 SimpleRemoteEPCExecutorInfo EI;
259 if (SPSSerialize::deserialize(IB, EI))
267 if (
auto Err = T->start())
274 return EI.takeError();
278 dbgs() <<
"SimpleRemoteEPC received setup message:\n"
279 <<
" Triple: " << EI->TargetTriple <<
"\n"
280 <<
" Page size: " << EI->PageSize <<
"\n"
281 <<
" Bootstrap map" << (EI->BootstrapMap.empty() ?
" empty" :
":")
283 for (
const auto &KV : EI->BootstrapMap)
284 dbgs() <<
" " << KV.first() <<
": " << KV.second.size()
285 <<
"-byte SPS encoded buffer\n";
286 dbgs() <<
" Bootstrap symbols"
287 << (EI->BootstrapSymbols.empty() ?
" empty" :
":") <<
"\n";
288 for (
const auto &KV : EI->BootstrapSymbols)
289 dbgs() <<
" " << KV.first() <<
": " << KV.second <<
"\n";
316 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
317 auto I = PendingCallWrapperResults.find(SeqNo);
318 if (
I == PendingCallWrapperResults.end())
322 SendResult = std::move(
I->second);
323 PendingCallWrapperResults.erase(
I);
329 SendResult(std::move(WFR));
333void SimpleRemoteEPC::handleCallWrapper(
336 assert(
ES &&
"No ExecutionSession attached");
338 [
this, RemoteSeqNo, TagAddr, ArgBytes = std::move(ArgBytes)]()
mutable {
339 ES->runJITDispatchHandler(
340 [
this, RemoteSeqNo](shared::WrapperFunctionBuffer WFR) {
343 ExecutorAddr(), {WFR.data(), WFR.size()}))
346 TagAddr, std::move(ArgBytes));
348 "callWrapper task"));
352 using namespace llvm::orc::shared;
353 auto WFR = WrapperFunctionBuffer::copyFrom(ArgBytes.data(), ArgBytes.size());
354 if (
const char *ErrMsg = WFR.getOutOfBandError())
357 orc::shared::detail::SPSSerializableError
Info;
358 SPSInputBuffer
IB(WFR.data(), WFR.size());
359 if (!SPSArgList<SPSError>::deserialize(IB, Info))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static RegisterPass< DebugifyModulePass > DM("debugify", "Attach debug info to everything")
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.
static Expected< EPCGenericDylibManager > Create(JITDylib &JD)
Create an EPCGenericDylibManager for the ORC runtime's NativeDylibManager interface,...
static Expected< std::unique_ptr< EPCGenericJITLinkMemoryManager > > Create(JITDylib &JD)
Create an EPCGenericJITLinkMemoryManager for the ORC runtime's SimpleNativeMemoryMap interface,...
static Expected< std::unique_ptr< MemoryAccess > > Create(ExecutionSession &ES)
Create an EPCGenericMemoryAccess instance that reaches the memory-access wrappers in ES's bootstrap J...
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)
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[]