diff options
author | Johannes Stoelp <johannes.stoelp@gmail.com> | 2022-11-05 23:24:05 +0100 |
---|---|---|
committer | Johannes Stoelp <johannes.stoelp@gmail.com> | 2022-11-05 23:24:05 +0100 |
commit | b14fce113fa45da85c8c3528552ae446ced340ca (patch) | |
tree | 5e7a68d113f8b4e3649a2c9cbcd5cd85442d0052 /content/2022-07-07-llvm-orc-jit/jit.h | |
parent | cfaa38627b3d315d47ddad295962fe0c92d74b84 (diff) | |
download | blog-b14fce113fa45da85c8c3528552ae446ced340ca.tar.gz blog-b14fce113fa45da85c8c3528552ae446ced340ca.zip |
llvm: small ORCv2 jit example
Diffstat (limited to 'content/2022-07-07-llvm-orc-jit/jit.h')
-rw-r--r-- | content/2022-07-07-llvm-orc-jit/jit.h | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/content/2022-07-07-llvm-orc-jit/jit.h b/content/2022-07-07-llvm-orc-jit/jit.h new file mode 100644 index 0000000..10d6d4a --- /dev/null +++ b/content/2022-07-07-llvm-orc-jit/jit.h @@ -0,0 +1,100 @@ +#ifndef JIT_H +#define JIT_H + +#include <llvm/ExecutionEngine/JITSymbol.h> +#include <llvm/ExecutionEngine/Orc/CompileUtils.h> +#include <llvm/ExecutionEngine/Orc/Core.h> +#include <llvm/ExecutionEngine/Orc/ExecutionUtils.h> +#include <llvm/ExecutionEngine/Orc/ExecutorProcessControl.h> +#include <llvm/ExecutionEngine/Orc/IRCompileLayer.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/LLVMContext.h> + +namespace jit { + +using llvm::DataLayout; +using llvm::Expected; +using llvm::JITEvaluatedSymbol; +using llvm::SectionMemoryManager; +using llvm::StringRef; + +using llvm::orc::ConcurrentIRCompiler; +// using llvm::orc::DynamicLibrarySearchGenerator; +using llvm::orc::ExecutionSession; +using llvm::orc::IRCompileLayer; +using llvm::orc::JITDylib; +using llvm::orc::JITTargetMachineBuilder; +using llvm::orc::MangleAndInterner; +using llvm::orc::ResourceTrackerSP; +using llvm::orc::RTDyldObjectLinkingLayer; +using llvm::orc::SelfExecutorProcessControl; +using llvm::orc::ThreadSafeModule; + +// Simple JIT engine based on the KaleidoscopeJIT. +// https://www.llvm.org/docs/tutorial/BuildingAJIT1.html +class Jit { +private: + std::unique_ptr<ExecutionSession> ES; + + DataLayout DL; + MangleAndInterner Mangle; + + RTDyldObjectLinkingLayer ObjectLayer; + IRCompileLayer CompileLayer; + + JITDylib &JD; + +public: + Jit(std::unique_ptr<ExecutionSession> ES, JITTargetMachineBuilder JTMB, + DataLayout DL) + : ES(std::move(ES)), DL(std::move(DL)), Mangle(*this->ES, this->DL), + ObjectLayer(*this->ES, + []() { return std::make_unique<SectionMemoryManager>(); }), + CompileLayer(*this->ES, ObjectLayer, + std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))), + JD(this->ES->createBareJITDylib("main")) { + // https://www.llvm.org/docs/ORCv2.html#how-to-add-process-and-library-symbols-to-jitdylibs + // JD.addGenerator( + // cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess( + // DL.getGlobalPrefix()))); + (void)JD.define(llvm::orc::absoluteSymbols( + {{Mangle("libc_puts"), llvm::JITEvaluatedSymbol::fromPointer(&puts)}})); + } + + ~Jit() { + if (auto Err = ES->endSession()) + ES->reportError(std::move(Err)); + } + + static std::unique_ptr<Jit> Create() { + auto EPC = cantFail(SelfExecutorProcessControl::Create()); + auto ES = std::make_unique<ExecutionSession>(std::move(EPC)); + + JITTargetMachineBuilder JTMB( + ES->getExecutorProcessControl().getTargetTriple()); + + auto DL = cantFail(JTMB.getDefaultDataLayoutForTarget()); + + return std::make_unique<Jit>(std::move(ES), std::move(JTMB), std::move(DL)); + } + + // Error addModule(ThreadSafeModule TSM) { + Expected<ResourceTrackerSP> addModule(ThreadSafeModule TSM) { + auto RT = JD.createResourceTracker(); + if (auto E = CompileLayer.add(RT, std::move(TSM))) { + return E; + } + return RT; + } + + Expected<JITEvaluatedSymbol> lookup(StringRef Name) { + return ES->lookup({&JD}, Mangle(Name.str())); + } +}; + +} // namespace jit + +#endif |