3 #include "llvm/ADT/StringRef.h"
4 #include "llvm/ExecutionEngine/JITSymbol.h"
5 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
6 #include "llvm/ExecutionEngine/Orc/Core.h"
7 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
8 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
9 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
10 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
11 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/Support/DynamicLibrary.h"
15 #include "llvm/Support/Error.h"
16 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm::orc;
27 RTDyldObjectLinkingLayer ObjectLayer;
28 IRCompileLayer CompileLayer;
31 MangleAndInterner Mangle;
32 ThreadSafeContext Ctx;
37 XACCJIT(JITTargetMachineBuilder JTMB, DataLayout DL)
39 []() {
return std::make_unique<SectionMemoryManager>(); }),
40 CompileLayer(ES, ObjectLayer, ConcurrentIRCompiler(std::move(JTMB))),
41 DL(std::move(DL)), Mangle(ES, this->DL),
42 Ctx(std::make_unique<LLVMContext>()),
43 MainJD(ES.createJITDylib(
"<main>")) {
45 cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(
46 DL.getGlobalPrefix())));
47 llvm::sys::DynamicLibrary::LoadLibraryPermanently(
nullptr);
50 static Expected<std::unique_ptr<XACCJIT>> Create() {
51 auto JTMB = JITTargetMachineBuilder::detectHost();
54 return JTMB.takeError();
56 auto DL = JTMB->getDefaultDataLayoutForTarget();
58 return DL.takeError();
60 return std::make_unique<XACCJIT>(std::move(*JTMB), std::move(*DL));
63 const DataLayout &getDataLayout()
const {
return DL; }
65 LLVMContext &getContext() {
return *Ctx.getContext(); }
67 Error addModule(std::unique_ptr<Module> M) {
69 MainJD.addGenerator(cantFail(DynamicLibrarySearchGenerator::Load(
70 "/home/cades/.xacc/lib/libxacc.so", DL.getGlobalPrefix())));
71 MainJD.addGenerator(cantFail(DynamicLibrarySearchGenerator::Load(
72 "/home/cades/.xacc/lib/libqrt.so", DL.getGlobalPrefix())));
73 MainJD.addGenerator(cantFail(DynamicLibrarySearchGenerator::Load(
74 "/home/cades/.xacc/lib/libqcor.so", DL.getGlobalPrefix())));
75 MainJD.addGenerator(cantFail(DynamicLibrarySearchGenerator::Load(
76 "/home/cades/.xacc/lib/libCppMicroServices.so", DL.getGlobalPrefix())));
78 return CompileLayer.add(MainJD, ThreadSafeModule(std::move(M), Ctx));
81 Expected<JITEvaluatedSymbol> lookup(StringRef Name) {
82 return ES.lookup({&MainJD}, Mangle(Name.str()));