Chris Lattner | a0d7b08 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 1 | //===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===// |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 345353d | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | d7ff578 | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 141ea3a | 2003-12-26 05:07:35 +0000 | [diff] [blame] | 10 | // This utility provides a simple wrapper around the LLVM Execution Engines, |
| 11 | // which allow the direct execution of LLVM programs through a Just-In-Time |
Torok Edwin | f011f28 | 2009-07-03 12:11:32 +0000 | [diff] [blame] | 12 | // compiler, or through an interpreter if no JIT is available for this platform. |
Chris Lattner | d7ff578 | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 16 | #include "RemoteJITUtils.h" |
NAKAMURA Takumi | cef0a821 | 2016-01-15 02:14:46 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringExtras.h" |
Duncan Sands | 3bd97fe | 2010-08-28 01:30:02 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Triple.h" |
Teresa Johnson | ad17679 | 2016-11-11 05:34:58 +0000 | [diff] [blame] | 19 | #include "llvm/Bitcode/BitcodeReader.h" |
David Blaikie | 4333f97 | 2018-04-11 18:49:37 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/CommandFlags.inc" |
Chris Lattner | 5af6a3f | 2006-08-01 22:34:35 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/LinkAllCodegenComponents.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 22 | #include "llvm/Config/llvm-config.h" |
Brian Gaeke | e99ca44 | 2003-09-05 19:42:34 +0000 | [diff] [blame] | 23 | #include "llvm/ExecutionEngine/GenericValue.h" |
Jeffrey Yasskin | 0b08f3d | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 24 | #include "llvm/ExecutionEngine/Interpreter.h" |
Jeffrey Yasskin | 0b08f3d | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 25 | #include "llvm/ExecutionEngine/JITEventListener.h" |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 26 | #include "llvm/ExecutionEngine/MCJIT.h" |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 27 | #include "llvm/ExecutionEngine/ObjectCache.h" |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 28 | #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h" |
| 29 | #include "llvm/ExecutionEngine/Orc/LLJIT.h" |
Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 30 | #include "llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h" |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 31 | #include "llvm/ExecutionEngine/OrcMCJITReplacement.h" |
Andrew Kaylor | 58365b9 | 2012-11-27 19:49:00 +0000 | [diff] [blame] | 32 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 33 | #include "llvm/IR/IRBuilder.h" |
Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 34 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Module.h" |
| 36 | #include "llvm/IR/Type.h" |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 37 | #include "llvm/IR/TypeBuilder.h" |
Lang Hames | adae9bf | 2018-07-02 22:30:18 +0000 | [diff] [blame] | 38 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 39 | #include "llvm/IRReader/IRReader.h" |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 40 | #include "llvm/Object/Archive.h" |
| 41 | #include "llvm/Object/ObjectFile.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 42 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Debug.h" |
| 44 | #include "llvm/Support/DynamicLibrary.h" |
| 45 | #include "llvm/Support/Format.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 46 | #include "llvm/Support/InitLLVM.h" |
Chris Lattner | 76d4632 | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 47 | #include "llvm/Support/ManagedStatic.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 48 | #include "llvm/Support/MathExtras.h" |
| 49 | #include "llvm/Support/Memory.h" |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 50 | #include "llvm/Support/MemoryBuffer.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Path.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 52 | #include "llvm/Support/PluginLoader.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 53 | #include "llvm/Support/Process.h" |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 54 | #include "llvm/Support/Program.h" |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 55 | #include "llvm/Support/SourceMgr.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 56 | #include "llvm/Support/TargetSelect.h" |
Jonas Devlieghere | 4a2863c | 2018-04-22 08:02:11 +0000 | [diff] [blame] | 57 | #include "llvm/Support/WithColor.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 58 | #include "llvm/Support/raw_ostream.h" |
Daniel Malea | a960c54 | 2013-06-28 19:11:40 +0000 | [diff] [blame] | 59 | #include "llvm/Transforms/Instrumentation.h" |
Chris Lattner | 4c522b9 | 2007-04-27 17:02:33 +0000 | [diff] [blame] | 60 | #include <cerrno> |
NAKAMURA Takumi | 1530487 | 2010-10-22 14:53:59 +0000 | [diff] [blame] | 61 | |
| 62 | #ifdef __CYGWIN__ |
| 63 | #include <cygwin/version.h> |
| 64 | #if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007 |
| 65 | #define DO_NOTHING_ATEXIT 1 |
| 66 | #endif |
| 67 | #endif |
| 68 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 69 | using namespace llvm; |
| 70 | |
Chandler Carruth | f98597a | 2014-04-22 03:10:36 +0000 | [diff] [blame] | 71 | #define DEBUG_TYPE "lli" |
| 72 | |
Chris Lattner | a0d7b08 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 73 | namespace { |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 74 | |
| 75 | enum class JITKind { MCJIT, OrcMCJITReplacement, OrcLazy }; |
| 76 | |
Chris Lattner | a0d7b08 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 77 | cl::opt<std::string> |
Gabor Greif | e16561c | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 78 | InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 79 | |
Chris Lattner | a0d7b08 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 80 | cl::list<std::string> |
| 81 | InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>...")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 82 | |
Chris Lattner | a0d7b08 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 83 | cl::opt<bool> ForceInterpreter("force-interpreter", |
Misha Brukman | c2186e3 | 2003-09-25 18:10:34 +0000 | [diff] [blame] | 84 | cl::desc("Force interpretation: disable JIT"), |
| 85 | cl::init(false)); |
Evan Cheng | 06d988e | 2008-08-08 08:12:06 +0000 | [diff] [blame] | 86 | |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 87 | cl::opt<JITKind> UseJITKind("jit-kind", |
| 88 | cl::desc("Choose underlying JIT kind."), |
| 89 | cl::init(JITKind::MCJIT), |
| 90 | cl::values( |
| 91 | clEnumValN(JITKind::MCJIT, "mcjit", |
| 92 | "MCJIT"), |
| 93 | clEnumValN(JITKind::OrcMCJITReplacement, |
| 94 | "orc-mcjit", |
| 95 | "Orc-based MCJIT replacement"), |
| 96 | clEnumValN(JITKind::OrcLazy, |
| 97 | "orc-lazy", |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 98 | "Orc-based lazy JIT."))); |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 99 | |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 100 | cl::opt<unsigned> |
| 101 | LazyJITCompileThreads("compile-threads", |
| 102 | cl::desc("Choose the number of compile threads " |
| 103 | "(jit-kind=orc-lazy only)"), |
| 104 | cl::init(0)); |
| 105 | |
| 106 | cl::list<std::string> |
| 107 | ThreadEntryPoints("thread-entry", |
| 108 | cl::desc("calls the given entry-point on a new thread " |
| 109 | "(jit-kind=orc-lazy only)")); |
| 110 | |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 111 | // The MCJIT supports building for a target address space separate from |
| 112 | // the JIT compilation process. Use a forked process and a copying |
| 113 | // memory manager with IPC to execute using this functionality. |
| 114 | cl::opt<bool> RemoteMCJIT("remote-mcjit", |
| 115 | cl::desc("Execute MCJIT'ed code in a separate process."), |
| 116 | cl::init(false)); |
| 117 | |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 118 | // Manually specify the child process for remote execution. This overrides |
| 119 | // the simulated remote execution that allocates address space for child |
Andrew Kaylor | 8935258 | 2013-10-29 01:33:14 +0000 | [diff] [blame] | 120 | // execution. The child process will be executed and will communicate with |
| 121 | // lli via stdin/stdout pipes. |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 122 | cl::opt<std::string> |
Alp Toker | a118638 | 2014-01-22 21:52:35 +0000 | [diff] [blame] | 123 | ChildExecPath("mcjit-remote-process", |
| 124 | cl::desc("Specify the filename of the process to launch " |
| 125 | "for remote MCJIT execution. If none is specified," |
| 126 | "\n\tremote execution will be simulated in-process."), |
| 127 | cl::value_desc("filename"), cl::init("")); |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 128 | |
Evan Cheng | 09bd0b1 | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 129 | // Determine optimization level. |
| 130 | cl::opt<char> |
| 131 | OptLevel("O", |
| 132 | cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
| 133 | "(default = '-O2')"), |
| 134 | cl::Prefix, |
| 135 | cl::ZeroOrMore, |
| 136 | cl::init(' ')); |
Evan Cheng | 06d988e | 2008-08-08 08:12:06 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 76766cb | 2005-12-16 05:00:21 +0000 | [diff] [blame] | 138 | cl::opt<std::string> |
Chris Lattner | 78e9e10 | 2005-12-16 05:19:18 +0000 | [diff] [blame] | 139 | TargetTriple("mtriple", cl::desc("Override target triple for module")); |
Evan Cheng | 6158254 | 2008-11-05 23:21:52 +0000 | [diff] [blame] | 140 | |
| 141 | cl::opt<std::string> |
| 142 | EntryFunc("entry-function", |
| 143 | cl::desc("Specify the entry function (default = 'main') " |
| 144 | "of the executable"), |
| 145 | cl::value_desc("function"), |
| 146 | cl::init("main")); |
Eli Bendersky | 4c64758 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 147 | |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 148 | cl::list<std::string> |
Andrew Kaylor | 4404eb4 | 2013-10-28 21:58:15 +0000 | [diff] [blame] | 149 | ExtraModules("extra-module", |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 150 | cl::desc("Extra modules to be loaded"), |
Alp Toker | 0a09ebf | 2013-10-28 22:51:25 +0000 | [diff] [blame] | 151 | cl::value_desc("input bitcode")); |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 152 | |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 153 | cl::list<std::string> |
| 154 | ExtraObjects("extra-object", |
| 155 | cl::desc("Extra object files to be loaded"), |
| 156 | cl::value_desc("input object")); |
| 157 | |
| 158 | cl::list<std::string> |
| 159 | ExtraArchives("extra-archive", |
| 160 | cl::desc("Extra archive files to be loaded"), |
| 161 | cl::value_desc("input archive")); |
| 162 | |
| 163 | cl::opt<bool> |
| 164 | EnableCacheManager("enable-cache-manager", |
Lang Hames | 1ddecc0 | 2014-01-09 05:24:05 +0000 | [diff] [blame] | 165 | cl::desc("Use cache manager to save/load mdoules"), |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 166 | cl::init(false)); |
| 167 | |
Chris Lattner | 5564406 | 2003-10-28 22:51:44 +0000 | [diff] [blame] | 168 | cl::opt<std::string> |
Lang Hames | 1ddecc0 | 2014-01-09 05:24:05 +0000 | [diff] [blame] | 169 | ObjectCacheDir("object-cache-dir", |
| 170 | cl::desc("Directory to store cached object files " |
| 171 | "(must be user writable)"), |
| 172 | cl::init("")); |
| 173 | |
| 174 | cl::opt<std::string> |
Chris Lattner | 5564406 | 2003-10-28 22:51:44 +0000 | [diff] [blame] | 175 | FakeArgv0("fake-argv0", |
| 176 | cl::desc("Override the 'argv[0]' value passed into the executing" |
| 177 | " program"), cl::value_desc("executable")); |
Eli Bendersky | 4c64758 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 3f0ffd3 | 2006-09-14 06:17:09 +0000 | [diff] [blame] | 179 | cl::opt<bool> |
| 180 | DisableCoreFiles("disable-core-files", cl::Hidden, |
| 181 | cl::desc("Disable emission of core files if possible")); |
Evan Cheng | 0422bef | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 182 | |
| 183 | cl::opt<bool> |
Evan Cheng | 16f036c | 2008-05-21 18:20:21 +0000 | [diff] [blame] | 184 | NoLazyCompilation("disable-lazy-compilation", |
Evan Cheng | 0422bef | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 185 | cl::desc("Disable JIT lazy compilation"), |
| 186 | cl::init(false)); |
Evan Cheng | 2129f59 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 187 | |
Nick Lewycky | b0e9789 | 2012-04-18 08:34:12 +0000 | [diff] [blame] | 188 | cl::opt<bool> |
Tim Northover | d4a2f5b | 2012-10-12 09:55:13 +0000 | [diff] [blame] | 189 | GenerateSoftFloatCalls("soft-float", |
| 190 | cl::desc("Generate software floating point library calls"), |
| 191 | cl::init(false)); |
| 192 | |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 193 | enum class DumpKind { |
| 194 | NoDump, |
| 195 | DumpFuncsToStdOut, |
| 196 | DumpModsToStdOut, |
| 197 | DumpModsToDisk |
| 198 | }; |
| 199 | |
| 200 | cl::opt<DumpKind> OrcDumpKind( |
| 201 | "orc-lazy-debug", cl::desc("Debug dumping for the orc-lazy JIT."), |
| 202 | cl::init(DumpKind::NoDump), |
| 203 | cl::values(clEnumValN(DumpKind::NoDump, "no-dump", |
| 204 | "Don't dump anything."), |
| 205 | clEnumValN(DumpKind::DumpFuncsToStdOut, "funcs-to-stdout", |
| 206 | "Dump function names to stdout."), |
| 207 | clEnumValN(DumpKind::DumpModsToStdOut, "mods-to-stdout", |
| 208 | "Dump modules to stdout."), |
| 209 | clEnumValN(DumpKind::DumpModsToDisk, "mods-to-disk", |
| 210 | "Dump modules to the current " |
| 211 | "working directory. (WARNING: " |
| 212 | "will overwrite existing files).")), |
| 213 | cl::Hidden); |
| 214 | |
Lang Hames | ef5a0ee | 2016-04-25 19:56:45 +0000 | [diff] [blame] | 215 | ExitOnError ExitOnErr; |
Chris Lattner | a0d7b08 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 216 | } |
Chris Lattner | 009f810 | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 217 | |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 218 | //===----------------------------------------------------------------------===// |
| 219 | // Object cache |
| 220 | // |
Lang Hames | 1ddecc0 | 2014-01-09 05:24:05 +0000 | [diff] [blame] | 221 | // This object cache implementation writes cached objects to disk to the |
| 222 | // directory specified by CacheDir, using a filename provided in the module |
| 223 | // descriptor. The cache tries to load a saved object using that path if the |
| 224 | // file exists. CacheDir defaults to "", in which case objects are cached |
NAKAMURA Takumi | f462f9c | 2014-01-10 10:38:28 +0000 | [diff] [blame] | 225 | // alongside their originating bitcodes. |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 226 | // |
| 227 | class LLIObjectCache : public ObjectCache { |
| 228 | public: |
Lang Hames | 1ddecc0 | 2014-01-09 05:24:05 +0000 | [diff] [blame] | 229 | LLIObjectCache(const std::string& CacheDir) : CacheDir(CacheDir) { |
| 230 | // Add trailing '/' to cache dir if necessary. |
Lang Hames | f9dd8fd | 2014-01-09 05:29:59 +0000 | [diff] [blame] | 231 | if (!this->CacheDir.empty() && |
| 232 | this->CacheDir[this->CacheDir.size() - 1] != '/') |
NAKAMURA Takumi | f462f9c | 2014-01-10 10:38:28 +0000 | [diff] [blame] | 233 | this->CacheDir += '/'; |
Lang Hames | 1ddecc0 | 2014-01-09 05:24:05 +0000 | [diff] [blame] | 234 | } |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 235 | ~LLIObjectCache() override {} |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 236 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 237 | void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override { |
Benjamin Kramer | 46e38f3 | 2016-06-08 10:01:20 +0000 | [diff] [blame] | 238 | const std::string &ModuleID = M->getModuleIdentifier(); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 239 | std::string CacheName; |
| 240 | if (!getCacheFilename(ModuleID, CacheName)) |
| 241 | return; |
NAKAMURA Takumi | 390e060 | 2014-01-10 10:38:34 +0000 | [diff] [blame] | 242 | if (!CacheDir.empty()) { // Create user-defined cache dir. |
Rafael Espindola | f662e00 | 2015-07-15 21:24:07 +0000 | [diff] [blame] | 243 | SmallString<128> dir(sys::path::parent_path(CacheName)); |
NAKAMURA Takumi | 390e060 | 2014-01-10 10:38:34 +0000 | [diff] [blame] | 244 | sys::fs::create_directories(Twine(dir)); |
| 245 | } |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 246 | std::error_code EC; |
| 247 | raw_fd_ostream outfile(CacheName, EC, sys::fs::F_None); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 248 | outfile.write(Obj.getBufferStart(), Obj.getBufferSize()); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 249 | outfile.close(); |
| 250 | } |
| 251 | |
Rafael Espindola | 5f2bb7d | 2014-08-13 18:49:01 +0000 | [diff] [blame] | 252 | std::unique_ptr<MemoryBuffer> getObject(const Module* M) override { |
Benjamin Kramer | 46e38f3 | 2016-06-08 10:01:20 +0000 | [diff] [blame] | 253 | const std::string &ModuleID = M->getModuleIdentifier(); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 254 | std::string CacheName; |
| 255 | if (!getCacheFilename(ModuleID, CacheName)) |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 256 | return nullptr; |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 257 | // Load the object from the cache filename |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 258 | ErrorOr<std::unique_ptr<MemoryBuffer>> IRObjectBuffer = |
Malcolm Parsons | 06ac79c | 2016-11-02 16:43:50 +0000 | [diff] [blame] | 259 | MemoryBuffer::getFile(CacheName, -1, false); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 260 | // If the file isn't there, that's OK. |
| 261 | if (!IRObjectBuffer) |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 262 | return nullptr; |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 263 | // MCJIT will want to write into this buffer, and we don't want that |
| 264 | // because the file has probably just been mmapped. Instead we make |
| 265 | // a copy. The filed-based buffer will be released when it goes |
| 266 | // out of scope. |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 267 | return MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer()); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | private: |
Lang Hames | 1ddecc0 | 2014-01-09 05:24:05 +0000 | [diff] [blame] | 271 | std::string CacheDir; |
| 272 | |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 273 | bool getCacheFilename(const std::string &ModID, std::string &CacheName) { |
| 274 | std::string Prefix("file:"); |
| 275 | size_t PrefixLength = Prefix.length(); |
| 276 | if (ModID.substr(0, PrefixLength) != Prefix) |
| 277 | return false; |
NAKAMURA Takumi | d7fd6d9 | 2014-01-10 10:38:40 +0000 | [diff] [blame] | 278 | std::string CacheSubdir = ModID.substr(PrefixLength); |
| 279 | #if defined(_WIN32) |
| 280 | // Transform "X:\foo" => "/X\foo" for convenience. |
| 281 | if (isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') { |
| 282 | CacheSubdir[1] = CacheSubdir[0]; |
| 283 | CacheSubdir[0] = '/'; |
| 284 | } |
| 285 | #endif |
| 286 | CacheName = CacheDir + CacheSubdir; |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 287 | size_t pos = CacheName.rfind('.'); |
| 288 | CacheName.replace(pos, CacheName.length() - pos, ".o"); |
| 289 | return true; |
| 290 | } |
| 291 | }; |
| 292 | |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 293 | // On Mingw and Cygwin, an external symbol named '__main' is called from the |
Simon Pilgrim | dae11f7 | 2016-11-20 13:31:13 +0000 | [diff] [blame] | 294 | // generated 'main' function to allow static initialization. To avoid linking |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 295 | // problems with remote targets (because lli's remote target support does not |
| 296 | // currently handle external linking) we add a secondary module which defines |
| 297 | // an empty '__main' function. |
Mehdi Amini | a7de820 | 2016-04-18 18:52:39 +0000 | [diff] [blame] | 298 | static void addCygMingExtraModule(ExecutionEngine &EE, LLVMContext &Context, |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 299 | StringRef TargetTripleStr) { |
| 300 | IRBuilder<> Builder(Context); |
| 301 | Triple TargetTriple(TargetTripleStr); |
| 302 | |
| 303 | // Create a new module. |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 304 | std::unique_ptr<Module> M = make_unique<Module>("CygMingHelper", Context); |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 305 | M->setTargetTriple(TargetTripleStr); |
| 306 | |
| 307 | // Create an empty function named "__main". |
| 308 | Function *Result; |
| 309 | if (TargetTriple.isArch64Bit()) { |
| 310 | Result = Function::Create( |
| 311 | TypeBuilder<int64_t(void), false>::get(Context), |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 312 | GlobalValue::ExternalLinkage, "__main", M.get()); |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 313 | } else { |
| 314 | Result = Function::Create( |
| 315 | TypeBuilder<int32_t(void), false>::get(Context), |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 316 | GlobalValue::ExternalLinkage, "__main", M.get()); |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 317 | } |
| 318 | BasicBlock *BB = BasicBlock::Create(Context, "__main", Result); |
| 319 | Builder.SetInsertPoint(BB); |
| 320 | Value *ReturnVal; |
| 321 | if (TargetTriple.isArch64Bit()) |
| 322 | ReturnVal = ConstantInt::get(Context, APInt(64, 0)); |
| 323 | else |
| 324 | ReturnVal = ConstantInt::get(Context, APInt(32, 0)); |
| 325 | Builder.CreateRet(ReturnVal); |
| 326 | |
| 327 | // Add this new module to the ExecutionEngine. |
Mehdi Amini | a7de820 | 2016-04-18 18:52:39 +0000 | [diff] [blame] | 328 | EE.addModule(std::move(M)); |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Lang Hames | 1899d0c | 2015-06-09 02:43:27 +0000 | [diff] [blame] | 331 | CodeGenOpt::Level getOptLevel() { |
| 332 | switch (OptLevel) { |
| 333 | default: |
Jonas Devlieghere | 4a2863c | 2018-04-22 08:02:11 +0000 | [diff] [blame] | 334 | WithColor::error(errs(), "lli") << "invalid optimization level.\n"; |
Lang Hames | 1899d0c | 2015-06-09 02:43:27 +0000 | [diff] [blame] | 335 | exit(1); |
| 336 | case '0': return CodeGenOpt::None; |
| 337 | case '1': return CodeGenOpt::Less; |
| 338 | case ' ': |
| 339 | case '2': return CodeGenOpt::Default; |
| 340 | case '3': return CodeGenOpt::Aggressive; |
| 341 | } |
| 342 | llvm_unreachable("Unrecognized opt level."); |
| 343 | } |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 344 | |
Davide Italiano | da8e6b2 | 2016-11-17 22:58:13 +0000 | [diff] [blame] | 345 | LLVM_ATTRIBUTE_NORETURN |
| 346 | static void reportError(SMDiagnostic Err, const char *ProgName) { |
| 347 | Err.print(ProgName, errs()); |
| 348 | exit(1); |
| 349 | } |
| 350 | |
Lang Hames | 37a6641 | 2018-08-28 20:20:31 +0000 | [diff] [blame] | 351 | int runOrcLazyJIT(const char *ProgName); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 352 | |
Chris Lattner | d7ff578 | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 353 | //===----------------------------------------------------------------------===// |
Chris Lattner | d7ff578 | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 354 | // main Driver function |
| 355 | // |
Misha Brukman | 5b255e5 | 2003-10-14 21:39:53 +0000 | [diff] [blame] | 356 | int main(int argc, char **argv, char * const *envp) { |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 357 | InitLLVM X(argc, argv); |
Daniel Dunbar | 3d92d93 | 2009-07-16 02:04:54 +0000 | [diff] [blame] | 358 | |
Lang Hames | ef5a0ee | 2016-04-25 19:56:45 +0000 | [diff] [blame] | 359 | if (argc > 1) |
| 360 | ExitOnErr.setBanner(std::string(argv[0]) + ": "); |
| 361 | |
Daniel Dunbar | 3d92d93 | 2009-07-16 02:04:54 +0000 | [diff] [blame] | 362 | // If we have a native target, initialize it to ensure it is linked in and |
| 363 | // usable by the JIT. |
| 364 | InitializeNativeTarget(); |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 365 | InitializeNativeTargetAsmPrinter(); |
Jim Grosbach | 2cce3f9 | 2012-11-05 19:06:05 +0000 | [diff] [blame] | 366 | InitializeNativeTargetAsmParser(); |
Daniel Dunbar | 3d92d93 | 2009-07-16 02:04:54 +0000 | [diff] [blame] | 367 | |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 368 | cl::ParseCommandLineOptions(argc, argv, |
Dan Gohman | 2c6a821 | 2007-10-08 15:45:12 +0000 | [diff] [blame] | 369 | "llvm interpreter & dynamic compiler\n"); |
Reid Spencer | 996ec72 | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 370 | |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 371 | // If the user doesn't want core files, disable them. |
| 372 | if (DisableCoreFiles) |
| 373 | sys::Process::PreventCoreFiles(); |
Eli Bendersky | 4c64758 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 374 | |
Lang Hames | 37a6641 | 2018-08-28 20:20:31 +0000 | [diff] [blame] | 375 | if (UseJITKind == JITKind::OrcLazy) |
| 376 | return runOrcLazyJIT(argv[0]); |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 377 | else { |
| 378 | // Make sure nobody used an orc-lazy specific option accidentally. |
| 379 | |
| 380 | if (LazyJITCompileThreads != 0) { |
| 381 | errs() << "-compile-threads requires -jit-kind=orc-lazy\n"; |
| 382 | exit(1); |
| 383 | } |
| 384 | |
| 385 | if (!ThreadEntryPoints.empty()) { |
| 386 | errs() << "-thread-entry requires -jit-kind=orc-lazy\n"; |
| 387 | exit(1); |
| 388 | } |
| 389 | } |
Lang Hames | 37a6641 | 2018-08-28 20:20:31 +0000 | [diff] [blame] | 390 | |
Mehdi Amini | a7de820 | 2016-04-18 18:52:39 +0000 | [diff] [blame] | 391 | LLVMContext Context; |
| 392 | |
Gabor Greif | e16561c | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 393 | // Load the bitcode... |
Daniel Dunbar | 9589bf8 | 2010-11-13 00:28:01 +0000 | [diff] [blame] | 394 | SMDiagnostic Err; |
Rafael Espindola | d233b06 | 2014-08-26 17:29:46 +0000 | [diff] [blame] | 395 | std::unique_ptr<Module> Owner = parseIRFile(InputFile, Err, Context); |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 396 | Module *Mod = Owner.get(); |
Davide Italiano | da8e6b2 | 2016-11-17 22:58:13 +0000 | [diff] [blame] | 397 | if (!Mod) |
| 398 | reportError(Err, argv[0]); |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 399 | |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 400 | if (EnableCacheManager) { |
Eric Christopher | 79cc1e3 | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 401 | std::string CacheName("file:"); |
| 402 | CacheName.append(InputFile); |
| 403 | Mod->setModuleIdentifier(CacheName); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 406 | // If not jitting lazily, load the whole bitcode file eagerly too. |
| 407 | if (NoLazyCompilation) { |
Davide Italiano | b0e067b | 2016-11-09 21:30:33 +0000 | [diff] [blame] | 408 | // Use *argv instead of argv[0] to work around a wrong GCC warning. |
| 409 | ExitOnError ExitOnErr(std::string(*argv) + |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 410 | ": bitcode didn't read correctly: "); |
| 411 | ExitOnErr(Mod->materializeAll()); |
Evan Cheng | 0422bef | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 412 | } |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 413 | |
Rafael Espindola | e9fab9b | 2014-01-14 23:51:27 +0000 | [diff] [blame] | 414 | std::string ErrorMsg; |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 415 | EngineBuilder builder(std::move(Owner)); |
Jeffrey Yasskin | 31faeff | 2010-02-05 16:19:36 +0000 | [diff] [blame] | 416 | builder.setMArch(MArch); |
Craig Topper | 243f20f | 2018-01-09 18:14:18 +0000 | [diff] [blame] | 417 | builder.setMCPU(getCPUStr()); |
| 418 | builder.setMAttrs(getFeatureList()); |
Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 419 | if (RelocModel.getNumOccurrences()) |
| 420 | builder.setRelocationModel(RelocModel); |
Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 421 | if (CMModel.getNumOccurrences()) |
| 422 | builder.setCodeModel(CMModel); |
Daniel Dunbar | a78d809 | 2009-07-18 08:07:13 +0000 | [diff] [blame] | 423 | builder.setErrorStr(&ErrorMsg); |
| 424 | builder.setEngineKind(ForceInterpreter |
Reid Kleckner | fc8a2d5 | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 425 | ? EngineKind::Interpreter |
| 426 | : EngineKind::JIT); |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 427 | builder.setUseOrcMCJITReplacement(UseJITKind == JITKind::OrcMCJITReplacement); |
Reid Kleckner | fc8a2d5 | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 428 | |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 429 | // If we are supposed to override the target triple, do so now. |
| 430 | if (!TargetTriple.empty()) |
Duncan Sands | 3bd97fe | 2010-08-28 01:30:02 +0000 | [diff] [blame] | 431 | Mod->setTargetTriple(Triple::normalize(TargetTriple)); |
Evan Cheng | 0422bef | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 432 | |
Eli Bendersky | 4c64758 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 433 | // Enable MCJIT if desired. |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 434 | RTDyldMemoryManager *RTDyldMM = nullptr; |
Eric Christopher | 79cc1e3 | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 435 | if (!ForceInterpreter) { |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 436 | if (RemoteMCJIT) |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 437 | RTDyldMM = new ForwardingMemoryManager(); |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 438 | else |
Filip Pizlo | 9bc53e8 | 2013-05-14 19:29:00 +0000 | [diff] [blame] | 439 | RTDyldMM = new SectionMemoryManager(); |
Lang Hames | 4a5697e | 2014-12-03 00:51:19 +0000 | [diff] [blame] | 440 | |
| 441 | // Deliberately construct a temp std::unique_ptr to pass in. Do not null out |
| 442 | // RTDyldMM: We still use it below, even though we don't own it. |
| 443 | builder.setMCJITMemoryManager( |
| 444 | std::unique_ptr<RTDyldMemoryManager>(RTDyldMM)); |
Lang Hames | 0f15490 | 2014-09-23 16:56:02 +0000 | [diff] [blame] | 445 | } else if (RemoteMCJIT) { |
Jonas Devlieghere | 4a2863c | 2018-04-22 08:02:11 +0000 | [diff] [blame] | 446 | WithColor::error(errs(), argv[0]) |
| 447 | << "remote process execution does not work with the interpreter.\n"; |
Lang Hames | 0f15490 | 2014-09-23 16:56:02 +0000 | [diff] [blame] | 448 | exit(1); |
Eli Bendersky | 4c64758 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 449 | } |
Daniel Dunbar | 70ff8b0 | 2010-11-17 16:06:37 +0000 | [diff] [blame] | 450 | |
Lang Hames | 1899d0c | 2015-06-09 02:43:27 +0000 | [diff] [blame] | 451 | builder.setOptLevel(getOptLevel()); |
Reid Kleckner | fc8a2d5 | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 452 | |
David Blaikie | c14bfec | 2017-11-27 19:43:58 +0000 | [diff] [blame] | 453 | TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Tim Northover | d4a2f5b | 2012-10-12 09:55:13 +0000 | [diff] [blame] | 454 | if (FloatABIForCalls != FloatABI::Default) |
| 455 | Options.FloatABIType = FloatABIForCalls; |
Tim Northover | d4a2f5b | 2012-10-12 09:55:13 +0000 | [diff] [blame] | 456 | |
Tim Northover | d4a2f5b | 2012-10-12 09:55:13 +0000 | [diff] [blame] | 457 | builder.setTargetOptions(Options); |
| 458 | |
Mehdi Amini | a7de820 | 2016-04-18 18:52:39 +0000 | [diff] [blame] | 459 | std::unique_ptr<ExecutionEngine> EE(builder.create()); |
Chris Lattner | f840ed7 | 2009-07-07 18:31:09 +0000 | [diff] [blame] | 460 | if (!EE) { |
| 461 | if (!ErrorMsg.empty()) |
Jonas Devlieghere | 4a2863c | 2018-04-22 08:02:11 +0000 | [diff] [blame] | 462 | WithColor::error(errs(), argv[0]) |
| 463 | << "error creating EE: " << ErrorMsg << "\n"; |
Chris Lattner | f840ed7 | 2009-07-07 18:31:09 +0000 | [diff] [blame] | 464 | else |
Jonas Devlieghere | 4a2863c | 2018-04-22 08:02:11 +0000 | [diff] [blame] | 465 | WithColor::error(errs(), argv[0]) << "unknown error creating EE!\n"; |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 466 | exit(1); |
| 467 | } |
| 468 | |
Mehdi Amini | a7de820 | 2016-04-18 18:52:39 +0000 | [diff] [blame] | 469 | std::unique_ptr<LLIObjectCache> CacheManager; |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 470 | if (EnableCacheManager) { |
Mehdi Amini | a7de820 | 2016-04-18 18:52:39 +0000 | [diff] [blame] | 471 | CacheManager.reset(new LLIObjectCache(ObjectCacheDir)); |
| 472 | EE->setObjectCache(CacheManager.get()); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 475 | // Load any additional modules specified on the command line. |
| 476 | for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) { |
Rafael Espindola | d233b06 | 2014-08-26 17:29:46 +0000 | [diff] [blame] | 477 | std::unique_ptr<Module> XMod = parseIRFile(ExtraModules[i], Err, Context); |
Davide Italiano | da8e6b2 | 2016-11-17 22:58:13 +0000 | [diff] [blame] | 478 | if (!XMod) |
| 479 | reportError(Err, argv[0]); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 480 | if (EnableCacheManager) { |
Eric Christopher | 79cc1e3 | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 481 | std::string CacheName("file:"); |
| 482 | CacheName.append(ExtraModules[i]); |
| 483 | XMod->setModuleIdentifier(CacheName); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 484 | } |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 485 | EE->addModule(std::move(XMod)); |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 488 | for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) { |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 489 | Expected<object::OwningBinary<object::ObjectFile>> Obj = |
Rafael Espindola | 51cc360 | 2014-01-22 00:14:49 +0000 | [diff] [blame] | 490 | object::ObjectFile::createObjectFile(ExtraObjects[i]); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 491 | if (!Obj) { |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 492 | // TODO: Actually report errors helpfully. |
| 493 | consumeError(Obj.takeError()); |
Davide Italiano | da8e6b2 | 2016-11-17 22:58:13 +0000 | [diff] [blame] | 494 | reportError(Err, argv[0]); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 495 | } |
Rafael Espindola | 061beab | 2014-08-20 15:19:37 +0000 | [diff] [blame] | 496 | object::OwningBinary<object::ObjectFile> &O = Obj.get(); |
Rafael Espindola | 7271c19 | 2014-08-26 21:04:04 +0000 | [diff] [blame] | 497 | EE->addObjectFile(std::move(O)); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 501 | ErrorOr<std::unique_ptr<MemoryBuffer>> ArBufOrErr = |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 502 | MemoryBuffer::getFileOrSTDIN(ExtraArchives[i]); |
Davide Italiano | da8e6b2 | 2016-11-17 22:58:13 +0000 | [diff] [blame] | 503 | if (!ArBufOrErr) |
| 504 | reportError(Err, argv[0]); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 505 | std::unique_ptr<MemoryBuffer> &ArBuf = ArBufOrErr.get(); |
Rafael Espindola | e192341 | 2014-08-01 18:31:17 +0000 | [diff] [blame] | 506 | |
Kevin Enderby | c60a321 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 507 | Expected<std::unique_ptr<object::Archive>> ArOrErr = |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 508 | object::Archive::create(ArBuf->getMemBufferRef()); |
Kevin Enderby | c60a321 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 509 | if (!ArOrErr) { |
| 510 | std::string Buf; |
| 511 | raw_string_ostream OS(Buf); |
| 512 | logAllUnhandledErrors(ArOrErr.takeError(), OS, ""); |
| 513 | OS.flush(); |
| 514 | errs() << Buf; |
Davide Italiano | 8651144 | 2016-11-17 22:59:13 +0000 | [diff] [blame] | 515 | exit(1); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 516 | } |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 517 | std::unique_ptr<object::Archive> &Ar = ArOrErr.get(); |
| 518 | |
| 519 | object::OwningBinary<object::Archive> OB(std::move(Ar), std::move(ArBuf)); |
| 520 | |
| 521 | EE->addArchive(std::move(OB)); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 524 | // If the target is Cygwin/MingW and we are generating remote code, we |
| 525 | // need an extra module to help out with linking. |
| 526 | if (RemoteMCJIT && Triple(Mod->getTargetTriple()).isOSCygMing()) { |
Mehdi Amini | a7de820 | 2016-04-18 18:52:39 +0000 | [diff] [blame] | 527 | addCygMingExtraModule(*EE, Context, Mod->getTargetTriple()); |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Eli Bendersky | 5262ad2 | 2012-03-13 08:33:15 +0000 | [diff] [blame] | 530 | // The following functions have no effect if their respective profiling |
| 531 | // support wasn't enabled in the build configuration. |
| 532 | EE->RegisterJITEventListener( |
| 533 | JITEventListener::createOProfileJITEventListener()); |
| 534 | EE->RegisterJITEventListener( |
| 535 | JITEventListener::createIntelJITEventListener()); |
Andres Freund | 376a3d3 | 2018-07-24 00:54:06 +0000 | [diff] [blame] | 536 | if (!RemoteMCJIT) |
| 537 | EE->RegisterJITEventListener( |
| 538 | JITEventListener::createPerfJITEventListener()); |
Jeffrey Yasskin | 0b08f3d | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 539 | |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 540 | if (!NoLazyCompilation && RemoteMCJIT) { |
Jonas Devlieghere | 4a2863c | 2018-04-22 08:02:11 +0000 | [diff] [blame] | 541 | WithColor::warning(errs(), argv[0]) |
| 542 | << "remote mcjit does not support lazy compilation\n"; |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 543 | NoLazyCompilation = true; |
| 544 | } |
Jeffrey Yasskin | aa8814a | 2009-10-27 22:39:42 +0000 | [diff] [blame] | 545 | EE->DisableLazyCompilation(NoLazyCompilation); |
Evan Cheng | 0422bef | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 546 | |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 547 | // If the user specifically requested an argv[0] to pass into the program, |
| 548 | // do it now. |
| 549 | if (!FakeArgv0.empty()) { |
Chris Bieneman | e71fb5c | 2015-01-22 01:49:59 +0000 | [diff] [blame] | 550 | InputFile = static_cast<std::string>(FakeArgv0); |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 551 | } else { |
| 552 | // Otherwise, if there is a .bc suffix on the executable strip it off, it |
| 553 | // might confuse the program. |
Benjamin Kramer | a944a9a | 2010-04-15 11:33:14 +0000 | [diff] [blame] | 554 | if (StringRef(InputFile).endswith(".bc")) |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 555 | InputFile.erase(InputFile.length() - 3); |
| 556 | } |
| 557 | |
| 558 | // Add the module's name to the start of the vector of arguments to main(). |
| 559 | InputArgv.insert(InputArgv.begin(), InputFile); |
| 560 | |
| 561 | // Call the main function from M as if its signature were: |
| 562 | // int main (int argc, char **argv, const char **envp) |
| 563 | // using the contents of Args to determine argc & argv, and the contents of |
| 564 | // EnvVars to determine envp. |
| 565 | // |
Evan Cheng | 6158254 | 2008-11-05 23:21:52 +0000 | [diff] [blame] | 566 | Function *EntryFn = Mod->getFunction(EntryFunc); |
| 567 | if (!EntryFn) { |
Jonas Devlieghere | 4a2863c | 2018-04-22 08:02:11 +0000 | [diff] [blame] | 568 | WithColor::error(errs(), argv[0]) |
| 569 | << '\'' << EntryFunc << "\' function not found in module.\n"; |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 570 | return -1; |
| 571 | } |
| 572 | |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 573 | // Reset errno to zero on entry to main. |
| 574 | errno = 0; |
Eli Bendersky | 4c64758 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 575 | |
Lang Hames | 3fde652 | 2016-04-18 19:55:43 +0000 | [diff] [blame] | 576 | int Result = -1; |
Andrew Kaylor | c87c347 | 2013-10-02 18:04:40 +0000 | [diff] [blame] | 577 | |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 578 | // Sanity check use of remote-jit: LLI currently only supports use of the |
| 579 | // remote JIT on Unix platforms. |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 580 | if (RemoteMCJIT) { |
| 581 | #ifndef LLVM_ON_UNIX |
Jonas Devlieghere | 4a2863c | 2018-04-22 08:02:11 +0000 | [diff] [blame] | 582 | WithColor::warning(errs(), argv[0]) |
Jonas Devlieghere | 7b5fa24 | 2018-04-22 08:35:00 +0000 | [diff] [blame] | 583 | << "host does not support external remote targets.\n"; |
| 584 | WithColor::note() << "defaulting to local execution\n"; |
Lang Hames | 4b6e021 | 2016-01-11 21:41:34 +0000 | [diff] [blame] | 585 | return -1; |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 586 | #else |
| 587 | if (ChildExecPath.empty()) { |
Jonas Devlieghere | 4a2863c | 2018-04-22 08:02:11 +0000 | [diff] [blame] | 588 | WithColor::error(errs(), argv[0]) |
| 589 | << "-remote-mcjit requires -mcjit-remote-process.\n"; |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 590 | exit(1); |
| 591 | } else if (!sys::fs::can_execute(ChildExecPath)) { |
Jonas Devlieghere | 4a2863c | 2018-04-22 08:02:11 +0000 | [diff] [blame] | 592 | WithColor::error(errs(), argv[0]) |
| 593 | << "unable to find usable child executable: '" << ChildExecPath |
| 594 | << "'\n"; |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 595 | return -1; |
| 596 | } |
| 597 | #endif |
| 598 | } |
| 599 | |
Andrew Kaylor | 58365b9 | 2012-11-27 19:49:00 +0000 | [diff] [blame] | 600 | if (!RemoteMCJIT) { |
Andrew Kaylor | 6587bcf | 2013-10-11 22:47:10 +0000 | [diff] [blame] | 601 | // If the program doesn't explicitly call exit, we will need the Exit |
| 602 | // function later on to make an explicit call, so get the function now. |
| 603 | Constant *Exit = Mod->getOrInsertFunction("exit", Type::getVoidTy(Context), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 604 | Type::getInt32Ty(Context)); |
Andrew Kaylor | 6587bcf | 2013-10-11 22:47:10 +0000 | [diff] [blame] | 605 | |
Andrew Kaylor | c87c347 | 2013-10-02 18:04:40 +0000 | [diff] [blame] | 606 | // Run static constructors. |
Eric Christopher | 79cc1e3 | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 607 | if (!ForceInterpreter) { |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 608 | // Give MCJIT a chance to apply relocations and set page permissions. |
| 609 | EE->finalizeObject(); |
| 610 | } |
| 611 | EE->runStaticConstructorsDestructors(false); |
Evan Cheng | 0422bef | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 612 | |
NAKAMURA Takumi | 87e0880 | 2013-12-07 11:21:42 +0000 | [diff] [blame] | 613 | // Trigger compilation separately so code regions that need to be |
Andrew Kaylor | c87c347 | 2013-10-02 18:04:40 +0000 | [diff] [blame] | 614 | // invalidated will be known. |
| 615 | (void)EE->getPointerToFunction(EntryFn); |
| 616 | // Clear instruction cache before code will be executed. |
| 617 | if (RTDyldMM) |
| 618 | static_cast<SectionMemoryManager*>(RTDyldMM)->invalidateInstructionCache(); |
| 619 | |
| 620 | // Run main. |
| 621 | Result = EE->runFunctionAsMain(EntryFn, InputArgv, envp); |
| 622 | |
| 623 | // Run static destructors. |
| 624 | EE->runStaticConstructorsDestructors(true); |
| 625 | |
| 626 | // If the program didn't call exit explicitly, we should call it now. |
| 627 | // This ensures that any atexit handlers get called correctly. |
| 628 | if (Function *ExitF = dyn_cast<Function>(Exit)) { |
| 629 | std::vector<GenericValue> Args; |
| 630 | GenericValue ResultGV; |
| 631 | ResultGV.IntVal = APInt(32, Result); |
| 632 | Args.push_back(ResultGV); |
| 633 | EE->runFunction(ExitF, Args); |
Jonas Devlieghere | 4a2863c | 2018-04-22 08:02:11 +0000 | [diff] [blame] | 634 | WithColor::error(errs(), argv[0]) << "exit(" << Result << ") returned!\n"; |
Andrew Kaylor | c87c347 | 2013-10-02 18:04:40 +0000 | [diff] [blame] | 635 | abort(); |
| 636 | } else { |
Jonas Devlieghere | 4a2863c | 2018-04-22 08:02:11 +0000 | [diff] [blame] | 637 | WithColor::error(errs(), argv[0]) |
| 638 | << "exit defined with wrong prototype!\n"; |
Andrew Kaylor | c87c347 | 2013-10-02 18:04:40 +0000 | [diff] [blame] | 639 | abort(); |
| 640 | } |
| 641 | } else { |
| 642 | // else == "if (RemoteMCJIT)" |
| 643 | |
| 644 | // Remote target MCJIT doesn't (yet) support static constructors. No reason |
Hiroshi Inoue | 0ca79dc | 2017-07-11 06:04:59 +0000 | [diff] [blame] | 645 | // it couldn't. This is a limitation of the LLI implementation, not the |
Andrew Kaylor | c87c347 | 2013-10-02 18:04:40 +0000 | [diff] [blame] | 646 | // MCJIT itself. FIXME. |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 647 | |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 648 | // Lanch the remote process and get a channel to it. |
Lang Hames | 1f2bf2d | 2016-11-11 21:42:09 +0000 | [diff] [blame] | 649 | std::unique_ptr<FDRawChannel> C = launchRemote(); |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 650 | if (!C) { |
Jonas Devlieghere | 4a2863c | 2018-04-22 08:02:11 +0000 | [diff] [blame] | 651 | WithColor::error(errs(), argv[0]) << "failed to launch remote JIT.\n"; |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 652 | exit(1); |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 655 | // Create a remote target client running over the channel. |
Lang Hames | bd0cb78 | 2018-05-30 01:57:45 +0000 | [diff] [blame] | 656 | llvm::orc::ExecutionSession ES; |
| 657 | ES.setErrorReporter([&](Error Err) { ExitOnErr(std::move(Err)); }); |
Lang Hames | 9e68b734 | 2017-09-04 20:54:46 +0000 | [diff] [blame] | 658 | typedef orc::remote::OrcRemoteTargetClient MyRemote; |
Lang Hames | bd0cb78 | 2018-05-30 01:57:45 +0000 | [diff] [blame] | 659 | auto R = ExitOnErr(MyRemote::Create(*C, ES)); |
Jim Grosbach | 748b947 | 2012-08-28 23:22:30 +0000 | [diff] [blame] | 660 | |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 661 | // Create a remote memory manager. |
Lang Hames | 9e68b734 | 2017-09-04 20:54:46 +0000 | [diff] [blame] | 662 | auto RemoteMM = ExitOnErr(R->createRemoteMemoryManager()); |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 663 | |
| 664 | // Forward MCJIT's memory manager calls to the remote memory manager. |
| 665 | static_cast<ForwardingMemoryManager*>(RTDyldMM)->setMemMgr( |
| 666 | std::move(RemoteMM)); |
| 667 | |
| 668 | // Forward MCJIT's symbol resolution calls to the remote. |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 669 | static_cast<ForwardingMemoryManager *>(RTDyldMM)->setResolver( |
| 670 | orc::createLambdaResolver( |
| 671 | [](const std::string &Name) { return nullptr; }, |
| 672 | [&](const std::string &Name) { |
| 673 | if (auto Addr = ExitOnErr(R->getSymbolAddress(Name))) |
| 674 | return JITSymbol(Addr, JITSymbolFlags::Exported); |
| 675 | return JITSymbol(nullptr); |
| 676 | })); |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 677 | |
| 678 | // Grab the target address of the JIT'd main function on the remote and call |
| 679 | // it. |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 680 | // FIXME: argv and envp handling. |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 681 | JITTargetAddress Entry = EE->getFunctionAddress(EntryFn->getName().str()); |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 682 | EE->finalizeObject(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 683 | LLVM_DEBUG(dbgs() << "Executing '" << EntryFn->getName() << "' at 0x" |
| 684 | << format("%llx", Entry) << "\n"); |
Lang Hames | 1f2bf2d | 2016-11-11 21:42:09 +0000 | [diff] [blame] | 685 | Result = ExitOnErr(R->callIntVoid(Entry)); |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 686 | |
Andrew Kaylor | c87c347 | 2013-10-02 18:04:40 +0000 | [diff] [blame] | 687 | // Like static constructors, the remote target MCJIT support doesn't handle |
| 688 | // this yet. It could. FIXME. |
| 689 | |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 690 | // Delete the EE - we need to tear it down *before* we terminate the session |
| 691 | // with the remote, otherwise it'll crash when it tries to release resources |
| 692 | // on a remote that has already been disconnected. |
Mehdi Amini | a7de820 | 2016-04-18 18:52:39 +0000 | [diff] [blame] | 693 | EE.reset(); |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 694 | |
| 695 | // Signal the remote target that we're done JITing. |
Lang Hames | 1f2bf2d | 2016-11-11 21:42:09 +0000 | [diff] [blame] | 696 | ExitOnErr(R->terminateSession()); |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 697 | } |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 698 | |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 699 | return Result; |
Chris Lattner | d7ff578 | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 700 | } |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 701 | |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 702 | static orc::IRTransformLayer2::TransformFunction createDebugDumper() { |
| 703 | switch (OrcDumpKind) { |
| 704 | case DumpKind::NoDump: |
Lang Hames | 3e709d5 | 2018-09-28 21:49:53 +0000 | [diff] [blame^] | 705 | return [](orc::ThreadSafeModule TSM, |
| 706 | const orc::MaterializationResponsibility &R) { return TSM; }; |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 707 | |
| 708 | case DumpKind::DumpFuncsToStdOut: |
Lang Hames | 3e709d5 | 2018-09-28 21:49:53 +0000 | [diff] [blame^] | 709 | return [](orc::ThreadSafeModule TSM, |
| 710 | const orc::MaterializationResponsibility &R) { |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 711 | printf("[ "); |
| 712 | |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 713 | for (const auto &F : *TSM.getModule()) { |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 714 | if (F.isDeclaration()) |
| 715 | continue; |
| 716 | |
| 717 | if (F.hasName()) { |
| 718 | std::string Name(F.getName()); |
| 719 | printf("%s ", Name.c_str()); |
| 720 | } else |
| 721 | printf("<anon> "); |
| 722 | } |
| 723 | |
| 724 | printf("]\n"); |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 725 | return TSM; |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 726 | }; |
| 727 | |
| 728 | case DumpKind::DumpModsToStdOut: |
Lang Hames | 3e709d5 | 2018-09-28 21:49:53 +0000 | [diff] [blame^] | 729 | return [](orc::ThreadSafeModule TSM, |
| 730 | const orc::MaterializationResponsibility &R) { |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 731 | outs() << "----- Module Start -----\n" |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 732 | << *TSM.getModule() << "----- Module End -----\n"; |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 733 | |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 734 | return TSM; |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 735 | }; |
| 736 | |
| 737 | case DumpKind::DumpModsToDisk: |
Lang Hames | 3e709d5 | 2018-09-28 21:49:53 +0000 | [diff] [blame^] | 738 | return [](orc::ThreadSafeModule TSM, |
| 739 | const orc::MaterializationResponsibility &R) { |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 740 | std::error_code EC; |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 741 | raw_fd_ostream Out(TSM.getModule()->getModuleIdentifier() + ".ll", EC, |
| 742 | sys::fs::F_Text); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 743 | if (EC) { |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 744 | errs() << "Couldn't open " << TSM.getModule()->getModuleIdentifier() |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 745 | << " for dumping.\nError:" << EC.message() << "\n"; |
| 746 | exit(1); |
| 747 | } |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 748 | Out << *TSM.getModule(); |
| 749 | return TSM; |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 750 | }; |
| 751 | } |
| 752 | llvm_unreachable("Unknown DumpKind"); |
| 753 | } |
| 754 | |
Lang Hames | 37a6641 | 2018-08-28 20:20:31 +0000 | [diff] [blame] | 755 | int runOrcLazyJIT(const char *ProgName) { |
| 756 | // Start setting up the JIT environment. |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 757 | |
Lang Hames | 37a6641 | 2018-08-28 20:20:31 +0000 | [diff] [blame] | 758 | // First add lli's symbols into the JIT's search space. |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 759 | std::string ErrMsg; |
| 760 | sys::DynamicLibrary LibLLI = |
| 761 | sys::DynamicLibrary::getPermanentLibrary(nullptr, &ErrMsg); |
| 762 | if (!LibLLI.isValid()) { |
| 763 | errs() << "Error loading lli symbols: " << ErrMsg << ".\n"; |
| 764 | return 1; |
| 765 | } |
| 766 | |
Lang Hames | 37a6641 | 2018-08-28 20:20:31 +0000 | [diff] [blame] | 767 | // Parse the main module. |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 768 | orc::ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>()); |
Lang Hames | 37a6641 | 2018-08-28 20:20:31 +0000 | [diff] [blame] | 769 | SMDiagnostic Err; |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 770 | auto MainModule = orc::ThreadSafeModule( |
| 771 | parseIRFile(InputFile, Err, *TSCtx.getContext()), TSCtx); |
Lang Hames | 37a6641 | 2018-08-28 20:20:31 +0000 | [diff] [blame] | 772 | if (!MainModule) |
| 773 | reportError(Err, ProgName); |
| 774 | |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 775 | const auto &TT = MainModule.getModule()->getTargetTriple(); |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 776 | orc::JITTargetMachineBuilder JTMB = |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 777 | TT.empty() ? ExitOnErr(orc::JITTargetMachineBuilder::detectHost()) |
| 778 | : orc::JITTargetMachineBuilder(Triple(TT)); |
| 779 | |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 780 | JTMB.setArch(MArch) |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 781 | .setCPU(getCPUStr()) |
| 782 | .addFeatures(getFeatureList()) |
| 783 | .setRelocationModel(RelocModel.getNumOccurrences() |
| 784 | ? Optional<Reloc::Model>(RelocModel) |
| 785 | : None) |
| 786 | .setCodeModel(CMModel.getNumOccurrences() |
| 787 | ? Optional<CodeModel::Model>(CMModel) |
| 788 | : None); |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 789 | DataLayout DL(""); |
| 790 | { |
| 791 | // Create a throwaway TargetMachine to get the data layout. |
| 792 | auto TM = ExitOnErr(JTMB.createTargetMachine()); |
| 793 | DL = TM->createDataLayout(); |
| 794 | } |
| 795 | auto J = ExitOnErr(orc::LLLazyJIT::Create(std::move(JTMB), DL, LazyJITCompileThreads)); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 796 | |
Lang Hames | adae9bf | 2018-07-02 22:30:18 +0000 | [diff] [blame] | 797 | auto Dump = createDebugDumper(); |
| 798 | |
Lang Hames | 3e709d5 | 2018-09-28 21:49:53 +0000 | [diff] [blame^] | 799 | J->setLazyCompileTransform([&](orc::ThreadSafeModule TSM, |
| 800 | const orc::MaterializationResponsibility &R) { |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 801 | if (verifyModule(*TSM.getModule(), &dbgs())) { |
| 802 | dbgs() << "Bad module: " << *TSM.getModule() << "\n"; |
| 803 | exit(1); |
| 804 | } |
Lang Hames | 3e709d5 | 2018-09-28 21:49:53 +0000 | [diff] [blame^] | 805 | return Dump(std::move(TSM), R); |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 806 | }); |
Lang Hames | d5f56c5 | 2018-08-17 21:18:18 +0000 | [diff] [blame] | 807 | J->getMainJITDylib().setFallbackDefinitionGenerator( |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 808 | orc::DynamicLibraryFallbackGenerator( |
| 809 | std::move(LibLLI), DL, [](orc::SymbolStringPtr) { return true; })); |
| 810 | |
| 811 | orc::MangleAndInterner Mangle(J->getExecutionSession(), DL); |
| 812 | orc::LocalCXXRuntimeOverrides2 CXXRuntimeOverrides; |
Lang Hames | d5f56c5 | 2018-08-17 21:18:18 +0000 | [diff] [blame] | 813 | ExitOnErr(CXXRuntimeOverrides.enable(J->getMainJITDylib(), Mangle)); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 814 | |
Lang Hames | 37a6641 | 2018-08-28 20:20:31 +0000 | [diff] [blame] | 815 | // Add the main module. |
| 816 | ExitOnErr(J->addLazyIRModule(std::move(MainModule))); |
| 817 | |
| 818 | // Add any extra modules. |
| 819 | for (auto &ModulePath : ExtraModules) { |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 820 | auto M = parseIRFile(ModulePath, Err, *TSCtx.getContext()); |
Lang Hames | 37a6641 | 2018-08-28 20:20:31 +0000 | [diff] [blame] | 821 | if (!M) |
| 822 | reportError(Err, ProgName); |
| 823 | |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 824 | ExitOnErr(J->addLazyIRModule(orc::ThreadSafeModule(std::move(M), TSCtx))); |
Lang Hames | adae9bf | 2018-07-02 22:30:18 +0000 | [diff] [blame] | 825 | } |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 826 | |
Lang Hames | 37a6641 | 2018-08-28 20:20:31 +0000 | [diff] [blame] | 827 | // Add the objects. |
| 828 | for (auto &ObjPath : ExtraObjects) { |
| 829 | auto Obj = ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ObjPath))); |
| 830 | ExitOnErr(J->addObjectFile(std::move(Obj))); |
| 831 | } |
| 832 | |
| 833 | // Generate a argument string. |
| 834 | std::vector<std::string> Args; |
| 835 | Args.push_back(InputFile); |
| 836 | for (auto &Arg : InputArgv) |
| 837 | Args.push_back(Arg); |
| 838 | |
| 839 | // Run any static constructors. |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 840 | ExitOnErr(J->runConstructors()); |
| 841 | |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 842 | // Run any -thread-entry points. |
| 843 | std::vector<std::thread> AltEntryThreads; |
| 844 | for (auto &ThreadEntryPoint : ThreadEntryPoints) { |
| 845 | auto EntryPointSym = ExitOnErr(J->lookup(ThreadEntryPoint)); |
| 846 | typedef void (*EntryPointPtr)(); |
| 847 | auto EntryPoint = |
| 848 | reinterpret_cast<EntryPointPtr>(static_cast<uintptr_t>(EntryPointSym.getAddress())); |
| 849 | AltEntryThreads.push_back(std::thread([EntryPoint]() { EntryPoint(); })); |
| 850 | } |
| 851 | |
Lang Hames | 37a6641 | 2018-08-28 20:20:31 +0000 | [diff] [blame] | 852 | // Run main. |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 853 | auto MainSym = ExitOnErr(J->lookup("main")); |
| 854 | typedef int (*MainFnPtr)(int, const char *[]); |
| 855 | std::vector<const char *> ArgV; |
| 856 | for (auto &Arg : Args) |
| 857 | ArgV.push_back(Arg.c_str()); |
Lang Hames | 94fe9d4 | 2018-09-27 19:27:19 +0000 | [diff] [blame] | 858 | ArgV.push_back(nullptr); |
| 859 | |
| 860 | int ArgC = ArgV.size() - 1; |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 861 | auto Main = |
| 862 | reinterpret_cast<MainFnPtr>(static_cast<uintptr_t>(MainSym.getAddress())); |
Lang Hames | 94fe9d4 | 2018-09-27 19:27:19 +0000 | [diff] [blame] | 863 | auto Result = Main(ArgC, (const char **)ArgV.data()); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 864 | |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 865 | // Wait for -entry-point threads. |
| 866 | for (auto &AltEntryThread : AltEntryThreads) |
| 867 | AltEntryThread.join(); |
Hans Wennborg | 20b5abe | 2018-09-26 12:15:23 +0000 | [diff] [blame] | 868 | |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 869 | // Run destructors. |
| 870 | ExitOnErr(J->runDestructors()); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 871 | CXXRuntimeOverrides.runDestructors(); |
| 872 | |
| 873 | return Result; |
| 874 | } |
| 875 | |
Lang Hames | 1f2bf2d | 2016-11-11 21:42:09 +0000 | [diff] [blame] | 876 | std::unique_ptr<FDRawChannel> launchRemote() { |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 877 | #ifndef LLVM_ON_UNIX |
| 878 | llvm_unreachable("launchRemote not supported on non-Unix platforms"); |
| 879 | #else |
| 880 | int PipeFD[2][2]; |
| 881 | pid_t ChildPID; |
| 882 | |
| 883 | // Create two pipes. |
| 884 | if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0) |
| 885 | perror("Error creating pipe: "); |
| 886 | |
| 887 | ChildPID = fork(); |
| 888 | |
| 889 | if (ChildPID == 0) { |
| 890 | // In the child... |
| 891 | |
| 892 | // Close the parent ends of the pipes |
| 893 | close(PipeFD[0][1]); |
| 894 | close(PipeFD[1][0]); |
| 895 | |
| 896 | |
| 897 | // Execute the child process. |
| 898 | std::unique_ptr<char[]> ChildPath, ChildIn, ChildOut; |
| 899 | { |
| 900 | ChildPath.reset(new char[ChildExecPath.size() + 1]); |
| 901 | std::copy(ChildExecPath.begin(), ChildExecPath.end(), &ChildPath[0]); |
| 902 | ChildPath[ChildExecPath.size()] = '\0'; |
NAKAMURA Takumi | cef0a821 | 2016-01-15 02:14:46 +0000 | [diff] [blame] | 903 | std::string ChildInStr = utostr(PipeFD[0][0]); |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 904 | ChildIn.reset(new char[ChildInStr.size() + 1]); |
| 905 | std::copy(ChildInStr.begin(), ChildInStr.end(), &ChildIn[0]); |
| 906 | ChildIn[ChildInStr.size()] = '\0'; |
NAKAMURA Takumi | cef0a821 | 2016-01-15 02:14:46 +0000 | [diff] [blame] | 907 | std::string ChildOutStr = utostr(PipeFD[1][1]); |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 908 | ChildOut.reset(new char[ChildOutStr.size() + 1]); |
| 909 | std::copy(ChildOutStr.begin(), ChildOutStr.end(), &ChildOut[0]); |
| 910 | ChildOut[ChildOutStr.size()] = '\0'; |
| 911 | } |
| 912 | |
| 913 | char * const args[] = { &ChildPath[0], &ChildIn[0], &ChildOut[0], nullptr }; |
| 914 | int rc = execv(ChildExecPath.c_str(), args); |
| 915 | if (rc != 0) |
| 916 | perror("Error executing child process: "); |
| 917 | llvm_unreachable("Error executing child process"); |
| 918 | } |
| 919 | // else we're the parent... |
| 920 | |
| 921 | // Close the child ends of the pipes |
| 922 | close(PipeFD[0][0]); |
| 923 | close(PipeFD[1][1]); |
| 924 | |
| 925 | // Return an RPC channel connected to our end of the pipes. |
Lang Hames | 1f2bf2d | 2016-11-11 21:42:09 +0000 | [diff] [blame] | 926 | return llvm::make_unique<FDRawChannel>(PipeFD[1][0], PipeFD[0][1]); |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 927 | #endif |
| 928 | } |