ExecutorProcessControl.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //===---- ExecutorProcessControl.cpp -- Executor process control APIs -----===//
  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. #include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
  9. #include "llvm/ExecutionEngine/Orc/Core.h"
  10. #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
  11. #include "llvm/Support/FormatVariadic.h"
  12. #include "llvm/Support/Host.h"
  13. #include "llvm/Support/Process.h"
  14. #define DEBUG_TYPE "orc"
  15. namespace llvm {
  16. namespace orc {
  17. ExecutorProcessControl::MemoryAccess::~MemoryAccess() = default;
  18. ExecutorProcessControl::~ExecutorProcessControl() = default;
  19. SelfExecutorProcessControl::SelfExecutorProcessControl(
  20. std::shared_ptr<SymbolStringPool> SSP, std::unique_ptr<TaskDispatcher> D,
  21. Triple TargetTriple, unsigned PageSize,
  22. std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr)
  23. : ExecutorProcessControl(std::move(SSP), std::move(D)) {
  24. OwnedMemMgr = std::move(MemMgr);
  25. if (!OwnedMemMgr)
  26. OwnedMemMgr = std::make_unique<jitlink::InProcessMemoryManager>(
  27. sys::Process::getPageSizeEstimate());
  28. this->TargetTriple = std::move(TargetTriple);
  29. this->PageSize = PageSize;
  30. this->MemMgr = OwnedMemMgr.get();
  31. this->MemAccess = this;
  32. this->JDI = {ExecutorAddr::fromPtr(jitDispatchViaWrapperFunctionManager),
  33. ExecutorAddr::fromPtr(this)};
  34. if (this->TargetTriple.isOSBinFormatMachO())
  35. GlobalManglingPrefix = '_';
  36. }
  37. Expected<std::unique_ptr<SelfExecutorProcessControl>>
  38. SelfExecutorProcessControl::Create(
  39. std::shared_ptr<SymbolStringPool> SSP,
  40. std::unique_ptr<TaskDispatcher> D,
  41. std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr) {
  42. if (!SSP)
  43. SSP = std::make_shared<SymbolStringPool>();
  44. if (!D) {
  45. #if LLVM_ENABLE_THREADS
  46. D = std::make_unique<DynamicThreadPoolTaskDispatcher>();
  47. #else
  48. D = std::make_unique<InPlaceTaskDispatcher>();
  49. #endif
  50. }
  51. auto PageSize = sys::Process::getPageSize();
  52. if (!PageSize)
  53. return PageSize.takeError();
  54. Triple TT(sys::getProcessTriple());
  55. return std::make_unique<SelfExecutorProcessControl>(
  56. std::move(SSP), std::move(D), std::move(TT), *PageSize,
  57. std::move(MemMgr));
  58. }
  59. Expected<tpctypes::DylibHandle>
  60. SelfExecutorProcessControl::loadDylib(const char *DylibPath) {
  61. std::string ErrMsg;
  62. auto Dylib = sys::DynamicLibrary::getPermanentLibrary(DylibPath, &ErrMsg);
  63. if (!Dylib.isValid())
  64. return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
  65. return ExecutorAddr::fromPtr(Dylib.getOSSpecificHandle());
  66. }
  67. Expected<std::vector<tpctypes::LookupResult>>
  68. SelfExecutorProcessControl::lookupSymbols(ArrayRef<LookupRequest> Request) {
  69. std::vector<tpctypes::LookupResult> R;
  70. for (auto &Elem : Request) {
  71. sys::DynamicLibrary Dylib(Elem.Handle.toPtr<void *>());
  72. R.push_back(std::vector<ExecutorAddr>());
  73. for (auto &KV : Elem.Symbols) {
  74. auto &Sym = KV.first;
  75. std::string Tmp((*Sym).data() + !!GlobalManglingPrefix,
  76. (*Sym).size() - !!GlobalManglingPrefix);
  77. void *Addr = Dylib.getAddressOfSymbol(Tmp.c_str());
  78. if (!Addr && KV.second == SymbolLookupFlags::RequiredSymbol) {
  79. // FIXME: Collect all failing symbols before erroring out.
  80. SymbolNameVector MissingSymbols;
  81. MissingSymbols.push_back(Sym);
  82. return make_error<SymbolsNotFound>(SSP, std::move(MissingSymbols));
  83. }
  84. R.back().push_back(ExecutorAddr::fromPtr(Addr));
  85. }
  86. }
  87. return R;
  88. }
  89. Expected<int32_t>
  90. SelfExecutorProcessControl::runAsMain(ExecutorAddr MainFnAddr,
  91. ArrayRef<std::string> Args) {
  92. using MainTy = int (*)(int, char *[]);
  93. return orc::runAsMain(MainFnAddr.toPtr<MainTy>(), Args);
  94. }
  95. Expected<int32_t>
  96. SelfExecutorProcessControl::runAsVoidFunction(ExecutorAddr VoidFnAddr) {
  97. using VoidTy = int (*)();
  98. return orc::runAsVoidFunction(VoidFnAddr.toPtr<VoidTy>());
  99. }
  100. Expected<int32_t>
  101. SelfExecutorProcessControl::runAsIntFunction(ExecutorAddr IntFnAddr, int Arg) {
  102. using IntTy = int (*)(int);
  103. return orc::runAsIntFunction(IntFnAddr.toPtr<IntTy>(), Arg);
  104. }
  105. void SelfExecutorProcessControl::callWrapperAsync(ExecutorAddr WrapperFnAddr,
  106. IncomingWFRHandler SendResult,
  107. ArrayRef<char> ArgBuffer) {
  108. using WrapperFnTy =
  109. shared::CWrapperFunctionResult (*)(const char *Data, size_t Size);
  110. auto *WrapperFn = WrapperFnAddr.toPtr<WrapperFnTy>();
  111. SendResult(WrapperFn(ArgBuffer.data(), ArgBuffer.size()));
  112. }
  113. Error SelfExecutorProcessControl::disconnect() {
  114. D->shutdown();
  115. return Error::success();
  116. }
  117. void SelfExecutorProcessControl::writeUInt8sAsync(
  118. ArrayRef<tpctypes::UInt8Write> Ws, WriteResultFn OnWriteComplete) {
  119. for (auto &W : Ws)
  120. *W.Addr.toPtr<uint8_t *>() = W.Value;
  121. OnWriteComplete(Error::success());
  122. }
  123. void SelfExecutorProcessControl::writeUInt16sAsync(
  124. ArrayRef<tpctypes::UInt16Write> Ws, WriteResultFn OnWriteComplete) {
  125. for (auto &W : Ws)
  126. *W.Addr.toPtr<uint16_t *>() = W.Value;
  127. OnWriteComplete(Error::success());
  128. }
  129. void SelfExecutorProcessControl::writeUInt32sAsync(
  130. ArrayRef<tpctypes::UInt32Write> Ws, WriteResultFn OnWriteComplete) {
  131. for (auto &W : Ws)
  132. *W.Addr.toPtr<uint32_t *>() = W.Value;
  133. OnWriteComplete(Error::success());
  134. }
  135. void SelfExecutorProcessControl::writeUInt64sAsync(
  136. ArrayRef<tpctypes::UInt64Write> Ws, WriteResultFn OnWriteComplete) {
  137. for (auto &W : Ws)
  138. *W.Addr.toPtr<uint64_t *>() = W.Value;
  139. OnWriteComplete(Error::success());
  140. }
  141. void SelfExecutorProcessControl::writeBuffersAsync(
  142. ArrayRef<tpctypes::BufferWrite> Ws, WriteResultFn OnWriteComplete) {
  143. for (auto &W : Ws)
  144. memcpy(W.Addr.toPtr<char *>(), W.Buffer.data(), W.Buffer.size());
  145. OnWriteComplete(Error::success());
  146. }
  147. shared::CWrapperFunctionResult
  148. SelfExecutorProcessControl::jitDispatchViaWrapperFunctionManager(
  149. void *Ctx, const void *FnTag, const char *Data, size_t Size) {
  150. LLVM_DEBUG({
  151. dbgs() << "jit-dispatch call with tag " << FnTag << " and " << Size
  152. << " byte payload.\n";
  153. });
  154. std::promise<shared::WrapperFunctionResult> ResultP;
  155. auto ResultF = ResultP.get_future();
  156. static_cast<SelfExecutorProcessControl *>(Ctx)
  157. ->getExecutionSession()
  158. .runJITDispatchHandler(
  159. [ResultP = std::move(ResultP)](
  160. shared::WrapperFunctionResult Result) mutable {
  161. ResultP.set_value(std::move(Result));
  162. },
  163. pointerToJITTargetAddress(FnTag), {Data, Size});
  164. return ResultF.get().release();
  165. }
  166. } // end namespace orc
  167. } // end namespace llvm