14#define DEBUG_TYPE "orc"
21 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
22 assert(Disconnected &&
"Destroyed without disconnection");
27SimpleRemoteEPC::loadDylib(
const char *DylibPath) {
28 return EPCDylibMgr->open(DylibPath, 0);
37 std::vector<tpctypes::LookupResult>
Result,
40 return Complete(std::move(
Result));
42 auto &Element = Request.
front();
43 DylibMgr.
lookupAsync(Element.Handle, Element.Symbols,
44 [&DylibMgr, Request, Complete = std::move(Complete),
47 return Complete(R.takeError());
49 Result.back().reserve(R->size());
58void SimpleRemoteEPC::lookupSymbolsAsync(ArrayRef<LookupRequest> Request,
59 SymbolLookupCompleteFn Complete) {
60 lookupSymbolsAsyncHelper(*EPCDylibMgr, Request, {}, std::move(Complete));
66 if (
auto Err = callSPSWrapper<rt::SPSRunAsMainSignature>(
67 RunAsMainAddr, Result, MainFnAddr, Args))
68 return std::move(Err);
74 if (
auto Err = callSPSWrapper<rt::SPSRunAsVoidFunctionSignature>(
75 RunAsVoidFunctionAddr, Result, VoidFnAddr))
76 return std::move(Err);
83 if (
auto Err = callSPSWrapper<rt::SPSRunAsIntFunctionSignature>(
84 RunAsIntFunctionAddr, Result, IntFnAddr, Arg))
85 return std::move(Err);
94 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
95 SeqNo = getNextSeqNo();
96 assert(!PendingCallWrapperResults.count(SeqNo) &&
"SeqNo already in use");
97 PendingCallWrapperResults[SeqNo] = std::move(OnComplete);
100 if (
auto Err = sendMessage(SimpleRemoteEPCOpcode::CallWrapper, SeqNo,
101 WrapperFnAddr, ArgBuffer)) {
110 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
111 auto I = PendingCallWrapperResults.find(SeqNo);
112 if (
I != PendingCallWrapperResults.end()) {
113 H = std::move(
I->second);
114 PendingCallWrapperResults.erase(
I);
119 H(shared::WrapperFunctionResult::createOutOfBandError(
"disconnecting"));
121 getExecutionSession().reportError(std::move(Err));
125Error SimpleRemoteEPC::disconnect() {
128 std::unique_lock<std::mutex> Lock(SimpleRemoteEPCMutex);
129 DisconnectCV.wait(Lock, [
this] {
return Disconnected; });
130 return std::move(DisconnectErr);
139 dbgs() <<
"SimpleRemoteEPC::handleMessage: opc = ";
141 case SimpleRemoteEPCOpcode::Setup:
143 assert(SeqNo == 0 &&
"Non-zero SeqNo for Setup?");
144 assert(!TagAddr &&
"Non-zero TagAddr for Setup?");
146 case SimpleRemoteEPCOpcode::Hangup:
148 assert(SeqNo == 0 &&
"Non-zero SeqNo for Hangup?");
149 assert(!TagAddr &&
"Non-zero TagAddr for Hangup?");
151 case SimpleRemoteEPCOpcode::Result:
153 assert(!TagAddr &&
"Non-zero TagAddr for Result?");
155 case SimpleRemoteEPCOpcode::CallWrapper:
156 dbgs() <<
"CallWrapper";
159 dbgs() <<
", seqno = " << SeqNo <<
", tag-addr = " << TagAddr
160 <<
", arg-buffer = " <<
formatv(
"{0:x}", ArgBytes.
size())
164 using UT = std::underlying_type_t<SimpleRemoteEPCOpcode>;
165 if (
static_cast<UT
>(OpC) >
static_cast<UT
>(SimpleRemoteEPCOpcode::LastOpC))
166 return make_error<StringError>(
"Unexpected opcode",
170 case SimpleRemoteEPCOpcode::Setup:
171 if (
auto Err = handleSetup(SeqNo, TagAddr, std::move(ArgBytes)))
172 return std::move(Err);
174 case SimpleRemoteEPCOpcode::Hangup:
176 if (
auto Err = handleHangup(std::move(ArgBytes)))
177 return std::move(Err);
179 case SimpleRemoteEPCOpcode::Result:
180 if (
auto Err = handleResult(SeqNo, TagAddr, std::move(ArgBytes)))
181 return std::move(Err);
183 case SimpleRemoteEPCOpcode::CallWrapper:
184 handleCallWrapper(SeqNo, TagAddr, std::move(ArgBytes));
187 return ContinueSession;
190void SimpleRemoteEPC::handleDisconnect(
Error Err) {
192 dbgs() <<
"SimpleRemoteEPC::handleDisconnect: "
193 << (Err ?
"failure" :
"success") <<
"\n";
199 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
200 std::swap(TmpPending, PendingCallWrapperResults);
203 for (
auto &KV : TmpPending)
205 shared::WrapperFunctionResult::createOutOfBandError(
"disconnecting"));
207 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
208 DisconnectErr =
joinErrors(std::move(DisconnectErr), std::move(Err));
210 DisconnectCV.notify_all();
217 {{SAs.Allocator, rt::SimpleExecutorMemoryManagerInstanceName},
218 {SAs.Reserve, rt::SimpleExecutorMemoryManagerReserveWrapperName},
219 {SAs.Finalize, rt::SimpleExecutorMemoryManagerFinalizeWrapperName},
221 rt::SimpleExecutorMemoryManagerDeallocateWrapperName}}))
222 return std::move(Err);
224 return std::make_unique<EPCGenericJITLinkMemoryManager>(SREPC, SAs);
227Expected<std::unique_ptr<MemoryAccess>>
228SimpleRemoteEPC::createDefaultMemoryAccess(SimpleRemoteEPC &SREPC) {
229 EPCGenericMemoryAccess::FuncAddrs FAs;
230 if (
auto Err = SREPC.getBootstrapSymbols(
231 {{FAs.WriteUInt8s, rt::MemoryWriteUInt8sWrapperName},
232 {FAs.WriteUInt16s, rt::MemoryWriteUInt16sWrapperName},
233 {FAs.WriteUInt32s, rt::MemoryWriteUInt32sWrapperName},
234 {FAs.WriteUInt64s, rt::MemoryWriteUInt64sWrapperName},
235 {FAs.WriteBuffers, rt::MemoryWriteBuffersWrapperName},
236 {FAs.WritePointers, rt::MemoryWritePointersWrapperName},
237 {FAs.ReadUInt8s, rt::MemoryReadUInt8sWrapperName},
238 {FAs.ReadUInt16s, rt::MemoryReadUInt16sWrapperName},
239 {FAs.ReadUInt32s, rt::MemoryReadUInt32sWrapperName},
240 {FAs.ReadUInt64s, rt::MemoryReadUInt64sWrapperName},
241 {FAs.ReadBuffers, rt::MemoryReadBuffersWrapperName},
242 {FAs.ReadStrings, rt::MemoryReadStringsWrapperName}}))
243 return std::move(Err);
245 return std::make_unique<EPCGenericMemoryAccess>(SREPC, FAs);
248Error SimpleRemoteEPC::sendMessage(SimpleRemoteEPCOpcode OpC,
uint64_t SeqNo,
249 ExecutorAddr TagAddr,
250 ArrayRef<char> ArgBytes) {
251 assert(OpC != SimpleRemoteEPCOpcode::Setup &&
252 "SimpleRemoteEPC sending Setup message? That's the wrong direction.");
255 dbgs() <<
"SimpleRemoteEPC::sendMessage: opc = ";
257 case SimpleRemoteEPCOpcode::Hangup:
259 assert(SeqNo == 0 &&
"Non-zero SeqNo for Hangup?");
260 assert(!TagAddr &&
"Non-zero TagAddr for Hangup?");
262 case SimpleRemoteEPCOpcode::Result:
264 assert(!TagAddr &&
"Non-zero TagAddr for Result?");
266 case SimpleRemoteEPCOpcode::CallWrapper:
267 dbgs() <<
"CallWrapper";
272 dbgs() <<
", seqno = " << SeqNo <<
", tag-addr = " << TagAddr
273 <<
", arg-buffer = " <<
formatv(
"{0:x}", ArgBytes.size())
276 auto Err =
T->sendMessage(OpC, SeqNo, TagAddr, ArgBytes);
279 dbgs() <<
" \\--> SimpleRemoteEPC::sendMessage failed\n";
284Error SimpleRemoteEPC::handleSetup(
uint64_t SeqNo, ExecutorAddr TagAddr,
285 SimpleRemoteEPCArgBytesVector ArgBytes) {
287 return make_error<StringError>(
"Setup packet SeqNo not zero",
291 return make_error<StringError>(
"Setup packet TagAddr not zero",
294 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
295 auto I = PendingCallWrapperResults.find(0);
296 assert(PendingCallWrapperResults.size() == 1 &&
297 I != PendingCallWrapperResults.end() &&
298 "Setup message handler not connectly set up");
299 auto SetupMsgHandler = std::move(
I->second);
300 PendingCallWrapperResults.erase(
I);
303 shared::WrapperFunctionResult::copyFrom(ArgBytes.data(), ArgBytes.size());
304 SetupMsgHandler(std::move(WFR));
305 return Error::success();
308Error SimpleRemoteEPC::setup(Setup S) {
309 using namespace SimpleRemoteEPCDefaultBootstrapSymbolNames;
311 std::promise<MSVCPExpected<SimpleRemoteEPCExecutorInfo>> EIP;
312 auto EIF = EIP.get_future();
315 PendingCallWrapperResults[0] =
317 [&](shared::WrapperFunctionResult SetupMsgBytes) {
318 if (
const char *ErrMsg = SetupMsgBytes.getOutOfBandError()) {
324 shared::SPSArgList<shared::SPSSimpleRemoteEPCExecutorInfo>;
325 shared::SPSInputBuffer
IB(SetupMsgBytes.data(), SetupMsgBytes.size());
326 SimpleRemoteEPCExecutorInfo EI;
327 if (SPSSerialize::deserialize(IB, EI))
330 EIP.set_value(make_error<StringError>(
335 if (
auto Err =
T->start())
342 return EI.takeError();
346 dbgs() <<
"SimpleRemoteEPC received setup message:\n"
347 <<
" Triple: " << EI->TargetTriple <<
"\n"
348 <<
" Page size: " << EI->PageSize <<
"\n"
349 <<
" Bootstrap map" << (EI->BootstrapMap.empty() ?
" empty" :
":")
351 for (
const auto &KV : EI->BootstrapMap)
352 dbgs() <<
" " << KV.first() <<
": " << KV.second.size()
353 <<
"-byte SPS encoded buffer\n";
354 dbgs() <<
" Bootstrap symbols"
355 << (EI->BootstrapSymbols.empty() ?
" empty" :
":") <<
"\n";
356 for (
const auto &KV : EI->BootstrapSymbols)
357 dbgs() <<
" " << KV.first() <<
": " << KV.second <<
"\n";
359 TargetTriple = Triple(EI->TargetTriple);
361 BootstrapMap = std::move(EI->BootstrapMap);
362 BootstrapSymbols = std::move(EI->BootstrapSymbols);
364 if (
auto Err = getBootstrapSymbols(
367 {RunAsMainAddr, rt::RunAsMainWrapperName},
368 {RunAsVoidFunctionAddr, rt::RunAsVoidFunctionWrapperName},
369 {RunAsIntFunctionAddr, rt::RunAsIntFunctionWrapperName}}))
373 EPCGenericDylibManager::CreateWithDefaultBootstrapSymbols(*
this))
374 EPCDylibMgr = std::make_unique<EPCGenericDylibManager>(std::move(*
DM));
376 return DM.takeError();
379 if (!S.CreateMemoryManager)
380 S.CreateMemoryManager = createDefaultMemoryManager;
382 if (
auto MemMgr = S.CreateMemoryManager(*
this)) {
383 OwnedMemMgr = std::move(*MemMgr);
384 this->MemMgr = OwnedMemMgr.get();
386 return MemMgr.takeError();
389 if (!S.CreateMemoryAccess)
390 S.CreateMemoryAccess = createDefaultMemoryAccess;
392 if (
auto MemAccess = S.CreateMemoryAccess(*
this)) {
393 OwnedMemAccess = std::move(*MemAccess);
394 this->MemAccess = OwnedMemAccess.get();
396 return MemAccess.takeError();
398 return Error::success();
401Error SimpleRemoteEPC::handleResult(
uint64_t SeqNo, ExecutorAddr TagAddr,
402 SimpleRemoteEPCArgBytesVector ArgBytes) {
403 IncomingWFRHandler SendResult;
406 return make_error<StringError>(
"Unexpected TagAddr in result message",
410 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
411 auto I = PendingCallWrapperResults.find(SeqNo);
412 if (
I == PendingCallWrapperResults.end())
413 return make_error<StringError>(
"No call for sequence number " +
416 SendResult = std::move(
I->second);
417 PendingCallWrapperResults.erase(
I);
422 shared::WrapperFunctionResult::copyFrom(ArgBytes.data(), ArgBytes.size());
423 SendResult(std::move(WFR));
424 return Error::success();
427void SimpleRemoteEPC::handleCallWrapper(
428 uint64_t RemoteSeqNo, ExecutorAddr TagAddr,
429 SimpleRemoteEPCArgBytesVector ArgBytes) {
430 assert(ES &&
"No ExecutionSession attached");
432 [
this, RemoteSeqNo, TagAddr, ArgBytes = std::move(ArgBytes)]() {
433 ES->runJITDispatchHandler(
434 [
this, RemoteSeqNo](shared::WrapperFunctionResult WFR) {
436 sendMessage(SimpleRemoteEPCOpcode::Result, RemoteSeqNo,
437 ExecutorAddr(), {WFR.data(), WFR.size()}))
442 "callWrapper task"));
445Error SimpleRemoteEPC::handleHangup(SimpleRemoteEPCArgBytesVector ArgBytes) {
447 auto WFR = WrapperFunctionResult::copyFrom(ArgBytes.data(), ArgBytes.size());
448 if (
const char *ErrMsg = WFR.getOutOfBandError())
454 return make_error<StringError>(
"Could not deserialize hangup info",
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static Error reportError(StringRef Message)
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Analysis containing CSE Info
static RegisterPass< DebugifyModulePass > DM("debugify", "Attach debug info to everything")
static cl::opt< int > PageSize("imp-null-check-page-size", cl::desc("The page size of the target in bytes"), cl::init(4096), cl::Hidden)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
const T & front() const
front - Get the first element.
bool empty() const
empty - Check if the array is empty.
Lightweight error class with error context and mandatory checking.
Tagged union holding either a T or a Error.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
LLVM_ABI void lookupAsync(tpctypes::DylibHandle H, const SymbolLookupSet &Lookup, SymbolLookupCompleteFn Complete)
Looks up symbols within the given dylib.
Represents an address in the executor process.
A handler or incoming WrapperFunctionResults – either return values from callWrapper* calls,...
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...
A utility class for serializing to a blob from a variadic list.
unique_function is a type-erasing functor similar to std::function.
#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
Error fromSPSSerializable(SPSSerializableError BSE)
std::unique_ptr< GenericNamedTask > makeGenericNamedTask(FnT &&Fn, std::string Desc)
Create a generic named task from a std::string description.
static void lookupSymbolsAsyncHelper(EPCGenericDylibManager &DylibMgr, ArrayRef< DylibManager::LookupRequest > Request, std::vector< tpctypes::LookupResult > Result, DylibManager::SymbolLookupCompleteFn Complete)
Async helper to chain together calls to DylibMgr::lookupAsync to fulfill all all the requests.
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...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
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.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Function addresses for memory access.
Helper type for serializing Errors.