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,
60                    unique_function<void(Error)> ErrorReporter);
61 
62   // Implement ObjectLinkingLayerCreator
63   Expected<std::unique_ptr<ObjectLayer>> operator()(ExecutionSession &,
64                                                     const Triple &);
65 
66   std::unique_ptr<ExecutionSession> startSession();
67   Error disconnect();
68 
69   Error addDebugSupport(ObjectLayer &ObjLayer);
70 
71   Expected<std::unique_ptr<DefinitionGenerator>>
72   loadDylib(StringRef RemotePath);
73 
74   Expected<int> runAsMain(JITEvaluatedSymbol MainSym,
75                           ArrayRef<std::string> Args);
76 
77   virtual ~JITLinkExecutor();
78 
79 protected:
80   std::unique_ptr<RemoteExecutorProcessControl> OwnedEPC;
81   RemoteExecutorProcessControl *EPC{nullptr};
82 
83   JITLinkExecutor();
84 };
85 
86 /// JITLinkExecutor that runs in a child process on the local machine.
87 class ChildProcessJITLinkExecutor : public JITLinkExecutor {
88 public:
89   Error launch(unique_function<void(Error)> ErrorReporter);
90 
91   pid_t getPID() const { return ProcessID; }
92   StringRef getPath() const { return ExecutablePath; }
93 
94 private:
95   std::string ExecutablePath;
96   pid_t ProcessID;
97 
98   ChildProcessJITLinkExecutor(std::string ExecutablePath)
99       : ExecutablePath(std::move(ExecutablePath)) {}
100 
101   static std::string defaultPath(const char *HostArgv0, StringRef ExecutorName);
102   friend class JITLinkExecutor;
103 };
104 
105 /// JITLinkExecutor connected through a TCP socket.
106 class TCPSocketJITLinkExecutor : public JITLinkExecutor {
107 private:
108   TCPSocketJITLinkExecutor(std::unique_ptr<RemoteExecutorProcessControl> EPC);
109 
110   friend class JITLinkExecutor;
111 };
112 
113 } // namespace orc
114 } // namespace llvm
115 
116 #endif
117