blob: 981e0812d45ee9405d3f1a6dcc08e4ee93f30075 [file] [log] [blame]
Chris Lattnera0d7b082002-12-23 23:59:41 +00001//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Misha Brukman650ba8e2005-04-22 00:00:37 +00006//
John Criswell09344dc2003-10-20 17:47:21 +00007//===----------------------------------------------------------------------===//
Chris Lattnerd7ff5782001-08-23 17:05:04 +00008//
Chris Lattner141ea3a2003-12-26 05:07:35 +00009// This utility provides a simple wrapper around the LLVM Execution Engines,
10// which allow the direct execution of LLVM programs through a Just-In-Time
Torok Edwinf011f282009-07-03 12:11:32 +000011// compiler, or through an interpreter if no JIT is available for this platform.
Chris Lattnerd7ff5782001-08-23 17:05:04 +000012//
13//===----------------------------------------------------------------------===//
14
Lang Hames9d7a2692016-01-11 16:35:55 +000015#include "RemoteJITUtils.h"
NAKAMURA Takumicef0a8212016-01-15 02:14:46 +000016#include "llvm/ADT/StringExtras.h"
Duncan Sands3bd97fe2010-08-28 01:30:02 +000017#include "llvm/ADT/Triple.h"
Teresa Johnsonad176792016-11-11 05:34:58 +000018#include "llvm/Bitcode/BitcodeReader.h"
serge-sans-pailleac1d23e2020-03-04 00:47:43 +010019#include "llvm/CodeGen/CommandFlags.h"
Chris Lattner5af6a3f2006-08-01 22:34:35 +000020#include "llvm/CodeGen/LinkAllCodegenComponents.h"
Nico Weber432a3882018-04-30 14:59:11 +000021#include "llvm/Config/llvm-config.h"
Brian Gaekee99ca442003-09-05 19:42:34 +000022#include "llvm/ExecutionEngine/GenericValue.h"
Jeffrey Yasskin0b08f3d2009-06-25 02:04:04 +000023#include "llvm/ExecutionEngine/Interpreter.h"
Jeffrey Yasskin0b08f3d2009-06-25 02:04:04 +000024#include "llvm/ExecutionEngine/JITEventListener.h"
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000025#include "llvm/ExecutionEngine/MCJIT.h"
Lang Hames173c69f2014-01-08 04:09:09 +000026#include "llvm/ExecutionEngine/ObjectCache.h"
Lang Hames85fb9972019-12-16 02:50:40 -080027#include "llvm/ExecutionEngine/Orc/DebugUtils.h"
Lang Hames6a941342018-06-26 21:35:48 +000028#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
Lang Hamesd435ce42018-09-30 19:12:23 +000029#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
Lang Hames6a941342018-06-26 21:35:48 +000030#include "llvm/ExecutionEngine/Orc/LLJIT.h"
Lang Hames85fb9972019-12-16 02:50:40 -080031#include "llvm/ExecutionEngine/Orc/MachOPlatform.h"
Rafael Espindola79e238a2017-08-03 02:16:21 +000032#include "llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h"
Lang Hames39253a52020-03-19 16:14:18 -070033#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
Lang Hames93de2a12015-01-23 21:25:00 +000034#include "llvm/ExecutionEngine/OrcMCJITReplacement.h"
Andrew Kaylor58365b92012-11-27 19:49:00 +000035#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Andrew Kaylor1ca510e2013-10-29 01:29:56 +000036#include "llvm/IR/IRBuilder.h"
Rafael Espindola79e238a2017-08-03 02:16:21 +000037#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000038#include "llvm/IR/Module.h"
39#include "llvm/IR/Type.h"
Lang Hamesadae9bf2018-07-02 22:30:18 +000040#include "llvm/IR/Verifier.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000041#include "llvm/IRReader/IRReader.h"
Lang Hames173c69f2014-01-08 04:09:09 +000042#include "llvm/Object/Archive.h"
43#include "llvm/Object/ObjectFile.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000044#include "llvm/Support/CommandLine.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000045#include "llvm/Support/Debug.h"
46#include "llvm/Support/DynamicLibrary.h"
47#include "llvm/Support/Format.h"
Rui Ueyama197194b2018-04-13 18:26:06 +000048#include "llvm/Support/InitLLVM.h"
Chris Lattner76d46322006-12-06 01:18:01 +000049#include "llvm/Support/ManagedStatic.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000050#include "llvm/Support/MathExtras.h"
51#include "llvm/Support/Memory.h"
Chris Lattner2785bdb2007-05-06 04:58:26 +000052#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000053#include "llvm/Support/Path.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000054#include "llvm/Support/PluginLoader.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000055#include "llvm/Support/Process.h"
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000056#include "llvm/Support/Program.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000057#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000058#include "llvm/Support/TargetSelect.h"
Jonas Devlieghere4a2863c2018-04-22 08:02:11 +000059#include "llvm/Support/WithColor.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000060#include "llvm/Support/raw_ostream.h"
Daniel Maleaa960c542013-06-28 19:11:40 +000061#include "llvm/Transforms/Instrumentation.h"
Chris Lattner4c522b92007-04-27 17:02:33 +000062#include <cerrno>
NAKAMURA Takumi15304872010-10-22 14:53:59 +000063
64#ifdef __CYGWIN__
65#include <cygwin/version.h>
66#if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
67#define DO_NOTHING_ATEXIT 1
68#endif
69#endif
70
Brian Gaeke960707c2003-11-11 22:41:34 +000071using namespace llvm;
72
serge-sans-pailleac1d23e2020-03-04 00:47:43 +010073static codegen::RegisterCodeGenFlags CGF;
74
Chandler Carruthf98597a2014-04-22 03:10:36 +000075#define DEBUG_TYPE "lli"
76
Chris Lattnera0d7b082002-12-23 23:59:41 +000077namespace {
Lang Hames9528bba2015-03-25 12:11:48 +000078
79 enum class JITKind { MCJIT, OrcMCJITReplacement, OrcLazy };
80
Chris Lattnera0d7b082002-12-23 23:59:41 +000081 cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000082 InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000083
Chris Lattnera0d7b082002-12-23 23:59:41 +000084 cl::list<std::string>
85 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
Chris Lattnerf5cad152002-07-22 02:10:13 +000086
Chris Lattnera0d7b082002-12-23 23:59:41 +000087 cl::opt<bool> ForceInterpreter("force-interpreter",
Misha Brukmanc2186e32003-09-25 18:10:34 +000088 cl::desc("Force interpretation: disable JIT"),
89 cl::init(false));
Evan Cheng06d988e2008-08-08 08:12:06 +000090
Lang Hames17164542019-07-17 16:40:52 +000091 cl::opt<JITKind> UseJITKind(
92 "jit-kind", cl::desc("Choose underlying JIT kind."),
93 cl::init(JITKind::MCJIT),
94 cl::values(clEnumValN(JITKind::MCJIT, "mcjit", "MCJIT"),
95 clEnumValN(JITKind::OrcMCJITReplacement, "orc-mcjit",
96 "Orc-based MCJIT replacement "
97 "(deprecated)"),
98 clEnumValN(JITKind::OrcLazy, "orc-lazy",
99 "Orc-based lazy JIT.")));
Lang Hames93de2a12015-01-23 21:25:00 +0000100
Lang Hamesf0a3fd882018-09-26 16:26:59 +0000101 cl::opt<unsigned>
102 LazyJITCompileThreads("compile-threads",
103 cl::desc("Choose the number of compile threads "
104 "(jit-kind=orc-lazy only)"),
105 cl::init(0));
106
107 cl::list<std::string>
108 ThreadEntryPoints("thread-entry",
109 cl::desc("calls the given entry-point on a new thread "
110 "(jit-kind=orc-lazy only)"));
111
Lang Hames98440292018-09-29 23:49:57 +0000112 cl::opt<bool> PerModuleLazy(
113 "per-module-lazy",
114 cl::desc("Performs lazy compilation on whole module boundaries "
115 "rather than individual functions"),
116 cl::init(false));
117
Lang Hames23cb2e72018-10-23 23:01:39 +0000118 cl::list<std::string>
119 JITDylibs("jd",
120 cl::desc("Specifies the JITDylib to be used for any subsequent "
121 "-extra-module arguments."));
122
Lang Hamesb7be6b42020-02-14 14:22:59 -0800123 cl::list<std::string>
124 Dylibs("dlopen", cl::desc("Dynamic libraries to load before linking"),
125 cl::ZeroOrMore);
126
Jim Grosbach0f435d02012-09-05 16:50:34 +0000127 // The MCJIT supports building for a target address space separate from
128 // the JIT compilation process. Use a forked process and a copying
129 // memory manager with IPC to execute using this functionality.
130 cl::opt<bool> RemoteMCJIT("remote-mcjit",
131 cl::desc("Execute MCJIT'ed code in a separate process."),
132 cl::init(false));
133
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000134 // Manually specify the child process for remote execution. This overrides
135 // the simulated remote execution that allocates address space for child
Andrew Kaylor89352582013-10-29 01:33:14 +0000136 // execution. The child process will be executed and will communicate with
137 // lli via stdin/stdout pipes.
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000138 cl::opt<std::string>
Alp Tokera1186382014-01-22 21:52:35 +0000139 ChildExecPath("mcjit-remote-process",
140 cl::desc("Specify the filename of the process to launch "
141 "for remote MCJIT execution. If none is specified,"
142 "\n\tremote execution will be simulated in-process."),
143 cl::value_desc("filename"), cl::init(""));
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000144
Evan Cheng09bd0b12009-05-04 23:05:19 +0000145 // Determine optimization level.
146 cl::opt<char>
147 OptLevel("O",
148 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
149 "(default = '-O2')"),
150 cl::Prefix,
151 cl::ZeroOrMore,
152 cl::init(' '));
Evan Cheng06d988e2008-08-08 08:12:06 +0000153
Chris Lattner76766cb2005-12-16 05:00:21 +0000154 cl::opt<std::string>
Chris Lattner78e9e102005-12-16 05:19:18 +0000155 TargetTriple("mtriple", cl::desc("Override target triple for module"));
Evan Cheng61582542008-11-05 23:21:52 +0000156
157 cl::opt<std::string>
158 EntryFunc("entry-function",
159 cl::desc("Specify the entry function (default = 'main') "
160 "of the executable"),
161 cl::value_desc("function"),
162 cl::init("main"));
Eli Bendersky4c647582012-01-16 08:56:09 +0000163
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +0000164 cl::list<std::string>
Andrew Kaylor4404eb42013-10-28 21:58:15 +0000165 ExtraModules("extra-module",
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +0000166 cl::desc("Extra modules to be loaded"),
Alp Toker0a09ebf2013-10-28 22:51:25 +0000167 cl::value_desc("input bitcode"));
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +0000168
Lang Hames173c69f2014-01-08 04:09:09 +0000169 cl::list<std::string>
170 ExtraObjects("extra-object",
171 cl::desc("Extra object files to be loaded"),
172 cl::value_desc("input object"));
173
174 cl::list<std::string>
175 ExtraArchives("extra-archive",
176 cl::desc("Extra archive files to be loaded"),
177 cl::value_desc("input archive"));
178
179 cl::opt<bool>
180 EnableCacheManager("enable-cache-manager",
Lang Hames15c072a2019-04-27 16:13:53 +0000181 cl::desc("Use cache manager to save/load modules"),
Lang Hames173c69f2014-01-08 04:09:09 +0000182 cl::init(false));
183
Chris Lattner55644062003-10-28 22:51:44 +0000184 cl::opt<std::string>
Lang Hames1ddecc02014-01-09 05:24:05 +0000185 ObjectCacheDir("object-cache-dir",
186 cl::desc("Directory to store cached object files "
187 "(must be user writable)"),
188 cl::init(""));
189
190 cl::opt<std::string>
Chris Lattner55644062003-10-28 22:51:44 +0000191 FakeArgv0("fake-argv0",
192 cl::desc("Override the 'argv[0]' value passed into the executing"
193 " program"), cl::value_desc("executable"));
Eli Bendersky4c647582012-01-16 08:56:09 +0000194
Chris Lattner3f0ffd32006-09-14 06:17:09 +0000195 cl::opt<bool>
196 DisableCoreFiles("disable-core-files", cl::Hidden,
197 cl::desc("Disable emission of core files if possible"));
Evan Cheng0422bef2008-04-22 06:51:41 +0000198
199 cl::opt<bool>
Evan Cheng16f036c2008-05-21 18:20:21 +0000200 NoLazyCompilation("disable-lazy-compilation",
Evan Cheng0422bef2008-04-22 06:51:41 +0000201 cl::desc("Disable JIT lazy compilation"),
202 cl::init(false));
Evan Cheng2129f592011-07-19 06:37:02 +0000203
Nick Lewyckyb0e97892012-04-18 08:34:12 +0000204 cl::opt<bool>
Tim Northoverd4a2f5b2012-10-12 09:55:13 +0000205 GenerateSoftFloatCalls("soft-float",
206 cl::desc("Generate software floating point library calls"),
207 cl::init(false));
208
Lang Hamesce2207a2020-01-21 16:28:30 -0800209 cl::opt<bool> NoProcessSymbols(
210 "no-process-syms",
211 cl::desc("Do not resolve lli process symbols in JIT'd code"),
212 cl::init(false));
213
Lang Hames85fb9972019-12-16 02:50:40 -0800214 enum class LLJITPlatform { DetectHost, GenericIR, MachO };
215
216 cl::opt<LLJITPlatform>
217 Platform("lljit-platform", cl::desc("Platform to use with LLJIT"),
218 cl::init(LLJITPlatform::DetectHost),
219 cl::values(clEnumValN(LLJITPlatform::DetectHost, "DetectHost",
220 "Select based on JIT target triple"),
221 clEnumValN(LLJITPlatform::GenericIR, "GenericIR",
222 "Use LLJITGenericIRPlatform"),
223 clEnumValN(LLJITPlatform::MachO, "MachO",
224 "Use LLJITMachOPlatform")),
225 cl::Hidden);
226
Lang Hames6a941342018-06-26 21:35:48 +0000227 enum class DumpKind {
228 NoDump,
229 DumpFuncsToStdOut,
230 DumpModsToStdOut,
231 DumpModsToDisk
232 };
233
234 cl::opt<DumpKind> OrcDumpKind(
235 "orc-lazy-debug", cl::desc("Debug dumping for the orc-lazy JIT."),
236 cl::init(DumpKind::NoDump),
237 cl::values(clEnumValN(DumpKind::NoDump, "no-dump",
238 "Don't dump anything."),
239 clEnumValN(DumpKind::DumpFuncsToStdOut, "funcs-to-stdout",
240 "Dump function names to stdout."),
241 clEnumValN(DumpKind::DumpModsToStdOut, "mods-to-stdout",
242 "Dump modules to stdout."),
243 clEnumValN(DumpKind::DumpModsToDisk, "mods-to-disk",
244 "Dump modules to the current "
245 "working directory. (WARNING: "
246 "will overwrite existing files).")),
247 cl::Hidden);
248
Lang Hamesef5a0ee2016-04-25 19:56:45 +0000249 ExitOnError ExitOnErr;
Chris Lattnera0d7b082002-12-23 23:59:41 +0000250}
Chris Lattner009f8102001-10-27 08:43:52 +0000251
Lang Hames173c69f2014-01-08 04:09:09 +0000252//===----------------------------------------------------------------------===//
253// Object cache
254//
Lang Hames1ddecc02014-01-09 05:24:05 +0000255// This object cache implementation writes cached objects to disk to the
256// directory specified by CacheDir, using a filename provided in the module
257// descriptor. The cache tries to load a saved object using that path if the
258// file exists. CacheDir defaults to "", in which case objects are cached
NAKAMURA Takumif462f9c2014-01-10 10:38:28 +0000259// alongside their originating bitcodes.
Lang Hames173c69f2014-01-08 04:09:09 +0000260//
261class LLIObjectCache : public ObjectCache {
262public:
Lang Hames1ddecc02014-01-09 05:24:05 +0000263 LLIObjectCache(const std::string& CacheDir) : CacheDir(CacheDir) {
264 // Add trailing '/' to cache dir if necessary.
Lang Hamesf9dd8fd2014-01-09 05:29:59 +0000265 if (!this->CacheDir.empty() &&
266 this->CacheDir[this->CacheDir.size() - 1] != '/')
NAKAMURA Takumif462f9c2014-01-10 10:38:28 +0000267 this->CacheDir += '/';
Lang Hames1ddecc02014-01-09 05:24:05 +0000268 }
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000269 ~LLIObjectCache() override {}
Lang Hames173c69f2014-01-08 04:09:09 +0000270
Rafael Espindola48af1c22014-08-19 18:44:46 +0000271 void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override {
Benjamin Kramer46e38f32016-06-08 10:01:20 +0000272 const std::string &ModuleID = M->getModuleIdentifier();
Lang Hames173c69f2014-01-08 04:09:09 +0000273 std::string CacheName;
274 if (!getCacheFilename(ModuleID, CacheName))
275 return;
NAKAMURA Takumi390e0602014-01-10 10:38:34 +0000276 if (!CacheDir.empty()) { // Create user-defined cache dir.
Rafael Espindolaf662e002015-07-15 21:24:07 +0000277 SmallString<128> dir(sys::path::parent_path(CacheName));
NAKAMURA Takumi390e0602014-01-10 10:38:34 +0000278 sys::fs::create_directories(Twine(dir));
279 }
Lang Hames1df947a2020-02-22 09:49:55 -0800280
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000281 std::error_code EC;
Fangrui Songd9b948b2019-08-05 05:43:48 +0000282 raw_fd_ostream outfile(CacheName, EC, sys::fs::OF_None);
Rafael Espindola48af1c22014-08-19 18:44:46 +0000283 outfile.write(Obj.getBufferStart(), Obj.getBufferSize());
Lang Hames173c69f2014-01-08 04:09:09 +0000284 outfile.close();
285 }
286
Rafael Espindola5f2bb7d2014-08-13 18:49:01 +0000287 std::unique_ptr<MemoryBuffer> getObject(const Module* M) override {
Benjamin Kramer46e38f32016-06-08 10:01:20 +0000288 const std::string &ModuleID = M->getModuleIdentifier();
Lang Hames173c69f2014-01-08 04:09:09 +0000289 std::string CacheName;
290 if (!getCacheFilename(ModuleID, CacheName))
Craig Toppere6cb63e2014-04-25 04:24:47 +0000291 return nullptr;
Lang Hames173c69f2014-01-08 04:09:09 +0000292 // Load the object from the cache filename
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000293 ErrorOr<std::unique_ptr<MemoryBuffer>> IRObjectBuffer =
Malcolm Parsons06ac79c2016-11-02 16:43:50 +0000294 MemoryBuffer::getFile(CacheName, -1, false);
Lang Hames173c69f2014-01-08 04:09:09 +0000295 // If the file isn't there, that's OK.
296 if (!IRObjectBuffer)
Craig Toppere6cb63e2014-04-25 04:24:47 +0000297 return nullptr;
Lang Hames173c69f2014-01-08 04:09:09 +0000298 // MCJIT will want to write into this buffer, and we don't want that
299 // because the file has probably just been mmapped. Instead we make
300 // a copy. The filed-based buffer will be released when it goes
301 // out of scope.
Rafael Espindola3560ff22014-08-27 20:03:13 +0000302 return MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer());
Lang Hames173c69f2014-01-08 04:09:09 +0000303 }
304
305private:
Lang Hames1ddecc02014-01-09 05:24:05 +0000306 std::string CacheDir;
307
Lang Hames173c69f2014-01-08 04:09:09 +0000308 bool getCacheFilename(const std::string &ModID, std::string &CacheName) {
309 std::string Prefix("file:");
310 size_t PrefixLength = Prefix.length();
311 if (ModID.substr(0, PrefixLength) != Prefix)
312 return false;
Lang Hames1df947a2020-02-22 09:49:55 -0800313
314 std::string CacheSubdir = ModID.substr(PrefixLength);
NAKAMURA Takumid7fd6d92014-01-10 10:38:40 +0000315#if defined(_WIN32)
Lang Hames1df947a2020-02-22 09:49:55 -0800316 // Transform "X:\foo" => "/X\foo" for convenience.
317 if (isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') {
318 CacheSubdir[1] = CacheSubdir[0];
319 CacheSubdir[0] = '/';
320 }
NAKAMURA Takumid7fd6d92014-01-10 10:38:40 +0000321#endif
Lang Hames1df947a2020-02-22 09:49:55 -0800322
NAKAMURA Takumid7fd6d92014-01-10 10:38:40 +0000323 CacheName = CacheDir + CacheSubdir;
Lang Hames173c69f2014-01-08 04:09:09 +0000324 size_t pos = CacheName.rfind('.');
325 CacheName.replace(pos, CacheName.length() - pos, ".o");
326 return true;
327 }
328};
329
Andrew Kaylor1ca510e2013-10-29 01:29:56 +0000330// On Mingw and Cygwin, an external symbol named '__main' is called from the
Simon Pilgrimdae11f72016-11-20 13:31:13 +0000331// generated 'main' function to allow static initialization. To avoid linking
Andrew Kaylor1ca510e2013-10-29 01:29:56 +0000332// problems with remote targets (because lli's remote target support does not
333// currently handle external linking) we add a secondary module which defines
334// an empty '__main' function.
Mehdi Aminia7de8202016-04-18 18:52:39 +0000335static void addCygMingExtraModule(ExecutionEngine &EE, LLVMContext &Context,
Andrew Kaylor1ca510e2013-10-29 01:29:56 +0000336 StringRef TargetTripleStr) {
337 IRBuilder<> Builder(Context);
338 Triple TargetTriple(TargetTripleStr);
339
340 // Create a new module.
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000341 std::unique_ptr<Module> M = std::make_unique<Module>("CygMingHelper", Context);
Andrew Kaylor1ca510e2013-10-29 01:29:56 +0000342 M->setTargetTriple(TargetTripleStr);
343
344 // Create an empty function named "__main".
James Y Knightc0044112019-01-13 16:09:28 +0000345 Type *ReturnTy;
346 if (TargetTriple.isArch64Bit())
347 ReturnTy = Type::getInt64Ty(Context);
348 else
349 ReturnTy = Type::getInt32Ty(Context);
350 Function *Result =
351 Function::Create(FunctionType::get(ReturnTy, {}, false),
352 GlobalValue::ExternalLinkage, "__main", M.get());
353
Andrew Kaylor1ca510e2013-10-29 01:29:56 +0000354 BasicBlock *BB = BasicBlock::Create(Context, "__main", Result);
355 Builder.SetInsertPoint(BB);
James Y Knightc0044112019-01-13 16:09:28 +0000356 Value *ReturnVal = ConstantInt::get(ReturnTy, 0);
Andrew Kaylor1ca510e2013-10-29 01:29:56 +0000357 Builder.CreateRet(ReturnVal);
358
359 // Add this new module to the ExecutionEngine.
Mehdi Aminia7de8202016-04-18 18:52:39 +0000360 EE.addModule(std::move(M));
Andrew Kaylor1ca510e2013-10-29 01:29:56 +0000361}
362
Lang Hames1899d0c2015-06-09 02:43:27 +0000363CodeGenOpt::Level getOptLevel() {
364 switch (OptLevel) {
365 default:
Jonas Devlieghere4a2863c2018-04-22 08:02:11 +0000366 WithColor::error(errs(), "lli") << "invalid optimization level.\n";
Lang Hames1899d0c2015-06-09 02:43:27 +0000367 exit(1);
368 case '0': return CodeGenOpt::None;
369 case '1': return CodeGenOpt::Less;
370 case ' ':
371 case '2': return CodeGenOpt::Default;
372 case '3': return CodeGenOpt::Aggressive;
373 }
374 llvm_unreachable("Unrecognized opt level.");
375}
Andrew Kaylor1ca510e2013-10-29 01:29:56 +0000376
Davide Italianoda8e6b22016-11-17 22:58:13 +0000377LLVM_ATTRIBUTE_NORETURN
378static void reportError(SMDiagnostic Err, const char *ProgName) {
379 Err.print(ProgName, errs());
380 exit(1);
381}
382
Lang Hamesb7be6b42020-02-14 14:22:59 -0800383Error loadDylibs();
Lang Hames37a66412018-08-28 20:20:31 +0000384int runOrcLazyJIT(const char *ProgName);
Lang Hames98440292018-09-29 23:49:57 +0000385void disallowOrcOptions();
Lang Hames6a941342018-06-26 21:35:48 +0000386
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000387//===----------------------------------------------------------------------===//
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000388// main Driver function
389//
Misha Brukman5b255e52003-10-14 21:39:53 +0000390int main(int argc, char **argv, char * const *envp) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000391 InitLLVM X(argc, argv);
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000392
Lang Hamesef5a0ee2016-04-25 19:56:45 +0000393 if (argc > 1)
394 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
395
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000396 // If we have a native target, initialize it to ensure it is linked in and
397 // usable by the JIT.
398 InitializeNativeTarget();
Jim Grosbach7b162492011-03-18 22:48:41 +0000399 InitializeNativeTargetAsmPrinter();
Jim Grosbach2cce3f92012-11-05 19:06:05 +0000400 InitializeNativeTargetAsmParser();
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000401
Chris Lattner2785bdb2007-05-06 04:58:26 +0000402 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman2c6a8212007-10-08 15:45:12 +0000403 "llvm interpreter & dynamic compiler\n");
Reid Spencer996ec722004-12-30 05:36:08 +0000404
Chris Lattner2785bdb2007-05-06 04:58:26 +0000405 // If the user doesn't want core files, disable them.
406 if (DisableCoreFiles)
407 sys::Process::PreventCoreFiles();
Eli Bendersky4c647582012-01-16 08:56:09 +0000408
Lang Hamesb7be6b42020-02-14 14:22:59 -0800409 ExitOnErr(loadDylibs());
410
Lang Hames37a66412018-08-28 20:20:31 +0000411 if (UseJITKind == JITKind::OrcLazy)
412 return runOrcLazyJIT(argv[0]);
Lang Hames98440292018-09-29 23:49:57 +0000413 else
414 disallowOrcOptions();
Lang Hames37a66412018-08-28 20:20:31 +0000415
Mehdi Aminia7de8202016-04-18 18:52:39 +0000416 LLVMContext Context;
417
Gabor Greife16561c2007-07-05 17:07:56 +0000418 // Load the bitcode...
Daniel Dunbar9589bf82010-11-13 00:28:01 +0000419 SMDiagnostic Err;
Rafael Espindolad233b062014-08-26 17:29:46 +0000420 std::unique_ptr<Module> Owner = parseIRFile(InputFile, Err, Context);
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000421 Module *Mod = Owner.get();
Davide Italianoda8e6b22016-11-17 22:58:13 +0000422 if (!Mod)
423 reportError(Err, argv[0]);
Chris Lattner2785bdb2007-05-06 04:58:26 +0000424
Lang Hames173c69f2014-01-08 04:09:09 +0000425 if (EnableCacheManager) {
Eric Christopher79cc1e32014-09-02 22:28:02 +0000426 std::string CacheName("file:");
427 CacheName.append(InputFile);
428 Mod->setModuleIdentifier(CacheName);
Lang Hames173c69f2014-01-08 04:09:09 +0000429 }
430
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000431 // If not jitting lazily, load the whole bitcode file eagerly too.
432 if (NoLazyCompilation) {
Davide Italianob0e067b2016-11-09 21:30:33 +0000433 // Use *argv instead of argv[0] to work around a wrong GCC warning.
434 ExitOnError ExitOnErr(std::string(*argv) +
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000435 ": bitcode didn't read correctly: ");
436 ExitOnErr(Mod->materializeAll());
Evan Cheng0422bef2008-04-22 06:51:41 +0000437 }
Chris Lattner2785bdb2007-05-06 04:58:26 +0000438
Rafael Espindolae9fab9b2014-01-14 23:51:27 +0000439 std::string ErrorMsg;
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000440 EngineBuilder builder(std::move(Owner));
serge-sans-pailleac1d23e2020-03-04 00:47:43 +0100441 builder.setMArch(codegen::getMArch());
442 builder.setMCPU(codegen::getCPUStr());
443 builder.setMAttrs(codegen::getFeatureList());
444 if (auto RM = codegen::getExplicitRelocModel())
445 builder.setRelocationModel(RM.getValue());
446 if (auto CM = codegen::getExplicitCodeModel())
447 builder.setCodeModel(CM.getValue());
Daniel Dunbara78d8092009-07-18 08:07:13 +0000448 builder.setErrorStr(&ErrorMsg);
449 builder.setEngineKind(ForceInterpreter
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000450 ? EngineKind::Interpreter
451 : EngineKind::JIT);
Lang Hames17164542019-07-17 16:40:52 +0000452 builder.setUseOrcMCJITReplacement(AcknowledgeORCv1Deprecation,
453 UseJITKind == JITKind::OrcMCJITReplacement);
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000454
Chris Lattner2785bdb2007-05-06 04:58:26 +0000455 // If we are supposed to override the target triple, do so now.
456 if (!TargetTriple.empty())
Duncan Sands3bd97fe2010-08-28 01:30:02 +0000457 Mod->setTargetTriple(Triple::normalize(TargetTriple));
Evan Cheng0422bef2008-04-22 06:51:41 +0000458
Eli Bendersky4c647582012-01-16 08:56:09 +0000459 // Enable MCJIT if desired.
Craig Toppere6cb63e2014-04-25 04:24:47 +0000460 RTDyldMemoryManager *RTDyldMM = nullptr;
Eric Christopher79cc1e32014-09-02 22:28:02 +0000461 if (!ForceInterpreter) {
Jim Grosbach0f435d02012-09-05 16:50:34 +0000462 if (RemoteMCJIT)
Lang Hames9d7a2692016-01-11 16:35:55 +0000463 RTDyldMM = new ForwardingMemoryManager();
Jim Grosbach0f435d02012-09-05 16:50:34 +0000464 else
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000465 RTDyldMM = new SectionMemoryManager();
Lang Hames4a5697e2014-12-03 00:51:19 +0000466
467 // Deliberately construct a temp std::unique_ptr to pass in. Do not null out
468 // RTDyldMM: We still use it below, even though we don't own it.
469 builder.setMCJITMemoryManager(
470 std::unique_ptr<RTDyldMemoryManager>(RTDyldMM));
Lang Hames0f154902014-09-23 16:56:02 +0000471 } else if (RemoteMCJIT) {
Jonas Devlieghere4a2863c2018-04-22 08:02:11 +0000472 WithColor::error(errs(), argv[0])
473 << "remote process execution does not work with the interpreter.\n";
Lang Hames0f154902014-09-23 16:56:02 +0000474 exit(1);
Eli Bendersky4c647582012-01-16 08:56:09 +0000475 }
Daniel Dunbar70ff8b02010-11-17 16:06:37 +0000476
Lang Hames1899d0c2015-06-09 02:43:27 +0000477 builder.setOptLevel(getOptLevel());
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000478
serge-sans-pailleac1d23e2020-03-04 00:47:43 +0100479 TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
480 if (codegen::getFloatABIForCalls() != FloatABI::Default)
481 Options.FloatABIType = codegen::getFloatABIForCalls();
Tim Northoverd4a2f5b2012-10-12 09:55:13 +0000482
Tim Northoverd4a2f5b2012-10-12 09:55:13 +0000483 builder.setTargetOptions(Options);
484
Mehdi Aminia7de8202016-04-18 18:52:39 +0000485 std::unique_ptr<ExecutionEngine> EE(builder.create());
Chris Lattnerf840ed72009-07-07 18:31:09 +0000486 if (!EE) {
487 if (!ErrorMsg.empty())
Jonas Devlieghere4a2863c2018-04-22 08:02:11 +0000488 WithColor::error(errs(), argv[0])
489 << "error creating EE: " << ErrorMsg << "\n";
Chris Lattnerf840ed72009-07-07 18:31:09 +0000490 else
Jonas Devlieghere4a2863c2018-04-22 08:02:11 +0000491 WithColor::error(errs(), argv[0]) << "unknown error creating EE!\n";
Chris Lattner2785bdb2007-05-06 04:58:26 +0000492 exit(1);
493 }
494
Mehdi Aminia7de8202016-04-18 18:52:39 +0000495 std::unique_ptr<LLIObjectCache> CacheManager;
Lang Hames173c69f2014-01-08 04:09:09 +0000496 if (EnableCacheManager) {
Mehdi Aminia7de8202016-04-18 18:52:39 +0000497 CacheManager.reset(new LLIObjectCache(ObjectCacheDir));
498 EE->setObjectCache(CacheManager.get());
Lang Hames173c69f2014-01-08 04:09:09 +0000499 }
500
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +0000501 // Load any additional modules specified on the command line.
502 for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) {
Rafael Espindolad233b062014-08-26 17:29:46 +0000503 std::unique_ptr<Module> XMod = parseIRFile(ExtraModules[i], Err, Context);
Davide Italianoda8e6b22016-11-17 22:58:13 +0000504 if (!XMod)
505 reportError(Err, argv[0]);
Lang Hames173c69f2014-01-08 04:09:09 +0000506 if (EnableCacheManager) {
Eric Christopher79cc1e32014-09-02 22:28:02 +0000507 std::string CacheName("file:");
508 CacheName.append(ExtraModules[i]);
509 XMod->setModuleIdentifier(CacheName);
Lang Hames173c69f2014-01-08 04:09:09 +0000510 }
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000511 EE->addModule(std::move(XMod));
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +0000512 }
513
Lang Hames173c69f2014-01-08 04:09:09 +0000514 for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) {
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000515 Expected<object::OwningBinary<object::ObjectFile>> Obj =
Rafael Espindola51cc3602014-01-22 00:14:49 +0000516 object::ObjectFile::createObjectFile(ExtraObjects[i]);
Lang Hames173c69f2014-01-08 04:09:09 +0000517 if (!Obj) {
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000518 // TODO: Actually report errors helpfully.
519 consumeError(Obj.takeError());
Davide Italianoda8e6b22016-11-17 22:58:13 +0000520 reportError(Err, argv[0]);
Lang Hames173c69f2014-01-08 04:09:09 +0000521 }
Rafael Espindola061beab2014-08-20 15:19:37 +0000522 object::OwningBinary<object::ObjectFile> &O = Obj.get();
Rafael Espindola7271c192014-08-26 21:04:04 +0000523 EE->addObjectFile(std::move(O));
Lang Hames173c69f2014-01-08 04:09:09 +0000524 }
525
526 for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000527 ErrorOr<std::unique_ptr<MemoryBuffer>> ArBufOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000528 MemoryBuffer::getFileOrSTDIN(ExtraArchives[i]);
Davide Italianoda8e6b22016-11-17 22:58:13 +0000529 if (!ArBufOrErr)
530 reportError(Err, argv[0]);
Rafael Espindola48af1c22014-08-19 18:44:46 +0000531 std::unique_ptr<MemoryBuffer> &ArBuf = ArBufOrErr.get();
Rafael Espindolae1923412014-08-01 18:31:17 +0000532
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000533 Expected<std::unique_ptr<object::Archive>> ArOrErr =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000534 object::Archive::create(ArBuf->getMemBufferRef());
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000535 if (!ArOrErr) {
536 std::string Buf;
537 raw_string_ostream OS(Buf);
Jonas Devlieghere45eb84f2018-11-11 01:46:03 +0000538 logAllUnhandledErrors(ArOrErr.takeError(), OS);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000539 OS.flush();
540 errs() << Buf;
Davide Italiano86511442016-11-17 22:59:13 +0000541 exit(1);
Lang Hames173c69f2014-01-08 04:09:09 +0000542 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000543 std::unique_ptr<object::Archive> &Ar = ArOrErr.get();
544
545 object::OwningBinary<object::Archive> OB(std::move(Ar), std::move(ArBuf));
546
547 EE->addArchive(std::move(OB));
Lang Hames173c69f2014-01-08 04:09:09 +0000548 }
549
Andrew Kaylor1ca510e2013-10-29 01:29:56 +0000550 // If the target is Cygwin/MingW and we are generating remote code, we
551 // need an extra module to help out with linking.
552 if (RemoteMCJIT && Triple(Mod->getTargetTriple()).isOSCygMing()) {
Mehdi Aminia7de8202016-04-18 18:52:39 +0000553 addCygMingExtraModule(*EE, Context, Mod->getTargetTriple());
Andrew Kaylor1ca510e2013-10-29 01:29:56 +0000554 }
555
Eli Bendersky5262ad22012-03-13 08:33:15 +0000556 // The following functions have no effect if their respective profiling
557 // support wasn't enabled in the build configuration.
558 EE->RegisterJITEventListener(
559 JITEventListener::createOProfileJITEventListener());
560 EE->RegisterJITEventListener(
561 JITEventListener::createIntelJITEventListener());
Andres Freund376a3d32018-07-24 00:54:06 +0000562 if (!RemoteMCJIT)
563 EE->RegisterJITEventListener(
564 JITEventListener::createPerfJITEventListener());
Jeffrey Yasskin0b08f3d2009-06-25 02:04:04 +0000565
Jim Grosbach0f435d02012-09-05 16:50:34 +0000566 if (!NoLazyCompilation && RemoteMCJIT) {
Jonas Devlieghere4a2863c2018-04-22 08:02:11 +0000567 WithColor::warning(errs(), argv[0])
568 << "remote mcjit does not support lazy compilation\n";
Jim Grosbach0f435d02012-09-05 16:50:34 +0000569 NoLazyCompilation = true;
570 }
Jeffrey Yasskinaa8814a2009-10-27 22:39:42 +0000571 EE->DisableLazyCompilation(NoLazyCompilation);
Evan Cheng0422bef2008-04-22 06:51:41 +0000572
Chris Lattner2785bdb2007-05-06 04:58:26 +0000573 // If the user specifically requested an argv[0] to pass into the program,
574 // do it now.
575 if (!FakeArgv0.empty()) {
Chris Bienemane71fb5c2015-01-22 01:49:59 +0000576 InputFile = static_cast<std::string>(FakeArgv0);
Chris Lattner2785bdb2007-05-06 04:58:26 +0000577 } else {
578 // Otherwise, if there is a .bc suffix on the executable strip it off, it
579 // might confuse the program.
Benjamin Kramera944a9a2010-04-15 11:33:14 +0000580 if (StringRef(InputFile).endswith(".bc"))
Chris Lattner2785bdb2007-05-06 04:58:26 +0000581 InputFile.erase(InputFile.length() - 3);
582 }
583
584 // Add the module's name to the start of the vector of arguments to main().
585 InputArgv.insert(InputArgv.begin(), InputFile);
586
587 // Call the main function from M as if its signature were:
588 // int main (int argc, char **argv, const char **envp)
589 // using the contents of Args to determine argc & argv, and the contents of
590 // EnvVars to determine envp.
591 //
Evan Cheng61582542008-11-05 23:21:52 +0000592 Function *EntryFn = Mod->getFunction(EntryFunc);
593 if (!EntryFn) {
Jonas Devlieghere4a2863c2018-04-22 08:02:11 +0000594 WithColor::error(errs(), argv[0])
595 << '\'' << EntryFunc << "\' function not found in module.\n";
Chris Lattner2785bdb2007-05-06 04:58:26 +0000596 return -1;
597 }
598
Chris Lattner2785bdb2007-05-06 04:58:26 +0000599 // Reset errno to zero on entry to main.
600 errno = 0;
Eli Bendersky4c647582012-01-16 08:56:09 +0000601
Lang Hames3fde6522016-04-18 19:55:43 +0000602 int Result = -1;
Andrew Kaylorc87c3472013-10-02 18:04:40 +0000603
Lang Hames9d7a2692016-01-11 16:35:55 +0000604 // Sanity check use of remote-jit: LLI currently only supports use of the
605 // remote JIT on Unix platforms.
Lang Hames9d7a2692016-01-11 16:35:55 +0000606 if (RemoteMCJIT) {
607#ifndef LLVM_ON_UNIX
Jonas Devlieghere4a2863c2018-04-22 08:02:11 +0000608 WithColor::warning(errs(), argv[0])
Jonas Devlieghere7b5fa242018-04-22 08:35:00 +0000609 << "host does not support external remote targets.\n";
610 WithColor::note() << "defaulting to local execution\n";
Lang Hames4b6e0212016-01-11 21:41:34 +0000611 return -1;
Lang Hames9d7a2692016-01-11 16:35:55 +0000612#else
613 if (ChildExecPath.empty()) {
Jonas Devlieghere4a2863c2018-04-22 08:02:11 +0000614 WithColor::error(errs(), argv[0])
615 << "-remote-mcjit requires -mcjit-remote-process.\n";
Lang Hames9d7a2692016-01-11 16:35:55 +0000616 exit(1);
617 } else if (!sys::fs::can_execute(ChildExecPath)) {
Jonas Devlieghere4a2863c2018-04-22 08:02:11 +0000618 WithColor::error(errs(), argv[0])
619 << "unable to find usable child executable: '" << ChildExecPath
620 << "'\n";
Lang Hames9d7a2692016-01-11 16:35:55 +0000621 return -1;
622 }
623#endif
624 }
625
Andrew Kaylor58365b92012-11-27 19:49:00 +0000626 if (!RemoteMCJIT) {
Andrew Kaylor6587bcf2013-10-11 22:47:10 +0000627 // If the program doesn't explicitly call exit, we will need the Exit
628 // function later on to make an explicit call, so get the function now.
James Y Knight13680222019-02-01 02:28:03 +0000629 FunctionCallee Exit = Mod->getOrInsertFunction(
630 "exit", Type::getVoidTy(Context), Type::getInt32Ty(Context));
Andrew Kaylor6587bcf2013-10-11 22:47:10 +0000631
Andrew Kaylorc87c3472013-10-02 18:04:40 +0000632 // Run static constructors.
Eric Christopher79cc1e32014-09-02 22:28:02 +0000633 if (!ForceInterpreter) {
Andrew Kaylorea395922013-10-01 01:47:35 +0000634 // Give MCJIT a chance to apply relocations and set page permissions.
635 EE->finalizeObject();
636 }
637 EE->runStaticConstructorsDestructors(false);
Evan Cheng0422bef2008-04-22 06:51:41 +0000638
NAKAMURA Takumi87e08802013-12-07 11:21:42 +0000639 // Trigger compilation separately so code regions that need to be
Andrew Kaylorc87c3472013-10-02 18:04:40 +0000640 // invalidated will be known.
641 (void)EE->getPointerToFunction(EntryFn);
642 // Clear instruction cache before code will be executed.
643 if (RTDyldMM)
644 static_cast<SectionMemoryManager*>(RTDyldMM)->invalidateInstructionCache();
645
646 // Run main.
647 Result = EE->runFunctionAsMain(EntryFn, InputArgv, envp);
648
649 // Run static destructors.
650 EE->runStaticConstructorsDestructors(true);
651
652 // If the program didn't call exit explicitly, we should call it now.
653 // This ensures that any atexit handlers get called correctly.
James Y Knight13680222019-02-01 02:28:03 +0000654 if (Function *ExitF =
655 dyn_cast<Function>(Exit.getCallee()->stripPointerCasts())) {
656 if (ExitF->getFunctionType() == Exit.getFunctionType()) {
657 std::vector<GenericValue> Args;
658 GenericValue ResultGV;
659 ResultGV.IntVal = APInt(32, Result);
660 Args.push_back(ResultGV);
661 EE->runFunction(ExitF, Args);
662 WithColor::error(errs(), argv[0])
663 << "exit(" << Result << ") returned!\n";
664 abort();
665 }
Andrew Kaylorc87c3472013-10-02 18:04:40 +0000666 }
James Y Knight13680222019-02-01 02:28:03 +0000667 WithColor::error(errs(), argv[0]) << "exit defined with wrong prototype!\n";
668 abort();
Andrew Kaylorc87c3472013-10-02 18:04:40 +0000669 } else {
670 // else == "if (RemoteMCJIT)"
671
672 // Remote target MCJIT doesn't (yet) support static constructors. No reason
Hiroshi Inoue0ca79dc2017-07-11 06:04:59 +0000673 // it couldn't. This is a limitation of the LLI implementation, not the
Andrew Kaylorc87c3472013-10-02 18:04:40 +0000674 // MCJIT itself. FIXME.
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000675
Lang Hames9d7a2692016-01-11 16:35:55 +0000676 // Lanch the remote process and get a channel to it.
Lang Hames1f2bf2d2016-11-11 21:42:09 +0000677 std::unique_ptr<FDRawChannel> C = launchRemote();
Lang Hames9d7a2692016-01-11 16:35:55 +0000678 if (!C) {
Jonas Devlieghere4a2863c2018-04-22 08:02:11 +0000679 WithColor::error(errs(), argv[0]) << "failed to launch remote JIT.\n";
Lang Hames9d7a2692016-01-11 16:35:55 +0000680 exit(1);
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000681 }
682
Lang Hames9d7a2692016-01-11 16:35:55 +0000683 // Create a remote target client running over the channel.
Lang Hamesbd0cb782018-05-30 01:57:45 +0000684 llvm::orc::ExecutionSession ES;
685 ES.setErrorReporter([&](Error Err) { ExitOnErr(std::move(Err)); });
Lang Hames9e68b7342017-09-04 20:54:46 +0000686 typedef orc::remote::OrcRemoteTargetClient MyRemote;
Lang Hamesbd0cb782018-05-30 01:57:45 +0000687 auto R = ExitOnErr(MyRemote::Create(*C, ES));
Jim Grosbach748b9472012-08-28 23:22:30 +0000688
Lang Hames9d7a2692016-01-11 16:35:55 +0000689 // Create a remote memory manager.
Lang Hames9e68b7342017-09-04 20:54:46 +0000690 auto RemoteMM = ExitOnErr(R->createRemoteMemoryManager());
Lang Hames9d7a2692016-01-11 16:35:55 +0000691
692 // Forward MCJIT's memory manager calls to the remote memory manager.
693 static_cast<ForwardingMemoryManager*>(RTDyldMM)->setMemMgr(
694 std::move(RemoteMM));
695
696 // Forward MCJIT's symbol resolution calls to the remote.
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +0000697 static_cast<ForwardingMemoryManager *>(RTDyldMM)->setResolver(
698 orc::createLambdaResolver(
Lang Hames17164542019-07-17 16:40:52 +0000699 AcknowledgeORCv1Deprecation,
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +0000700 [](const std::string &Name) { return nullptr; },
701 [&](const std::string &Name) {
702 if (auto Addr = ExitOnErr(R->getSymbolAddress(Name)))
703 return JITSymbol(Addr, JITSymbolFlags::Exported);
704 return JITSymbol(nullptr);
705 }));
Lang Hames9d7a2692016-01-11 16:35:55 +0000706
707 // Grab the target address of the JIT'd main function on the remote and call
708 // it.
Jim Grosbach0f435d02012-09-05 16:50:34 +0000709 // FIXME: argv and envp handling.
Lang Hamesad4a9112016-08-01 20:49:11 +0000710 JITTargetAddress Entry = EE->getFunctionAddress(EntryFn->getName().str());
Lang Hames9d7a2692016-01-11 16:35:55 +0000711 EE->finalizeObject();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000712 LLVM_DEBUG(dbgs() << "Executing '" << EntryFn->getName() << "' at 0x"
713 << format("%llx", Entry) << "\n");
Lang Hames1f2bf2d2016-11-11 21:42:09 +0000714 Result = ExitOnErr(R->callIntVoid(Entry));
Jim Grosbach0f435d02012-09-05 16:50:34 +0000715
Andrew Kaylorc87c3472013-10-02 18:04:40 +0000716 // Like static constructors, the remote target MCJIT support doesn't handle
717 // this yet. It could. FIXME.
718
Lang Hames9d7a2692016-01-11 16:35:55 +0000719 // Delete the EE - we need to tear it down *before* we terminate the session
720 // with the remote, otherwise it'll crash when it tries to release resources
721 // on a remote that has already been disconnected.
Mehdi Aminia7de8202016-04-18 18:52:39 +0000722 EE.reset();
Lang Hames9d7a2692016-01-11 16:35:55 +0000723
724 // Signal the remote target that we're done JITing.
Lang Hames1f2bf2d2016-11-11 21:42:09 +0000725 ExitOnErr(R->terminateSession());
Chris Lattner2785bdb2007-05-06 04:58:26 +0000726 }
Jim Grosbach0f435d02012-09-05 16:50:34 +0000727
Jim Grosbach0f435d02012-09-05 16:50:34 +0000728 return Result;
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000729}
Lang Hames9d7a2692016-01-11 16:35:55 +0000730
Lang Hames809e9d12019-08-02 15:21:37 +0000731static std::function<void(Module &)> createDebugDumper() {
Lang Hames6a941342018-06-26 21:35:48 +0000732 switch (OrcDumpKind) {
733 case DumpKind::NoDump:
Lang Hames809e9d12019-08-02 15:21:37 +0000734 return [](Module &M) {};
Lang Hames6a941342018-06-26 21:35:48 +0000735
736 case DumpKind::DumpFuncsToStdOut:
Lang Hames809e9d12019-08-02 15:21:37 +0000737 return [](Module &M) {
Lang Hames6a941342018-06-26 21:35:48 +0000738 printf("[ ");
739
Lang Hames809e9d12019-08-02 15:21:37 +0000740 for (const auto &F : M) {
Lang Hames6a941342018-06-26 21:35:48 +0000741 if (F.isDeclaration())
742 continue;
743
744 if (F.hasName()) {
Benjamin Krameradcd0262020-01-28 20:23:46 +0100745 std::string Name(std::string(F.getName()));
Lang Hames6a941342018-06-26 21:35:48 +0000746 printf("%s ", Name.c_str());
747 } else
748 printf("<anon> ");
749 }
750
751 printf("]\n");
Lang Hames6a941342018-06-26 21:35:48 +0000752 };
753
754 case DumpKind::DumpModsToStdOut:
Lang Hames809e9d12019-08-02 15:21:37 +0000755 return [](Module &M) {
756 outs() << "----- Module Start -----\n" << M << "----- Module End -----\n";
Lang Hames6a941342018-06-26 21:35:48 +0000757 };
758
759 case DumpKind::DumpModsToDisk:
Lang Hames809e9d12019-08-02 15:21:37 +0000760 return [](Module &M) {
Lang Hames6a941342018-06-26 21:35:48 +0000761 std::error_code EC;
Fangrui Songd9b948b2019-08-05 05:43:48 +0000762 raw_fd_ostream Out(M.getModuleIdentifier() + ".ll", EC, sys::fs::OF_Text);
Lang Hames6a941342018-06-26 21:35:48 +0000763 if (EC) {
Lang Hames809e9d12019-08-02 15:21:37 +0000764 errs() << "Couldn't open " << M.getModuleIdentifier()
Lang Hames6a941342018-06-26 21:35:48 +0000765 << " for dumping.\nError:" << EC.message() << "\n";
766 exit(1);
767 }
Lang Hames809e9d12019-08-02 15:21:37 +0000768 Out << M;
Lang Hames6a941342018-06-26 21:35:48 +0000769 };
770 }
771 llvm_unreachable("Unknown DumpKind");
772}
773
Lang Hamesb7be6b42020-02-14 14:22:59 -0800774Error loadDylibs() {
775 for (const auto &Dylib : Dylibs) {
776 std::string ErrMsg;
777 if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg))
778 return make_error<StringError>(ErrMsg, inconvertibleErrorCode());
779 }
780
781 return Error::success();
782}
783
Lang Hames23cb2e72018-10-23 23:01:39 +0000784static void exitOnLazyCallThroughFailure() { exit(1); }
785
Lang Hames1df947a2020-02-22 09:49:55 -0800786Expected<orc::ThreadSafeModule>
787loadModule(StringRef Path, orc::ThreadSafeContext TSCtx) {
788 SMDiagnostic Err;
789 auto M = parseIRFile(Path, Err, *TSCtx.getContext());
790 if (!M) {
791 std::string ErrMsg;
792 {
793 raw_string_ostream ErrMsgStream(ErrMsg);
794 Err.print("lli", ErrMsgStream);
795 }
796 return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
797 }
798
799 if (EnableCacheManager)
800 M->setModuleIdentifier("file:" + M->getModuleIdentifier());
801
802 return orc::ThreadSafeModule(std::move(M), std::move(TSCtx));
803}
804
Lang Hames37a66412018-08-28 20:20:31 +0000805int runOrcLazyJIT(const char *ProgName) {
806 // Start setting up the JIT environment.
Lang Hames6a941342018-06-26 21:35:48 +0000807
Lang Hames37a66412018-08-28 20:20:31 +0000808 // Parse the main module.
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000809 orc::ThreadSafeContext TSCtx(std::make_unique<LLVMContext>());
Lang Hames1df947a2020-02-22 09:49:55 -0800810 auto MainModule = ExitOnErr(loadModule(InputFile, TSCtx));
Lang Hames37a66412018-08-28 20:20:31 +0000811
Lang Hames1df947a2020-02-22 09:49:55 -0800812 // Get TargetTriple and DataLayout from the main module if they're explicitly
813 // set.
814 Optional<Triple> TT;
815 Optional<DataLayout> DL;
816 MainModule.withModuleDo([&](Module &M) {
817 if (!M.getTargetTriple().empty())
818 TT = Triple(M.getTargetTriple());
819 if (!M.getDataLayout().isDefault())
820 DL = M.getDataLayout();
821 });
822
Lang Hameseb14dc72019-04-29 22:37:27 +0000823 orc::LLLazyJITBuilder Builder;
824
825 Builder.setJITTargetMachineBuilder(
Lang Hames1df947a2020-02-22 09:49:55 -0800826 TT ? orc::JITTargetMachineBuilder(*TT)
827 : ExitOnErr(orc::JITTargetMachineBuilder::detectHost()));
828
829 TT = Builder.getJITTargetMachineBuilder()->getTargetTriple();
830 if (DL)
831 Builder.setDataLayout(DL);
Lang Hames6a941342018-06-26 21:35:48 +0000832
serge-sans-pailleac1d23e2020-03-04 00:47:43 +0100833 if (!codegen::getMArch().empty())
834 Builder.getJITTargetMachineBuilder()->getTargetTriple().setArchName(
835 codegen::getMArch());
Lang Hamesd435ce42018-09-30 19:12:23 +0000836
Lang Hameseb14dc72019-04-29 22:37:27 +0000837 Builder.getJITTargetMachineBuilder()
serge-sans-pailleac1d23e2020-03-04 00:47:43 +0100838 ->setCPU(codegen::getCPUStr())
839 .addFeatures(codegen::getFeatureList())
840 .setRelocationModel(codegen::getExplicitRelocModel())
841 .setCodeModel(codegen::getExplicitCodeModel());
Lang Hamesd435ce42018-09-30 19:12:23 +0000842
Lang Hameseb14dc72019-04-29 22:37:27 +0000843 Builder.setLazyCompileFailureAddr(
844 pointerToJITTargetAddress(exitOnLazyCallThroughFailure));
845 Builder.setNumCompileThreads(LazyJITCompileThreads);
Lang Hames23cb2e72018-10-23 23:01:39 +0000846
Lang Hames1df947a2020-02-22 09:49:55 -0800847 // If the object cache is enabled then set a custom compile function
848 // creator to use the cache.
849 std::unique_ptr<LLIObjectCache> CacheManager;
850 if (EnableCacheManager) {
851
852 CacheManager = std::make_unique<LLIObjectCache>(ObjectCacheDir);
853
854 Builder.setCompileFunctionCreator(
855 [&](orc::JITTargetMachineBuilder JTMB)
856 -> Expected<std::unique_ptr<orc::IRCompileLayer::IRCompiler>> {
857 if (LazyJITCompileThreads > 0)
858 return std::make_unique<orc::ConcurrentIRCompiler>(std::move(JTMB),
859 CacheManager.get());
860
861 auto TM = JTMB.createTargetMachine();
862 if (!TM)
863 return TM.takeError();
864
865 return std::make_unique<orc::TMOwningSimpleCompiler>(std::move(*TM),
866 CacheManager.get());
867 });
868 }
869
Lang Hames85fb9972019-12-16 02:50:40 -0800870 // Set up LLJIT platform.
871 {
872 LLJITPlatform P = Platform;
873 if (P == LLJITPlatform::DetectHost) {
Lang Hames1df947a2020-02-22 09:49:55 -0800874 if (TT->isOSBinFormatMachO())
Lang Hames85fb9972019-12-16 02:50:40 -0800875 P = LLJITPlatform::MachO;
876 else
877 P = LLJITPlatform::GenericIR;
878 }
879
880 switch (P) {
881 case LLJITPlatform::GenericIR:
882 // Nothing to do: LLJITBuilder will use this by default.
883 break;
884 case LLJITPlatform::MachO:
885 Builder.setPlatformSetUp(orc::setUpMachOPlatform);
886 ExitOnErr(orc::enableObjCRegistration("libobjc.dylib"));
887 break;
888 default:
889 llvm_unreachable("Unrecognized platform value");
890 }
891 }
892
Lang Hameseb14dc72019-04-29 22:37:27 +0000893 auto J = ExitOnErr(Builder.create());
Lang Hames6a941342018-06-26 21:35:48 +0000894
Lang Hames39253a52020-03-19 16:14:18 -0700895 if (TT->isOSBinFormatELF())
896 static_cast<llvm::orc::RTDyldObjectLinkingLayer &>(J->getObjLinkingLayer())
897 .registerJITEventListener(
898 *JITEventListener::createGDBRegistrationListener());
899
Lang Hames98440292018-09-29 23:49:57 +0000900 if (PerModuleLazy)
Lang Hames079df9a2018-10-15 22:56:10 +0000901 J->setPartitionFunction(orc::CompileOnDemandLayer::compileWholeModule);
Lang Hames98440292018-09-29 23:49:57 +0000902
Lang Hamesadae9bf2018-07-02 22:30:18 +0000903 auto Dump = createDebugDumper();
904
Lang Hamese9e26c02020-01-14 18:14:00 -0800905 J->getIRTransformLayer().setTransform(
906 [&](orc::ThreadSafeModule TSM,
907 const orc::MaterializationResponsibility &R) {
908 TSM.withModuleDo([&](Module &M) {
909 if (verifyModule(M, &dbgs())) {
910 dbgs() << "Bad module: " << &M << "\n";
911 exit(1);
912 }
913 Dump(M);
914 });
915 return TSM;
916 });
Lang Hames6a941342018-06-26 21:35:48 +0000917
Lang Hameseb14dc72019-04-29 22:37:27 +0000918 orc::MangleAndInterner Mangle(J->getExecutionSession(), J->getDataLayout());
Lang Hamesce2207a2020-01-21 16:28:30 -0800919
920 // Unless they've been explicitly disabled, make process symbols available to
921 // JIT'd code.
922 if (!NoProcessSymbols)
923 J->getMainJITDylib().addGenerator(
924 ExitOnErr(orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(
925 J->getDataLayout().getGlobalPrefix(),
926 [MainName = Mangle("main")](const orc::SymbolStringPtr &Name) {
927 return Name != MainName;
928 })));
Lang Hames35598312020-01-02 15:47:47 -0800929
Lang Hames37a66412018-08-28 20:20:31 +0000930 // Add the main module.
Lang Hames1df947a2020-02-22 09:49:55 -0800931 ExitOnErr(J->addLazyIRModule(std::move(MainModule)));
Lang Hames37a66412018-08-28 20:20:31 +0000932
Lang Hames23cb2e72018-10-23 23:01:39 +0000933 // Create JITDylibs and add any extra modules.
934 {
935 // Create JITDylibs, keep a map from argument index to dylib. We will use
936 // -extra-module argument indexes to determine what dylib to use for each
937 // -extra-module.
938 std::map<unsigned, orc::JITDylib *> IdxToDylib;
939 IdxToDylib[0] = &J->getMainJITDylib();
940 for (auto JDItr = JITDylibs.begin(), JDEnd = JITDylibs.end();
941 JDItr != JDEnd; ++JDItr) {
Lang Hamesa2ee80b2019-05-21 22:07:53 +0000942 orc::JITDylib *JD = J->getJITDylibByName(*JDItr);
Lang Hames85fb9972019-12-16 02:50:40 -0800943 if (!JD) {
944 JD = &ExitOnErr(J->createJITDylib(*JDItr));
Lang Hamesc66f8902020-05-04 16:43:42 -0700945 J->getMainJITDylib().addToLinkOrder(*JD);
946 JD->addToLinkOrder(J->getMainJITDylib());
Lang Hames85fb9972019-12-16 02:50:40 -0800947 }
Lang Hamesa2ee80b2019-05-21 22:07:53 +0000948 IdxToDylib[JITDylibs.getPosition(JDItr - JITDylibs.begin())] = JD;
Lang Hames23cb2e72018-10-23 23:01:39 +0000949 }
Lang Hames37a66412018-08-28 20:20:31 +0000950
Lang Hames23cb2e72018-10-23 23:01:39 +0000951 for (auto EMItr = ExtraModules.begin(), EMEnd = ExtraModules.end();
952 EMItr != EMEnd; ++EMItr) {
Lang Hames1df947a2020-02-22 09:49:55 -0800953 auto M = ExitOnErr(loadModule(*EMItr, TSCtx));
Lang Hames23cb2e72018-10-23 23:01:39 +0000954
955 auto EMIdx = ExtraModules.getPosition(EMItr - ExtraModules.begin());
956 assert(EMIdx != 0 && "ExtraModule should have index > 0");
957 auto JDItr = std::prev(IdxToDylib.lower_bound(EMIdx));
958 auto &JD = *JDItr->second;
Lang Hames1df947a2020-02-22 09:49:55 -0800959 ExitOnErr(J->addLazyIRModule(JD, std::move(M)));
Lang Hames23cb2e72018-10-23 23:01:39 +0000960 }
Lang Hames52a34a72019-08-13 16:05:18 +0000961
962 for (auto EAItr = ExtraArchives.begin(), EAEnd = ExtraArchives.end();
963 EAItr != EAEnd; ++EAItr) {
964 auto EAIdx = ExtraArchives.getPosition(EAItr - ExtraArchives.begin());
965 assert(EAIdx != 0 && "ExtraArchive should have index > 0");
966 auto JDItr = std::prev(IdxToDylib.lower_bound(EAIdx));
967 auto &JD = *JDItr->second;
968 JD.addGenerator(ExitOnErr(orc::StaticLibraryDefinitionGenerator::Load(
Lang Hames1b39c6f2020-04-04 09:50:56 -0700969 J->getObjLinkingLayer(), EAItr->c_str(), *TT)));
Lang Hames52a34a72019-08-13 16:05:18 +0000970 }
Lang Hamesadae9bf2018-07-02 22:30:18 +0000971 }
Lang Hames6a941342018-06-26 21:35:48 +0000972
Lang Hames37a66412018-08-28 20:20:31 +0000973 // Add the objects.
974 for (auto &ObjPath : ExtraObjects) {
975 auto Obj = ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ObjPath)));
976 ExitOnErr(J->addObjectFile(std::move(Obj)));
977 }
978
Lang Hames37a66412018-08-28 20:20:31 +0000979 // Run any static constructors.
Lang Hames85fb9972019-12-16 02:50:40 -0800980 ExitOnErr(J->initialize(J->getMainJITDylib()));
Lang Hames6a941342018-06-26 21:35:48 +0000981
Lang Hamesf0a3fd882018-09-26 16:26:59 +0000982 // Run any -thread-entry points.
983 std::vector<std::thread> AltEntryThreads;
984 for (auto &ThreadEntryPoint : ThreadEntryPoints) {
985 auto EntryPointSym = ExitOnErr(J->lookup(ThreadEntryPoint));
986 typedef void (*EntryPointPtr)();
987 auto EntryPoint =
988 reinterpret_cast<EntryPointPtr>(static_cast<uintptr_t>(EntryPointSym.getAddress()));
989 AltEntryThreads.push_back(std::thread([EntryPoint]() { EntryPoint(); }));
990 }
991
Lang Hames37a66412018-08-28 20:20:31 +0000992 // Run main.
Lang Hames6a941342018-06-26 21:35:48 +0000993 auto MainSym = ExitOnErr(J->lookup("main"));
Lang Hames94fe9d42018-09-27 19:27:19 +0000994
Lang Hamesece8fed2019-12-02 01:45:49 -0800995 typedef int (*MainFnPtr)(int, char *[]);
996 auto Result = orc::runAsMain(
Lang Hames2cdb18a2020-01-11 12:58:38 -0800997 jitTargetAddressToFunction<MainFnPtr>(MainSym.getAddress()), InputArgv,
998 StringRef(InputFile));
Lang Hames6a941342018-06-26 21:35:48 +0000999
Lang Hamesf0a3fd882018-09-26 16:26:59 +00001000 // Wait for -entry-point threads.
1001 for (auto &AltEntryThread : AltEntryThreads)
1002 AltEntryThread.join();
Hans Wennborg20b5abe2018-09-26 12:15:23 +00001003
Lang Hamesf0a3fd882018-09-26 16:26:59 +00001004 // Run destructors.
Lang Hames85fb9972019-12-16 02:50:40 -08001005 ExitOnErr(J->deinitialize(J->getMainJITDylib()));
Lang Hames6a941342018-06-26 21:35:48 +00001006
1007 return Result;
1008}
1009
Lang Hames98440292018-09-29 23:49:57 +00001010void disallowOrcOptions() {
1011 // Make sure nobody used an orc-lazy specific option accidentally.
1012
1013 if (LazyJITCompileThreads != 0) {
1014 errs() << "-compile-threads requires -jit-kind=orc-lazy\n";
1015 exit(1);
1016 }
1017
1018 if (!ThreadEntryPoints.empty()) {
1019 errs() << "-thread-entry requires -jit-kind=orc-lazy\n";
1020 exit(1);
1021 }
1022
1023 if (PerModuleLazy) {
1024 errs() << "-per-module-lazy requires -jit-kind=orc-lazy\n";
1025 exit(1);
1026 }
1027}
1028
Lang Hames1f2bf2d2016-11-11 21:42:09 +00001029std::unique_ptr<FDRawChannel> launchRemote() {
Lang Hames9d7a2692016-01-11 16:35:55 +00001030#ifndef LLVM_ON_UNIX
1031 llvm_unreachable("launchRemote not supported on non-Unix platforms");
1032#else
1033 int PipeFD[2][2];
1034 pid_t ChildPID;
1035
1036 // Create two pipes.
1037 if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0)
1038 perror("Error creating pipe: ");
1039
1040 ChildPID = fork();
1041
1042 if (ChildPID == 0) {
1043 // In the child...
1044
1045 // Close the parent ends of the pipes
1046 close(PipeFD[0][1]);
1047 close(PipeFD[1][0]);
1048
1049
1050 // Execute the child process.
1051 std::unique_ptr<char[]> ChildPath, ChildIn, ChildOut;
1052 {
1053 ChildPath.reset(new char[ChildExecPath.size() + 1]);
1054 std::copy(ChildExecPath.begin(), ChildExecPath.end(), &ChildPath[0]);
1055 ChildPath[ChildExecPath.size()] = '\0';
NAKAMURA Takumicef0a8212016-01-15 02:14:46 +00001056 std::string ChildInStr = utostr(PipeFD[0][0]);
Lang Hames9d7a2692016-01-11 16:35:55 +00001057 ChildIn.reset(new char[ChildInStr.size() + 1]);
1058 std::copy(ChildInStr.begin(), ChildInStr.end(), &ChildIn[0]);
1059 ChildIn[ChildInStr.size()] = '\0';
NAKAMURA Takumicef0a8212016-01-15 02:14:46 +00001060 std::string ChildOutStr = utostr(PipeFD[1][1]);
Lang Hames9d7a2692016-01-11 16:35:55 +00001061 ChildOut.reset(new char[ChildOutStr.size() + 1]);
1062 std::copy(ChildOutStr.begin(), ChildOutStr.end(), &ChildOut[0]);
1063 ChildOut[ChildOutStr.size()] = '\0';
1064 }
1065
1066 char * const args[] = { &ChildPath[0], &ChildIn[0], &ChildOut[0], nullptr };
1067 int rc = execv(ChildExecPath.c_str(), args);
1068 if (rc != 0)
1069 perror("Error executing child process: ");
1070 llvm_unreachable("Error executing child process");
1071 }
1072 // else we're the parent...
1073
1074 // Close the child ends of the pipes
1075 close(PipeFD[0][0]);
1076 close(PipeFD[1][1]);
1077
1078 // Return an RPC channel connected to our end of the pipes.
Jonas Devlieghere0eaee542019-08-15 15:54:37 +00001079 return std::make_unique<FDRawChannel>(PipeFD[1][0], PipeFD[0][1]);
Lang Hames9d7a2692016-01-11 16:35:55 +00001080#endif
1081}