1 //===- KaleidoscopeJIT.h - A simple JIT for Kaleidoscope --------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Contains a simple JIT definition for use in the kaleidoscope tutorials. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H 14 #define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H 15 16 #include "llvm/ADT/StringRef.h" 17 #include "llvm/ExecutionEngine/JITSymbol.h" 18 #include "llvm/ExecutionEngine/Orc/CompileUtils.h" 19 #include "llvm/ExecutionEngine/Orc/Core.h" 20 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h" 21 #include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h" 22 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" 23 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h" 24 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" 25 #include "llvm/ExecutionEngine/Orc/Shared/ExecutorSymbolDef.h" 26 #include "llvm/ExecutionEngine/SectionMemoryManager.h" 27 #include "llvm/IR/DataLayout.h" 28 #include "llvm/IR/LLVMContext.h" 29 #include <memory> 30 31 namespace llvm { 32 namespace orc { 33 34 class KaleidoscopeJIT { 35 private: 36 std::unique_ptr<ExecutionSession> ES; 37 38 DataLayout DL; 39 MangleAndInterner Mangle; 40 41 RTDyldObjectLinkingLayer ObjectLayer; 42 IRCompileLayer CompileLayer; 43 44 JITDylib &MainJD; 45 46 public: KaleidoscopeJIT(std::unique_ptr<ExecutionSession> ES,JITTargetMachineBuilder JTMB,DataLayout DL)47 KaleidoscopeJIT(std::unique_ptr<ExecutionSession> ES, 48 JITTargetMachineBuilder JTMB, DataLayout DL) 49 : ES(std::move(ES)), DL(std::move(DL)), Mangle(*this->ES, this->DL), 50 ObjectLayer(*this->ES, 51 []() { return std::make_unique<SectionMemoryManager>(); }), 52 CompileLayer(*this->ES, ObjectLayer, 53 std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))), 54 MainJD(this->ES->createBareJITDylib("<main>")) { 55 MainJD.addGenerator( 56 cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess( 57 DL.getGlobalPrefix()))); 58 if (JTMB.getTargetTriple().isOSBinFormatCOFF()) { 59 ObjectLayer.setOverrideObjectFlagsWithResponsibilityFlags(true); 60 ObjectLayer.setAutoClaimResponsibilityForObjectSymbols(true); 61 } 62 } 63 ~KaleidoscopeJIT()64 ~KaleidoscopeJIT() { 65 if (auto Err = ES->endSession()) 66 ES->reportError(std::move(Err)); 67 } 68 Create()69 static Expected<std::unique_ptr<KaleidoscopeJIT>> Create() { 70 auto EPC = SelfExecutorProcessControl::Create(); 71 if (!EPC) 72 return EPC.takeError(); 73 74 auto ES = std::make_unique<ExecutionSession>(std::move(*EPC)); 75 76 JITTargetMachineBuilder JTMB( 77 ES->getExecutorProcessControl().getTargetTriple()); 78 79 auto DL = JTMB.getDefaultDataLayoutForTarget(); 80 if (!DL) 81 return DL.takeError(); 82 83 return std::make_unique<KaleidoscopeJIT>(std::move(ES), std::move(JTMB), 84 std::move(*DL)); 85 } 86 getDataLayout()87 const DataLayout &getDataLayout() const { return DL; } 88 getMainJITDylib()89 JITDylib &getMainJITDylib() { return MainJD; } 90 91 Error addModule(ThreadSafeModule TSM, ResourceTrackerSP RT = nullptr) { 92 if (!RT) 93 RT = MainJD.getDefaultResourceTracker(); 94 return CompileLayer.add(RT, std::move(TSM)); 95 } 96 lookup(StringRef Name)97 Expected<ExecutorSymbolDef> lookup(StringRef Name) { 98 return ES->lookup({&MainJD}, Mangle(Name.str())); 99 } 100 }; 101 102 } // end namespace orc 103 } // end namespace llvm 104 105 #endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H 106