#ifndef LLVM_EXECUTIONENGINE_ORC_LLJIT_H
#define LLVM_EXECUTIONENGINE_ORC_LLJIT_H
#include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
#include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ThreadPool.h"
namespace llvm {
namespace orc {
class LLJITBuilderState;
class LLLazyJITBuilderState;
class ObjectTransformLayer;
class ExecutorProcessControl;
class LLJIT {
template <typename, typename, typename> friend class LLJITBuilderSetters;
friend void setUpGenericLLVMIRPlatform(LLJIT &J);
public:
class PlatformSupport {
public:
virtual ~PlatformSupport();
virtual Error initialize(JITDylib &JD) = 0;
virtual Error deinitialize(JITDylib &JD) = 0;
protected:
static void setInitTransform(LLJIT &J,
IRTransformLayer::TransformFunction T);
};
virtual ~LLJIT();
ExecutionSession &getExecutionSession() { return *ES; }
const Triple &getTargetTriple() const { return TT; }
const DataLayout &getDataLayout() const { return DL; }
JITDylib &getMainJITDylib() { return *Main; }
JITDylib *getJITDylibByName(StringRef Name) {
return ES->getJITDylibByName(Name);
}
Expected<JITDylib &> createJITDylib(std::string Name) {
return ES->createJITDylib(std::move(Name));
}
Error addIRModule(ResourceTrackerSP RT, ThreadSafeModule TSM);
Error addIRModule(JITDylib &JD, ThreadSafeModule TSM);
Error addIRModule(ThreadSafeModule TSM) {
return addIRModule(*Main, std::move(TSM));
}
Error addObjectFile(ResourceTrackerSP RT, std::unique_ptr<MemoryBuffer> Obj);
Error addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj);
Error addObjectFile(std::unique_ptr<MemoryBuffer> Obj) {
return addObjectFile(*Main, std::move(Obj));
}
Expected<ExecutorAddr> lookupLinkerMangled(JITDylib &JD,
SymbolStringPtr Name);
Expected<ExecutorAddr> lookupLinkerMangled(JITDylib &JD,
StringRef Name) {
return lookupLinkerMangled(JD, ES->intern(Name));
}
Expected<ExecutorAddr> lookupLinkerMangled(StringRef Name) {
return lookupLinkerMangled(*Main, Name);
}
Expected<ExecutorAddr> lookup(JITDylib &JD, StringRef UnmangledName) {
return lookupLinkerMangled(JD, mangle(UnmangledName));
}
Expected<ExecutorAddr> lookup(StringRef UnmangledName) {
return lookup(*Main, UnmangledName);
}
void setPlatformSupport(std::unique_ptr<PlatformSupport> PS) {
this->PS = std::move(PS);
}
PlatformSupport *getPlatformSupport() { return PS.get(); }
Error initialize(JITDylib &JD) {
DEBUG_WITH_TYPE("orc", {
dbgs() << "LLJIT running initializers for JITDylib \"" << JD.getName()
<< "\"\n";
});
assert(PS && "PlatformSupport must be set to run initializers.");
return PS->initialize(JD);
}
Error deinitialize(JITDylib &JD) {
DEBUG_WITH_TYPE("orc", {
dbgs() << "LLJIT running deinitializers for JITDylib \"" << JD.getName()
<< "\"\n";
});
assert(PS && "PlatformSupport must be set to run initializers.");
return PS->deinitialize(JD);
}
ObjectLayer &getObjLinkingLayer() { return *ObjLinkingLayer; }
ObjectTransformLayer &getObjTransformLayer() { return *ObjTransformLayer; }
IRTransformLayer &getIRTransformLayer() { return *TransformLayer; }
IRCompileLayer &getIRCompileLayer() { return *CompileLayer; }
std::string mangle(StringRef UnmangledName) const;
SymbolStringPtr mangleAndIntern(StringRef UnmangledName) const {
return ES->intern(mangle(UnmangledName));
}
protected:
static Expected<std::unique_ptr<ObjectLayer>>
createObjectLinkingLayer(LLJITBuilderState &S, ExecutionSession &ES);
static Expected<std::unique_ptr<IRCompileLayer::IRCompiler>>
createCompileFunction(LLJITBuilderState &S, JITTargetMachineBuilder JTMB);
LLJIT(LLJITBuilderState &S, Error &Err);
Error applyDataLayout(Module &M);
void recordCtorDtors(Module &M);
std::unique_ptr<ExecutionSession> ES;
std::unique_ptr<PlatformSupport> PS;
JITDylib *Main = nullptr;
DataLayout DL;
Triple TT;
std::unique_ptr<ThreadPool> CompileThreads;
std::unique_ptr<ObjectLayer> ObjLinkingLayer;
std::unique_ptr<ObjectTransformLayer> ObjTransformLayer;
std::unique_ptr<IRCompileLayer> CompileLayer;
std::unique_ptr<IRTransformLayer> TransformLayer;
std::unique_ptr<IRTransformLayer> InitHelperTransformLayer;
};
class LLLazyJIT : public LLJIT {
template <typename, typename, typename> friend class LLJITBuilderSetters;
public:
void
setPartitionFunction(CompileOnDemandLayer::PartitionFunction Partition) {
CODLayer->setPartitionFunction(std::move(Partition));
}
CompileOnDemandLayer &getCompileOnDemandLayer() { return *CODLayer; }
Error addLazyIRModule(JITDylib &JD, ThreadSafeModule M);
Error addLazyIRModule(ThreadSafeModule M) {
return addLazyIRModule(*Main, std::move(M));
}
private:
LLLazyJIT(LLLazyJITBuilderState &S, Error &Err);
std::unique_ptr<LazyCallThroughManager> LCTMgr;
std::unique_ptr<CompileOnDemandLayer> CODLayer;
};
class LLJITBuilderState {
public:
using ObjectLinkingLayerCreator =
std::function<Expected<std::unique_ptr<ObjectLayer>>(ExecutionSession &,
const Triple &)>;
using CompileFunctionCreator =
std::function<Expected<std::unique_ptr<IRCompileLayer::IRCompiler>>(
JITTargetMachineBuilder JTMB)>;
using PlatformSetupFunction = std::function<Error(LLJIT &J)>;
std::unique_ptr<ExecutorProcessControl> EPC;
std::unique_ptr<ExecutionSession> ES;
Optional<JITTargetMachineBuilder> JTMB;
Optional<DataLayout> DL;
ObjectLinkingLayerCreator CreateObjectLinkingLayer;
CompileFunctionCreator CreateCompileFunction;
PlatformSetupFunction SetUpPlatform;
unsigned NumCompileThreads = 0;
Error prepareForConstruction();
};
template <typename JITType, typename SetterImpl, typename State>
class LLJITBuilderSetters {
public:
SetterImpl &
setExecutorProcessControl(std::unique_ptr<ExecutorProcessControl> EPC) {
assert(
!impl().ES &&
"setExecutorProcessControl should not be called if an ExecutionSession "
"has already been set");
impl().EPC = std::move(EPC);
return impl();
}
SetterImpl &setExecutionSession(std::unique_ptr<ExecutionSession> ES) {
impl().ES = std::move(ES);
return impl();
}
SetterImpl &setJITTargetMachineBuilder(JITTargetMachineBuilder JTMB) {
impl().JTMB = std::move(JTMB);
return impl();
}
Optional<JITTargetMachineBuilder> &getJITTargetMachineBuilder() {
return impl().JTMB;
}
SetterImpl &setDataLayout(Optional<DataLayout> DL) {
impl().DL = std::move(DL);
return impl();
}
SetterImpl &setObjectLinkingLayerCreator(
LLJITBuilderState::ObjectLinkingLayerCreator CreateObjectLinkingLayer) {
impl().CreateObjectLinkingLayer = std::move(CreateObjectLinkingLayer);
return impl();
}
SetterImpl &setCompileFunctionCreator(
LLJITBuilderState::CompileFunctionCreator CreateCompileFunction) {
impl().CreateCompileFunction = std::move(CreateCompileFunction);
return impl();
}
SetterImpl &
setPlatformSetUp(LLJITBuilderState::PlatformSetupFunction SetUpPlatform) {
impl().SetUpPlatform = std::move(SetUpPlatform);
return impl();
}
SetterImpl &setNumCompileThreads(unsigned NumCompileThreads) {
impl().NumCompileThreads = NumCompileThreads;
return impl();
}
SetterImpl &setExecutorProcessControl(ExecutorProcessControl &EPC) {
impl().EPC = &EPC;
return impl();
}
Expected<std::unique_ptr<JITType>> create() {
if (auto Err = impl().prepareForConstruction())
return std::move(Err);
Error Err = Error::success();
std::unique_ptr<JITType> J(new JITType(impl(), Err));
if (Err)
return std::move(Err);
return std::move(J);
}
protected:
SetterImpl &impl() { return static_cast<SetterImpl &>(*this); }
};
class LLJITBuilder
: public LLJITBuilderState,
public LLJITBuilderSetters<LLJIT, LLJITBuilder, LLJITBuilderState> {};
class LLLazyJITBuilderState : public LLJITBuilderState {
friend class LLLazyJIT;
public:
using IndirectStubsManagerBuilderFunction =
std::function<std::unique_ptr<IndirectStubsManager>()>;
Triple TT;
ExecutorAddr LazyCompileFailureAddr;
std::unique_ptr<LazyCallThroughManager> LCTMgr;
IndirectStubsManagerBuilderFunction ISMBuilder;
Error prepareForConstruction();
};
template <typename JITType, typename SetterImpl, typename State>
class LLLazyJITBuilderSetters
: public LLJITBuilderSetters<JITType, SetterImpl, State> {
public:
SetterImpl &setLazyCompileFailureAddr(ExecutorAddr Addr) {
this->impl().LazyCompileFailureAddr = Addr;
return this->impl();
}
SetterImpl &
setLazyCallthroughManager(std::unique_ptr<LazyCallThroughManager> LCTMgr) {
this->impl().LCTMgr = std::move(LCTMgr);
return this->impl();
}
SetterImpl &setIndirectStubsManagerBuilder(
LLLazyJITBuilderState::IndirectStubsManagerBuilderFunction ISMBuilder) {
this->impl().ISMBuilder = std::move(ISMBuilder);
return this->impl();
}
};
class LLLazyJITBuilder
: public LLLazyJITBuilderState,
public LLLazyJITBuilderSetters<LLLazyJIT, LLLazyJITBuilder,
LLLazyJITBuilderState> {};
void setUpGenericLLVMIRPlatform(LLJIT &J);
Error setUpInactivePlatform(LLJIT &J);
} }
#endif