10b57cec5SDimitry Andric //===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This utility provides a simple wrapper around the LLVM Execution Engines,
100b57cec5SDimitry Andric // which allow the direct execution of LLVM programs through a Just-In-Time
110b57cec5SDimitry Andric // compiler, or through an interpreter if no JIT is available for this platform.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric
15349cc55cSDimitry Andric #include "ForwardingMemoryManager.h"
160b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
170b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeReader.h"
185ffd83dbSDimitry Andric #include "llvm/CodeGen/CommandFlags.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/LinkAllCodegenComponents.h"
200b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
210b57cec5SDimitry Andric #include "llvm/ExecutionEngine/GenericValue.h"
220b57cec5SDimitry Andric #include "llvm/ExecutionEngine/Interpreter.h"
230b57cec5SDimitry Andric #include "llvm/ExecutionEngine/JITEventListener.h"
24fe6060f1SDimitry Andric #include "llvm/ExecutionEngine/JITSymbol.h"
250b57cec5SDimitry Andric #include "llvm/ExecutionEngine/MCJIT.h"
260b57cec5SDimitry Andric #include "llvm/ExecutionEngine/ObjectCache.h"
275ffd83dbSDimitry Andric #include "llvm/ExecutionEngine/Orc/DebugUtils.h"
285f757f3fSDimitry Andric #include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h"
2981ad6265SDimitry Andric #include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
30fe6060f1SDimitry Andric #include "llvm/ExecutionEngine/Orc/EPCEHFrameRegistrar.h"
31349cc55cSDimitry Andric #include "llvm/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.h"
320b57cec5SDimitry Andric #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
330b57cec5SDimitry Andric #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
340b57cec5SDimitry Andric #include "llvm/ExecutionEngine/Orc/LLJIT.h"
357a6dacacSDimitry Andric #include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
365ffd83dbSDimitry Andric #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
37349cc55cSDimitry Andric #include "llvm/ExecutionEngine/Orc/SimpleRemoteEPC.h"
38fe6060f1SDimitry Andric #include "llvm/ExecutionEngine/Orc/SymbolStringPool.h"
39fe6060f1SDimitry Andric #include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
40fe6060f1SDimitry Andric #include "llvm/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.h"
41e8d8bef9SDimitry Andric #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
420b57cec5SDimitry Andric #include "llvm/ExecutionEngine/SectionMemoryManager.h"
430b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h"
440b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
450b57cec5SDimitry Andric #include "llvm/IR/Module.h"
460b57cec5SDimitry Andric #include "llvm/IR/Type.h"
470b57cec5SDimitry Andric #include "llvm/IR/Verifier.h"
480b57cec5SDimitry Andric #include "llvm/IRReader/IRReader.h"
490b57cec5SDimitry Andric #include "llvm/Object/Archive.h"
500b57cec5SDimitry Andric #include "llvm/Object/ObjectFile.h"
510b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
520b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
530b57cec5SDimitry Andric #include "llvm/Support/DynamicLibrary.h"
540b57cec5SDimitry Andric #include "llvm/Support/Format.h"
550b57cec5SDimitry Andric #include "llvm/Support/InitLLVM.h"
560b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
570b57cec5SDimitry Andric #include "llvm/Support/Memory.h"
580b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
590b57cec5SDimitry Andric #include "llvm/Support/Path.h"
600b57cec5SDimitry Andric #include "llvm/Support/PluginLoader.h"
610b57cec5SDimitry Andric #include "llvm/Support/Process.h"
620b57cec5SDimitry Andric #include "llvm/Support/Program.h"
630b57cec5SDimitry Andric #include "llvm/Support/SourceMgr.h"
640b57cec5SDimitry Andric #include "llvm/Support/TargetSelect.h"
657a6dacacSDimitry Andric #include "llvm/Support/ToolOutputFile.h"
660b57cec5SDimitry Andric #include "llvm/Support/WithColor.h"
670b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
6806c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h"
690b57cec5SDimitry Andric #include "llvm/Transforms/Instrumentation.h"
700b57cec5SDimitry Andric #include <cerrno>
71bdd1243dSDimitry Andric #include <optional>
720b57cec5SDimitry Andric
73349cc55cSDimitry Andric #if !defined(_MSC_VER) && !defined(__MINGW32__)
74349cc55cSDimitry Andric #include <unistd.h>
75349cc55cSDimitry Andric #else
76349cc55cSDimitry Andric #include <io.h>
77349cc55cSDimitry Andric #endif
78349cc55cSDimitry Andric
790b57cec5SDimitry Andric #ifdef __CYGWIN__
800b57cec5SDimitry Andric #include <cygwin/version.h>
810b57cec5SDimitry Andric #if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
820b57cec5SDimitry Andric #define DO_NOTHING_ATEXIT 1
830b57cec5SDimitry Andric #endif
840b57cec5SDimitry Andric #endif
850b57cec5SDimitry Andric
860b57cec5SDimitry Andric using namespace llvm;
870b57cec5SDimitry Andric
885ffd83dbSDimitry Andric static codegen::RegisterCodeGenFlags CGF;
895ffd83dbSDimitry Andric
900b57cec5SDimitry Andric #define DEBUG_TYPE "lli"
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric namespace {
930b57cec5SDimitry Andric
94fe6060f1SDimitry Andric enum class JITKind { MCJIT, Orc, OrcLazy };
95fe6060f1SDimitry Andric enum class JITLinkerKind { Default, RuntimeDyld, JITLink };
960b57cec5SDimitry Andric
970b57cec5SDimitry Andric cl::opt<std::string>
980b57cec5SDimitry Andric InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-"));
990b57cec5SDimitry Andric
1000b57cec5SDimitry Andric cl::list<std::string>
1010b57cec5SDimitry Andric InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
1020b57cec5SDimitry Andric
1030b57cec5SDimitry Andric cl::opt<bool> ForceInterpreter("force-interpreter",
1040b57cec5SDimitry Andric cl::desc("Force interpretation: disable JIT"),
1050b57cec5SDimitry Andric cl::init(false));
1060b57cec5SDimitry Andric
1070b57cec5SDimitry Andric cl::opt<JITKind> UseJITKind(
1080b57cec5SDimitry Andric "jit-kind", cl::desc("Choose underlying JIT kind."),
109fe6060f1SDimitry Andric cl::init(JITKind::Orc),
1100b57cec5SDimitry Andric cl::values(clEnumValN(JITKind::MCJIT, "mcjit", "MCJIT"),
111fe6060f1SDimitry Andric clEnumValN(JITKind::Orc, "orc", "Orc JIT"),
1120b57cec5SDimitry Andric clEnumValN(JITKind::OrcLazy, "orc-lazy",
1130b57cec5SDimitry Andric "Orc-based lazy JIT.")));
1140b57cec5SDimitry Andric
115fe6060f1SDimitry Andric cl::opt<JITLinkerKind>
116fe6060f1SDimitry Andric JITLinker("jit-linker", cl::desc("Choose the dynamic linker/loader."),
117fe6060f1SDimitry Andric cl::init(JITLinkerKind::Default),
118fe6060f1SDimitry Andric cl::values(clEnumValN(JITLinkerKind::Default, "default",
119fe6060f1SDimitry Andric "Default for platform and JIT-kind"),
120fe6060f1SDimitry Andric clEnumValN(JITLinkerKind::RuntimeDyld, "rtdyld",
121fe6060f1SDimitry Andric "RuntimeDyld"),
122fe6060f1SDimitry Andric clEnumValN(JITLinkerKind::JITLink, "jitlink",
123fe6060f1SDimitry Andric "Orc-specific linker")));
12481ad6265SDimitry Andric cl::opt<std::string> OrcRuntime("orc-runtime",
12581ad6265SDimitry Andric cl::desc("Use ORC runtime from given path"),
12681ad6265SDimitry Andric cl::init(""));
127fe6060f1SDimitry Andric
1280b57cec5SDimitry Andric cl::opt<unsigned>
1290b57cec5SDimitry Andric LazyJITCompileThreads("compile-threads",
1300b57cec5SDimitry Andric cl::desc("Choose the number of compile threads "
1310b57cec5SDimitry Andric "(jit-kind=orc-lazy only)"),
1320b57cec5SDimitry Andric cl::init(0));
1330b57cec5SDimitry Andric
1340b57cec5SDimitry Andric cl::list<std::string>
1350b57cec5SDimitry Andric ThreadEntryPoints("thread-entry",
1360b57cec5SDimitry Andric cl::desc("calls the given entry-point on a new thread "
1370b57cec5SDimitry Andric "(jit-kind=orc-lazy only)"));
1380b57cec5SDimitry Andric
1390b57cec5SDimitry Andric cl::opt<bool> PerModuleLazy(
1400b57cec5SDimitry Andric "per-module-lazy",
1410b57cec5SDimitry Andric cl::desc("Performs lazy compilation on whole module boundaries "
1420b57cec5SDimitry Andric "rather than individual functions"),
1430b57cec5SDimitry Andric cl::init(false));
1440b57cec5SDimitry Andric
1450b57cec5SDimitry Andric cl::list<std::string>
1460b57cec5SDimitry Andric JITDylibs("jd",
1470b57cec5SDimitry Andric cl::desc("Specifies the JITDylib to be used for any subsequent "
1480b57cec5SDimitry Andric "-extra-module arguments."));
1490b57cec5SDimitry Andric
1505ffd83dbSDimitry Andric cl::list<std::string>
15181ad6265SDimitry Andric Dylibs("dlopen", cl::desc("Dynamic libraries to load before linking"));
1525ffd83dbSDimitry Andric
1530b57cec5SDimitry Andric // The MCJIT supports building for a target address space separate from
1540b57cec5SDimitry Andric // the JIT compilation process. Use a forked process and a copying
1550b57cec5SDimitry Andric // memory manager with IPC to execute using this functionality.
1560b57cec5SDimitry Andric cl::opt<bool> RemoteMCJIT("remote-mcjit",
1570b57cec5SDimitry Andric cl::desc("Execute MCJIT'ed code in a separate process."),
1580b57cec5SDimitry Andric cl::init(false));
1590b57cec5SDimitry Andric
1600b57cec5SDimitry Andric // Manually specify the child process for remote execution. This overrides
1610b57cec5SDimitry Andric // the simulated remote execution that allocates address space for child
1620b57cec5SDimitry Andric // execution. The child process will be executed and will communicate with
1630b57cec5SDimitry Andric // lli via stdin/stdout pipes.
1640b57cec5SDimitry Andric cl::opt<std::string>
1650b57cec5SDimitry Andric ChildExecPath("mcjit-remote-process",
1660b57cec5SDimitry Andric cl::desc("Specify the filename of the process to launch "
1670b57cec5SDimitry Andric "for remote MCJIT execution. If none is specified,"
1680b57cec5SDimitry Andric "\n\tremote execution will be simulated in-process."),
1690b57cec5SDimitry Andric cl::value_desc("filename"), cl::init(""));
1700b57cec5SDimitry Andric
1710b57cec5SDimitry Andric // Determine optimization level.
17281ad6265SDimitry Andric cl::opt<char> OptLevel("O",
1730b57cec5SDimitry Andric cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
1740b57cec5SDimitry Andric "(default = '-O2')"),
175bdd1243dSDimitry Andric cl::Prefix, cl::init('2'));
1760b57cec5SDimitry Andric
1770b57cec5SDimitry Andric cl::opt<std::string>
1780b57cec5SDimitry Andric TargetTriple("mtriple", cl::desc("Override target triple for module"));
1790b57cec5SDimitry Andric
1800b57cec5SDimitry Andric cl::opt<std::string>
1810b57cec5SDimitry Andric EntryFunc("entry-function",
1820b57cec5SDimitry Andric cl::desc("Specify the entry function (default = 'main') "
1830b57cec5SDimitry Andric "of the executable"),
1840b57cec5SDimitry Andric cl::value_desc("function"),
1850b57cec5SDimitry Andric cl::init("main"));
1860b57cec5SDimitry Andric
1870b57cec5SDimitry Andric cl::list<std::string>
1880b57cec5SDimitry Andric ExtraModules("extra-module",
1890b57cec5SDimitry Andric cl::desc("Extra modules to be loaded"),
1900b57cec5SDimitry Andric cl::value_desc("input bitcode"));
1910b57cec5SDimitry Andric
1920b57cec5SDimitry Andric cl::list<std::string>
1930b57cec5SDimitry Andric ExtraObjects("extra-object",
1940b57cec5SDimitry Andric cl::desc("Extra object files to be loaded"),
1950b57cec5SDimitry Andric cl::value_desc("input object"));
1960b57cec5SDimitry Andric
1970b57cec5SDimitry Andric cl::list<std::string>
1980b57cec5SDimitry Andric ExtraArchives("extra-archive",
1990b57cec5SDimitry Andric cl::desc("Extra archive files to be loaded"),
2000b57cec5SDimitry Andric cl::value_desc("input archive"));
2010b57cec5SDimitry Andric
2020b57cec5SDimitry Andric cl::opt<bool>
2030b57cec5SDimitry Andric EnableCacheManager("enable-cache-manager",
2040b57cec5SDimitry Andric cl::desc("Use cache manager to save/load modules"),
2050b57cec5SDimitry Andric cl::init(false));
2060b57cec5SDimitry Andric
2070b57cec5SDimitry Andric cl::opt<std::string>
2080b57cec5SDimitry Andric ObjectCacheDir("object-cache-dir",
2090b57cec5SDimitry Andric cl::desc("Directory to store cached object files "
2100b57cec5SDimitry Andric "(must be user writable)"),
2110b57cec5SDimitry Andric cl::init(""));
2120b57cec5SDimitry Andric
2130b57cec5SDimitry Andric cl::opt<std::string>
2140b57cec5SDimitry Andric FakeArgv0("fake-argv0",
2150b57cec5SDimitry Andric cl::desc("Override the 'argv[0]' value passed into the executing"
2160b57cec5SDimitry Andric " program"), cl::value_desc("executable"));
2170b57cec5SDimitry Andric
2180b57cec5SDimitry Andric cl::opt<bool>
2190b57cec5SDimitry Andric DisableCoreFiles("disable-core-files", cl::Hidden,
2200b57cec5SDimitry Andric cl::desc("Disable emission of core files if possible"));
2210b57cec5SDimitry Andric
2220b57cec5SDimitry Andric cl::opt<bool>
2230b57cec5SDimitry Andric NoLazyCompilation("disable-lazy-compilation",
2240b57cec5SDimitry Andric cl::desc("Disable JIT lazy compilation"),
2250b57cec5SDimitry Andric cl::init(false));
2260b57cec5SDimitry Andric
2270b57cec5SDimitry Andric cl::opt<bool>
2280b57cec5SDimitry Andric GenerateSoftFloatCalls("soft-float",
2290b57cec5SDimitry Andric cl::desc("Generate software floating point library calls"),
2300b57cec5SDimitry Andric cl::init(false));
2310b57cec5SDimitry Andric
23213138422SDimitry Andric cl::opt<bool> NoProcessSymbols(
23313138422SDimitry Andric "no-process-syms",
23413138422SDimitry Andric cl::desc("Do not resolve lli process symbols in JIT'd code"),
23513138422SDimitry Andric cl::init(false));
23613138422SDimitry Andric
23706c3fb27SDimitry Andric enum class LLJITPlatform { Inactive, Auto, ExecutorNative, GenericIR };
2385ffd83dbSDimitry Andric
23906c3fb27SDimitry Andric cl::opt<LLJITPlatform> Platform(
24006c3fb27SDimitry Andric "lljit-platform", cl::desc("Platform to use with LLJIT"),
24106c3fb27SDimitry Andric cl::init(LLJITPlatform::Auto),
24206c3fb27SDimitry Andric cl::values(clEnumValN(LLJITPlatform::Auto, "Auto",
24306c3fb27SDimitry Andric "Like 'ExecutorNative' if ORC runtime "
24406c3fb27SDimitry Andric "provided, otherwise like 'GenericIR'"),
24506c3fb27SDimitry Andric clEnumValN(LLJITPlatform::ExecutorNative, "ExecutorNative",
24606c3fb27SDimitry Andric "Use the native platform for the executor."
24706c3fb27SDimitry Andric "Requires -orc-runtime"),
2485ffd83dbSDimitry Andric clEnumValN(LLJITPlatform::GenericIR, "GenericIR",
2495ffd83dbSDimitry Andric "Use LLJITGenericIRPlatform"),
250fe6060f1SDimitry Andric clEnumValN(LLJITPlatform::Inactive, "Inactive",
251fe6060f1SDimitry Andric "Disable platform support explicitly")),
2525ffd83dbSDimitry Andric cl::Hidden);
2535ffd83dbSDimitry Andric
2540b57cec5SDimitry Andric enum class DumpKind {
2550b57cec5SDimitry Andric NoDump,
2560b57cec5SDimitry Andric DumpFuncsToStdOut,
2570b57cec5SDimitry Andric DumpModsToStdOut,
2587a6dacacSDimitry Andric DumpModsToDisk,
2597a6dacacSDimitry Andric DumpDebugDescriptor,
2607a6dacacSDimitry Andric DumpDebugObjects,
2610b57cec5SDimitry Andric };
2620b57cec5SDimitry Andric
2630b57cec5SDimitry Andric cl::opt<DumpKind> OrcDumpKind(
2640b57cec5SDimitry Andric "orc-lazy-debug", cl::desc("Debug dumping for the orc-lazy JIT."),
2650b57cec5SDimitry Andric cl::init(DumpKind::NoDump),
2667a6dacacSDimitry Andric cl::values(
2677a6dacacSDimitry Andric clEnumValN(DumpKind::NoDump, "no-dump", "Don't dump anything."),
2680b57cec5SDimitry Andric clEnumValN(DumpKind::DumpFuncsToStdOut, "funcs-to-stdout",
2690b57cec5SDimitry Andric "Dump function names to stdout."),
2700b57cec5SDimitry Andric clEnumValN(DumpKind::DumpModsToStdOut, "mods-to-stdout",
2710b57cec5SDimitry Andric "Dump modules to stdout."),
2720b57cec5SDimitry Andric clEnumValN(DumpKind::DumpModsToDisk, "mods-to-disk",
2730b57cec5SDimitry Andric "Dump modules to the current "
2740b57cec5SDimitry Andric "working directory. (WARNING: "
2757a6dacacSDimitry Andric "will overwrite existing files)."),
2767a6dacacSDimitry Andric clEnumValN(DumpKind::DumpDebugDescriptor, "jit-debug-descriptor",
277fe6060f1SDimitry Andric "Dump __jit_debug_descriptor contents to stdout"),
2787a6dacacSDimitry Andric clEnumValN(DumpKind::DumpDebugObjects, "jit-debug-objects",
279fe6060f1SDimitry Andric "Dump __jit_debug_descriptor in-memory debug "
280fe6060f1SDimitry Andric "objects as tool output")),
281fe6060f1SDimitry Andric cl::Hidden);
282fe6060f1SDimitry Andric
2830b57cec5SDimitry Andric ExitOnError ExitOnErr;
2840b57cec5SDimitry Andric }
2850b57cec5SDimitry Andric
linkComponents()286fe6060f1SDimitry Andric LLVM_ATTRIBUTE_USED void linkComponents() {
287fe6060f1SDimitry Andric errs() << (void *)&llvm_orc_registerEHFrameSectionWrapper
288fe6060f1SDimitry Andric << (void *)&llvm_orc_deregisterEHFrameSectionWrapper
28961cfbce3SDimitry Andric << (void *)&llvm_orc_registerJITLoaderGDBWrapper
29061cfbce3SDimitry Andric << (void *)&llvm_orc_registerJITLoaderGDBAllocAction;
291fe6060f1SDimitry Andric }
292fe6060f1SDimitry Andric
2930b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2940b57cec5SDimitry Andric // Object cache
2950b57cec5SDimitry Andric //
2960b57cec5SDimitry Andric // This object cache implementation writes cached objects to disk to the
2970b57cec5SDimitry Andric // directory specified by CacheDir, using a filename provided in the module
2980b57cec5SDimitry Andric // descriptor. The cache tries to load a saved object using that path if the
2990b57cec5SDimitry Andric // file exists. CacheDir defaults to "", in which case objects are cached
3000b57cec5SDimitry Andric // alongside their originating bitcodes.
3010b57cec5SDimitry Andric //
3020b57cec5SDimitry Andric class LLIObjectCache : public ObjectCache {
3030b57cec5SDimitry Andric public:
LLIObjectCache(const std::string & CacheDir)3040b57cec5SDimitry Andric LLIObjectCache(const std::string& CacheDir) : CacheDir(CacheDir) {
3050b57cec5SDimitry Andric // Add trailing '/' to cache dir if necessary.
3060b57cec5SDimitry Andric if (!this->CacheDir.empty() &&
3070b57cec5SDimitry Andric this->CacheDir[this->CacheDir.size() - 1] != '/')
3080b57cec5SDimitry Andric this->CacheDir += '/';
3090b57cec5SDimitry Andric }
~LLIObjectCache()3100b57cec5SDimitry Andric ~LLIObjectCache() override {}
3110b57cec5SDimitry Andric
notifyObjectCompiled(const Module * M,MemoryBufferRef Obj)3120b57cec5SDimitry Andric void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override {
3130b57cec5SDimitry Andric const std::string &ModuleID = M->getModuleIdentifier();
3140b57cec5SDimitry Andric std::string CacheName;
3150b57cec5SDimitry Andric if (!getCacheFilename(ModuleID, CacheName))
3160b57cec5SDimitry Andric return;
3170b57cec5SDimitry Andric if (!CacheDir.empty()) { // Create user-defined cache dir.
3180b57cec5SDimitry Andric SmallString<128> dir(sys::path::parent_path(CacheName));
3190b57cec5SDimitry Andric sys::fs::create_directories(Twine(dir));
3200b57cec5SDimitry Andric }
3215ffd83dbSDimitry Andric
3220b57cec5SDimitry Andric std::error_code EC;
3238bcb0991SDimitry Andric raw_fd_ostream outfile(CacheName, EC, sys::fs::OF_None);
3240b57cec5SDimitry Andric outfile.write(Obj.getBufferStart(), Obj.getBufferSize());
3250b57cec5SDimitry Andric outfile.close();
3260b57cec5SDimitry Andric }
3270b57cec5SDimitry Andric
getObject(const Module * M)3280b57cec5SDimitry Andric std::unique_ptr<MemoryBuffer> getObject(const Module* M) override {
3290b57cec5SDimitry Andric const std::string &ModuleID = M->getModuleIdentifier();
3300b57cec5SDimitry Andric std::string CacheName;
3310b57cec5SDimitry Andric if (!getCacheFilename(ModuleID, CacheName))
3320b57cec5SDimitry Andric return nullptr;
3330b57cec5SDimitry Andric // Load the object from the cache filename
3340b57cec5SDimitry Andric ErrorOr<std::unique_ptr<MemoryBuffer>> IRObjectBuffer =
335fe6060f1SDimitry Andric MemoryBuffer::getFile(CacheName, /*IsText=*/false,
336fe6060f1SDimitry Andric /*RequiresNullTerminator=*/false);
3370b57cec5SDimitry Andric // If the file isn't there, that's OK.
3380b57cec5SDimitry Andric if (!IRObjectBuffer)
3390b57cec5SDimitry Andric return nullptr;
3400b57cec5SDimitry Andric // MCJIT will want to write into this buffer, and we don't want that
3410b57cec5SDimitry Andric // because the file has probably just been mmapped. Instead we make
3420b57cec5SDimitry Andric // a copy. The filed-based buffer will be released when it goes
3430b57cec5SDimitry Andric // out of scope.
3440b57cec5SDimitry Andric return MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer());
3450b57cec5SDimitry Andric }
3460b57cec5SDimitry Andric
3470b57cec5SDimitry Andric private:
3480b57cec5SDimitry Andric std::string CacheDir;
3490b57cec5SDimitry Andric
getCacheFilename(StringRef ModID,std::string & CacheName)350*0fca6ea1SDimitry Andric bool getCacheFilename(StringRef ModID, std::string &CacheName) {
351*0fca6ea1SDimitry Andric if (!ModID.consume_front("file:"))
3520b57cec5SDimitry Andric return false;
3535ffd83dbSDimitry Andric
354*0fca6ea1SDimitry Andric std::string CacheSubdir = std::string(ModID);
355349cc55cSDimitry Andric // Transform "X:\foo" => "/X\foo" for convenience on Windows.
356349cc55cSDimitry Andric if (is_style_windows(llvm::sys::path::Style::native) &&
357349cc55cSDimitry Andric isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') {
3580b57cec5SDimitry Andric CacheSubdir[1] = CacheSubdir[0];
3590b57cec5SDimitry Andric CacheSubdir[0] = '/';
3600b57cec5SDimitry Andric }
3615ffd83dbSDimitry Andric
3620b57cec5SDimitry Andric CacheName = CacheDir + CacheSubdir;
3630b57cec5SDimitry Andric size_t pos = CacheName.rfind('.');
3640b57cec5SDimitry Andric CacheName.replace(pos, CacheName.length() - pos, ".o");
3650b57cec5SDimitry Andric return true;
3660b57cec5SDimitry Andric }
3670b57cec5SDimitry Andric };
3680b57cec5SDimitry Andric
3690b57cec5SDimitry Andric // On Mingw and Cygwin, an external symbol named '__main' is called from the
3700b57cec5SDimitry Andric // generated 'main' function to allow static initialization. To avoid linking
3710b57cec5SDimitry Andric // problems with remote targets (because lli's remote target support does not
3720b57cec5SDimitry Andric // currently handle external linking) we add a secondary module which defines
3730b57cec5SDimitry Andric // an empty '__main' function.
addCygMingExtraModule(ExecutionEngine & EE,LLVMContext & Context,StringRef TargetTripleStr)3740b57cec5SDimitry Andric static void addCygMingExtraModule(ExecutionEngine &EE, LLVMContext &Context,
3750b57cec5SDimitry Andric StringRef TargetTripleStr) {
3760b57cec5SDimitry Andric IRBuilder<> Builder(Context);
3770b57cec5SDimitry Andric Triple TargetTriple(TargetTripleStr);
3780b57cec5SDimitry Andric
3790b57cec5SDimitry Andric // Create a new module.
3808bcb0991SDimitry Andric std::unique_ptr<Module> M = std::make_unique<Module>("CygMingHelper", Context);
3810b57cec5SDimitry Andric M->setTargetTriple(TargetTripleStr);
3820b57cec5SDimitry Andric
3830b57cec5SDimitry Andric // Create an empty function named "__main".
3840b57cec5SDimitry Andric Type *ReturnTy;
3850b57cec5SDimitry Andric if (TargetTriple.isArch64Bit())
3860b57cec5SDimitry Andric ReturnTy = Type::getInt64Ty(Context);
3870b57cec5SDimitry Andric else
3880b57cec5SDimitry Andric ReturnTy = Type::getInt32Ty(Context);
3890b57cec5SDimitry Andric Function *Result =
3900b57cec5SDimitry Andric Function::Create(FunctionType::get(ReturnTy, {}, false),
3910b57cec5SDimitry Andric GlobalValue::ExternalLinkage, "__main", M.get());
3920b57cec5SDimitry Andric
3930b57cec5SDimitry Andric BasicBlock *BB = BasicBlock::Create(Context, "__main", Result);
3940b57cec5SDimitry Andric Builder.SetInsertPoint(BB);
3950b57cec5SDimitry Andric Value *ReturnVal = ConstantInt::get(ReturnTy, 0);
3960b57cec5SDimitry Andric Builder.CreateRet(ReturnVal);
3970b57cec5SDimitry Andric
3980b57cec5SDimitry Andric // Add this new module to the ExecutionEngine.
3990b57cec5SDimitry Andric EE.addModule(std::move(M));
4000b57cec5SDimitry Andric }
4010b57cec5SDimitry Andric
getOptLevel()4025f757f3fSDimitry Andric CodeGenOptLevel getOptLevel() {
403bdd1243dSDimitry Andric if (auto Level = CodeGenOpt::parseLevel(OptLevel))
404bdd1243dSDimitry Andric return *Level;
4050b57cec5SDimitry Andric WithColor::error(errs(), "lli") << "invalid optimization level.\n";
4060b57cec5SDimitry Andric exit(1);
4070b57cec5SDimitry Andric }
4080b57cec5SDimitry Andric
reportError(SMDiagnostic Err,const char * ProgName)409349cc55cSDimitry Andric [[noreturn]] static void reportError(SMDiagnostic Err, const char *ProgName) {
4100b57cec5SDimitry Andric Err.print(ProgName, errs());
4110b57cec5SDimitry Andric exit(1);
4120b57cec5SDimitry Andric }
4130b57cec5SDimitry Andric
4145ffd83dbSDimitry Andric Error loadDylibs();
415fe6060f1SDimitry Andric int runOrcJIT(const char *ProgName);
4160b57cec5SDimitry Andric void disallowOrcOptions();
417349cc55cSDimitry Andric Expected<std::unique_ptr<orc::ExecutorProcessControl>> launchRemote();
4180b57cec5SDimitry Andric
4190b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
4200b57cec5SDimitry Andric // main Driver function
4210b57cec5SDimitry Andric //
main(int argc,char ** argv,char * const * envp)4220b57cec5SDimitry Andric int main(int argc, char **argv, char * const *envp) {
4230b57cec5SDimitry Andric InitLLVM X(argc, argv);
4240b57cec5SDimitry Andric
4250b57cec5SDimitry Andric if (argc > 1)
4260b57cec5SDimitry Andric ExitOnErr.setBanner(std::string(argv[0]) + ": ");
4270b57cec5SDimitry Andric
4280b57cec5SDimitry Andric // If we have a native target, initialize it to ensure it is linked in and
4290b57cec5SDimitry Andric // usable by the JIT.
4300b57cec5SDimitry Andric InitializeNativeTarget();
4310b57cec5SDimitry Andric InitializeNativeTargetAsmPrinter();
4320b57cec5SDimitry Andric InitializeNativeTargetAsmParser();
4330b57cec5SDimitry Andric
4340b57cec5SDimitry Andric cl::ParseCommandLineOptions(argc, argv,
4350b57cec5SDimitry Andric "llvm interpreter & dynamic compiler\n");
4360b57cec5SDimitry Andric
4370b57cec5SDimitry Andric // If the user doesn't want core files, disable them.
4380b57cec5SDimitry Andric if (DisableCoreFiles)
4390b57cec5SDimitry Andric sys::Process::PreventCoreFiles();
4400b57cec5SDimitry Andric
4415ffd83dbSDimitry Andric ExitOnErr(loadDylibs());
4425ffd83dbSDimitry Andric
4435f757f3fSDimitry Andric if (EntryFunc.empty()) {
4445f757f3fSDimitry Andric WithColor::error(errs(), argv[0])
4455f757f3fSDimitry Andric << "--entry-function name cannot be empty\n";
4465f757f3fSDimitry Andric exit(1);
4475f757f3fSDimitry Andric }
4485f757f3fSDimitry Andric
4495f757f3fSDimitry Andric if (UseJITKind == JITKind::MCJIT || ForceInterpreter)
4500b57cec5SDimitry Andric disallowOrcOptions();
451fe6060f1SDimitry Andric else
452fe6060f1SDimitry Andric return runOrcJIT(argv[0]);
4530b57cec5SDimitry Andric
454fe6060f1SDimitry Andric // Old lli implementation based on ExecutionEngine and MCJIT.
4550b57cec5SDimitry Andric LLVMContext Context;
4560b57cec5SDimitry Andric
4570b57cec5SDimitry Andric // Load the bitcode...
4580b57cec5SDimitry Andric SMDiagnostic Err;
4590b57cec5SDimitry Andric std::unique_ptr<Module> Owner = parseIRFile(InputFile, Err, Context);
4600b57cec5SDimitry Andric Module *Mod = Owner.get();
4610b57cec5SDimitry Andric if (!Mod)
4620b57cec5SDimitry Andric reportError(Err, argv[0]);
4630b57cec5SDimitry Andric
4640b57cec5SDimitry Andric if (EnableCacheManager) {
4650b57cec5SDimitry Andric std::string CacheName("file:");
4660b57cec5SDimitry Andric CacheName.append(InputFile);
4670b57cec5SDimitry Andric Mod->setModuleIdentifier(CacheName);
4680b57cec5SDimitry Andric }
4690b57cec5SDimitry Andric
4700b57cec5SDimitry Andric // If not jitting lazily, load the whole bitcode file eagerly too.
4710b57cec5SDimitry Andric if (NoLazyCompilation) {
4720b57cec5SDimitry Andric // Use *argv instead of argv[0] to work around a wrong GCC warning.
4730b57cec5SDimitry Andric ExitOnError ExitOnErr(std::string(*argv) +
4740b57cec5SDimitry Andric ": bitcode didn't read correctly: ");
4750b57cec5SDimitry Andric ExitOnErr(Mod->materializeAll());
4760b57cec5SDimitry Andric }
4770b57cec5SDimitry Andric
4780b57cec5SDimitry Andric std::string ErrorMsg;
4790b57cec5SDimitry Andric EngineBuilder builder(std::move(Owner));
4805ffd83dbSDimitry Andric builder.setMArch(codegen::getMArch());
4815ffd83dbSDimitry Andric builder.setMCPU(codegen::getCPUStr());
4825ffd83dbSDimitry Andric builder.setMAttrs(codegen::getFeatureList());
4835ffd83dbSDimitry Andric if (auto RM = codegen::getExplicitRelocModel())
484bdd1243dSDimitry Andric builder.setRelocationModel(*RM);
4855ffd83dbSDimitry Andric if (auto CM = codegen::getExplicitCodeModel())
486bdd1243dSDimitry Andric builder.setCodeModel(*CM);
4870b57cec5SDimitry Andric builder.setErrorStr(&ErrorMsg);
4880b57cec5SDimitry Andric builder.setEngineKind(ForceInterpreter
4890b57cec5SDimitry Andric ? EngineKind::Interpreter
4900b57cec5SDimitry Andric : EngineKind::JIT);
4910b57cec5SDimitry Andric
4920b57cec5SDimitry Andric // If we are supposed to override the target triple, do so now.
4930b57cec5SDimitry Andric if (!TargetTriple.empty())
4940b57cec5SDimitry Andric Mod->setTargetTriple(Triple::normalize(TargetTriple));
4950b57cec5SDimitry Andric
4960b57cec5SDimitry Andric // Enable MCJIT if desired.
4970b57cec5SDimitry Andric RTDyldMemoryManager *RTDyldMM = nullptr;
4980b57cec5SDimitry Andric if (!ForceInterpreter) {
4990b57cec5SDimitry Andric if (RemoteMCJIT)
5000b57cec5SDimitry Andric RTDyldMM = new ForwardingMemoryManager();
5010b57cec5SDimitry Andric else
5020b57cec5SDimitry Andric RTDyldMM = new SectionMemoryManager();
5030b57cec5SDimitry Andric
5040b57cec5SDimitry Andric // Deliberately construct a temp std::unique_ptr to pass in. Do not null out
5050b57cec5SDimitry Andric // RTDyldMM: We still use it below, even though we don't own it.
5060b57cec5SDimitry Andric builder.setMCJITMemoryManager(
5070b57cec5SDimitry Andric std::unique_ptr<RTDyldMemoryManager>(RTDyldMM));
5080b57cec5SDimitry Andric } else if (RemoteMCJIT) {
5090b57cec5SDimitry Andric WithColor::error(errs(), argv[0])
5100b57cec5SDimitry Andric << "remote process execution does not work with the interpreter.\n";
5110b57cec5SDimitry Andric exit(1);
5120b57cec5SDimitry Andric }
5130b57cec5SDimitry Andric
5140b57cec5SDimitry Andric builder.setOptLevel(getOptLevel());
5150b57cec5SDimitry Andric
516e8d8bef9SDimitry Andric TargetOptions Options =
517e8d8bef9SDimitry Andric codegen::InitTargetOptionsFromCodeGenFlags(Triple(TargetTriple));
5185ffd83dbSDimitry Andric if (codegen::getFloatABIForCalls() != FloatABI::Default)
5195ffd83dbSDimitry Andric Options.FloatABIType = codegen::getFloatABIForCalls();
5200b57cec5SDimitry Andric
5210b57cec5SDimitry Andric builder.setTargetOptions(Options);
5220b57cec5SDimitry Andric
5230b57cec5SDimitry Andric std::unique_ptr<ExecutionEngine> EE(builder.create());
5240b57cec5SDimitry Andric if (!EE) {
5250b57cec5SDimitry Andric if (!ErrorMsg.empty())
5260b57cec5SDimitry Andric WithColor::error(errs(), argv[0])
5270b57cec5SDimitry Andric << "error creating EE: " << ErrorMsg << "\n";
5280b57cec5SDimitry Andric else
5290b57cec5SDimitry Andric WithColor::error(errs(), argv[0]) << "unknown error creating EE!\n";
5300b57cec5SDimitry Andric exit(1);
5310b57cec5SDimitry Andric }
5320b57cec5SDimitry Andric
5330b57cec5SDimitry Andric std::unique_ptr<LLIObjectCache> CacheManager;
5340b57cec5SDimitry Andric if (EnableCacheManager) {
5350b57cec5SDimitry Andric CacheManager.reset(new LLIObjectCache(ObjectCacheDir));
5360b57cec5SDimitry Andric EE->setObjectCache(CacheManager.get());
5370b57cec5SDimitry Andric }
5380b57cec5SDimitry Andric
5390b57cec5SDimitry Andric // Load any additional modules specified on the command line.
5400b57cec5SDimitry Andric for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) {
5410b57cec5SDimitry Andric std::unique_ptr<Module> XMod = parseIRFile(ExtraModules[i], Err, Context);
5420b57cec5SDimitry Andric if (!XMod)
5430b57cec5SDimitry Andric reportError(Err, argv[0]);
5440b57cec5SDimitry Andric if (EnableCacheManager) {
5450b57cec5SDimitry Andric std::string CacheName("file:");
5460b57cec5SDimitry Andric CacheName.append(ExtraModules[i]);
5470b57cec5SDimitry Andric XMod->setModuleIdentifier(CacheName);
5480b57cec5SDimitry Andric }
5490b57cec5SDimitry Andric EE->addModule(std::move(XMod));
5500b57cec5SDimitry Andric }
5510b57cec5SDimitry Andric
5520b57cec5SDimitry Andric for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) {
5530b57cec5SDimitry Andric Expected<object::OwningBinary<object::ObjectFile>> Obj =
5540b57cec5SDimitry Andric object::ObjectFile::createObjectFile(ExtraObjects[i]);
5550b57cec5SDimitry Andric if (!Obj) {
5560b57cec5SDimitry Andric // TODO: Actually report errors helpfully.
5570b57cec5SDimitry Andric consumeError(Obj.takeError());
5580b57cec5SDimitry Andric reportError(Err, argv[0]);
5590b57cec5SDimitry Andric }
5600b57cec5SDimitry Andric object::OwningBinary<object::ObjectFile> &O = Obj.get();
5610b57cec5SDimitry Andric EE->addObjectFile(std::move(O));
5620b57cec5SDimitry Andric }
5630b57cec5SDimitry Andric
5640b57cec5SDimitry Andric for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) {
5650b57cec5SDimitry Andric ErrorOr<std::unique_ptr<MemoryBuffer>> ArBufOrErr =
5660b57cec5SDimitry Andric MemoryBuffer::getFileOrSTDIN(ExtraArchives[i]);
5670b57cec5SDimitry Andric if (!ArBufOrErr)
5680b57cec5SDimitry Andric reportError(Err, argv[0]);
5690b57cec5SDimitry Andric std::unique_ptr<MemoryBuffer> &ArBuf = ArBufOrErr.get();
5700b57cec5SDimitry Andric
5710b57cec5SDimitry Andric Expected<std::unique_ptr<object::Archive>> ArOrErr =
5720b57cec5SDimitry Andric object::Archive::create(ArBuf->getMemBufferRef());
5730b57cec5SDimitry Andric if (!ArOrErr) {
5740b57cec5SDimitry Andric std::string Buf;
5750b57cec5SDimitry Andric raw_string_ostream OS(Buf);
5760b57cec5SDimitry Andric logAllUnhandledErrors(ArOrErr.takeError(), OS);
5770b57cec5SDimitry Andric OS.flush();
5780b57cec5SDimitry Andric errs() << Buf;
5790b57cec5SDimitry Andric exit(1);
5800b57cec5SDimitry Andric }
5810b57cec5SDimitry Andric std::unique_ptr<object::Archive> &Ar = ArOrErr.get();
5820b57cec5SDimitry Andric
5830b57cec5SDimitry Andric object::OwningBinary<object::Archive> OB(std::move(Ar), std::move(ArBuf));
5840b57cec5SDimitry Andric
5850b57cec5SDimitry Andric EE->addArchive(std::move(OB));
5860b57cec5SDimitry Andric }
5870b57cec5SDimitry Andric
5880b57cec5SDimitry Andric // If the target is Cygwin/MingW and we are generating remote code, we
5890b57cec5SDimitry Andric // need an extra module to help out with linking.
5900b57cec5SDimitry Andric if (RemoteMCJIT && Triple(Mod->getTargetTriple()).isOSCygMing()) {
5910b57cec5SDimitry Andric addCygMingExtraModule(*EE, Context, Mod->getTargetTriple());
5920b57cec5SDimitry Andric }
5930b57cec5SDimitry Andric
5940b57cec5SDimitry Andric // The following functions have no effect if their respective profiling
5950b57cec5SDimitry Andric // support wasn't enabled in the build configuration.
5960b57cec5SDimitry Andric EE->RegisterJITEventListener(
5970b57cec5SDimitry Andric JITEventListener::createOProfileJITEventListener());
5980b57cec5SDimitry Andric EE->RegisterJITEventListener(
5990b57cec5SDimitry Andric JITEventListener::createIntelJITEventListener());
6000b57cec5SDimitry Andric if (!RemoteMCJIT)
6010b57cec5SDimitry Andric EE->RegisterJITEventListener(
6020b57cec5SDimitry Andric JITEventListener::createPerfJITEventListener());
6030b57cec5SDimitry Andric
6040b57cec5SDimitry Andric if (!NoLazyCompilation && RemoteMCJIT) {
6050b57cec5SDimitry Andric WithColor::warning(errs(), argv[0])
6060b57cec5SDimitry Andric << "remote mcjit does not support lazy compilation\n";
6070b57cec5SDimitry Andric NoLazyCompilation = true;
6080b57cec5SDimitry Andric }
6090b57cec5SDimitry Andric EE->DisableLazyCompilation(NoLazyCompilation);
6100b57cec5SDimitry Andric
6110b57cec5SDimitry Andric // If the user specifically requested an argv[0] to pass into the program,
6120b57cec5SDimitry Andric // do it now.
6130b57cec5SDimitry Andric if (!FakeArgv0.empty()) {
6140b57cec5SDimitry Andric InputFile = static_cast<std::string>(FakeArgv0);
6150b57cec5SDimitry Andric } else {
6160b57cec5SDimitry Andric // Otherwise, if there is a .bc suffix on the executable strip it off, it
6170b57cec5SDimitry Andric // might confuse the program.
6185f757f3fSDimitry Andric if (StringRef(InputFile).ends_with(".bc"))
6190b57cec5SDimitry Andric InputFile.erase(InputFile.length() - 3);
6200b57cec5SDimitry Andric }
6210b57cec5SDimitry Andric
6220b57cec5SDimitry Andric // Add the module's name to the start of the vector of arguments to main().
6230b57cec5SDimitry Andric InputArgv.insert(InputArgv.begin(), InputFile);
6240b57cec5SDimitry Andric
6250b57cec5SDimitry Andric // Call the main function from M as if its signature were:
6260b57cec5SDimitry Andric // int main (int argc, char **argv, const char **envp)
6270b57cec5SDimitry Andric // using the contents of Args to determine argc & argv, and the contents of
6280b57cec5SDimitry Andric // EnvVars to determine envp.
6290b57cec5SDimitry Andric //
6300b57cec5SDimitry Andric Function *EntryFn = Mod->getFunction(EntryFunc);
6310b57cec5SDimitry Andric if (!EntryFn) {
6320b57cec5SDimitry Andric WithColor::error(errs(), argv[0])
6330b57cec5SDimitry Andric << '\'' << EntryFunc << "\' function not found in module.\n";
6340b57cec5SDimitry Andric return -1;
6350b57cec5SDimitry Andric }
6360b57cec5SDimitry Andric
6370b57cec5SDimitry Andric // Reset errno to zero on entry to main.
6380b57cec5SDimitry Andric errno = 0;
6390b57cec5SDimitry Andric
6400b57cec5SDimitry Andric int Result = -1;
6410b57cec5SDimitry Andric
6420b57cec5SDimitry Andric // Sanity check use of remote-jit: LLI currently only supports use of the
6430b57cec5SDimitry Andric // remote JIT on Unix platforms.
6440b57cec5SDimitry Andric if (RemoteMCJIT) {
6450b57cec5SDimitry Andric #ifndef LLVM_ON_UNIX
6460b57cec5SDimitry Andric WithColor::warning(errs(), argv[0])
6470b57cec5SDimitry Andric << "host does not support external remote targets.\n";
6480b57cec5SDimitry Andric WithColor::note() << "defaulting to local execution\n";
6490b57cec5SDimitry Andric return -1;
6500b57cec5SDimitry Andric #else
6510b57cec5SDimitry Andric if (ChildExecPath.empty()) {
6520b57cec5SDimitry Andric WithColor::error(errs(), argv[0])
6530b57cec5SDimitry Andric << "-remote-mcjit requires -mcjit-remote-process.\n";
6540b57cec5SDimitry Andric exit(1);
6550b57cec5SDimitry Andric } else if (!sys::fs::can_execute(ChildExecPath)) {
6560b57cec5SDimitry Andric WithColor::error(errs(), argv[0])
6570b57cec5SDimitry Andric << "unable to find usable child executable: '" << ChildExecPath
6580b57cec5SDimitry Andric << "'\n";
6590b57cec5SDimitry Andric return -1;
6600b57cec5SDimitry Andric }
6610b57cec5SDimitry Andric #endif
6620b57cec5SDimitry Andric }
6630b57cec5SDimitry Andric
6640b57cec5SDimitry Andric if (!RemoteMCJIT) {
6650b57cec5SDimitry Andric // If the program doesn't explicitly call exit, we will need the Exit
6660b57cec5SDimitry Andric // function later on to make an explicit call, so get the function now.
6670b57cec5SDimitry Andric FunctionCallee Exit = Mod->getOrInsertFunction(
6680b57cec5SDimitry Andric "exit", Type::getVoidTy(Context), Type::getInt32Ty(Context));
6690b57cec5SDimitry Andric
6700b57cec5SDimitry Andric // Run static constructors.
6710b57cec5SDimitry Andric if (!ForceInterpreter) {
6720b57cec5SDimitry Andric // Give MCJIT a chance to apply relocations and set page permissions.
6730b57cec5SDimitry Andric EE->finalizeObject();
6740b57cec5SDimitry Andric }
6750b57cec5SDimitry Andric EE->runStaticConstructorsDestructors(false);
6760b57cec5SDimitry Andric
6770b57cec5SDimitry Andric // Trigger compilation separately so code regions that need to be
6780b57cec5SDimitry Andric // invalidated will be known.
6790b57cec5SDimitry Andric (void)EE->getPointerToFunction(EntryFn);
6800b57cec5SDimitry Andric // Clear instruction cache before code will be executed.
6810b57cec5SDimitry Andric if (RTDyldMM)
6820b57cec5SDimitry Andric static_cast<SectionMemoryManager*>(RTDyldMM)->invalidateInstructionCache();
6830b57cec5SDimitry Andric
6840b57cec5SDimitry Andric // Run main.
6850b57cec5SDimitry Andric Result = EE->runFunctionAsMain(EntryFn, InputArgv, envp);
6860b57cec5SDimitry Andric
6870b57cec5SDimitry Andric // Run static destructors.
6880b57cec5SDimitry Andric EE->runStaticConstructorsDestructors(true);
6890b57cec5SDimitry Andric
6900b57cec5SDimitry Andric // If the program didn't call exit explicitly, we should call it now.
6910b57cec5SDimitry Andric // This ensures that any atexit handlers get called correctly.
6920b57cec5SDimitry Andric if (Function *ExitF =
6930b57cec5SDimitry Andric dyn_cast<Function>(Exit.getCallee()->stripPointerCasts())) {
6940b57cec5SDimitry Andric if (ExitF->getFunctionType() == Exit.getFunctionType()) {
6950b57cec5SDimitry Andric std::vector<GenericValue> Args;
6960b57cec5SDimitry Andric GenericValue ResultGV;
6970b57cec5SDimitry Andric ResultGV.IntVal = APInt(32, Result);
6980b57cec5SDimitry Andric Args.push_back(ResultGV);
6990b57cec5SDimitry Andric EE->runFunction(ExitF, Args);
7000b57cec5SDimitry Andric WithColor::error(errs(), argv[0])
7010b57cec5SDimitry Andric << "exit(" << Result << ") returned!\n";
7020b57cec5SDimitry Andric abort();
7030b57cec5SDimitry Andric }
7040b57cec5SDimitry Andric }
7050b57cec5SDimitry Andric WithColor::error(errs(), argv[0]) << "exit defined with wrong prototype!\n";
7060b57cec5SDimitry Andric abort();
7070b57cec5SDimitry Andric } else {
7080b57cec5SDimitry Andric // else == "if (RemoteMCJIT)"
70906c3fb27SDimitry Andric std::unique_ptr<orc::ExecutorProcessControl> EPC = ExitOnErr(launchRemote());
7100b57cec5SDimitry Andric
7110b57cec5SDimitry Andric // Remote target MCJIT doesn't (yet) support static constructors. No reason
7120b57cec5SDimitry Andric // it couldn't. This is a limitation of the LLI implementation, not the
7130b57cec5SDimitry Andric // MCJIT itself. FIXME.
7140b57cec5SDimitry Andric
7150b57cec5SDimitry Andric // Create a remote memory manager.
716349cc55cSDimitry Andric auto RemoteMM = ExitOnErr(
717349cc55cSDimitry Andric orc::EPCGenericRTDyldMemoryManager::CreateWithDefaultBootstrapSymbols(
718349cc55cSDimitry Andric *EPC));
7190b57cec5SDimitry Andric
7200b57cec5SDimitry Andric // Forward MCJIT's memory manager calls to the remote memory manager.
7210b57cec5SDimitry Andric static_cast<ForwardingMemoryManager*>(RTDyldMM)->setMemMgr(
7220b57cec5SDimitry Andric std::move(RemoteMM));
7230b57cec5SDimitry Andric
7240b57cec5SDimitry Andric // Forward MCJIT's symbol resolution calls to the remote.
7250b57cec5SDimitry Andric static_cast<ForwardingMemoryManager *>(RTDyldMM)->setResolver(
726349cc55cSDimitry Andric ExitOnErr(RemoteResolver::Create(*EPC)));
7270b57cec5SDimitry Andric // Grab the target address of the JIT'd main function on the remote and call
7280b57cec5SDimitry Andric // it.
7290b57cec5SDimitry Andric // FIXME: argv and envp handling.
730349cc55cSDimitry Andric auto Entry =
731349cc55cSDimitry Andric orc::ExecutorAddr(EE->getFunctionAddress(EntryFn->getName().str()));
7320b57cec5SDimitry Andric EE->finalizeObject();
7330b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Executing '" << EntryFn->getName() << "' at 0x"
734349cc55cSDimitry Andric << format("%llx", Entry.getValue()) << "\n");
735349cc55cSDimitry Andric Result = ExitOnErr(EPC->runAsMain(Entry, {}));
7360b57cec5SDimitry Andric
7370b57cec5SDimitry Andric // Like static constructors, the remote target MCJIT support doesn't handle
7380b57cec5SDimitry Andric // this yet. It could. FIXME.
7390b57cec5SDimitry Andric
7400b57cec5SDimitry Andric // Delete the EE - we need to tear it down *before* we terminate the session
7410b57cec5SDimitry Andric // with the remote, otherwise it'll crash when it tries to release resources
7420b57cec5SDimitry Andric // on a remote that has already been disconnected.
7430b57cec5SDimitry Andric EE.reset();
7440b57cec5SDimitry Andric
7450b57cec5SDimitry Andric // Signal the remote target that we're done JITing.
746349cc55cSDimitry Andric ExitOnErr(EPC->disconnect());
7470b57cec5SDimitry Andric }
7480b57cec5SDimitry Andric
7490b57cec5SDimitry Andric return Result;
7500b57cec5SDimitry Andric }
7510b57cec5SDimitry Andric
7527a6dacacSDimitry Andric // JITLink debug support plugins put information about JITed code in this GDB
7537a6dacacSDimitry Andric // JIT Interface global from OrcTargetProcess.
7547a6dacacSDimitry Andric extern "C" struct jit_descriptor __jit_debug_descriptor;
7557a6dacacSDimitry Andric
7567a6dacacSDimitry Andric static struct jit_code_entry *
findNextDebugDescriptorEntry(struct jit_code_entry * Latest)7577a6dacacSDimitry Andric findNextDebugDescriptorEntry(struct jit_code_entry *Latest) {
7587a6dacacSDimitry Andric if (Latest == nullptr)
7597a6dacacSDimitry Andric return __jit_debug_descriptor.first_entry;
7607a6dacacSDimitry Andric if (Latest->next_entry)
7617a6dacacSDimitry Andric return Latest->next_entry;
7627a6dacacSDimitry Andric return nullptr;
7637a6dacacSDimitry Andric }
7647a6dacacSDimitry Andric
claimToolOutput()7657a6dacacSDimitry Andric static ToolOutputFile &claimToolOutput() {
7667a6dacacSDimitry Andric static std::unique_ptr<ToolOutputFile> ToolOutput = nullptr;
7677a6dacacSDimitry Andric if (ToolOutput) {
7687a6dacacSDimitry Andric WithColor::error(errs(), "lli")
7697a6dacacSDimitry Andric << "Can not claim stdout for tool output twice\n";
7707a6dacacSDimitry Andric exit(1);
7717a6dacacSDimitry Andric }
7727a6dacacSDimitry Andric std::error_code EC;
7737a6dacacSDimitry Andric ToolOutput = std::make_unique<ToolOutputFile>("-", EC, sys::fs::OF_None);
7747a6dacacSDimitry Andric if (EC) {
7757a6dacacSDimitry Andric WithColor::error(errs(), "lli")
7767a6dacacSDimitry Andric << "Failed to create tool output file: " << EC.message() << "\n";
7777a6dacacSDimitry Andric exit(1);
7787a6dacacSDimitry Andric }
7797a6dacacSDimitry Andric return *ToolOutput;
7807a6dacacSDimitry Andric }
7817a6dacacSDimitry Andric
createIRDebugDumper()7827a6dacacSDimitry Andric static std::function<void(Module &)> createIRDebugDumper() {
7830b57cec5SDimitry Andric switch (OrcDumpKind) {
7840b57cec5SDimitry Andric case DumpKind::NoDump:
7857a6dacacSDimitry Andric case DumpKind::DumpDebugDescriptor:
7867a6dacacSDimitry Andric case DumpKind::DumpDebugObjects:
7878bcb0991SDimitry Andric return [](Module &M) {};
7880b57cec5SDimitry Andric
7890b57cec5SDimitry Andric case DumpKind::DumpFuncsToStdOut:
7908bcb0991SDimitry Andric return [](Module &M) {
7910b57cec5SDimitry Andric printf("[ ");
7920b57cec5SDimitry Andric
7938bcb0991SDimitry Andric for (const auto &F : M) {
7940b57cec5SDimitry Andric if (F.isDeclaration())
7950b57cec5SDimitry Andric continue;
7960b57cec5SDimitry Andric
7970b57cec5SDimitry Andric if (F.hasName()) {
7985ffd83dbSDimitry Andric std::string Name(std::string(F.getName()));
7990b57cec5SDimitry Andric printf("%s ", Name.c_str());
8000b57cec5SDimitry Andric } else
8010b57cec5SDimitry Andric printf("<anon> ");
8020b57cec5SDimitry Andric }
8030b57cec5SDimitry Andric
8040b57cec5SDimitry Andric printf("]\n");
8050b57cec5SDimitry Andric };
8060b57cec5SDimitry Andric
8070b57cec5SDimitry Andric case DumpKind::DumpModsToStdOut:
8088bcb0991SDimitry Andric return [](Module &M) {
8098bcb0991SDimitry Andric outs() << "----- Module Start -----\n" << M << "----- Module End -----\n";
8100b57cec5SDimitry Andric };
8110b57cec5SDimitry Andric
8120b57cec5SDimitry Andric case DumpKind::DumpModsToDisk:
8138bcb0991SDimitry Andric return [](Module &M) {
8140b57cec5SDimitry Andric std::error_code EC;
815fe6060f1SDimitry Andric raw_fd_ostream Out(M.getModuleIdentifier() + ".ll", EC,
816fe6060f1SDimitry Andric sys::fs::OF_TextWithCRLF);
8170b57cec5SDimitry Andric if (EC) {
8188bcb0991SDimitry Andric errs() << "Couldn't open " << M.getModuleIdentifier()
8190b57cec5SDimitry Andric << " for dumping.\nError:" << EC.message() << "\n";
8200b57cec5SDimitry Andric exit(1);
8210b57cec5SDimitry Andric }
8228bcb0991SDimitry Andric Out << M;
8230b57cec5SDimitry Andric };
8240b57cec5SDimitry Andric }
8250b57cec5SDimitry Andric llvm_unreachable("Unknown DumpKind");
8260b57cec5SDimitry Andric }
8270b57cec5SDimitry Andric
createObjDebugDumper()8287a6dacacSDimitry Andric static std::function<void(MemoryBuffer &)> createObjDebugDumper() {
8297a6dacacSDimitry Andric switch (OrcDumpKind) {
8307a6dacacSDimitry Andric case DumpKind::NoDump:
8317a6dacacSDimitry Andric case DumpKind::DumpFuncsToStdOut:
8327a6dacacSDimitry Andric case DumpKind::DumpModsToStdOut:
8337a6dacacSDimitry Andric case DumpKind::DumpModsToDisk:
8347a6dacacSDimitry Andric return [](MemoryBuffer &) {};
8357a6dacacSDimitry Andric
8367a6dacacSDimitry Andric case DumpKind::DumpDebugDescriptor: {
8377a6dacacSDimitry Andric // Dump the empty descriptor at startup once
8387a6dacacSDimitry Andric fprintf(stderr, "jit_debug_descriptor 0x%016" PRIx64 "\n",
8397a6dacacSDimitry Andric pointerToJITTargetAddress(__jit_debug_descriptor.first_entry));
8407a6dacacSDimitry Andric return [](MemoryBuffer &) {
8417a6dacacSDimitry Andric // Dump new entries as they appear
8427a6dacacSDimitry Andric static struct jit_code_entry *Latest = nullptr;
8437a6dacacSDimitry Andric while (auto *NewEntry = findNextDebugDescriptorEntry(Latest)) {
8447a6dacacSDimitry Andric fprintf(stderr, "jit_debug_descriptor 0x%016" PRIx64 "\n",
8457a6dacacSDimitry Andric pointerToJITTargetAddress(NewEntry));
8467a6dacacSDimitry Andric Latest = NewEntry;
8477a6dacacSDimitry Andric }
8487a6dacacSDimitry Andric };
8497a6dacacSDimitry Andric }
8507a6dacacSDimitry Andric
8517a6dacacSDimitry Andric case DumpKind::DumpDebugObjects: {
8527a6dacacSDimitry Andric return [](MemoryBuffer &Obj) {
8537a6dacacSDimitry Andric static struct jit_code_entry *Latest = nullptr;
8547a6dacacSDimitry Andric static ToolOutputFile &ToolOutput = claimToolOutput();
8557a6dacacSDimitry Andric while (auto *NewEntry = findNextDebugDescriptorEntry(Latest)) {
8567a6dacacSDimitry Andric ToolOutput.os().write(NewEntry->symfile_addr, NewEntry->symfile_size);
8577a6dacacSDimitry Andric Latest = NewEntry;
8587a6dacacSDimitry Andric }
8597a6dacacSDimitry Andric };
8607a6dacacSDimitry Andric }
8617a6dacacSDimitry Andric }
8627a6dacacSDimitry Andric llvm_unreachable("Unknown DumpKind");
8637a6dacacSDimitry Andric }
8647a6dacacSDimitry Andric
loadDylibs()8655ffd83dbSDimitry Andric Error loadDylibs() {
8665ffd83dbSDimitry Andric for (const auto &Dylib : Dylibs) {
8675ffd83dbSDimitry Andric std::string ErrMsg;
8685ffd83dbSDimitry Andric if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg))
8695ffd83dbSDimitry Andric return make_error<StringError>(ErrMsg, inconvertibleErrorCode());
8705ffd83dbSDimitry Andric }
8715ffd83dbSDimitry Andric
8725ffd83dbSDimitry Andric return Error::success();
8735ffd83dbSDimitry Andric }
8745ffd83dbSDimitry Andric
exitOnLazyCallThroughFailure()8750b57cec5SDimitry Andric static void exitOnLazyCallThroughFailure() { exit(1); }
8760b57cec5SDimitry Andric
8775ffd83dbSDimitry Andric Expected<orc::ThreadSafeModule>
loadModule(StringRef Path,orc::ThreadSafeContext TSCtx)8785ffd83dbSDimitry Andric loadModule(StringRef Path, orc::ThreadSafeContext TSCtx) {
8795ffd83dbSDimitry Andric SMDiagnostic Err;
8805ffd83dbSDimitry Andric auto M = parseIRFile(Path, Err, *TSCtx.getContext());
8815ffd83dbSDimitry Andric if (!M) {
8825ffd83dbSDimitry Andric std::string ErrMsg;
8835ffd83dbSDimitry Andric {
8845ffd83dbSDimitry Andric raw_string_ostream ErrMsgStream(ErrMsg);
8855ffd83dbSDimitry Andric Err.print("lli", ErrMsgStream);
8865ffd83dbSDimitry Andric }
8875ffd83dbSDimitry Andric return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
8885ffd83dbSDimitry Andric }
8895ffd83dbSDimitry Andric
8905ffd83dbSDimitry Andric if (EnableCacheManager)
8915ffd83dbSDimitry Andric M->setModuleIdentifier("file:" + M->getModuleIdentifier());
8925ffd83dbSDimitry Andric
8935ffd83dbSDimitry Andric return orc::ThreadSafeModule(std::move(M), std::move(TSCtx));
8945ffd83dbSDimitry Andric }
8955ffd83dbSDimitry Andric
mingw_noop_main(void)89606c3fb27SDimitry Andric int mingw_noop_main(void) {
89706c3fb27SDimitry Andric // Cygwin and MinGW insert calls from the main function to the runtime
89806c3fb27SDimitry Andric // function __main. The __main function is responsible for setting up main's
89906c3fb27SDimitry Andric // environment (e.g. running static constructors), however this is not needed
90006c3fb27SDimitry Andric // when running under lli: the executor process will have run non-JIT ctors,
90106c3fb27SDimitry Andric // and ORC will take care of running JIT'd ctors. To avoid a missing symbol
90206c3fb27SDimitry Andric // error we just implement __main as a no-op.
90306c3fb27SDimitry Andric //
90406c3fb27SDimitry Andric // FIXME: Move this to ORC-RT (and the ORC-RT substitution library once it
90506c3fb27SDimitry Andric // exists). That will allow it to work out-of-process, and for all
90606c3fb27SDimitry Andric // ORC tools (the problem isn't lli specific).
90706c3fb27SDimitry Andric return 0;
90806c3fb27SDimitry Andric }
90906c3fb27SDimitry Andric
9105f757f3fSDimitry Andric // Try to enable debugger support for the given instance.
9115f757f3fSDimitry Andric // This alway returns success, but prints a warning if it's not able to enable
9125f757f3fSDimitry Andric // debugger support.
tryEnableDebugSupport(orc::LLJIT & J)9135f757f3fSDimitry Andric Error tryEnableDebugSupport(orc::LLJIT &J) {
9145f757f3fSDimitry Andric if (auto Err = enableDebuggerSupport(J)) {
9155f757f3fSDimitry Andric [[maybe_unused]] std::string ErrMsg = toString(std::move(Err));
9165f757f3fSDimitry Andric LLVM_DEBUG(dbgs() << "lli: " << ErrMsg << "\n");
9175f757f3fSDimitry Andric }
9185f757f3fSDimitry Andric return Error::success();
9195f757f3fSDimitry Andric }
9205f757f3fSDimitry Andric
runOrcJIT(const char * ProgName)921fe6060f1SDimitry Andric int runOrcJIT(const char *ProgName) {
9220b57cec5SDimitry Andric // Start setting up the JIT environment.
9230b57cec5SDimitry Andric
9240b57cec5SDimitry Andric // Parse the main module.
9258bcb0991SDimitry Andric orc::ThreadSafeContext TSCtx(std::make_unique<LLVMContext>());
9265ffd83dbSDimitry Andric auto MainModule = ExitOnErr(loadModule(InputFile, TSCtx));
9270b57cec5SDimitry Andric
9285ffd83dbSDimitry Andric // Get TargetTriple and DataLayout from the main module if they're explicitly
9295ffd83dbSDimitry Andric // set.
930bdd1243dSDimitry Andric std::optional<Triple> TT;
931bdd1243dSDimitry Andric std::optional<DataLayout> DL;
9325ffd83dbSDimitry Andric MainModule.withModuleDo([&](Module &M) {
9335ffd83dbSDimitry Andric if (!M.getTargetTriple().empty())
9345ffd83dbSDimitry Andric TT = Triple(M.getTargetTriple());
9355ffd83dbSDimitry Andric if (!M.getDataLayout().isDefault())
9365ffd83dbSDimitry Andric DL = M.getDataLayout();
9375ffd83dbSDimitry Andric });
9385ffd83dbSDimitry Andric
9390b57cec5SDimitry Andric orc::LLLazyJITBuilder Builder;
9400b57cec5SDimitry Andric
9410b57cec5SDimitry Andric Builder.setJITTargetMachineBuilder(
9425ffd83dbSDimitry Andric TT ? orc::JITTargetMachineBuilder(*TT)
9435ffd83dbSDimitry Andric : ExitOnErr(orc::JITTargetMachineBuilder::detectHost()));
9440b57cec5SDimitry Andric
9455ffd83dbSDimitry Andric TT = Builder.getJITTargetMachineBuilder()->getTargetTriple();
9465ffd83dbSDimitry Andric if (DL)
9475ffd83dbSDimitry Andric Builder.setDataLayout(DL);
9485ffd83dbSDimitry Andric
9495ffd83dbSDimitry Andric if (!codegen::getMArch().empty())
9505ffd83dbSDimitry Andric Builder.getJITTargetMachineBuilder()->getTargetTriple().setArchName(
9515ffd83dbSDimitry Andric codegen::getMArch());
9520b57cec5SDimitry Andric
9530b57cec5SDimitry Andric Builder.getJITTargetMachineBuilder()
9545ffd83dbSDimitry Andric ->setCPU(codegen::getCPUStr())
9555ffd83dbSDimitry Andric .addFeatures(codegen::getFeatureList())
9565ffd83dbSDimitry Andric .setRelocationModel(codegen::getExplicitRelocModel())
9575ffd83dbSDimitry Andric .setCodeModel(codegen::getExplicitCodeModel());
9580b57cec5SDimitry Andric
95906c3fb27SDimitry Andric // Link process symbols unless NoProcessSymbols is set.
96006c3fb27SDimitry Andric Builder.setLinkProcessSymbolsByDefault(!NoProcessSymbols);
96106c3fb27SDimitry Andric
962fe6060f1SDimitry Andric // FIXME: Setting a dummy call-through manager in non-lazy mode prevents the
963fe6060f1SDimitry Andric // JIT builder to instantiate a default (which would fail with an error for
964fe6060f1SDimitry Andric // unsupported architectures).
965fe6060f1SDimitry Andric if (UseJITKind != JITKind::OrcLazy) {
966fe6060f1SDimitry Andric auto ES = std::make_unique<orc::ExecutionSession>(
967fe6060f1SDimitry Andric ExitOnErr(orc::SelfExecutorProcessControl::Create()));
968fe6060f1SDimitry Andric Builder.setLazyCallthroughManager(
96906c3fb27SDimitry Andric std::make_unique<orc::LazyCallThroughManager>(*ES, orc::ExecutorAddr(),
97006c3fb27SDimitry Andric nullptr));
971fe6060f1SDimitry Andric Builder.setExecutionSession(std::move(ES));
972fe6060f1SDimitry Andric }
973fe6060f1SDimitry Andric
9740b57cec5SDimitry Andric Builder.setLazyCompileFailureAddr(
97581ad6265SDimitry Andric orc::ExecutorAddr::fromPtr(exitOnLazyCallThroughFailure));
9760b57cec5SDimitry Andric Builder.setNumCompileThreads(LazyJITCompileThreads);
9770b57cec5SDimitry Andric
9785ffd83dbSDimitry Andric // If the object cache is enabled then set a custom compile function
9795ffd83dbSDimitry Andric // creator to use the cache.
9805ffd83dbSDimitry Andric std::unique_ptr<LLIObjectCache> CacheManager;
9815ffd83dbSDimitry Andric if (EnableCacheManager) {
9825ffd83dbSDimitry Andric
9835ffd83dbSDimitry Andric CacheManager = std::make_unique<LLIObjectCache>(ObjectCacheDir);
9845ffd83dbSDimitry Andric
9855ffd83dbSDimitry Andric Builder.setCompileFunctionCreator(
9865ffd83dbSDimitry Andric [&](orc::JITTargetMachineBuilder JTMB)
9875ffd83dbSDimitry Andric -> Expected<std::unique_ptr<orc::IRCompileLayer::IRCompiler>> {
9885ffd83dbSDimitry Andric if (LazyJITCompileThreads > 0)
9895ffd83dbSDimitry Andric return std::make_unique<orc::ConcurrentIRCompiler>(std::move(JTMB),
9905ffd83dbSDimitry Andric CacheManager.get());
9915ffd83dbSDimitry Andric
9925ffd83dbSDimitry Andric auto TM = JTMB.createTargetMachine();
9935ffd83dbSDimitry Andric if (!TM)
9945ffd83dbSDimitry Andric return TM.takeError();
9955ffd83dbSDimitry Andric
9965ffd83dbSDimitry Andric return std::make_unique<orc::TMOwningSimpleCompiler>(std::move(*TM),
9975ffd83dbSDimitry Andric CacheManager.get());
9985ffd83dbSDimitry Andric });
9995ffd83dbSDimitry Andric }
10005ffd83dbSDimitry Andric
10015f757f3fSDimitry Andric // Enable debugging of JIT'd code (only works on JITLink for ELF and MachO).
10025f757f3fSDimitry Andric Builder.setPrePlatformSetup(tryEnableDebugSupport);
10035f757f3fSDimitry Andric
10045ffd83dbSDimitry Andric // Set up LLJIT platform.
10055ffd83dbSDimitry Andric LLJITPlatform P = Platform;
100606c3fb27SDimitry Andric if (P == LLJITPlatform::Auto)
100706c3fb27SDimitry Andric P = OrcRuntime.empty() ? LLJITPlatform::GenericIR
100806c3fb27SDimitry Andric : LLJITPlatform::ExecutorNative;
100906c3fb27SDimitry Andric
10105ffd83dbSDimitry Andric switch (P) {
101106c3fb27SDimitry Andric case LLJITPlatform::ExecutorNative: {
101206c3fb27SDimitry Andric Builder.setPlatformSetUp(orc::ExecutorNativePlatform(OrcRuntime));
101381ad6265SDimitry Andric break;
101406c3fb27SDimitry Andric }
10155ffd83dbSDimitry Andric case LLJITPlatform::GenericIR:
10165ffd83dbSDimitry Andric // Nothing to do: LLJITBuilder will use this by default.
10175ffd83dbSDimitry Andric break;
1018fe6060f1SDimitry Andric case LLJITPlatform::Inactive:
1019fe6060f1SDimitry Andric Builder.setPlatformSetUp(orc::setUpInactivePlatform);
10205ffd83dbSDimitry Andric break;
10215ffd83dbSDimitry Andric default:
10225ffd83dbSDimitry Andric llvm_unreachable("Unrecognized platform value");
10235ffd83dbSDimitry Andric }
10245ffd83dbSDimitry Andric
1025fe6060f1SDimitry Andric std::unique_ptr<orc::ExecutorProcessControl> EPC = nullptr;
1026fe6060f1SDimitry Andric if (JITLinker == JITLinkerKind::JITLink) {
1027fe6060f1SDimitry Andric EPC = ExitOnErr(orc::SelfExecutorProcessControl::Create(
1028fe6060f1SDimitry Andric std::make_shared<orc::SymbolStringPool>()));
1029fe6060f1SDimitry Andric
10301db9f3b2SDimitry Andric Builder.getJITTargetMachineBuilder()
10311db9f3b2SDimitry Andric ->setRelocationModel(Reloc::PIC_)
10321db9f3b2SDimitry Andric .setCodeModel(CodeModel::Small);
10331db9f3b2SDimitry Andric Builder.setObjectLinkingLayerCreator([&P](orc::ExecutionSession &ES,
103481ad6265SDimitry Andric const Triple &TT) {
10351db9f3b2SDimitry Andric auto L = std::make_unique<orc::ObjectLinkingLayer>(ES);
103606c3fb27SDimitry Andric if (P != LLJITPlatform::ExecutorNative)
1037fe6060f1SDimitry Andric L->addPlugin(std::make_unique<orc::EHFrameRegistrationPlugin>(
1038fe6060f1SDimitry Andric ES, ExitOnErr(orc::EPCEHFrameRegistrar::Create(ES))));
1039fe6060f1SDimitry Andric return L;
1040fe6060f1SDimitry Andric });
1041fe6060f1SDimitry Andric }
1042fe6060f1SDimitry Andric
10430b57cec5SDimitry Andric auto J = ExitOnErr(Builder.create());
10440b57cec5SDimitry Andric
1045fe6060f1SDimitry Andric auto *ObjLayer = &J->getObjLinkingLayer();
104606c3fb27SDimitry Andric if (auto *RTDyldObjLayer = dyn_cast<orc::RTDyldObjectLinkingLayer>(ObjLayer)) {
1047fe6060f1SDimitry Andric RTDyldObjLayer->registerJITEventListener(
10485ffd83dbSDimitry Andric *JITEventListener::createGDBRegistrationListener());
104906c3fb27SDimitry Andric #if LLVM_USE_OPROFILE
105006c3fb27SDimitry Andric RTDyldObjLayer->registerJITEventListener(
105106c3fb27SDimitry Andric *JITEventListener::createOProfileJITEventListener());
105206c3fb27SDimitry Andric #endif
105306c3fb27SDimitry Andric #if LLVM_USE_INTEL_JITEVENTS
105406c3fb27SDimitry Andric RTDyldObjLayer->registerJITEventListener(
105506c3fb27SDimitry Andric *JITEventListener::createIntelJITEventListener());
105606c3fb27SDimitry Andric #endif
105706c3fb27SDimitry Andric #if LLVM_USE_PERF
105806c3fb27SDimitry Andric RTDyldObjLayer->registerJITEventListener(
105906c3fb27SDimitry Andric *JITEventListener::createPerfJITEventListener());
106006c3fb27SDimitry Andric #endif
106106c3fb27SDimitry Andric }
10625ffd83dbSDimitry Andric
10630b57cec5SDimitry Andric if (PerModuleLazy)
10640b57cec5SDimitry Andric J->setPartitionFunction(orc::CompileOnDemandLayer::compileWholeModule);
10650b57cec5SDimitry Andric
10667a6dacacSDimitry Andric auto IRDump = createIRDebugDumper();
10675ffd83dbSDimitry Andric J->getIRTransformLayer().setTransform(
10685ffd83dbSDimitry Andric [&](orc::ThreadSafeModule TSM,
10690b57cec5SDimitry Andric const orc::MaterializationResponsibility &R) {
10708bcb0991SDimitry Andric TSM.withModuleDo([&](Module &M) {
10718bcb0991SDimitry Andric if (verifyModule(M, &dbgs())) {
10728bcb0991SDimitry Andric dbgs() << "Bad module: " << &M << "\n";
10730b57cec5SDimitry Andric exit(1);
10740b57cec5SDimitry Andric }
10757a6dacacSDimitry Andric IRDump(M);
10760b57cec5SDimitry Andric });
10778bcb0991SDimitry Andric return TSM;
10788bcb0991SDimitry Andric });
10790b57cec5SDimitry Andric
10807a6dacacSDimitry Andric auto ObjDump = createObjDebugDumper();
10817a6dacacSDimitry Andric J->getObjTransformLayer().setTransform(
10827a6dacacSDimitry Andric [&](std::unique_ptr<MemoryBuffer> Obj)
10837a6dacacSDimitry Andric -> Expected<std::unique_ptr<MemoryBuffer>> {
10847a6dacacSDimitry Andric ObjDump(*Obj);
10857a6dacacSDimitry Andric return std::move(Obj);
10867a6dacacSDimitry Andric });
1087fe6060f1SDimitry Andric
108806c3fb27SDimitry Andric // If this is a Mingw or Cygwin executor then we need to alias __main to
108906c3fb27SDimitry Andric // orc_rt_int_void_return_0.
109006c3fb27SDimitry Andric if (J->getTargetTriple().isOSCygMing())
109106c3fb27SDimitry Andric ExitOnErr(J->getProcessSymbolsJITDylib()->define(
109206c3fb27SDimitry Andric orc::absoluteSymbols({{J->mangleAndIntern("__main"),
109306c3fb27SDimitry Andric {orc::ExecutorAddr::fromPtr(mingw_noop_main),
109406c3fb27SDimitry Andric JITSymbolFlags::Exported}}})));
109581ad6265SDimitry Andric
1096fe6060f1SDimitry Andric // Regular modules are greedy: They materialize as a whole and trigger
1097fe6060f1SDimitry Andric // materialization for all required symbols recursively. Lazy modules go
1098fe6060f1SDimitry Andric // through partitioning and they replace outgoing calls with reexport stubs
1099fe6060f1SDimitry Andric // that resolve on call-through.
1100fe6060f1SDimitry Andric auto AddModule = [&](orc::JITDylib &JD, orc::ThreadSafeModule M) {
1101fe6060f1SDimitry Andric return UseJITKind == JITKind::OrcLazy ? J->addLazyIRModule(JD, std::move(M))
1102fe6060f1SDimitry Andric : J->addIRModule(JD, std::move(M));
1103fe6060f1SDimitry Andric };
1104fe6060f1SDimitry Andric
11050b57cec5SDimitry Andric // Add the main module.
1106fe6060f1SDimitry Andric ExitOnErr(AddModule(J->getMainJITDylib(), std::move(MainModule)));
11070b57cec5SDimitry Andric
11080b57cec5SDimitry Andric // Create JITDylibs and add any extra modules.
11090b57cec5SDimitry Andric {
11100b57cec5SDimitry Andric // Create JITDylibs, keep a map from argument index to dylib. We will use
11110b57cec5SDimitry Andric // -extra-module argument indexes to determine what dylib to use for each
11120b57cec5SDimitry Andric // -extra-module.
11130b57cec5SDimitry Andric std::map<unsigned, orc::JITDylib *> IdxToDylib;
11140b57cec5SDimitry Andric IdxToDylib[0] = &J->getMainJITDylib();
11150b57cec5SDimitry Andric for (auto JDItr = JITDylibs.begin(), JDEnd = JITDylibs.end();
11160b57cec5SDimitry Andric JDItr != JDEnd; ++JDItr) {
11170b57cec5SDimitry Andric orc::JITDylib *JD = J->getJITDylibByName(*JDItr);
11185ffd83dbSDimitry Andric if (!JD) {
11195ffd83dbSDimitry Andric JD = &ExitOnErr(J->createJITDylib(*JDItr));
11205ffd83dbSDimitry Andric J->getMainJITDylib().addToLinkOrder(*JD);
11215ffd83dbSDimitry Andric JD->addToLinkOrder(J->getMainJITDylib());
11225ffd83dbSDimitry Andric }
11230b57cec5SDimitry Andric IdxToDylib[JITDylibs.getPosition(JDItr - JITDylibs.begin())] = JD;
11240b57cec5SDimitry Andric }
11250b57cec5SDimitry Andric
11260b57cec5SDimitry Andric for (auto EMItr = ExtraModules.begin(), EMEnd = ExtraModules.end();
11270b57cec5SDimitry Andric EMItr != EMEnd; ++EMItr) {
11285ffd83dbSDimitry Andric auto M = ExitOnErr(loadModule(*EMItr, TSCtx));
11290b57cec5SDimitry Andric
11300b57cec5SDimitry Andric auto EMIdx = ExtraModules.getPosition(EMItr - ExtraModules.begin());
11310b57cec5SDimitry Andric assert(EMIdx != 0 && "ExtraModule should have index > 0");
11320b57cec5SDimitry Andric auto JDItr = std::prev(IdxToDylib.lower_bound(EMIdx));
11330b57cec5SDimitry Andric auto &JD = *JDItr->second;
1134fe6060f1SDimitry Andric ExitOnErr(AddModule(JD, std::move(M)));
11350b57cec5SDimitry Andric }
11368bcb0991SDimitry Andric
11378bcb0991SDimitry Andric for (auto EAItr = ExtraArchives.begin(), EAEnd = ExtraArchives.end();
11388bcb0991SDimitry Andric EAItr != EAEnd; ++EAItr) {
11398bcb0991SDimitry Andric auto EAIdx = ExtraArchives.getPosition(EAItr - ExtraArchives.begin());
11408bcb0991SDimitry Andric assert(EAIdx != 0 && "ExtraArchive should have index > 0");
11418bcb0991SDimitry Andric auto JDItr = std::prev(IdxToDylib.lower_bound(EAIdx));
11428bcb0991SDimitry Andric auto &JD = *JDItr->second;
114306c3fb27SDimitry Andric ExitOnErr(J->linkStaticLibraryInto(JD, EAItr->c_str()));
11448bcb0991SDimitry Andric }
11450b57cec5SDimitry Andric }
11460b57cec5SDimitry Andric
11470b57cec5SDimitry Andric // Add the objects.
11480b57cec5SDimitry Andric for (auto &ObjPath : ExtraObjects) {
11490b57cec5SDimitry Andric auto Obj = ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ObjPath)));
11500b57cec5SDimitry Andric ExitOnErr(J->addObjectFile(std::move(Obj)));
11510b57cec5SDimitry Andric }
11520b57cec5SDimitry Andric
11530b57cec5SDimitry Andric // Run any static constructors.
11545ffd83dbSDimitry Andric ExitOnErr(J->initialize(J->getMainJITDylib()));
11550b57cec5SDimitry Andric
11560b57cec5SDimitry Andric // Run any -thread-entry points.
11570b57cec5SDimitry Andric std::vector<std::thread> AltEntryThreads;
11580b57cec5SDimitry Andric for (auto &ThreadEntryPoint : ThreadEntryPoints) {
11590b57cec5SDimitry Andric auto EntryPointSym = ExitOnErr(J->lookup(ThreadEntryPoint));
11600b57cec5SDimitry Andric typedef void (*EntryPointPtr)();
116181ad6265SDimitry Andric auto EntryPoint = EntryPointSym.toPtr<EntryPointPtr>();
11620b57cec5SDimitry Andric AltEntryThreads.push_back(std::thread([EntryPoint]() { EntryPoint(); }));
11630b57cec5SDimitry Andric }
11640b57cec5SDimitry Andric
1165fe6060f1SDimitry Andric // Resolve and run the main function.
116681ad6265SDimitry Andric auto MainAddr = ExitOnErr(J->lookup(EntryFunc));
1167fe6060f1SDimitry Andric int Result;
11680b57cec5SDimitry Andric
1169fe6060f1SDimitry Andric if (EPC) {
1170fe6060f1SDimitry Andric // ExecutorProcessControl-based execution with JITLink.
117181ad6265SDimitry Andric Result = ExitOnErr(EPC->runAsMain(MainAddr, InputArgv));
1172fe6060f1SDimitry Andric } else {
1173fe6060f1SDimitry Andric // Manual in-process execution with RuntimeDyld.
1174fe6060f1SDimitry Andric using MainFnTy = int(int, char *[]);
117581ad6265SDimitry Andric auto MainFn = MainAddr.toPtr<MainFnTy *>();
1176fe6060f1SDimitry Andric Result = orc::runAsMain(MainFn, InputArgv, StringRef(InputFile));
1177fe6060f1SDimitry Andric }
11780b57cec5SDimitry Andric
11790b57cec5SDimitry Andric // Wait for -entry-point threads.
11800b57cec5SDimitry Andric for (auto &AltEntryThread : AltEntryThreads)
11810b57cec5SDimitry Andric AltEntryThread.join();
11820b57cec5SDimitry Andric
11830b57cec5SDimitry Andric // Run destructors.
11845ffd83dbSDimitry Andric ExitOnErr(J->deinitialize(J->getMainJITDylib()));
11850b57cec5SDimitry Andric
11860b57cec5SDimitry Andric return Result;
11870b57cec5SDimitry Andric }
11880b57cec5SDimitry Andric
disallowOrcOptions()11890b57cec5SDimitry Andric void disallowOrcOptions() {
11900b57cec5SDimitry Andric // Make sure nobody used an orc-lazy specific option accidentally.
11910b57cec5SDimitry Andric
11920b57cec5SDimitry Andric if (LazyJITCompileThreads != 0) {
11930b57cec5SDimitry Andric errs() << "-compile-threads requires -jit-kind=orc-lazy\n";
11940b57cec5SDimitry Andric exit(1);
11950b57cec5SDimitry Andric }
11960b57cec5SDimitry Andric
11970b57cec5SDimitry Andric if (!ThreadEntryPoints.empty()) {
11980b57cec5SDimitry Andric errs() << "-thread-entry requires -jit-kind=orc-lazy\n";
11990b57cec5SDimitry Andric exit(1);
12000b57cec5SDimitry Andric }
12010b57cec5SDimitry Andric
12020b57cec5SDimitry Andric if (PerModuleLazy) {
12030b57cec5SDimitry Andric errs() << "-per-module-lazy requires -jit-kind=orc-lazy\n";
12040b57cec5SDimitry Andric exit(1);
12050b57cec5SDimitry Andric }
12060b57cec5SDimitry Andric }
12070b57cec5SDimitry Andric
launchRemote()1208349cc55cSDimitry Andric Expected<std::unique_ptr<orc::ExecutorProcessControl>> launchRemote() {
12090b57cec5SDimitry Andric #ifndef LLVM_ON_UNIX
12100b57cec5SDimitry Andric llvm_unreachable("launchRemote not supported on non-Unix platforms");
12110b57cec5SDimitry Andric #else
12120b57cec5SDimitry Andric int PipeFD[2][2];
12130b57cec5SDimitry Andric pid_t ChildPID;
12140b57cec5SDimitry Andric
12150b57cec5SDimitry Andric // Create two pipes.
12160b57cec5SDimitry Andric if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0)
12170b57cec5SDimitry Andric perror("Error creating pipe: ");
12180b57cec5SDimitry Andric
12190b57cec5SDimitry Andric ChildPID = fork();
12200b57cec5SDimitry Andric
12210b57cec5SDimitry Andric if (ChildPID == 0) {
12220b57cec5SDimitry Andric // In the child...
12230b57cec5SDimitry Andric
12240b57cec5SDimitry Andric // Close the parent ends of the pipes
12250b57cec5SDimitry Andric close(PipeFD[0][1]);
12260b57cec5SDimitry Andric close(PipeFD[1][0]);
12270b57cec5SDimitry Andric
12280b57cec5SDimitry Andric
12290b57cec5SDimitry Andric // Execute the child process.
12300b57cec5SDimitry Andric std::unique_ptr<char[]> ChildPath, ChildIn, ChildOut;
12310b57cec5SDimitry Andric {
12320b57cec5SDimitry Andric ChildPath.reset(new char[ChildExecPath.size() + 1]);
12330b57cec5SDimitry Andric std::copy(ChildExecPath.begin(), ChildExecPath.end(), &ChildPath[0]);
12340b57cec5SDimitry Andric ChildPath[ChildExecPath.size()] = '\0';
12350b57cec5SDimitry Andric std::string ChildInStr = utostr(PipeFD[0][0]);
12360b57cec5SDimitry Andric ChildIn.reset(new char[ChildInStr.size() + 1]);
12370b57cec5SDimitry Andric std::copy(ChildInStr.begin(), ChildInStr.end(), &ChildIn[0]);
12380b57cec5SDimitry Andric ChildIn[ChildInStr.size()] = '\0';
12390b57cec5SDimitry Andric std::string ChildOutStr = utostr(PipeFD[1][1]);
12400b57cec5SDimitry Andric ChildOut.reset(new char[ChildOutStr.size() + 1]);
12410b57cec5SDimitry Andric std::copy(ChildOutStr.begin(), ChildOutStr.end(), &ChildOut[0]);
12420b57cec5SDimitry Andric ChildOut[ChildOutStr.size()] = '\0';
12430b57cec5SDimitry Andric }
12440b57cec5SDimitry Andric
12450b57cec5SDimitry Andric char * const args[] = { &ChildPath[0], &ChildIn[0], &ChildOut[0], nullptr };
12460b57cec5SDimitry Andric int rc = execv(ChildExecPath.c_str(), args);
12470b57cec5SDimitry Andric if (rc != 0)
12480b57cec5SDimitry Andric perror("Error executing child process: ");
12490b57cec5SDimitry Andric llvm_unreachable("Error executing child process");
12500b57cec5SDimitry Andric }
12510b57cec5SDimitry Andric // else we're the parent...
12520b57cec5SDimitry Andric
12530b57cec5SDimitry Andric // Close the child ends of the pipes
12540b57cec5SDimitry Andric close(PipeFD[0][0]);
12550b57cec5SDimitry Andric close(PipeFD[1][1]);
12560b57cec5SDimitry Andric
1257349cc55cSDimitry Andric // Return a SimpleRemoteEPC instance connected to our end of the pipes.
1258349cc55cSDimitry Andric return orc::SimpleRemoteEPC::Create<orc::FDSimpleRemoteEPCTransport>(
1259349cc55cSDimitry Andric std::make_unique<llvm::orc::InPlaceTaskDispatcher>(),
1260349cc55cSDimitry Andric llvm::orc::SimpleRemoteEPC::Setup(), PipeFD[1][0], PipeFD[0][1]);
12610b57cec5SDimitry Andric #endif
12620b57cec5SDimitry Andric }
126306c3fb27SDimitry Andric
126406c3fb27SDimitry Andric // For MinGW environments, manually export the __chkstk function from the lli
126506c3fb27SDimitry Andric // executable.
126606c3fb27SDimitry Andric //
126706c3fb27SDimitry Andric // Normally, this function is provided by compiler-rt builtins or libgcc.
126806c3fb27SDimitry Andric // It is named "_alloca" on i386, "___chkstk_ms" on x86_64, and "__chkstk" on
126906c3fb27SDimitry Andric // arm/aarch64. In MSVC configurations, it's named "__chkstk" in all
127006c3fb27SDimitry Andric // configurations.
127106c3fb27SDimitry Andric //
127206c3fb27SDimitry Andric // When Orc tries to resolve symbols at runtime, this succeeds in MSVC
127306c3fb27SDimitry Andric // configurations, somewhat by accident/luck; kernelbase.dll does export a
127406c3fb27SDimitry Andric // symbol named "__chkstk" which gets found by Orc, even if regular applications
127506c3fb27SDimitry Andric // never link against that function from that DLL (it's linked in statically
127606c3fb27SDimitry Andric // from a compiler support library).
127706c3fb27SDimitry Andric //
127806c3fb27SDimitry Andric // The MinGW specific symbol names aren't available in that DLL though.
127906c3fb27SDimitry Andric // Therefore, manually export the relevant symbol from lli, to let it be
128006c3fb27SDimitry Andric // found at runtime during tests.
128106c3fb27SDimitry Andric //
128206c3fb27SDimitry Andric // For real JIT uses, the real compiler support libraries should be linked
128306c3fb27SDimitry Andric // in, somehow; this is a workaround to let tests pass.
128406c3fb27SDimitry Andric //
12854542f901SDimitry Andric // We need to make sure that this symbol actually is linked in when we
12864542f901SDimitry Andric // try to export it; if no functions allocate a large enough stack area,
12874542f901SDimitry Andric // nothing would reference it. Therefore, manually declare it and add a
12884542f901SDimitry Andric // reference to it. (Note, the declarations of _alloca/___chkstk_ms/__chkstk
12894542f901SDimitry Andric // are somewhat bogus, these functions use a different custom calling
12904542f901SDimitry Andric // convention.)
12914542f901SDimitry Andric //
129206c3fb27SDimitry Andric // TODO: Move this into libORC at some point, see
129306c3fb27SDimitry Andric // https://github.com/llvm/llvm-project/issues/56603.
129406c3fb27SDimitry Andric #ifdef __MINGW32__
129506c3fb27SDimitry Andric // This is a MinGW version of #pragma comment(linker, "...") that doesn't
129606c3fb27SDimitry Andric // require compiling with -fms-extensions.
129706c3fb27SDimitry Andric #if defined(__i386__)
12984542f901SDimitry Andric #undef _alloca
12994542f901SDimitry Andric extern "C" void _alloca(void);
13004542f901SDimitry Andric static __attribute__((used)) void (*const ref_func)(void) = _alloca;
130106c3fb27SDimitry Andric static __attribute__((section(".drectve"), used)) const char export_chkstk[] =
130206c3fb27SDimitry Andric "-export:_alloca";
130306c3fb27SDimitry Andric #elif defined(__x86_64__)
13044542f901SDimitry Andric extern "C" void ___chkstk_ms(void);
13054542f901SDimitry Andric static __attribute__((used)) void (*const ref_func)(void) = ___chkstk_ms;
130606c3fb27SDimitry Andric static __attribute__((section(".drectve"), used)) const char export_chkstk[] =
130706c3fb27SDimitry Andric "-export:___chkstk_ms";
130806c3fb27SDimitry Andric #else
13094542f901SDimitry Andric extern "C" void __chkstk(void);
13104542f901SDimitry Andric static __attribute__((used)) void (*const ref_func)(void) = __chkstk;
131106c3fb27SDimitry Andric static __attribute__((section(".drectve"), used)) const char export_chkstk[] =
131206c3fb27SDimitry Andric "-export:__chkstk";
131306c3fb27SDimitry Andric #endif
131406c3fb27SDimitry Andric #endif
1315