aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm12/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h
blob: ff0dc7d33f39e7b03a2bae98645cdd05ac6a6b20 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#pragma once

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif

//===- OrcRemoteTargetRPCAPI.h - Orc Remote-target RPC API ------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines the Orc remote-target RPC API. It should not be used
// directly, but is used by the RemoteTargetClient and RemoteTargetServer
// classes.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETRPCAPI_H
#define LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETRPCAPI_H

#include "llvm/ExecutionEngine/JITSymbol.h"
#include "llvm/ExecutionEngine/Orc/Shared/RPCUtils.h"
#include "llvm/ExecutionEngine/Orc/Shared/RawByteChannel.h"

namespace llvm {
namespace orc {

namespace remote {

/// Template error for missing resources.
template <typename ResourceIdT>
class ResourceNotFound
  : public ErrorInfo<ResourceNotFound<ResourceIdT>> {
public:
  static char ID;

  ResourceNotFound(ResourceIdT ResourceId,
                   std::string ResourceDescription = "")
    : ResourceId(std::move(ResourceId)),
      ResourceDescription(std::move(ResourceDescription)) {}

  std::error_code convertToErrorCode() const override {
    return orcError(OrcErrorCode::UnknownResourceHandle);
  }

  void log(raw_ostream &OS) const override {
    OS << (ResourceDescription.empty()
             ? "Remote resource with id "
               : ResourceDescription)
       << " " << ResourceId << " not found";
  }

private:
  ResourceIdT ResourceId;
  std::string ResourceDescription;
};

template <typename ResourceIdT>
char ResourceNotFound<ResourceIdT>::ID = 0;

class DirectBufferWriter {
public:
  DirectBufferWriter() = default;
  DirectBufferWriter(const char *Src, JITTargetAddress Dst, uint64_t Size)
      : Src(Src), Dst(Dst), Size(Size) {}

  const char *getSrc() const { return Src; }
  JITTargetAddress getDst() const { return Dst; }
  uint64_t getSize() const { return Size; }

private:
  const char *Src;
  JITTargetAddress Dst;
  uint64_t Size;
};

} // end namespace remote

namespace shared {

template <> class SerializationTypeName<JITSymbolFlags> {
public:
  static const char *getName() { return "JITSymbolFlags"; }
};

template <typename ChannelT>
class SerializationTraits<ChannelT, JITSymbolFlags> {
public:

  static Error serialize(ChannelT &C, const JITSymbolFlags &Flags) {
    return serializeSeq(C, Flags.getRawFlagsValue(), Flags.getTargetFlags());
  }

  static Error deserialize(ChannelT &C, JITSymbolFlags &Flags) {
    JITSymbolFlags::UnderlyingType JITFlags;
    JITSymbolFlags::TargetFlagsType TargetFlags;
    if (auto Err = deserializeSeq(C, JITFlags, TargetFlags))
      return Err;
    Flags = JITSymbolFlags(static_cast<JITSymbolFlags::FlagNames>(JITFlags),
                           TargetFlags);
    return Error::success();
  }
};

template <> class SerializationTypeName<remote::DirectBufferWriter> {
public:
  static const char *getName() { return "DirectBufferWriter"; }
};

template <typename ChannelT>
class SerializationTraits<
    ChannelT, remote::DirectBufferWriter, remote::DirectBufferWriter,
    std::enable_if_t<std::is_base_of<RawByteChannel, ChannelT>::value>> {
public:
  static Error serialize(ChannelT &C, const remote::DirectBufferWriter &DBW) {
    if (auto EC = serializeSeq(C, DBW.getDst()))
      return EC;
    if (auto EC = serializeSeq(C, DBW.getSize()))
      return EC;
    return C.appendBytes(DBW.getSrc(), DBW.getSize());
  }

  static Error deserialize(ChannelT &C, remote::DirectBufferWriter &DBW) {
    JITTargetAddress Dst;
    if (auto EC = deserializeSeq(C, Dst))
      return EC;
    uint64_t Size;
    if (auto EC = deserializeSeq(C, Size))
      return EC;
    char *Addr = reinterpret_cast<char *>(static_cast<uintptr_t>(Dst));

    DBW = remote::DirectBufferWriter(nullptr, Dst, Size);

    return C.readBytes(Addr, Size);
  }
};

} // end namespace shared

namespace remote {

class ResourceIdMgr {
public:
  using ResourceId = uint64_t;
  static const ResourceId InvalidId = ~0U;

  ResourceIdMgr() = default;
  explicit ResourceIdMgr(ResourceId FirstValidId)
    : NextId(std::move(FirstValidId)) {}

  ResourceId getNext() {
    if (!FreeIds.empty()) {
      ResourceId I = FreeIds.back();
      FreeIds.pop_back();
      return I;
    }
    assert(NextId + 1 != ~0ULL && "All ids allocated");
    return NextId++;
  }

  void release(ResourceId I) { FreeIds.push_back(I); }

private:
  ResourceId NextId = 1;
  std::vector<ResourceId> FreeIds;
};

/// Registers EH frames on the remote.
namespace eh {

  /// Registers EH frames on the remote.
class RegisterEHFrames
    : public shared::RPCFunction<RegisterEHFrames,
                                 void(JITTargetAddress Addr, uint32_t Size)> {
public:
  static const char *getName() { return "RegisterEHFrames"; }
};

  /// Deregisters EH frames on the remote.
class DeregisterEHFrames
    : public shared::RPCFunction<DeregisterEHFrames,
                                 void(JITTargetAddress Addr, uint32_t Size)> {
public:
  static const char *getName() { return "DeregisterEHFrames"; }
};

} // end namespace eh

/// RPC functions for executing remote code.
namespace exec {

  /// Call an 'int32_t()'-type function on the remote, returns the called
  /// function's return value.
class CallIntVoid
    : public shared::RPCFunction<CallIntVoid, int32_t(JITTargetAddress Addr)> {
public:
  static const char *getName() { return "CallIntVoid"; }
};

  /// Call an 'int32_t(int32_t)'-type function on the remote, returns the called
  /// function's return value.
class CallIntInt
    : public shared::RPCFunction<CallIntInt,
                                 int32_t(JITTargetAddress Addr, int)> {
public:
  static const char *getName() { return "CallIntInt"; }
};

  /// Call an 'int32_t(int32_t, char**)'-type function on the remote, returns the
  /// called function's return value.
class CallMain
    : public shared::RPCFunction<CallMain,
                                 int32_t(JITTargetAddress Addr,
                                         std::vector<std::string> Args)> {
public:
  static const char *getName() { return "CallMain"; }
};

  /// Calls a 'void()'-type function on the remote, returns when the called
  /// function completes.
class CallVoidVoid
    : public shared::RPCFunction<CallVoidVoid, void(JITTargetAddress FnAddr)> {
public:
  static const char *getName() { return "CallVoidVoid"; }
};

} // end namespace exec

/// RPC functions for remote memory management / inspection / modification.
namespace mem {

  /// Creates a memory allocator on the remote.
class CreateRemoteAllocator
    : public shared::RPCFunction<CreateRemoteAllocator,
                                 void(ResourceIdMgr::ResourceId AllocatorID)> {
public:
  static const char *getName() { return "CreateRemoteAllocator"; }
};

  /// Destroys a remote allocator, freeing any memory allocated by it.
class DestroyRemoteAllocator
    : public shared::RPCFunction<DestroyRemoteAllocator,
                                 void(ResourceIdMgr::ResourceId AllocatorID)> {
public:
  static const char *getName() { return "DestroyRemoteAllocator"; }
};

  /// Read a remote memory block.
class ReadMem
    : public shared::RPCFunction<
          ReadMem, std::vector<uint8_t>(JITTargetAddress Src, uint64_t Size)> {
public:
  static const char *getName() { return "ReadMem"; }
};

  /// Reserve a block of memory on the remote via the given allocator.
class ReserveMem
    : public shared::RPCFunction<
          ReserveMem, JITTargetAddress(ResourceIdMgr::ResourceId AllocID,
                                       uint64_t Size, uint32_t Align)> {
public:
  static const char *getName() { return "ReserveMem"; }
};

  /// Set the memory protection on a memory block.
class SetProtections
    : public shared::RPCFunction<
          SetProtections, void(ResourceIdMgr::ResourceId AllocID,
                               JITTargetAddress Dst, uint32_t ProtFlags)> {
public:
  static const char *getName() { return "SetProtections"; }
};

  /// Write to a remote memory block.
class WriteMem
    : public shared::RPCFunction<WriteMem,
                                 void(remote::DirectBufferWriter DB)> {
public:
  static const char *getName() { return "WriteMem"; }
};

  /// Write to a remote pointer.
class WritePtr
    : public shared::RPCFunction<WritePtr, void(JITTargetAddress Dst,
                                                JITTargetAddress Val)> {
public:
  static const char *getName() { return "WritePtr"; }
};

} // end namespace mem

/// RPC functions for remote stub and trampoline management.
namespace stubs {

  /// Creates an indirect stub owner on the remote.
class CreateIndirectStubsOwner
    : public shared::RPCFunction<CreateIndirectStubsOwner,
                                 void(ResourceIdMgr::ResourceId StubOwnerID)> {
public:
  static const char *getName() { return "CreateIndirectStubsOwner"; }
};

  /// RPC function for destroying an indirect stubs owner.
class DestroyIndirectStubsOwner
    : public shared::RPCFunction<DestroyIndirectStubsOwner,
                                 void(ResourceIdMgr::ResourceId StubsOwnerID)> {
public:
  static const char *getName() { return "DestroyIndirectStubsOwner"; }
};

  /// EmitIndirectStubs result is (StubsBase, PtrsBase, NumStubsEmitted).
class EmitIndirectStubs
    : public shared::RPCFunction<
          EmitIndirectStubs,
          std::tuple<JITTargetAddress, JITTargetAddress, uint32_t>(
              ResourceIdMgr::ResourceId StubsOwnerID,
              uint32_t NumStubsRequired)> {
public:
  static const char *getName() { return "EmitIndirectStubs"; }
};

  /// RPC function to emit the resolver block and return its address.
class EmitResolverBlock
    : public shared::RPCFunction<EmitResolverBlock, void()> {
public:
  static const char *getName() { return "EmitResolverBlock"; }
};

  /// EmitTrampolineBlock result is (BlockAddr, NumTrampolines).
class EmitTrampolineBlock
    : public shared::RPCFunction<EmitTrampolineBlock,
                                 std::tuple<JITTargetAddress, uint32_t>()> {
public:
  static const char *getName() { return "EmitTrampolineBlock"; }
};

} // end namespace stubs

/// Miscelaneous RPC functions for dealing with remotes.
namespace utils {

  /// GetRemoteInfo result is (Triple, PointerSize, PageSize, TrampolineSize,
  ///                          IndirectStubsSize).
class GetRemoteInfo
    : public shared::RPCFunction<
          GetRemoteInfo,
          std::tuple<std::string, uint32_t, uint32_t, uint32_t, uint32_t>()> {
public:
  static const char *getName() { return "GetRemoteInfo"; }
};

  /// Get the address of a remote symbol.
class GetSymbolAddress
    : public shared::RPCFunction<GetSymbolAddress,
                                 JITTargetAddress(std::string SymbolName)> {
public:
  static const char *getName() { return "GetSymbolAddress"; }
};

  /// Request that the host execute a compile callback.
class RequestCompile
    : public shared::RPCFunction<
          RequestCompile, JITTargetAddress(JITTargetAddress TrampolineAddr)> {
public:
  static const char *getName() { return "RequestCompile"; }
};

  /// Notify the remote and terminate the session.
class TerminateSession : public shared::RPCFunction<TerminateSession, void()> {
public:
  static const char *getName() { return "TerminateSession"; }
};

} // namespace utils

class OrcRemoteTargetRPCAPI
    : public shared::SingleThreadedRPCEndpoint<shared::RawByteChannel> {
public:
  // FIXME: Remove constructors once MSVC supports synthesizing move-ops.
  OrcRemoteTargetRPCAPI(shared::RawByteChannel &C)
      : shared::SingleThreadedRPCEndpoint<shared::RawByteChannel>(C, true) {}
};

} // end namespace remote

} // end namespace orc
} // end namespace llvm

#endif // LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETRPCAPI_H

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif