1 //===-- RemoteJITUtils.h - Utilities for remote-JITing ----------*- C++ -*-===//
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 //
9 // Utilities for ExecutorProcessControl-based remote JITing with Orc and
10 // JITLink.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXAMPLES_ORCV2EXAMPLES_LLJITWITHREMOTEDEBUGGING_REMOTEJITUTILS_H
15 #define LLVM_EXAMPLES_ORCV2EXAMPLES_LLJITWITHREMOTEDEBUGGING_REMOTEJITUTILS_H
16 
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/ExecutionEngine/JITSymbol.h"
21 #include "llvm/ExecutionEngine/Orc/Core.h"
22 #include "llvm/ExecutionEngine/Orc/Layer.h"
23 #include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
24 #include "llvm/ExecutionEngine/Orc/Shared/FDRawByteChannel.h"
25 #include "llvm/Support/Error.h"
26 
27 #include <memory>
28 #include <string>
29 
30 #if !defined(_MSC_VER) && !defined(__MINGW32__)
31 #include <unistd.h>
32 #else
33 #include <io.h>
34 #endif
35 
36 namespace llvm {
37 namespace orc {
38 
39 class ChildProcessJITLinkExecutor;
40 class RemoteExecutorProcessControl;
41 class TCPSocketJITLinkExecutor;
42 
43 class JITLinkExecutor {
44 public:
45   using RPCChannel = shared::FDRawByteChannel;
46 
47   /// Create a JITLinkExecutor for the given exectuable on disk.
48   static Expected<std::unique_ptr<ChildProcessJITLinkExecutor>>
49   CreateLocal(std::string ExecutablePath);
50 
51   /// Find the default exectuable on disk and create a JITLinkExecutor for it.
52   static Expected<std::unique_ptr<ChildProcessJITLinkExecutor>>
53   FindLocal(const char *JITArgv0);
54 
55   /// Create a JITLinkExecutor that connects to the given network address
56   /// through a TCP socket. A valid NetworkAddress provides hostname and port,
57   /// e.g. localhost:20000.
58   static Expected<std::unique_ptr<TCPSocketJITLinkExecutor>>
59   ConnectTCPSocket(StringRef NetworkAddress, ExecutionSession &ES);
60 
61   // Implement ObjectLinkingLayerCreator
62   Expected<std::unique_ptr<ObjectLayer>> operator()(ExecutionSession &,
63                                                     const Triple &);
64 
65   Error addDebugSupport(ObjectLayer &ObjLayer);
66 
67   Expected<std::unique_ptr<DefinitionGenerator>>
68   loadDylib(StringRef RemotePath);
69 
70   Expected<int> runAsMain(JITEvaluatedSymbol MainSym,
71                           ArrayRef<std::string> Args);
72   Error disconnect();
73 
74   virtual ~JITLinkExecutor();
75 
76 protected:
77   std::unique_ptr<RemoteExecutorProcessControl> EPC;
78 
79   JITLinkExecutor();
80 };
81 
82 /// JITLinkExecutor that runs in a child process on the local machine.
83 class ChildProcessJITLinkExecutor : public JITLinkExecutor {
84 public:
85   Error launch(ExecutionSession &ES);
86 
getPID()87   pid_t getPID() const { return ProcessID; }
getPath()88   StringRef getPath() const { return ExecutablePath; }
89 
90 private:
91   std::string ExecutablePath;
92   pid_t ProcessID;
93 
ChildProcessJITLinkExecutor(std::string ExecutablePath)94   ChildProcessJITLinkExecutor(std::string ExecutablePath)
95       : ExecutablePath(std::move(ExecutablePath)) {}
96 
97   static std::string defaultPath(const char *HostArgv0, StringRef ExecutorName);
98   friend class JITLinkExecutor;
99 };
100 
101 /// JITLinkExecutor connected through a TCP socket.
102 class TCPSocketJITLinkExecutor : public JITLinkExecutor {
103 private:
104   TCPSocketJITLinkExecutor(std::unique_ptr<RemoteExecutorProcessControl> EPC);
105 
106   friend class JITLinkExecutor;
107 };
108 
109 } // namespace orc
110 } // namespace llvm
111 
112 #endif
113