1 //===- KaleidoscopeJIT.h - A simple JIT for Kaleidoscope --------*- C++ -*-===// 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 // Contains a simple JIT definition for use in the kaleidoscope tutorials. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H 15 #define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H 16 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ExecutionEngine/ExecutionEngine.h" 19 #include "llvm/ExecutionEngine/JITSymbol.h" 20 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" 21 #include "llvm/ExecutionEngine/SectionMemoryManager.h" 22 #include "llvm/ExecutionEngine/Orc/CompileUtils.h" 23 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" 24 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h" 25 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" 26 #include "llvm/IR/DataLayout.h" 27 #include "llvm/IR/Mangler.h" 28 #include "llvm/Support/DynamicLibrary.h" 29 #include "llvm/Support/raw_ostream.h" 30 #include "llvm/Target/TargetMachine.h" 31 #include <algorithm> 32 #include <memory> 33 #include <string> 34 #include <vector> 35 36 namespace llvm { 37 namespace orc { 38 39 class KaleidoscopeJIT { 40 private: 41 ExecutionSession ES; 42 std::shared_ptr<SymbolResolver> Resolver; 43 std::unique_ptr<TargetMachine> TM; 44 const DataLayout DL; 45 RTDyldObjectLinkingLayer ObjectLayer; 46 IRCompileLayer<decltype(ObjectLayer), SimpleCompiler> CompileLayer; 47 48 public: 49 KaleidoscopeJIT() 50 : Resolver(createLegacyLookupResolver( 51 ES, 52 [this](const std::string &Name) -> JITSymbol { 53 if (auto Sym = CompileLayer.findSymbol(Name, false)) 54 return Sym; 55 else if (auto Err = Sym.takeError()) 56 return std::move(Err); 57 if (auto SymAddr = 58 RTDyldMemoryManager::getSymbolAddressInProcess(Name)) 59 return JITSymbol(SymAddr, JITSymbolFlags::Exported); 60 return nullptr; 61 }, 62 [](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })), 63 TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()), 64 ObjectLayer(ES, 65 [this](VModuleKey) { 66 return RTDyldObjectLinkingLayer::Resources{ 67 std::make_shared<SectionMemoryManager>(), Resolver}; 68 }), 69 CompileLayer(ObjectLayer, SimpleCompiler(*TM)) { 70 llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr); 71 } 72 73 TargetMachine &getTargetMachine() { return *TM; } 74 75 VModuleKey addModule(std::unique_ptr<Module> M) { 76 // Add the module to the JIT with a new VModuleKey. 77 auto K = ES.allocateVModule(); 78 cantFail(CompileLayer.addModule(K, std::move(M))); 79 return K; 80 } 81 82 JITSymbol findSymbol(const std::string Name) { 83 std::string MangledName; 84 raw_string_ostream MangledNameStream(MangledName); 85 Mangler::getNameWithPrefix(MangledNameStream, Name, DL); 86 return CompileLayer.findSymbol(MangledNameStream.str(), true); 87 } 88 89 JITTargetAddress getSymbolAddress(const std::string Name) { 90 return cantFail(findSymbol(Name).getAddress()); 91 } 92 93 void removeModule(VModuleKey K) { 94 cantFail(CompileLayer.removeModule(K)); 95 } 96 }; 97 98 } // end namespace orc 99 } // end namespace llvm 100 101 #endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H 102