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/SectionMemoryManager.h" 26 #include "llvm/IR/DataLayout.h" 27 #include "llvm/IR/LLVMContext.h" 28 #include <memory> 29 30 namespace llvm { 31 namespace orc { 32 33 class KaleidoscopeJIT { 34 private: 35 std::unique_ptr<ExecutionSession> ES; 36 37 DataLayout DL; 38 MangleAndInterner Mangle; 39 40 RTDyldObjectLinkingLayer ObjectLayer; 41 IRCompileLayer CompileLayer; 42 43 JITDylib &MainJD; 44 45 public: KaleidoscopeJIT(std::unique_ptr<ExecutionSession> ES,JITTargetMachineBuilder JTMB,DataLayout DL)46 KaleidoscopeJIT(std::unique_ptr<ExecutionSession> ES, 47 JITTargetMachineBuilder JTMB, DataLayout DL) 48 : ES(std::move(ES)), DL(std::move(DL)), Mangle(*this->ES, this->DL), 49 ObjectLayer(*this->ES, 50 []() { return std::make_unique<SectionMemoryManager>(); }), 51 CompileLayer(*this->ES, ObjectLayer, 52 std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))), 53 MainJD(this->ES->createBareJITDylib("<main>")) { 54 MainJD.addGenerator( 55 cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess( 56 DL.getGlobalPrefix()))); 57 if (JTMB.getTargetTriple().isOSBinFormatCOFF()) { 58 ObjectLayer.setOverrideObjectFlagsWithResponsibilityFlags(true); 59 ObjectLayer.setAutoClaimResponsibilityForObjectSymbols(true); 60 } 61 } 62 ~KaleidoscopeJIT()63 ~KaleidoscopeJIT() { 64 if (auto Err = ES->endSession()) 65 ES->reportError(std::move(Err)); 66 } 67 Create()68 static Expected<std::unique_ptr<KaleidoscopeJIT>> Create() { 69 auto EPC = SelfExecutorProcessControl::Create(); 70 if (!EPC) 71 return EPC.takeError(); 72 73 auto ES = std::make_unique<ExecutionSession>(std::move(*EPC)); 74 75 JITTargetMachineBuilder JTMB( 76 ES->getExecutorProcessControl().getTargetTriple()); 77 78 auto DL = JTMB.getDefaultDataLayoutForTarget(); 79 if (!DL) 80 return DL.takeError(); 81 82 return std::make_unique<KaleidoscopeJIT>(std::move(ES), std::move(JTMB), 83 std::move(*DL)); 84 } 85 getDataLayout()86 const DataLayout &getDataLayout() const { return DL; } 87 getMainJITDylib()88 JITDylib &getMainJITDylib() { return MainJD; } 89 90 Error addModule(ThreadSafeModule TSM, ResourceTrackerSP RT = nullptr) { 91 if (!RT) 92 RT = MainJD.getDefaultResourceTracker(); 93 return CompileLayer.add(RT, std::move(TSM)); 94 } 95 lookup(StringRef Name)96 Expected<JITEvaluatedSymbol> lookup(StringRef Name) { 97 return ES->lookup({&MainJD}, Mangle(Name.str())); 98 } 99 }; 100 101 } // end namespace orc 102 } // end namespace llvm 103 104 #endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H 105