1 //===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
62 #include <cygwin/version.h>
63 #if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
64 #define DO_NOTHING_ATEXIT 1
70 #define DEBUG_TYPE "lli"
74 enum class JITKind { MCJIT, OrcMCJITReplacement, OrcLazy };
77 InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-"));
80 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
82 cl::opt<bool> ForceInterpreter("force-interpreter",
83 cl::desc("Force interpretation: disable JIT"),
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 "
93 clEnumValN(JITKind::OrcLazy, "orc-lazy",
94 "Orc-based lazy JIT.")));
97 LazyJITCompileThreads("compile-threads",
98 cl::desc("Choose the number of compile threads "
99 "(jit-kind=orc-lazy only)"),
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(
109 cl::desc("Performs lazy compilation on whole module boundaries "
110 "rather than individual functions"),
113 cl::list<std::string>
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."),
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.
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.
139 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
140 "(default = '-O2')"),
146 TargetTriple("mtriple", cl::desc("Override target triple for module"));
149 EntryFunc("entry-function",
150 cl::desc("Specify the entry function (default = 'main') "
151 "of the executable"),
152 cl::value_desc("function"),
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"));
171 EnableCacheManager("enable-cache-manager",
172 cl::desc("Use cache manager to save/load modules"),
176 ObjectCacheDir("object-cache-dir",
177 cl::desc("Directory to store cached object files "
178 "(must be user writable)"),
182 FakeArgv0("fake-argv0",
183 cl::desc("Override the 'argv[0]' value passed into the executing"
184 " program"), cl::value_desc("executable"));
187 DisableCoreFiles("disable-core-files", cl::Hidden,
188 cl::desc("Disable emission of core files if possible"));
191 NoLazyCompilation("disable-lazy-compilation",
192 cl::desc("Disable JIT lazy compilation"),
196 GenerateSoftFloatCalls("soft-float",
197 cl::desc("Generate software floating point library calls"),
200 enum class DumpKind {
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).")),
222 ExitOnError ExitOnErr;
225 //===----------------------------------------------------------------------===//
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 {
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))
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));
254 raw_fd_ostream outfile(CacheName, EC, sys::fs::F_None);
255 outfile.write(Obj.getBufferStart(), Obj.getBufferSize());
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))
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.
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
274 return MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer());
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)
285 std::string CacheSubdir = ModID.substr(PrefixLength);
287 // Transform "X:\foo" => "/X\foo" for convenience.
288 if (isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') {
289 CacheSubdir[1] = CacheSubdir[0];
290 CacheSubdir[0] = '/';
293 CacheName = CacheDir + CacheSubdir;
294 size_t pos = CacheName.rfind('.');
295 CacheName.replace(pos, CacheName.length() - pos, ".o");
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 = make_unique<Module>("CygMingHelper", Context);
312 M->setTargetTriple(TargetTripleStr);
314 // Create an empty function named "__main".
316 if (TargetTriple.isArch64Bit())
317 ReturnTy = Type::getInt64Ty(Context);
319 ReturnTy = Type::getInt32Ty(Context);
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() {
336 WithColor::error(errs(), "lli") << "invalid optimization level.\n";
338 case '0': return CodeGenOpt::None;
339 case '1': return CodeGenOpt::Less;
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());
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);
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]);
381 disallowOrcOptions();
385 // Load the bitcode...
387 std::unique_ptr<Module> Owner = parseIRFile(InputFile, Err, Context);
388 Module *Mod = Owner.get();
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
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) {
430 RTDyldMM = new ForwardingMemoryManager();
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";
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());
454 if (!ErrorMsg.empty())
455 WithColor::error(errs(), argv[0])
456 << "error creating EE: " << ErrorMsg << "\n";
458 WithColor::error(errs(), argv[0]) << "unknown error creating EE!\n";
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);
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]);
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]);
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());
504 raw_string_ostream OS(Buf);
505 logAllUnhandledErrors(ArOrErr.takeError(), OS);
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());
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,
542 if (!FakeArgv0.empty()) {
543 InputFile = static_cast<std::string>(FakeArgv0);
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);
561 WithColor::error(errs(), argv[0])
562 << '\'' << EntryFunc << "\' function not found in module.\n";
566 // Reset errno to zero on entry to main.
571 // Sanity check use of remote-jit: LLI currently only supports use of the
572 // remote JIT on Unix platforms.
575 WithColor::warning(errs(), argv[0])
576 << "host does not support external remote targets.\n";
577 WithColor::note() << "defaulting to local execution\n";
580 if (ChildExecPath.empty()) {
581 WithColor::error(errs(), argv[0])
582 << "-remote-mcjit requires -mcjit-remote-process.\n";
584 } else if (!sys::fs::can_execute(ChildExecPath)) {
585 WithColor::error(errs(), argv[0])
586 << "unable to find usable child executable: '" << ChildExecPath
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.
611 static_cast<SectionMemoryManager*>(RTDyldMM)->invalidateInstructionCache();
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";
634 WithColor::error(errs(), argv[0]) << "exit defined with wrong prototype!\n";
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();
646 WithColor::error(errs(), argv[0]) << "failed to launch remote JIT.\n";
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);
674 // Grab the target address of the JIT'd main function on the remote and call
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.
691 // Signal the remote target that we're done JITing.
692 ExitOnErr(R->terminateSession());
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; };
704 case DumpKind::DumpFuncsToStdOut:
705 return [](orc::ThreadSafeModule TSM,
706 const orc::MaterializationResponsibility &R) {
709 for (const auto &F : *TSM.getModule()) {
710 if (F.isDeclaration())
714 std::string Name(F.getName());
715 printf("%s ", Name.c_str());
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";
733 case DumpKind::DumpModsToDisk:
734 return [](orc::ThreadSafeModule TSM,
735 const orc::MaterializationResponsibility &R) {
737 raw_fd_ostream Out(TSM.getModule()->getModuleIdentifier() + ".ll", EC,
740 errs() << "Couldn't open " << TSM.getModule()->getModuleIdentifier()
741 << " for dumping.\nError:" << EC.message() << "\n";
744 Out << *TSM.getModule();
748 llvm_unreachable("Unknown DumpKind");
751 static void exitOnLazyCallThroughFailure() { exit(1); }
753 int runOrcLazyJIT(const char *ProgName) {
754 // Start setting up the JIT environment.
756 // Parse the main module.
757 orc::ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>());
759 auto MainModule = orc::ThreadSafeModule(
760 parseIRFile(InputFile, Err, *TSCtx.getContext()), TSCtx);
762 reportError(Err, ProgName);
764 const auto &TT = MainModule.getModule()->getTargetTriple();
765 orc::LLLazyJITBuilder Builder;
767 Builder.setJITTargetMachineBuilder(
768 TT.empty() ? ExitOnErr(orc::JITTargetMachineBuilder::detectHost())
769 : orc::JITTargetMachineBuilder(Triple(TT)));
772 Builder.getJITTargetMachineBuilder()->getTargetTriple().setArchName(MArch);
774 Builder.getJITTargetMachineBuilder()
775 ->setCPU(getCPUStr())
776 .addFeatures(getFeatureList())
777 .setRelocationModel(RelocModel.getNumOccurrences()
778 ? Optional<Reloc::Model>(RelocModel)
780 .setCodeModel(CMModel.getNumOccurrences()
781 ? Optional<CodeModel::Model>(CMModel)
784 Builder.setLazyCompileFailureAddr(
785 pointerToJITTargetAddress(exitOnLazyCallThroughFailure));
786 Builder.setNumCompileThreads(LazyJITCompileThreads);
788 auto J = ExitOnErr(Builder.create());
791 J->setPartitionFunction(orc::CompileOnDemandLayer::compileWholeModule);
793 auto Dump = createDebugDumper();
795 J->setLazyCompileTransform([&](orc::ThreadSafeModule TSM,
796 const orc::MaterializationResponsibility &R) {
797 if (verifyModule(*TSM.getModule(), &dbgs())) {
798 dbgs() << "Bad module: " << *TSM.getModule() << "\n";
801 return Dump(std::move(TSM), R);
803 J->getMainJITDylib().setGenerator(
804 ExitOnErr(orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(
805 J->getDataLayout().getGlobalPrefix())));
807 orc::MangleAndInterner Mangle(J->getExecutionSession(), J->getDataLayout());
808 orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides;
809 ExitOnErr(CXXRuntimeOverrides.enable(J->getMainJITDylib(), Mangle));
811 // Add the main module.
812 ExitOnErr(J->addLazyIRModule(std::move(MainModule)));
814 // Create JITDylibs and add any extra modules.
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
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);
825 JD = &J->createJITDylib(*JDItr);
826 IdxToDylib[JITDylibs.getPosition(JDItr - JITDylibs.begin())] = JD;
829 for (auto EMItr = ExtraModules.begin(), EMEnd = ExtraModules.end();
830 EMItr != EMEnd; ++EMItr) {
831 auto M = parseIRFile(*EMItr, Err, *TSCtx.getContext());
833 reportError(Err, ProgName);
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;
840 J->addLazyIRModule(JD, orc::ThreadSafeModule(std::move(M), TSCtx)));
845 for (auto &ObjPath : ExtraObjects) {
846 auto Obj = ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ObjPath)));
847 ExitOnErr(J->addObjectFile(std::move(Obj)));
850 // Generate a argument string.
851 std::vector<std::string> Args;
852 Args.push_back(InputFile);
853 for (auto &Arg : InputArgv)
856 // Run any static constructors.
857 ExitOnErr(J->runConstructors());
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)();
865 reinterpret_cast<EntryPointPtr>(static_cast<uintptr_t>(EntryPointSym.getAddress()));
866 AltEntryThreads.push_back(std::thread([EntryPoint]() { EntryPoint(); }));
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);
877 int ArgC = ArgV.size() - 1;
879 reinterpret_cast<MainFnPtr>(static_cast<uintptr_t>(MainSym.getAddress()));
880 auto Result = Main(ArgC, (const char **)ArgV.data());
882 // Wait for -entry-point threads.
883 for (auto &AltEntryThread : AltEntryThreads)
884 AltEntryThread.join();
887 ExitOnErr(J->runDestructors());
888 CXXRuntimeOverrides.runDestructors();
893 void disallowOrcOptions() {
894 // Make sure nobody used an orc-lazy specific option accidentally.
896 if (LazyJITCompileThreads != 0) {
897 errs() << "-compile-threads requires -jit-kind=orc-lazy\n";
901 if (!ThreadEntryPoints.empty()) {
902 errs() << "-thread-entry requires -jit-kind=orc-lazy\n";
907 errs() << "-per-module-lazy requires -jit-kind=orc-lazy\n";
912 std::unique_ptr<FDRawChannel> launchRemote() {
914 llvm_unreachable("launchRemote not supported on non-Unix platforms");
920 if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0)
921 perror("Error creating pipe: ");
928 // Close the parent ends of the pipes
933 // Execute the child process.
934 std::unique_ptr<char[]> ChildPath, ChildIn, ChildOut;
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';
949 char * const args[] = { &ChildPath[0], &ChildIn[0], &ChildOut[0], nullptr };
950 int rc = execv(ChildExecPath.c_str(), args);
952 perror("Error executing child process: ");
953 llvm_unreachable("Error executing child process");
955 // else we're the parent...
957 // Close the child ends of the pipes
961 // Return an RPC channel connected to our end of the pipes.
962 return llvm::make_unique<FDRawChannel>(PipeFD[1][0], PipeFD[0][1]);