LLVM 22.0.0git
SimpleRemoteEPCServer.cpp
Go to the documentation of this file.
1//===------- SimpleEPCServer.cpp - EPC over simple abstract channel -------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
16
17#include "OrcRTBootstrap.h"
18
19#define DEBUG_TYPE "orc"
20
21using namespace llvm::orc::shared;
22
23namespace llvm {
24namespace orc {
25
27
29
30#if LLVM_ENABLE_THREADS
31void SimpleRemoteEPCServer::ThreadDispatcher::dispatch(
32 unique_function<void()> Work) {
33 {
34 std::lock_guard<std::mutex> Lock(DispatchMutex);
35 if (!Running)
36 return;
37 ++Outstanding;
38 }
39
40 std::thread([this, Work = std::move(Work)]() mutable {
41 Work();
42 std::lock_guard<std::mutex> Lock(DispatchMutex);
43 --Outstanding;
44 OutstandingCV.notify_all();
45 }).detach();
46}
47
48void SimpleRemoteEPCServer::ThreadDispatcher::shutdown() {
49 std::unique_lock<std::mutex> Lock(DispatchMutex);
50 Running = false;
51 OutstandingCV.wait(Lock, [this]() { return Outstanding == 0; });
52}
53#endif
54
58 return DBS;
59}
60
63 ExecutorAddr TagAddr,
65
67 dbgs() << "SimpleRemoteEPCServer::handleMessage: opc = ";
68 switch (OpC) {
70 dbgs() << "Setup";
71 assert(SeqNo == 0 && "Non-zero SeqNo for Setup?");
72 assert(!TagAddr && "Non-zero TagAddr for Setup?");
73 break;
75 dbgs() << "Hangup";
76 assert(SeqNo == 0 && "Non-zero SeqNo for Hangup?");
77 assert(!TagAddr && "Non-zero TagAddr for Hangup?");
78 break;
80 dbgs() << "Result";
81 assert(!TagAddr && "Non-zero TagAddr for Result?");
82 break;
84 dbgs() << "CallWrapper";
85 break;
86 }
87 dbgs() << ", seqno = " << SeqNo << ", tag-addr = " << TagAddr
88 << ", arg-buffer = " << formatv("{0:x}", ArgBytes.size())
89 << " bytes\n";
90 });
91
92 using UT = std::underlying_type_t<SimpleRemoteEPCOpcode>;
93 if (static_cast<UT>(OpC) > static_cast<UT>(SimpleRemoteEPCOpcode::LastOpC))
94 return make_error<StringError>("Unexpected opcode",
96
97 // TODO: Clean detach message?
98 switch (OpC) {
100 return make_error<StringError>("Unexpected Setup opcode",
105 if (auto Err = handleResult(SeqNo, TagAddr, std::move(ArgBytes)))
106 return std::move(Err);
107 break;
109 handleCallWrapper(SeqNo, TagAddr, std::move(ArgBytes));
110 break;
111 }
112 return ContinueSession;
113}
114
116 std::unique_lock<std::mutex> Lock(ServerStateMutex);
117 ShutdownCV.wait(Lock, [this]() { return RunState == ServerShutDown; });
118 return std::move(ShutdownErr);
119}
120
123
124 {
125 std::lock_guard<std::mutex> Lock(ServerStateMutex);
126 std::swap(TmpPending, PendingJITDispatchResults);
127 RunState = ServerShuttingDown;
128 }
129
130 // Send out-of-band errors to any waiting threads.
131 for (auto &KV : TmpPending)
132 KV.second->set_value(
134
135 // Wait for dispatcher to clear.
136 D->shutdown();
137
138 // Shut down services.
139 while (!Services.empty()) {
140 ShutdownErr =
141 joinErrors(std::move(ShutdownErr), Services.back()->shutdown());
142 Services.pop_back();
143 }
144
145 std::lock_guard<std::mutex> Lock(ServerStateMutex);
146 ShutdownErr = joinErrors(std::move(ShutdownErr), std::move(Err));
147 RunState = ServerShutDown;
148 ShutdownCV.notify_all();
149}
150
151Error SimpleRemoteEPCServer::sendMessage(SimpleRemoteEPCOpcode OpC,
152 uint64_t SeqNo, ExecutorAddr TagAddr,
153 ArrayRef<char> ArgBytes) {
154
155 LLVM_DEBUG({
156 dbgs() << "SimpleRemoteEPCServer::sendMessage: opc = ";
157 switch (OpC) {
159 dbgs() << "Setup";
160 assert(SeqNo == 0 && "Non-zero SeqNo for Setup?");
161 assert(!TagAddr && "Non-zero TagAddr for Setup?");
162 break;
164 dbgs() << "Hangup";
165 assert(SeqNo == 0 && "Non-zero SeqNo for Hangup?");
166 assert(!TagAddr && "Non-zero TagAddr for Hangup?");
167 break;
169 dbgs() << "Result";
170 assert(!TagAddr && "Non-zero TagAddr for Result?");
171 break;
173 dbgs() << "CallWrapper";
174 break;
175 }
176 dbgs() << ", seqno = " << SeqNo << ", tag-addr = " << TagAddr
177 << ", arg-buffer = " << formatv("{0:x}", ArgBytes.size())
178 << " bytes\n";
179 });
180 auto Err = T->sendMessage(OpC, SeqNo, TagAddr, ArgBytes);
181 LLVM_DEBUG({
182 if (Err)
183 dbgs() << " \\--> SimpleRemoteEPC::sendMessage failed\n";
184 });
185 return Err;
186}
187
188Error SimpleRemoteEPCServer::sendSetupMessage(
189 StringMap<std::vector<char>> BootstrapMap,
190 StringMap<ExecutorAddr> BootstrapSymbols) {
191
192 using namespace SimpleRemoteEPCDefaultBootstrapSymbolNames;
193
197 EI.PageSize = *PageSize;
198 else
199 return PageSize.takeError();
200 EI.BootstrapMap = std::move(BootstrapMap);
201 EI.BootstrapSymbols = std::move(BootstrapSymbols);
202
203 assert(!EI.BootstrapSymbols.count(ExecutorSessionObjectName) &&
204 "Dispatch context name should not be set");
205 assert(!EI.BootstrapSymbols.count(DispatchFnName) &&
206 "Dispatch function name should not be set");
213
214 using SPSSerialize =
216 auto SetupPacketBytes =
217 shared::WrapperFunctionResult::allocate(SPSSerialize::size(EI));
218 shared::SPSOutputBuffer OB(SetupPacketBytes.data(), SetupPacketBytes.size());
219 if (!SPSSerialize::serialize(OB, EI))
220 return make_error<StringError>("Could not send setup packet",
222
223 return sendMessage(SimpleRemoteEPCOpcode::Setup, 0, ExecutorAddr(),
224 {SetupPacketBytes.data(), SetupPacketBytes.size()});
225}
226
227Error SimpleRemoteEPCServer::handleResult(
228 uint64_t SeqNo, ExecutorAddr TagAddr,
230 std::promise<shared::WrapperFunctionResult> *P = nullptr;
231 {
232 std::lock_guard<std::mutex> Lock(ServerStateMutex);
233 auto I = PendingJITDispatchResults.find(SeqNo);
234 if (I == PendingJITDispatchResults.end())
235 return make_error<StringError>("No call for sequence number " +
236 Twine(SeqNo),
238 P = I->second;
239 PendingJITDispatchResults.erase(I);
240 releaseSeqNo(SeqNo);
241 }
243 memcpy(R.data(), ArgBytes.data(), ArgBytes.size());
244 P->set_value(std::move(R));
245 return Error::success();
246}
247
248void SimpleRemoteEPCServer::handleCallWrapper(
249 uint64_t RemoteSeqNo, ExecutorAddr TagAddr,
251 D->dispatch([this, RemoteSeqNo, TagAddr, ArgBytes = std::move(ArgBytes)]() {
252 using WrapperFnTy =
253 shared::CWrapperFunctionResult (*)(const char *, size_t);
254 auto *Fn = TagAddr.toPtr<WrapperFnTy>();
256 Fn(ArgBytes.data(), ArgBytes.size()));
257 if (auto Err = sendMessage(SimpleRemoteEPCOpcode::Result, RemoteSeqNo,
258 ExecutorAddr(),
259 {ResultBytes.data(), ResultBytes.size()}))
260 ReportError(std::move(Err));
261 });
262}
263
265SimpleRemoteEPCServer::doJITDispatch(const void *FnTag, const char *ArgData,
266 size_t ArgSize) {
267 uint64_t SeqNo;
268 std::promise<shared::WrapperFunctionResult> ResultP;
269 auto ResultF = ResultP.get_future();
270 {
271 std::lock_guard<std::mutex> Lock(ServerStateMutex);
272 if (RunState != ServerRunning)
274 "jit_dispatch not available (EPC server shut down)");
275
276 SeqNo = getNextSeqNo();
277 assert(!PendingJITDispatchResults.count(SeqNo) && "SeqNo already in use");
278 PendingJITDispatchResults[SeqNo] = &ResultP;
279 }
280
281 if (auto Err = sendMessage(SimpleRemoteEPCOpcode::CallWrapper, SeqNo,
282 ExecutorAddr::fromPtr(FnTag), {ArgData, ArgSize}))
283 ReportError(std::move(Err));
284
285 return ResultF.get();
286}
287
289SimpleRemoteEPCServer::jitDispatchEntry(void *DispatchCtx, const void *FnTag,
290 const char *ArgData, size_t ArgSize) {
291 return reinterpret_cast<SimpleRemoteEPCServer *>(DispatchCtx)
292 ->doJITDispatch(FnTag, ArgData, ArgSize)
293 .release();
294}
295
296} // end namespace orc
297} // end namespace llvm
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)
#define I(x, y, z)
Definition: MD5.cpp:58
#define P(N)
Provides a library for accessing information about this process and other processes on the operating ...
LLVM_ABI llvm::orc::shared::CWrapperFunctionResult llvm_orc_deregisterEHFrameSectionAllocAction(const char *ArgData, size_t ArgSize)
LLVM_ABI llvm::orc::shared::CWrapperFunctionResult llvm_orc_registerEHFrameSectionAllocAction(const char *ArgData, size_t ArgSize)
#define LLVM_DEBUG(...)
Definition: Debug.h:119
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:147
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:177
bool erase(const KeyT &Val)
Definition: DenseMap.h:319
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:173
iterator end()
Definition: DenseMap.h:87
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
static ErrorSuccess success()
Create a success value.
Definition: Error.h:336
Tagged union holding either a T or a Error.
Definition: Error.h:485
size_t size() const
Definition: SmallVector.h:79
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:287
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:133
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
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.
A simple EPC server implementation.
static StringMap< ExecutorAddr > defaultBootstrapSymbols()
Expected< HandleMessageAction > handleMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo, ExecutorAddr TagAddr, SimpleRemoteEPCArgBytesVector ArgBytes) override
Call to handle an incoming message.
void handleDisconnect(Error Err) override
Handle a disconnection from the underlying transport.
A utility class for serializing to a blob from a variadic list.
Output char buffer with overflow check.
C++ wrapper function result: Same as CWrapperFunctionResult but auto-releases memory.
static WrapperFunctionResult allocate(size_t Size)
Create a WrapperFunctionResult with the given size and return a pointer to the underlying memory.
CWrapperFunctionResult release()
Release ownership of the contained CWrapperFunctionResult.
static WrapperFunctionResult createOutOfBandError(const char *Msg)
Create an out-of-band error by copying the given string.
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.
Definition: X86BaseInfo.h:732
void addTo(StringMap< ExecutorAddr > &M)
LLVM_ABI const char * DeregisterEHFrameSectionAllocActionName
Definition: OrcRTBridge.cpp:72
LLVM_ABI const char * RegisterEHFrameSectionAllocActionName
Definition: OrcRTBridge.cpp:70
LLVM_ABI std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
Definition: Host.cpp:2434
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:442
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:858
StringMap< ExecutorAddr > BootstrapSymbols
StringMap< std::vector< char > > BootstrapMap