Skip to content

Commit 0b4d13a

Browse files
authored
ASR pass for unique symbols (#2149)
1 parent 8d69c93 commit 0b4d13a

File tree

8 files changed

+681
-6
lines changed

8 files changed

+681
-6
lines changed

src/bin/lpython.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ int emit_asr(const std::string &infile,
243243
pass_options.always_run = true;
244244
pass_options.verbose = compiler_options.verbose;
245245
pass_options.pass_cumulative = compiler_options.pass_cumulative;
246+
pass_options.all_symbols_mangling = compiler_options.all_symbols_mangling;
247+
pass_options.module_name_mangling = compiler_options.module_name_mangling;
248+
pass_options.global_symbols_mangling = compiler_options.global_symbols_mangling;
249+
pass_options.intrinsic_symbols_mangling = compiler_options.intrinsic_symbols_mangling;
250+
246251

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

@@ -345,6 +350,10 @@ int emit_c(const std::string &infile,
345350
pass_options.run_fun = "f";
346351
pass_options.always_run = true;
347352
pass_options.verbose = compiler_options.verbose;
353+
pass_options.all_symbols_mangling = compiler_options.all_symbols_mangling;
354+
pass_options.module_name_mangling = compiler_options.module_name_mangling;
355+
pass_options.global_symbols_mangling = compiler_options.global_symbols_mangling;
356+
pass_options.intrinsic_symbols_mangling = compiler_options.intrinsic_symbols_mangling;
348357

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

@@ -398,6 +407,10 @@ int emit_c_to_file(const std::string &infile, const std::string &outfile,
398407
pass_options.run_fun = "f";
399408
pass_options.always_run = true;
400409
pass_options.verbose = compiler_options.verbose;
410+
pass_options.all_symbols_mangling = compiler_options.all_symbols_mangling;
411+
pass_options.module_name_mangling = compiler_options.module_name_mangling;
412+
pass_options.global_symbols_mangling = compiler_options.global_symbols_mangling;
413+
pass_options.intrinsic_symbols_mangling = compiler_options.intrinsic_symbols_mangling;
401414

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

@@ -1501,6 +1514,7 @@ int main(int argc, char *argv[])
15011514
bool print_targets = false;
15021515
bool print_rtl_header_dir = false;
15031516
bool print_rtl_dir = false;
1517+
bool separate_compilation = false;
15041518

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

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

1619-
lcompilers_unique_ID = LCompilers::get_unique_ID();
1638+
lcompilers_unique_ID = separate_compilation ? LCompilers::get_unique_ID(): "";
16201639

16211640

16221641
if( compiler_options.fast && compiler_options.enable_bounds_checking ) {

src/libasr/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ set(SRC
6262
pass/pass_array_by_data.cpp
6363
pass/pass_list_expr.cpp
6464
pass/pass_compare.cpp
65+
pass/unique_symbols.cpp
6566

6667
asr_verify.cpp
6768
asr_utils.cpp

src/libasr/asr_scopes.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include <libasr/asr_scopes.h>
55
#include <libasr/asr_utils.h>
66

7-
std::string lcompilers_unique_ID;
87

8+
std::string lcompilers_unique_ID;
99
namespace LCompilers {
1010

1111
template< typename T >
@@ -53,6 +53,7 @@ void SymbolTable::mark_all_variables_external(Allocator &al) {
5353
}
5454
}
5555

56+
5657
ASR::symbol_t *SymbolTable::find_scoped_symbol(const std::string &name,
5758
size_t n_scope_names, char **m_scope_names) {
5859
const SymbolTable *s = this;
@@ -84,7 +85,7 @@ ASR::symbol_t *SymbolTable::find_scoped_symbol(const std::string &name,
8485

8586
std::string SymbolTable::get_unique_name(const std::string &name, bool use_unique_id) {
8687
std::string unique_name = name;
87-
if( use_unique_id ) {
88+
if( use_unique_id && !lcompilers_unique_ID.empty()) {
8889
unique_name += "_" + lcompilers_unique_ID;
8990
}
9091
int counter = 1;

src/libasr/gen_pass.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"unused_functions",
3333
"update_array_dim_intrinsic_calls",
3434
"replace_where",
35+
"unique_symbols",
3536
]
3637

3738

src/libasr/pass/pass_manager.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <libasr/pass/create_subroutine_from_function.h>
4747
#include <libasr/pass/transform_optional_argument_functions.h>
4848
#include <libasr/pass/nested_vars.h>
49+
#include <libasr/pass/unique_symbols.h>
4950
#include <libasr/pass/replace_print_struct_type.h>
5051
#include <libasr/asr_verify.h>
5152

@@ -94,7 +95,8 @@ namespace LCompilers {
9495
{"init_expr", &pass_replace_init_expr},
9596
{"nested_vars", &pass_nested_vars},
9697
{"where", &pass_replace_where},
97-
{"print_struct_type", &pass_replace_print_struct_type}
98+
{"print_struct_type", &pass_replace_print_struct_type},
99+
{"unique_symbols", &pass_unique_symbols}
98100
};
99101

100102
bool is_fast;
@@ -213,7 +215,8 @@ namespace LCompilers {
213215
"select_case",
214216
"inline_function_calls",
215217
"unused_functions",
216-
"transform_optional_argument_functions"
218+
"transform_optional_argument_functions",
219+
"unique_symbols"
217220
};
218221

219222
_with_optimization_passes = {
@@ -244,7 +247,8 @@ namespace LCompilers {
244247
"div_to_mul",
245248
"fma",
246249
"transform_optional_argument_functions",
247-
"inline_function_calls"
250+
"inline_function_calls",
251+
"unique_symbols"
248252
};
249253

250254
// These are re-write passes which are already handled

0 commit comments

Comments
 (0)