Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine global_init and global_stmts functions into global_stmts #2696

Merged
merged 5 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,8 @@ RUN(NAME expr_02u LABELS cpython llvm llvm_jit c NOFAST)
RUN(NAME expr_03u LABELS cpython llvm llvm_jit c NOFAST)
RUN(NAME expr_04u LABELS cpython llvm llvm_jit c)

RUN(NAME list_01 LABELS cpython llvm llvm_jit)

RUN(NAME loop_01 LABELS cpython llvm llvm_jit c)
RUN(NAME loop_02 LABELS cpython llvm llvm_jit c wasm wasm_x86 wasm_x64)
RUN(NAME loop_03 LABELS cpython llvm llvm_jit c wasm wasm_x64)
Expand Down
21 changes: 21 additions & 0 deletions integration_tests/list_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from lpython import i32

l: list[i32] = [1, 2, 3, 4]
print("Before Pop:", l)

assert len(l) == 4
assert l[0] == 1
assert l[1] == 2
assert l[2] == 3
assert l[3] == 4

x: i32 = l.pop()
print("After Pop:", l)

assert x == 4
assert len(l) == 3
assert l[0] == 1
assert l[1] == 2
assert l[2] == 3

print("Popped Element: ", x)
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
54 changes: 12 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,12 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
tmp = nullptr;
tmp_vec.clear();
visit_stmt(*x.m_body[i]);
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 +4898,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 +4982,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 +5001,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 +8414,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
2 changes: 1 addition & 1 deletion tests/reference/llvm-structs_11-09fea6a.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "llvm-structs_11-09fea6a.stdout",
"stdout_hash": "b1de8efeefa8bb2d76ce4fcb13e049cd550cb2242189bec5d0003b80",
"stdout_hash": "5257bcde84f3ca956bdf8ce0d0dcffab462418097139e577b06748a3",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
9 changes: 0 additions & 9 deletions tests/reference/llvm-structs_11-09fea6a.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ source_filename = "LFortran"
@4 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@5 = private unnamed_addr constant [5 x i8] c"%d%s\00", align 1

define void @__module___main_____main__global_init() {
.entry:
br label %return

return: ; preds = %.entry
ret void
}

define void @__module___main_____main__global_stmts() {
.entry:
%0 = load i32, i32* getelementptr inbounds (%Bar, %Bar* @bar, i32 0, i32 0, i32 0), align 4
Expand All @@ -37,7 +29,6 @@ declare void @_lfortran_printf(i8*, ...)
define i32 @main(i32 %0, i8** %1) {
.entry:
call void @_lpython_call_initial_functions(i32 %0, i8** %1)
call void @__module___main_____main__global_init()
call void @__module___main_____main__global_stmts()
ret i32 0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "python-test_aggregate_constants-26c89d6.stdout",
"stdout_hash": "3d5fa791404534643f6f2a096b2bc1561cf6c03a78d060b79df4f17b",
"stdout_hash": "615052b21f411decc56105bba5b9b1286e369c3da614dddfcaa6e3a2",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
41 changes: 20 additions & 21 deletions tests/reference/python-test_aggregate_constants-26c89d6.stdout
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
def __main__global_init():
my_first_list = [1, 2, 3, 4]
my_second_list = ["a", "b", "c", "d"]
my_third_list = [[1, 2], [3, 4], [5, 6]]
my_fourth_list = [[1.000000, 2.200000], [3.600000, 4.900000], [5.100000, 6.300000]]
my_fifth_list = [{"a", "b"}, {"c", "d"}]
my_sixth_list = [(1, "a"), (2, "b")]
my_first_tuple = (1, "hello", 2.400000)
my_second_tuple = ((1, "hello"), "world")
my_third_tuple = (["hello", "world"], "world")
my_fourth_tuple = ({"hello", "world"}, "world")
my_first_set = {1, 2, 3, 2, 4}
my_second_set = {1.100000, 2.500000, 6.800000}
my_third_set = {"a", "b", "a", "c"}
my_fourth_set = {(1, "a"), (2, "b"), (3, "c")}
my_first_dict = {"a": 1, "b": 2, "c": 3}
my_second_dict = {1: 1.330000, 2: 2.330000, 3: 3.330000}
my_third_dict = {"a": "A", "b": "B", "c": "C"}
my_fourth_dict = {1: (1.200000, 4.500000), 2: (3.600000, 9.200000)}
my_fifth_dict = {"list1": [1, 2, 3], "list2": [4, 5, 6]}
my_sixth_dict = {"set1": {1, 2, 1}, "set2": {4, 1, 2}}
def __main__global_stmts():
my_first_list = [1, 2, 3, 4]
print(my_first_list)
my_second_list = ["a", "b", "c", "d"]
print(my_second_list)
my_third_list = [[1, 2], [3, 4], [5, 6]]
print(my_third_list)
my_fourth_list = [[1.000000, 2.200000], [3.600000, 4.900000], [5.100000, 6.300000]]
print(my_fourth_list)
my_fifth_list = [{"a", "b"}, {"c", "d"}]
print(my_fifth_list)
my_sixth_list = [(1, "a"), (2, "b")]
print(my_sixth_list)
my_first_tuple = (1, "hello", 2.400000)
print(my_first_tuple)
my_second_tuple = ((1, "hello"), "world")
print(my_second_tuple)
my_third_tuple = (["hello", "world"], "world")
print(my_third_tuple)
my_fourth_tuple = ({"hello", "world"}, "world")
print(my_fourth_tuple)
my_first_set = {1, 2, 3, 2, 4}
print(my_first_set)
my_second_set = {1.100000, 2.500000, 6.800000}
print(my_second_set)
my_third_set = {"a", "b", "a", "c"}
print(my_third_set)
my_fourth_set = {(1, "a"), (2, "b"), (3, "c")}
print(my_fourth_set)
my_first_dict = {"a": 1, "b": 2, "c": 3}
print(my_first_dict)
my_second_dict = {1: 1.330000, 2: 2.330000, 3: 3.330000}
print(my_second_dict)
my_third_dict = {"a": "A", "b": "B", "c": "C"}
print(my_third_dict)
my_fourth_dict = {1: (1.200000, 4.500000), 2: (3.600000, 9.200000)}
print(my_fourth_dict)
my_fifth_dict = {"list1": [1, 2, 3], "list2": [4, 5, 6]}
print(my_fifth_dict)
my_sixth_dict = {"set1": {1, 2, 1}, "set2": {4, 1, 2}}
print(my_sixth_dict)
fn()
def fn():
Expand Down
Loading