• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- OrcRemoteTargetRPCAPI.h - Orc Remote-target RPC API ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the Orc remote-target RPC API. It should not be used
11 // directly, but is used by the RemoteTargetClient and RemoteTargetServer
12 // classes.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETRPCAPI_H
17 #define LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETRPCAPI_H
18 
19 #include "JITSymbol.h"
20 #include "RPCChannel.h"
21 #include "RPCUtils.h"
22 
23 namespace llvm {
24 namespace orc {
25 namespace remote {
26 
27 class DirectBufferWriter {
28 public:
29   DirectBufferWriter() = default;
DirectBufferWriter(const char * Src,TargetAddress Dst,uint64_t Size)30   DirectBufferWriter(const char *Src, TargetAddress Dst, uint64_t Size)
31       : Src(Src), Dst(Dst), Size(Size) {}
32 
getSrc()33   const char *getSrc() const { return Src; }
getDst()34   TargetAddress getDst() const { return Dst; }
getSize()35   uint64_t getSize() const { return Size; }
36 
37 private:
38   const char *Src;
39   TargetAddress Dst;
40   uint64_t Size;
41 };
42 
serialize(RPCChannel & C,const DirectBufferWriter & DBW)43 inline Error serialize(RPCChannel &C, const DirectBufferWriter &DBW) {
44   if (auto EC = serialize(C, DBW.getDst()))
45     return EC;
46   if (auto EC = serialize(C, DBW.getSize()))
47     return EC;
48   return C.appendBytes(DBW.getSrc(), DBW.getSize());
49 }
50 
deserialize(RPCChannel & C,DirectBufferWriter & DBW)51 inline Error deserialize(RPCChannel &C, DirectBufferWriter &DBW) {
52   TargetAddress Dst;
53   if (auto EC = deserialize(C, Dst))
54     return EC;
55   uint64_t Size;
56   if (auto EC = deserialize(C, Size))
57     return EC;
58   char *Addr = reinterpret_cast<char *>(static_cast<uintptr_t>(Dst));
59 
60   DBW = DirectBufferWriter(0, Dst, Size);
61 
62   return C.readBytes(Addr, Size);
63 }
64 
65 class OrcRemoteTargetRPCAPI : public RPC<RPCChannel> {
66 protected:
67   class ResourceIdMgr {
68   public:
69     typedef uint64_t ResourceId;
70     static const ResourceId InvalidId = ~0U;
71 
getNext()72     ResourceId getNext() {
73       if (!FreeIds.empty()) {
74         ResourceId I = FreeIds.back();
75         FreeIds.pop_back();
76         return I;
77       }
78       return NextId++;
79     }
release(ResourceId I)80     void release(ResourceId I) { FreeIds.push_back(I); }
81 
82   private:
83     ResourceId NextId = 0;
84     std::vector<ResourceId> FreeIds;
85   };
86 
87 public:
88   // FIXME: Remove constructors once MSVC supports synthesizing move-ops.
89   OrcRemoteTargetRPCAPI() = default;
90   OrcRemoteTargetRPCAPI(const OrcRemoteTargetRPCAPI &) = delete;
91   OrcRemoteTargetRPCAPI &operator=(const OrcRemoteTargetRPCAPI &) = delete;
92 
OrcRemoteTargetRPCAPI(OrcRemoteTargetRPCAPI &&)93   OrcRemoteTargetRPCAPI(OrcRemoteTargetRPCAPI &&) {}
94   OrcRemoteTargetRPCAPI &operator=(OrcRemoteTargetRPCAPI &&) { return *this; }
95 
96   enum JITFuncId : uint32_t {
97     InvalidId = RPCFunctionIdTraits<JITFuncId>::InvalidId,
98     CallIntVoidId = RPCFunctionIdTraits<JITFuncId>::FirstValidId,
99     CallMainId,
100     CallVoidVoidId,
101     CreateRemoteAllocatorId,
102     CreateIndirectStubsOwnerId,
103     DeregisterEHFramesId,
104     DestroyRemoteAllocatorId,
105     DestroyIndirectStubsOwnerId,
106     EmitIndirectStubsId,
107     EmitResolverBlockId,
108     EmitTrampolineBlockId,
109     GetSymbolAddressId,
110     GetRemoteInfoId,
111     ReadMemId,
112     RegisterEHFramesId,
113     ReserveMemId,
114     RequestCompileId,
115     SetProtectionsId,
116     TerminateSessionId,
117     WriteMemId,
118     WritePtrId
119   };
120 
121   static const char *getJITFuncIdName(JITFuncId Id);
122 
123   typedef Function<CallIntVoidId, int32_t(TargetAddress Addr)> CallIntVoid;
124 
125   typedef Function<CallMainId,
126                    int32_t(TargetAddress Addr, std::vector<std::string> Args)>
127       CallMain;
128 
129   typedef Function<CallVoidVoidId, void(TargetAddress FnAddr)> CallVoidVoid;
130 
131   typedef Function<CreateRemoteAllocatorId,
132                    void(ResourceIdMgr::ResourceId AllocatorID)>
133       CreateRemoteAllocator;
134 
135   typedef Function<CreateIndirectStubsOwnerId,
136                    void(ResourceIdMgr::ResourceId StubOwnerID)>
137       CreateIndirectStubsOwner;
138 
139   typedef Function<DeregisterEHFramesId,
140                    void(TargetAddress Addr, uint32_t Size)>
141       DeregisterEHFrames;
142 
143   typedef Function<DestroyRemoteAllocatorId,
144                    void(ResourceIdMgr::ResourceId AllocatorID)>
145       DestroyRemoteAllocator;
146 
147   typedef Function<DestroyIndirectStubsOwnerId,
148                    void(ResourceIdMgr::ResourceId StubsOwnerID)>
149       DestroyIndirectStubsOwner;
150 
151   /// EmitIndirectStubs result is (StubsBase, PtrsBase, NumStubsEmitted).
152   typedef Function<EmitIndirectStubsId,
153                    std::tuple<TargetAddress, TargetAddress, uint32_t>(
154                        ResourceIdMgr::ResourceId StubsOwnerID,
155                        uint32_t NumStubsRequired)>
156       EmitIndirectStubs;
157 
158   typedef Function<EmitResolverBlockId, void()> EmitResolverBlock;
159 
160   /// EmitTrampolineBlock result is (BlockAddr, NumTrampolines).
161   typedef Function<EmitTrampolineBlockId, std::tuple<TargetAddress, uint32_t>()>
162       EmitTrampolineBlock;
163 
164   typedef Function<GetSymbolAddressId, TargetAddress(std::string SymbolName)>
165       GetSymbolAddress;
166 
167   /// GetRemoteInfo result is (Triple, PointerSize, PageSize, TrampolineSize,
168   ///                          IndirectStubsSize).
169   typedef Function<GetRemoteInfoId, std::tuple<std::string, uint32_t, uint32_t,
170                                                uint32_t, uint32_t>()>
171       GetRemoteInfo;
172 
173   typedef Function<ReadMemId,
174                    std::vector<char>(TargetAddress Src, uint64_t Size)>
175       ReadMem;
176 
177   typedef Function<RegisterEHFramesId, void(TargetAddress Addr, uint32_t Size)>
178       RegisterEHFrames;
179 
180   typedef Function<ReserveMemId,
181                    TargetAddress(ResourceIdMgr::ResourceId AllocID,
182                                  uint64_t Size, uint32_t Align)>
183       ReserveMem;
184 
185   typedef Function<RequestCompileId,
186                    TargetAddress(TargetAddress TrampolineAddr)>
187       RequestCompile;
188 
189   typedef Function<SetProtectionsId,
190                    void(ResourceIdMgr::ResourceId AllocID, TargetAddress Dst,
191                         uint32_t ProtFlags)>
192       SetProtections;
193 
194   typedef Function<TerminateSessionId, void()> TerminateSession;
195 
196   typedef Function<WriteMemId, void(DirectBufferWriter DB)> WriteMem;
197 
198   typedef Function<WritePtrId, void(TargetAddress Dst, TargetAddress Val)>
199       WritePtr;
200 };
201 
202 } // end namespace remote
203 } // end namespace orc
204 } // end namespace llvm
205 
206 #endif
207