1 //===- OrcRemoteTargetRPCAPI.h - Orc Remote-target RPC API ------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the Orc remote-target RPC API. It should not be used
10 // directly, but is used by the RemoteTargetClient and RemoteTargetServer
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETRPCAPI_H
16 #define LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETRPCAPI_H
18 #include "llvm/ExecutionEngine/JITSymbol.h"
19 #include "llvm/ExecutionEngine/Orc/RPCUtils.h"
20 #include "llvm/ExecutionEngine/Orc/RawByteChannel.h"
27 /// Template error for missing resources.
28 template <typename ResourceIdT
>
29 class ResourceNotFound
30 : public ErrorInfo
<ResourceNotFound
<ResourceIdT
>> {
34 ResourceNotFound(ResourceIdT ResourceId
,
35 std::string ResourceDescription
= "")
36 : ResourceId(std::move(ResourceId
)),
37 ResourceDescription(std::move(ResourceDescription
)) {}
39 std::error_code
convertToErrorCode() const override
{
40 return orcError(OrcErrorCode::UnknownResourceHandle
);
43 void log(raw_ostream
&OS
) const override
{
44 OS
<< (ResourceDescription
.empty()
45 ? "Remote resource with id "
46 : ResourceDescription
)
47 << " " << ResourceId
<< " not found";
51 ResourceIdT ResourceId
;
52 std::string ResourceDescription
;
55 template <typename ResourceIdT
>
56 char ResourceNotFound
<ResourceIdT
>::ID
= 0;
58 class DirectBufferWriter
{
60 DirectBufferWriter() = default;
61 DirectBufferWriter(const char *Src
, JITTargetAddress Dst
, uint64_t Size
)
62 : Src(Src
), Dst(Dst
), Size(Size
) {}
64 const char *getSrc() const { return Src
; }
65 JITTargetAddress
getDst() const { return Dst
; }
66 uint64_t getSize() const { return Size
; }
74 } // end namespace remote
79 class RPCTypeName
<JITSymbolFlags
> {
81 static const char *getName() { return "JITSymbolFlags"; }
84 template <typename ChannelT
>
85 class SerializationTraits
<ChannelT
, JITSymbolFlags
> {
88 static Error
serialize(ChannelT
&C
, const JITSymbolFlags
&Flags
) {
89 return serializeSeq(C
, Flags
.getRawFlagsValue(), Flags
.getTargetFlags());
92 static Error
deserialize(ChannelT
&C
, JITSymbolFlags
&Flags
) {
93 JITSymbolFlags::UnderlyingType JITFlags
;
94 JITSymbolFlags::TargetFlagsType TargetFlags
;
95 if (auto Err
= deserializeSeq(C
, JITFlags
, TargetFlags
))
97 Flags
= JITSymbolFlags(static_cast<JITSymbolFlags::FlagNames
>(JITFlags
),
99 return Error::success();
103 template <> class RPCTypeName
<remote::DirectBufferWriter
> {
105 static const char *getName() { return "DirectBufferWriter"; }
108 template <typename ChannelT
>
109 class SerializationTraits
<
110 ChannelT
, remote::DirectBufferWriter
, remote::DirectBufferWriter
,
111 typename
std::enable_if
<
112 std::is_base_of
<RawByteChannel
, ChannelT
>::value
>::type
> {
114 static Error
serialize(ChannelT
&C
, const remote::DirectBufferWriter
&DBW
) {
115 if (auto EC
= serializeSeq(C
, DBW
.getDst()))
117 if (auto EC
= serializeSeq(C
, DBW
.getSize()))
119 return C
.appendBytes(DBW
.getSrc(), DBW
.getSize());
122 static Error
deserialize(ChannelT
&C
, remote::DirectBufferWriter
&DBW
) {
123 JITTargetAddress Dst
;
124 if (auto EC
= deserializeSeq(C
, Dst
))
127 if (auto EC
= deserializeSeq(C
, Size
))
129 char *Addr
= reinterpret_cast<char *>(static_cast<uintptr_t>(Dst
));
131 DBW
= remote::DirectBufferWriter(nullptr, Dst
, Size
);
133 return C
.readBytes(Addr
, Size
);
137 } // end namespace rpc
141 class ResourceIdMgr
{
143 using ResourceId
= uint64_t;
144 static const ResourceId InvalidId
= ~0U;
146 ResourceIdMgr() = default;
147 explicit ResourceIdMgr(ResourceId FirstValidId
)
148 : NextId(std::move(FirstValidId
)) {}
150 ResourceId
getNext() {
151 if (!FreeIds
.empty()) {
152 ResourceId I
= FreeIds
.back();
156 assert(NextId
+ 1 != ~0ULL && "All ids allocated");
160 void release(ResourceId I
) { FreeIds
.push_back(I
); }
163 ResourceId NextId
= 1;
164 std::vector
<ResourceId
> FreeIds
;
167 /// Registers EH frames on the remote.
170 /// Registers EH frames on the remote.
171 class RegisterEHFrames
172 : public rpc::Function
<RegisterEHFrames
,
173 void(JITTargetAddress Addr
, uint32_t Size
)> {
175 static const char *getName() { return "RegisterEHFrames"; }
178 /// Deregisters EH frames on the remote.
179 class DeregisterEHFrames
180 : public rpc::Function
<DeregisterEHFrames
,
181 void(JITTargetAddress Addr
, uint32_t Size
)> {
183 static const char *getName() { return "DeregisterEHFrames"; }
186 } // end namespace eh
188 /// RPC functions for executing remote code.
191 /// Call an 'int32_t()'-type function on the remote, returns the called
192 /// function's return value.
194 : public rpc::Function
<CallIntVoid
, int32_t(JITTargetAddress Addr
)> {
196 static const char *getName() { return "CallIntVoid"; }
199 /// Call an 'int32_t(int32_t, char**)'-type function on the remote, returns the
200 /// called function's return value.
202 : public rpc::Function
<CallMain
, int32_t(JITTargetAddress Addr
,
203 std::vector
<std::string
> Args
)> {
205 static const char *getName() { return "CallMain"; }
208 /// Calls a 'void()'-type function on the remote, returns when the called
209 /// function completes.
211 : public rpc::Function
<CallVoidVoid
, void(JITTargetAddress FnAddr
)> {
213 static const char *getName() { return "CallVoidVoid"; }
216 } // end namespace exec
218 /// RPC functions for remote memory management / inspection / modification.
221 /// Creates a memory allocator on the remote.
222 class CreateRemoteAllocator
223 : public rpc::Function
<CreateRemoteAllocator
,
224 void(ResourceIdMgr::ResourceId AllocatorID
)> {
226 static const char *getName() { return "CreateRemoteAllocator"; }
229 /// Destroys a remote allocator, freeing any memory allocated by it.
230 class DestroyRemoteAllocator
231 : public rpc::Function
<DestroyRemoteAllocator
,
232 void(ResourceIdMgr::ResourceId AllocatorID
)> {
234 static const char *getName() { return "DestroyRemoteAllocator"; }
237 /// Read a remote memory block.
239 : public rpc::Function
<ReadMem
, std::vector
<uint8_t>(JITTargetAddress Src
,
242 static const char *getName() { return "ReadMem"; }
245 /// Reserve a block of memory on the remote via the given allocator.
247 : public rpc::Function
<ReserveMem
,
248 JITTargetAddress(ResourceIdMgr::ResourceId AllocID
,
249 uint64_t Size
, uint32_t Align
)> {
251 static const char *getName() { return "ReserveMem"; }
254 /// Set the memory protection on a memory block.
256 : public rpc::Function
<SetProtections
,
257 void(ResourceIdMgr::ResourceId AllocID
,
258 JITTargetAddress Dst
, uint32_t ProtFlags
)> {
260 static const char *getName() { return "SetProtections"; }
263 /// Write to a remote memory block.
265 : public rpc::Function
<WriteMem
, void(remote::DirectBufferWriter DB
)> {
267 static const char *getName() { return "WriteMem"; }
270 /// Write to a remote pointer.
271 class WritePtr
: public rpc::Function
<WritePtr
, void(JITTargetAddress Dst
,
272 JITTargetAddress Val
)> {
274 static const char *getName() { return "WritePtr"; }
277 } // end namespace mem
279 /// RPC functions for remote stub and trampoline management.
282 /// Creates an indirect stub owner on the remote.
283 class CreateIndirectStubsOwner
284 : public rpc::Function
<CreateIndirectStubsOwner
,
285 void(ResourceIdMgr::ResourceId StubOwnerID
)> {
287 static const char *getName() { return "CreateIndirectStubsOwner"; }
290 /// RPC function for destroying an indirect stubs owner.
291 class DestroyIndirectStubsOwner
292 : public rpc::Function
<DestroyIndirectStubsOwner
,
293 void(ResourceIdMgr::ResourceId StubsOwnerID
)> {
295 static const char *getName() { return "DestroyIndirectStubsOwner"; }
298 /// EmitIndirectStubs result is (StubsBase, PtrsBase, NumStubsEmitted).
299 class EmitIndirectStubs
300 : public rpc::Function
<
302 std::tuple
<JITTargetAddress
, JITTargetAddress
, uint32_t>(
303 ResourceIdMgr::ResourceId StubsOwnerID
,
304 uint32_t NumStubsRequired
)> {
306 static const char *getName() { return "EmitIndirectStubs"; }
309 /// RPC function to emit the resolver block and return its address.
310 class EmitResolverBlock
: public rpc::Function
<EmitResolverBlock
, void()> {
312 static const char *getName() { return "EmitResolverBlock"; }
315 /// EmitTrampolineBlock result is (BlockAddr, NumTrampolines).
316 class EmitTrampolineBlock
317 : public rpc::Function
<EmitTrampolineBlock
,
318 std::tuple
<JITTargetAddress
, uint32_t>()> {
320 static const char *getName() { return "EmitTrampolineBlock"; }
323 } // end namespace stubs
325 /// Miscelaneous RPC functions for dealing with remotes.
328 /// GetRemoteInfo result is (Triple, PointerSize, PageSize, TrampolineSize,
329 /// IndirectStubsSize).
331 : public rpc::Function
<
333 std::tuple
<std::string
, uint32_t, uint32_t, uint32_t, uint32_t>()> {
335 static const char *getName() { return "GetRemoteInfo"; }
338 /// Get the address of a remote symbol.
339 class GetSymbolAddress
340 : public rpc::Function
<GetSymbolAddress
,
341 JITTargetAddress(std::string SymbolName
)> {
343 static const char *getName() { return "GetSymbolAddress"; }
346 /// Request that the host execute a compile callback.
348 : public rpc::Function
<
349 RequestCompile
, JITTargetAddress(JITTargetAddress TrampolineAddr
)> {
351 static const char *getName() { return "RequestCompile"; }
354 /// Notify the remote and terminate the session.
355 class TerminateSession
: public rpc::Function
<TerminateSession
, void()> {
357 static const char *getName() { return "TerminateSession"; }
362 class OrcRemoteTargetRPCAPI
363 : public rpc::SingleThreadedRPCEndpoint
<rpc::RawByteChannel
> {
365 // FIXME: Remove constructors once MSVC supports synthesizing move-ops.
366 OrcRemoteTargetRPCAPI(rpc::RawByteChannel
&C
)
367 : rpc::SingleThreadedRPCEndpoint
<rpc::RawByteChannel
>(C
, true) {}
370 } // end namespace remote
372 } // end namespace orc
373 } // end namespace llvm
375 #endif // LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETRPCAPI_H