1
0
mirror of https://github.com/exaloop/codon.git synced 2025-06-03 15:03:52 +08:00
This commit is contained in:
A. R. Shajii 2021-10-28 13:03:06 -04:00
parent 94965760ba
commit d80d882663
6 changed files with 96 additions and 74 deletions

View File

@ -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

View File

@ -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<llvm::JITEvaluatedSymbol> 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<ir::transform::PassManager>(/*debug=*/true)),
plm(std::make_unique<PluginManager>()),
llvisitor(std::make_unique<ir::LLVMVisitor>(/*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<ir::Var *> &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<ir::Func>(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

View File

@ -3,6 +3,8 @@
#include <memory>
#include <vector>
#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<llvm::JITEvaluatedSymbol> lookup(llvm::StringRef name);
};
class JIT {
private:
ir::Module *module;
std::unique_ptr<ir::transform::PassManager> pm;
std::unique_ptr<PluginManager> plm;
std::unique_ptr<ir::LLVMVisitor> llvisitor;
std::unique_ptr<Engine> engine;
public:
JIT(ir::Module *module);
ir::Module *getModule() const { return module; }
void init();
void run(const ir::Func *input, const std::vector<ir::Var *> &newGlobals = {});
};
} // namespace jit
} // namespace codon

61
codon/jit/jit.cpp Normal file
View File

@ -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<ir::transform::PassManager>(/*debug=*/true)),
plm(std::make_unique<PluginManager>()),
llvisitor(std::make_unique<ir::LLVMVisitor>(/*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<ir::Var *> &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<ir::Func>(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

30
codon/jit/jit.h Normal file
View File

@ -0,0 +1,30 @@
#pragma once
#include <memory>
#include <vector>
#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<ir::transform::PassManager> pm;
std::unique_ptr<PluginManager> plm;
std::unique_ptr<ir::LLVMVisitor> llvisitor;
std::unique_ptr<Engine> engine;
public:
JIT(ir::Module *module);
ir::Module *getModule() const { return module; }
void init();
void run(const ir::Func *input, const std::vector<ir::Var *> &newGlobals = {});
};
} // namespace jit
} // namespace codon

View File

@ -6,7 +6,7 @@
#include <string>
#include <vector>
#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"