[yaml2obj/obj2yaml] - Add support for .stack_sizes sections.
[llvm-complete.git] / tools / lli / lli.cpp
blobccad067214141ba6da7955952daaa13fcb8a458e
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.
13 //===----------------------------------------------------------------------===//
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>
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
68 using namespace llvm;
70 #define DEBUG_TYPE "lli"
72 namespace {
74 enum class JITKind { MCJIT, OrcMCJITReplacement, OrcLazy };
76 cl::opt<std::string>
77 InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-"));
79 cl::list<std::string>
80 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
82 cl::opt<bool> ForceInterpreter("force-interpreter",
83 cl::desc("Force interpretation: disable JIT"),
84 cl::init(false));
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.")));
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));
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)"));
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));
113 cl::list<std::string>
114 JITDylibs("jd",
115 cl::desc("Specifies the JITDylib to be used for any subsequent "
116 "-extra-module arguments."));
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));
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(""));
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(' '));
145 cl::opt<std::string>
146 TargetTriple("mtriple", cl::desc("Override target triple for module"));
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"));
155 cl::list<std::string>
156 ExtraModules("extra-module",
157 cl::desc("Extra modules to be loaded"),
158 cl::value_desc("input bitcode"));
160 cl::list<std::string>
161 ExtraObjects("extra-object",
162 cl::desc("Extra object files to be loaded"),
163 cl::value_desc("input object"));
165 cl::list<std::string>
166 ExtraArchives("extra-archive",
167 cl::desc("Extra archive files to be loaded"),
168 cl::value_desc("input archive"));
170 cl::opt<bool>
171 EnableCacheManager("enable-cache-manager",
172 cl::desc("Use cache manager to save/load modules"),
173 cl::init(false));
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(""));
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"));
186 cl::opt<bool>
187 DisableCoreFiles("disable-core-files", cl::Hidden,
188 cl::desc("Disable emission of core files if possible"));
190 cl::opt<bool>
191 NoLazyCompilation("disable-lazy-compilation",
192 cl::desc("Disable JIT lazy compilation"),
193 cl::init(false));
195 cl::opt<bool>
196 GenerateSoftFloatCalls("soft-float",
197 cl::desc("Generate software floating point library calls"),
198 cl::init(false));
200 enum class DumpKind {
201 NoDump,
202 DumpFuncsToStdOut,
203 DumpModsToStdOut,
204 DumpModsToDisk
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);
222 ExitOnError ExitOnErr;
225 //===----------------------------------------------------------------------===//
226 // Object cache
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.
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 += '/';
242 ~LLIObjectCache() override {}
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));
253 std::error_code EC;
254 raw_fd_ostream outfile(CacheName, EC, sys::fs::OF_None);
255 outfile.write(Obj.getBufferStart(), Obj.getBufferSize());
256 outfile.close();
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());
277 private:
278 std::string CacheDir;
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] = '/';
292 #endif
293 CacheName = CacheDir + CacheSubdir;
294 size_t pos = CacheName.rfind('.');
295 CacheName.replace(pos, CacheName.length() - pos, ".o");
296 return true;
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);
310 // Create a new module.
311 std::unique_ptr<Module> M = std::make_unique<Module>("CygMingHelper", Context);
312 M->setTargetTriple(TargetTripleStr);
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());
324 BasicBlock *BB = BasicBlock::Create(Context, "__main", Result);
325 Builder.SetInsertPoint(BB);
326 Value *ReturnVal = ConstantInt::get(ReturnTy, 0);
327 Builder.CreateRet(ReturnVal);
329 // Add this new module to the ExecutionEngine.
330 EE.addModule(std::move(M));
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;
344 llvm_unreachable("Unrecognized opt level.");
347 LLVM_ATTRIBUTE_NORETURN
348 static void reportError(SMDiagnostic Err, const char *ProgName) {
349 Err.print(ProgName, errs());
350 exit(1);
353 int runOrcLazyJIT(const char *ProgName);
354 void disallowOrcOptions();
356 //===----------------------------------------------------------------------===//
357 // main Driver function
359 int main(int argc, char **argv, char * const *envp) {
360 InitLLVM X(argc, argv);
362 if (argc > 1)
363 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
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();
371 cl::ParseCommandLineOptions(argc, argv,
372 "llvm interpreter & dynamic compiler\n");
374 // If the user doesn't want core files, disable them.
375 if (DisableCoreFiles)
376 sys::Process::PreventCoreFiles();
378 if (UseJITKind == JITKind::OrcLazy)
379 return runOrcLazyJIT(argv[0]);
380 else
381 disallowOrcOptions();
383 LLVMContext Context;
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]);
392 if (EnableCacheManager) {
393 std::string CacheName("file:");
394 CacheName.append(InputFile);
395 Mod->setModuleIdentifier(CacheName);
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());
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);
422 // If we are supposed to override the target triple, do so now.
423 if (!TargetTriple.empty())
424 Mod->setTargetTriple(Triple::normalize(TargetTriple));
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();
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);
444 builder.setOptLevel(getOptLevel());
446 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
447 if (FloatABIForCalls != FloatABI::Default)
448 Options.FloatABIType = FloatABIForCalls;
450 builder.setTargetOptions(Options);
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);
462 std::unique_ptr<LLIObjectCache> CacheManager;
463 if (EnableCacheManager) {
464 CacheManager.reset(new LLIObjectCache(ObjectCacheDir));
465 EE->setObjectCache(CacheManager.get());
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);
478 EE->addModule(std::move(XMod));
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]);
489 object::OwningBinary<object::ObjectFile> &O = Obj.get();
490 EE->addObjectFile(std::move(O));
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();
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);
510 std::unique_ptr<object::Archive> &Ar = ArOrErr.get();
512 object::OwningBinary<object::Archive> OB(std::move(Ar), std::move(ArBuf));
514 EE->addArchive(std::move(OB));
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());
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());
533 if (!NoLazyCompilation && RemoteMCJIT) {
534 WithColor::warning(errs(), argv[0])
535 << "remote mcjit does not support lazy compilation\n";
536 NoLazyCompilation = true;
538 EE->DisableLazyCompilation(NoLazyCompilation);
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);
551 // Add the module's name to the start of the vector of arguments to main().
552 InputArgv.insert(InputArgv.begin(), InputFile);
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.
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;
566 // Reset errno to zero on entry to main.
567 errno = 0;
569 int Result = -1;
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;
590 #endif
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));
599 // Run static constructors.
600 if (!ForceInterpreter) {
601 // Give MCJIT a chance to apply relocations and set page permissions.
602 EE->finalizeObject();
604 EE->runStaticConstructorsDestructors(false);
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();
613 // Run main.
614 Result = EE->runFunctionAsMain(EntryFn, InputArgv, envp);
616 // Run static destructors.
617 EE->runStaticConstructorsDestructors(true);
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();
634 WithColor::error(errs(), argv[0]) << "exit defined with wrong prototype!\n";
635 abort();
636 } else {
637 // else == "if (RemoteMCJIT)"
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.
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);
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));
656 // Create a remote memory manager.
657 auto RemoteMM = ExitOnErr(R->createRemoteMemoryManager());
659 // Forward MCJIT's memory manager calls to the remote memory manager.
660 static_cast<ForwardingMemoryManager*>(RTDyldMM)->setMemMgr(
661 std::move(RemoteMM));
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 }));
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));
683 // Like static constructors, the remote target MCJIT support doesn't handle
684 // this yet. It could. FIXME.
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();
691 // Signal the remote target that we're done JITing.
692 ExitOnErr(R->terminateSession());
695 return Result;
698 static std::function<void(Module &)> createDebugDumper() {
699 switch (OrcDumpKind) {
700 case DumpKind::NoDump:
701 return [](Module &M) {};
703 case DumpKind::DumpFuncsToStdOut:
704 return [](Module &M) {
705 printf("[ ");
707 for (const auto &F : M) {
708 if (F.isDeclaration())
709 continue;
711 if (F.hasName()) {
712 std::string Name(F.getName());
713 printf("%s ", Name.c_str());
714 } else
715 printf("<anon> ");
718 printf("]\n");
721 case DumpKind::DumpModsToStdOut:
722 return [](Module &M) {
723 outs() << "----- Module Start -----\n" << M << "----- Module End -----\n";
726 case DumpKind::DumpModsToDisk:
727 return [](Module &M) {
728 std::error_code EC;
729 raw_fd_ostream Out(M.getModuleIdentifier() + ".ll", EC, sys::fs::OF_Text);
730 if (EC) {
731 errs() << "Couldn't open " << M.getModuleIdentifier()
732 << " for dumping.\nError:" << EC.message() << "\n";
733 exit(1);
735 Out << M;
738 llvm_unreachable("Unknown DumpKind");
741 static void exitOnLazyCallThroughFailure() { exit(1); }
743 int runOrcLazyJIT(const char *ProgName) {
744 // Start setting up the JIT environment.
746 // Parse the main module.
747 orc::ThreadSafeContext TSCtx(std::make_unique<LLVMContext>());
748 SMDiagnostic Err;
749 auto MainModule = parseIRFile(InputFile, Err, *TSCtx.getContext());
750 if (!MainModule)
751 reportError(Err, ProgName);
753 const auto &TT = MainModule->getTargetTriple();
754 orc::LLLazyJITBuilder Builder;
756 Builder.setJITTargetMachineBuilder(
757 TT.empty() ? ExitOnErr(orc::JITTargetMachineBuilder::detectHost())
758 : orc::JITTargetMachineBuilder(Triple(TT)));
760 if (!MArch.empty())
761 Builder.getJITTargetMachineBuilder()->getTargetTriple().setArchName(MArch);
763 Builder.getJITTargetMachineBuilder()
764 ->setCPU(getCPUStr())
765 .addFeatures(getFeatureList())
766 .setRelocationModel(RelocModel.getNumOccurrences()
767 ? Optional<Reloc::Model>(RelocModel)
768 : None)
769 .setCodeModel(CMModel.getNumOccurrences()
770 ? Optional<CodeModel::Model>(CMModel)
771 : None);
773 Builder.setLazyCompileFailureAddr(
774 pointerToJITTargetAddress(exitOnLazyCallThroughFailure));
775 Builder.setNumCompileThreads(LazyJITCompileThreads);
777 auto J = ExitOnErr(Builder.create());
779 if (PerModuleLazy)
780 J->setPartitionFunction(orc::CompileOnDemandLayer::compileWholeModule);
782 auto Dump = createDebugDumper();
784 J->setLazyCompileTransform([&](orc::ThreadSafeModule TSM,
785 const orc::MaterializationResponsibility &R) {
786 TSM.withModuleDo([&](Module &M) {
787 if (verifyModule(M, &dbgs())) {
788 dbgs() << "Bad module: " << &M << "\n";
789 exit(1);
791 Dump(M);
793 return TSM;
795 J->getMainJITDylib().addGenerator(
796 ExitOnErr(orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(
797 J->getDataLayout().getGlobalPrefix())));
799 orc::MangleAndInterner Mangle(J->getExecutionSession(), J->getDataLayout());
800 orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides;
801 ExitOnErr(CXXRuntimeOverrides.enable(J->getMainJITDylib(), Mangle));
803 // Add the main module.
804 ExitOnErr(
805 J->addLazyIRModule(orc::ThreadSafeModule(std::move(MainModule), TSCtx)));
807 // Create JITDylibs and add any extra modules.
809 // Create JITDylibs, keep a map from argument index to dylib. We will use
810 // -extra-module argument indexes to determine what dylib to use for each
811 // -extra-module.
812 std::map<unsigned, orc::JITDylib *> IdxToDylib;
813 IdxToDylib[0] = &J->getMainJITDylib();
814 for (auto JDItr = JITDylibs.begin(), JDEnd = JITDylibs.end();
815 JDItr != JDEnd; ++JDItr) {
816 orc::JITDylib *JD = J->getJITDylibByName(*JDItr);
817 if (!JD)
818 JD = &J->createJITDylib(*JDItr);
819 IdxToDylib[JITDylibs.getPosition(JDItr - JITDylibs.begin())] = JD;
822 for (auto EMItr = ExtraModules.begin(), EMEnd = ExtraModules.end();
823 EMItr != EMEnd; ++EMItr) {
824 auto M = parseIRFile(*EMItr, Err, *TSCtx.getContext());
825 if (!M)
826 reportError(Err, ProgName);
828 auto EMIdx = ExtraModules.getPosition(EMItr - ExtraModules.begin());
829 assert(EMIdx != 0 && "ExtraModule should have index > 0");
830 auto JDItr = std::prev(IdxToDylib.lower_bound(EMIdx));
831 auto &JD = *JDItr->second;
832 ExitOnErr(
833 J->addLazyIRModule(JD, orc::ThreadSafeModule(std::move(M), TSCtx)));
836 for (auto EAItr = ExtraArchives.begin(), EAEnd = ExtraArchives.end();
837 EAItr != EAEnd; ++EAItr) {
838 auto EAIdx = ExtraArchives.getPosition(EAItr - ExtraArchives.begin());
839 assert(EAIdx != 0 && "ExtraArchive should have index > 0");
840 auto JDItr = std::prev(IdxToDylib.lower_bound(EAIdx));
841 auto &JD = *JDItr->second;
842 JD.addGenerator(ExitOnErr(orc::StaticLibraryDefinitionGenerator::Load(
843 J->getObjLinkingLayer(), EAItr->c_str())));
847 // Add the objects.
848 for (auto &ObjPath : ExtraObjects) {
849 auto Obj = ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ObjPath)));
850 ExitOnErr(J->addObjectFile(std::move(Obj)));
853 // Generate a argument string.
854 std::vector<std::string> Args;
855 Args.push_back(InputFile);
856 for (auto &Arg : InputArgv)
857 Args.push_back(Arg);
859 // Run any static constructors.
860 ExitOnErr(J->runConstructors());
862 // Run any -thread-entry points.
863 std::vector<std::thread> AltEntryThreads;
864 for (auto &ThreadEntryPoint : ThreadEntryPoints) {
865 auto EntryPointSym = ExitOnErr(J->lookup(ThreadEntryPoint));
866 typedef void (*EntryPointPtr)();
867 auto EntryPoint =
868 reinterpret_cast<EntryPointPtr>(static_cast<uintptr_t>(EntryPointSym.getAddress()));
869 AltEntryThreads.push_back(std::thread([EntryPoint]() { EntryPoint(); }));
872 // Run main.
873 auto MainSym = ExitOnErr(J->lookup("main"));
874 typedef int (*MainFnPtr)(int, const char *[]);
875 std::vector<const char *> ArgV;
876 for (auto &Arg : Args)
877 ArgV.push_back(Arg.c_str());
878 ArgV.push_back(nullptr);
880 int ArgC = ArgV.size() - 1;
881 auto Main =
882 reinterpret_cast<MainFnPtr>(static_cast<uintptr_t>(MainSym.getAddress()));
883 auto Result = Main(ArgC, (const char **)ArgV.data());
885 // Wait for -entry-point threads.
886 for (auto &AltEntryThread : AltEntryThreads)
887 AltEntryThread.join();
889 // Run destructors.
890 ExitOnErr(J->runDestructors());
891 CXXRuntimeOverrides.runDestructors();
893 return Result;
896 void disallowOrcOptions() {
897 // Make sure nobody used an orc-lazy specific option accidentally.
899 if (LazyJITCompileThreads != 0) {
900 errs() << "-compile-threads requires -jit-kind=orc-lazy\n";
901 exit(1);
904 if (!ThreadEntryPoints.empty()) {
905 errs() << "-thread-entry requires -jit-kind=orc-lazy\n";
906 exit(1);
909 if (PerModuleLazy) {
910 errs() << "-per-module-lazy requires -jit-kind=orc-lazy\n";
911 exit(1);
915 std::unique_ptr<FDRawChannel> launchRemote() {
916 #ifndef LLVM_ON_UNIX
917 llvm_unreachable("launchRemote not supported on non-Unix platforms");
918 #else
919 int PipeFD[2][2];
920 pid_t ChildPID;
922 // Create two pipes.
923 if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0)
924 perror("Error creating pipe: ");
926 ChildPID = fork();
928 if (ChildPID == 0) {
929 // In the child...
931 // Close the parent ends of the pipes
932 close(PipeFD[0][1]);
933 close(PipeFD[1][0]);
936 // Execute the child process.
937 std::unique_ptr<char[]> ChildPath, ChildIn, ChildOut;
939 ChildPath.reset(new char[ChildExecPath.size() + 1]);
940 std::copy(ChildExecPath.begin(), ChildExecPath.end(), &ChildPath[0]);
941 ChildPath[ChildExecPath.size()] = '\0';
942 std::string ChildInStr = utostr(PipeFD[0][0]);
943 ChildIn.reset(new char[ChildInStr.size() + 1]);
944 std::copy(ChildInStr.begin(), ChildInStr.end(), &ChildIn[0]);
945 ChildIn[ChildInStr.size()] = '\0';
946 std::string ChildOutStr = utostr(PipeFD[1][1]);
947 ChildOut.reset(new char[ChildOutStr.size() + 1]);
948 std::copy(ChildOutStr.begin(), ChildOutStr.end(), &ChildOut[0]);
949 ChildOut[ChildOutStr.size()] = '\0';
952 char * const args[] = { &ChildPath[0], &ChildIn[0], &ChildOut[0], nullptr };
953 int rc = execv(ChildExecPath.c_str(), args);
954 if (rc != 0)
955 perror("Error executing child process: ");
956 llvm_unreachable("Error executing child process");
958 // else we're the parent...
960 // Close the child ends of the pipes
961 close(PipeFD[0][0]);
962 close(PipeFD[1][1]);
964 // Return an RPC channel connected to our end of the pipes.
965 return std::make_unique<FDRawChannel>(PipeFD[1][0], PipeFD[0][1]);
966 #endif