| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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: |
| 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 |
} |
| 58 |
|
| 59 |
~KaleidoscopeJIT() { |
| 60 |
if (auto Err = ES->endSession()) |
| 61 |
ES->reportError(std::move(Err)); |
| 62 |
} |
| 63 |
|
| 64 |
static Expected<std::unique_ptr<KaleidoscopeJIT>> Create() { |
| 65 |
auto EPC = SelfExecutorProcessControl::Create(); |
| 66 |
if (!EPC) |
| 67 |
return EPC.takeError(); |
| 68 |
|
| 69 |
auto ES = std::make_unique<ExecutionSession>(std::move(*EPC)); |
| 70 |
|
| 71 |
JITTargetMachineBuilder JTMB( |
| 72 |
ES->getExecutorProcessControl().getTargetTriple()); |
| 73 |
|
| 74 |
auto DL = JTMB.getDefaultDataLayoutForTarget(); |
| 75 |
if (!DL) |
| 76 |
return DL.takeError(); |
| 77 |
|
| 78 |
return std::make_unique<KaleidoscopeJIT>(std::move(ES), std::move(JTMB), |
| 79 |
std::move(*DL)); |
| 80 |
} |
| 81 |
|
| 82 |
const DataLayout &getDataLayout() const { return DL; } |
| 83 |
|
| 84 |
JITDylib &getMainJITDylib() { return MainJD; } |
| 85 |
|
| 86 |
Error addModule(ThreadSafeModule TSM, ResourceTrackerSP RT = nullptr) { |
| 87 |
if (!RT) |
| 88 |
RT = MainJD.getDefaultResourceTracker(); |
| 89 |
return CompileLayer.add(RT, std::move(TSM)); |
| 90 |
} |
| 91 |
|
| 92 |
Expected<JITEvaluatedSymbol> lookup(StringRef Name) { |
| 93 |
return ES->lookup({&MainJD}, Mangle(Name.str())); |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
#endif |