[llvm] 2c8e784 - [ORC] Add Shared/OrcRTBridge, and TargetProcess/OrcRTBootstrap.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 13 17:27:22 PDT 2021


Author: Lang Hames
Date: 2021-09-14T10:19:45+10:00
New Revision: 2c8e784915887f72f13ee49cd513efb446eb23be

URL: https://github.com/llvm/llvm-project/commit/2c8e784915887f72f13ee49cd513efb446eb23be
DIFF: https://github.com/llvm/llvm-project/commit/2c8e784915887f72f13ee49cd513efb446eb23be.diff

LOG: [ORC] Add Shared/OrcRTBridge, and TargetProcess/OrcRTBootstrap.

This is a small first step towards reorganization of the ORC libraries:

Declarations for types and function names (as strings) to be found in the
"ORC runtime bootstrap" set are moved into OrcRTBridge.h / OrcRTBridge.cpp.

The current implementation of the "ORC runtime bootstrap" functions is moved
into OrcRTBootstrap.h and OrcRTBootstrap.cpp. It is likely that this code will
eventually be moved into ORT-RT proper (in compiler RT).

The immediate goal of this change is to make these bootstrap functions usable
for clients other than SimpleRemoteEPC/SimpleRemoteEPCServer. The first planned
client is a new RuntimeDyld::MemoryManager that will run over EPC, which will
allow us to remove the old OrcRemoteTarget code.

Added: 
    llvm/include/llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h
    llvm/lib/ExecutionEngine/Orc/Shared/OrcRTBridge.cpp
    llvm/lib/ExecutionEngine/Orc/TargetProcess/OrcRTBootstrap.cpp
    llvm/lib/ExecutionEngine/Orc/TargetProcess/OrcRTBootstrap.h

Modified: 
    llvm/include/llvm/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.h
    llvm/include/llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h
    llvm/lib/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.cpp
    llvm/lib/ExecutionEngine/Orc/Shared/CMakeLists.txt
    llvm/lib/ExecutionEngine/Orc/SimpleRemoteEPC.cpp
    llvm/lib/ExecutionEngine/Orc/TargetProcess/CMakeLists.txt
    llvm/lib/ExecutionEngine/Orc/TargetProcess/SimpleRemoteEPCServer.cpp
    llvm/unittests/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManagerTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h b/llvm/include/llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h
new file mode 100644
index 0000000000000..688439f539e0a
--- /dev/null
+++ b/llvm/include/llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h
@@ -0,0 +1,46 @@
+//===---- OrcRTBridge.h -- Utils for interacting with orc-rt ----*- 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Declares types and symbol names provided by the ORC runtime.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_EXECUTIONENGINE_ORC_SHARED_ORCRTBRIDGE_H
+#define LLVM_EXECUTIONENGINE_ORC_SHARED_ORCRTBRIDGE_H
+
+#include "llvm/ADT/StringMap.h"
+#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
+#include "llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h"
+
+namespace llvm {
+namespace orc {
+namespace rt {
+
+extern const char *MemoryReserveWrapperName;
+extern const char *MemoryFinalizeWrapperName;
+extern const char *MemoryDeallocateWrapperName;
+extern const char *MemoryWriteUInt8sWrapperName;
+extern const char *MemoryWriteUInt16sWrapperName;
+extern const char *MemoryWriteUInt32sWrapperName;
+extern const char *MemoryWriteUInt64sWrapperName;
+extern const char *MemoryWriteBuffersWrapperName;
+extern const char *RunAsMainWrapperName;
+
+using SPSMemoryReserveSignature =
+    shared::SPSExpected<shared::SPSExecutorAddress>(uint64_t);
+using SPSMemoryFinalizeSignature = shared::SPSError(shared::SPSFinalizeRequest);
+using SPSMemoryDeallocateSignature =
+    shared::SPSError(shared::SPSExecutorAddress, uint64_t);
+using SPSRunAsMainSignature = int64_t(shared::SPSExecutorAddress,
+                                      shared::SPSSequence<shared::SPSString>);
+
+} // end namespace rt
+} // end namespace orc
+} // end namespace llvm
+
+#endif // LLVM_EXECUTIONENGINE_ORC_SHARED_ORCRTBRIDGE_H

diff  --git a/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.h b/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.h
index c9d0d9889d3d2..3a1e9ae3783b9 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.h
@@ -211,9 +211,6 @@ class SPSSerializationTraits<SPSSimpleRemoteEPCExecutorInfo,
   }
 };
 
-using SPSRunAsMainSignature = int64_t(SPSExecutorAddress,
-                                      SPSSequence<SPSString>);
-
 using SPSLoadDylibSignature =
     SPSExpected<SPSExecutorAddress>(SPSExecutorAddress, SPSString, uint64_t);
 

diff  --git a/llvm/include/llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h b/llvm/include/llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h
index 815dd73ff2877..f4fd6199ef012 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h
@@ -222,9 +222,6 @@ class SPSSerializationTraits<SPSMemoryAccessBufferWrite,
   }
 };
 
-using SPSOrcTargetProcessAllocate = SPSExpected<SPSExecutorAddress>(uint64_t);
-using SPSOrcTargetProcessFinalize = SPSError(SPSFinalizeRequest);
-using SPSOrcTargetProcessDeallocate = SPSError(SPSExecutorAddress, uint64_t);
 
 } // end namespace shared
 } // end namespace orc

diff  --git a/llvm/lib/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.cpp b/llvm/lib/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.cpp
index 5303b7cf2e5a5..0cdb0aa7c04ab 100644
--- a/llvm/lib/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.cpp
@@ -8,6 +8,7 @@
 
 #include "llvm/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.h"
 #include "llvm/ExecutionEngine/Orc/LookupAndRecordAddrs.h"
+#include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
 
 #include <limits>
 
@@ -58,7 +59,7 @@ class EPCGenericJITLinkMemoryManager::Alloc
           {WorkingMem, static_cast<size_t>(KV.second.ContentSize)}});
       WorkingMem += KV.second.ContentSize;
     }
-    Parent.EPC.callSPSWrapperAsync<shared::SPSOrcTargetProcessFinalize>(
+    Parent.EPC.callSPSWrapperAsync<rt::SPSMemoryFinalizeSignature>(
         [OnFinalize = std::move(OnFinalize)](Error SerializationErr,
                                              Error FinalizeErr) {
           if (SerializationErr)
@@ -71,9 +72,8 @@ class EPCGenericJITLinkMemoryManager::Alloc
 
   Error deallocate() override {
     Error Err = Error::success();
-    if (auto E2 =
-            Parent.EPC.callSPSWrapper<shared::SPSOrcTargetProcessDeallocate>(
-                Parent.FAs.Deallocate.getValue(), Err, TargetAddr, TargetSize))
+    if (auto E2 = Parent.EPC.callSPSWrapper<rt::SPSMemoryDeallocateSignature>(
+            Parent.FAs.Deallocate.getValue(), Err, TargetAddr, TargetSize))
       return E2;
     return Err;
   }
@@ -111,7 +111,7 @@ EPCGenericJITLinkMemoryManager::allocate(const jitlink::JITLinkDylib *JD,
   if (WorkingSize > 0)
     WorkingBuffer = std::make_unique<char[]>(WorkingSize);
   Expected<ExecutorAddress> TargetAllocAddr((ExecutorAddress()));
-  if (auto Err = EPC.callSPSWrapper<shared::SPSOrcTargetProcessAllocate>(
+  if (auto Err = EPC.callSPSWrapper<rt::SPSMemoryReserveSignature>(
           FAs.Reserve.getValue(), TargetAllocAddr, AllocSize))
     return std::move(Err);
   if (!TargetAllocAddr)

diff  --git a/llvm/lib/ExecutionEngine/Orc/Shared/CMakeLists.txt b/llvm/lib/ExecutionEngine/Orc/Shared/CMakeLists.txt
index 08f0f19213107..bf734ba99e171 100644
--- a/llvm/lib/ExecutionEngine/Orc/Shared/CMakeLists.txt
+++ b/llvm/lib/ExecutionEngine/Orc/Shared/CMakeLists.txt
@@ -1,5 +1,6 @@
 add_llvm_component_library(LLVMOrcShared
   OrcError.cpp
+  OrcRTBridge.cpp
   RPCError.cpp
   SimpleRemoteEPCUtils.cpp
   ADDITIONAL_HEADER_DIRS

diff  --git a/llvm/lib/ExecutionEngine/Orc/Shared/OrcRTBridge.cpp b/llvm/lib/ExecutionEngine/Orc/Shared/OrcRTBridge.cpp
new file mode 100644
index 0000000000000..16fee1fd03cab
--- /dev/null
+++ b/llvm/lib/ExecutionEngine/Orc/Shared/OrcRTBridge.cpp
@@ -0,0 +1,35 @@
+//===------ OrcRTBridge.cpp - Executor functions for bootstrap -----===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
+
+namespace llvm {
+namespace orc {
+namespace rt {
+
+const char *MemoryReserveWrapperName =
+    "__llvm_orc_bootstrap_mem_reserve_wrapper";
+const char *MemoryFinalizeWrapperName =
+    "__llvm_orc_bootstrap_mem_finalize_wrapper";
+const char *MemoryDeallocateWrapperName =
+    "__llvm_orc_bootstrap_mem_deallocate_wrapper";
+const char *MemoryWriteUInt8sWrapperName =
+    "__llvm_orc_bootstrap_mem_write_uint8s_wrapper";
+const char *MemoryWriteUInt16sWrapperName =
+    "__llvm_orc_bootstrap_mem_write_uint16s_wrapper";
+const char *MemoryWriteUInt32sWrapperName =
+    "__llvm_orc_bootstrap_mem_write_uint32s_wrapper";
+const char *MemoryWriteUInt64sWrapperName =
+    "__llvm_orc_bootstrap_mem_write_uint64s_wrapper";
+const char *MemoryWriteBuffersWrapperName =
+    "__llvm_orc_bootstrap_mem_write_buffers_wrapper";
+const char *RunAsMainWrapperName = "__llvm_orc_bootstrap_run_as_main_wrapper";
+
+} // end namespace rt
+} // end namespace orc
+} // end namespace llvm

diff  --git a/llvm/lib/ExecutionEngine/Orc/SimpleRemoteEPC.cpp b/llvm/lib/ExecutionEngine/Orc/SimpleRemoteEPC.cpp
index 22e49eb69bcde..c9d7db21298ac 100644
--- a/llvm/lib/ExecutionEngine/Orc/SimpleRemoteEPC.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/SimpleRemoteEPC.cpp
@@ -9,6 +9,7 @@
 #include "llvm/ExecutionEngine/Orc/SimpleRemoteEPC.h"
 #include "llvm/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.h"
 #include "llvm/ExecutionEngine/Orc/EPCGenericMemoryAccess.h"
+#include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
 #include "llvm/Support/FormatVariadic.h"
 
 #define DEBUG_TYPE "orc"
@@ -89,7 +90,7 @@ SimpleRemoteEPC::lookupSymbols(ArrayRef<LookupRequest> Request) {
 Expected<int32_t> SimpleRemoteEPC::runAsMain(JITTargetAddress MainFnAddr,
                                              ArrayRef<std::string> Args) {
   int64_t Result = 0;
-  if (auto Err = callSPSWrapper<shared::SPSRunAsMainSignature>(
+  if (auto Err = callSPSWrapper<rt::SPSRunAsMainSignature>(
           RunAsMainAddr.getValue(), Result, ExecutorAddress(MainFnAddr), Args))
     return std::move(Err);
   return Result;
@@ -171,9 +172,9 @@ Expected<std::unique_ptr<jitlink::JITLinkMemoryManager>>
 SimpleRemoteEPC::createMemoryManager() {
   EPCGenericJITLinkMemoryManager::FuncAddrs FAs;
   if (auto Err = getBootstrapSymbols(
-          {{FAs.Reserve, "__llvm_orc_memory_reserve"},
-           {FAs.Finalize, "__llvm_orc_memory_finalize"},
-           {FAs.Deallocate, "__llvm_orc_memory_deallocate"}}))
+          {{FAs.Reserve, rt::MemoryReserveWrapperName},
+           {FAs.Finalize, rt::MemoryFinalizeWrapperName},
+           {FAs.Deallocate, rt::MemoryDeallocateWrapperName}}))
     return std::move(Err);
 
   return std::make_unique<EPCGenericJITLinkMemoryManager>(*this, FAs);
@@ -252,7 +253,7 @@ Error SimpleRemoteEPC::setup(std::unique_ptr<SimpleRemoteEPCTransport> T,
            {JDI.JITDispatchFunctionAddress, DispatchFnName},
            {LoadDylibAddr, "__llvm_orc_load_dylib"},
            {LookupSymbolsAddr, "__llvm_orc_lookup_symbols"},
-           {RunAsMainAddr, "__llvm_orc_run_as_main"}}))
+           {RunAsMainAddr, rt::RunAsMainWrapperName}}))
     return Err;
 
   if (auto MemMgr = createMemoryManager()) {

diff  --git a/llvm/lib/ExecutionEngine/Orc/TargetProcess/CMakeLists.txt b/llvm/lib/ExecutionEngine/Orc/TargetProcess/CMakeLists.txt
index 2cc89db8c0bee..1ae02c30298b4 100644
--- a/llvm/lib/ExecutionEngine/Orc/TargetProcess/CMakeLists.txt
+++ b/llvm/lib/ExecutionEngine/Orc/TargetProcess/CMakeLists.txt
@@ -1,5 +1,6 @@
 add_llvm_component_library(LLVMOrcTargetProcess
   JITLoaderGDB.cpp
+  OrcRTBootstrap.cpp
   RegisterEHFrames.cpp
   SimpleRemoteEPCServer.cpp
   TargetExecutionUtils.cpp

diff  --git a/llvm/lib/ExecutionEngine/Orc/TargetProcess/OrcRTBootstrap.cpp b/llvm/lib/ExecutionEngine/Orc/TargetProcess/OrcRTBootstrap.cpp
new file mode 100644
index 0000000000000..a95bcdbe1b79b
--- /dev/null
+++ b/llvm/lib/ExecutionEngine/Orc/TargetProcess/OrcRTBootstrap.cpp
@@ -0,0 +1,134 @@
+//===------------------------ OrcRTBootstrap.cpp --------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "OrcRTBootstrap.h"
+
+#include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
+#include "llvm/ExecutionEngine/Orc/Shared/WrapperFunctionUtils.h"
+#include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
+
+#define DEBUG_TYPE "orc"
+
+using namespace llvm::orc::shared;
+
+namespace llvm {
+namespace orc {
+namespace rt_bootstrap {
+
+static llvm::orc::shared::detail::CWrapperFunctionResult
+reserveWrapper(const char *ArgData, size_t ArgSize) {
+  return WrapperFunction<rt::SPSMemoryReserveSignature>::handle(
+             ArgData, ArgSize,
+             [](uint64_t Size) -> Expected<ExecutorAddress> {
+               std::error_code EC;
+               auto MB = sys::Memory::allocateMappedMemory(
+                   Size, 0, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
+               if (EC)
+                 return errorCodeToError(EC);
+               return ExecutorAddress::fromPtr(MB.base());
+             })
+      .release();
+}
+
+static llvm::orc::shared::detail::CWrapperFunctionResult
+finalizeWrapper(const char *ArgData, size_t ArgSize) {
+  return WrapperFunction<rt::SPSMemoryFinalizeSignature>::handle(
+             ArgData, ArgSize,
+             [](const tpctypes::FinalizeRequest &FR) -> Error {
+               for (auto &Seg : FR) {
+                 char *Mem = Seg.Addr.toPtr<char *>();
+                 memcpy(Mem, Seg.Content.data(), Seg.Content.size());
+                 memset(Mem + Seg.Content.size(), 0,
+                        Seg.Size - Seg.Content.size());
+                 assert(Seg.Size <= std::numeric_limits<size_t>::max());
+                 if (auto EC = sys::Memory::protectMappedMemory(
+                         {Mem, static_cast<size_t>(Seg.Size)},
+                         tpctypes::fromWireProtectionFlags(Seg.Prot)))
+                   return errorCodeToError(EC);
+                 if (Seg.Prot & tpctypes::WPF_Exec)
+                   sys::Memory::InvalidateInstructionCache(Mem, Seg.Size);
+               }
+               return Error::success();
+             })
+      .release();
+}
+
+static llvm::orc::shared::detail::CWrapperFunctionResult
+deallocateWrapper(const char *ArgData, size_t ArgSize) {
+  return WrapperFunction<rt::SPSMemoryDeallocateSignature>::handle(
+             ArgData, ArgSize,
+             [](ExecutorAddress Base, uint64_t Size) -> Error {
+               sys::MemoryBlock MB(Base.toPtr<void *>(), Size);
+               if (auto EC = sys::Memory::releaseMappedMemory(MB))
+                 return errorCodeToError(EC);
+               return Error::success();
+             })
+      .release();
+}
+
+template <typename WriteT, typename SPSWriteT>
+static llvm::orc::shared::detail::CWrapperFunctionResult
+writeUIntsWrapper(const char *ArgData, size_t ArgSize) {
+  return WrapperFunction<void(SPSSequence<SPSWriteT>)>::handle(
+             ArgData, ArgSize,
+             [](std::vector<WriteT> Ws) {
+               for (auto &W : Ws)
+                 *jitTargetAddressToPointer<decltype(W.Value) *>(W.Address) =
+                     W.Value;
+             })
+      .release();
+}
+
+static llvm::orc::shared::detail::CWrapperFunctionResult
+writeBuffersWrapper(const char *ArgData, size_t ArgSize) {
+  return WrapperFunction<void(SPSSequence<SPSMemoryAccessBufferWrite>)>::handle(
+             ArgData, ArgSize,
+             [](std::vector<tpctypes::BufferWrite> Ws) {
+               for (auto &W : Ws)
+                 memcpy(jitTargetAddressToPointer<char *>(W.Address),
+                        W.Buffer.data(), W.Buffer.size());
+             })
+      .release();
+}
+
+static llvm::orc::shared::detail::CWrapperFunctionResult
+runAsMainWrapper(const char *ArgData, size_t ArgSize) {
+  return WrapperFunction<rt::SPSRunAsMainSignature>::handle(
+             ArgData, ArgSize,
+             [](ExecutorAddress MainAddr,
+                std::vector<std::string> Args) -> int64_t {
+               return runAsMain(MainAddr.toPtr<int (*)(int, char *[])>(), Args);
+             })
+      .release();
+}
+
+void addTo(StringMap<ExecutorAddress> &M) {
+  M[rt::MemoryReserveWrapperName] = ExecutorAddress::fromPtr(&reserveWrapper);
+  M[rt::MemoryFinalizeWrapperName] = ExecutorAddress::fromPtr(&finalizeWrapper);
+  M[rt::MemoryDeallocateWrapperName] =
+      ExecutorAddress::fromPtr(&deallocateWrapper);
+  M[rt::MemoryWriteUInt8sWrapperName] = ExecutorAddress::fromPtr(
+      &writeUIntsWrapper<tpctypes::UInt8Write,
+                         shared::SPSMemoryAccessUInt8Write>);
+  M[rt::MemoryWriteUInt16sWrapperName] = ExecutorAddress::fromPtr(
+      &writeUIntsWrapper<tpctypes::UInt16Write,
+                         shared::SPSMemoryAccessUInt16Write>);
+  M[rt::MemoryWriteUInt32sWrapperName] = ExecutorAddress::fromPtr(
+      &writeUIntsWrapper<tpctypes::UInt32Write,
+                         shared::SPSMemoryAccessUInt32Write>);
+  M[rt::MemoryWriteUInt64sWrapperName] = ExecutorAddress::fromPtr(
+      &writeUIntsWrapper<tpctypes::UInt64Write,
+                         shared::SPSMemoryAccessUInt64Write>);
+  M[rt::MemoryWriteBuffersWrapperName] =
+      ExecutorAddress::fromPtr(&writeBuffersWrapper);
+  M[rt::RunAsMainWrapperName] = ExecutorAddress::fromPtr(&runAsMainWrapper);
+}
+
+} // end namespace rt_bootstrap
+} // end namespace orc
+} // end namespace llvm

diff  --git a/llvm/lib/ExecutionEngine/Orc/TargetProcess/OrcRTBootstrap.h b/llvm/lib/ExecutionEngine/Orc/TargetProcess/OrcRTBootstrap.h
new file mode 100644
index 0000000000000..008e87071eead
--- /dev/null
+++ b/llvm/lib/ExecutionEngine/Orc/TargetProcess/OrcRTBootstrap.h
@@ -0,0 +1,36 @@
+//===----------------------- OrcRTBootstrap.h -------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+//
+// OrcRTPrelinkImpl provides functions that should be linked into the executor
+// to bootstrap common JIT functionality (e.g. memory allocation and memory
+// access).
+//
+// Call rt_impl::addTo to add these functions to a bootstrap symbols map.
+//
+// FIXME: The functionality in this file should probably be moved to an ORC
+// runtime bootstrap library in compiler-rt.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LIB_EXECUTIONENGINE_ORC_TARGETPROCESS_ORCRTBOOTSTRAP_H
+#define LIB_EXECUTIONENGINE_ORC_TARGETPROCESS_ORCRTBOOTSTRAP_H
+
+#include "llvm/ADT/StringMap.h"
+#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
+
+namespace llvm {
+namespace orc {
+namespace rt_bootstrap {
+
+void addTo(StringMap<ExecutorAddress> &M);
+
+} // namespace rt_bootstrap
+} // end namespace orc
+} // end namespace llvm
+
+#endif // LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_ORCRTBOOTSTRAP_H

diff  --git a/llvm/lib/ExecutionEngine/Orc/TargetProcess/SimpleRemoteEPCServer.cpp b/llvm/lib/ExecutionEngine/Orc/TargetProcess/SimpleRemoteEPCServer.cpp
index ce341ff1583c4..e95c6cb1f3a74 100644
--- a/llvm/lib/ExecutionEngine/Orc/TargetProcess/SimpleRemoteEPCServer.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/TargetProcess/SimpleRemoteEPCServer.cpp
@@ -9,11 +9,12 @@
 #include "llvm/ExecutionEngine/Orc/TargetProcess/SimpleRemoteEPCServer.h"
 
 #include "llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h"
-#include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/Host.h"
 #include "llvm/Support/Process.h"
 
+#include "OrcRTBootstrap.h"
+
 #define DEBUG_TYPE "orc"
 
 using namespace llvm::orc::shared;
@@ -21,93 +22,6 @@ using namespace llvm::orc::shared;
 namespace llvm {
 namespace orc {
 
-static llvm::orc::shared::detail::CWrapperFunctionResult
-reserveWrapper(const char *ArgData, size_t ArgSize) {
-  return WrapperFunction<SPSOrcTargetProcessAllocate>::handle(
-             ArgData, ArgSize,
-             [](uint64_t Size) -> Expected<ExecutorAddress> {
-               std::error_code EC;
-               auto MB = sys::Memory::allocateMappedMemory(
-                   Size, 0, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
-               if (EC)
-                 return errorCodeToError(EC);
-               return ExecutorAddress::fromPtr(MB.base());
-             })
-      .release();
-}
-
-static llvm::orc::shared::detail::CWrapperFunctionResult
-finalizeWrapper(const char *ArgData, size_t ArgSize) {
-  return WrapperFunction<SPSOrcTargetProcessFinalize>::handle(
-             ArgData, ArgSize,
-             [](const tpctypes::FinalizeRequest &FR) -> Error {
-               for (auto &Seg : FR) {
-                 char *Mem = Seg.Addr.toPtr<char *>();
-                 memcpy(Mem, Seg.Content.data(), Seg.Content.size());
-                 memset(Mem + Seg.Content.size(), 0,
-                        Seg.Size - Seg.Content.size());
-                 assert(Seg.Size <= std::numeric_limits<size_t>::max());
-                 if (auto EC = sys::Memory::protectMappedMemory(
-                         {Mem, static_cast<size_t>(Seg.Size)},
-                         tpctypes::fromWireProtectionFlags(Seg.Prot)))
-                   return errorCodeToError(EC);
-                 if (Seg.Prot & tpctypes::WPF_Exec)
-                   sys::Memory::InvalidateInstructionCache(Mem, Seg.Size);
-               }
-               return Error::success();
-             })
-      .release();
-}
-
-static llvm::orc::shared::detail::CWrapperFunctionResult
-deallocateWrapper(const char *ArgData, size_t ArgSize) {
-  return WrapperFunction<SPSOrcTargetProcessDeallocate>::handle(
-             ArgData, ArgSize,
-             [](ExecutorAddress Base, uint64_t Size) -> Error {
-               sys::MemoryBlock MB(Base.toPtr<void *>(), Size);
-               if (auto EC = sys::Memory::releaseMappedMemory(MB))
-                 return errorCodeToError(EC);
-               return Error::success();
-             })
-      .release();
-}
-
-template <typename WriteT, typename SPSWriteT>
-static llvm::orc::shared::detail::CWrapperFunctionResult
-writeUIntsWrapper(const char *ArgData, size_t ArgSize) {
-  return WrapperFunction<void(SPSSequence<SPSWriteT>)>::handle(
-             ArgData, ArgSize,
-             [](std::vector<WriteT> Ws) {
-               for (auto &W : Ws)
-                 *jitTargetAddressToPointer<decltype(W.Value) *>(W.Address) =
-                     W.Value;
-             })
-      .release();
-}
-
-static llvm::orc::shared::detail::CWrapperFunctionResult
-writeBuffersWrapper(const char *ArgData, size_t ArgSize) {
-  return WrapperFunction<void(SPSSequence<SPSMemoryAccessBufferWrite>)>::handle(
-             ArgData, ArgSize,
-             [](std::vector<tpctypes::BufferWrite> Ws) {
-               for (auto &W : Ws)
-                 memcpy(jitTargetAddressToPointer<char *>(W.Address),
-                        W.Buffer.data(), W.Buffer.size());
-             })
-      .release();
-}
-
-static llvm::orc::shared::detail::CWrapperFunctionResult
-runAsMainWrapper(const char *ArgData, size_t ArgSize) {
-  return WrapperFunction<SPSRunAsMainSignature>::handle(
-             ArgData, ArgSize,
-             [](ExecutorAddress MainAddr,
-                std::vector<std::string> Args) -> int64_t {
-               return runAsMain(MainAddr.toPtr<int (*)(int, char *[])>(), Args);
-             })
-      .release();
-}
-
 SimpleRemoteEPCServer::Dispatcher::~Dispatcher() {}
 
 #if LLVM_ENABLE_THREADS
@@ -137,27 +51,7 @@ void SimpleRemoteEPCServer::ThreadDispatcher::shutdown() {
 
 StringMap<ExecutorAddress> SimpleRemoteEPCServer::defaultBootstrapSymbols() {
   StringMap<ExecutorAddress> DBS;
-
-  DBS["__llvm_orc_memory_reserve"] = ExecutorAddress::fromPtr(&reserveWrapper);
-  DBS["__llvm_orc_memory_finalize"] =
-      ExecutorAddress::fromPtr(&finalizeWrapper);
-  DBS["__llvm_orc_memory_deallocate"] =
-      ExecutorAddress::fromPtr(&deallocateWrapper);
-  DBS["__llvm_orc_memory_write_uint8s"] = ExecutorAddress::fromPtr(
-      &writeUIntsWrapper<tpctypes::UInt8Write,
-                         shared::SPSMemoryAccessUInt8Write>);
-  DBS["__llvm_orc_memory_write_uint16s"] = ExecutorAddress::fromPtr(
-      &writeUIntsWrapper<tpctypes::UInt16Write,
-                         shared::SPSMemoryAccessUInt16Write>);
-  DBS["__llvm_orc_memory_write_uint32s"] = ExecutorAddress::fromPtr(
-      &writeUIntsWrapper<tpctypes::UInt32Write,
-                         shared::SPSMemoryAccessUInt32Write>);
-  DBS["__llvm_orc_memory_write_uint64s"] = ExecutorAddress::fromPtr(
-      &writeUIntsWrapper<tpctypes::UInt64Write,
-                         shared::SPSMemoryAccessUInt64Write>);
-  DBS["__llvm_orc_memory_write_buffers"] =
-      ExecutorAddress::fromPtr(&writeBuffersWrapper);
-  DBS["__llvm_orc_run_as_main"] = ExecutorAddress::fromPtr(&runAsMainWrapper);
+  rt_bootstrap::addTo(DBS);
   DBS["__llvm_orc_load_dylib"] = ExecutorAddress::fromPtr(&loadDylibWrapper);
   DBS["__llvm_orc_lookup_symbols"] =
       ExecutorAddress::fromPtr(&lookupSymbolsWrapper);

diff  --git a/llvm/unittests/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManagerTest.cpp b/llvm/unittests/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManagerTest.cpp
index 186aa75b4966c..9a0481c2b10e9 100644
--- a/llvm/unittests/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManagerTest.cpp
+++ b/llvm/unittests/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManagerTest.cpp
@@ -9,6 +9,7 @@
 #include "OrcTestCommon.h"
 
 #include "llvm/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.h"
+#include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
 #include "llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h"
 #include "llvm/Support/Memory.h"
 #include "llvm/Testing/Support/Error.h"
@@ -23,7 +24,7 @@ namespace {
 
 llvm::orc::shared::detail::CWrapperFunctionResult
 testReserve(const char *ArgData, size_t ArgSize) {
-  return WrapperFunction<SPSOrcTargetProcessAllocate>::handle(
+  return WrapperFunction<rt::SPSMemoryReserveSignature>::handle(
              ArgData, ArgSize,
              [](uint64_t Size) -> Expected<ExecutorAddress> {
                std::error_code EC;
@@ -38,7 +39,7 @@ testReserve(const char *ArgData, size_t ArgSize) {
 
 llvm::orc::shared::detail::CWrapperFunctionResult
 testFinalize(const char *ArgData, size_t ArgSize) {
-  return WrapperFunction<SPSOrcTargetProcessFinalize>::handle(
+  return WrapperFunction<rt::SPSMemoryFinalizeSignature>::handle(
              ArgData, ArgSize,
              [](const tpctypes::FinalizeRequest &FR) -> Error {
                for (auto &Seg : FR) {
@@ -61,7 +62,7 @@ testFinalize(const char *ArgData, size_t ArgSize) {
 
 llvm::orc::shared::detail::CWrapperFunctionResult
 testDeallocate(const char *ArgData, size_t ArgSize) {
-  return WrapperFunction<SPSOrcTargetProcessDeallocate>::handle(
+  return WrapperFunction<rt::SPSMemoryDeallocateSignature>::handle(
              ArgData, ArgSize,
              [](ExecutorAddress Base, uint64_t Size) -> Error {
                sys::MemoryBlock MB(Base.toPtr<void *>(), Size);


        


More information about the llvm-commits mailing list