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());
59void SimpleRemoteEPC::lookupSymbolsAsync(ArrayRef<LookupRequest> Request,
60 SymbolLookupCompleteFn Complete) {
61 lookupSymbolsAsyncHelper(*EPCDylibMgr, Request, {}, std::move(Complete));
67 if (
auto Err = callSPSWrapper<rt::SPSRunAsMainSignature>(
68 RunAsMainAddr, Result, MainFnAddr, Args))
69 return std::move(Err);
75 if (
auto Err = callSPSWrapper<rt::SPSRunAsVoidFunctionSignature>(
76 RunAsVoidFunctionAddr, Result, VoidFnAddr))
77 return std::move(Err);
84 if (
auto Err = callSPSWrapper<rt::SPSRunAsIntFunctionSignature>(
85 RunAsIntFunctionAddr, Result, IntFnAddr, Arg))
86 return std::move(Err);
95 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
96 SeqNo = getNextSeqNo();
97 assert(!PendingCallWrapperResults.count(SeqNo) &&
"SeqNo already in use");
98 PendingCallWrapperResults[SeqNo] = std::move(OnComplete);
101 if (
auto Err = sendMessage(SimpleRemoteEPCOpcode::CallWrapper, SeqNo,
102 WrapperFnAddr, ArgBuffer)) {
111 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
112 auto I = PendingCallWrapperResults.find(SeqNo);
113 if (
I != PendingCallWrapperResults.end()) {
114 H = std::move(
I->second);
115 PendingCallWrapperResults.erase(
I);
120 H(shared::WrapperFunctionResult::createOutOfBandError(
"disconnecting"));
122 getExecutionSession().reportError(std::move(Err));
126Error SimpleRemoteEPC::disconnect() {
129 std::unique_lock<std::mutex> Lock(SimpleRemoteEPCMutex);
130 DisconnectCV.wait(Lock, [
this] {
return Disconnected; });
131 return std::move(DisconnectErr);
140 dbgs() <<
"SimpleRemoteEPC::handleMessage: opc = ";
142 case SimpleRemoteEPCOpcode::Setup:
144 assert(SeqNo == 0 &&
"Non-zero SeqNo for Setup?");
145 assert(!TagAddr &&
"Non-zero TagAddr for Setup?");
147 case SimpleRemoteEPCOpcode::Hangup:
149 assert(SeqNo == 0 &&
"Non-zero SeqNo for Hangup?");
150 assert(!TagAddr &&
"Non-zero TagAddr for Hangup?");
152 case SimpleRemoteEPCOpcode::Result:
154 assert(!TagAddr &&
"Non-zero TagAddr for Result?");
156 case SimpleRemoteEPCOpcode::CallWrapper:
157 dbgs() <<
"CallWrapper";
160 dbgs() <<
", seqno = " << SeqNo <<
", tag-addr = " << TagAddr
161 <<
", arg-buffer = " <<
formatv(
"{0:x}", ArgBytes.
size())
165 using UT = std::underlying_type_t<SimpleRemoteEPCOpcode>;
166 if (
static_cast<UT
>(OpC) >
static_cast<UT
>(SimpleRemoteEPCOpcode::LastOpC))
167 return make_error<StringError>(
"Unexpected opcode",
171 case SimpleRemoteEPCOpcode::Setup:
172 if (
auto Err = handleSetup(SeqNo, TagAddr, std::move(ArgBytes)))
173 return std::move(Err);
175 case SimpleRemoteEPCOpcode::Hangup:
177 if (
auto Err = handleHangup(std::move(ArgBytes)))
178 return std::move(Err);
180 case SimpleRemoteEPCOpcode::Result:
181 if (
auto Err = handleResult(SeqNo, TagAddr, std::move(ArgBytes)))
182 return std::move(Err);
184 case SimpleRemoteEPCOpcode::CallWrapper:
185 handleCallWrapper(SeqNo, TagAddr, std::move(ArgBytes));
188 return ContinueSession;
191void SimpleRemoteEPC::handleDisconnect(
Error Err) {
193 dbgs() <<
"SimpleRemoteEPC::handleDisconnect: "
194 << (Err ?
"failure" :
"success") <<
"\n";
200 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
201 std::swap(TmpPending, PendingCallWrapperResults);
204 for (
auto &KV : TmpPending)
206 shared::WrapperFunctionResult::createOutOfBandError(
"disconnecting"));
208 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
209 DisconnectErr =
joinErrors(std::move(DisconnectErr), std::move(Err));
211 DisconnectCV.notify_all();
218 {{SAs.Allocator, rt::SimpleExecutorMemoryManagerInstanceName},
219 {SAs.Reserve, rt::SimpleExecutorMemoryManagerReserveWrapperName},
220 {SAs.Finalize, rt::SimpleExecutorMemoryManagerFinalizeWrapperName},
222 rt::SimpleExecutorMemoryManagerDeallocateWrapperName}}))
223 return std::move(Err);
225 return std::make_unique<EPCGenericJITLinkMemoryManager>(SREPC, SAs);
228Expected<std::unique_ptr<ExecutorProcessControl::MemoryAccess>>
229SimpleRemoteEPC::createDefaultMemoryAccess(SimpleRemoteEPC &SREPC) {
230 EPCGenericMemoryAccess::FuncAddrs FAs;
231 if (
auto Err = SREPC.getBootstrapSymbols(
232 {{FAs.WriteUInt8s, rt::MemoryWriteUInt8sWrapperName},
233 {FAs.WriteUInt16s, rt::MemoryWriteUInt16sWrapperName},
234 {FAs.WriteUInt32s, rt::MemoryWriteUInt32sWrapperName},
235 {FAs.WriteUInt64s, rt::MemoryWriteUInt64sWrapperName},
236 {FAs.WriteBuffers, rt::MemoryWriteBuffersWrapperName},
237 {FAs.WritePointers, rt::MemoryWritePointersWrapperName}}))
238 return std::move(Err);
240 return std::make_unique<EPCGenericMemoryAccess>(SREPC, FAs);
243Error SimpleRemoteEPC::sendMessage(SimpleRemoteEPCOpcode OpC,
uint64_t SeqNo,
244 ExecutorAddr TagAddr,
245 ArrayRef<char> ArgBytes) {
246 assert(OpC != SimpleRemoteEPCOpcode::Setup &&
247 "SimpleRemoteEPC sending Setup message? That's the wrong direction.");
250 dbgs() <<
"SimpleRemoteEPC::sendMessage: opc = ";
252 case SimpleRemoteEPCOpcode::Hangup:
254 assert(SeqNo == 0 &&
"Non-zero SeqNo for Hangup?");
255 assert(!TagAddr &&
"Non-zero TagAddr for Hangup?");
257 case SimpleRemoteEPCOpcode::Result:
259 assert(!TagAddr &&
"Non-zero TagAddr for Result?");
261 case SimpleRemoteEPCOpcode::CallWrapper:
262 dbgs() <<
"CallWrapper";
267 dbgs() <<
", seqno = " << SeqNo <<
", tag-addr = " << TagAddr
268 <<
", arg-buffer = " <<
formatv(
"{0:x}", ArgBytes.size())
271 auto Err =
T->sendMessage(OpC, SeqNo, TagAddr, ArgBytes);
274 dbgs() <<
" \\--> SimpleRemoteEPC::sendMessage failed\n";
279Error SimpleRemoteEPC::handleSetup(
uint64_t SeqNo, ExecutorAddr TagAddr,
280 SimpleRemoteEPCArgBytesVector ArgBytes) {
282 return make_error<StringError>(
"Setup packet SeqNo not zero",
286 return make_error<StringError>(
"Setup packet TagAddr not zero",
289 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
290 auto I = PendingCallWrapperResults.find(0);
291 assert(PendingCallWrapperResults.size() == 1 &&
292 I != PendingCallWrapperResults.end() &&
293 "Setup message handler not connectly set up");
294 auto SetupMsgHandler = std::move(
I->second);
295 PendingCallWrapperResults.erase(
I);
298 shared::WrapperFunctionResult::copyFrom(ArgBytes.data(), ArgBytes.size());
299 SetupMsgHandler(std::move(WFR));
300 return Error::success();
303Error SimpleRemoteEPC::setup(Setup S) {
304 using namespace SimpleRemoteEPCDefaultBootstrapSymbolNames;
306 std::promise<MSVCPExpected<SimpleRemoteEPCExecutorInfo>> EIP;
307 auto EIF = EIP.get_future();
310 PendingCallWrapperResults[0] =
312 [&](shared::WrapperFunctionResult SetupMsgBytes) {
313 if (
const char *ErrMsg = SetupMsgBytes.getOutOfBandError()) {
319 shared::SPSArgList<shared::SPSSimpleRemoteEPCExecutorInfo>;
320 shared::SPSInputBuffer
IB(SetupMsgBytes.data(), SetupMsgBytes.size());
321 SimpleRemoteEPCExecutorInfo EI;
322 if (SPSSerialize::deserialize(IB, EI))
325 EIP.set_value(make_error<StringError>(
330 if (
auto Err =
T->start())
337 return EI.takeError();
341 dbgs() <<
"SimpleRemoteEPC received setup message:\n"
342 <<
" Triple: " << EI->TargetTriple <<
"\n"
343 <<
" Page size: " << EI->PageSize <<
"\n"
344 <<
" Bootstrap map" << (EI->BootstrapMap.empty() ?
" empty" :
":")
346 for (
const auto &KV : EI->BootstrapMap)
347 dbgs() <<
" " << KV.first() <<
": " << KV.second.size()
348 <<
"-byte SPS encoded buffer\n";
349 dbgs() <<
" Bootstrap symbols"
350 << (EI->BootstrapSymbols.empty() ?
" empty" :
":") <<
"\n";
351 for (
const auto &KV : EI->BootstrapSymbols)
352 dbgs() <<
" " << KV.first() <<
": " << KV.second <<
"\n";
354 TargetTriple = Triple(EI->TargetTriple);
356 BootstrapMap = std::move(EI->BootstrapMap);
357 BootstrapSymbols = std::move(EI->BootstrapSymbols);
359 if (
auto Err = getBootstrapSymbols(
362 {RunAsMainAddr, rt::RunAsMainWrapperName},
363 {RunAsVoidFunctionAddr, rt::RunAsVoidFunctionWrapperName},
364 {RunAsIntFunctionAddr, rt::RunAsIntFunctionWrapperName}}))
368 EPCGenericDylibManager::CreateWithDefaultBootstrapSymbols(*
this))
369 EPCDylibMgr = std::make_unique<EPCGenericDylibManager>(std::move(*
DM));
371 return DM.takeError();
374 if (!S.CreateMemoryManager)
375 S.CreateMemoryManager = createDefaultMemoryManager;
377 if (
auto MemMgr = S.CreateMemoryManager(*
this)) {
378 OwnedMemMgr = std::move(*MemMgr);
379 this->MemMgr = OwnedMemMgr.get();
381 return MemMgr.takeError();
384 if (!S.CreateMemoryAccess)
385 S.CreateMemoryAccess = createDefaultMemoryAccess;
387 if (
auto MemAccess = S.CreateMemoryAccess(*
this)) {
388 OwnedMemAccess = std::move(*MemAccess);
389 this->MemAccess = OwnedMemAccess.get();
391 return MemAccess.takeError();
393 return Error::success();
396Error SimpleRemoteEPC::handleResult(
uint64_t SeqNo, ExecutorAddr TagAddr,
397 SimpleRemoteEPCArgBytesVector ArgBytes) {
398 IncomingWFRHandler SendResult;
401 return make_error<StringError>(
"Unexpected TagAddr in result message",
405 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
406 auto I = PendingCallWrapperResults.find(SeqNo);
407 if (
I == PendingCallWrapperResults.end())
408 return make_error<StringError>(
"No call for sequence number " +
411 SendResult = std::move(
I->second);
412 PendingCallWrapperResults.erase(
I);
417 shared::WrapperFunctionResult::copyFrom(ArgBytes.data(), ArgBytes.size());
418 SendResult(std::move(WFR));
419 return Error::success();
422void SimpleRemoteEPC::handleCallWrapper(
423 uint64_t RemoteSeqNo, ExecutorAddr TagAddr,
424 SimpleRemoteEPCArgBytesVector ArgBytes) {
425 assert(ES &&
"No ExecutionSession attached");
427 [
this, RemoteSeqNo, TagAddr, ArgBytes = std::move(ArgBytes)]() {
428 ES->runJITDispatchHandler(
429 [
this, RemoteSeqNo](shared::WrapperFunctionResult WFR) {
431 sendMessage(SimpleRemoteEPCOpcode::Result, RemoteSeqNo,
432 ExecutorAddr(), {WFR.data(), WFR.size()}))
437 "callWrapper task"));
440Error SimpleRemoteEPC::handleHangup(SimpleRemoteEPCArgBytesVector ArgBytes) {
442 auto WFR = WrapperFunctionResult::copyFrom(ArgBytes.data(), ArgBytes.size());
443 if (
const char *ErrMsg = WFR.getOutOfBandError())
449 return make_error<StringError>(
"Could not deserialize hangup info",
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)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
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.
const char * DispatchFnName
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.
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.
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.