Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
combine global_init and global_stmts
  • Loading branch information
Vipul-Cariappa committed May 11, 2024
commit 0b4bfe2741b79ba43d8686c05fb065a2b947846d
7 changes: 0 additions & 7 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -900,19 +900,12 @@ int compile_python_using_llvm(

auto llvm_start = std::chrono::high_resolution_clock::now();

bool call_init = false;
bool call_stmts = false;
if (m->get_return_type("__module___main_____main__global_init") == "void") {
call_init = true;
}
if (m->get_return_type("__module___main_____main__global_stmts") == "void") {
call_stmts = true;
}

e.add_module(std::move(m));
if (call_init) {
e.voidfn("__module___main_____main__global_init");
}
if (call_stmts) {
e.voidfn("__module___main_____main__global_stmts");
}
Expand Down
56 changes: 14 additions & 42 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,11 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,

// Here, we call the global_initializer & global_statements to
// initialize and execute the global symbols
void get_calls_to_global_init_and_stmts(Allocator &al, const Location &loc, SymbolTable* scope,
void get_calls_to_global_stmts(Allocator &al, const Location &loc, SymbolTable* scope,
ASR::Module_t* mod, std::vector<ASR::asr_t *> &tmp_vec) {

std::string mod_name = mod->m_name;
std::string g_func_name = mod_name + "global_init";
std::string g_func_name = mod_name + "global_stmts";
ASR::symbol_t *g_func = mod->m_symtab->get_symbol(g_func_name);
if (g_func && !scope->get_symbol(g_func_name)) {
ASR::symbol_t *es = ASR::down_cast<ASR::symbol_t>(
Expand All @@ -306,19 +306,6 @@ void get_calls_to_global_init_and_stmts(Allocator &al, const Location &loc, Symb
tmp_vec.push_back(ASRUtils::make_SubroutineCall_t_util(al, loc,
es, g_func, nullptr, 0, nullptr, nullptr, false, false));
}

g_func_name = mod_name + "global_stmts";
g_func = mod->m_symtab->get_symbol(g_func_name);
if (g_func && !scope->get_symbol(g_func_name)) {
ASR::symbol_t *es = ASR::down_cast<ASR::symbol_t>(
ASR::make_ExternalSymbol_t(al, mod->base.base.loc,
scope, s2c(al, g_func_name), g_func,
s2c(al, mod_name), nullptr, 0, s2c(al, g_func_name),
ASR::accessType::Public));
scope->add_symbol(g_func_name, es);
tmp_vec.push_back(ASRUtils::make_SubroutineCall_t_util(al, loc,
es, g_func, nullptr, 0, nullptr, nullptr, false, false));
}
}

template <class Struct>
Expand Down Expand Up @@ -4888,6 +4875,14 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
tmp = nullptr;
tmp_vec.clear();
visit_stmt(*x.m_body[i]);
if (!global_init.empty()) {
for (auto t: global_init) {
if (t) {
items.push_back(al, t);
global_init.erase(t);
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be simplified to

Suggested change
if (!global_init.empty()) {
for (auto t: global_init) {
if (t) {
items.push_back(al, t);
global_init.erase(t);
}
}
}
for (auto t: global_init) {
if (t) {
items.push_back(al, t);
}
}
global_init.n = 0;

if (tmp) {
items.push_back(al, tmp);
} else if (!tmp_vec.empty()) {
Expand All @@ -4905,30 +4900,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
mod->m_dependencies = current_module_dependencies.p;
mod->n_dependencies = current_module_dependencies.n;

if (global_init.n > 0) {
// unit->m_items is used and set to nullptr in the
// `pass_wrap_global_stmts_into_function` pass
unit->m_items = global_init.p;
unit->n_items = global_init.size();
std::string func_name = module_name + "global_init";
LCompilers::PassOptions pass_options;
pass_options.run_fun = func_name;
pass_wrap_global_stmts(al, *unit, pass_options);

ASR::symbol_t *f_sym = unit->m_symtab->get_symbol(func_name);
if (f_sym) {
// Add the `global_initilaizer` function into the
// module and later call this function to initialize the
// global variables like list, ...
ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(f_sym);
f->m_symtab->parent = mod->m_symtab;
mod->m_symtab->add_symbol(func_name, (ASR::symbol_t *) f);
// Erase the function in TranslationUnit
unit->m_symtab->erase_symbol(func_name);
}
global_init.p = nullptr;
global_init.n = 0;
}
LCOMPILERS_ASSERT(global_init.empty())

if (items.n > 0) {
unit->m_items = items.p;
Expand Down Expand Up @@ -5012,7 +4984,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
ASR::symbol_t *mod_sym = current_scope->resolve_symbol(mod_name);
if (mod_sym) {
ASR::Module_t *mod = ASR::down_cast<ASR::Module_t>(mod_sym);
get_calls_to_global_init_and_stmts(al, x.base.base.loc, current_scope, mod, tmp_vec);
get_calls_to_global_stmts(al, x.base.base.loc, current_scope, mod, tmp_vec);
}
}
}
Expand All @@ -5031,7 +5003,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
ASR::symbol_t *mod_sym = current_scope->resolve_symbol(mod_name);
if (mod_sym) {
ASR::Module_t *mod = ASR::down_cast<ASR::Module_t>(mod_sym);
get_calls_to_global_init_and_stmts(al, x.base.base.loc, current_scope, mod, tmp_vec);
get_calls_to_global_stmts(al, x.base.base.loc, current_scope, mod, tmp_vec);
}
tmp = nullptr;
}
Expand Down Expand Up @@ -8444,7 +8416,7 @@ Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al, LocationManager
ASR::Module_t *mod = ASR::down_cast<ASR::Module_t>(mod_sym);
LCOMPILERS_ASSERT(mod);
std::vector<ASR::asr_t*> tmp_vec;
get_calls_to_global_init_and_stmts(al, tu->base.base.loc, program_scope, mod, tmp_vec);
get_calls_to_global_stmts(al, tu->base.base.loc, program_scope, mod, tmp_vec);

for (auto i:tmp_vec) {
prog_body.push_back(al, ASRUtils::STMT(i));
Expand Down