mirror of
https://github.com/exaloop/codon.git
synced 2025-06-03 15:03:52 +08:00
Update new global detection and processing in JIT
This commit is contained in:
parent
c3c48b9c6d
commit
6f4e24fb00
@ -58,23 +58,14 @@ llvm::Error JIT::init() {
|
|||||||
return llvm::Error::success();
|
return llvm::Error::success();
|
||||||
}
|
}
|
||||||
|
|
||||||
llvm::Expected<std::string> JIT::run(const ir::Func *input,
|
llvm::Expected<std::string> JIT::run(const ir::Func *input) {
|
||||||
const std::vector<ir::Var *> &newGlobals) {
|
|
||||||
auto *module = compiler->getModule();
|
auto *module = compiler->getModule();
|
||||||
auto *pm = compiler->getPassManager();
|
auto *pm = compiler->getPassManager();
|
||||||
auto *llvisitor = compiler->getLLVMVisitor();
|
auto *llvisitor = compiler->getLLVMVisitor();
|
||||||
pm->run(module);
|
pm->run(module);
|
||||||
|
|
||||||
const std::string name = ir::LLVMVisitor::getNameForFunction(input);
|
const std::string name = ir::LLVMVisitor::getNameForFunction(input);
|
||||||
llvisitor->registerGlobal(input);
|
llvisitor->processNewGlobals(module);
|
||||||
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 pair = llvisitor->takeModule();
|
||||||
|
|
||||||
if (auto err = engine->addModule({std::move(pair.first), std::move(pair.second)}))
|
if (auto err = engine->addModule({std::move(pair.first), std::move(pair.second)}))
|
||||||
@ -138,11 +129,7 @@ llvm::Expected<std::string> JIT::exec(const std::string &code) {
|
|||||||
|
|
||||||
auto *cache = compiler->getCache();
|
auto *cache = compiler->getCache();
|
||||||
auto typechecked = ast::TypecheckVisitor::apply(cache, simplified);
|
auto typechecked = ast::TypecheckVisitor::apply(cache, simplified);
|
||||||
std::vector<std::string> globalNames;
|
|
||||||
for (auto &g : cache->globals) {
|
|
||||||
if (!g.second)
|
|
||||||
globalNames.push_back(g.first);
|
|
||||||
}
|
|
||||||
// add newly realized functions
|
// add newly realized functions
|
||||||
std::vector<ast::StmtPtr> v;
|
std::vector<ast::StmtPtr> v;
|
||||||
std::vector<ir::Func **> frs;
|
std::vector<ir::Func **> frs;
|
||||||
@ -155,16 +142,7 @@ llvm::Expected<std::string> JIT::exec(const std::string &code) {
|
|||||||
ast::TranslateVisitor::apply(cache, std::make_shared<ast::SuiteStmt>(v, false));
|
ast::TranslateVisitor::apply(cache, std::make_shared<ast::SuiteStmt>(v, false));
|
||||||
cache->jitCell++;
|
cache->jitCell++;
|
||||||
|
|
||||||
std::vector<ir::Var *> globalVars;
|
return run(func);
|
||||||
for (auto &g : globalNames) {
|
|
||||||
seqassert(cache->globals[g], "JIT global {} not set", g);
|
|
||||||
globalVars.push_back(cache->globals[g]);
|
|
||||||
}
|
|
||||||
for (auto &i : frs) {
|
|
||||||
seqassert(*i, "JIT fn not set");
|
|
||||||
globalVars.push_back(*i);
|
|
||||||
}
|
|
||||||
return run(func, globalVars);
|
|
||||||
} catch (const exc::ParserException &e) {
|
} catch (const exc::ParserException &e) {
|
||||||
*cache = bCache;
|
*cache = bCache;
|
||||||
*(cache->imports[MAIN_IMPORT].ctx) = bSimplify;
|
*(cache->imports[MAIN_IMPORT].ctx) = bSimplify;
|
||||||
|
@ -28,8 +28,7 @@ public:
|
|||||||
Engine *getEngine() const { return engine.get(); }
|
Engine *getEngine() const { return engine.get(); }
|
||||||
|
|
||||||
llvm::Error init();
|
llvm::Error init();
|
||||||
llvm::Expected<std::string> run(const ir::Func *input,
|
llvm::Expected<std::string> run(const ir::Func *input);
|
||||||
const std::vector<ir::Var *> &newGlobals = {});
|
|
||||||
llvm::Expected<std::string> exec(const std::string &code);
|
llvm::Expected<std::string> exec(const std::string &code);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -113,6 +113,25 @@ void LLVMVisitor::registerGlobal(const Var *var) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LLVMVisitor::processNewGlobals(Module *module) {
|
||||||
|
std::vector<Func *> newFuncs;
|
||||||
|
for (auto *var : *module) {
|
||||||
|
if (!var->isGlobal())
|
||||||
|
continue;
|
||||||
|
auto id = var->getId();
|
||||||
|
auto *func = cast<Func>(var);
|
||||||
|
bool isNewFunc = (func && funcs.find(id) == funcs.end());
|
||||||
|
if (isNewFunc || (!func && vars.find(id) == vars.end()))
|
||||||
|
registerGlobal(var);
|
||||||
|
if (isNewFunc)
|
||||||
|
newFuncs.push_back(func);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto *func : newFuncs) {
|
||||||
|
func->accept(*this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
llvm::Value *LLVMVisitor::getVar(const Var *var) {
|
llvm::Value *LLVMVisitor::getVar(const Var *var) {
|
||||||
auto it = vars.find(var->getId());
|
auto it = vars.find(var->getId());
|
||||||
if (db.jit && var->isGlobal()) {
|
if (db.jit && var->isGlobal()) {
|
||||||
|
@ -275,6 +275,11 @@ public:
|
|||||||
/// @param var the global variable (or function) to register
|
/// @param var the global variable (or function) to register
|
||||||
void registerGlobal(const Var *var);
|
void registerGlobal(const Var *var);
|
||||||
|
|
||||||
|
/// Processes new globals that were not previously
|
||||||
|
/// compiled. Used in JIT mode.
|
||||||
|
/// @param module the IR module
|
||||||
|
void processNewGlobals(Module *module);
|
||||||
|
|
||||||
/// Returns the default LLVM linkage type for the module.
|
/// Returns the default LLVM linkage type for the module.
|
||||||
/// @return LLVM linkage type
|
/// @return LLVM linkage type
|
||||||
llvm::GlobalValue::LinkageTypes getDefaultLinkage();
|
llvm::GlobalValue::LinkageTypes getDefaultLinkage();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user