From 0b4bfe2741b79ba43d8686c05fb065a2b947846d Mon Sep 17 00:00:00 2001 From: Vipul Cariappa Date: Sat, 11 May 2024 10:14:46 +0530 Subject: [PATCH 1/5] combine `global_init` and `global_stmts` --- src/bin/lpython.cpp | 7 --- src/lpython/semantics/python_ast_to_asr.cpp | 56 ++++++--------------- 2 files changed, 14 insertions(+), 49 deletions(-) diff --git a/src/bin/lpython.cpp b/src/bin/lpython.cpp index 83ea8f6ca0..4a5da1a3d8 100644 --- a/src/bin/lpython.cpp +++ b/src/bin/lpython.cpp @@ -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"); } diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 169c6c957f..1a1925e54c 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -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 &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( @@ -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::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 @@ -4888,6 +4875,14 @@ class BodyVisitor : public CommonVisitor { 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); + } + } + } if (tmp) { items.push_back(al, tmp); } else if (!tmp_vec.empty()) { @@ -4905,30 +4900,7 @@ class BodyVisitor : public CommonVisitor { 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(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; @@ -5012,7 +4984,7 @@ class BodyVisitor : public CommonVisitor { ASR::symbol_t *mod_sym = current_scope->resolve_symbol(mod_name); if (mod_sym) { ASR::Module_t *mod = ASR::down_cast(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); } } } @@ -5031,7 +5003,7 @@ class BodyVisitor : public CommonVisitor { ASR::symbol_t *mod_sym = current_scope->resolve_symbol(mod_name); if (mod_sym) { ASR::Module_t *mod = ASR::down_cast(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; } @@ -8444,7 +8416,7 @@ Result python_ast_to_asr(Allocator &al, LocationManager ASR::Module_t *mod = ASR::down_cast(mod_sym); LCOMPILERS_ASSERT(mod); std::vector 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)); From ff9e6db8f04b8cd5d6237f905c54dc3d8e11d86b Mon Sep 17 00:00:00 2001 From: Vipul Cariappa Date: Sat, 11 May 2024 10:14:56 +0530 Subject: [PATCH 2/5] updated reference tests --- tests/reference/llvm-structs_11-09fea6a.json | 2 +- .../reference/llvm-structs_11-09fea6a.stdout | 9 ---- ...thon-test_aggregate_constants-26c89d6.json | 2 +- ...on-test_aggregate_constants-26c89d6.stdout | 41 +++++++++---------- 4 files changed, 22 insertions(+), 32 deletions(-) diff --git a/tests/reference/llvm-structs_11-09fea6a.json b/tests/reference/llvm-structs_11-09fea6a.json index 88e4e9adba..c5d19b276c 100644 --- a/tests/reference/llvm-structs_11-09fea6a.json +++ b/tests/reference/llvm-structs_11-09fea6a.json @@ -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 diff --git a/tests/reference/llvm-structs_11-09fea6a.stdout b/tests/reference/llvm-structs_11-09fea6a.stdout index bc78d40bab..a175373e46 100644 --- a/tests/reference/llvm-structs_11-09fea6a.stdout +++ b/tests/reference/llvm-structs_11-09fea6a.stdout @@ -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 @@ -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 } diff --git a/tests/reference/python-test_aggregate_constants-26c89d6.json b/tests/reference/python-test_aggregate_constants-26c89d6.json index e161f9f994..d73a486eda 100644 --- a/tests/reference/python-test_aggregate_constants-26c89d6.json +++ b/tests/reference/python-test_aggregate_constants-26c89d6.json @@ -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 diff --git a/tests/reference/python-test_aggregate_constants-26c89d6.stdout b/tests/reference/python-test_aggregate_constants-26c89d6.stdout index 06d23bbf09..22655fba95 100644 --- a/tests/reference/python-test_aggregate_constants-26c89d6.stdout +++ b/tests/reference/python-test_aggregate_constants-26c89d6.stdout @@ -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(): From 24ee4e5c1ea486ebc7259a52ebb46d437f19ee93 Mon Sep 17 00:00:00 2001 From: Vipul Cariappa Date: Sat, 11 May 2024 11:19:44 +0530 Subject: [PATCH 3/5] add integration test --- integration_tests/CMakeLists.txt | 2 ++ integration_tests/list_01.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 integration_tests/list_01.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index cb5287543c..41fd208895 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -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 c) + 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) diff --git a/integration_tests/list_01.py b/integration_tests/list_01.py new file mode 100644 index 0000000000..088b2237dd --- /dev/null +++ b/integration_tests/list_01.py @@ -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) From 60dbd8f4e982e05a7f38987e9863735893db3cbe Mon Sep 17 00:00:00 2001 From: Vipul Cariappa Date: Sat, 11 May 2024 11:20:11 +0530 Subject: [PATCH 4/5] update according to code review suggestion --- src/lpython/semantics/python_ast_to_asr.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 1a1925e54c..b8492e07a0 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -4875,14 +4875,12 @@ class BodyVisitor : public CommonVisitor { 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); - } + 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()) { From 0d09a9bbbe9436202b0a781caa58bd0edd5974a1 Mon Sep 17 00:00:00 2001 From: Vipul Cariappa Date: Sat, 11 May 2024 11:57:46 +0530 Subject: [PATCH 5/5] fix failing tests --- integration_tests/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 41fd208895..7e78ba7ef8 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -508,7 +508,7 @@ 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 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)