Skip to content

Commit 9817f8a

Browse files
Combine global_init and global_stmts functions into global_stmts (#2696)
* combine `global_init` and `global_stmts` * updated reference tests * add integration test * update according to code review suggestion * fix failing tests
1 parent 2ad3c4d commit 9817f8a

8 files changed

Lines changed: 57 additions & 81 deletions

integration_tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,8 @@ RUN(NAME expr_02u LABELS cpython llvm llvm_jit c NOFAST)
508508
RUN(NAME expr_03u LABELS cpython llvm llvm_jit c NOFAST)
509509
RUN(NAME expr_04u LABELS cpython llvm llvm_jit c)
510510

511+
RUN(NAME list_01 LABELS cpython llvm llvm_jit)
512+
511513
RUN(NAME loop_01 LABELS cpython llvm llvm_jit c)
512514
RUN(NAME loop_02 LABELS cpython llvm llvm_jit c wasm wasm_x86 wasm_x64)
513515
RUN(NAME loop_03 LABELS cpython llvm llvm_jit c wasm wasm_x64)

integration_tests/list_01.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from lpython import i32
2+
3+
l: list[i32] = [1, 2, 3, 4]
4+
print("Before Pop:", l)
5+
6+
assert len(l) == 4
7+
assert l[0] == 1
8+
assert l[1] == 2
9+
assert l[2] == 3
10+
assert l[3] == 4
11+
12+
x: i32 = l.pop()
13+
print("After Pop:", l)
14+
15+
assert x == 4
16+
assert len(l) == 3
17+
assert l[0] == 1
18+
assert l[1] == 2
19+
assert l[2] == 3
20+
21+
print("Popped Element: ", x)

src/bin/lpython.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -900,19 +900,12 @@ int compile_python_using_llvm(
900900

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

903-
bool call_init = false;
904903
bool call_stmts = false;
905-
if (m->get_return_type("__module___main_____main__global_init") == "void") {
906-
call_init = true;
907-
}
908904
if (m->get_return_type("__module___main_____main__global_stmts") == "void") {
909905
call_stmts = true;
910906
}
911907

912908
e.add_module(std::move(m));
913-
if (call_init) {
914-
e.voidfn("__module___main_____main__global_init");
915-
}
916909
if (call_stmts) {
917910
e.voidfn("__module___main_____main__global_stmts");
918911
}

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,11 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
290290

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

296296
std::string mod_name = mod->m_name;
297-
std::string g_func_name = mod_name + "global_init";
297+
std::string g_func_name = mod_name + "global_stmts";
298298
ASR::symbol_t *g_func = mod->m_symtab->get_symbol(g_func_name);
299299
if (g_func && !scope->get_symbol(g_func_name)) {
300300
ASR::symbol_t *es = ASR::down_cast<ASR::symbol_t>(
@@ -306,19 +306,6 @@ void get_calls_to_global_init_and_stmts(Allocator &al, const Location &loc, Symb
306306
tmp_vec.push_back(ASRUtils::make_SubroutineCall_t_util(al, loc,
307307
es, g_func, nullptr, 0, nullptr, nullptr, false, false));
308308
}
309-
310-
g_func_name = mod_name + "global_stmts";
311-
g_func = mod->m_symtab->get_symbol(g_func_name);
312-
if (g_func && !scope->get_symbol(g_func_name)) {
313-
ASR::symbol_t *es = ASR::down_cast<ASR::symbol_t>(
314-
ASR::make_ExternalSymbol_t(al, mod->base.base.loc,
315-
scope, s2c(al, g_func_name), g_func,
316-
s2c(al, mod_name), nullptr, 0, s2c(al, g_func_name),
317-
ASR::accessType::Public));
318-
scope->add_symbol(g_func_name, es);
319-
tmp_vec.push_back(ASRUtils::make_SubroutineCall_t_util(al, loc,
320-
es, g_func, nullptr, 0, nullptr, nullptr, false, false));
321-
}
322309
}
323310

324311
template <class Struct>
@@ -4888,6 +4875,12 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
48884875
tmp = nullptr;
48894876
tmp_vec.clear();
48904877
visit_stmt(*x.m_body[i]);
4878+
for (auto t: global_init) {
4879+
if (t) {
4880+
items.push_back(al, t);
4881+
}
4882+
}
4883+
global_init.n = 0;
48914884
if (tmp) {
48924885
items.push_back(al, tmp);
48934886
} else if (!tmp_vec.empty()) {
@@ -4905,30 +4898,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
49054898
mod->m_dependencies = current_module_dependencies.p;
49064899
mod->n_dependencies = current_module_dependencies.n;
49074900

4908-
if (global_init.n > 0) {
4909-
// unit->m_items is used and set to nullptr in the
4910-
// `pass_wrap_global_stmts_into_function` pass
4911-
unit->m_items = global_init.p;
4912-
unit->n_items = global_init.size();
4913-
std::string func_name = module_name + "global_init";
4914-
LCompilers::PassOptions pass_options;
4915-
pass_options.run_fun = func_name;
4916-
pass_wrap_global_stmts(al, *unit, pass_options);
4917-
4918-
ASR::symbol_t *f_sym = unit->m_symtab->get_symbol(func_name);
4919-
if (f_sym) {
4920-
// Add the `global_initilaizer` function into the
4921-
// module and later call this function to initialize the
4922-
// global variables like list, ...
4923-
ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(f_sym);
4924-
f->m_symtab->parent = mod->m_symtab;
4925-
mod->m_symtab->add_symbol(func_name, (ASR::symbol_t *) f);
4926-
// Erase the function in TranslationUnit
4927-
unit->m_symtab->erase_symbol(func_name);
4928-
}
4929-
global_init.p = nullptr;
4930-
global_init.n = 0;
4931-
}
4901+
LCOMPILERS_ASSERT(global_init.empty())
49324902

49334903
if (items.n > 0) {
49344904
unit->m_items = items.p;
@@ -5012,7 +4982,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
50124982
ASR::symbol_t *mod_sym = current_scope->resolve_symbol(mod_name);
50134983
if (mod_sym) {
50144984
ASR::Module_t *mod = ASR::down_cast<ASR::Module_t>(mod_sym);
5015-
get_calls_to_global_init_and_stmts(al, x.base.base.loc, current_scope, mod, tmp_vec);
4985+
get_calls_to_global_stmts(al, x.base.base.loc, current_scope, mod, tmp_vec);
50164986
}
50174987
}
50184988
}
@@ -5031,7 +5001,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
50315001
ASR::symbol_t *mod_sym = current_scope->resolve_symbol(mod_name);
50325002
if (mod_sym) {
50335003
ASR::Module_t *mod = ASR::down_cast<ASR::Module_t>(mod_sym);
5034-
get_calls_to_global_init_and_stmts(al, x.base.base.loc, current_scope, mod, tmp_vec);
5004+
get_calls_to_global_stmts(al, x.base.base.loc, current_scope, mod, tmp_vec);
50355005
}
50365006
tmp = nullptr;
50375007
}
@@ -8444,7 +8414,7 @@ Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al, LocationManager
84448414
ASR::Module_t *mod = ASR::down_cast<ASR::Module_t>(mod_sym);
84458415
LCOMPILERS_ASSERT(mod);
84468416
std::vector<ASR::asr_t*> tmp_vec;
8447-
get_calls_to_global_init_and_stmts(al, tu->base.base.loc, program_scope, mod, tmp_vec);
8417+
get_calls_to_global_stmts(al, tu->base.base.loc, program_scope, mod, tmp_vec);
84488418

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

tests/reference/llvm-structs_11-09fea6a.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "llvm-structs_11-09fea6a.stdout",
9-
"stdout_hash": "b1de8efeefa8bb2d76ce4fcb13e049cd550cb2242189bec5d0003b80",
9+
"stdout_hash": "5257bcde84f3ca956bdf8ce0d0dcffab462418097139e577b06748a3",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/llvm-structs_11-09fea6a.stdout

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ source_filename = "LFortran"
1212
@4 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
1313
@5 = private unnamed_addr constant [5 x i8] c"%d%s\00", align 1
1414

15-
define void @__module___main_____main__global_init() {
16-
.entry:
17-
br label %return
18-
19-
return: ; preds = %.entry
20-
ret void
21-
}
22-
2315
define void @__module___main_____main__global_stmts() {
2416
.entry:
2517
%0 = load i32, i32* getelementptr inbounds (%Bar, %Bar* @bar, i32 0, i32 0, i32 0), align 4
@@ -37,7 +29,6 @@ declare void @_lfortran_printf(i8*, ...)
3729
define i32 @main(i32 %0, i8** %1) {
3830
.entry:
3931
call void @_lpython_call_initial_functions(i32 %0, i8** %1)
40-
call void @__module___main_____main__global_init()
4132
call void @__module___main_____main__global_stmts()
4233
ret i32 0
4334
}

tests/reference/python-test_aggregate_constants-26c89d6.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "python-test_aggregate_constants-26c89d6.stdout",
9-
"stdout_hash": "3d5fa791404534643f6f2a096b2bc1561cf6c03a78d060b79df4f17b",
9+
"stdout_hash": "615052b21f411decc56105bba5b9b1286e369c3da614dddfcaa6e3a2",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/python-test_aggregate_constants-26c89d6.stdout

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
1-
def __main__global_init():
2-
my_first_list = [1, 2, 3, 4]
3-
my_second_list = ["a", "b", "c", "d"]
4-
my_third_list = [[1, 2], [3, 4], [5, 6]]
5-
my_fourth_list = [[1.000000, 2.200000], [3.600000, 4.900000], [5.100000, 6.300000]]
6-
my_fifth_list = [{"a", "b"}, {"c", "d"}]
7-
my_sixth_list = [(1, "a"), (2, "b")]
8-
my_first_tuple = (1, "hello", 2.400000)
9-
my_second_tuple = ((1, "hello"), "world")
10-
my_third_tuple = (["hello", "world"], "world")
11-
my_fourth_tuple = ({"hello", "world"}, "world")
12-
my_first_set = {1, 2, 3, 2, 4}
13-
my_second_set = {1.100000, 2.500000, 6.800000}
14-
my_third_set = {"a", "b", "a", "c"}
15-
my_fourth_set = {(1, "a"), (2, "b"), (3, "c")}
16-
my_first_dict = {"a": 1, "b": 2, "c": 3}
17-
my_second_dict = {1: 1.330000, 2: 2.330000, 3: 3.330000}
18-
my_third_dict = {"a": "A", "b": "B", "c": "C"}
19-
my_fourth_dict = {1: (1.200000, 4.500000), 2: (3.600000, 9.200000)}
20-
my_fifth_dict = {"list1": [1, 2, 3], "list2": [4, 5, 6]}
21-
my_sixth_dict = {"set1": {1, 2, 1}, "set2": {4, 1, 2}}
221
def __main__global_stmts():
2+
my_first_list = [1, 2, 3, 4]
233
print(my_first_list)
4+
my_second_list = ["a", "b", "c", "d"]
245
print(my_second_list)
6+
my_third_list = [[1, 2], [3, 4], [5, 6]]
257
print(my_third_list)
8+
my_fourth_list = [[1.000000, 2.200000], [3.600000, 4.900000], [5.100000, 6.300000]]
269
print(my_fourth_list)
10+
my_fifth_list = [{"a", "b"}, {"c", "d"}]
2711
print(my_fifth_list)
12+
my_sixth_list = [(1, "a"), (2, "b")]
2813
print(my_sixth_list)
14+
my_first_tuple = (1, "hello", 2.400000)
2915
print(my_first_tuple)
16+
my_second_tuple = ((1, "hello"), "world")
3017
print(my_second_tuple)
18+
my_third_tuple = (["hello", "world"], "world")
3119
print(my_third_tuple)
20+
my_fourth_tuple = ({"hello", "world"}, "world")
3221
print(my_fourth_tuple)
22+
my_first_set = {1, 2, 3, 2, 4}
3323
print(my_first_set)
24+
my_second_set = {1.100000, 2.500000, 6.800000}
3425
print(my_second_set)
26+
my_third_set = {"a", "b", "a", "c"}
3527
print(my_third_set)
28+
my_fourth_set = {(1, "a"), (2, "b"), (3, "c")}
3629
print(my_fourth_set)
30+
my_first_dict = {"a": 1, "b": 2, "c": 3}
3731
print(my_first_dict)
32+
my_second_dict = {1: 1.330000, 2: 2.330000, 3: 3.330000}
3833
print(my_second_dict)
34+
my_third_dict = {"a": "A", "b": "B", "c": "C"}
3935
print(my_third_dict)
36+
my_fourth_dict = {1: (1.200000, 4.500000), 2: (3.600000, 9.200000)}
4037
print(my_fourth_dict)
38+
my_fifth_dict = {"list1": [1, 2, 3], "list2": [4, 5, 6]}
4139
print(my_fifth_dict)
40+
my_sixth_dict = {"set1": {1, 2, 1}, "set2": {4, 1, 2}}
4241
print(my_sixth_dict)
4342
fn()
4443
def fn():

0 commit comments

Comments
 (0)