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 | 5a9808b | 2015-04-01 04:42:56 +0000 | [diff] [blame] | 12 | #include "llvm/Support/DynamicLibrary.h" |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace llvm; |
| 15 | |
Lang Hames | 3c9e20d | 2015-03-30 18:37:01 +0000 | [diff] [blame] | 16 | OrcLazyJIT::CallbackManagerBuilder |
| 17 | OrcLazyJIT::createCallbackManagerBuilder(Triple T) { |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 18 | switch (T.getArch()) { |
Lang Hames | 3c9e20d | 2015-03-30 18:37:01 +0000 | [diff] [blame] | 19 | default: return nullptr; |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 20 | |
| 21 | case Triple::x86_64: { |
| 22 | typedef orc::JITCompileCallbackManager<CompileLayerT, |
| 23 | orc::OrcX86_64> CCMgrT; |
Lang Hames | 3c9e20d | 2015-03-30 18:37:01 +0000 | [diff] [blame] | 24 | return [](CompileLayerT &CompileLayer, RuntimeDyld::MemoryManager &MemMgr, |
| 25 | LLVMContext &Context) { |
| 26 | return make_unique<CCMgrT>(CompileLayer, MemMgr, Context, 0, 64); |
| 27 | }; |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | int llvm::runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]) { |
Lang Hames | 5a9808b | 2015-04-01 04:42:56 +0000 | [diff] [blame] | 33 | // Add the program's symbols into the JIT's search space. |
| 34 | if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) { |
| 35 | errs() << "Error loading program symbols.\n"; |
| 36 | return 1; |
| 37 | } |
| 38 | |
| 39 | // Grab a target machine and try to build a factory function for the |
| 40 | // target-specific Orc callback manager. |
Lang Hames | 3c9e20d | 2015-03-30 18:37:01 +0000 | [diff] [blame] | 41 | auto TM = std::unique_ptr<TargetMachine>(EngineBuilder().selectTarget()); |
| 42 | auto &Context = getGlobalContext(); |
| 43 | auto CallbackMgrBuilder = |
| 44 | OrcLazyJIT::createCallbackManagerBuilder(Triple(TM->getTargetTriple())); |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 45 | |
Lang Hames | 5a9808b | 2015-04-01 04:42:56 +0000 | [diff] [blame] | 46 | // If we couldn't build the factory function then there must not be a callback |
| 47 | // manager for this target. Bail out. |
Lang Hames | 3c9e20d | 2015-03-30 18:37:01 +0000 | [diff] [blame] | 48 | if (!CallbackMgrBuilder) { |
| 49 | errs() << "No callback manager available for target '" |
| 50 | << TM->getTargetTriple() << "'.\n"; |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 51 | return 1; |
| 52 | } |
| 53 | |
Lang Hames | 5a9808b | 2015-04-01 04:42:56 +0000 | [diff] [blame] | 54 | // Everything looks good. Build the JIT. |
Lang Hames | 3c9e20d | 2015-03-30 18:37:01 +0000 | [diff] [blame] | 55 | OrcLazyJIT J(std::move(TM), Context, CallbackMgrBuilder); |
| 56 | |
Lang Hames | 5a9808b | 2015-04-01 04:42:56 +0000 | [diff] [blame] | 57 | // Add the module, look up main and run it. |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 58 | auto MainHandle = J.addModule(std::move(M)); |
| 59 | auto MainSym = J.findSymbolIn(MainHandle, "main"); |
| 60 | |
| 61 | if (!MainSym) { |
| 62 | errs() << "Could not find main function.\n"; |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | typedef int (*MainFnPtr)(int, char*[]); |
Lang Hames | b1cd98a | 2015-04-02 04:34:45 +0000 | [diff] [blame^] | 67 | auto Main = OrcLazyJIT::fromTargetAddress<MainFnPtr>(MainSym.getAddress()); |
Lang Hames | 9528bba | 2015-03-25 12:11:48 +0000 | [diff] [blame] | 68 | return Main(ArgC, ArgV); |
| 69 | } |