17#define DEBUG_TYPE "orc"
28#if LLVM_ENABLE_THREADS
29void SimpleRemoteEPCServer::ThreadDispatcher::dispatch(
32 std::lock_guard<std::mutex> Lock(DispatchMutex);
38 std::thread([
this, Work = std::move(Work)]()
mutable {
40 std::lock_guard<std::mutex> Lock(DispatchMutex);
42 OutstandingCV.notify_all();
46void SimpleRemoteEPCServer::ThreadDispatcher::shutdown() {
47 std::unique_lock<std::mutex> Lock(DispatchMutex);
49 OutstandingCV.wait(Lock, [
this]() {
return Outstanding == 0; });
65 dbgs() <<
"SimpleRemoteEPCServer::handleMessage: opc = ";
69 assert(SeqNo == 0 &&
"Non-zero SeqNo for Setup?");
70 assert(!TagAddr &&
"Non-zero TagAddr for Setup?");
74 assert(SeqNo == 0 &&
"Non-zero SeqNo for Hangup?");
75 assert(!TagAddr &&
"Non-zero TagAddr for Hangup?");
79 assert(!TagAddr &&
"Non-zero TagAddr for Result?");
82 dbgs() <<
"CallWrapper";
85 dbgs() <<
", seqno = " << SeqNo <<
", tag-addr = " << TagAddr
86 <<
", arg-buffer = " <<
formatv(
"{0:x}", ArgBytes.
size())
90 using UT = std::underlying_type_t<SimpleRemoteEPCOpcode>;
102 std::lock_guard<std::mutex> Lock(ServerStateMutex);
106 return std::move(Err);
110 if (
auto Err = handleResult(SeqNo, TagAddr, std::move(ArgBytes)))
111 return std::move(Err);
114 handleCallWrapper(SeqNo, TagAddr, std::move(ArgBytes));
121 std::unique_lock<std::mutex> Lock(ServerStateMutex);
122 ShutdownCV.wait(Lock, [
this]() {
return RunState == ServerShutDown; });
123 return std::move(ShutdownErr);
127 PendingJITDispatchResultsMap TmpPending;
130 std::lock_guard<std::mutex> Lock(ServerStateMutex);
131 std::swap(TmpPending, PendingJITDispatchResults);
132 RunState = ServerShuttingDown;
136 for (
auto &KV : TmpPending)
137 KV.second->set_value(
144 while (!Services.empty()) {
146 joinErrors(std::move(ShutdownErr), Services.back()->shutdown());
150 std::lock_guard<std::mutex> Lock(ServerStateMutex);
164 Error DisconnectReason =
165 (!Err && !RemoteHangup)
170 ShutdownErr =
joinErrors(std::move(ShutdownErr), std::move(DisconnectReason));
171 RunState = ServerShutDown;
172 ShutdownCV.notify_all();
180 dbgs() <<
"SimpleRemoteEPCServer::sendMessage: opc = ";
184 assert(SeqNo == 0 &&
"Non-zero SeqNo for Setup?");
185 assert(!TagAddr &&
"Non-zero TagAddr for Setup?");
189 assert(SeqNo == 0 &&
"Non-zero SeqNo for Hangup?");
190 assert(!TagAddr &&
"Non-zero TagAddr for Hangup?");
194 assert(!TagAddr &&
"Non-zero TagAddr for Result?");
197 dbgs() <<
"CallWrapper";
200 dbgs() <<
", seqno = " << SeqNo <<
", tag-addr = " << TagAddr
201 <<
", arg-buffer = " <<
formatv(
"{0:x}", ArgBytes.
size())
204 auto Err =
T->sendMessage(OpC, SeqNo, TagAddr, ArgBytes);
207 dbgs() <<
" \\--> SimpleRemoteEPC::sendMessage failed\n";
212Error SimpleRemoteEPCServer::sendSetupMessage(
213 StringMap<std::vector<char>> BootstrapMap,
214 StringMap<ExecutorAddr> BootstrapSymbols) {
216 using namespace SimpleRemoteEPCDefaultBootstrapSymbolNames;
218 SimpleRemoteEPCExecutorInfo EI;
228 "Dispatch context name should not be set");
230 "Dispatch function name should not be set");
236 shared::SPSArgList<shared::SPSSimpleRemoteEPCExecutorInfo>;
237 auto SetupPacketBytes =
239 shared::SPSOutputBuffer
OB(SetupPacketBytes.data(), SetupPacketBytes.size());
240 if (!SPSSerialize::serialize(OB, EI))
245 {SetupPacketBytes.data(), SetupPacketBytes.size()});
248Error SimpleRemoteEPCServer::handleResult(
251 std::promise<shared::WrapperFunctionBuffer> *
P =
nullptr;
253 std::lock_guard<std::mutex> Lock(ServerStateMutex);
254 auto I = PendingJITDispatchResults.find(SeqNo);
255 if (
I == PendingJITDispatchResults.end())
260 PendingJITDispatchResults.erase(
I);
264 memcpy(
R.data(), ArgBytes.
data(), ArgBytes.
size());
265 P->set_value(std::move(R));
269void SimpleRemoteEPCServer::handleCallWrapper(
272 D->dispatch([
this, RemoteSeqNo, TagAddr, ArgBytes = std::move(ArgBytes)]() {
274 shared::CWrapperFunctionBuffer (*)(
const char *, size_t);
275 auto *Fn = TagAddr.
toPtr<WrapperFnTy>();
276 shared::WrapperFunctionBuffer ResultBytes(
277 Fn(ArgBytes.
data(), ArgBytes.
size()));
280 {ResultBytes.data(), ResultBytes.size()}))
281 ReportError(std::move(Err));
286SimpleRemoteEPCServer::doJITDispatch(
const void *FnTag,
const char *ArgData,
289 std::promise<shared::WrapperFunctionBuffer> ResultP;
290 auto ResultF = ResultP.get_future();
292 std::lock_guard<std::mutex> Lock(ServerStateMutex);
293 if (RunState != ServerRunning)
295 "jit_dispatch not available (EPC server shut down)");
297 SeqNo = getNextSeqNo();
298 assert(!PendingJITDispatchResults.count(SeqNo) &&
"SeqNo already in use");
299 PendingJITDispatchResults[SeqNo] = &ResultP;
304 ReportError(std::move(Err));
306 return ResultF.get();
310SimpleRemoteEPCServer::jitDispatchEntry(
void *DispatchCtx,
const void *FnTag,
311 const char *ArgData,
size_t ArgSize) {
312 return reinterpret_cast<SimpleRemoteEPCServer *
>(DispatchCtx)
313 ->doJITDispatch(FnTag, ArgData, ArgSize)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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)
Provides a library for accessing information about this process and other processes on the operating ...
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.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Represents an address in the executor process.
static ExecutorAddr fromPtr(T *Ptr, UnwrapFn &&Unwrap=UnwrapFn())
Create an ExecutorAddr from the given pointer.
std::enable_if_t< std::is_pointer< T >::value, T > toPtr(WrapFn &&Wrap=WrapFn()) const
Cast this ExecutorAddr to a pointer of the given type.
virtual ~ExecutorBootstrapService()
static StringMap< ExecutorAddr > defaultBootstrapSymbols()
void handleDisconnect(Error Err) override
Handle a disconnection from the underlying transport.
Expected< HandleMessageAction > handleMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo, ExecutorAddr TagAddr, shared::WrapperFunctionBuffer ArgBytes) override
Call to handle an incoming message.
Error waitForDisconnect()
C++ wrapper function buffer: Same as CWrapperFunctionBuffer but auto-releases memory.
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.
char * data()
Get a pointer to the data contained in this instance.
static WrapperFunctionBuffer allocate(size_t Size)
Create a WrapperFunctionBuffer with the given size and return a pointer to the underlying memory.
static LLVM_ABI Expected< unsigned > getPageSize()
Get the process's page size.
unique_function is a type-erasing functor similar to std::function.
@ OB
OB - OneByte - Set if this instruction has a one byte opcode.
LLVM_ABI const char * DispatchFnName
LLVM_ABI const char * ExecutorSessionObjectName
void addTo(StringMap< ExecutorAddr > &M)
Adds all default target-process bootstrap wrappers.
LLVM_ABI void addDefaultBootstrapValuesForHostProcess(StringMap< std::vector< char > > &BootstrapMap, StringMap< ExecutorAddr > &BootstrapSymbols)
LLVM_ABI Error decodeHangupPayload(shared::WrapperFunctionBuffer Payload)
Decode a Hangup payload produced by encodeHangupPayload.
LLVM_ABI std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
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.
StringMap< ExecutorAddr > BootstrapSymbols
StringMap< std::vector< char > > BootstrapMap