xo-jit: + machgen_current_module(): generate machine code!

This commit is contained in:
Roland Conybeare 2024-06-15 17:13:59 -04:00
commit 1b3718bd12
2 changed files with 48 additions and 6 deletions

View file

@ -98,6 +98,13 @@ namespace xo {
llvm::Value * codegen(ref::brw<Expression> expr);
// ----- jit online execution -----
/** add IR code in current module to JIT,
* so that its available for execution
**/
void machgen_current_module();
llvm::orc::ExecutorAddr lookup_symbol(const std::string & x);
virtual void display(std::ostream & os) const;
@ -116,6 +123,9 @@ namespace xo {
/* iniitialize native builder (i.e. for platform we're running on) */
static void init_once();
/** (re)create pipeline to turn expressions into llvm IR code **/
void recreate_llvm_ir_pipeline();
private:
// ----- this part adapted from LLVM 19.0 KaleidoscopeJIT.hpp [wip] -----
@ -133,6 +143,14 @@ namespace xo {
// ----- this part adapted from kaleidoscope.cpp -----
/** everything bleow represents a pipeline
* that takes expressions, and turns them into llvm IR.
*
* llvm IR can be added to running JIT by calling
* kal_jit_.addModule()
* Note that this makes the module itself unavailable to us
**/
/** owns + manages core "global" llvm data,
* including type- and constant- unique-ing tables.
*
@ -142,7 +160,9 @@ namespace xo {
std::unique_ptr<llvm::LLVMContext> llvm_cx_;
/** builder for intermediate-representation objects **/
std::unique_ptr<llvm::IRBuilder<>> llvm_ir_builder_;
/** a module (aka library) being prepared by llvm.
/** a module (1:1 with library) being prepared by llvm.
* IR-level -- does not contain machine code
*
* - function names are unique within a module.
**/
std::unique_ptr<llvm::Module> llvm_module_;

View file

@ -98,7 +98,7 @@ namespace xo {
llvm::DataLayout dl
#endif
)
: kal_jit_{std::move(kal_jit)},
: kal_jit_{std::move(kal_jit)}
#ifdef NOT_USING
jit_es_(std::move(jit_es)),
jit_data_layout_(std::move(dl)),
@ -114,9 +114,6 @@ namespace xo {
jit_our_dynamic_lib_(this->jit_es_->createBareJITDylib("<xojitlib>")), /*was MainJD*/
#endif
llvm_cx_{std::make_unique<llvm::LLVMContext>()},
llvm_ir_builder_{std::make_unique<llvm::IRBuilder<>>(*llvm_cx_)},
llvm_module_{std::make_unique<llvm::Module>("xojit", *llvm_cx_)}
{
#ifdef NOT_USING
jit_our_dynamic_lib_.addGenerator
@ -129,6 +126,16 @@ namespace xo {
}
#endif
this->recreate_llvm_ir_pipeline();
}
void
Jit::recreate_llvm_ir_pipeline()
{
llvm_cx_ = std::make_unique<llvm::LLVMContext>();
llvm_ir_builder_ = std::make_unique<llvm::IRBuilder<>>(*llvm_cx_);
llvm_module_ = std::make_unique<llvm::Module>("xojit", *llvm_cx_);
llvm_module_->setDataLayout(kal_jit_->getDataLayout());
if (!llvm_cx_.get()) {
@ -162,7 +169,7 @@ namespace xo {
llvm_pass_builder.registerModuleAnalyses(*llvm_mamgr_);
llvm_pass_builder.registerFunctionAnalyses(*llvm_famgr_);
llvm_pass_builder.crossRegisterProxies(*llvm_lamgr_, *llvm_famgr_, *llvm_cgamgr_, *llvm_mamgr_);
}
} /*recreate_llvm_ir_pipeline*/
const std::string &
Jit::target_triple() const {
@ -465,6 +472,21 @@ namespace xo {
return nullptr;
} /*codegen*/
void
Jit::machgen_current_module()
{
static llvm::ExitOnError llvm_exit_on_err;
auto tracker = kal_jit_->getMainJITDylib().createResourceTracker();
auto ts_module = llvm::orc::ThreadSafeModule(std::move(llvm_module_),
std::move(llvm_cx_));
llvm_exit_on_err(kal_jit_->addModule(std::move(ts_module), tracker));
this->recreate_llvm_ir_pipeline();
}
llvm::orc::ExecutorAddr
Jit::lookup_symbol(const std::string & sym)
{