1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
//===------- SimpleEPCServer.cpp - EPC over simple abstract channel -------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ExecutionEngine/Orc/TargetProcess/SimpleRemoteEPCServer.h"
#include "llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/Process.h"
#include "OrcRTBootstrap.h"
#define DEBUG_TYPE "orc"
using namespace llvm::orc::shared;
namespace llvm {
namespace orc {
ExecutorBootstrapService::~ExecutorBootstrapService() {}
SimpleRemoteEPCServer::Dispatcher::~Dispatcher() {}
#if LLVM_ENABLE_THREADS
void SimpleRemoteEPCServer::ThreadDispatcher::dispatch(
unique_function<void()> Work) {
{
std::lock_guard<std::mutex> Lock(DispatchMutex);
if (!Running)
return;
++Outstanding;
}
std::thread([this, Work = std::move(Work)]() mutable {
Work();
std::lock_guard<std::mutex> Lock(DispatchMutex);
--Outstanding;
OutstandingCV.notify_all();
}).detach();
}
void SimpleRemoteEPCServer::ThreadDispatcher::shutdown() {
std::unique_lock<std::mutex> Lock(DispatchMutex);
Running = false;
OutstandingCV.wait(Lock, [this]() { return Outstanding == 0; });
}
#endif
StringMap<ExecutorAddr> SimpleRemoteEPCServer::defaultBootstrapSymbols() {
StringMap<ExecutorAddr> DBS;
rt_bootstrap::addTo(DBS);
return DBS;
}
Expected<SimpleRemoteEPCTransportClient::HandleMessageAction>
SimpleRemoteEPCServer::handleMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo,
ExecutorAddr TagAddr,
SimpleRemoteEPCArgBytesVector ArgBytes) {
LLVM_DEBUG({
dbgs() << "SimpleRemoteEPCServer::handleMessage: opc = ";
switch (OpC) {
case SimpleRemoteEPCOpcode::Setup:
dbgs() << "Setup";
assert(SeqNo == 0 && "Non-zero SeqNo for Setup?");
assert(TagAddr.getValue() == 0 && "Non-zero TagAddr for Setup?");
break;
case SimpleRemoteEPCOpcode::Hangup:
dbgs() << "Hangup";
assert(SeqNo == 0 && "Non-zero SeqNo for Hangup?");
assert(TagAddr.getValue() == 0 && "Non-zero TagAddr for Hangup?");
break;
case SimpleRemoteEPCOpcode::Result:
dbgs() << "Result";
assert(TagAddr.getValue() == 0 && "Non-zero TagAddr for Result?");
break;
case SimpleRemoteEPCOpcode::CallWrapper:
dbgs() << "CallWrapper";
break;
}
dbgs() << ", seqno = " << SeqNo
<< ", tag-addr = " << formatv("{0:x}", TagAddr.getValue())
<< ", arg-buffer = " << formatv("{0:x}", ArgBytes.size())
<< " bytes\n";
});
using UT = std::underlying_type_t<SimpleRemoteEPCOpcode>;
if (static_cast<UT>(OpC) > static_cast<UT>(SimpleRemoteEPCOpcode::LastOpC))
return make_error<StringError>("Unexpected opcode",
inconvertibleErrorCode());
// TODO: Clean detach message?
switch (OpC) {
case SimpleRemoteEPCOpcode::Setup:
return make_error<StringError>("Unexpected Setup opcode",
inconvertibleErrorCode());
case SimpleRemoteEPCOpcode::Hangup:
return SimpleRemoteEPCTransportClient::EndSession;
case SimpleRemoteEPCOpcode::Result:
if (auto Err = handleResult(SeqNo, TagAddr, std::move(ArgBytes)))
return std::move(Err);
break;
case SimpleRemoteEPCOpcode::CallWrapper:
handleCallWrapper(SeqNo, TagAddr, std::move(ArgBytes));
break;
}
return ContinueSession;
}
Error SimpleRemoteEPCServer::waitForDisconnect() {
std::unique_lock<std::mutex> Lock(ServerStateMutex);
ShutdownCV.wait(Lock, [this]() { return RunState == ServerShutDown; });
return std::move(ShutdownErr);
}
void SimpleRemoteEPCServer::handleDisconnect(Error Err) {
PendingJITDispatchResultsMap TmpPending;
{
std::lock_guard<std::mutex> Lock(ServerStateMutex);
std::swap(TmpPending, PendingJITDispatchResults);
RunState = ServerShuttingDown;
}
// Send out-of-band errors to any waiting threads.
for (auto &KV : TmpPending)
KV.second->set_value(
shared::WrapperFunctionResult::createOutOfBandError("disconnecting"));
// Wait for dispatcher to clear.
D->shutdown();
// Shut down services.
while (!Services.empty()) {
ShutdownErr =
joinErrors(std::move(ShutdownErr), Services.back()->shutdown());
Services.pop_back();
}
std::lock_guard<std::mutex> Lock(ServerStateMutex);
ShutdownErr = joinErrors(std::move(ShutdownErr), std::move(Err));
RunState = ServerShutDown;
ShutdownCV.notify_all();
}
Error SimpleRemoteEPCServer::sendMessage(SimpleRemoteEPCOpcode OpC,
uint64_t SeqNo, ExecutorAddr TagAddr,
ArrayRef<char> ArgBytes) {
LLVM_DEBUG({
dbgs() << "SimpleRemoteEPCServer::sendMessage: opc = ";
switch (OpC) {
case SimpleRemoteEPCOpcode::Setup:
dbgs() << "Setup";
assert(SeqNo == 0 && "Non-zero SeqNo for Setup?");
assert(TagAddr.getValue() == 0 && "Non-zero TagAddr for Setup?");
break;
case SimpleRemoteEPCOpcode::Hangup:
dbgs() << "Hangup";
assert(SeqNo == 0 && "Non-zero SeqNo for Hangup?");
assert(TagAddr.getValue() == 0 && "Non-zero TagAddr for Hangup?");
break;
case SimpleRemoteEPCOpcode::Result:
dbgs() << "Result";
assert(TagAddr.getValue() == 0 && "Non-zero TagAddr for Result?");
break;
case SimpleRemoteEPCOpcode::CallWrapper:
dbgs() << "CallWrapper";
break;
}
dbgs() << ", seqno = " << SeqNo
<< ", tag-addr = " << formatv("{0:x}", TagAddr.getValue())
<< ", arg-buffer = " << formatv("{0:x}", ArgBytes.size())
<< " bytes\n";
});
auto Err = T->sendMessage(OpC, SeqNo, TagAddr, ArgBytes);
LLVM_DEBUG({
if (Err)
dbgs() << " \\--> SimpleRemoteEPC::sendMessage failed\n";
});
return Err;
}
Error SimpleRemoteEPCServer::sendSetupMessage(
StringMap<ExecutorAddr> BootstrapSymbols) {
using namespace SimpleRemoteEPCDefaultBootstrapSymbolNames;
std::vector<char> SetupPacket;
SimpleRemoteEPCExecutorInfo EI;
EI.TargetTriple = sys::getProcessTriple();
if (auto PageSize = sys::Process::getPageSize())
EI.PageSize = *PageSize;
else
return PageSize.takeError();
EI.BootstrapSymbols = std::move(BootstrapSymbols);
assert(!EI.BootstrapSymbols.count(ExecutorSessionObjectName) &&
"Dispatch context name should not be set");
assert(!EI.BootstrapSymbols.count(DispatchFnName) &&
"Dispatch function name should not be set");
EI.BootstrapSymbols[ExecutorSessionObjectName] = ExecutorAddr::fromPtr(this);
EI.BootstrapSymbols[DispatchFnName] = ExecutorAddr::fromPtr(jitDispatchEntry);
using SPSSerialize =
shared::SPSArgList<shared::SPSSimpleRemoteEPCExecutorInfo>;
auto SetupPacketBytes =
shared::WrapperFunctionResult::allocate(SPSSerialize::size(EI));
shared::SPSOutputBuffer OB(SetupPacketBytes.data(), SetupPacketBytes.size());
if (!SPSSerialize::serialize(OB, EI))
return make_error<StringError>("Could not send setup packet",
inconvertibleErrorCode());
return sendMessage(SimpleRemoteEPCOpcode::Setup, 0, ExecutorAddr(),
{SetupPacketBytes.data(), SetupPacketBytes.size()});
}
Error SimpleRemoteEPCServer::handleResult(
uint64_t SeqNo, ExecutorAddr TagAddr,
SimpleRemoteEPCArgBytesVector ArgBytes) {
std::promise<shared::WrapperFunctionResult> *P = nullptr;
{
std::lock_guard<std::mutex> Lock(ServerStateMutex);
auto I = PendingJITDispatchResults.find(SeqNo);
if (I == PendingJITDispatchResults.end())
return make_error<StringError>("No call for sequence number " +
Twine(SeqNo),
inconvertibleErrorCode());
P = I->second;
PendingJITDispatchResults.erase(I);
releaseSeqNo(SeqNo);
}
auto R = shared::WrapperFunctionResult::allocate(ArgBytes.size());
memcpy(R.data(), ArgBytes.data(), ArgBytes.size());
P->set_value(std::move(R));
return Error::success();
}
void SimpleRemoteEPCServer::handleCallWrapper(
uint64_t RemoteSeqNo, ExecutorAddr TagAddr,
SimpleRemoteEPCArgBytesVector ArgBytes) {
D->dispatch([this, RemoteSeqNo, TagAddr, ArgBytes = std::move(ArgBytes)]() {
using WrapperFnTy =
shared::CWrapperFunctionResult (*)(const char *, size_t);
auto *Fn = TagAddr.toPtr<WrapperFnTy>();
shared::WrapperFunctionResult ResultBytes(
Fn(ArgBytes.data(), ArgBytes.size()));
if (auto Err = sendMessage(SimpleRemoteEPCOpcode::Result, RemoteSeqNo,
ExecutorAddr(),
{ResultBytes.data(), ResultBytes.size()}))
ReportError(std::move(Err));
});
}
shared::WrapperFunctionResult
SimpleRemoteEPCServer::doJITDispatch(const void *FnTag, const char *ArgData,
size_t ArgSize) {
uint64_t SeqNo;
std::promise<shared::WrapperFunctionResult> ResultP;
auto ResultF = ResultP.get_future();
{
std::lock_guard<std::mutex> Lock(ServerStateMutex);
if (RunState != ServerRunning)
return shared::WrapperFunctionResult::createOutOfBandError(
"jit_dispatch not available (EPC server shut down)");
SeqNo = getNextSeqNo();
assert(!PendingJITDispatchResults.count(SeqNo) && "SeqNo already in use");
PendingJITDispatchResults[SeqNo] = &ResultP;
}
if (auto Err = sendMessage(SimpleRemoteEPCOpcode::CallWrapper, SeqNo,
ExecutorAddr::fromPtr(FnTag), {ArgData, ArgSize}))
ReportError(std::move(Err));
return ResultF.get();
}
shared::CWrapperFunctionResult
SimpleRemoteEPCServer::jitDispatchEntry(void *DispatchCtx, const void *FnTag,
const char *ArgData, size_t ArgSize) {
return reinterpret_cast<SimpleRemoteEPCServer *>(DispatchCtx)
->doJITDispatch(FnTag, ArgData, ArgSize)
.release();
}
} // end namespace orc
} // end namespace llvm
|