#ifndef LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
#define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
-#include "llvm/ADT/STLExtras.h"
-#include "llvm/ExecutionEngine/ExecutionEngine.h"
+#include "llvm/ADT/StringRef.h"
#include "llvm/ExecutionEngine/JITSymbol.h"
-#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
-#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
+#include "llvm/ExecutionEngine/Orc/Core.h"
+#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
-#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
+#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/IR/DataLayout.h"
-#include "llvm/IR/Mangler.h"
-#include "llvm/Support/DynamicLibrary.h"
-#include "llvm/Support/raw_ostream.h"
-#include "llvm/Target/TargetMachine.h"
-#include <algorithm>
+#include "llvm/IR/LLVMContext.h"
#include <memory>
-#include <string>
-#include <vector>
namespace llvm {
namespace orc {
class KaleidoscopeJIT {
private:
+
ExecutionSession ES;
- std::shared_ptr<SymbolResolver> Resolver;
- std::unique_ptr<TargetMachine> TM;
- const DataLayout DL;
- LegacyRTDyldObjectLinkingLayer ObjectLayer;
- LegacyIRCompileLayer<decltype(ObjectLayer), SimpleCompiler> CompileLayer;
+ RTDyldObjectLinkingLayer ObjectLayer{ES, getMemoryMgr};
+ IRCompileLayer CompileLayer{ES, ObjectLayer,
+ ConcurrentIRCompiler(getJTMB())};
+ DataLayout DL{cantFail(getJTMB().getDefaultDataLayoutForTarget())};
+ MangleAndInterner Mangle{ES, DL};
+ ThreadSafeContext Ctx{llvm::make_unique<LLVMContext>()};
-public:
- KaleidoscopeJIT()
- : Resolver(createLegacyLookupResolver(
- ES,
- [this](const std::string &Name) -> JITSymbol {
- if (auto Sym = CompileLayer.findSymbol(Name, false))
- return Sym;
- else if (auto Err = Sym.takeError())
- return std::move(Err);
- if (auto SymAddr =
- RTDyldMemoryManager::getSymbolAddressInProcess(Name))
- return JITSymbol(SymAddr, JITSymbolFlags::Exported);
- return nullptr;
- },
- [](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })),
- TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
- ObjectLayer(ES,
- [this](VModuleKey) {
- return LegacyRTDyldObjectLinkingLayer::Resources{
- std::make_shared<SectionMemoryManager>(), Resolver};
- }),
- CompileLayer(ObjectLayer, SimpleCompiler(*TM)) {
- llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
+ static JITTargetMachineBuilder getJTMB() {
+ return cantFail(JITTargetMachineBuilder::detectHost());
}
- TargetMachine &getTargetMachine() { return *TM; }
-
- VModuleKey addModule(std::unique_ptr<Module> M) {
- // Add the module to the JIT with a new VModuleKey.
- auto K = ES.allocateVModule();
- cantFail(CompileLayer.addModule(K, std::move(M)));
- return K;
+ static std::unique_ptr<SectionMemoryManager> getMemoryMgr() {
+ return llvm::make_unique<SectionMemoryManager>();
}
- JITSymbol findSymbol(const std::string Name) {
- std::string MangledName;
- raw_string_ostream MangledNameStream(MangledName);
- Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
- return CompileLayer.findSymbol(MangledNameStream.str(), true);
+public:
+
+ KaleidoscopeJIT() {
+ ES.getMainJITDylib().setGenerator(
+ cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(DL)));
}
- JITTargetAddress getSymbolAddress(const std::string Name) {
- return cantFail(findSymbol(Name).getAddress());
+ const DataLayout &getDataLayout() const { return DL; }
+
+ LLVMContext &getContext() { return *Ctx.getContext(); }
+
+ void addModule(std::unique_ptr<Module> M) {
+ cantFail(CompileLayer.add(ES.getMainJITDylib(),
+ ThreadSafeModule(std::move(M), Ctx)));
}
- void removeModule(VModuleKey K) {
- cantFail(CompileLayer.removeModule(K));
+ Expected<JITEvaluatedSymbol> lookup(StringRef Name) {
+ return ES.lookup({&ES.getMainJITDylib()}, Mangle(Name.str()));
}
};