From f7db84972f458bcd7f7caa41d297aa60dae7d304 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Sat, 15 Jun 2024 16:02:56 -0400 Subject: [PATCH] xo-jit: setup analsysis pipeline (most of kaleidoscope4) --- include/xo/jit/Jit.hpp | 24 +++++++++++++++++++++++- src/jit/Jit.cpp | 27 +++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/include/xo/jit/Jit.hpp b/include/xo/jit/Jit.hpp index cf3f3a0f..3a8e4c14 100644 --- a/include/xo/jit/Jit.hpp +++ b/include/xo/jit/Jit.hpp @@ -79,7 +79,10 @@ namespace xo { /** target triple = string describing target host for codegen **/ const std::string & target_triple() const; - /** append function names defined in attached module to *p_v **/ + /** append function names defined in attached module to *p_v + * + * (RC 15jun2024 - this part is working) + **/ std::vector get_function_name_v(); /** write state of execution session (all the associated dynamic libraries) **/ @@ -148,8 +151,27 @@ namespace xo { std::map> global_env_; /** map variable names (formal parameters) to * corresponding llvm interactor + * + * only supports one level atm (i.e. only top-level functions) **/ std::map nested_env_; + + // ----- transforms (also adapted from kaleidescope.cpp) ------ + + /** manages all the passes+analaysis (?) **/ + std::unique_ptr llvm_fpmgr_; + /** loop analysis (?) **/ + std::unique_ptr llvm_lamgr_; + /** function-level analysis (?) **/ + std::unique_ptr llvm_famgr_; + /** cgscc (?) analysis **/ + std::unique_ptr llvm_cgamgr_; + /** module analsyis (?) **/ + std::unique_ptr llvm_mamgr_; + /** pass instrumentation **/ + std::unique_ptr llvm_pic_; + /** standard instrumentation **/ + std::unique_ptr llvm_si_; }; /*Jit*/ inline std::ostream & diff --git a/src/jit/Jit.cpp b/src/jit/Jit.cpp index eef5c197..7d53a313 100644 --- a/src/jit/Jit.cpp +++ b/src/jit/Jit.cpp @@ -67,6 +67,7 @@ namespace xo { } #endif + static llvm::ExitOnError llvm_exit_on_err; std::unique_ptr kal_jit = llvm_exit_on_err(KaleidoscopeJIT::Create()); @@ -139,6 +140,28 @@ namespace xo { if (!llvm_module_.get()) { throw std::runtime_error("Jit::ctor: expected non-empty llvm module"); } + + this->llvm_fpmgr_ = std::make_unique(); + this->llvm_lamgr_ = std::make_unique(); + this->llvm_famgr_ = std::make_unique(); + this->llvm_cgamgr_ = std::make_unique(); + this->llvm_mamgr_ = std::make_unique(); + this->llvm_pic_ = std::make_unique(); + this->llvm_si_ = std::make_unique(*llvm_cx_, + /*DebugLogging*/ true); + + this->llvm_si_->registerCallbacks(*llvm_pic_, llvm_mamgr_.get()); + + // TODO: llvm_fpmgr_->addPass(InstCombinePass()) etc. + // TODO: llvm_fpmgr_->addPass(ReassociatePass()) etc. + // TODO: llvm_fpmgr_->addPass(GVNPasss()) etc. + // TODO: llvm_fpmgr_->addPass(SimplifyCFGPass()) etc. + + /** tracking for analysis passes that share info? **/ + llvm::PassBuilder llvm_pass_builder; + llvm_pass_builder.registerModuleAnalyses(*llvm_mamgr_); + llvm_pass_builder.registerFunctionAnalyses(*llvm_famgr_); + llvm_pass_builder.crossRegisterProxies(*llvm_lamgr_, *llvm_famgr_, *llvm_cgamgr_, *llvm_mamgr_); } const std::string & @@ -389,8 +412,8 @@ namespace xo { /* validate! always validate! */ llvm::verifyFunction(*fn); - /* optimize! */ - // thefpm->run(*fn, *thefam); + /* optimize! does this generate code? */ + llvm_fpmgr_->run(*fn, *llvm_famgr_); return fn; }