LLVM  14.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 
80  void callWrapperAsync(ExecutorAddr WrapperFnAddr,
81  IncomingWFRHandler OnComplete,
82  ArrayRef<char> ArgBuffer) override;
83 
84  Error disconnect() override;
85 
88  SimpleRemoteEPCArgBytesVector ArgBytes) override;
89 
90  void handleDisconnect(Error Err) override;
91 
92 private:
93  SimpleRemoteEPC(std::shared_ptr<SymbolStringPool> SSP,
94  std::unique_ptr<TaskDispatcher> D)
96 
98  createDefaultMemoryManager(SimpleRemoteEPC &SREPC);
100  createDefaultMemoryAccess(SimpleRemoteEPC &SREPC);
101 
102  Error sendMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo,
103  ExecutorAddr TagAddr, ArrayRef<char> ArgBytes);
104 
105  Error handleSetup(uint64_t SeqNo, ExecutorAddr TagAddr,
107  Error setup(Setup S);
108 
109  Error handleResult(uint64_t SeqNo, ExecutorAddr TagAddr,
111  void handleCallWrapper(uint64_t RemoteSeqNo, ExecutorAddr TagAddr,
113  Error handleHangup(SimpleRemoteEPCArgBytesVector ArgBytes);
114 
115  uint64_t getNextSeqNo() { return NextSeqNo++; }
116  void releaseSeqNo(uint64_t SeqNo) {}
117 
118  using PendingCallWrapperResultsMap =
119  DenseMap<uint64_t, IncomingWFRHandler>;
120 
121  std::mutex SimpleRemoteEPCMutex;
122  std::condition_variable DisconnectCV;
123  bool Disconnected = false;
124  Error DisconnectErr = Error::success();
125 
126  std::unique_ptr<SimpleRemoteEPCTransport> T;
127  std::unique_ptr<jitlink::JITLinkMemoryManager> OwnedMemMgr;
128  std::unique_ptr<MemoryAccess> OwnedMemAccess;
129 
130  std::unique_ptr<EPCGenericDylibManager> DylibMgr;
131  ExecutorAddr RunAsMainAddr;
132 
133  uint64_t NextSeqNo = 0;
134  PendingCallWrapperResultsMap PendingCallWrapperResults;
135 };
136 
137 } // end namespace orc
138 } // end namespace llvm
139 
140 #endif // LLVM_EXECUTIONENGINE_ORC_SIMPLEREMOTEEPC_H
llvm::orc::ExecutorAddr
Represents an address in the executor process.
Definition: ExecutorAddress.h:37
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
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:102
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:1168
SimpleRemoteEPCUtils.h
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
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:358
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:42
EPCGenericJITLinkMemoryManager.h
EPCGenericMemoryAccess.h
llvm::orc::SimpleRemoteEPC::handleDisconnect
void handleDisconnect(Error Err) override
Handle a disconnection from the underlying transport.
Definition: SimpleRemoteEPC.cpp:159
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:57
llvm::orc::SimpleRemoteEPC::disconnect
Error disconnect() override
Disconnect from the target process.
Definition: SimpleRemoteEPC.cpp:93
llvm::orc::SimpleRemoteEPC::Setup::CreateMemoryManager
unique_function< CreateMemoryManagerFn > CreateMemoryManager
Definition: SimpleRemoteEPC.h:44
uint64_t
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:428
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:1658
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:838
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::orc::ExecutorProcessControl::D
std::unique_ptr< TaskDispatcher > D
Definition: ExecutorProcessControl.h:359
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:389
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