SimpleRemoteEPC.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===---- SimpleRemoteEPC.h - Simple remote executor control ----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Simple remote executor process control.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_EXECUTIONENGINE_ORC_SIMPLEREMOTEEPC_H
  18. #define LLVM_EXECUTIONENGINE_ORC_SIMPLEREMOTEEPC_H
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/ADT/FunctionExtras.h"
  21. #include "llvm/ExecutionEngine/Orc/EPCGenericDylibManager.h"
  22. #include "llvm/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.h"
  23. #include "llvm/ExecutionEngine/Orc/EPCGenericMemoryAccess.h"
  24. #include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
  25. #include "llvm/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.h"
  26. #include "llvm/Support/Error.h"
  27. #include "llvm/Support/MSVCErrorWorkarounds.h"
  28. #include <future>
  29. namespace llvm {
  30. namespace orc {
  31. class SimpleRemoteEPC : public ExecutorProcessControl,
  32. public SimpleRemoteEPCTransportClient {
  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 =
  39. Expected<std::unique_ptr<jitlink::JITLinkMemoryManager>>(
  40. SimpleRemoteEPC &);
  41. using CreateMemoryAccessFn =
  42. Expected<std::unique_ptr<MemoryAccess>>(SimpleRemoteEPC &);
  43. unique_function<CreateMemoryManagerFn> CreateMemoryManager;
  44. unique_function<CreateMemoryAccessFn> CreateMemoryAccess;
  45. };
  46. /// Create a SimpleRemoteEPC using the given transport type and args.
  47. template <typename TransportT, typename... TransportTCtorArgTs>
  48. static Expected<std::unique_ptr<SimpleRemoteEPC>>
  49. Create(std::unique_ptr<TaskDispatcher> D, Setup S,
  50. TransportTCtorArgTs &&...TransportTCtorArgs) {
  51. std::unique_ptr<SimpleRemoteEPC> SREPC(
  52. new SimpleRemoteEPC(std::make_shared<SymbolStringPool>(),
  53. std::move(D)));
  54. auto T = TransportT::Create(
  55. *SREPC, std::forward<TransportTCtorArgTs>(TransportTCtorArgs)...);
  56. if (!T)
  57. return T.takeError();
  58. SREPC->T = std::move(*T);
  59. if (auto Err = SREPC->setup(std::move(S)))
  60. return joinErrors(std::move(Err), SREPC->disconnect());
  61. return std::move(SREPC);
  62. }
  63. SimpleRemoteEPC(const SimpleRemoteEPC &) = delete;
  64. SimpleRemoteEPC &operator=(const SimpleRemoteEPC &) = delete;
  65. SimpleRemoteEPC(SimpleRemoteEPC &&) = delete;
  66. SimpleRemoteEPC &operator=(SimpleRemoteEPC &&) = delete;
  67. ~SimpleRemoteEPC();
  68. Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override;
  69. Expected<std::vector<tpctypes::LookupResult>>
  70. lookupSymbols(ArrayRef<LookupRequest> Request) override;
  71. Expected<int32_t> runAsMain(ExecutorAddr MainFnAddr,
  72. ArrayRef<std::string> Args) override;
  73. Expected<int32_t> runAsVoidFunction(ExecutorAddr VoidFnAddr) override;
  74. Expected<int32_t> runAsIntFunction(ExecutorAddr IntFnAddr, int Arg) override;
  75. void callWrapperAsync(ExecutorAddr WrapperFnAddr,
  76. IncomingWFRHandler OnComplete,
  77. ArrayRef<char> ArgBuffer) override;
  78. Error disconnect() override;
  79. Expected<HandleMessageAction>
  80. handleMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo, ExecutorAddr TagAddr,
  81. SimpleRemoteEPCArgBytesVector ArgBytes) override;
  82. void handleDisconnect(Error Err) override;
  83. private:
  84. SimpleRemoteEPC(std::shared_ptr<SymbolStringPool> SSP,
  85. std::unique_ptr<TaskDispatcher> D)
  86. : ExecutorProcessControl(std::move(SSP), std::move(D)) {}
  87. static Expected<std::unique_ptr<jitlink::JITLinkMemoryManager>>
  88. createDefaultMemoryManager(SimpleRemoteEPC &SREPC);
  89. static Expected<std::unique_ptr<MemoryAccess>>
  90. createDefaultMemoryAccess(SimpleRemoteEPC &SREPC);
  91. Error sendMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo,
  92. ExecutorAddr TagAddr, ArrayRef<char> ArgBytes);
  93. Error handleSetup(uint64_t SeqNo, ExecutorAddr TagAddr,
  94. SimpleRemoteEPCArgBytesVector ArgBytes);
  95. Error setup(Setup S);
  96. Error handleResult(uint64_t SeqNo, ExecutorAddr TagAddr,
  97. SimpleRemoteEPCArgBytesVector ArgBytes);
  98. void handleCallWrapper(uint64_t RemoteSeqNo, ExecutorAddr TagAddr,
  99. SimpleRemoteEPCArgBytesVector ArgBytes);
  100. Error handleHangup(SimpleRemoteEPCArgBytesVector ArgBytes);
  101. uint64_t getNextSeqNo() { return NextSeqNo++; }
  102. void releaseSeqNo(uint64_t SeqNo) {}
  103. using PendingCallWrapperResultsMap =
  104. DenseMap<uint64_t, IncomingWFRHandler>;
  105. std::mutex SimpleRemoteEPCMutex;
  106. std::condition_variable DisconnectCV;
  107. bool Disconnected = false;
  108. Error DisconnectErr = Error::success();
  109. std::unique_ptr<SimpleRemoteEPCTransport> T;
  110. std::unique_ptr<jitlink::JITLinkMemoryManager> OwnedMemMgr;
  111. std::unique_ptr<MemoryAccess> OwnedMemAccess;
  112. std::unique_ptr<EPCGenericDylibManager> DylibMgr;
  113. ExecutorAddr RunAsMainAddr;
  114. ExecutorAddr RunAsVoidFunctionAddr;
  115. ExecutorAddr RunAsIntFunctionAddr;
  116. uint64_t NextSeqNo = 0;
  117. PendingCallWrapperResultsMap PendingCallWrapperResults;
  118. };
  119. } // end namespace orc
  120. } // end namespace llvm
  121. #endif // LLVM_EXECUTIONENGINE_ORC_SIMPLEREMOTEEPC_H
  122. #ifdef __GNUC__
  123. #pragma GCC diagnostic pop
  124. #endif