]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/tools/lli/lli.cpp
MFC r355940:
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / tools / lli / lli.cpp
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 "RemoteJITUtils.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/Bitcode/BitcodeReader.h"
19 #include "llvm/CodeGen/CommandFlags.inc"
20 #include "llvm/CodeGen/LinkAllCodegenComponents.h"
21 #include "llvm/Config/llvm-config.h"
22 #include "llvm/ExecutionEngine/GenericValue.h"
23 #include "llvm/ExecutionEngine/Interpreter.h"
24 #include "llvm/ExecutionEngine/JITEventListener.h"
25 #include "llvm/ExecutionEngine/MCJIT.h"
26 #include "llvm/ExecutionEngine/ObjectCache.h"
27 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
28 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
29 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
30 #include "llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h"
31 #include "llvm/ExecutionEngine/OrcMCJITReplacement.h"
32 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
33 #include "llvm/IR/IRBuilder.h"
34 #include "llvm/IR/LLVMContext.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/IR/Type.h"
37 #include "llvm/IR/Verifier.h"
38 #include "llvm/IRReader/IRReader.h"
39 #include "llvm/Object/Archive.h"
40 #include "llvm/Object/ObjectFile.h"
41 #include "llvm/Support/CommandLine.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Support/DynamicLibrary.h"
44 #include "llvm/Support/Format.h"
45 #include "llvm/Support/InitLLVM.h"
46 #include "llvm/Support/ManagedStatic.h"
47 #include "llvm/Support/MathExtras.h"
48 #include "llvm/Support/Memory.h"
49 #include "llvm/Support/MemoryBuffer.h"
50 #include "llvm/Support/Path.h"
51 #include "llvm/Support/PluginLoader.h"
52 #include "llvm/Support/Process.h"
53 #include "llvm/Support/Program.h"
54 #include "llvm/Support/SourceMgr.h"
55 #include "llvm/Support/TargetSelect.h"
56 #include "llvm/Support/WithColor.h"
57 #include "llvm/Support/raw_ostream.h"
58 #include "llvm/Transforms/Instrumentation.h"
59 #include <cerrno>
60
61 #ifdef __CYGWIN__
62 #include <cygwin/version.h>
63 #if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
64 #define DO_NOTHING_ATEXIT 1
65 #endif
66 #endif
67
68 using namespace llvm;
69
70 #define DEBUG_TYPE "lli"
71
72 namespace {
73
74   enum class JITKind { MCJIT, OrcMCJITReplacement, OrcLazy };
75
76   cl::opt<std::string>
77   InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-"));
78
79   cl::list<std::string>
80   InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
81
82   cl::opt<bool> ForceInterpreter("force-interpreter",
83                                  cl::desc("Force interpretation: disable JIT"),
84                                  cl::init(false));
85
86   cl::opt<JITKind> UseJITKind(
87       "jit-kind", cl::desc("Choose underlying JIT kind."),
88       cl::init(JITKind::MCJIT),
89       cl::values(clEnumValN(JITKind::MCJIT, "mcjit", "MCJIT"),
90                  clEnumValN(JITKind::OrcMCJITReplacement, "orc-mcjit",
91                             "Orc-based MCJIT replacement "
92                             "(deprecated)"),
93                  clEnumValN(JITKind::OrcLazy, "orc-lazy",
94                             "Orc-based lazy JIT.")));
95
96   cl::opt<unsigned>
97   LazyJITCompileThreads("compile-threads",
98                         cl::desc("Choose the number of compile threads "
99                                  "(jit-kind=orc-lazy only)"),
100                         cl::init(0));
101
102   cl::list<std::string>
103   ThreadEntryPoints("thread-entry",
104                     cl::desc("calls the given entry-point on a new thread "
105                              "(jit-kind=orc-lazy only)"));
106
107   cl::opt<bool> PerModuleLazy(
108       "per-module-lazy",
109       cl::desc("Performs lazy compilation on whole module boundaries "
110                "rather than individual functions"),
111       cl::init(false));
112
113   cl::list<std::string>
114       JITDylibs("jd",
115                 cl::desc("Specifies the JITDylib to be used for any subsequent "
116                          "-extra-module arguments."));
117
118   // The MCJIT supports building for a target address space separate from
119   // the JIT compilation process. Use a forked process and a copying
120   // memory manager with IPC to execute using this functionality.
121   cl::opt<bool> RemoteMCJIT("remote-mcjit",
122     cl::desc("Execute MCJIT'ed code in a separate process."),
123     cl::init(false));
124
125   // Manually specify the child process for remote execution. This overrides
126   // the simulated remote execution that allocates address space for child
127   // execution. The child process will be executed and will communicate with
128   // lli via stdin/stdout pipes.
129   cl::opt<std::string>
130   ChildExecPath("mcjit-remote-process",
131                 cl::desc("Specify the filename of the process to launch "
132                          "for remote MCJIT execution.  If none is specified,"
133                          "\n\tremote execution will be simulated in-process."),
134                 cl::value_desc("filename"), cl::init(""));
135
136   // Determine optimization level.
137   cl::opt<char>
138   OptLevel("O",
139            cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
140                     "(default = '-O2')"),
141            cl::Prefix,
142            cl::ZeroOrMore,
143            cl::init(' '));
144
145   cl::opt<std::string>
146   TargetTriple("mtriple", cl::desc("Override target triple for module"));
147
148   cl::opt<std::string>
149   EntryFunc("entry-function",
150             cl::desc("Specify the entry function (default = 'main') "
151                      "of the executable"),
152             cl::value_desc("function"),
153             cl::init("main"));
154
155   cl::list<std::string>
156   ExtraModules("extra-module",
157          cl::desc("Extra modules to be loaded"),
158          cl::value_desc("input bitcode"));
159
160   cl::list<std::string>
161   ExtraObjects("extra-object",
162          cl::desc("Extra object files to be loaded"),
163          cl::value_desc("input object"));
164
165   cl::list<std::string>
166   ExtraArchives("extra-archive",
167          cl::desc("Extra archive files to be loaded"),
168          cl::value_desc("input archive"));
169
170   cl::opt<bool>
171   EnableCacheManager("enable-cache-manager",
172         cl::desc("Use cache manager to save/load modules"),
173         cl::init(false));
174
175   cl::opt<std::string>
176   ObjectCacheDir("object-cache-dir",
177                   cl::desc("Directory to store cached object files "
178                            "(must be user writable)"),
179                   cl::init(""));
180
181   cl::opt<std::string>
182   FakeArgv0("fake-argv0",
183             cl::desc("Override the 'argv[0]' value passed into the executing"
184                      " program"), cl::value_desc("executable"));
185
186   cl::opt<bool>
187   DisableCoreFiles("disable-core-files", cl::Hidden,
188                    cl::desc("Disable emission of core files if possible"));
189
190   cl::opt<bool>
191   NoLazyCompilation("disable-lazy-compilation",
192                   cl::desc("Disable JIT lazy compilation"),
193                   cl::init(false));
194
195   cl::opt<bool>
196   GenerateSoftFloatCalls("soft-float",
197     cl::desc("Generate software floating point library calls"),
198     cl::init(false));
199
200   enum class DumpKind {
201     NoDump,
202     DumpFuncsToStdOut,
203     DumpModsToStdOut,
204     DumpModsToDisk
205   };
206
207   cl::opt<DumpKind> OrcDumpKind(
208       "orc-lazy-debug", cl::desc("Debug dumping for the orc-lazy JIT."),
209       cl::init(DumpKind::NoDump),
210       cl::values(clEnumValN(DumpKind::NoDump, "no-dump",
211                             "Don't dump anything."),
212                  clEnumValN(DumpKind::DumpFuncsToStdOut, "funcs-to-stdout",
213                             "Dump function names to stdout."),
214                  clEnumValN(DumpKind::DumpModsToStdOut, "mods-to-stdout",
215                             "Dump modules to stdout."),
216                  clEnumValN(DumpKind::DumpModsToDisk, "mods-to-disk",
217                             "Dump modules to the current "
218                             "working directory. (WARNING: "
219                             "will overwrite existing files).")),
220       cl::Hidden);
221
222   ExitOnError ExitOnErr;
223 }
224
225 //===----------------------------------------------------------------------===//
226 // Object cache
227 //
228 // This object cache implementation writes cached objects to disk to the
229 // directory specified by CacheDir, using a filename provided in the module
230 // descriptor. The cache tries to load a saved object using that path if the
231 // file exists. CacheDir defaults to "", in which case objects are cached
232 // alongside their originating bitcodes.
233 //
234 class LLIObjectCache : public ObjectCache {
235 public:
236   LLIObjectCache(const std::string& CacheDir) : CacheDir(CacheDir) {
237     // Add trailing '/' to cache dir if necessary.
238     if (!this->CacheDir.empty() &&
239         this->CacheDir[this->CacheDir.size() - 1] != '/')
240       this->CacheDir += '/';
241   }
242   ~LLIObjectCache() override {}
243
244   void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override {
245     const std::string &ModuleID = M->getModuleIdentifier();
246     std::string CacheName;
247     if (!getCacheFilename(ModuleID, CacheName))
248       return;
249     if (!CacheDir.empty()) { // Create user-defined cache dir.
250       SmallString<128> dir(sys::path::parent_path(CacheName));
251       sys::fs::create_directories(Twine(dir));
252     }
253     std::error_code EC;
254     raw_fd_ostream outfile(CacheName, EC, sys::fs::F_None);
255     outfile.write(Obj.getBufferStart(), Obj.getBufferSize());
256     outfile.close();
257   }
258
259   std::unique_ptr<MemoryBuffer> getObject(const Module* M) override {
260     const std::string &ModuleID = M->getModuleIdentifier();
261     std::string CacheName;
262     if (!getCacheFilename(ModuleID, CacheName))
263       return nullptr;
264     // Load the object from the cache filename
265     ErrorOr<std::unique_ptr<MemoryBuffer>> IRObjectBuffer =
266         MemoryBuffer::getFile(CacheName, -1, false);
267     // If the file isn't there, that's OK.
268     if (!IRObjectBuffer)
269       return nullptr;
270     // MCJIT will want to write into this buffer, and we don't want that
271     // because the file has probably just been mmapped.  Instead we make
272     // a copy.  The filed-based buffer will be released when it goes
273     // out of scope.
274     return MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer());
275   }
276
277 private:
278   std::string CacheDir;
279
280   bool getCacheFilename(const std::string &ModID, std::string &CacheName) {
281     std::string Prefix("file:");
282     size_t PrefixLength = Prefix.length();
283     if (ModID.substr(0, PrefixLength) != Prefix)
284       return false;
285         std::string CacheSubdir = ModID.substr(PrefixLength);
286 #if defined(_WIN32)
287         // Transform "X:\foo" => "/X\foo" for convenience.
288         if (isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') {
289           CacheSubdir[1] = CacheSubdir[0];
290           CacheSubdir[0] = '/';
291         }
292 #endif
293     CacheName = CacheDir + CacheSubdir;
294     size_t pos = CacheName.rfind('.');
295     CacheName.replace(pos, CacheName.length() - pos, ".o");
296     return true;
297   }
298 };
299
300 // On Mingw and Cygwin, an external symbol named '__main' is called from the
301 // generated 'main' function to allow static initialization.  To avoid linking
302 // problems with remote targets (because lli's remote target support does not
303 // currently handle external linking) we add a secondary module which defines
304 // an empty '__main' function.
305 static void addCygMingExtraModule(ExecutionEngine &EE, LLVMContext &Context,
306                                   StringRef TargetTripleStr) {
307   IRBuilder<> Builder(Context);
308   Triple TargetTriple(TargetTripleStr);
309
310   // Create a new module.
311   std::unique_ptr<Module> M = make_unique<Module>("CygMingHelper", Context);
312   M->setTargetTriple(TargetTripleStr);
313
314   // Create an empty function named "__main".
315   Type *ReturnTy;
316   if (TargetTriple.isArch64Bit())
317     ReturnTy = Type::getInt64Ty(Context);
318   else
319     ReturnTy = Type::getInt32Ty(Context);
320   Function *Result =
321       Function::Create(FunctionType::get(ReturnTy, {}, false),
322                        GlobalValue::ExternalLinkage, "__main", M.get());
323
324   BasicBlock *BB = BasicBlock::Create(Context, "__main", Result);
325   Builder.SetInsertPoint(BB);
326   Value *ReturnVal = ConstantInt::get(ReturnTy, 0);
327   Builder.CreateRet(ReturnVal);
328
329   // Add this new module to the ExecutionEngine.
330   EE.addModule(std::move(M));
331 }
332
333 CodeGenOpt::Level getOptLevel() {
334   switch (OptLevel) {
335   default:
336     WithColor::error(errs(), "lli") << "invalid optimization level.\n";
337     exit(1);
338   case '0': return CodeGenOpt::None;
339   case '1': return CodeGenOpt::Less;
340   case ' ':
341   case '2': return CodeGenOpt::Default;
342   case '3': return CodeGenOpt::Aggressive;
343   }
344   llvm_unreachable("Unrecognized opt level.");
345 }
346
347 LLVM_ATTRIBUTE_NORETURN
348 static void reportError(SMDiagnostic Err, const char *ProgName) {
349   Err.print(ProgName, errs());
350   exit(1);
351 }
352
353 int runOrcLazyJIT(const char *ProgName);
354 void disallowOrcOptions();
355
356 //===----------------------------------------------------------------------===//
357 // main Driver function
358 //
359 int main(int argc, char **argv, char * const *envp) {
360   InitLLVM X(argc, argv);
361
362   if (argc > 1)
363     ExitOnErr.setBanner(std::string(argv[0]) + ": ");
364
365   // If we have a native target, initialize it to ensure it is linked in and
366   // usable by the JIT.
367   InitializeNativeTarget();
368   InitializeNativeTargetAsmPrinter();
369   InitializeNativeTargetAsmParser();
370
371   cl::ParseCommandLineOptions(argc, argv,
372                               "llvm interpreter & dynamic compiler\n");
373
374   // If the user doesn't want core files, disable them.
375   if (DisableCoreFiles)
376     sys::Process::PreventCoreFiles();
377
378   if (UseJITKind == JITKind::OrcLazy)
379     return runOrcLazyJIT(argv[0]);
380   else
381     disallowOrcOptions();
382
383   LLVMContext Context;
384
385   // Load the bitcode...
386   SMDiagnostic Err;
387   std::unique_ptr<Module> Owner = parseIRFile(InputFile, Err, Context);
388   Module *Mod = Owner.get();
389   if (!Mod)
390     reportError(Err, argv[0]);
391
392   if (EnableCacheManager) {
393     std::string CacheName("file:");
394     CacheName.append(InputFile);
395     Mod->setModuleIdentifier(CacheName);
396   }
397
398   // If not jitting lazily, load the whole bitcode file eagerly too.
399   if (NoLazyCompilation) {
400     // Use *argv instead of argv[0] to work around a wrong GCC warning.
401     ExitOnError ExitOnErr(std::string(*argv) +
402                           ": bitcode didn't read correctly: ");
403     ExitOnErr(Mod->materializeAll());
404   }
405
406   std::string ErrorMsg;
407   EngineBuilder builder(std::move(Owner));
408   builder.setMArch(MArch);
409   builder.setMCPU(getCPUStr());
410   builder.setMAttrs(getFeatureList());
411   if (RelocModel.getNumOccurrences())
412     builder.setRelocationModel(RelocModel);
413   if (CMModel.getNumOccurrences())
414     builder.setCodeModel(CMModel);
415   builder.setErrorStr(&ErrorMsg);
416   builder.setEngineKind(ForceInterpreter
417                         ? EngineKind::Interpreter
418                         : EngineKind::JIT);
419   builder.setUseOrcMCJITReplacement(AcknowledgeORCv1Deprecation,
420                                     UseJITKind == JITKind::OrcMCJITReplacement);
421
422   // If we are supposed to override the target triple, do so now.
423   if (!TargetTriple.empty())
424     Mod->setTargetTriple(Triple::normalize(TargetTriple));
425
426   // Enable MCJIT if desired.
427   RTDyldMemoryManager *RTDyldMM = nullptr;
428   if (!ForceInterpreter) {
429     if (RemoteMCJIT)
430       RTDyldMM = new ForwardingMemoryManager();
431     else
432       RTDyldMM = new SectionMemoryManager();
433
434     // Deliberately construct a temp std::unique_ptr to pass in. Do not null out
435     // RTDyldMM: We still use it below, even though we don't own it.
436     builder.setMCJITMemoryManager(
437       std::unique_ptr<RTDyldMemoryManager>(RTDyldMM));
438   } else if (RemoteMCJIT) {
439     WithColor::error(errs(), argv[0])
440         << "remote process execution does not work with the interpreter.\n";
441     exit(1);
442   }
443
444   builder.setOptLevel(getOptLevel());
445
446   TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
447   if (FloatABIForCalls != FloatABI::Default)
448     Options.FloatABIType = FloatABIForCalls;
449
450   builder.setTargetOptions(Options);
451
452   std::unique_ptr<ExecutionEngine> EE(builder.create());
453   if (!EE) {
454     if (!ErrorMsg.empty())
455       WithColor::error(errs(), argv[0])
456           << "error creating EE: " << ErrorMsg << "\n";
457     else
458       WithColor::error(errs(), argv[0]) << "unknown error creating EE!\n";
459     exit(1);
460   }
461
462   std::unique_ptr<LLIObjectCache> CacheManager;
463   if (EnableCacheManager) {
464     CacheManager.reset(new LLIObjectCache(ObjectCacheDir));
465     EE->setObjectCache(CacheManager.get());
466   }
467
468   // Load any additional modules specified on the command line.
469   for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) {
470     std::unique_ptr<Module> XMod = parseIRFile(ExtraModules[i], Err, Context);
471     if (!XMod)
472       reportError(Err, argv[0]);
473     if (EnableCacheManager) {
474       std::string CacheName("file:");
475       CacheName.append(ExtraModules[i]);
476       XMod->setModuleIdentifier(CacheName);
477     }
478     EE->addModule(std::move(XMod));
479   }
480
481   for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) {
482     Expected<object::OwningBinary<object::ObjectFile>> Obj =
483         object::ObjectFile::createObjectFile(ExtraObjects[i]);
484     if (!Obj) {
485       // TODO: Actually report errors helpfully.
486       consumeError(Obj.takeError());
487       reportError(Err, argv[0]);
488     }
489     object::OwningBinary<object::ObjectFile> &O = Obj.get();
490     EE->addObjectFile(std::move(O));
491   }
492
493   for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) {
494     ErrorOr<std::unique_ptr<MemoryBuffer>> ArBufOrErr =
495         MemoryBuffer::getFileOrSTDIN(ExtraArchives[i]);
496     if (!ArBufOrErr)
497       reportError(Err, argv[0]);
498     std::unique_ptr<MemoryBuffer> &ArBuf = ArBufOrErr.get();
499
500     Expected<std::unique_ptr<object::Archive>> ArOrErr =
501         object::Archive::create(ArBuf->getMemBufferRef());
502     if (!ArOrErr) {
503       std::string Buf;
504       raw_string_ostream OS(Buf);
505       logAllUnhandledErrors(ArOrErr.takeError(), OS);
506       OS.flush();
507       errs() << Buf;
508       exit(1);
509     }
510     std::unique_ptr<object::Archive> &Ar = ArOrErr.get();
511
512     object::OwningBinary<object::Archive> OB(std::move(Ar), std::move(ArBuf));
513
514     EE->addArchive(std::move(OB));
515   }
516
517   // If the target is Cygwin/MingW and we are generating remote code, we
518   // need an extra module to help out with linking.
519   if (RemoteMCJIT && Triple(Mod->getTargetTriple()).isOSCygMing()) {
520     addCygMingExtraModule(*EE, Context, Mod->getTargetTriple());
521   }
522
523   // The following functions have no effect if their respective profiling
524   // support wasn't enabled in the build configuration.
525   EE->RegisterJITEventListener(
526                 JITEventListener::createOProfileJITEventListener());
527   EE->RegisterJITEventListener(
528                 JITEventListener::createIntelJITEventListener());
529   if (!RemoteMCJIT)
530     EE->RegisterJITEventListener(
531                 JITEventListener::createPerfJITEventListener());
532
533   if (!NoLazyCompilation && RemoteMCJIT) {
534     WithColor::warning(errs(), argv[0])
535         << "remote mcjit does not support lazy compilation\n";
536     NoLazyCompilation = true;
537   }
538   EE->DisableLazyCompilation(NoLazyCompilation);
539
540   // If the user specifically requested an argv[0] to pass into the program,
541   // do it now.
542   if (!FakeArgv0.empty()) {
543     InputFile = static_cast<std::string>(FakeArgv0);
544   } else {
545     // Otherwise, if there is a .bc suffix on the executable strip it off, it
546     // might confuse the program.
547     if (StringRef(InputFile).endswith(".bc"))
548       InputFile.erase(InputFile.length() - 3);
549   }
550
551   // Add the module's name to the start of the vector of arguments to main().
552   InputArgv.insert(InputArgv.begin(), InputFile);
553
554   // Call the main function from M as if its signature were:
555   //   int main (int argc, char **argv, const char **envp)
556   // using the contents of Args to determine argc & argv, and the contents of
557   // EnvVars to determine envp.
558   //
559   Function *EntryFn = Mod->getFunction(EntryFunc);
560   if (!EntryFn) {
561     WithColor::error(errs(), argv[0])
562         << '\'' << EntryFunc << "\' function not found in module.\n";
563     return -1;
564   }
565
566   // Reset errno to zero on entry to main.
567   errno = 0;
568
569   int Result = -1;
570
571   // Sanity check use of remote-jit: LLI currently only supports use of the
572   // remote JIT on Unix platforms.
573   if (RemoteMCJIT) {
574 #ifndef LLVM_ON_UNIX
575     WithColor::warning(errs(), argv[0])
576         << "host does not support external remote targets.\n";
577     WithColor::note() << "defaulting to local execution\n";
578     return -1;
579 #else
580     if (ChildExecPath.empty()) {
581       WithColor::error(errs(), argv[0])
582           << "-remote-mcjit requires -mcjit-remote-process.\n";
583       exit(1);
584     } else if (!sys::fs::can_execute(ChildExecPath)) {
585       WithColor::error(errs(), argv[0])
586           << "unable to find usable child executable: '" << ChildExecPath
587           << "'\n";
588       return -1;
589     }
590 #endif
591   }
592
593   if (!RemoteMCJIT) {
594     // If the program doesn't explicitly call exit, we will need the Exit
595     // function later on to make an explicit call, so get the function now.
596     FunctionCallee Exit = Mod->getOrInsertFunction(
597         "exit", Type::getVoidTy(Context), Type::getInt32Ty(Context));
598
599     // Run static constructors.
600     if (!ForceInterpreter) {
601       // Give MCJIT a chance to apply relocations and set page permissions.
602       EE->finalizeObject();
603     }
604     EE->runStaticConstructorsDestructors(false);
605
606     // Trigger compilation separately so code regions that need to be
607     // invalidated will be known.
608     (void)EE->getPointerToFunction(EntryFn);
609     // Clear instruction cache before code will be executed.
610     if (RTDyldMM)
611       static_cast<SectionMemoryManager*>(RTDyldMM)->invalidateInstructionCache();
612
613     // Run main.
614     Result = EE->runFunctionAsMain(EntryFn, InputArgv, envp);
615
616     // Run static destructors.
617     EE->runStaticConstructorsDestructors(true);
618
619     // If the program didn't call exit explicitly, we should call it now.
620     // This ensures that any atexit handlers get called correctly.
621     if (Function *ExitF =
622             dyn_cast<Function>(Exit.getCallee()->stripPointerCasts())) {
623       if (ExitF->getFunctionType() == Exit.getFunctionType()) {
624         std::vector<GenericValue> Args;
625         GenericValue ResultGV;
626         ResultGV.IntVal = APInt(32, Result);
627         Args.push_back(ResultGV);
628         EE->runFunction(ExitF, Args);
629         WithColor::error(errs(), argv[0])
630             << "exit(" << Result << ") returned!\n";
631         abort();
632       }
633     }
634     WithColor::error(errs(), argv[0]) << "exit defined with wrong prototype!\n";
635     abort();
636   } else {
637     // else == "if (RemoteMCJIT)"
638
639     // Remote target MCJIT doesn't (yet) support static constructors. No reason
640     // it couldn't. This is a limitation of the LLI implementation, not the
641     // MCJIT itself. FIXME.
642
643     // Lanch the remote process and get a channel to it.
644     std::unique_ptr<FDRawChannel> C = launchRemote();
645     if (!C) {
646       WithColor::error(errs(), argv[0]) << "failed to launch remote JIT.\n";
647       exit(1);
648     }
649
650     // Create a remote target client running over the channel.
651     llvm::orc::ExecutionSession ES;
652     ES.setErrorReporter([&](Error Err) { ExitOnErr(std::move(Err)); });
653     typedef orc::remote::OrcRemoteTargetClient MyRemote;
654     auto R = ExitOnErr(MyRemote::Create(*C, ES));
655
656     // Create a remote memory manager.
657     auto RemoteMM = ExitOnErr(R->createRemoteMemoryManager());
658
659     // Forward MCJIT's memory manager calls to the remote memory manager.
660     static_cast<ForwardingMemoryManager*>(RTDyldMM)->setMemMgr(
661       std::move(RemoteMM));
662
663     // Forward MCJIT's symbol resolution calls to the remote.
664     static_cast<ForwardingMemoryManager *>(RTDyldMM)->setResolver(
665         orc::createLambdaResolver(
666             AcknowledgeORCv1Deprecation,
667             [](const std::string &Name) { return nullptr; },
668             [&](const std::string &Name) {
669               if (auto Addr = ExitOnErr(R->getSymbolAddress(Name)))
670                 return JITSymbol(Addr, JITSymbolFlags::Exported);
671               return JITSymbol(nullptr);
672             }));
673
674     // Grab the target address of the JIT'd main function on the remote and call
675     // it.
676     // FIXME: argv and envp handling.
677     JITTargetAddress Entry = EE->getFunctionAddress(EntryFn->getName().str());
678     EE->finalizeObject();
679     LLVM_DEBUG(dbgs() << "Executing '" << EntryFn->getName() << "' at 0x"
680                       << format("%llx", Entry) << "\n");
681     Result = ExitOnErr(R->callIntVoid(Entry));
682
683     // Like static constructors, the remote target MCJIT support doesn't handle
684     // this yet. It could. FIXME.
685
686     // Delete the EE - we need to tear it down *before* we terminate the session
687     // with the remote, otherwise it'll crash when it tries to release resources
688     // on a remote that has already been disconnected.
689     EE.reset();
690
691     // Signal the remote target that we're done JITing.
692     ExitOnErr(R->terminateSession());
693   }
694
695   return Result;
696 }
697
698 static orc::IRTransformLayer::TransformFunction createDebugDumper() {
699   switch (OrcDumpKind) {
700   case DumpKind::NoDump:
701     return [](orc::ThreadSafeModule TSM,
702               const orc::MaterializationResponsibility &R) { return TSM; };
703
704   case DumpKind::DumpFuncsToStdOut:
705     return [](orc::ThreadSafeModule TSM,
706               const orc::MaterializationResponsibility &R) {
707       printf("[ ");
708
709       for (const auto &F : *TSM.getModule()) {
710         if (F.isDeclaration())
711           continue;
712
713         if (F.hasName()) {
714           std::string Name(F.getName());
715           printf("%s ", Name.c_str());
716         } else
717           printf("<anon> ");
718       }
719
720       printf("]\n");
721       return TSM;
722     };
723
724   case DumpKind::DumpModsToStdOut:
725     return [](orc::ThreadSafeModule TSM,
726               const orc::MaterializationResponsibility &R) {
727       outs() << "----- Module Start -----\n"
728              << *TSM.getModule() << "----- Module End -----\n";
729
730       return TSM;
731     };
732
733   case DumpKind::DumpModsToDisk:
734     return [](orc::ThreadSafeModule TSM,
735               const orc::MaterializationResponsibility &R) {
736       std::error_code EC;
737       raw_fd_ostream Out(TSM.getModule()->getModuleIdentifier() + ".ll", EC,
738                          sys::fs::F_Text);
739       if (EC) {
740         errs() << "Couldn't open " << TSM.getModule()->getModuleIdentifier()
741                << " for dumping.\nError:" << EC.message() << "\n";
742         exit(1);
743       }
744       Out << *TSM.getModule();
745       return TSM;
746     };
747   }
748   llvm_unreachable("Unknown DumpKind");
749 }
750
751 static void exitOnLazyCallThroughFailure() { exit(1); }
752
753 int runOrcLazyJIT(const char *ProgName) {
754   // Start setting up the JIT environment.
755
756   // Parse the main module.
757   orc::ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>());
758   SMDiagnostic Err;
759   auto MainModule = orc::ThreadSafeModule(
760       parseIRFile(InputFile, Err, *TSCtx.getContext()), TSCtx);
761   if (!MainModule)
762     reportError(Err, ProgName);
763
764   const auto &TT = MainModule.getModule()->getTargetTriple();
765   orc::LLLazyJITBuilder Builder;
766
767   Builder.setJITTargetMachineBuilder(
768       TT.empty() ? ExitOnErr(orc::JITTargetMachineBuilder::detectHost())
769                  : orc::JITTargetMachineBuilder(Triple(TT)));
770
771   if (!MArch.empty())
772     Builder.getJITTargetMachineBuilder()->getTargetTriple().setArchName(MArch);
773
774   Builder.getJITTargetMachineBuilder()
775       ->setCPU(getCPUStr())
776       .addFeatures(getFeatureList())
777       .setRelocationModel(RelocModel.getNumOccurrences()
778                               ? Optional<Reloc::Model>(RelocModel)
779                               : None)
780       .setCodeModel(CMModel.getNumOccurrences()
781                         ? Optional<CodeModel::Model>(CMModel)
782                         : None);
783
784   Builder.setLazyCompileFailureAddr(
785       pointerToJITTargetAddress(exitOnLazyCallThroughFailure));
786   Builder.setNumCompileThreads(LazyJITCompileThreads);
787
788   auto J = ExitOnErr(Builder.create());
789
790   if (PerModuleLazy)
791     J->setPartitionFunction(orc::CompileOnDemandLayer::compileWholeModule);
792
793   auto Dump = createDebugDumper();
794
795   J->setLazyCompileTransform([&](orc::ThreadSafeModule TSM,
796                                  const orc::MaterializationResponsibility &R) {
797     if (verifyModule(*TSM.getModule(), &dbgs())) {
798       dbgs() << "Bad module: " << *TSM.getModule() << "\n";
799       exit(1);
800     }
801     return Dump(std::move(TSM), R);
802   });
803   J->getMainJITDylib().setGenerator(
804       ExitOnErr(orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(
805           J->getDataLayout().getGlobalPrefix())));
806
807   orc::MangleAndInterner Mangle(J->getExecutionSession(), J->getDataLayout());
808   orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides;
809   ExitOnErr(CXXRuntimeOverrides.enable(J->getMainJITDylib(), Mangle));
810
811   // Add the main module.
812   ExitOnErr(J->addLazyIRModule(std::move(MainModule)));
813
814   // Create JITDylibs and add any extra modules.
815   {
816     // Create JITDylibs, keep a map from argument index to dylib. We will use
817     // -extra-module argument indexes to determine what dylib to use for each
818     // -extra-module.
819     std::map<unsigned, orc::JITDylib *> IdxToDylib;
820     IdxToDylib[0] = &J->getMainJITDylib();
821     for (auto JDItr = JITDylibs.begin(), JDEnd = JITDylibs.end();
822          JDItr != JDEnd; ++JDItr) {
823       orc::JITDylib *JD = J->getJITDylibByName(*JDItr);
824       if (!JD)
825         JD = &J->createJITDylib(*JDItr);
826       IdxToDylib[JITDylibs.getPosition(JDItr - JITDylibs.begin())] = JD;
827     }
828
829     for (auto EMItr = ExtraModules.begin(), EMEnd = ExtraModules.end();
830          EMItr != EMEnd; ++EMItr) {
831       auto M = parseIRFile(*EMItr, Err, *TSCtx.getContext());
832       if (!M)
833         reportError(Err, ProgName);
834
835       auto EMIdx = ExtraModules.getPosition(EMItr - ExtraModules.begin());
836       assert(EMIdx != 0 && "ExtraModule should have index > 0");
837       auto JDItr = std::prev(IdxToDylib.lower_bound(EMIdx));
838       auto &JD = *JDItr->second;
839       ExitOnErr(
840           J->addLazyIRModule(JD, orc::ThreadSafeModule(std::move(M), TSCtx)));
841     }
842   }
843
844   // Add the objects.
845   for (auto &ObjPath : ExtraObjects) {
846     auto Obj = ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ObjPath)));
847     ExitOnErr(J->addObjectFile(std::move(Obj)));
848   }
849
850   // Generate a argument string.
851   std::vector<std::string> Args;
852   Args.push_back(InputFile);
853   for (auto &Arg : InputArgv)
854     Args.push_back(Arg);
855
856   // Run any static constructors.
857   ExitOnErr(J->runConstructors());
858
859   // Run any -thread-entry points.
860   std::vector<std::thread> AltEntryThreads;
861   for (auto &ThreadEntryPoint : ThreadEntryPoints) {
862     auto EntryPointSym = ExitOnErr(J->lookup(ThreadEntryPoint));
863     typedef void (*EntryPointPtr)();
864     auto EntryPoint =
865       reinterpret_cast<EntryPointPtr>(static_cast<uintptr_t>(EntryPointSym.getAddress()));
866     AltEntryThreads.push_back(std::thread([EntryPoint]() { EntryPoint(); }));
867   }
868
869   // Run main.
870   auto MainSym = ExitOnErr(J->lookup("main"));
871   typedef int (*MainFnPtr)(int, const char *[]);
872   std::vector<const char *> ArgV;
873   for (auto &Arg : Args)
874     ArgV.push_back(Arg.c_str());
875   ArgV.push_back(nullptr);
876
877   int ArgC = ArgV.size() - 1;
878   auto Main =
879       reinterpret_cast<MainFnPtr>(static_cast<uintptr_t>(MainSym.getAddress()));
880   auto Result = Main(ArgC, (const char **)ArgV.data());
881
882   // Wait for -entry-point threads.
883   for (auto &AltEntryThread : AltEntryThreads)
884     AltEntryThread.join();
885
886   // Run destructors.
887   ExitOnErr(J->runDestructors());
888   CXXRuntimeOverrides.runDestructors();
889
890   return Result;
891 }
892
893 void disallowOrcOptions() {
894   // Make sure nobody used an orc-lazy specific option accidentally.
895
896   if (LazyJITCompileThreads != 0) {
897     errs() << "-compile-threads requires -jit-kind=orc-lazy\n";
898     exit(1);
899   }
900
901   if (!ThreadEntryPoints.empty()) {
902     errs() << "-thread-entry requires -jit-kind=orc-lazy\n";
903     exit(1);
904   }
905
906   if (PerModuleLazy) {
907     errs() << "-per-module-lazy requires -jit-kind=orc-lazy\n";
908     exit(1);
909   }
910 }
911
912 std::unique_ptr<FDRawChannel> launchRemote() {
913 #ifndef LLVM_ON_UNIX
914   llvm_unreachable("launchRemote not supported on non-Unix platforms");
915 #else
916   int PipeFD[2][2];
917   pid_t ChildPID;
918
919   // Create two pipes.
920   if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0)
921     perror("Error creating pipe: ");
922
923   ChildPID = fork();
924
925   if (ChildPID == 0) {
926     // In the child...
927
928     // Close the parent ends of the pipes
929     close(PipeFD[0][1]);
930     close(PipeFD[1][0]);
931
932
933     // Execute the child process.
934     std::unique_ptr<char[]> ChildPath, ChildIn, ChildOut;
935     {
936       ChildPath.reset(new char[ChildExecPath.size() + 1]);
937       std::copy(ChildExecPath.begin(), ChildExecPath.end(), &ChildPath[0]);
938       ChildPath[ChildExecPath.size()] = '\0';
939       std::string ChildInStr = utostr(PipeFD[0][0]);
940       ChildIn.reset(new char[ChildInStr.size() + 1]);
941       std::copy(ChildInStr.begin(), ChildInStr.end(), &ChildIn[0]);
942       ChildIn[ChildInStr.size()] = '\0';
943       std::string ChildOutStr = utostr(PipeFD[1][1]);
944       ChildOut.reset(new char[ChildOutStr.size() + 1]);
945       std::copy(ChildOutStr.begin(), ChildOutStr.end(), &ChildOut[0]);
946       ChildOut[ChildOutStr.size()] = '\0';
947     }
948
949     char * const args[] = { &ChildPath[0], &ChildIn[0], &ChildOut[0], nullptr };
950     int rc = execv(ChildExecPath.c_str(), args);
951     if (rc != 0)
952       perror("Error executing child process: ");
953     llvm_unreachable("Error executing child process");
954   }
955   // else we're the parent...
956
957   // Close the child ends of the pipes
958   close(PipeFD[0][0]);
959   close(PipeFD[1][1]);
960
961   // Return an RPC channel connected to our end of the pipes.
962   return llvm::make_unique<FDRawChannel>(PipeFD[1][0], PipeFD[0][1]);
963 #endif
964 }