Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ASR pass for unique symbols #2149

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

Merged
merged 9 commits into from
Jul 15, 2023
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
21 changes: 20 additions & 1 deletion src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ int emit_asr(const std::string &infile,
pass_options.always_run = true;
pass_options.verbose = compiler_options.verbose;
pass_options.pass_cumulative = compiler_options.pass_cumulative;
pass_options.all_symbols_mangling = compiler_options.all_symbols_mangling;
pass_options.module_name_mangling = compiler_options.module_name_mangling;
pass_options.global_symbols_mangling = compiler_options.global_symbols_mangling;
pass_options.intrinsic_symbols_mangling = compiler_options.intrinsic_symbols_mangling;


pass_manager.apply_passes(al, asr, pass_options, diagnostics);

Expand Down Expand Up @@ -345,6 +350,10 @@ int emit_c(const std::string &infile,
pass_options.run_fun = "f";
pass_options.always_run = true;
pass_options.verbose = compiler_options.verbose;
pass_options.all_symbols_mangling = compiler_options.all_symbols_mangling;
pass_options.module_name_mangling = compiler_options.module_name_mangling;
pass_options.global_symbols_mangling = compiler_options.global_symbols_mangling;
pass_options.intrinsic_symbols_mangling = compiler_options.intrinsic_symbols_mangling;

pass_manager.apply_passes(al, asr, pass_options, diagnostics);

Expand Down Expand Up @@ -398,6 +407,10 @@ int emit_c_to_file(const std::string &infile, const std::string &outfile,
pass_options.run_fun = "f";
pass_options.always_run = true;
pass_options.verbose = compiler_options.verbose;
pass_options.all_symbols_mangling = compiler_options.all_symbols_mangling;
pass_options.module_name_mangling = compiler_options.module_name_mangling;
pass_options.global_symbols_mangling = compiler_options.global_symbols_mangling;
pass_options.intrinsic_symbols_mangling = compiler_options.intrinsic_symbols_mangling;

pass_manager.apply_passes(al, asr, pass_options, diagnostics);

Expand Down Expand Up @@ -1501,6 +1514,7 @@ int main(int argc, char *argv[])
bool print_targets = false;
bool print_rtl_header_dir = false;
bool print_rtl_dir = false;
bool separate_compilation = false;

std::string arg_fmt_file;
// int arg_fmt_indent = 4;
Expand Down Expand Up @@ -1579,6 +1593,11 @@ int main(int argc, char *argv[])
app.add_flag("--enable-cpython", compiler_options.enable_cpython, "Enable CPython runtime");
app.add_flag("--enable-symengine", compiler_options.enable_symengine, "Enable Symengine runtime");
app.add_flag("--link-numpy", compiler_options.link_numpy, "Enable NumPy runtime (implies --enable-cpython)");
app.add_flag("--separate-compilation", separate_compilation, "Generates unique names for all the symbols");
app.add_flag("--module-mangling", compiler_options.module_name_mangling, "Mangles the module name");
app.add_flag("--global-mangling", compiler_options.global_symbols_mangling, "Mangles all the global symbols");
app.add_flag("--intrinsic-mangling", compiler_options.intrinsic_symbols_mangling, "Mangles all the intrinsic symbols");
app.add_flag("--all-mangling", compiler_options.all_symbols_mangling, "Mangles all possible symbols");

// LSP specific options
app.add_flag("--show-errors", show_errors, "Show errors when LSP is running in the background");
Expand Down Expand Up @@ -1616,7 +1635,7 @@ int main(int argc, char *argv[])
app.require_subcommand(0, 1);
CLI11_PARSE(app, argc, argv);

lcompilers_unique_ID = LCompilers::get_unique_ID();
lcompilers_unique_ID = separate_compilation ? LCompilers::get_unique_ID(): "";
Copy link
Contributor

Choose a reason for hiding this comment

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

I would not do that here, but rather have lcompilers_unique_ID always have a unique ID. But have this logic of turning it on and off in asr_scopes. If asr_scopes doesn't have access to separate_compilation then let's create another variable lcompilers_unique_ID_separate_compilation and have that variable on and off as needed.



if( compiler_options.fast && compiler_options.enable_bounds_checking ) {
Expand Down
1 change: 1 addition & 0 deletions src/libasr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ set(SRC
pass/pass_array_by_data.cpp
pass/pass_list_expr.cpp
pass/pass_compare.cpp
pass/unique_symbols.cpp

asr_verify.cpp
asr_utils.cpp
Expand Down
5 changes: 3 additions & 2 deletions src/libasr/asr_scopes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <libasr/asr_scopes.h>
#include <libasr/asr_utils.h>

std::string lcompilers_unique_ID;

std::string lcompilers_unique_ID;
namespace LCompilers {

template< typename T >
Expand Down Expand Up @@ -53,6 +53,7 @@ void SymbolTable::mark_all_variables_external(Allocator &al) {
}
}


ASR::symbol_t *SymbolTable::find_scoped_symbol(const std::string &name,
size_t n_scope_names, char **m_scope_names) {
const SymbolTable *s = this;
Expand Down Expand Up @@ -84,7 +85,7 @@ ASR::symbol_t *SymbolTable::find_scoped_symbol(const std::string &name,

std::string SymbolTable::get_unique_name(const std::string &name, bool use_unique_id) {
std::string unique_name = name;
if( use_unique_id ) {
if( use_unique_id && !lcompilers_unique_ID.empty()) {
unique_name += "_" + lcompilers_unique_ID;
}
int counter = 1;
Expand Down
1 change: 1 addition & 0 deletions src/libasr/gen_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"unused_functions",
"update_array_dim_intrinsic_calls",
"replace_where",
"unique_symbols",
]


Expand Down
10 changes: 7 additions & 3 deletions src/libasr/pass/pass_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <libasr/pass/create_subroutine_from_function.h>
#include <libasr/pass/transform_optional_argument_functions.h>
#include <libasr/pass/nested_vars.h>
#include <libasr/pass/unique_symbols.h>
#include <libasr/pass/replace_print_struct_type.h>
#include <libasr/asr_verify.h>

Expand Down Expand Up @@ -94,7 +95,8 @@ namespace LCompilers {
{"init_expr", &pass_replace_init_expr},
{"nested_vars", &pass_nested_vars},
{"where", &pass_replace_where},
{"print_struct_type", &pass_replace_print_struct_type}
{"print_struct_type", &pass_replace_print_struct_type},
{"unique_symbols", &pass_unique_symbols}
};

bool is_fast;
Expand Down Expand Up @@ -213,7 +215,8 @@ namespace LCompilers {
"select_case",
"inline_function_calls",
"unused_functions",
"transform_optional_argument_functions"
"transform_optional_argument_functions",
"unique_symbols"
};

_with_optimization_passes = {
Expand Down Expand Up @@ -244,7 +247,8 @@ namespace LCompilers {
"div_to_mul",
"fma",
"transform_optional_argument_functions",
"inline_function_calls"
"inline_function_calls",
"unique_symbols"
};

// These are re-write passes which are already handled
Expand Down
Loading