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