Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 1 | //===------ OrcLazyJIT.cpp - Basic Orc-based JIT for lazy execution -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "OrcLazyJIT.h" |
| 11 | #include "llvm/ExecutionEngine/Orc/OrcTargetSupport.h" |
Lang Hames | cd3fd83 | 2015-04-13 22:12:54 +0000 | [diff] [blame^] | 12 | #include "llvm/Support/Debug.h" |
Lang Hames | 5a9808b | 2015-04-01 04:42:56 +0000 | [diff] [blame] | 13 | #include "llvm/Support/DynamicLibrary.h" |
Lang Hames | cd3fd83 | 2015-04-13 22:12:54 +0000 | [diff] [blame^] | 14 | #include <system_error> |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace llvm; |
| 17 | |
Lang Hames | cd3fd83 | 2015-04-13 22:12:54 +0000 | [diff] [blame^] | 18 | namespace { |
| 19 | |
| 20 | enum class DumpKind { NoDump, DumpFuncsToStdErr, DumpModsToStdErr, |
| 21 | DumpModsToDisk }; |
| 22 | |
| 23 | cl::opt<DumpKind> OrcDumpKind("orc-lazy-debug", |
| 24 | cl::desc("Debug dumping for the orc-lazy JIT."), |
| 25 | cl::init(DumpKind::NoDump), |
| 26 | cl::values( |
| 27 | clEnumValN(DumpKind::NoDump, "no-dump", |
| 28 | "Don't dump anything."), |
| 29 | clEnumValN(DumpKind::DumpFuncsToStdErr, |
| 30 | "funcs-to-stderr", |
| 31 | "Dump function names to stderr."), |
| 32 | clEnumValN(DumpKind::DumpModsToStdErr, |
| 33 | "mods-to-stderr", |
| 34 | "Dump modules to stderr."), |
| 35 | clEnumValN(DumpKind::DumpModsToDisk, |
| 36 | "mods-to-disk", |
| 37 | "Dump modules to the current " |
| 38 | "working directory. (WARNING: " |
| 39 | "will overwrite existing files)."), |
| 40 | clEnumValEnd)); |
| 41 | } |
| 42 | |
Lang Hames | 3c9e20d | 2015-03-30 18:37:01 +0000 | [diff] [blame] | 43 | OrcLazyJIT::CallbackManagerBuilder |
| 44 | OrcLazyJIT::createCallbackManagerBuilder(Triple T) { |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 45 | switch (T.getArch()) { |
Lang Hames | 3c9e20d | 2015-03-30 18:37:01 +0000 | [diff] [blame] | 46 | default: return nullptr; |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 47 | |
| 48 | case Triple::x86_64: { |
Lang Hames | cd3fd83 | 2015-04-13 22:12:54 +0000 | [diff] [blame^] | 49 | typedef orc::JITCompileCallbackManager<IRDumpLayerT, |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 50 | orc::OrcX86_64> CCMgrT; |
Lang Hames | cd3fd83 | 2015-04-13 22:12:54 +0000 | [diff] [blame^] | 51 | return [](IRDumpLayerT &IRDumpLayer, RuntimeDyld::MemoryManager &MemMgr, |
Lang Hames | 3c9e20d | 2015-03-30 18:37:01 +0000 | [diff] [blame] | 52 | LLVMContext &Context) { |
Lang Hames | cd3fd83 | 2015-04-13 22:12:54 +0000 | [diff] [blame^] | 53 | return make_unique<CCMgrT>(IRDumpLayer, MemMgr, Context, 0, 64); |
Lang Hames | 3c9e20d | 2015-03-30 18:37:01 +0000 | [diff] [blame] | 54 | }; |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
Lang Hames | cd3fd83 | 2015-04-13 22:12:54 +0000 | [diff] [blame^] | 59 | OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() { |
| 60 | |
| 61 | switch (OrcDumpKind) { |
| 62 | case DumpKind::NoDump: |
| 63 | return [](std::unique_ptr<Module> M) { return std::move(M); }; |
| 64 | |
| 65 | case DumpKind::DumpFuncsToStdErr: |
| 66 | return [](std::unique_ptr<Module> M) { |
| 67 | dbgs() << "[ "; |
| 68 | |
| 69 | for (const auto &F : *M) { |
| 70 | if (F.isDeclaration()) |
| 71 | continue; |
| 72 | |
| 73 | if (F.hasName()) |
| 74 | dbgs() << F.getName() << " "; |
| 75 | else |
| 76 | dbgs() << "<anon> "; |
| 77 | } |
| 78 | |
| 79 | dbgs() << "]\n"; |
| 80 | return std::move(M); |
| 81 | }; |
| 82 | |
| 83 | case DumpKind::DumpModsToStdErr: |
| 84 | return [](std::unique_ptr<Module> M) { |
| 85 | dbgs() << "----- Module Start -----\n" << *M |
| 86 | << "----- Module End -----\n"; |
| 87 | |
| 88 | return std::move(M); |
| 89 | }; |
| 90 | |
| 91 | case DumpKind::DumpModsToDisk: |
| 92 | return [](std::unique_ptr<Module> M) { |
| 93 | std::error_code EC; |
| 94 | raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC, |
| 95 | sys::fs::F_Text); |
| 96 | if (EC) { |
| 97 | errs() << "Couldn't open " << M->getModuleIdentifier() |
| 98 | << " for dumping.\nError:" << EC.message() << "\n"; |
| 99 | exit(1); |
| 100 | } |
| 101 | Out << *M; |
| 102 | return std::move(M); |
| 103 | }; |
| 104 | } |
| 105 | } |
| 106 | |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 107 | int llvm::runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]) { |
Lang Hames | 5a9808b | 2015-04-01 04:42:56 +0000 | [diff] [blame] | 108 | // Add the program's symbols into the JIT's search space. |
| 109 | if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) { |
| 110 | errs() << "Error loading program symbols.\n"; |
| 111 | return 1; |
| 112 | } |
| 113 | |
| 114 | // Grab a target machine and try to build a factory function for the |
| 115 | // target-specific Orc callback manager. |
Lang Hames | 3c9e20d | 2015-03-30 18:37:01 +0000 | [diff] [blame] | 116 | auto TM = std::unique_ptr<TargetMachine>(EngineBuilder().selectTarget()); |
| 117 | auto &Context = getGlobalContext(); |
| 118 | auto CallbackMgrBuilder = |
| 119 | OrcLazyJIT::createCallbackManagerBuilder(Triple(TM->getTargetTriple())); |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 120 | |
Lang Hames | 5a9808b | 2015-04-01 04:42:56 +0000 | [diff] [blame] | 121 | // If we couldn't build the factory function then there must not be a callback |
| 122 | // manager for this target. Bail out. |
Lang Hames | 3c9e20d | 2015-03-30 18:37:01 +0000 | [diff] [blame] | 123 | if (!CallbackMgrBuilder) { |
| 124 | errs() << "No callback manager available for target '" |
| 125 | << TM->getTargetTriple() << "'.\n"; |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 126 | return 1; |
| 127 | } |
| 128 | |
Lang Hames | 5a9808b | 2015-04-01 04:42:56 +0000 | [diff] [blame] | 129 | // Everything looks good. Build the JIT. |
Lang Hames | 3c9e20d | 2015-03-30 18:37:01 +0000 | [diff] [blame] | 130 | OrcLazyJIT J(std::move(TM), Context, CallbackMgrBuilder); |
| 131 | |
Lang Hames | 5a9808b | 2015-04-01 04:42:56 +0000 | [diff] [blame] | 132 | // Add the module, look up main and run it. |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 133 | auto MainHandle = J.addModule(std::move(M)); |
| 134 | auto MainSym = J.findSymbolIn(MainHandle, "main"); |
| 135 | |
| 136 | if (!MainSym) { |
| 137 | errs() << "Could not find main function.\n"; |
| 138 | return 1; |
| 139 | } |
| 140 | |
| 141 | typedef int (*MainFnPtr)(int, char*[]); |
Lang Hames | b1cd98a | 2015-04-02 04:34:45 +0000 | [diff] [blame] | 142 | auto Main = OrcLazyJIT::fromTargetAddress<MainFnPtr>(MainSym.getAddress()); |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 143 | return Main(ArgC, ArgV); |
| 144 | } |