From d80d88266367625a7de6e23e09c47540748c0f3b Mon Sep 17 00:00:00 2001 From: "A. R. Shajii" Date: Thu, 28 Oct 2021 13:03:06 -0400 Subject: [PATCH] Refactor --- CMakeLists.txt | 2 ++ codon/jit/engine.cpp | 53 ----------------------------------- codon/jit/engine.h | 22 ++------------- codon/jit/jit.cpp | 61 +++++++++++++++++++++++++++++++++++++++++ codon/jit/jit.h | 30 ++++++++++++++++++++ codon/parser/parser.cpp | 2 +- 6 files changed, 96 insertions(+), 74 deletions(-) create mode 100644 codon/jit/jit.cpp create mode 100644 codon/jit/jit.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d292a7fc..cb4645c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,6 +76,7 @@ set(CODON_HPPFILES codon/dsl/dsl.h codon/dsl/plugins.h codon/jit/engine.h + codon/jit/jit.h codon/parser/ast.h codon/parser/ast/expr.h codon/parser/ast/stmt.h @@ -198,6 +199,7 @@ set(CODON_HPPFILES set(CODON_CPPFILES codon/dsl/plugins.cpp codon/jit/engine.cpp + codon/jit/jit.cpp codon/parser/ast/expr.cpp codon/parser/ast/stmt.cpp codon/parser/ast/types.cpp diff --git a/codon/jit/engine.cpp b/codon/jit/engine.cpp index 6b63f81b..4fa350aa 100644 --- a/codon/jit/engine.cpp +++ b/codon/jit/engine.cpp @@ -1,6 +1,5 @@ #include "engine.h" -#include "codon/runtime/lib.h" #include "codon/sir/llvm/memory_manager.h" #include "codon/sir/llvm/optimize.h" @@ -86,57 +85,5 @@ llvm::Expected Engine::lookup(llvm::StringRef name) { return sess->lookup({&mainJD}, mangle(name.str())); } -typedef int MainFunc(int, char **); -typedef void InputFunc(); - -JIT::JIT(ir::Module *module) - : module(module), pm(std::make_unique(/*debug=*/true)), - plm(std::make_unique()), - llvisitor(std::make_unique(/*debug=*/true, /*jit=*/true)) { - if (auto e = Engine::create()) { - engine = std::move(e.get()); - } else { - engine = {}; - seqassert(false, "JIT engine creation error"); - } - llvisitor->setPluginManager(plm.get()); -} - -void JIT::init() { - module->accept(*llvisitor); - auto pair = llvisitor->takeModule(); - // auto rt = engine->getMainJITDylib().createResourceTracker(); - llvm::cantFail(engine->addModule({std::move(pair.second), std::move(pair.first)})); - auto func = llvm::cantFail(engine->lookup("main")); - auto *main = (MainFunc *)func.getAddress(); - (*main)(0, nullptr); - // llvm::cantFail(rt->remove()); -} - -void JIT::run(const ir::Func *input, const std::vector &newGlobals) { - const std::string name = ir::LLVMVisitor::getNameForFunction(input); - llvisitor->registerGlobal(input); - for (auto *var : newGlobals) { - llvisitor->registerGlobal(var); - } - for (auto *var : newGlobals) { - if (auto *func = ir::cast(var)) - func->accept(*llvisitor); - } - input->accept(*llvisitor); - auto pair = llvisitor->takeModule(); - // auto rt = engine->getMainJITDylib().createResourceTracker(); - llvm::StripDebugInfo(*pair.second); // TODO: needed? - llvm::cantFail(engine->addModule({std::move(pair.second), std::move(pair.first)})); - auto func = llvm::cantFail(engine->lookup(name)); - auto *repl = (InputFunc *)func.getAddress(); - try { - (*repl)(); - } catch (const seq_jit_error &) { - // nothing to do - } - // llvm::cantFail(rt->remove()); -} - } // namespace jit } // namespace codon diff --git a/codon/jit/engine.h b/codon/jit/engine.h index 51810bed..fef9e641 100644 --- a/codon/jit/engine.h +++ b/codon/jit/engine.h @@ -3,6 +3,8 @@ #include #include +#include "codon/sir/llvm/llvm.h" + #include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h" #include "llvm/ExecutionEngine/Orc/CompileUtils.h" #include "llvm/ExecutionEngine/Orc/Core.h" @@ -14,11 +16,6 @@ #include "llvm/ExecutionEngine/Orc/TPCIndirectionUtils.h" #include "llvm/ExecutionEngine/Orc/TargetProcessControl.h" -#include "codon/sir/llvm/llvisitor.h" -#include "codon/sir/llvm/llvm.h" -#include "codon/sir/transform/manager.h" -#include "codon/sir/var.h" - namespace codon { namespace jit { @@ -64,20 +61,5 @@ public: llvm::Expected lookup(llvm::StringRef name); }; -class JIT { -private: - ir::Module *module; - std::unique_ptr pm; - std::unique_ptr plm; - std::unique_ptr llvisitor; - std::unique_ptr engine; - -public: - JIT(ir::Module *module); - ir::Module *getModule() const { return module; } - void init(); - void run(const ir::Func *input, const std::vector &newGlobals = {}); -}; - } // namespace jit } // namespace codon diff --git a/codon/jit/jit.cpp b/codon/jit/jit.cpp new file mode 100644 index 00000000..49638a11 --- /dev/null +++ b/codon/jit/jit.cpp @@ -0,0 +1,61 @@ +#include "jit.h" + +#include "codon/runtime/lib.h" + +namespace codon { +namespace jit { + +typedef int MainFunc(int, char **); +typedef void InputFunc(); + +JIT::JIT(ir::Module *module) + : module(module), pm(std::make_unique(/*debug=*/true)), + plm(std::make_unique()), + llvisitor(std::make_unique(/*debug=*/true, /*jit=*/true)) { + if (auto e = Engine::create()) { + engine = std::move(e.get()); + } else { + engine = {}; + seqassert(false, "JIT engine creation error"); + } + llvisitor->setPluginManager(plm.get()); +} + +void JIT::init() { + module->accept(*llvisitor); + auto pair = llvisitor->takeModule(); + // auto rt = engine->getMainJITDylib().createResourceTracker(); + llvm::cantFail(engine->addModule({std::move(pair.second), std::move(pair.first)})); + auto func = llvm::cantFail(engine->lookup("main")); + auto *main = (MainFunc *)func.getAddress(); + (*main)(0, nullptr); + // llvm::cantFail(rt->remove()); +} + +void JIT::run(const ir::Func *input, const std::vector &newGlobals) { + const std::string name = ir::LLVMVisitor::getNameForFunction(input); + llvisitor->registerGlobal(input); + for (auto *var : newGlobals) { + llvisitor->registerGlobal(var); + } + for (auto *var : newGlobals) { + if (auto *func = ir::cast(var)) + func->accept(*llvisitor); + } + input->accept(*llvisitor); + auto pair = llvisitor->takeModule(); + // auto rt = engine->getMainJITDylib().createResourceTracker(); + llvm::StripDebugInfo(*pair.second); // TODO: needed? + llvm::cantFail(engine->addModule({std::move(pair.second), std::move(pair.first)})); + auto func = llvm::cantFail(engine->lookup(name)); + auto *repl = (InputFunc *)func.getAddress(); + try { + (*repl)(); + } catch (const seq_jit_error &) { + // nothing to do + } + // llvm::cantFail(rt->remove()); +} + +} // namespace jit +} // namespace codon diff --git a/codon/jit/jit.h b/codon/jit/jit.h new file mode 100644 index 00000000..6f554a0e --- /dev/null +++ b/codon/jit/jit.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include + +#include "codon/jit/engine.h" +#include "codon/sir/llvm/llvisitor.h" +#include "codon/sir/transform/manager.h" +#include "codon/sir/var.h" + +namespace codon { +namespace jit { + +class JIT { +private: + ir::Module *module; + std::unique_ptr pm; + std::unique_ptr plm; + std::unique_ptr llvisitor; + std::unique_ptr engine; + +public: + JIT(ir::Module *module); + ir::Module *getModule() const { return module; } + void init(); + void run(const ir::Func *input, const std::vector &newGlobals = {}); +}; + +} // namespace jit +} // namespace codon diff --git a/codon/parser/parser.cpp b/codon/parser/parser.cpp index 480aaf3c..d1f6c0d1 100644 --- a/codon/parser/parser.cpp +++ b/codon/parser/parser.cpp @@ -6,7 +6,7 @@ #include #include -#include "codon/jit/engine.h" +#include "codon/jit/jit.h" #include "codon/parser/cache.h" #include "codon/parser/peg/peg.h" #include "codon/parser/visitors/doc/doc.h"