LLVM  16.0.0git
SimpleRemoteEPC.h
Go to the documentation of this file.
1 //===---- SimpleRemoteEPC.h - Simple remote executor control ----*- C++ -*-===//
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 //
9 // Simple remote executor process control.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_ORC_SIMPLEREMOTEEPC_H
14 #define LLVM_EXECUTIONENGINE_ORC_SIMPLEREMOTEEPC_H
15 
16 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/Support/Error.h"
25 
26 #include <future>
27 
28 namespace llvm {
29 namespace orc {
30 
33 public:
34  /// A setup object containing callbacks to construct a memory manager and
35  /// memory access object. Both are optional. If not specified,
36  /// EPCGenericJITLinkMemoryManager and EPCGenericMemoryAccess will be used.
37  struct Setup {
38  using CreateMemoryManagerFn =
41  using CreateMemoryAccessFn =
43 
46  };
47 
48  /// Create a SimpleRemoteEPC using the given transport type and args.
49  template <typename TransportT, typename... TransportTCtorArgTs>
51  Create(std::unique_ptr<TaskDispatcher> D, Setup S,
52  TransportTCtorArgTs &&...TransportTCtorArgs) {
53  std::unique_ptr<SimpleRemoteEPC> SREPC(
54  new SimpleRemoteEPC(std::make_shared<SymbolStringPool>(),
55  std::move(D)));
56  auto T = TransportT::Create(
57  *SREPC, std::forward<TransportTCtorArgTs>(TransportTCtorArgs)...);
58  if (!T)
59  return T.takeError();
60  SREPC->T = std::move(*T);
61  if (auto Err = SREPC->setup(std::move(S)))
62  return joinErrors(std::move(Err), SREPC->disconnect());
63  return std::move(SREPC);
64  }
65 
66  SimpleRemoteEPC(const SimpleRemoteEPC &) = delete;
67  SimpleRemoteEPC &operator=(const SimpleRemoteEPC &) = delete;
68  SimpleRemoteEPC(SimpleRemoteEPC &&) = delete;
71 
72  Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override;
73 
75  lookupSymbols(ArrayRef<LookupRequest> Request) override;
76 
78  ArrayRef<std::string> Args) override;
79 
81 
82  Expected<int32_t> runAsIntFunction(ExecutorAddr IntFnAddr, int Arg) override;
83 
84  void callWrapperAsync(ExecutorAddr WrapperFnAddr,
85  IncomingWFRHandler OnComplete,
86  ArrayRef<char> ArgBuffer) override;
87 
88  Error disconnect() override;
89 
92  SimpleRemoteEPCArgBytesVector ArgBytes) override;
93 
94  void handleDisconnect(Error Err) override;
95 
96 private:
97  SimpleRemoteEPC(std::shared_ptr<SymbolStringPool> SSP,
98  std::unique_ptr<TaskDispatcher> D)
100 
102  createDefaultMemoryManager(SimpleRemoteEPC &SREPC);
104  createDefaultMemoryAccess(SimpleRemoteEPC &SREPC);
105 
106  Error sendMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo,
107  ExecutorAddr TagAddr, ArrayRef<char> ArgBytes);
108 
109  Error handleSetup(uint64_t SeqNo, ExecutorAddr TagAddr,
111  Error setup(Setup S);
112 
113  Error handleResult(uint64_t SeqNo, ExecutorAddr TagAddr,
115  void handleCallWrapper(uint64_t RemoteSeqNo, ExecutorAddr TagAddr,
117  Error handleHangup(SimpleRemoteEPCArgBytesVector ArgBytes);
118 
119  uint64_t getNextSeqNo() { return NextSeqNo++; }
120  void releaseSeqNo(uint64_t SeqNo) {}
121 
122  using PendingCallWrapperResultsMap =
123  DenseMap<uint64_t, IncomingWFRHandler>;
124 
125  std::mutex SimpleRemoteEPCMutex;
126  std::condition_variable DisconnectCV;
127  bool Disconnected = false;
128  Error DisconnectErr = Error::success();
129 
130  std::unique_ptr<SimpleRemoteEPCTransport> T;
131  std::unique_ptr<jitlink::JITLinkMemoryManager> OwnedMemMgr;
132  std::unique_ptr<MemoryAccess> OwnedMemAccess;
133 
134  std::unique_ptr<EPCGenericDylibManager> DylibMgr;
135  ExecutorAddr RunAsMainAddr;
136  ExecutorAddr RunAsVoidFunctionAddr;
137  ExecutorAddr RunAsIntFunctionAddr;
138 
139  uint64_t NextSeqNo = 0;
140  PendingCallWrapperResultsMap PendingCallWrapperResults;
141 };
142 
143 } // end namespace orc
144 } // end namespace llvm
145 
146 #endif // LLVM_EXECUTIONENGINE_ORC_SIMPLEREMOTEEPC_H
llvm::orc::ExecutorAddr
Represents an address in the executor process.
Definition: ExecutorAddress.h:31
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::orc::SimpleRemoteEPCOpcode
SimpleRemoteEPCOpcode
Definition: SimpleRemoteEPCUtils.h:37
llvm::orc::SimpleRemoteEPC::handleMessage
Expected< HandleMessageAction > handleMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo, ExecutorAddr TagAddr, SimpleRemoteEPCArgBytesVector ArgBytes) override
Handle receipt of a message.
Definition: SimpleRemoteEPC.cpp:119
llvm::lltok::Error
@ Error
Definition: LLToken.h:21
FunctionExtras.h
llvm::unique_function< CreateMemoryManagerFn >
llvm::orc::SimpleRemoteEPC::~SimpleRemoteEPC
~SimpleRemoteEPC()
Definition: SimpleRemoteEPC.cpp:20
llvm::orc::SimpleRemoteEPC::Setup
A setup object containing callbacks to construct a memory manager and memory access object.
Definition: SimpleRemoteEPC.h:37
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1181
SimpleRemoteEPCUtils.h
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
Error.h
DenseMap.h
llvm::orc::ExecutorProcessControl
ExecutorProcessControl supports interaction with a JIT target process.
Definition: ExecutorProcessControl.h:38
MSVCErrorWorkarounds.h
llvm::orc::ExecutorProcessControl::SSP
std::shared_ptr< SymbolStringPool > SSP
Definition: ExecutorProcessControl.h:367
llvm::orc::SimpleRemoteEPC::Setup::CreateMemoryManagerFn
Expected< std::unique_ptr< jitlink::JITLinkMemoryManager > >(SimpleRemoteEPC &) CreateMemoryManagerFn
Definition: SimpleRemoteEPC.h:40
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
EPCGenericJITLinkMemoryManager.h
llvm::orc::SimpleRemoteEPC::runAsVoidFunction
Expected< int32_t > runAsVoidFunction(ExecutorAddr VoidFnAddr) override
Run function with a int (*)(void) signature.
Definition: SimpleRemoteEPC.cpp:57
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:187
EPCGenericMemoryAccess.h
llvm::orc::SimpleRemoteEPC::handleDisconnect
void handleDisconnect(Error Err) override
Handle a disconnection from the underlying transport.
Definition: SimpleRemoteEPC.cpp:176
llvm::orc::SimpleRemoteEPC::SimpleRemoteEPC
SimpleRemoteEPC(const SimpleRemoteEPC &)=delete
llvm::orc::SimpleRemoteEPC::operator=
SimpleRemoteEPC & operator=(const SimpleRemoteEPC &)=delete
llvm::orc::SimpleRemoteEPC::callWrapperAsync
void callWrapperAsync(ExecutorAddr WrapperFnAddr, IncomingWFRHandler OnComplete, ArrayRef< char > ArgBuffer) override
Run a wrapper function in the executor.
Definition: SimpleRemoteEPC.cpp:74
llvm::orc::SimpleRemoteEPC::disconnect
Error disconnect() override
Disconnect from the target process.
Definition: SimpleRemoteEPC.cpp:110
llvm::orc::SimpleRemoteEPC::Setup::CreateMemoryManager
unique_function< CreateMemoryManagerFn > CreateMemoryManager
Definition: SimpleRemoteEPC.h:44
uint64_t
llvm::orc::SimpleRemoteEPC::runAsIntFunction
Expected< int32_t > runAsIntFunction(ExecutorAddr IntFnAddr, int Arg) override
Run function with a int (*)(int) signature.
Definition: SimpleRemoteEPC.cpp:65
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::orc::SimpleRemoteEPC
Definition: SimpleRemoteEPC.h:31
llvm::orc::SimpleRemoteEPC::Setup::CreateMemoryAccess
unique_function< CreateMemoryAccessFn > CreateMemoryAccess
Definition: SimpleRemoteEPC.h:45
llvm::joinErrors
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:426
llvm::orc::SimpleRemoteEPC::loadDylib
Expected< tpctypes::DylibHandle > loadDylib(const char *DylibPath) override
Load the dynamic library at the given path and return a handle to it.
Definition: SimpleRemoteEPC.cpp:28
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1666
EPCGenericDylibManager.h
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::orc::SimpleRemoteEPCArgBytesVector
SmallVector< char, 128 > SimpleRemoteEPCArgBytesVector
Definition: SimpleRemoteEPCUtils.h:51
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
std
Definition: BitVector.h:851
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::orc::ExecutorProcessControl::D
std::unique_ptr< TaskDispatcher > D
Definition: ExecutorProcessControl.h:368
llvm::orc::SimpleRemoteEPCTransportClient
Definition: SimpleRemoteEPCUtils.h:53
ExecutorProcessControl.h
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:394
llvm::orc::SimpleRemoteEPC::Create
static Expected< std::unique_ptr< SimpleRemoteEPC > > Create(std::unique_ptr< TaskDispatcher > D, Setup S, TransportTCtorArgTs &&...TransportTCtorArgs)
Create a SimpleRemoteEPC using the given transport type and args.
Definition: SimpleRemoteEPC.h:51
llvm::orc::SimpleRemoteEPC::runAsMain
Expected< int32_t > runAsMain(ExecutorAddr MainFnAddr, ArrayRef< std::string > Args) override
Run function with a main-like signature.
Definition: SimpleRemoteEPC.cpp:48
llvm::orc::SimpleRemoteEPC::lookupSymbols
Expected< std::vector< tpctypes::LookupResult > > lookupSymbols(ArrayRef< LookupRequest > Request) override
Search for symbols in the target process.
Definition: SimpleRemoteEPC.cpp:33