1 //===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
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 // 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
11 // compiler, or through an interpreter if no JIT is available for this platform.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "ExecutionUtils.h"
16 #include "ForwardingMemoryManager.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Bitcode/BitcodeReader.h"
20 #include "llvm/CodeGen/CommandFlags.h"
21 #include "llvm/CodeGen/LinkAllCodegenComponents.h"
22 #include "llvm/Config/llvm-config.h"
23 #include "llvm/ExecutionEngine/GenericValue.h"
24 #include "llvm/ExecutionEngine/Interpreter.h"
25 #include "llvm/ExecutionEngine/JITEventListener.h"
26 #include "llvm/ExecutionEngine/JITSymbol.h"
27 #include "llvm/ExecutionEngine/MCJIT.h"
28 #include "llvm/ExecutionEngine/ObjectCache.h"
29 #include "llvm/ExecutionEngine/Orc/DebugObjectManagerPlugin.h"
30 #include "llvm/ExecutionEngine/Orc/DebugUtils.h"
31 #include "llvm/ExecutionEngine/Orc/ELFNixPlatform.h"
32 #include "llvm/ExecutionEngine/Orc/EPCDebugObjectRegistrar.h"
33 #include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
34 #include "llvm/ExecutionEngine/Orc/EPCEHFrameRegistrar.h"
35 #include "llvm/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.h"
36 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
37 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
38 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
39 #include "llvm/ExecutionEngine/Orc/MachOPlatform.h"
40 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
41 #include "llvm/ExecutionEngine/Orc/SimpleRemoteEPC.h"
42 #include "llvm/ExecutionEngine/Orc/SymbolStringPool.h"
43 #include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
44 #include "llvm/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.h"
45 #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
46 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
47 #include "llvm/IR/IRBuilder.h"
48 #include "llvm/IR/LLVMContext.h"
49 #include "llvm/IR/Module.h"
50 #include "llvm/IR/Type.h"
51 #include "llvm/IR/Verifier.h"
52 #include "llvm/IRReader/IRReader.h"
53 #include "llvm/Object/Archive.h"
54 #include "llvm/Object/ObjectFile.h"
55 #include "llvm/Support/CommandLine.h"
56 #include "llvm/Support/Debug.h"
57 #include "llvm/Support/DynamicLibrary.h"
58 #include "llvm/Support/Format.h"
59 #include "llvm/Support/InitLLVM.h"
60 #include "llvm/Support/MathExtras.h"
61 #include "llvm/Support/Memory.h"
62 #include "llvm/Support/MemoryBuffer.h"
63 #include "llvm/Support/Path.h"
64 #include "llvm/Support/PluginLoader.h"
65 #include "llvm/Support/Process.h"
66 #include "llvm/Support/Program.h"
67 #include "llvm/Support/SourceMgr.h"
68 #include "llvm/Support/TargetSelect.h"
69 #include "llvm/Support/WithColor.h"
70 #include "llvm/Support/raw_ostream.h"
71 #include "llvm/Transforms/Instrumentation.h"
72 #include <cerrno>
73
74 #if !defined(_MSC_VER) && !defined(__MINGW32__)
75 #include <unistd.h>
76 #else
77 #include <io.h>
78 #endif
79
80 #ifdef __CYGWIN__
81 #include <cygwin/version.h>
82 #if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
83 #define DO_NOTHING_ATEXIT 1
84 #endif
85 #endif
86
87 using namespace llvm;
88
89 static codegen::RegisterCodeGenFlags CGF;
90
91 #define DEBUG_TYPE "lli"
92
93 namespace {
94
95 enum class JITKind { MCJIT, Orc, OrcLazy };
96 enum class JITLinkerKind { Default, RuntimeDyld, JITLink };
97
98 cl::opt<std::string>
99 InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-"));
100
101 cl::list<std::string>
102 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
103
104 cl::opt<bool> ForceInterpreter("force-interpreter",
105 cl::desc("Force interpretation: disable JIT"),
106 cl::init(false));
107
108 cl::opt<JITKind> UseJITKind(
109 "jit-kind", cl::desc("Choose underlying JIT kind."),
110 cl::init(JITKind::Orc),
111 cl::values(clEnumValN(JITKind::MCJIT, "mcjit", "MCJIT"),
112 clEnumValN(JITKind::Orc, "orc", "Orc JIT"),
113 clEnumValN(JITKind::OrcLazy, "orc-lazy",
114 "Orc-based lazy JIT.")));
115
116 cl::opt<JITLinkerKind>
117 JITLinker("jit-linker", cl::desc("Choose the dynamic linker/loader."),
118 cl::init(JITLinkerKind::Default),
119 cl::values(clEnumValN(JITLinkerKind::Default, "default",
120 "Default for platform and JIT-kind"),
121 clEnumValN(JITLinkerKind::RuntimeDyld, "rtdyld",
122 "RuntimeDyld"),
123 clEnumValN(JITLinkerKind::JITLink, "jitlink",
124 "Orc-specific linker")));
125 cl::opt<std::string> OrcRuntime("orc-runtime",
126 cl::desc("Use ORC runtime from given path"),
127 cl::init(""));
128
129 cl::opt<unsigned>
130 LazyJITCompileThreads("compile-threads",
131 cl::desc("Choose the number of compile threads "
132 "(jit-kind=orc-lazy only)"),
133 cl::init(0));
134
135 cl::list<std::string>
136 ThreadEntryPoints("thread-entry",
137 cl::desc("calls the given entry-point on a new thread "
138 "(jit-kind=orc-lazy only)"));
139
140 cl::opt<bool> PerModuleLazy(
141 "per-module-lazy",
142 cl::desc("Performs lazy compilation on whole module boundaries "
143 "rather than individual functions"),
144 cl::init(false));
145
146 cl::list<std::string>
147 JITDylibs("jd",
148 cl::desc("Specifies the JITDylib to be used for any subsequent "
149 "-extra-module arguments."));
150
151 cl::list<std::string>
152 Dylibs("dlopen", cl::desc("Dynamic libraries to load before linking"));
153
154 // The MCJIT supports building for a target address space separate from
155 // the JIT compilation process. Use a forked process and a copying
156 // memory manager with IPC to execute using this functionality.
157 cl::opt<bool> RemoteMCJIT("remote-mcjit",
158 cl::desc("Execute MCJIT'ed code in a separate process."),
159 cl::init(false));
160
161 // Manually specify the child process for remote execution. This overrides
162 // the simulated remote execution that allocates address space for child
163 // execution. The child process will be executed and will communicate with
164 // lli via stdin/stdout pipes.
165 cl::opt<std::string>
166 ChildExecPath("mcjit-remote-process",
167 cl::desc("Specify the filename of the process to launch "
168 "for remote MCJIT execution. If none is specified,"
169 "\n\tremote execution will be simulated in-process."),
170 cl::value_desc("filename"), cl::init(""));
171
172 // Determine optimization level.
173 cl::opt<char> OptLevel("O",
174 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
175 "(default = '-O2')"),
176 cl::Prefix, cl::init(' '));
177
178 cl::opt<std::string>
179 TargetTriple("mtriple", cl::desc("Override target triple for module"));
180
181 cl::opt<std::string>
182 EntryFunc("entry-function",
183 cl::desc("Specify the entry function (default = 'main') "
184 "of the executable"),
185 cl::value_desc("function"),
186 cl::init("main"));
187
188 cl::list<std::string>
189 ExtraModules("extra-module",
190 cl::desc("Extra modules to be loaded"),
191 cl::value_desc("input bitcode"));
192
193 cl::list<std::string>
194 ExtraObjects("extra-object",
195 cl::desc("Extra object files to be loaded"),
196 cl::value_desc("input object"));
197
198 cl::list<std::string>
199 ExtraArchives("extra-archive",
200 cl::desc("Extra archive files to be loaded"),
201 cl::value_desc("input archive"));
202
203 cl::opt<bool>
204 EnableCacheManager("enable-cache-manager",
205 cl::desc("Use cache manager to save/load modules"),
206 cl::init(false));
207
208 cl::opt<std::string>
209 ObjectCacheDir("object-cache-dir",
210 cl::desc("Directory to store cached object files "
211 "(must be user writable)"),
212 cl::init(""));
213
214 cl::opt<std::string>
215 FakeArgv0("fake-argv0",
216 cl::desc("Override the 'argv[0]' value passed into the executing"
217 " program"), cl::value_desc("executable"));
218
219 cl::opt<bool>
220 DisableCoreFiles("disable-core-files", cl::Hidden,
221 cl::desc("Disable emission of core files if possible"));
222
223 cl::opt<bool>
224 NoLazyCompilation("disable-lazy-compilation",
225 cl::desc("Disable JIT lazy compilation"),
226 cl::init(false));
227
228 cl::opt<bool>
229 GenerateSoftFloatCalls("soft-float",
230 cl::desc("Generate software floating point library calls"),
231 cl::init(false));
232
233 cl::opt<bool> NoProcessSymbols(
234 "no-process-syms",
235 cl::desc("Do not resolve lli process symbols in JIT'd code"),
236 cl::init(false));
237
238 enum class LLJITPlatform { Inactive, DetectHost, ORC, GenericIR };
239
240 cl::opt<LLJITPlatform>
241 Platform("lljit-platform", cl::desc("Platform to use with LLJIT"),
242 cl::init(LLJITPlatform::DetectHost),
243 cl::values(clEnumValN(LLJITPlatform::DetectHost, "DetectHost",
244 "Select based on JIT target triple"),
245 clEnumValN(LLJITPlatform::ORC, "ORC",
246 "Use ORCPlatform with the ORC runtime"),
247 clEnumValN(LLJITPlatform::GenericIR, "GenericIR",
248 "Use LLJITGenericIRPlatform"),
249 clEnumValN(LLJITPlatform::Inactive, "Inactive",
250 "Disable platform support explicitly")),
251 cl::Hidden);
252
253 enum class DumpKind {
254 NoDump,
255 DumpFuncsToStdOut,
256 DumpModsToStdOut,
257 DumpModsToDisk
258 };
259
260 cl::opt<DumpKind> OrcDumpKind(
261 "orc-lazy-debug", cl::desc("Debug dumping for the orc-lazy JIT."),
262 cl::init(DumpKind::NoDump),
263 cl::values(clEnumValN(DumpKind::NoDump, "no-dump",
264 "Don't dump anything."),
265 clEnumValN(DumpKind::DumpFuncsToStdOut, "funcs-to-stdout",
266 "Dump function names to stdout."),
267 clEnumValN(DumpKind::DumpModsToStdOut, "mods-to-stdout",
268 "Dump modules to stdout."),
269 clEnumValN(DumpKind::DumpModsToDisk, "mods-to-disk",
270 "Dump modules to the current "
271 "working directory. (WARNING: "
272 "will overwrite existing files).")),
273 cl::Hidden);
274
275 cl::list<BuiltinFunctionKind> GenerateBuiltinFunctions(
276 "generate",
277 cl::desc("Provide built-in functions for access by JITed code "
278 "(jit-kind=orc-lazy only)"),
279 cl::values(clEnumValN(BuiltinFunctionKind::DumpDebugDescriptor,
280 "__dump_jit_debug_descriptor",
281 "Dump __jit_debug_descriptor contents to stdout"),
282 clEnumValN(BuiltinFunctionKind::DumpDebugObjects,
283 "__dump_jit_debug_objects",
284 "Dump __jit_debug_descriptor in-memory debug "
285 "objects as tool output")),
286 cl::Hidden);
287
288 ExitOnError ExitOnErr;
289 }
290
linkComponents()291 LLVM_ATTRIBUTE_USED void linkComponents() {
292 errs() << (void *)&llvm_orc_registerEHFrameSectionWrapper
293 << (void *)&llvm_orc_deregisterEHFrameSectionWrapper
294 << (void *)&llvm_orc_registerJITLoaderGDBWrapper
295 << (void *)&llvm_orc_registerJITLoaderGDBAllocAction;
296 }
297
298 //===----------------------------------------------------------------------===//
299 // Object cache
300 //
301 // This object cache implementation writes cached objects to disk to the
302 // directory specified by CacheDir, using a filename provided in the module
303 // descriptor. The cache tries to load a saved object using that path if the
304 // file exists. CacheDir defaults to "", in which case objects are cached
305 // alongside their originating bitcodes.
306 //
307 class LLIObjectCache : public ObjectCache {
308 public:
LLIObjectCache(const std::string & CacheDir)309 LLIObjectCache(const std::string& CacheDir) : CacheDir(CacheDir) {
310 // Add trailing '/' to cache dir if necessary.
311 if (!this->CacheDir.empty() &&
312 this->CacheDir[this->CacheDir.size() - 1] != '/')
313 this->CacheDir += '/';
314 }
~LLIObjectCache()315 ~LLIObjectCache() override {}
316
notifyObjectCompiled(const Module * M,MemoryBufferRef Obj)317 void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override {
318 const std::string &ModuleID = M->getModuleIdentifier();
319 std::string CacheName;
320 if (!getCacheFilename(ModuleID, CacheName))
321 return;
322 if (!CacheDir.empty()) { // Create user-defined cache dir.
323 SmallString<128> dir(sys::path::parent_path(CacheName));
324 sys::fs::create_directories(Twine(dir));
325 }
326
327 std::error_code EC;
328 raw_fd_ostream outfile(CacheName, EC, sys::fs::OF_None);
329 outfile.write(Obj.getBufferStart(), Obj.getBufferSize());
330 outfile.close();
331 }
332
getObject(const Module * M)333 std::unique_ptr<MemoryBuffer> getObject(const Module* M) override {
334 const std::string &ModuleID = M->getModuleIdentifier();
335 std::string CacheName;
336 if (!getCacheFilename(ModuleID, CacheName))
337 return nullptr;
338 // Load the object from the cache filename
339 ErrorOr<std::unique_ptr<MemoryBuffer>> IRObjectBuffer =
340 MemoryBuffer::getFile(CacheName, /*IsText=*/false,
341 /*RequiresNullTerminator=*/false);
342 // If the file isn't there, that's OK.
343 if (!IRObjectBuffer)
344 return nullptr;
345 // MCJIT will want to write into this buffer, and we don't want that
346 // because the file has probably just been mmapped. Instead we make
347 // a copy. The filed-based buffer will be released when it goes
348 // out of scope.
349 return MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer());
350 }
351
352 private:
353 std::string CacheDir;
354
getCacheFilename(const std::string & ModID,std::string & CacheName)355 bool getCacheFilename(const std::string &ModID, std::string &CacheName) {
356 std::string Prefix("file:");
357 size_t PrefixLength = Prefix.length();
358 if (ModID.substr(0, PrefixLength) != Prefix)
359 return false;
360
361 std::string CacheSubdir = ModID.substr(PrefixLength);
362 // Transform "X:\foo" => "/X\foo" for convenience on Windows.
363 if (is_style_windows(llvm::sys::path::Style::native) &&
364 isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') {
365 CacheSubdir[1] = CacheSubdir[0];
366 CacheSubdir[0] = '/';
367 }
368
369 CacheName = CacheDir + CacheSubdir;
370 size_t pos = CacheName.rfind('.');
371 CacheName.replace(pos, CacheName.length() - pos, ".o");
372 return true;
373 }
374 };
375
376 class ORCPlatformSupport : public orc::LLJIT::PlatformSupport {
377 public:
ORCPlatformSupport(orc::LLJIT & J)378 ORCPlatformSupport(orc::LLJIT &J) : J(J) {}
379
initialize(orc::JITDylib & JD)380 Error initialize(orc::JITDylib &JD) override {
381 using llvm::orc::shared::SPSExecutorAddr;
382 using llvm::orc::shared::SPSString;
383 using SPSDLOpenSig = SPSExecutorAddr(SPSString, int32_t);
384 enum dlopen_mode : int32_t {
385 ORC_RT_RTLD_LAZY = 0x1,
386 ORC_RT_RTLD_NOW = 0x2,
387 ORC_RT_RTLD_LOCAL = 0x4,
388 ORC_RT_RTLD_GLOBAL = 0x8
389 };
390
391 if (auto WrapperAddr = J.lookup("__orc_rt_jit_dlopen_wrapper")) {
392 return J.getExecutionSession().callSPSWrapper<SPSDLOpenSig>(
393 *WrapperAddr, DSOHandles[&JD], JD.getName(),
394 int32_t(ORC_RT_RTLD_LAZY));
395 } else
396 return WrapperAddr.takeError();
397 }
398
deinitialize(orc::JITDylib & JD)399 Error deinitialize(orc::JITDylib &JD) override {
400 using llvm::orc::shared::SPSExecutorAddr;
401 using SPSDLCloseSig = int32_t(SPSExecutorAddr);
402
403 if (auto WrapperAddr = J.lookup("__orc_rt_jit_dlclose_wrapper")) {
404 int32_t result;
405 auto E = J.getExecutionSession().callSPSWrapper<SPSDLCloseSig>(
406 *WrapperAddr, result, DSOHandles[&JD]);
407 if (E)
408 return E;
409 else if (result)
410 return make_error<StringError>("dlclose failed",
411 inconvertibleErrorCode());
412 DSOHandles.erase(&JD);
413 } else
414 return WrapperAddr.takeError();
415 return Error::success();
416 }
417
418 private:
419 orc::LLJIT &J;
420 DenseMap<orc::JITDylib *, orc::ExecutorAddr> DSOHandles;
421 };
422
423 // On Mingw and Cygwin, an external symbol named '__main' is called from the
424 // generated 'main' function to allow static initialization. To avoid linking
425 // problems with remote targets (because lli's remote target support does not
426 // currently handle external linking) we add a secondary module which defines
427 // an empty '__main' function.
addCygMingExtraModule(ExecutionEngine & EE,LLVMContext & Context,StringRef TargetTripleStr)428 static void addCygMingExtraModule(ExecutionEngine &EE, LLVMContext &Context,
429 StringRef TargetTripleStr) {
430 IRBuilder<> Builder(Context);
431 Triple TargetTriple(TargetTripleStr);
432
433 // Create a new module.
434 std::unique_ptr<Module> M = std::make_unique<Module>("CygMingHelper", Context);
435 M->setTargetTriple(TargetTripleStr);
436
437 // Create an empty function named "__main".
438 Type *ReturnTy;
439 if (TargetTriple.isArch64Bit())
440 ReturnTy = Type::getInt64Ty(Context);
441 else
442 ReturnTy = Type::getInt32Ty(Context);
443 Function *Result =
444 Function::Create(FunctionType::get(ReturnTy, {}, false),
445 GlobalValue::ExternalLinkage, "__main", M.get());
446
447 BasicBlock *BB = BasicBlock::Create(Context, "__main", Result);
448 Builder.SetInsertPoint(BB);
449 Value *ReturnVal = ConstantInt::get(ReturnTy, 0);
450 Builder.CreateRet(ReturnVal);
451
452 // Add this new module to the ExecutionEngine.
453 EE.addModule(std::move(M));
454 }
455
getOptLevel()456 CodeGenOpt::Level getOptLevel() {
457 switch (OptLevel) {
458 default:
459 WithColor::error(errs(), "lli") << "invalid optimization level.\n";
460 exit(1);
461 case '0': return CodeGenOpt::None;
462 case '1': return CodeGenOpt::Less;
463 case ' ':
464 case '2': return CodeGenOpt::Default;
465 case '3': return CodeGenOpt::Aggressive;
466 }
467 llvm_unreachable("Unrecognized opt level.");
468 }
469
reportError(SMDiagnostic Err,const char * ProgName)470 [[noreturn]] static void reportError(SMDiagnostic Err, const char *ProgName) {
471 Err.print(ProgName, errs());
472 exit(1);
473 }
474
475 Error loadDylibs();
476 int runOrcJIT(const char *ProgName);
477 void disallowOrcOptions();
478 Expected<std::unique_ptr<orc::ExecutorProcessControl>> launchRemote();
479
480 //===----------------------------------------------------------------------===//
481 // main Driver function
482 //
main(int argc,char ** argv,char * const * envp)483 int main(int argc, char **argv, char * const *envp) {
484 InitLLVM X(argc, argv);
485
486 if (argc > 1)
487 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
488
489 // If we have a native target, initialize it to ensure it is linked in and
490 // usable by the JIT.
491 InitializeNativeTarget();
492 InitializeNativeTargetAsmPrinter();
493 InitializeNativeTargetAsmParser();
494
495 cl::ParseCommandLineOptions(argc, argv,
496 "llvm interpreter & dynamic compiler\n");
497
498 // If the user doesn't want core files, disable them.
499 if (DisableCoreFiles)
500 sys::Process::PreventCoreFiles();
501
502 ExitOnErr(loadDylibs());
503
504 if (UseJITKind == JITKind::MCJIT)
505 disallowOrcOptions();
506 else
507 return runOrcJIT(argv[0]);
508
509 // Old lli implementation based on ExecutionEngine and MCJIT.
510 LLVMContext Context;
511
512 // Load the bitcode...
513 SMDiagnostic Err;
514 std::unique_ptr<Module> Owner = parseIRFile(InputFile, Err, Context);
515 Module *Mod = Owner.get();
516 if (!Mod)
517 reportError(Err, argv[0]);
518
519 if (EnableCacheManager) {
520 std::string CacheName("file:");
521 CacheName.append(InputFile);
522 Mod->setModuleIdentifier(CacheName);
523 }
524
525 // If not jitting lazily, load the whole bitcode file eagerly too.
526 if (NoLazyCompilation) {
527 // Use *argv instead of argv[0] to work around a wrong GCC warning.
528 ExitOnError ExitOnErr(std::string(*argv) +
529 ": bitcode didn't read correctly: ");
530 ExitOnErr(Mod->materializeAll());
531 }
532
533 std::string ErrorMsg;
534 EngineBuilder builder(std::move(Owner));
535 builder.setMArch(codegen::getMArch());
536 builder.setMCPU(codegen::getCPUStr());
537 builder.setMAttrs(codegen::getFeatureList());
538 if (auto RM = codegen::getExplicitRelocModel())
539 builder.setRelocationModel(RM.value());
540 if (auto CM = codegen::getExplicitCodeModel())
541 builder.setCodeModel(CM.value());
542 builder.setErrorStr(&ErrorMsg);
543 builder.setEngineKind(ForceInterpreter
544 ? EngineKind::Interpreter
545 : EngineKind::JIT);
546
547 // If we are supposed to override the target triple, do so now.
548 if (!TargetTriple.empty())
549 Mod->setTargetTriple(Triple::normalize(TargetTriple));
550
551 // Enable MCJIT if desired.
552 RTDyldMemoryManager *RTDyldMM = nullptr;
553 if (!ForceInterpreter) {
554 if (RemoteMCJIT)
555 RTDyldMM = new ForwardingMemoryManager();
556 else
557 RTDyldMM = new SectionMemoryManager();
558
559 // Deliberately construct a temp std::unique_ptr to pass in. Do not null out
560 // RTDyldMM: We still use it below, even though we don't own it.
561 builder.setMCJITMemoryManager(
562 std::unique_ptr<RTDyldMemoryManager>(RTDyldMM));
563 } else if (RemoteMCJIT) {
564 WithColor::error(errs(), argv[0])
565 << "remote process execution does not work with the interpreter.\n";
566 exit(1);
567 }
568
569 builder.setOptLevel(getOptLevel());
570
571 TargetOptions Options =
572 codegen::InitTargetOptionsFromCodeGenFlags(Triple(TargetTriple));
573 if (codegen::getFloatABIForCalls() != FloatABI::Default)
574 Options.FloatABIType = codegen::getFloatABIForCalls();
575
576 builder.setTargetOptions(Options);
577
578 std::unique_ptr<ExecutionEngine> EE(builder.create());
579 if (!EE) {
580 if (!ErrorMsg.empty())
581 WithColor::error(errs(), argv[0])
582 << "error creating EE: " << ErrorMsg << "\n";
583 else
584 WithColor::error(errs(), argv[0]) << "unknown error creating EE!\n";
585 exit(1);
586 }
587
588 std::unique_ptr<LLIObjectCache> CacheManager;
589 if (EnableCacheManager) {
590 CacheManager.reset(new LLIObjectCache(ObjectCacheDir));
591 EE->setObjectCache(CacheManager.get());
592 }
593
594 // Load any additional modules specified on the command line.
595 for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) {
596 std::unique_ptr<Module> XMod = parseIRFile(ExtraModules[i], Err, Context);
597 if (!XMod)
598 reportError(Err, argv[0]);
599 if (EnableCacheManager) {
600 std::string CacheName("file:");
601 CacheName.append(ExtraModules[i]);
602 XMod->setModuleIdentifier(CacheName);
603 }
604 EE->addModule(std::move(XMod));
605 }
606
607 for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) {
608 Expected<object::OwningBinary<object::ObjectFile>> Obj =
609 object::ObjectFile::createObjectFile(ExtraObjects[i]);
610 if (!Obj) {
611 // TODO: Actually report errors helpfully.
612 consumeError(Obj.takeError());
613 reportError(Err, argv[0]);
614 }
615 object::OwningBinary<object::ObjectFile> &O = Obj.get();
616 EE->addObjectFile(std::move(O));
617 }
618
619 for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) {
620 ErrorOr<std::unique_ptr<MemoryBuffer>> ArBufOrErr =
621 MemoryBuffer::getFileOrSTDIN(ExtraArchives[i]);
622 if (!ArBufOrErr)
623 reportError(Err, argv[0]);
624 std::unique_ptr<MemoryBuffer> &ArBuf = ArBufOrErr.get();
625
626 Expected<std::unique_ptr<object::Archive>> ArOrErr =
627 object::Archive::create(ArBuf->getMemBufferRef());
628 if (!ArOrErr) {
629 std::string Buf;
630 raw_string_ostream OS(Buf);
631 logAllUnhandledErrors(ArOrErr.takeError(), OS);
632 OS.flush();
633 errs() << Buf;
634 exit(1);
635 }
636 std::unique_ptr<object::Archive> &Ar = ArOrErr.get();
637
638 object::OwningBinary<object::Archive> OB(std::move(Ar), std::move(ArBuf));
639
640 EE->addArchive(std::move(OB));
641 }
642
643 // If the target is Cygwin/MingW and we are generating remote code, we
644 // need an extra module to help out with linking.
645 if (RemoteMCJIT && Triple(Mod->getTargetTriple()).isOSCygMing()) {
646 addCygMingExtraModule(*EE, Context, Mod->getTargetTriple());
647 }
648
649 // The following functions have no effect if their respective profiling
650 // support wasn't enabled in the build configuration.
651 EE->RegisterJITEventListener(
652 JITEventListener::createOProfileJITEventListener());
653 EE->RegisterJITEventListener(
654 JITEventListener::createIntelJITEventListener());
655 if (!RemoteMCJIT)
656 EE->RegisterJITEventListener(
657 JITEventListener::createPerfJITEventListener());
658
659 if (!NoLazyCompilation && RemoteMCJIT) {
660 WithColor::warning(errs(), argv[0])
661 << "remote mcjit does not support lazy compilation\n";
662 NoLazyCompilation = true;
663 }
664 EE->DisableLazyCompilation(NoLazyCompilation);
665
666 // If the user specifically requested an argv[0] to pass into the program,
667 // do it now.
668 if (!FakeArgv0.empty()) {
669 InputFile = static_cast<std::string>(FakeArgv0);
670 } else {
671 // Otherwise, if there is a .bc suffix on the executable strip it off, it
672 // might confuse the program.
673 if (StringRef(InputFile).endswith(".bc"))
674 InputFile.erase(InputFile.length() - 3);
675 }
676
677 // Add the module's name to the start of the vector of arguments to main().
678 InputArgv.insert(InputArgv.begin(), InputFile);
679
680 // Call the main function from M as if its signature were:
681 // int main (int argc, char **argv, const char **envp)
682 // using the contents of Args to determine argc & argv, and the contents of
683 // EnvVars to determine envp.
684 //
685 Function *EntryFn = Mod->getFunction(EntryFunc);
686 if (!EntryFn) {
687 WithColor::error(errs(), argv[0])
688 << '\'' << EntryFunc << "\' function not found in module.\n";
689 return -1;
690 }
691
692 // Reset errno to zero on entry to main.
693 errno = 0;
694
695 int Result = -1;
696
697 // Sanity check use of remote-jit: LLI currently only supports use of the
698 // remote JIT on Unix platforms.
699 if (RemoteMCJIT) {
700 #ifndef LLVM_ON_UNIX
701 WithColor::warning(errs(), argv[0])
702 << "host does not support external remote targets.\n";
703 WithColor::note() << "defaulting to local execution\n";
704 return -1;
705 #else
706 if (ChildExecPath.empty()) {
707 WithColor::error(errs(), argv[0])
708 << "-remote-mcjit requires -mcjit-remote-process.\n";
709 exit(1);
710 } else if (!sys::fs::can_execute(ChildExecPath)) {
711 WithColor::error(errs(), argv[0])
712 << "unable to find usable child executable: '" << ChildExecPath
713 << "'\n";
714 return -1;
715 }
716 #endif
717 }
718
719 std::unique_ptr<orc::ExecutorProcessControl> EPC =
720 RemoteMCJIT ? ExitOnErr(launchRemote())
721 : ExitOnErr(orc::SelfExecutorProcessControl::Create());
722
723 if (!RemoteMCJIT) {
724 // If the program doesn't explicitly call exit, we will need the Exit
725 // function later on to make an explicit call, so get the function now.
726 FunctionCallee Exit = Mod->getOrInsertFunction(
727 "exit", Type::getVoidTy(Context), Type::getInt32Ty(Context));
728
729 // Run static constructors.
730 if (!ForceInterpreter) {
731 // Give MCJIT a chance to apply relocations and set page permissions.
732 EE->finalizeObject();
733 }
734 EE->runStaticConstructorsDestructors(false);
735
736 // Trigger compilation separately so code regions that need to be
737 // invalidated will be known.
738 (void)EE->getPointerToFunction(EntryFn);
739 // Clear instruction cache before code will be executed.
740 if (RTDyldMM)
741 static_cast<SectionMemoryManager*>(RTDyldMM)->invalidateInstructionCache();
742
743 // Run main.
744 Result = EE->runFunctionAsMain(EntryFn, InputArgv, envp);
745
746 // Run static destructors.
747 EE->runStaticConstructorsDestructors(true);
748
749 // If the program didn't call exit explicitly, we should call it now.
750 // This ensures that any atexit handlers get called correctly.
751 if (Function *ExitF =
752 dyn_cast<Function>(Exit.getCallee()->stripPointerCasts())) {
753 if (ExitF->getFunctionType() == Exit.getFunctionType()) {
754 std::vector<GenericValue> Args;
755 GenericValue ResultGV;
756 ResultGV.IntVal = APInt(32, Result);
757 Args.push_back(ResultGV);
758 EE->runFunction(ExitF, Args);
759 WithColor::error(errs(), argv[0])
760 << "exit(" << Result << ") returned!\n";
761 abort();
762 }
763 }
764 WithColor::error(errs(), argv[0]) << "exit defined with wrong prototype!\n";
765 abort();
766 } else {
767 // else == "if (RemoteMCJIT)"
768
769 // Remote target MCJIT doesn't (yet) support static constructors. No reason
770 // it couldn't. This is a limitation of the LLI implementation, not the
771 // MCJIT itself. FIXME.
772
773 // Create a remote memory manager.
774 auto RemoteMM = ExitOnErr(
775 orc::EPCGenericRTDyldMemoryManager::CreateWithDefaultBootstrapSymbols(
776 *EPC));
777
778 // Forward MCJIT's memory manager calls to the remote memory manager.
779 static_cast<ForwardingMemoryManager*>(RTDyldMM)->setMemMgr(
780 std::move(RemoteMM));
781
782 // Forward MCJIT's symbol resolution calls to the remote.
783 static_cast<ForwardingMemoryManager *>(RTDyldMM)->setResolver(
784 ExitOnErr(RemoteResolver::Create(*EPC)));
785 // Grab the target address of the JIT'd main function on the remote and call
786 // it.
787 // FIXME: argv and envp handling.
788 auto Entry =
789 orc::ExecutorAddr(EE->getFunctionAddress(EntryFn->getName().str()));
790 EE->finalizeObject();
791 LLVM_DEBUG(dbgs() << "Executing '" << EntryFn->getName() << "' at 0x"
792 << format("%llx", Entry.getValue()) << "\n");
793 Result = ExitOnErr(EPC->runAsMain(Entry, {}));
794
795 // Like static constructors, the remote target MCJIT support doesn't handle
796 // this yet. It could. FIXME.
797
798 // Delete the EE - we need to tear it down *before* we terminate the session
799 // with the remote, otherwise it'll crash when it tries to release resources
800 // on a remote that has already been disconnected.
801 EE.reset();
802
803 // Signal the remote target that we're done JITing.
804 ExitOnErr(EPC->disconnect());
805 }
806
807 return Result;
808 }
809
createDebugDumper()810 static std::function<void(Module &)> createDebugDumper() {
811 switch (OrcDumpKind) {
812 case DumpKind::NoDump:
813 return [](Module &M) {};
814
815 case DumpKind::DumpFuncsToStdOut:
816 return [](Module &M) {
817 printf("[ ");
818
819 for (const auto &F : M) {
820 if (F.isDeclaration())
821 continue;
822
823 if (F.hasName()) {
824 std::string Name(std::string(F.getName()));
825 printf("%s ", Name.c_str());
826 } else
827 printf("<anon> ");
828 }
829
830 printf("]\n");
831 };
832
833 case DumpKind::DumpModsToStdOut:
834 return [](Module &M) {
835 outs() << "----- Module Start -----\n" << M << "----- Module End -----\n";
836 };
837
838 case DumpKind::DumpModsToDisk:
839 return [](Module &M) {
840 std::error_code EC;
841 raw_fd_ostream Out(M.getModuleIdentifier() + ".ll", EC,
842 sys::fs::OF_TextWithCRLF);
843 if (EC) {
844 errs() << "Couldn't open " << M.getModuleIdentifier()
845 << " for dumping.\nError:" << EC.message() << "\n";
846 exit(1);
847 }
848 Out << M;
849 };
850 }
851 llvm_unreachable("Unknown DumpKind");
852 }
853
loadDylibs()854 Error loadDylibs() {
855 for (const auto &Dylib : Dylibs) {
856 std::string ErrMsg;
857 if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg))
858 return make_error<StringError>(ErrMsg, inconvertibleErrorCode());
859 }
860
861 return Error::success();
862 }
863
exitOnLazyCallThroughFailure()864 static void exitOnLazyCallThroughFailure() { exit(1); }
865
866 Expected<orc::ThreadSafeModule>
loadModule(StringRef Path,orc::ThreadSafeContext TSCtx)867 loadModule(StringRef Path, orc::ThreadSafeContext TSCtx) {
868 SMDiagnostic Err;
869 auto M = parseIRFile(Path, Err, *TSCtx.getContext());
870 if (!M) {
871 std::string ErrMsg;
872 {
873 raw_string_ostream ErrMsgStream(ErrMsg);
874 Err.print("lli", ErrMsgStream);
875 }
876 return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
877 }
878
879 if (EnableCacheManager)
880 M->setModuleIdentifier("file:" + M->getModuleIdentifier());
881
882 return orc::ThreadSafeModule(std::move(M), std::move(TSCtx));
883 }
884
runOrcJIT(const char * ProgName)885 int runOrcJIT(const char *ProgName) {
886 // Start setting up the JIT environment.
887
888 // Parse the main module.
889 orc::ThreadSafeContext TSCtx(std::make_unique<LLVMContext>());
890 auto MainModule = ExitOnErr(loadModule(InputFile, TSCtx));
891
892 // Get TargetTriple and DataLayout from the main module if they're explicitly
893 // set.
894 Optional<Triple> TT;
895 Optional<DataLayout> DL;
896 MainModule.withModuleDo([&](Module &M) {
897 if (!M.getTargetTriple().empty())
898 TT = Triple(M.getTargetTriple());
899 if (!M.getDataLayout().isDefault())
900 DL = M.getDataLayout();
901 });
902
903 orc::LLLazyJITBuilder Builder;
904
905 Builder.setJITTargetMachineBuilder(
906 TT ? orc::JITTargetMachineBuilder(*TT)
907 : ExitOnErr(orc::JITTargetMachineBuilder::detectHost()));
908
909 TT = Builder.getJITTargetMachineBuilder()->getTargetTriple();
910 if (DL)
911 Builder.setDataLayout(DL);
912
913 if (!codegen::getMArch().empty())
914 Builder.getJITTargetMachineBuilder()->getTargetTriple().setArchName(
915 codegen::getMArch());
916
917 Builder.getJITTargetMachineBuilder()
918 ->setCPU(codegen::getCPUStr())
919 .addFeatures(codegen::getFeatureList())
920 .setRelocationModel(codegen::getExplicitRelocModel())
921 .setCodeModel(codegen::getExplicitCodeModel());
922
923 // FIXME: Setting a dummy call-through manager in non-lazy mode prevents the
924 // JIT builder to instantiate a default (which would fail with an error for
925 // unsupported architectures).
926 if (UseJITKind != JITKind::OrcLazy) {
927 auto ES = std::make_unique<orc::ExecutionSession>(
928 ExitOnErr(orc::SelfExecutorProcessControl::Create()));
929 Builder.setLazyCallthroughManager(
930 std::make_unique<orc::LazyCallThroughManager>(*ES, 0, nullptr));
931 Builder.setExecutionSession(std::move(ES));
932 }
933
934 Builder.setLazyCompileFailureAddr(
935 orc::ExecutorAddr::fromPtr(exitOnLazyCallThroughFailure));
936 Builder.setNumCompileThreads(LazyJITCompileThreads);
937
938 // If the object cache is enabled then set a custom compile function
939 // creator to use the cache.
940 std::unique_ptr<LLIObjectCache> CacheManager;
941 if (EnableCacheManager) {
942
943 CacheManager = std::make_unique<LLIObjectCache>(ObjectCacheDir);
944
945 Builder.setCompileFunctionCreator(
946 [&](orc::JITTargetMachineBuilder JTMB)
947 -> Expected<std::unique_ptr<orc::IRCompileLayer::IRCompiler>> {
948 if (LazyJITCompileThreads > 0)
949 return std::make_unique<orc::ConcurrentIRCompiler>(std::move(JTMB),
950 CacheManager.get());
951
952 auto TM = JTMB.createTargetMachine();
953 if (!TM)
954 return TM.takeError();
955
956 return std::make_unique<orc::TMOwningSimpleCompiler>(std::move(*TM),
957 CacheManager.get());
958 });
959 }
960
961 // Set up LLJIT platform.
962 LLJITPlatform P = Platform;
963 if (P == LLJITPlatform::DetectHost) {
964 if (JITLinker == JITLinkerKind::JITLink && !OrcRuntime.empty() &&
965 (TT->isOSBinFormatMachO() || TT->isOSBinFormatELF()))
966 P = LLJITPlatform::ORC;
967 else
968 P = LLJITPlatform::GenericIR;
969 }
970 switch (P) {
971 case LLJITPlatform::ORC:
972 Builder.setPlatformSetUp([](llvm::orc::LLJIT &J) -> llvm::Error {
973 J.setPlatformSupport(std::make_unique<ORCPlatformSupport>(J));
974 return Error::success();
975 });
976 break;
977 case LLJITPlatform::GenericIR:
978 // Nothing to do: LLJITBuilder will use this by default.
979 break;
980 case LLJITPlatform::Inactive:
981 Builder.setPlatformSetUp(orc::setUpInactivePlatform);
982 break;
983 default:
984 llvm_unreachable("Unrecognized platform value");
985 }
986
987 std::unique_ptr<orc::ExecutorProcessControl> EPC = nullptr;
988 if (JITLinker == JITLinkerKind::JITLink) {
989 EPC = ExitOnErr(orc::SelfExecutorProcessControl::Create(
990 std::make_shared<orc::SymbolStringPool>()));
991
992 Builder.setObjectLinkingLayerCreator([&EPC, &P](orc::ExecutionSession &ES,
993 const Triple &TT) {
994 auto L = std::make_unique<orc::ObjectLinkingLayer>(ES, EPC->getMemMgr());
995 if (P != LLJITPlatform::ORC) {
996 L->addPlugin(std::make_unique<orc::EHFrameRegistrationPlugin>(
997 ES, ExitOnErr(orc::EPCEHFrameRegistrar::Create(ES))));
998 L->addPlugin(std::make_unique<orc::DebugObjectManagerPlugin>(
999 ES, ExitOnErr(orc::createJITLoaderGDBRegistrar(ES))));
1000 }
1001 return L;
1002 });
1003 }
1004
1005 auto J = ExitOnErr(Builder.create());
1006
1007 auto *ObjLayer = &J->getObjLinkingLayer();
1008 if (auto *RTDyldObjLayer = dyn_cast<orc::RTDyldObjectLinkingLayer>(ObjLayer))
1009 RTDyldObjLayer->registerJITEventListener(
1010 *JITEventListener::createGDBRegistrationListener());
1011
1012 if (PerModuleLazy)
1013 J->setPartitionFunction(orc::CompileOnDemandLayer::compileWholeModule);
1014
1015 auto Dump = createDebugDumper();
1016
1017 J->getIRTransformLayer().setTransform(
1018 [&](orc::ThreadSafeModule TSM,
1019 const orc::MaterializationResponsibility &R) {
1020 TSM.withModuleDo([&](Module &M) {
1021 if (verifyModule(M, &dbgs())) {
1022 dbgs() << "Bad module: " << &M << "\n";
1023 exit(1);
1024 }
1025 Dump(M);
1026 });
1027 return TSM;
1028 });
1029
1030 orc::MangleAndInterner Mangle(J->getExecutionSession(), J->getDataLayout());
1031
1032 // Unless they've been explicitly disabled, make process symbols available to
1033 // JIT'd code.
1034 if (!NoProcessSymbols)
1035 J->getMainJITDylib().addGenerator(
1036 ExitOnErr(orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(
1037 J->getDataLayout().getGlobalPrefix(),
1038 [MainName = Mangle("main")](const orc::SymbolStringPtr &Name) {
1039 return Name != MainName;
1040 })));
1041
1042 if (GenerateBuiltinFunctions.size() > 0)
1043 J->getMainJITDylib().addGenerator(
1044 std::make_unique<LLIBuiltinFunctionGenerator>(GenerateBuiltinFunctions,
1045 Mangle));
1046
1047 if (P == LLJITPlatform::ORC) {
1048 if (auto *OLL = llvm::dyn_cast<llvm::orc::ObjectLinkingLayer>(ObjLayer)) {
1049 auto &ES = J->getExecutionSession();
1050 if (TT->isOSBinFormatMachO()) {
1051 if (auto P = llvm::orc::MachOPlatform::Create(
1052 ES, *OLL, J->getMainJITDylib(), OrcRuntime.c_str()))
1053 ES.setPlatform(std::move(*P));
1054 else
1055 ExitOnErr(P.takeError());
1056 } else if (TT->isOSBinFormatELF()) {
1057 if (auto P = llvm::orc::ELFNixPlatform::Create(
1058 ES, *OLL, J->getMainJITDylib(), OrcRuntime.c_str()))
1059 ES.setPlatform(std::move(*P));
1060 else
1061 ExitOnErr(P.takeError());
1062 } else {
1063 errs() << "No ORC platform support\n";
1064 exit(1);
1065 }
1066 } else {
1067 errs() << "ORC platform requires JITLink\n";
1068 exit(1);
1069 }
1070 }
1071
1072 // Regular modules are greedy: They materialize as a whole and trigger
1073 // materialization for all required symbols recursively. Lazy modules go
1074 // through partitioning and they replace outgoing calls with reexport stubs
1075 // that resolve on call-through.
1076 auto AddModule = [&](orc::JITDylib &JD, orc::ThreadSafeModule M) {
1077 return UseJITKind == JITKind::OrcLazy ? J->addLazyIRModule(JD, std::move(M))
1078 : J->addIRModule(JD, std::move(M));
1079 };
1080
1081 // Add the main module.
1082 ExitOnErr(AddModule(J->getMainJITDylib(), std::move(MainModule)));
1083
1084 // Create JITDylibs and add any extra modules.
1085 {
1086 // Create JITDylibs, keep a map from argument index to dylib. We will use
1087 // -extra-module argument indexes to determine what dylib to use for each
1088 // -extra-module.
1089 std::map<unsigned, orc::JITDylib *> IdxToDylib;
1090 IdxToDylib[0] = &J->getMainJITDylib();
1091 for (auto JDItr = JITDylibs.begin(), JDEnd = JITDylibs.end();
1092 JDItr != JDEnd; ++JDItr) {
1093 orc::JITDylib *JD = J->getJITDylibByName(*JDItr);
1094 if (!JD) {
1095 JD = &ExitOnErr(J->createJITDylib(*JDItr));
1096 J->getMainJITDylib().addToLinkOrder(*JD);
1097 JD->addToLinkOrder(J->getMainJITDylib());
1098 }
1099 IdxToDylib[JITDylibs.getPosition(JDItr - JITDylibs.begin())] = JD;
1100 }
1101
1102 for (auto EMItr = ExtraModules.begin(), EMEnd = ExtraModules.end();
1103 EMItr != EMEnd; ++EMItr) {
1104 auto M = ExitOnErr(loadModule(*EMItr, TSCtx));
1105
1106 auto EMIdx = ExtraModules.getPosition(EMItr - ExtraModules.begin());
1107 assert(EMIdx != 0 && "ExtraModule should have index > 0");
1108 auto JDItr = std::prev(IdxToDylib.lower_bound(EMIdx));
1109 auto &JD = *JDItr->second;
1110 ExitOnErr(AddModule(JD, std::move(M)));
1111 }
1112
1113 for (auto EAItr = ExtraArchives.begin(), EAEnd = ExtraArchives.end();
1114 EAItr != EAEnd; ++EAItr) {
1115 auto EAIdx = ExtraArchives.getPosition(EAItr - ExtraArchives.begin());
1116 assert(EAIdx != 0 && "ExtraArchive should have index > 0");
1117 auto JDItr = std::prev(IdxToDylib.lower_bound(EAIdx));
1118 auto &JD = *JDItr->second;
1119 JD.addGenerator(ExitOnErr(orc::StaticLibraryDefinitionGenerator::Load(
1120 J->getObjLinkingLayer(), EAItr->c_str(), *TT)));
1121 }
1122 }
1123
1124 // Add the objects.
1125 for (auto &ObjPath : ExtraObjects) {
1126 auto Obj = ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ObjPath)));
1127 ExitOnErr(J->addObjectFile(std::move(Obj)));
1128 }
1129
1130 // Run any static constructors.
1131 ExitOnErr(J->initialize(J->getMainJITDylib()));
1132
1133 // Run any -thread-entry points.
1134 std::vector<std::thread> AltEntryThreads;
1135 for (auto &ThreadEntryPoint : ThreadEntryPoints) {
1136 auto EntryPointSym = ExitOnErr(J->lookup(ThreadEntryPoint));
1137 typedef void (*EntryPointPtr)();
1138 auto EntryPoint = EntryPointSym.toPtr<EntryPointPtr>();
1139 AltEntryThreads.push_back(std::thread([EntryPoint]() { EntryPoint(); }));
1140 }
1141
1142 // Resolve and run the main function.
1143 auto MainAddr = ExitOnErr(J->lookup(EntryFunc));
1144 int Result;
1145
1146 if (EPC) {
1147 // ExecutorProcessControl-based execution with JITLink.
1148 Result = ExitOnErr(EPC->runAsMain(MainAddr, InputArgv));
1149 } else {
1150 // Manual in-process execution with RuntimeDyld.
1151 using MainFnTy = int(int, char *[]);
1152 auto MainFn = MainAddr.toPtr<MainFnTy *>();
1153 Result = orc::runAsMain(MainFn, InputArgv, StringRef(InputFile));
1154 }
1155
1156 // Wait for -entry-point threads.
1157 for (auto &AltEntryThread : AltEntryThreads)
1158 AltEntryThread.join();
1159
1160 // Run destructors.
1161 ExitOnErr(J->deinitialize(J->getMainJITDylib()));
1162
1163 return Result;
1164 }
1165
disallowOrcOptions()1166 void disallowOrcOptions() {
1167 // Make sure nobody used an orc-lazy specific option accidentally.
1168
1169 if (LazyJITCompileThreads != 0) {
1170 errs() << "-compile-threads requires -jit-kind=orc-lazy\n";
1171 exit(1);
1172 }
1173
1174 if (!ThreadEntryPoints.empty()) {
1175 errs() << "-thread-entry requires -jit-kind=orc-lazy\n";
1176 exit(1);
1177 }
1178
1179 if (PerModuleLazy) {
1180 errs() << "-per-module-lazy requires -jit-kind=orc-lazy\n";
1181 exit(1);
1182 }
1183 }
1184
launchRemote()1185 Expected<std::unique_ptr<orc::ExecutorProcessControl>> launchRemote() {
1186 #ifndef LLVM_ON_UNIX
1187 llvm_unreachable("launchRemote not supported on non-Unix platforms");
1188 #else
1189 int PipeFD[2][2];
1190 pid_t ChildPID;
1191
1192 // Create two pipes.
1193 if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0)
1194 perror("Error creating pipe: ");
1195
1196 ChildPID = fork();
1197
1198 if (ChildPID == 0) {
1199 // In the child...
1200
1201 // Close the parent ends of the pipes
1202 close(PipeFD[0][1]);
1203 close(PipeFD[1][0]);
1204
1205
1206 // Execute the child process.
1207 std::unique_ptr<char[]> ChildPath, ChildIn, ChildOut;
1208 {
1209 ChildPath.reset(new char[ChildExecPath.size() + 1]);
1210 std::copy(ChildExecPath.begin(), ChildExecPath.end(), &ChildPath[0]);
1211 ChildPath[ChildExecPath.size()] = '\0';
1212 std::string ChildInStr = utostr(PipeFD[0][0]);
1213 ChildIn.reset(new char[ChildInStr.size() + 1]);
1214 std::copy(ChildInStr.begin(), ChildInStr.end(), &ChildIn[0]);
1215 ChildIn[ChildInStr.size()] = '\0';
1216 std::string ChildOutStr = utostr(PipeFD[1][1]);
1217 ChildOut.reset(new char[ChildOutStr.size() + 1]);
1218 std::copy(ChildOutStr.begin(), ChildOutStr.end(), &ChildOut[0]);
1219 ChildOut[ChildOutStr.size()] = '\0';
1220 }
1221
1222 char * const args[] = { &ChildPath[0], &ChildIn[0], &ChildOut[0], nullptr };
1223 int rc = execv(ChildExecPath.c_str(), args);
1224 if (rc != 0)
1225 perror("Error executing child process: ");
1226 llvm_unreachable("Error executing child process");
1227 }
1228 // else we're the parent...
1229
1230 // Close the child ends of the pipes
1231 close(PipeFD[0][0]);
1232 close(PipeFD[1][1]);
1233
1234 // Return a SimpleRemoteEPC instance connected to our end of the pipes.
1235 return orc::SimpleRemoteEPC::Create<orc::FDSimpleRemoteEPCTransport>(
1236 std::make_unique<llvm::orc::InPlaceTaskDispatcher>(),
1237 llvm::orc::SimpleRemoteEPC::Setup(), PipeFD[1][0], PipeFD[0][1]);
1238 #endif
1239 }
1240