LLVM 17.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
15
16#include "OrcRTBootstrap.h"
17
18#define DEBUG_TYPE "orc"
19
20using namespace llvm::orc::shared;
21
22namespace llvm {
23namespace orc {
24
26
28
29#if LLVM_ENABLE_THREADS
30void SimpleRemoteEPCServer::ThreadDispatcher::dispatch(
31 unique_function<void()> Work) {
32 {
33 std::lock_guard<std::mutex> Lock(DispatchMutex);
34 if (!Running)
35 return;
36 ++Outstanding;
37 }
38
39 std::thread([this, Work = std::move(Work)]() mutable {
40 Work();
41 std::lock_guard<std::mutex> Lock(DispatchMutex);
42 --Outstanding;
43 OutstandingCV.notify_all();
44 }).detach();
45}
46
47void SimpleRemoteEPCServer::ThreadDispatcher::shutdown() {
48 std::unique_lock<std::mutex> Lock(DispatchMutex);
49 Running = false;
50 OutstandingCV.wait(Lock, [this]() { return Outstanding == 0; });
51}
52#endif
53
57 return DBS;
58}
59
62 ExecutorAddr TagAddr,
64
66 dbgs() << "SimpleRemoteEPCServer::handleMessage: opc = ";
67 switch (OpC) {
69 dbgs() << "Setup";
70 assert(SeqNo == 0 && "Non-zero SeqNo for Setup?");
71 assert(!TagAddr && "Non-zero TagAddr for Setup?");
72 break;
74 dbgs() << "Hangup";
75 assert(SeqNo == 0 && "Non-zero SeqNo for Hangup?");
76 assert(!TagAddr && "Non-zero TagAddr for Hangup?");
77 break;
79 dbgs() << "Result";
80 assert(!TagAddr && "Non-zero TagAddr for Result?");
81 break;
83 dbgs() << "CallWrapper";
84 break;
85 }
86 dbgs() << ", seqno = " << SeqNo << ", tag-addr = " << TagAddr
87 << ", arg-buffer = " << formatv("{0:x}", ArgBytes.size())
88 << " bytes\n";
89 });
90
91 using UT = std::underlying_type_t<SimpleRemoteEPCOpcode>;
92 if (static_cast<UT>(OpC) > static_cast<UT>(SimpleRemoteEPCOpcode::LastOpC))
93 return make_error<StringError>("Unexpected opcode",
95
96 // TODO: Clean detach message?
97 switch (OpC) {
99 return make_error<StringError>("Unexpected Setup opcode",
104 if (auto Err = handleResult(SeqNo, TagAddr, std::move(ArgBytes)))
105 return std::move(Err);
106 break;
108 handleCallWrapper(SeqNo, TagAddr, std::move(ArgBytes));
109 break;
110 }
111 return ContinueSession;
112}
113
115 std::unique_lock<std::mutex> Lock(ServerStateMutex);
116 ShutdownCV.wait(Lock, [this]() { return RunState == ServerShutDown; });
117 return std::move(ShutdownErr);
118}
119
122
123 {
124 std::lock_guard<std::mutex> Lock(ServerStateMutex);
125 std::swap(TmpPending, PendingJITDispatchResults);
126 RunState = ServerShuttingDown;
127 }
128
129 // Send out-of-band errors to any waiting threads.
130 for (auto &KV : TmpPending)
131 KV.second->set_value(
133
134 // Wait for dispatcher to clear.
135 D->shutdown();
136
137 // Shut down services.
138 while (!Services.empty()) {
139 ShutdownErr =
140 joinErrors(std::move(ShutdownErr), Services.back()->shutdown());
141 Services.pop_back();
142 }
143
144 std::lock_guard<std::mutex> Lock(ServerStateMutex);
145 ShutdownErr = joinErrors(std::move(ShutdownErr), std::move(Err));
146 RunState = ServerShutDown;
147 ShutdownCV.notify_all();
148}
149
150Error SimpleRemoteEPCServer::sendMessage(SimpleRemoteEPCOpcode OpC,
151 uint64_t SeqNo, ExecutorAddr TagAddr,
152 ArrayRef<char> ArgBytes) {
153
154 LLVM_DEBUG({
155 dbgs() << "SimpleRemoteEPCServer::sendMessage: opc = ";
156 switch (OpC) {
158 dbgs() << "Setup";
159 assert(SeqNo == 0 && "Non-zero SeqNo for Setup?");
160 assert(!TagAddr && "Non-zero TagAddr for Setup?");
161 break;
163 dbgs() << "Hangup";
164 assert(SeqNo == 0 && "Non-zero SeqNo for Hangup?");
165 assert(!TagAddr && "Non-zero TagAddr for Hangup?");
166 break;
168 dbgs() << "Result";
169 assert(!TagAddr && "Non-zero TagAddr for Result?");
170 break;
172 dbgs() << "CallWrapper";
173 break;
174 }
175 dbgs() << ", seqno = " << SeqNo << ", tag-addr = " << TagAddr
176 << ", arg-buffer = " << formatv("{0:x}", ArgBytes.size())
177 << " bytes\n";
178 });
179 auto Err = T->sendMessage(OpC, SeqNo, TagAddr, ArgBytes);
180 LLVM_DEBUG({
181 if (Err)
182 dbgs() << " \\--> SimpleRemoteEPC::sendMessage failed\n";
183 });
184 return Err;
185}
186
187Error SimpleRemoteEPCServer::sendSetupMessage(
188 StringMap<std::vector<char>> BootstrapMap,
189 StringMap<ExecutorAddr> BootstrapSymbols) {
190
191 using namespace SimpleRemoteEPCDefaultBootstrapSymbolNames;
192
193 std::vector<char> SetupPacket;
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");
209
210 using SPSSerialize =
212 auto SetupPacketBytes =
213 shared::WrapperFunctionResult::allocate(SPSSerialize::size(EI));
214 shared::SPSOutputBuffer OB(SetupPacketBytes.data(), SetupPacketBytes.size());
215 if (!SPSSerialize::serialize(OB, EI))
216 return make_error<StringError>("Could not send setup packet",
218
219 return sendMessage(SimpleRemoteEPCOpcode::Setup, 0, ExecutorAddr(),
220 {SetupPacketBytes.data(), SetupPacketBytes.size()});
221}
222
223Error SimpleRemoteEPCServer::handleResult(
224 uint64_t SeqNo, ExecutorAddr TagAddr,
226 std::promise<shared::WrapperFunctionResult> *P = nullptr;
227 {
228 std::lock_guard<std::mutex> Lock(ServerStateMutex);
229 auto I = PendingJITDispatchResults.find(SeqNo);
230 if (I == PendingJITDispatchResults.end())
231 return make_error<StringError>("No call for sequence number " +
232 Twine(SeqNo),
234 P = I->second;
235 PendingJITDispatchResults.erase(I);
236 releaseSeqNo(SeqNo);
237 }
239 memcpy(R.data(), ArgBytes.data(), ArgBytes.size());
240 P->set_value(std::move(R));
241 return Error::success();
242}
243
244void SimpleRemoteEPCServer::handleCallWrapper(
245 uint64_t RemoteSeqNo, ExecutorAddr TagAddr,
247 D->dispatch([this, RemoteSeqNo, TagAddr, ArgBytes = std::move(ArgBytes)]() {
248 using WrapperFnTy =
249 shared::CWrapperFunctionResult (*)(const char *, size_t);
250 auto *Fn = TagAddr.toPtr<WrapperFnTy>();
252 Fn(ArgBytes.data(), ArgBytes.size()));
253 if (auto Err = sendMessage(SimpleRemoteEPCOpcode::Result, RemoteSeqNo,
254 ExecutorAddr(),
255 {ResultBytes.data(), ResultBytes.size()}))
256 ReportError(std::move(Err));
257 });
258}
259
261SimpleRemoteEPCServer::doJITDispatch(const void *FnTag, const char *ArgData,
262 size_t ArgSize) {
263 uint64_t SeqNo;
264 std::promise<shared::WrapperFunctionResult> ResultP;
265 auto ResultF = ResultP.get_future();
266 {
267 std::lock_guard<std::mutex> Lock(ServerStateMutex);
268 if (RunState != ServerRunning)
270 "jit_dispatch not available (EPC server shut down)");
271
272 SeqNo = getNextSeqNo();
273 assert(!PendingJITDispatchResults.count(SeqNo) && "SeqNo already in use");
274 PendingJITDispatchResults[SeqNo] = &ResultP;
275 }
276
277 if (auto Err = sendMessage(SimpleRemoteEPCOpcode::CallWrapper, SeqNo,
278 ExecutorAddr::fromPtr(FnTag), {ArgData, ArgSize}))
279 ReportError(std::move(Err));
280
281 return ResultF.get();
282}
283
285SimpleRemoteEPCServer::jitDispatchEntry(void *DispatchCtx, const void *FnTag,
286 const char *ArgData, size_t ArgSize) {
287 return reinterpret_cast<SimpleRemoteEPCServer *>(DispatchCtx)
288 ->doJITDispatch(FnTag, ArgData, ArgSize)
289 .release();
290}
291
292} // end namespace orc
293} // end namespace llvm
#define LLVM_DEBUG(X)
Definition: Debug.h:101
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 ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:163
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
bool erase(const KeyT &Val)
Definition: DenseMap.h:329
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:151
iterator end()
Definition: DenseMap.h:84
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
Tagged union holding either a T or a Error.
Definition: Error.h:470
size_t size() const
Definition: SmallVector.h:91
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:289
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:111
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
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 Expected< unsigned > getPageSize()
Get the process's page size.
unique_function is a type-erasing functor similar to std::function.
void addTo(StringMap< ExecutorAddr > &M)
std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
Definition: Host.cpp:1880
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:79
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:427
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
StringMap< ExecutorAddr > BootstrapSymbols
StringMap< std::vector< char > > BootstrapMap