15#define DEBUG_TYPE "orc"
22 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
23 assert(Disconnected &&
"Destroyed without disconnection");
29 return DylibMgr->open(DylibPath, 0);
34 std::vector<tpctypes::LookupResult>
Result;
36 for (
auto &Element : Request) {
37 if (
auto R = DylibMgr->lookup(Element.Handle, Element.Symbols)) {
39 Result.back().reserve(R->size());
51 if (
auto Err = callSPSWrapper<rt::SPSRunAsMainSignature>(
53 return std::move(Err);
59 if (
auto Err = callSPSWrapper<rt::SPSRunAsVoidFunctionSignature>(
61 return std::move(Err);
68 if (
auto Err = callSPSWrapper<rt::SPSRunAsIntFunctionSignature>(
70 return std::move(Err);
79 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
80 SeqNo = getNextSeqNo();
81 assert(!PendingCallWrapperResults.
count(SeqNo) &&
"SeqNo already in use");
82 PendingCallWrapperResults[SeqNo] = std::move(OnComplete);
86 WrapperFnAddr, ArgBuffer)) {
95 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
96 auto I = PendingCallWrapperResults.
find(SeqNo);
97 if (
I != PendingCallWrapperResults.
end()) {
98 H = std::move(
I->second);
99 PendingCallWrapperResults.
erase(
I);
113 std::unique_lock<std::mutex> Lock(SimpleRemoteEPCMutex);
114 DisconnectCV.wait(Lock, [
this] {
return Disconnected; });
115 return std::move(DisconnectErr);
124 dbgs() <<
"SimpleRemoteEPC::handleMessage: opc = ";
128 assert(SeqNo == 0 &&
"Non-zero SeqNo for Setup?");
133 assert(SeqNo == 0 &&
"Non-zero SeqNo for Hangup?");
134 assert(TagAddr.
getValue() == 0 &&
"Non-zero TagAddr for Hangup?");
138 assert(TagAddr.
getValue() == 0 &&
"Non-zero TagAddr for Result?");
141 dbgs() <<
"CallWrapper";
144 dbgs() <<
", seqno = " << SeqNo
146 <<
", arg-buffer = " <<
formatv(
"{0:x}", ArgBytes.
size())
150 using UT = std::underlying_type_t<SimpleRemoteEPCOpcode>;
152 return make_error<StringError>(
"Unexpected opcode",
157 if (
auto Err = handleSetup(SeqNo, TagAddr, std::move(ArgBytes)))
158 return std::move(Err);
162 if (
auto Err = handleHangup(std::move(ArgBytes)))
163 return std::move(Err);
166 if (
auto Err = handleResult(SeqNo, TagAddr, std::move(ArgBytes)))
167 return std::move(Err);
170 handleCallWrapper(SeqNo, TagAddr, std::move(ArgBytes));
178 dbgs() <<
"SimpleRemoteEPC::handleDisconnect: "
179 << (Err ?
"failure" :
"success") <<
"\n";
185 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
186 std::swap(TmpPending, PendingCallWrapperResults);
189 for (
auto &KV : TmpPending)
193 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
194 DisconnectErr =
joinErrors(std::move(DisconnectErr), std::move(Err));
196 DisconnectCV.notify_all();
203 {{SAs.Allocator, rt::SimpleExecutorMemoryManagerInstanceName},
204 {SAs.Reserve, rt::SimpleExecutorMemoryManagerReserveWrapperName},
205 {SAs.Finalize, rt::SimpleExecutorMemoryManagerFinalizeWrapperName},
207 rt::SimpleExecutorMemoryManagerDeallocateWrapperName}}))
208 return std::move(Err);
210 return std::make_unique<EPCGenericJITLinkMemoryManager>(SREPC, SAs);
213Expected<std::unique_ptr<ExecutorProcessControl::MemoryAccess>>
214SimpleRemoteEPC::createDefaultMemoryAccess(SimpleRemoteEPC &SREPC) {
218Error SimpleRemoteEPC::sendMessage(SimpleRemoteEPCOpcode OpC,
uint64_t SeqNo,
219 ExecutorAddr TagAddr,
220 ArrayRef<char> ArgBytes) {
221 assert(OpC != SimpleRemoteEPCOpcode::Setup &&
222 "SimpleRemoteEPC sending Setup message? That's the wrong direction.");
225 dbgs() <<
"SimpleRemoteEPC::sendMessage: opc = ";
227 case SimpleRemoteEPCOpcode::Hangup:
229 assert(SeqNo == 0 &&
"Non-zero SeqNo for Hangup?");
230 assert(TagAddr.getValue() == 0 &&
"Non-zero TagAddr for Hangup?");
232 case SimpleRemoteEPCOpcode::Result:
234 assert(TagAddr.getValue() == 0 &&
"Non-zero TagAddr for Result?");
236 case SimpleRemoteEPCOpcode::CallWrapper:
237 dbgs() <<
"CallWrapper";
242 dbgs() <<
", seqno = " << SeqNo
243 <<
", tag-addr = " <<
formatv(
"{0:x}", TagAddr.getValue())
244 <<
", arg-buffer = " <<
formatv(
"{0:x}", ArgBytes.size())
247 auto Err =
T->sendMessage(OpC, SeqNo, TagAddr, ArgBytes);
250 dbgs() <<
" \\--> SimpleRemoteEPC::sendMessage failed\n";
255Error SimpleRemoteEPC::handleSetup(
uint64_t SeqNo, ExecutorAddr TagAddr,
256 SimpleRemoteEPCArgBytesVector ArgBytes) {
258 return make_error<StringError>(
"Setup packet SeqNo not zero",
262 return make_error<StringError>(
"Setup packet TagAddr not zero",
265 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
266 auto I = PendingCallWrapperResults.find(0);
267 assert(PendingCallWrapperResults.size() == 1 &&
268 I != PendingCallWrapperResults.end() &&
269 "Setup message handler not connectly set up");
270 auto SetupMsgHandler = std::move(
I->second);
271 PendingCallWrapperResults.erase(
I);
274 shared::WrapperFunctionResult::copyFrom(ArgBytes.data(), ArgBytes.size());
275 SetupMsgHandler(std::move(WFR));
279Error SimpleRemoteEPC::setup(Setup S) {
280 using namespace SimpleRemoteEPCDefaultBootstrapSymbolNames;
282 std::promise<MSVCPExpected<SimpleRemoteEPCExecutorInfo>> EIP;
283 auto EIF = EIP.get_future();
286 PendingCallWrapperResults[0] =
288 [&](shared::WrapperFunctionResult SetupMsgBytes) {
289 if (
const char *ErrMsg = SetupMsgBytes.getOutOfBandError()) {
295 shared::SPSArgList<shared::SPSSimpleRemoteEPCExecutorInfo>;
296 shared::SPSInputBuffer
IB(SetupMsgBytes.data(), SetupMsgBytes.size());
297 SimpleRemoteEPCExecutorInfo EI;
298 if (SPSSerialize::deserialize(IB, EI))
301 EIP.set_value(make_error<StringError>(
306 if (
auto Err =
T->start())
313 return EI.takeError();
317 dbgs() <<
"SimpleRemoteEPC received setup message:\n"
318 <<
" Triple: " << EI->TargetTriple <<
"\n"
319 <<
" Page size: " << EI->PageSize <<
"\n"
320 <<
" Bootstrap symbols:\n";
321 for (
const auto &KV : EI->BootstrapSymbols)
322 dbgs() <<
" " << KV.first() <<
": "
323 <<
formatv(
"{0:x16}", KV.second.getValue()) <<
"\n";
325 TargetTriple = Triple(EI->TargetTriple);
327 BootstrapSymbols = std::move(EI->BootstrapSymbols);
329 if (
auto Err = getBootstrapSymbols(
332 {RunAsMainAddr, rt::RunAsMainWrapperName},
333 {RunAsVoidFunctionAddr, rt::RunAsVoidFunctionWrapperName},
334 {RunAsIntFunctionAddr, rt::RunAsIntFunctionWrapperName}}))
338 EPCGenericDylibManager::CreateWithDefaultBootstrapSymbols(*
this))
339 DylibMgr = std::make_unique<EPCGenericDylibManager>(std::move(*
DM));
341 return DM.takeError();
344 if (!S.CreateMemoryManager)
345 S.CreateMemoryManager = createDefaultMemoryManager;
347 if (
auto MemMgr = S.CreateMemoryManager(*
this)) {
348 OwnedMemMgr = std::move(*MemMgr);
349 this->MemMgr = OwnedMemMgr.get();
351 return MemMgr.takeError();
354 if (!S.CreateMemoryAccess)
355 S.CreateMemoryAccess = createDefaultMemoryAccess;
357 if (
auto MemAccess = S.CreateMemoryAccess(*
this)) {
358 OwnedMemAccess = std::move(*MemAccess);
359 this->MemAccess = OwnedMemAccess.get();
361 return MemAccess.takeError();
366Error SimpleRemoteEPC::handleResult(
uint64_t SeqNo, ExecutorAddr TagAddr,
367 SimpleRemoteEPCArgBytesVector ArgBytes) {
368 IncomingWFRHandler SendResult;
371 return make_error<StringError>(
"Unexpected TagAddr in result message",
375 std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
376 auto I = PendingCallWrapperResults.find(SeqNo);
377 if (
I == PendingCallWrapperResults.end())
378 return make_error<StringError>(
"No call for sequence number " +
381 SendResult = std::move(
I->second);
382 PendingCallWrapperResults.erase(
I);
387 shared::WrapperFunctionResult::copyFrom(ArgBytes.data(), ArgBytes.size());
388 SendResult(std::move(WFR));
392void SimpleRemoteEPC::handleCallWrapper(
393 uint64_t RemoteSeqNo, ExecutorAddr TagAddr,
394 SimpleRemoteEPCArgBytesVector ArgBytes) {
395 assert(ES &&
"No ExecutionSession attached");
397 [
this, RemoteSeqNo, TagAddr, ArgBytes = std::move(ArgBytes)]() {
398 ES->runJITDispatchHandler(
399 [
this, RemoteSeqNo](shared::WrapperFunctionResult WFR) {
401 sendMessage(SimpleRemoteEPCOpcode::Result, RemoteSeqNo,
402 ExecutorAddr(), {WFR.data(), WFR.size()}))
405 TagAddr.getValue(), ArgBytes);
407 "callWrapper task"));
410Error SimpleRemoteEPC::handleHangup(SimpleRemoteEPCArgBytesVector ArgBytes) {
412 auto WFR = WrapperFunctionResult::copyFrom(ArgBytes.data(), ArgBytes.size());
413 if (
const char *ErrMsg = WFR.getOutOfBandError())
419 return make_error<StringError>(
"Could not deserialize hangup info",
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
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),...
iterator find(const_arg_type_t< KeyT > Val)
bool erase(const KeyT &Val)
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
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.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
void reportError(Error Err)
Report a error for this execution session.
Represents an address in the executor process.
uint64_t getValue() const
A handler or incoming WrapperFunctionResults – either return values from callWrapper* calls,...
std::unique_ptr< TaskDispatcher > D
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...
ExecutionSession & getExecutionSession()
Return the ExecutionSession associated with this instance.
void handleDisconnect(Error Err) override
Handle a disconnection from the underlying transport.
Expected< int32_t > runAsMain(ExecutorAddr MainFnAddr, ArrayRef< std::string > Args) override
Run function with a main-like signature.
Expected< HandleMessageAction > handleMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo, ExecutorAddr TagAddr, SimpleRemoteEPCArgBytesVector ArgBytes) override
Handle receipt of a message.
Error disconnect() override
Disconnect from the target process.
Expected< tpctypes::DylibHandle > loadDylib(const char *DylibPath) override
Load the dynamic library at the given path and return a handle to it.
Expected< std::vector< tpctypes::LookupResult > > lookupSymbols(ArrayRef< LookupRequest > Request) override
Search for symbols in the target process.
Expected< int32_t > runAsVoidFunction(ExecutorAddr VoidFnAddr) override
Run function with a int (*)(void) signature.
void callWrapperAsync(ExecutorAddr WrapperFnAddr, IncomingWFRHandler OnComplete, ArrayRef< char > ArgBuffer) override
Run a wrapper function in the executor.
Expected< int32_t > runAsIntFunction(ExecutorAddr IntFnAddr, int Arg) override
Run function with a int (*)(int) signature.
A utility class for serializing to a blob from a variadic list.
static WrapperFunctionResult createOutOfBandError(const char *Msg)
Create an out-of-band error by copying the given string.
#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.
This is an optimization pass for GlobalISel generic memory operations.
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...
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.