From 1974914bcd7fdeef29b3962ada79b60631776e76 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Tue, 11 Jul 2023 11:27:47 +0530 Subject: [PATCH 1/9] ASR pass for unique symbols --- src/bin/lpython.cpp | 2 +- src/libasr/CMakeLists.txt | 1 + src/libasr/asr_scopes.cpp | 13 ++ src/libasr/asr_scopes.h | 2 + src/libasr/codegen/asr_to_c.cpp | 1 + src/libasr/pass/pass_manager.h | 10 +- src/libasr/pass/unique_symbols.cpp | 304 +++++++++++++++++++++++++++++ src/libasr/pass/unique_symbols.h | 14 ++ 8 files changed, 343 insertions(+), 4 deletions(-) create mode 100644 src/libasr/pass/unique_symbols.cpp create mode 100644 src/libasr/pass/unique_symbols.h diff --git a/src/bin/lpython.cpp b/src/bin/lpython.cpp index 715b29eadd..475b12dfcf 100644 --- a/src/bin/lpython.cpp +++ b/src/bin/lpython.cpp @@ -1616,7 +1616,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 = "__" + LCompilers::get_unique_ID(); if( compiler_options.fast && compiler_options.enable_bounds_checking ) { diff --git a/src/libasr/CMakeLists.txt b/src/libasr/CMakeLists.txt index fcd1ea6af3..bac6d4aa74 100644 --- a/src/libasr/CMakeLists.txt +++ b/src/libasr/CMakeLists.txt @@ -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 diff --git a/src/libasr/asr_scopes.cpp b/src/libasr/asr_scopes.cpp index 91fe330963..581a3dcc0e 100644 --- a/src/libasr/asr_scopes.cpp +++ b/src/libasr/asr_scopes.cpp @@ -4,6 +4,7 @@ #include #include + std::string lcompilers_unique_ID; namespace LCompilers { @@ -53,6 +54,18 @@ void SymbolTable::mark_all_variables_external(Allocator &al) { } } +// Create unique names in the symbol table that are replaced by +// name = name + lcompilers_unique_ID; +void SymbolTable::create_unique_symbols(std::string prefix) { + std::map tmp; + for (auto &it: scope) { + tmp[prefix + it.first + lcompilers_unique_ID] = it.second; + } + scope.clear(); + scope = tmp; + tmp.clear(); +} + ASR::symbol_t *SymbolTable::find_scoped_symbol(const std::string &name, size_t n_scope_names, char **m_scope_names) { const SymbolTable *s = this; diff --git a/src/libasr/asr_scopes.h b/src/libasr/asr_scopes.h index cd7cb96995..4cc6f4e7a1 100644 --- a/src/libasr/asr_scopes.h +++ b/src/libasr/asr_scopes.h @@ -89,6 +89,8 @@ struct SymbolTable { scope[name] = symbol; } + void create_unique_symbols(std::string prefix=""); + // Marks all variables as external void mark_all_variables_external(Allocator &al); diff --git a/src/libasr/codegen/asr_to_c.cpp b/src/libasr/codegen/asr_to_c.cpp index a63f5098c3..e3da532ba5 100644 --- a/src/libasr/codegen/asr_to_c.cpp +++ b/src/libasr/codegen/asr_to_c.cpp @@ -656,6 +656,7 @@ R"( std::vector build_order = ASRUtils::determine_module_dependencies(x); for (auto &item : build_order) { + std::cerr<<"line 641-- "<get_scope().find(item) != x.m_global_scope->get_scope().end()); if (startswith(item, "lfortran_intrinsic")) { diff --git a/src/libasr/pass/pass_manager.h b/src/libasr/pass/pass_manager.h index 801719367f..80f21c2c21 100644 --- a/src/libasr/pass/pass_manager.h +++ b/src/libasr/pass/pass_manager.h @@ -46,6 +46,7 @@ #include #include #include +#include #include #include @@ -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; @@ -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 = { @@ -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 diff --git a/src/libasr/pass/unique_symbols.cpp b/src/libasr/pass/unique_symbols.cpp new file mode 100644 index 0000000000..12d8a7e554 --- /dev/null +++ b/src/libasr/pass/unique_symbols.cpp @@ -0,0 +1,304 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + +namespace LCompilers { + +using ASR::down_cast; + +class UniqueSymbolVisitor: public ASR::CallReplacerOnExpressionsVisitor { + private: + + Allocator& al; + + public: + std::map orig_to_new_name; + std::string mod_name = ""; + + UniqueSymbolVisitor(Allocator& al_) : al(al_){} + + + void visit_TranslationUnit(const ASR::TranslationUnit_t &x) { + ASR::TranslationUnit_t& xx = const_cast(x); + SymbolTable* current_scope_copy = current_scope; + current_scope = xx.m_global_scope; + xx.m_global_scope->create_unique_symbols(); + for (auto &a : xx.m_global_scope->get_scope()) { + orig_to_new_name[a.second] = a.first; + this->visit_symbol(*a.second); + } + current_scope = current_scope_copy; + } + + void visit_Program(const ASR::Program_t &x) { + ASR::Program_t& xx = const_cast(x); + SymbolTable* current_scope_copy = current_scope; + current_scope = x.m_symtab; + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); + xx.m_name = s2c(al, orig_to_new_name[sym]); + xx.m_symtab->create_unique_symbols(); + for (auto &a : xx.m_symtab->get_scope()) { + orig_to_new_name[a.second] = a.first; + this->visit_symbol(*a.second); + } + transform_stmts(xx.m_body, xx.n_body); + current_scope = current_scope_copy; + } + + void visit_Module(const ASR::Module_t &x) { + ASR::Module_t& xx = const_cast(x); + SymbolTable* current_scope_copy = current_scope; + current_scope = x.m_symtab; + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); + xx.m_name = s2c(al, orig_to_new_name[sym]); + mod_name = orig_to_new_name[sym] + "_"; + xx.m_symtab->create_unique_symbols(mod_name); + for (auto &a : xx.m_symtab->get_scope()) { + orig_to_new_name[a.second] = a.first; + this->visit_symbol(*a.second); + } + current_scope = current_scope_copy; + mod_name = ""; + } + + void visit_Function(const ASR::Function_t &x) { + ASR::Function_t& xx = const_cast(x); + SymbolTable* current_scope_copy = current_scope; + current_scope = x.m_symtab; + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); + xx.m_name = s2c(al, orig_to_new_name[sym]); + xx.m_symtab->create_unique_symbols(); + for (auto &a : x.m_symtab->get_scope()) { + orig_to_new_name[a.second] = a.first; + this->visit_symbol(*a.second); + } + visit_ttype(*x.m_function_signature); + for (size_t i=0; i(&(x.m_args[i])); + call_replacer(); + current_expr = current_expr_copy_0; + if( x.m_args[i] ) + visit_expr(*x.m_args[i]); + } + transform_stmts(xx.m_body, xx.n_body); + if (xx.m_return_var) { + ASR::expr_t** current_expr_copy_1 = current_expr; + current_expr = const_cast(&(x.m_return_var)); + call_replacer(); + current_expr = current_expr_copy_1; + if( x.m_return_var ) + visit_expr(*x.m_return_var); + } + current_scope = current_scope_copy; + } + void visit_GenericProcedure(const ASR::GenericProcedure_t &x) { + ASR::GenericProcedure_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); + xx.m_name = s2c(al, orig_to_new_name[sym]); + } + void visit_CustomOperator(const ASR::CustomOperator_t &x) { + ASR::CustomOperator_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); + xx.m_name = s2c(al, orig_to_new_name[sym]); + } + + void visit_ExternalSymbol(const ASR::ExternalSymbol_t &x) { + ASR::ExternalSymbol_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); + xx.m_name = s2c(al, orig_to_new_name[sym]); + LCOMPILERS_ASSERT(orig_to_new_name.find(xx.m_external) != orig_to_new_name.end()); + xx.m_original_name = s2c(al, orig_to_new_name[xx.m_external]); + ASR::Module_t *m = ASRUtils::get_sym_module(x.m_external); + ASR::symbol_t *m_sym = ASR::down_cast((ASR::asr_t*)m); + LCOMPILERS_ASSERT(orig_to_new_name.find(m_sym) != orig_to_new_name.end()); + xx.m_module_name = s2c(al, orig_to_new_name[m_sym]); + } + + void visit_StructType(const ASR::StructType_t &x) { + ASR::StructType_t& xx = const_cast(x); + SymbolTable* current_scope_copy = current_scope; + current_scope = x.m_symtab; + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); + xx.m_name = s2c(al, orig_to_new_name[sym]); + xx.m_symtab->create_unique_symbols(); + for (auto &a : x.m_symtab->get_scope()) { + orig_to_new_name[a.second] = a.first; + this->visit_symbol(*a.second); + } + for (size_t i=0; i(&(x.m_alignment)); + call_replacer(); + current_expr = current_expr_copy_2; + if( x.m_alignment ) + visit_expr(*x.m_alignment); + } + current_scope = current_scope_copy; + } + void visit_EnumType(const ASR::EnumType_t &x) { + ASR::EnumType_t& xx = const_cast(x); + SymbolTable* current_scope_copy = current_scope; + current_scope = x.m_symtab; + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); + xx.m_name = s2c(al, orig_to_new_name[sym]); + xx.m_symtab->create_unique_symbols(); + for (auto &a : x.m_symtab->get_scope()) { + orig_to_new_name[a.second] = a.first; + this->visit_symbol(*a.second); + } + visit_ttype(*x.m_type); + current_scope = current_scope_copy; + } + void visit_UnionType(const ASR::UnionType_t &x) { + ASR::UnionType_t& xx = const_cast(x); + SymbolTable* current_scope_copy = current_scope; + current_scope = x.m_symtab; + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); + xx.m_name = s2c(al, orig_to_new_name[sym]); + xx.m_symtab->create_unique_symbols(); + for (auto &a : x.m_symtab->get_scope()) { + orig_to_new_name[a.second] = a.first; + this->visit_symbol(*a.second); + } + for (size_t i=0; i(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); + xx.m_name = s2c(al, orig_to_new_name[sym]); + SymbolTable* current_scope_copy = current_scope; + current_scope = x.m_parent_symtab; + if (x.m_symbolic_value) { + ASR::expr_t** current_expr_copy_3 = current_expr; + current_expr = const_cast(&(x.m_symbolic_value)); + call_replacer(); + current_expr = current_expr_copy_3; + if( x.m_symbolic_value ) + visit_expr(*x.m_symbolic_value); + } + if (x.m_value) { + ASR::expr_t** current_expr_copy_4 = current_expr; + current_expr = const_cast(&(x.m_value)); + call_replacer(); + current_expr = current_expr_copy_4; + if( x.m_value ) + visit_expr(*x.m_value); + } + visit_ttype(*x.m_type); + current_scope = current_scope_copy; + } + void visit_ClassType(const ASR::ClassType_t &x) { + ASR::ClassType_t& xx = const_cast(x); + SymbolTable* current_scope_copy = current_scope; + current_scope = x.m_symtab; + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); + xx.m_name = s2c(al, orig_to_new_name[sym]); + xx.m_symtab->create_unique_symbols(); + for (auto &a : xx.m_symtab->get_scope()) { + orig_to_new_name[a.second] = a.first; + this->visit_symbol(*a.second); + } + current_scope = current_scope_copy; + } + void visit_ClassProcedure(const ASR::ClassProcedure_t &x) { + ASR::ClassProcedure_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); + xx.m_name = s2c(al, orig_to_new_name[sym]); + } + void visit_AssociateBlock(const ASR::AssociateBlock_t &x) { + ASR::AssociateBlock_t& xx = const_cast(x); + SymbolTable* current_scope_copy = current_scope; + current_scope = x.m_symtab; + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); + xx.m_name = s2c(al, orig_to_new_name[sym]); + xx.m_symtab->create_unique_symbols(); + for (auto &a : xx.m_symtab->get_scope()) { + orig_to_new_name[a.second] = a.first; + this->visit_symbol(*a.second); + } + transform_stmts(xx.m_body, xx.n_body); + current_scope = current_scope_copy; + } + void visit_Block(const ASR::Block_t &x) { + ASR::Block_t& xx = const_cast(x); + SymbolTable* current_scope_copy = current_scope; + current_scope = x.m_symtab; + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); + xx.m_name = s2c(al, orig_to_new_name[sym]); + xx.m_symtab->create_unique_symbols(); + for (auto &a : xx.m_symtab->get_scope()) { + orig_to_new_name[a.second] = a.first; + this->visit_symbol(*a.second); + } + transform_stmts(xx.m_body, xx.n_body); + current_scope = current_scope_copy; + } + void visit_Requirement(const ASR::Requirement_t &x) { + ASR::Requirement_t& xx = const_cast(x); + SymbolTable* current_scope_copy = current_scope; + current_scope = x.m_symtab; + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); + xx.m_name = s2c(al, orig_to_new_name[sym]); + xx.m_symtab->create_unique_symbols(); + for (auto &a : xx.m_symtab->get_scope()) { + orig_to_new_name[a.second] = a.first; + this->visit_symbol(*a.second); + } + current_scope = current_scope_copy; + } + void visit_Template(const ASR::Template_t &x) { + ASR::Template_t& xx = const_cast(x); + SymbolTable* current_scope_copy = current_scope; + current_scope = x.m_symtab; + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); + xx.m_name = s2c(al, orig_to_new_name[sym]); + xx.m_symtab->create_unique_symbols(); + for (auto &a : xx.m_symtab->get_scope()) { + orig_to_new_name[a.second] = a.first; + this->visit_symbol(*a.second); + } + current_scope = current_scope_copy; + } + +}; + + +void pass_unique_symbols(Allocator &al, ASR::TranslationUnit_t &unit, + const LCompilers::PassOptions& /*pass_options*/) { + UniqueSymbolVisitor v(al); + v.visit_TranslationUnit(unit); + PassUtils::UpdateDependenciesVisitor x(al); + x.visit_TranslationUnit(unit); +} + + +} // namespace LCompilers diff --git a/src/libasr/pass/unique_symbols.h b/src/libasr/pass/unique_symbols.h new file mode 100644 index 0000000000..4d69b71a38 --- /dev/null +++ b/src/libasr/pass/unique_symbols.h @@ -0,0 +1,14 @@ +#ifndef LIBASR_PASS_UNIQUE_SYMBOLS_H +#define LIBASR_PASS_UNIQUE_SYMBOLS_H + +#include +#include + +namespace LCompilers { + + void pass_unique_symbols(Allocator &al, ASR::TranslationUnit_t &unit, + const PassOptions &pass_options); + +} // namespace LCompilers + +#endif // LIBASR_PASS_UNIQUE_SYMBOLS_H From a6925466ad88f0fe7c8f34ca24ad9b7d29a8d3fe Mon Sep 17 00:00:00 2001 From: Smit-create Date: Tue, 11 Jul 2023 17:07:58 +0530 Subject: [PATCH 2/9] fix --- src/libasr/asr_scopes.cpp | 13 - src/libasr/asr_scopes.h | 2 - src/libasr/pass/unique_symbols.cpp | 385 +++++++++++++++++------------ 3 files changed, 223 insertions(+), 177 deletions(-) diff --git a/src/libasr/asr_scopes.cpp b/src/libasr/asr_scopes.cpp index 581a3dcc0e..72652fa9eb 100644 --- a/src/libasr/asr_scopes.cpp +++ b/src/libasr/asr_scopes.cpp @@ -5,8 +5,6 @@ #include -std::string lcompilers_unique_ID; - namespace LCompilers { template< typename T > @@ -54,17 +52,6 @@ void SymbolTable::mark_all_variables_external(Allocator &al) { } } -// Create unique names in the symbol table that are replaced by -// name = name + lcompilers_unique_ID; -void SymbolTable::create_unique_symbols(std::string prefix) { - std::map tmp; - for (auto &it: scope) { - tmp[prefix + it.first + lcompilers_unique_ID] = it.second; - } - scope.clear(); - scope = tmp; - tmp.clear(); -} ASR::symbol_t *SymbolTable::find_scoped_symbol(const std::string &name, size_t n_scope_names, char **m_scope_names) { diff --git a/src/libasr/asr_scopes.h b/src/libasr/asr_scopes.h index 4cc6f4e7a1..cd7cb96995 100644 --- a/src/libasr/asr_scopes.h +++ b/src/libasr/asr_scopes.h @@ -89,8 +89,6 @@ struct SymbolTable { scope[name] = symbol; } - void create_unique_symbols(std::string prefix=""); - // Marks all variables as external void mark_all_variables_external(Allocator &al); diff --git a/src/libasr/pass/unique_symbols.cpp b/src/libasr/pass/unique_symbols.cpp index 12d8a7e554..81c974e69f 100644 --- a/src/libasr/pass/unique_symbols.cpp +++ b/src/libasr/pass/unique_symbols.cpp @@ -8,17 +8,27 @@ #include +std::string lcompilers_unique_ID; + namespace LCompilers { using ASR::down_cast; -class UniqueSymbolVisitor: public ASR::CallReplacerOnExpressionsVisitor { +std::string update_name(std::string curr_name, std::string prefix="") { + if (startswith(curr_name, "_lpython") || startswith(curr_name, "_lfortran") ) { + return curr_name; + } + return prefix + curr_name + lcompilers_unique_ID; +} + +class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { private: Allocator& al; public: - std::map orig_to_new_name; + std::map sym_to_new_name; + std::map tmp_current_syms; std::string mod_name = ""; UniqueSymbolVisitor(Allocator& al_) : al(al_){} @@ -26,267 +36,318 @@ class UniqueSymbolVisitor: public ASR::CallReplacerOnExpressionsVisitor(x); - SymbolTable* current_scope_copy = current_scope; - current_scope = xx.m_global_scope; - xx.m_global_scope->create_unique_symbols(); + std::map tmp_scope; for (auto &a : xx.m_global_scope->get_scope()) { - orig_to_new_name[a.second] = a.first; - this->visit_symbol(*a.second); + tmp_scope[a.second] = a.first; + if (sym_to_new_name.find(a.second) == sym_to_new_name.end()) { + visit_symbol(*a.second); + } + } + for (auto &a: tmp_scope) { + xx.m_global_scope->erase_symbol(a.second); + xx.m_global_scope->add_symbol(sym_to_new_name[a.first], a.first); } - current_scope = current_scope_copy; } void visit_Program(const ASR::Program_t &x) { ASR::Program_t& xx = const_cast(x); - SymbolTable* current_scope_copy = current_scope; - current_scope = x.m_symtab; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); - xx.m_name = s2c(al, orig_to_new_name[sym]); - xx.m_symtab->create_unique_symbols(); + if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { + return; + } + std::map tmp_scope; + xx.m_name = s2c(al, update_name(xx.m_name)); + sym_to_new_name[sym] = xx.m_name; + for (size_t i=0; iget_scope()) { - orig_to_new_name[a.second] = a.first; - this->visit_symbol(*a.second); + tmp_scope[a.second] = a.first; + if (sym_to_new_name.find(a.second) == sym_to_new_name.end()) { + visit_symbol(*a.second); + } + } + for (auto &a: tmp_scope) { + xx.m_symtab->erase_symbol(a.second); + xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); } - transform_stmts(xx.m_body, xx.n_body); - current_scope = current_scope_copy; } void visit_Module(const ASR::Module_t &x) { ASR::Module_t& xx = const_cast(x); - SymbolTable* current_scope_copy = current_scope; - current_scope = x.m_symtab; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); - xx.m_name = s2c(al, orig_to_new_name[sym]); - mod_name = orig_to_new_name[sym] + "_"; - xx.m_symtab->create_unique_symbols(mod_name); + if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { + return; + } + std::map tmp_scope; + xx.m_name = s2c(al, update_name(xx.m_name)); + mod_name = xx.m_name; + sym_to_new_name[sym] = xx.m_name; + for (size_t i=0; iget_scope()) { - orig_to_new_name[a.second] = a.first; - this->visit_symbol(*a.second); + tmp_scope[a.second] = a.first; + if (sym_to_new_name.find(a.second) == sym_to_new_name.end()) { + visit_symbol(*a.second); + } + } + for (auto &a: tmp_scope) { + xx.m_symtab->erase_symbol(a.second); + xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); } - current_scope = current_scope_copy; mod_name = ""; } void visit_Function(const ASR::Function_t &x) { ASR::Function_t& xx = const_cast(x); - SymbolTable* current_scope_copy = current_scope; - current_scope = x.m_symtab; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); - xx.m_name = s2c(al, orig_to_new_name[sym]); - xx.m_symtab->create_unique_symbols(); - for (auto &a : x.m_symtab->get_scope()) { - orig_to_new_name[a.second] = a.first; - this->visit_symbol(*a.second); + if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { + return; + } + std::map tmp_scope; + xx.m_name = s2c(al, update_name(xx.m_name)); + sym_to_new_name[sym] = xx.m_name; + for (size_t i=0; iget_scope()) { + tmp_scope[a.second] = a.first; + if (sym_to_new_name.find(a.second) == sym_to_new_name.end()) { + visit_symbol(*a.second); + } + } + for (auto &a: tmp_scope) { + xx.m_symtab->erase_symbol(a.second); + xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); } - visit_ttype(*x.m_function_signature); - for (size_t i=0; i(&(x.m_args[i])); - call_replacer(); - current_expr = current_expr_copy_0; - if( x.m_args[i] ) - visit_expr(*x.m_args[i]); - } - transform_stmts(xx.m_body, xx.n_body); - if (xx.m_return_var) { - ASR::expr_t** current_expr_copy_1 = current_expr; - current_expr = const_cast(&(x.m_return_var)); - call_replacer(); - current_expr = current_expr_copy_1; - if( x.m_return_var ) - visit_expr(*x.m_return_var); - } - current_scope = current_scope_copy; } + void visit_GenericProcedure(const ASR::GenericProcedure_t &x) { ASR::GenericProcedure_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); - xx.m_name = s2c(al, orig_to_new_name[sym]); + if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { + return; + } + xx.m_name = s2c(al, update_name(xx.m_name)); + sym_to_new_name[sym] = xx.m_name; } + void visit_CustomOperator(const ASR::CustomOperator_t &x) { ASR::CustomOperator_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); - xx.m_name = s2c(al, orig_to_new_name[sym]); + if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { + return; + } + xx.m_name = s2c(al, update_name(xx.m_name)); + sym_to_new_name[sym] = xx.m_name; } void visit_ExternalSymbol(const ASR::ExternalSymbol_t &x) { ASR::ExternalSymbol_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); - xx.m_name = s2c(al, orig_to_new_name[sym]); - LCOMPILERS_ASSERT(orig_to_new_name.find(xx.m_external) != orig_to_new_name.end()); - xx.m_original_name = s2c(al, orig_to_new_name[xx.m_external]); ASR::Module_t *m = ASRUtils::get_sym_module(x.m_external); ASR::symbol_t *m_sym = ASR::down_cast((ASR::asr_t*)m); - LCOMPILERS_ASSERT(orig_to_new_name.find(m_sym) != orig_to_new_name.end()); - xx.m_module_name = s2c(al, orig_to_new_name[m_sym]); + if (sym_to_new_name.find(m_sym) == sym_to_new_name.end()) { + visit_symbol(*m_sym); + } + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { + return; + } + xx.m_name = s2c(al, update_name(xx.m_name)); + sym_to_new_name[sym] = xx.m_name; + xx.m_original_name = s2c(al, update_name(xx.m_original_name)); + xx.m_module_name = s2c(al, update_name(xx.m_module_name)); } void visit_StructType(const ASR::StructType_t &x) { ASR::StructType_t& xx = const_cast(x); - SymbolTable* current_scope_copy = current_scope; - current_scope = x.m_symtab; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); - xx.m_name = s2c(al, orig_to_new_name[sym]); - xx.m_symtab->create_unique_symbols(); - for (auto &a : x.m_symtab->get_scope()) { - orig_to_new_name[a.second] = a.first; - this->visit_symbol(*a.second); + if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { + return; } - for (size_t i=0; i tmp_scope; + xx.m_name = s2c(al, update_name(xx.m_name)); + sym_to_new_name[sym] = xx.m_name; + for (size_t i=0; iget_scope()) { + tmp_scope[a.second] = a.first; + this->visit_symbol(*a.second); } - if (x.m_alignment) { - ASR::expr_t** current_expr_copy_2 = current_expr; - current_expr = const_cast(&(x.m_alignment)); - call_replacer(); - current_expr = current_expr_copy_2; - if( x.m_alignment ) - visit_expr(*x.m_alignment); + for (auto &a: tmp_scope) { + xx.m_symtab->erase_symbol(a.second); + xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); } - current_scope = current_scope_copy; } + void visit_EnumType(const ASR::EnumType_t &x) { ASR::EnumType_t& xx = const_cast(x); - SymbolTable* current_scope_copy = current_scope; - current_scope = x.m_symtab; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); - xx.m_name = s2c(al, orig_to_new_name[sym]); - xx.m_symtab->create_unique_symbols(); - for (auto &a : x.m_symtab->get_scope()) { - orig_to_new_name[a.second] = a.first; + if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { + return; + } + std::map tmp_scope; + xx.m_name = s2c(al, update_name(xx.m_name)); + sym_to_new_name[sym] = xx.m_name; + for (size_t i=0; iget_scope()) { + tmp_scope[a.second] = a.first; this->visit_symbol(*a.second); } - visit_ttype(*x.m_type); - current_scope = current_scope_copy; + for (auto &a: tmp_scope) { + xx.m_symtab->erase_symbol(a.second); + xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); + } } + void visit_UnionType(const ASR::UnionType_t &x) { ASR::UnionType_t& xx = const_cast(x); - SymbolTable* current_scope_copy = current_scope; - current_scope = x.m_symtab; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); - xx.m_name = s2c(al, orig_to_new_name[sym]); - xx.m_symtab->create_unique_symbols(); - for (auto &a : x.m_symtab->get_scope()) { - orig_to_new_name[a.second] = a.first; + if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { + return; + } + std::map tmp_scope; + xx.m_name = s2c(al, update_name(xx.m_name)); + sym_to_new_name[sym] = xx.m_name; + for (size_t i=0; iget_scope()) { + tmp_scope[a.second] = a.first; this->visit_symbol(*a.second); } - for (size_t i=0; ierase_symbol(a.second); + xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); } - current_scope = current_scope_copy; } + void visit_Variable(const ASR::Variable_t &x) { ASR::Variable_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); - xx.m_name = s2c(al, orig_to_new_name[sym]); - SymbolTable* current_scope_copy = current_scope; - current_scope = x.m_parent_symtab; - if (x.m_symbolic_value) { - ASR::expr_t** current_expr_copy_3 = current_expr; - current_expr = const_cast(&(x.m_symbolic_value)); - call_replacer(); - current_expr = current_expr_copy_3; - if( x.m_symbolic_value ) - visit_expr(*x.m_symbolic_value); - } - if (x.m_value) { - ASR::expr_t** current_expr_copy_4 = current_expr; - current_expr = const_cast(&(x.m_value)); - call_replacer(); - current_expr = current_expr_copy_4; - if( x.m_value ) - visit_expr(*x.m_value); - } - visit_ttype(*x.m_type); - current_scope = current_scope_copy; + xx.m_name = s2c(al, update_name(xx.m_name)); + sym_to_new_name[sym] = xx.m_name; + for (size_t i=0; i(x); - SymbolTable* current_scope_copy = current_scope; - current_scope = x.m_symtab; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); - xx.m_name = s2c(al, orig_to_new_name[sym]); - xx.m_symtab->create_unique_symbols(); + if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { + return; + } + std::map tmp_scope; + xx.m_name = s2c(al, update_name(xx.m_name)); + sym_to_new_name[sym] = xx.m_name; for (auto &a : xx.m_symtab->get_scope()) { - orig_to_new_name[a.second] = a.first; + tmp_scope[a.second] = a.first; this->visit_symbol(*a.second); } - current_scope = current_scope_copy; + for (auto &a: tmp_scope) { + xx.m_symtab->erase_symbol(a.second); + xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); + } } + void visit_ClassProcedure(const ASR::ClassProcedure_t &x) { ASR::ClassProcedure_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); - xx.m_name = s2c(al, orig_to_new_name[sym]); + if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { + return; + } + xx.m_name = s2c(al, update_name(xx.m_name)); + sym_to_new_name[sym] = xx.m_name; } + void visit_AssociateBlock(const ASR::AssociateBlock_t &x) { ASR::AssociateBlock_t& xx = const_cast(x); - SymbolTable* current_scope_copy = current_scope; - current_scope = x.m_symtab; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); - xx.m_name = s2c(al, orig_to_new_name[sym]); - xx.m_symtab->create_unique_symbols(); + if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { + return; + } + std::map tmp_scope; + xx.m_name = s2c(al, update_name(xx.m_name)); + sym_to_new_name[sym] = xx.m_name; for (auto &a : xx.m_symtab->get_scope()) { - orig_to_new_name[a.second] = a.first; + tmp_scope[a.second] = a.first; this->visit_symbol(*a.second); } - transform_stmts(xx.m_body, xx.n_body); - current_scope = current_scope_copy; + for (auto &a: tmp_scope) { + xx.m_symtab->erase_symbol(a.second); + xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); + } } + void visit_Block(const ASR::Block_t &x) { ASR::Block_t& xx = const_cast(x); - SymbolTable* current_scope_copy = current_scope; - current_scope = x.m_symtab; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); - xx.m_name = s2c(al, orig_to_new_name[sym]); - xx.m_symtab->create_unique_symbols(); + if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { + return; + } + std::map tmp_scope; + xx.m_name = s2c(al, update_name(xx.m_name)); + sym_to_new_name[sym] = xx.m_name; for (auto &a : xx.m_symtab->get_scope()) { - orig_to_new_name[a.second] = a.first; + tmp_scope[a.second] = a.first; this->visit_symbol(*a.second); } - transform_stmts(xx.m_body, xx.n_body); - current_scope = current_scope_copy; + for (auto &a: tmp_scope) { + xx.m_symtab->erase_symbol(a.second); + xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); + } } + void visit_Requirement(const ASR::Requirement_t &x) { ASR::Requirement_t& xx = const_cast(x); - SymbolTable* current_scope_copy = current_scope; - current_scope = x.m_symtab; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); - xx.m_name = s2c(al, orig_to_new_name[sym]); - xx.m_symtab->create_unique_symbols(); + if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { + return; + } + std::map tmp_scope; + xx.m_name = s2c(al, update_name(xx.m_name)); + sym_to_new_name[sym] = xx.m_name; for (auto &a : xx.m_symtab->get_scope()) { - orig_to_new_name[a.second] = a.first; + tmp_scope[a.second] = a.first; this->visit_symbol(*a.second); } - current_scope = current_scope_copy; + for (auto &a: tmp_scope) { + xx.m_symtab->erase_symbol(a.second); + xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); + } } + void visit_Template(const ASR::Template_t &x) { ASR::Template_t& xx = const_cast(x); - SymbolTable* current_scope_copy = current_scope; - current_scope = x.m_symtab; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - LCOMPILERS_ASSERT(orig_to_new_name.find(sym) != orig_to_new_name.end()); - xx.m_name = s2c(al, orig_to_new_name[sym]); - xx.m_symtab->create_unique_symbols(); + if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { + return; + } + std::map tmp_scope; + xx.m_name = s2c(al, update_name(xx.m_name)); + sym_to_new_name[sym] = xx.m_name; for (auto &a : xx.m_symtab->get_scope()) { - orig_to_new_name[a.second] = a.first; + tmp_scope[a.second] = a.first; this->visit_symbol(*a.second); } - current_scope = current_scope_copy; + for (auto &a: tmp_scope) { + xx.m_symtab->erase_symbol(a.second); + xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); + } } }; From d1fd1774793df98e83ca9302bce2e55641ac2abd Mon Sep 17 00:00:00 2001 From: Smit-create Date: Tue, 11 Jul 2023 17:32:52 +0530 Subject: [PATCH 3/9] fix bindc --- src/libasr/codegen/asr_to_c.cpp | 1 - src/libasr/pass/unique_symbols.cpp | 24 ++++++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/libasr/codegen/asr_to_c.cpp b/src/libasr/codegen/asr_to_c.cpp index e3da532ba5..a63f5098c3 100644 --- a/src/libasr/codegen/asr_to_c.cpp +++ b/src/libasr/codegen/asr_to_c.cpp @@ -656,7 +656,6 @@ R"( std::vector build_order = ASRUtils::determine_module_dependencies(x); for (auto &item : build_order) { - std::cerr<<"line 641-- "<get_scope().find(item) != x.m_global_scope->get_scope().end()); if (startswith(item, "lfortran_intrinsic")) { diff --git a/src/libasr/pass/unique_symbols.cpp b/src/libasr/pass/unique_symbols.cpp index 81c974e69f..43ede2164a 100644 --- a/src/libasr/pass/unique_symbols.cpp +++ b/src/libasr/pass/unique_symbols.cpp @@ -6,6 +6,7 @@ #include #include #include +#include std::string lcompilers_unique_ID; @@ -14,13 +15,6 @@ namespace LCompilers { using ASR::down_cast; -std::string update_name(std::string curr_name, std::string prefix="") { - if (startswith(curr_name, "_lpython") || startswith(curr_name, "_lfortran") ) { - return curr_name; - } - return prefix + curr_name + lcompilers_unique_ID; -} - class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { private: @@ -28,12 +22,22 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { public: std::map sym_to_new_name; - std::map tmp_current_syms; std::string mod_name = ""; + std::set skip_list; UniqueSymbolVisitor(Allocator& al_) : al(al_){} + std::string update_name(std::string curr_name, std::string prefix="") { + if (startswith(curr_name, "_lpython") || startswith(curr_name, "_lfortran") ) { + return curr_name; + } + if (skip_list.find(curr_name) != skip_list.end()) { + return curr_name; + } + return prefix + curr_name + lcompilers_unique_ID; + } + void visit_TranslationUnit(const ASR::TranslationUnit_t &x) { ASR::TranslationUnit_t& xx = const_cast(x); std::map tmp_scope; @@ -100,6 +104,10 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { } void visit_Function(const ASR::Function_t &x) { + ASR::FunctionType_t *f_type = ASRUtils::get_FunctionType(x); + if (f_type->m_abi == ASR::abiType::BindC) { + skip_list.insert(x.m_name); + } ASR::Function_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { From 7d53c5057d7ad03bbd1bba0f16ffaed8161f025f Mon Sep 17 00:00:00 2001 From: Smit-create Date: Tue, 11 Jul 2023 17:51:24 +0530 Subject: [PATCH 4/9] fix --- src/libasr/pass/unique_symbols.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/libasr/pass/unique_symbols.cpp b/src/libasr/pass/unique_symbols.cpp index 43ede2164a..3bd12d2d1f 100644 --- a/src/libasr/pass/unique_symbols.cpp +++ b/src/libasr/pass/unique_symbols.cpp @@ -153,15 +153,19 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { void visit_ExternalSymbol(const ASR::ExternalSymbol_t &x) { ASR::ExternalSymbol_t& xx = const_cast(x); - ASR::Module_t *m = ASRUtils::get_sym_module(x.m_external); - ASR::symbol_t *m_sym = ASR::down_cast((ASR::asr_t*)m); - if (sym_to_new_name.find(m_sym) == sym_to_new_name.end()) { - visit_symbol(*m_sym); - } ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { return; } + ASR::Module_t *m = ASRUtils::get_sym_module(x.m_external); + if (m) { + ASR::symbol_t *m_sym = ASR::down_cast((ASR::asr_t*)m); + if (sym_to_new_name.find(m_sym) == sym_to_new_name.end()) { + visit_symbol(*m_sym); + } + } else { + this->visit_symbol(*x.m_external); + } xx.m_name = s2c(al, update_name(xx.m_name)); sym_to_new_name[sym] = xx.m_name; xx.m_original_name = s2c(al, update_name(xx.m_original_name)); @@ -246,6 +250,9 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { void visit_Variable(const ASR::Variable_t &x) { ASR::Variable_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { + return; + } xx.m_name = s2c(al, update_name(xx.m_name)); sym_to_new_name[sym] = xx.m_name; for (size_t i=0; i Date: Tue, 11 Jul 2023 22:50:46 +0530 Subject: [PATCH 5/9] Use two classes --- src/libasr/pass/unique_symbols.cpp | 558 ++++++++++++++++++++--------- 1 file changed, 384 insertions(+), 174 deletions(-) diff --git a/src/libasr/pass/unique_symbols.cpp b/src/libasr/pass/unique_symbols.cpp index 3bd12d2d1f..777fdae4a3 100644 --- a/src/libasr/pass/unique_symbols.cpp +++ b/src/libasr/pass/unique_symbols.cpp @@ -15,248 +15,452 @@ namespace LCompilers { using ASR::down_cast; -class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { +class SymbolRenameVisitor: public ASR::BaseWalkVisitor { private: Allocator& al; public: - std::map sym_to_new_name; - std::string mod_name = ""; - std::set skip_list; + std::map sym_to_renamed; + + SymbolRenameVisitor(Allocator& al_) : al(al_){} + + + void visit_TranslationUnit(const ASR::TranslationUnit_t &x) { + ASR::TranslationUnit_t& xx = const_cast(x); + std::map tmp_scope; + for (auto &a : xx.m_global_scope->get_scope()) { + visit_symbol(*a.second); + } + } + + void visit_Program(const ASR::Program_t &x) { + ASR::Program_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = xx.m_name; + for (auto &a : x.m_symtab->get_scope()) { + visit_symbol(*a.second); + } + } + + void visit_Module(const ASR::Module_t &x) { + ASR::Module_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = xx.m_name; + for (auto &a : x.m_symtab->get_scope()) { + visit_symbol(*a.second); + } + } + + void visit_Function(const ASR::Function_t &x) { + ASR::FunctionType_t *f_type = ASRUtils::get_FunctionType(x); + if (f_type->m_abi != ASR::abiType::BindC) { + ASR::Function_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = xx.m_name; + } + for (auto &a : x.m_symtab->get_scope()) { + visit_symbol(*a.second); + } + } + + void visit_GenericProcedure(const ASR::GenericProcedure_t &x) { + ASR::GenericProcedure_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = xx.m_name; + } - UniqueSymbolVisitor(Allocator& al_) : al(al_){} + void visit_CustomOperator(const ASR::CustomOperator_t &x) { + ASR::CustomOperator_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = xx.m_name; + } + void visit_ExternalSymbol(const ASR::ExternalSymbol_t &x) { + ASR::ExternalSymbol_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = xx.m_name; + } - std::string update_name(std::string curr_name, std::string prefix="") { - if (startswith(curr_name, "_lpython") || startswith(curr_name, "_lfortran") ) { - return curr_name; + void visit_StructType(const ASR::StructType_t &x) { + if (x.m_abi != ASR::abiType::BindC) { + ASR::StructType_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = xx.m_name; } - if (skip_list.find(curr_name) != skip_list.end()) { - return curr_name; + for (auto &a : x.m_symtab->get_scope()) { + this->visit_symbol(*a.second); + } + } + + void visit_EnumType(const ASR::EnumType_t &x) { + if (x.m_abi != ASR::abiType::BindC) { + ASR::EnumType_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = xx.m_name; + } + for (auto &a : x.m_symtab->get_scope()) { + this->visit_symbol(*a.second); + } + } + + void visit_UnionType(const ASR::UnionType_t &x) { + if (x.m_abi != ASR::abiType::BindC) { + ASR::UnionType_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = xx.m_name; + } + for (auto &a : x.m_symtab->get_scope()) { + this->visit_symbol(*a.second); + } + } + + void visit_Variable(const ASR::Variable_t &x) { + ASR::Variable_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = xx.m_name; + } + + void visit_ClassType(const ASR::ClassType_t &x) { + if (x.m_abi != ASR::abiType::BindC) { + ASR::ClassType_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = xx.m_name; + } + for (auto &a : x.m_symtab->get_scope()) { + this->visit_symbol(*a.second); + } + } + + void visit_ClassProcedure(const ASR::ClassProcedure_t &x) { + if (x.m_abi != ASR::abiType::BindC) { + ASR::ClassProcedure_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = xx.m_name; + } + } + + void visit_AssociateBlock(const ASR::AssociateBlock_t &x) { + ASR::AssociateBlock_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = xx.m_name; + for (auto &a : x.m_symtab->get_scope()) { + this->visit_symbol(*a.second); + } + } + + void visit_Block(const ASR::Block_t &x) { + ASR::Block_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = xx.m_name; + std::map tmp_scope; + for (auto &a : x.m_symtab->get_scope()) { + this->visit_symbol(*a.second); } - return prefix + curr_name + lcompilers_unique_ID; } + void visit_Requirement(const ASR::Requirement_t &x) { + ASR::Requirement_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = xx.m_name; + std::map tmp_scope; + for (auto &a : x.m_symtab->get_scope()) { + this->visit_symbol(*a.second); + } + } + + void visit_Template(const ASR::Template_t &x) { + ASR::Template_t& xx = const_cast(x); + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = xx.m_name; + std::map tmp_scope; + for (auto &a : x.m_symtab->get_scope()) { + this->visit_symbol(*a.second); + } + } + +}; + +std::string update_name(std::string curr_name, std::string prefix="") { + if (startswith(curr_name, "_lpython") || startswith(curr_name, "_lfortran") ) { + return curr_name; + } + return prefix + curr_name + lcompilers_unique_ID; +} + +void symbol_name_mangling(std::map &sym_to_renamed) { + for (auto &it: sym_to_renamed) { + it.second = update_name(it.second); + } +} + +class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { + private: + + Allocator& al; + + public: + std::map sym_to_new_name; + std::string mod_name = ""; + std::map current_scope; + + UniqueSymbolVisitor(Allocator& al_, + std::map sn) : al(al_), sym_to_new_name(sn){} + + void visit_TranslationUnit(const ASR::TranslationUnit_t &x) { ASR::TranslationUnit_t& xx = const_cast(x); - std::map tmp_scope; + std::map current_scope_copy = current_scope; + current_scope = x.m_global_scope->get_scope(); for (auto &a : xx.m_global_scope->get_scope()) { - tmp_scope[a.second] = a.first; - if (sym_to_new_name.find(a.second) == sym_to_new_name.end()) { - visit_symbol(*a.second); - } + visit_symbol(*a.second); } - for (auto &a: tmp_scope) { - xx.m_global_scope->erase_symbol(a.second); - xx.m_global_scope->add_symbol(sym_to_new_name[a.first], a.first); + for (auto &a: current_scope) { + if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { + xx.m_global_scope->erase_symbol(a.first); + xx.m_global_scope->add_symbol(sym_to_new_name[a.second], a.second); + } } + current_scope = current_scope_copy; } void visit_Program(const ASR::Program_t &x) { ASR::Program_t& xx = const_cast(x); + std::map current_scope_copy = current_scope; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - return; + xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map tmp_scope; - xx.m_name = s2c(al, update_name(xx.m_name)); - sym_to_new_name[sym] = xx.m_name; for (size_t i=0; iget_scope()) { - tmp_scope[a.second] = a.first; - if (sym_to_new_name.find(a.second) == sym_to_new_name.end()) { + current_scope = x.m_symtab->get_scope(); + for (auto &a : x.m_symtab->get_scope()) { visit_symbol(*a.second); - } } - for (auto &a: tmp_scope) { - xx.m_symtab->erase_symbol(a.second); - xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); + for (auto &a: current_scope) { + if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { + xx.m_symtab->erase_symbol(a.first); + xx.m_symtab->add_symbol(sym_to_new_name[a.second], a.second); + } } + current_scope = current_scope_copy; } void visit_Module(const ASR::Module_t &x) { ASR::Module_t& xx = const_cast(x); + std::map current_scope_copy = current_scope; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - return; + xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map tmp_scope; - xx.m_name = s2c(al, update_name(xx.m_name)); - mod_name = xx.m_name; - sym_to_new_name[sym] = xx.m_name; for (size_t i=0; iget_scope()) { - tmp_scope[a.second] = a.first; - if (sym_to_new_name.find(a.second) == sym_to_new_name.end()) { + current_scope = x.m_symtab->get_scope(); + for (auto &a : x.m_symtab->get_scope()) { visit_symbol(*a.second); - } } - for (auto &a: tmp_scope) { - xx.m_symtab->erase_symbol(a.second); - xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); + for (auto &a: current_scope) { + if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { + xx.m_symtab->erase_symbol(a.first); + xx.m_symtab->add_symbol(sym_to_new_name[a.second], a.second); + } } - mod_name = ""; + current_scope = current_scope_copy; } void visit_Function(const ASR::Function_t &x) { - ASR::FunctionType_t *f_type = ASRUtils::get_FunctionType(x); - if (f_type->m_abi == ASR::abiType::BindC) { - skip_list.insert(x.m_name); - } ASR::Function_t& xx = const_cast(x); + std::map current_scope_copy = current_scope; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - return; + xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map tmp_scope; - xx.m_name = s2c(al, update_name(xx.m_name)); - sym_to_new_name[sym] = xx.m_name; for (size_t i=0; iget_scope()) { - tmp_scope[a.second] = a.first; - if (sym_to_new_name.find(a.second) == sym_to_new_name.end()) { + current_scope = x.m_symtab->get_scope(); + for (auto &a : x.m_symtab->get_scope()) { visit_symbol(*a.second); - } } - for (auto &a: tmp_scope) { - xx.m_symtab->erase_symbol(a.second); - xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); + for (auto &a: current_scope) { + if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { + xx.m_symtab->erase_symbol(a.first); + xx.m_symtab->add_symbol(sym_to_new_name[a.second], a.second); + } } + current_scope = current_scope_copy; } void visit_GenericProcedure(const ASR::GenericProcedure_t &x) { ASR::GenericProcedure_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - return; + xx.m_name = s2c(al, sym_to_new_name[sym]); } - xx.m_name = s2c(al, update_name(xx.m_name)); - sym_to_new_name[sym] = xx.m_name; } void visit_CustomOperator(const ASR::CustomOperator_t &x) { ASR::CustomOperator_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - return; + xx.m_name = s2c(al, sym_to_new_name[sym]); } - xx.m_name = s2c(al, update_name(xx.m_name)); - sym_to_new_name[sym] = xx.m_name; } void visit_ExternalSymbol(const ASR::ExternalSymbol_t &x) { ASR::ExternalSymbol_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - return; + xx.m_name = s2c(al, sym_to_new_name[sym]); } - ASR::Module_t *m = ASRUtils::get_sym_module(x.m_external); - if (m) { - ASR::symbol_t *m_sym = ASR::down_cast((ASR::asr_t*)m); - if (sym_to_new_name.find(m_sym) == sym_to_new_name.end()) { - visit_symbol(*m_sym); - } - } else { - this->visit_symbol(*x.m_external); + SymbolTable* s = ASRUtils::symbol_parent_symtab(x.m_external); + ASR::symbol_t *asr_owner = ASR::down_cast(s->asr_owner); + if (sym_to_new_name.find(x.m_external) != sym_to_new_name.end()) { + xx.m_original_name = s2c(al, sym_to_new_name[x.m_external]); + } + if (sym_to_new_name.find(asr_owner) != sym_to_new_name.end()) { + xx.m_module_name = s2c(al, sym_to_new_name[asr_owner]); } - xx.m_name = s2c(al, update_name(xx.m_name)); - sym_to_new_name[sym] = xx.m_name; - xx.m_original_name = s2c(al, update_name(xx.m_original_name)); - xx.m_module_name = s2c(al, update_name(xx.m_module_name)); } void visit_StructType(const ASR::StructType_t &x) { ASR::StructType_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - return; + xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map tmp_scope; - xx.m_name = s2c(al, update_name(xx.m_name)); - sym_to_new_name[sym] = xx.m_name; + std::map current_scope_copy = current_scope; for (size_t i=0; iget_scope(); for (size_t i=0; iget_scope()) { - tmp_scope[a.second] = a.first; - this->visit_symbol(*a.second); + for (auto &a : x.m_symtab->get_scope()) { + visit_symbol(*a.second); } - for (auto &a: tmp_scope) { - xx.m_symtab->erase_symbol(a.second); - xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); + for (auto &a: current_scope) { + if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { + xx.m_symtab->erase_symbol(a.first); + xx.m_symtab->add_symbol(sym_to_new_name[a.second], a.second); + } } + current_scope = current_scope_copy; } void visit_EnumType(const ASR::EnumType_t &x) { ASR::EnumType_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - return; + xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map tmp_scope; - xx.m_name = s2c(al, update_name(xx.m_name)); - sym_to_new_name[sym] = xx.m_name; + std::map current_scope_copy = current_scope; for (size_t i=0; iget_scope(); for (size_t i=0; iget_scope()) { - tmp_scope[a.second] = a.first; - this->visit_symbol(*a.second); + for (auto &a : x.m_symtab->get_scope()) { + visit_symbol(*a.second); } - for (auto &a: tmp_scope) { - xx.m_symtab->erase_symbol(a.second); - xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); + for (auto &a: current_scope) { + if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { + xx.m_symtab->erase_symbol(a.first); + xx.m_symtab->add_symbol(sym_to_new_name[a.second], a.second); + } } + current_scope = current_scope_copy; } void visit_UnionType(const ASR::UnionType_t &x) { ASR::UnionType_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - return; + xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map tmp_scope; - xx.m_name = s2c(al, update_name(xx.m_name)); - sym_to_new_name[sym] = xx.m_name; + std::map current_scope_copy = current_scope; for (size_t i=0; iget_scope(); for (size_t i=0; iget_scope()) { - tmp_scope[a.second] = a.first; - this->visit_symbol(*a.second); + for (auto &a : x.m_symtab->get_scope()) { + visit_symbol(*a.second); } - for (auto &a: tmp_scope) { - xx.m_symtab->erase_symbol(a.second); - xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); + for (auto &a: current_scope) { + if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { + xx.m_symtab->erase_symbol(a.first); + xx.m_symtab->add_symbol(sym_to_new_name[a.second], a.second); + } } + current_scope = current_scope_copy; } void visit_Variable(const ASR::Variable_t &x) { ASR::Variable_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - return; + xx.m_name = s2c(al, sym_to_new_name[sym]); } - xx.m_name = s2c(al, update_name(xx.m_name)); - sym_to_new_name[sym] = xx.m_name; for (size_t i=0; i { ASR::ClassType_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - return; + xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map tmp_scope; - xx.m_name = s2c(al, update_name(xx.m_name)); - sym_to_new_name[sym] = xx.m_name; - for (auto &a : xx.m_symtab->get_scope()) { - tmp_scope[a.second] = a.first; - this->visit_symbol(*a.second); + std::map current_scope_copy = current_scope; + current_scope = x.m_symtab->get_scope(); + for (auto &a : x.m_symtab->get_scope()) { + visit_symbol(*a.second); } - for (auto &a: tmp_scope) { - xx.m_symtab->erase_symbol(a.second); - xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); + for (auto &a: current_scope) { + if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { + xx.m_symtab->erase_symbol(a.first); + xx.m_symtab->add_symbol(sym_to_new_name[a.second], a.second); + } } + current_scope = current_scope_copy; } void visit_ClassProcedure(const ASR::ClassProcedure_t &x) { ASR::ClassProcedure_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - return; + xx.m_name = s2c(al, sym_to_new_name[sym]); } - xx.m_name = s2c(al, update_name(xx.m_name)); - sym_to_new_name[sym] = xx.m_name; } void visit_AssociateBlock(const ASR::AssociateBlock_t &x) { ASR::AssociateBlock_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - return; + xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map tmp_scope; - xx.m_name = s2c(al, update_name(xx.m_name)); - sym_to_new_name[sym] = xx.m_name; - for (auto &a : xx.m_symtab->get_scope()) { - tmp_scope[a.second] = a.first; - this->visit_symbol(*a.second); + std::map current_scope_copy = current_scope; + current_scope = x.m_symtab->get_scope(); + for (auto &a : x.m_symtab->get_scope()) { + visit_symbol(*a.second); } - for (auto &a: tmp_scope) { - xx.m_symtab->erase_symbol(a.second); - xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); + for (auto &a: current_scope) { + if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { + xx.m_symtab->erase_symbol(a.first); + xx.m_symtab->add_symbol(sym_to_new_name[a.second], a.second); + } } + current_scope = current_scope_copy; } void visit_Block(const ASR::Block_t &x) { ASR::Block_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - return; + xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map tmp_scope; - xx.m_name = s2c(al, update_name(xx.m_name)); - sym_to_new_name[sym] = xx.m_name; - for (auto &a : xx.m_symtab->get_scope()) { - tmp_scope[a.second] = a.first; - this->visit_symbol(*a.second); + std::map current_scope_copy = current_scope; + current_scope = x.m_symtab->get_scope(); + for (auto &a : x.m_symtab->get_scope()) { + visit_symbol(*a.second); } - for (auto &a: tmp_scope) { - xx.m_symtab->erase_symbol(a.second); - xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); + for (auto &a: current_scope) { + if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { + xx.m_symtab->erase_symbol(a.first); + xx.m_symtab->add_symbol(sym_to_new_name[a.second], a.second); + } } + current_scope = current_scope_copy; } void visit_Requirement(const ASR::Requirement_t &x) { ASR::Requirement_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - return; + xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map tmp_scope; - xx.m_name = s2c(al, update_name(xx.m_name)); - sym_to_new_name[sym] = xx.m_name; - for (auto &a : xx.m_symtab->get_scope()) { - tmp_scope[a.second] = a.first; - this->visit_symbol(*a.second); + std::map current_scope_copy = current_scope; + current_scope = x.m_symtab->get_scope(); + for (auto &a : x.m_symtab->get_scope()) { + visit_symbol(*a.second); } - for (auto &a: tmp_scope) { - xx.m_symtab->erase_symbol(a.second); - xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); + for (auto &a: current_scope) { + if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { + xx.m_symtab->erase_symbol(a.first); + xx.m_symtab->add_symbol(sym_to_new_name[a.second], a.second); + } } + current_scope = current_scope_copy; } void visit_Template(const ASR::Template_t &x) { ASR::Template_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { - return; + xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map tmp_scope; - xx.m_name = s2c(al, update_name(xx.m_name)); - sym_to_new_name[sym] = xx.m_name; - for (auto &a : xx.m_symtab->get_scope()) { - tmp_scope[a.second] = a.first; - this->visit_symbol(*a.second); + std::map current_scope_copy = current_scope; + current_scope = x.m_symtab->get_scope(); + for (auto &a : x.m_symtab->get_scope()) { + visit_symbol(*a.second); } - for (auto &a: tmp_scope) { - xx.m_symtab->erase_symbol(a.second); - xx.m_symtab->add_symbol(sym_to_new_name[a.first], a.first); + for (auto &a: current_scope) { + if (sym_to_new_name.find(a.second) != sym_to_new_name.end()) { + xx.m_symtab->erase_symbol(a.first); + xx.m_symtab->add_symbol(sym_to_new_name[a.second], a.second); + } } + current_scope = current_scope_copy; } }; @@ -370,8 +577,11 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { void pass_unique_symbols(Allocator &al, ASR::TranslationUnit_t &unit, const LCompilers::PassOptions& /*pass_options*/) { - UniqueSymbolVisitor v(al); + SymbolRenameVisitor v(al); v.visit_TranslationUnit(unit); + symbol_name_mangling(v.sym_to_renamed); + UniqueSymbolVisitor u(al, v.sym_to_renamed); + u.visit_TranslationUnit(unit); PassUtils::UpdateDependenciesVisitor x(al); x.visit_TranslationUnit(unit); } From bae8daebffa193953a79a7dd9c94c61d2220b6ab Mon Sep 17 00:00:00 2001 From: Smit-create Date: Tue, 11 Jul 2023 22:50:54 +0530 Subject: [PATCH 6/9] Comment one failing test --- 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 9a2c9e5110..665c154fcf 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -589,7 +589,7 @@ RUN(NAME structs_13 LABELS llvm c RUN(NAME structs_14 LABELS cpython llvm c) RUN(NAME structs_15 LABELS cpython llvm c) RUN(NAME structs_16 LABELS cpython llvm c) -RUN(NAME structs_17 LABELS cpython llvm c) +#RUN(NAME structs_17 LABELS cpython llvm c) RUN(NAME structs_18 LABELS cpython llvm c EXTRAFILES structs_18b.c) RUN(NAME structs_19 LABELS cpython llvm c From 91a16bf38df7c4eaad5ad7ad2bb1d1c27a30fbd0 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Thu, 13 Jul 2023 11:58:55 +0530 Subject: [PATCH 7/9] Add configurable options --- src/libasr/pass/unique_symbols.cpp | 150 ++++++++++++++++------------- 1 file changed, 81 insertions(+), 69 deletions(-) diff --git a/src/libasr/pass/unique_symbols.cpp b/src/libasr/pass/unique_symbols.cpp index 777fdae4a3..f6560b2d16 100644 --- a/src/libasr/pass/unique_symbols.cpp +++ b/src/libasr/pass/unique_symbols.cpp @@ -16,16 +16,27 @@ namespace LCompilers { using ASR::down_cast; class SymbolRenameVisitor: public ASR::BaseWalkVisitor { - private: - - Allocator& al; - public: std::map sym_to_renamed; + bool module_name_mangling; + bool global_symbols_mangling; + bool intrinsic_symbols_mangling; + bool all_symbols_mangling; + bool should_mangle = false; - SymbolRenameVisitor(Allocator& al_) : al(al_){} + SymbolRenameVisitor( + bool mm, bool gm, bool im, bool am) : module_name_mangling(mm), + global_symbols_mangling(gm), intrinsic_symbols_mangling(im), + all_symbols_mangling(am){} + std::string update_name(std::string curr_name) { + if (startswith(curr_name, "_lpython") || startswith(curr_name, "_lfortran") ) { + return curr_name; + } + return curr_name + lcompilers_unique_ID; + } + void visit_TranslationUnit(const ASR::TranslationUnit_t &x) { ASR::TranslationUnit_t& xx = const_cast(x); std::map tmp_scope; @@ -35,29 +46,34 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { } void visit_Program(const ASR::Program_t &x) { - ASR::Program_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = xx.m_name; for (auto &a : x.m_symtab->get_scope()) { visit_symbol(*a.second); } } void visit_Module(const ASR::Module_t &x) { - ASR::Module_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = xx.m_name; + bool should_mangle_copy = should_mangle; + if (all_symbols_mangling || module_name_mangling || should_mangle) { + sym_to_renamed[sym] = update_name(x.m_name); + } + if ((x.m_intrinsic && intrinsic_symbols_mangling) || + (global_symbols_mangling && startswith(x.m_name, "_global_symbols"))) { + should_mangle = true; + } for (auto &a : x.m_symtab->get_scope()) { visit_symbol(*a.second); } + should_mangle = should_mangle_copy; } void visit_Function(const ASR::Function_t &x) { ASR::FunctionType_t *f_type = ASRUtils::get_FunctionType(x); if (f_type->m_abi != ASR::abiType::BindC) { - ASR::Function_t& xx = const_cast(x); ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = xx.m_name; + if (all_symbols_mangling || should_mangle) { + sym_to_renamed[sym] = update_name(x.m_name); + } } for (auto &a : x.m_symtab->get_scope()) { visit_symbol(*a.second); @@ -65,28 +81,32 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { } void visit_GenericProcedure(const ASR::GenericProcedure_t &x) { - ASR::GenericProcedure_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = xx.m_name; + if (all_symbols_mangling || should_mangle) { + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = update_name(x.m_name); + } } void visit_CustomOperator(const ASR::CustomOperator_t &x) { - ASR::CustomOperator_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = xx.m_name; + if (all_symbols_mangling || should_mangle) { + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = update_name(x.m_name); + } } void visit_ExternalSymbol(const ASR::ExternalSymbol_t &x) { - ASR::ExternalSymbol_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = xx.m_name; + if (all_symbols_mangling || should_mangle) { + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = update_name(x.m_name); + } } void visit_StructType(const ASR::StructType_t &x) { if (x.m_abi != ASR::abiType::BindC) { - ASR::StructType_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = xx.m_name; + if (all_symbols_mangling || should_mangle) { + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = update_name(x.m_name); + } } for (auto &a : x.m_symtab->get_scope()) { this->visit_symbol(*a.second); @@ -95,9 +115,10 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { void visit_EnumType(const ASR::EnumType_t &x) { if (x.m_abi != ASR::abiType::BindC) { - ASR::EnumType_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = xx.m_name; + if (all_symbols_mangling || should_mangle) { + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = update_name(x.m_name); + } } for (auto &a : x.m_symtab->get_scope()) { this->visit_symbol(*a.second); @@ -106,9 +127,10 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { void visit_UnionType(const ASR::UnionType_t &x) { if (x.m_abi != ASR::abiType::BindC) { - ASR::UnionType_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = xx.m_name; + if (all_symbols_mangling || should_mangle) { + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = update_name(x.m_name); + } } for (auto &a : x.m_symtab->get_scope()) { this->visit_symbol(*a.second); @@ -116,16 +138,18 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { } void visit_Variable(const ASR::Variable_t &x) { - ASR::Variable_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = xx.m_name; + if (all_symbols_mangling || should_mangle) { + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = update_name(x.m_name); + } } void visit_ClassType(const ASR::ClassType_t &x) { if (x.m_abi != ASR::abiType::BindC) { - ASR::ClassType_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = xx.m_name; + if (all_symbols_mangling || should_mangle) { + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = update_name(x.m_name); + } } for (auto &a : x.m_symtab->get_scope()) { this->visit_symbol(*a.second); @@ -134,46 +158,48 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { void visit_ClassProcedure(const ASR::ClassProcedure_t &x) { if (x.m_abi != ASR::abiType::BindC) { - ASR::ClassProcedure_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = xx.m_name; + if (all_symbols_mangling || should_mangle) { + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = update_name(x.m_name); + } } } void visit_AssociateBlock(const ASR::AssociateBlock_t &x) { - ASR::AssociateBlock_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = xx.m_name; + if (all_symbols_mangling || should_mangle) { + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = update_name(x.m_name); + } for (auto &a : x.m_symtab->get_scope()) { this->visit_symbol(*a.second); } } void visit_Block(const ASR::Block_t &x) { - ASR::Block_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = xx.m_name; - std::map tmp_scope; + if (all_symbols_mangling || should_mangle) { + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = update_name(x.m_name); + } for (auto &a : x.m_symtab->get_scope()) { this->visit_symbol(*a.second); } } void visit_Requirement(const ASR::Requirement_t &x) { - ASR::Requirement_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = xx.m_name; - std::map tmp_scope; + if (all_symbols_mangling || should_mangle) { + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = update_name(x.m_name); + } for (auto &a : x.m_symtab->get_scope()) { this->visit_symbol(*a.second); } } void visit_Template(const ASR::Template_t &x) { - ASR::Template_t& xx = const_cast(x); - ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); - sym_to_renamed[sym] = xx.m_name; - std::map tmp_scope; + if (all_symbols_mangling || should_mangle) { + ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); + sym_to_renamed[sym] = update_name(x.m_name); + } for (auto &a : x.m_symtab->get_scope()) { this->visit_symbol(*a.second); } @@ -181,18 +207,6 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { }; -std::string update_name(std::string curr_name, std::string prefix="") { - if (startswith(curr_name, "_lpython") || startswith(curr_name, "_lfortran") ) { - return curr_name; - } - return prefix + curr_name + lcompilers_unique_ID; -} - -void symbol_name_mangling(std::map &sym_to_renamed) { - for (auto &it: sym_to_renamed) { - it.second = update_name(it.second); - } -} class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { private: @@ -201,7 +215,6 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { public: std::map sym_to_new_name; - std::string mod_name = ""; std::map current_scope; UniqueSymbolVisitor(Allocator& al_, @@ -577,9 +590,8 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { void pass_unique_symbols(Allocator &al, ASR::TranslationUnit_t &unit, const LCompilers::PassOptions& /*pass_options*/) { - SymbolRenameVisitor v(al); + SymbolRenameVisitor v(true, true, true, true); v.visit_TranslationUnit(unit); - symbol_name_mangling(v.sym_to_renamed); UniqueSymbolVisitor u(al, v.sym_to_renamed); u.visit_TranslationUnit(unit); PassUtils::UpdateDependenciesVisitor x(al); From aaeb641ddbf33d167687ed88213ab86e12c8176e Mon Sep 17 00:00:00 2001 From: Smit-create Date: Sat, 15 Jul 2023 10:35:41 +0530 Subject: [PATCH 8/9] Add docs and rebase with main --- src/bin/lpython.cpp | 2 +- src/libasr/asr_scopes.cpp | 1 + src/libasr/gen_pass.py | 1 + src/libasr/pass/unique_symbols.cpp | 55 +++++++++++++++++++----------- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/bin/lpython.cpp b/src/bin/lpython.cpp index 475b12dfcf..715b29eadd 100644 --- a/src/bin/lpython.cpp +++ b/src/bin/lpython.cpp @@ -1616,7 +1616,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 = LCompilers::get_unique_ID(); if( compiler_options.fast && compiler_options.enable_bounds_checking ) { diff --git a/src/libasr/asr_scopes.cpp b/src/libasr/asr_scopes.cpp index 72652fa9eb..5cf6b81eb8 100644 --- a/src/libasr/asr_scopes.cpp +++ b/src/libasr/asr_scopes.cpp @@ -5,6 +5,7 @@ #include +std::string lcompilers_unique_ID; namespace LCompilers { template< typename T > diff --git a/src/libasr/gen_pass.py b/src/libasr/gen_pass.py index 560ea4d71d..428565cde3 100644 --- a/src/libasr/gen_pass.py +++ b/src/libasr/gen_pass.py @@ -32,6 +32,7 @@ "unused_functions", "update_array_dim_intrinsic_calls", "replace_where", + "unique_symbols", ] diff --git a/src/libasr/pass/unique_symbols.cpp b/src/libasr/pass/unique_symbols.cpp index f6560b2d16..229f9e9f81 100644 --- a/src/libasr/pass/unique_symbols.cpp +++ b/src/libasr/pass/unique_symbols.cpp @@ -9,15 +9,32 @@ #include -std::string lcompilers_unique_ID; +extern std::string lcompilers_unique_ID; +/* +ASR pass for replacing symbol names with some new name, mostly because +we want to generate unique symbols for each generated ASR output +so that it doesn't give linking errors for the generated backend code like C. + +This is done using two classes: +1. SymbolRenameVisitor - This captures symbols to be renamed based on the options + provided: + 1.1: module_name_mangling - Mangles the module name. + 1.2: global_symbols_mangling - Mangles all the global symbols. + 1.3: intrinsic_symbols_mangling - Mangles all the intrinsic symbols. + 1.4: all_symbols_mangling - Mangles all possible symbols. + + Note: this skips BindC functions and symbols starting with `_lpython` or `_lfortran` + +2. UniqueSymbolVisitor: Renames all the captured symbols from SymbolRenameVisitor. +*/ namespace LCompilers { using ASR::down_cast; class SymbolRenameVisitor: public ASR::BaseWalkVisitor { public: - std::map sym_to_renamed; + std::unordered_map sym_to_renamed; bool module_name_mangling; bool global_symbols_mangling; bool intrinsic_symbols_mangling; @@ -34,12 +51,12 @@ class SymbolRenameVisitor: public ASR::BaseWalkVisitor { if (startswith(curr_name, "_lpython") || startswith(curr_name, "_lfortran") ) { return curr_name; } - return curr_name + lcompilers_unique_ID; + return curr_name + "_" + lcompilers_unique_ID; } void visit_TranslationUnit(const ASR::TranslationUnit_t &x) { ASR::TranslationUnit_t& xx = const_cast(x); - std::map tmp_scope; + std::unordered_map tmp_scope; for (auto &a : xx.m_global_scope->get_scope()) { visit_symbol(*a.second); } @@ -214,16 +231,16 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { Allocator& al; public: - std::map sym_to_new_name; - std::map current_scope; + std::unordered_map& sym_to_new_name; + std::unordered_map current_scope; UniqueSymbolVisitor(Allocator& al_, - std::map sn) : al(al_), sym_to_new_name(sn){} + std::unordered_map &sn) : al(al_), sym_to_new_name(sn){} void visit_TranslationUnit(const ASR::TranslationUnit_t &x) { ASR::TranslationUnit_t& xx = const_cast(x); - std::map current_scope_copy = current_scope; + std::unordered_map current_scope_copy = current_scope; current_scope = x.m_global_scope->get_scope(); for (auto &a : xx.m_global_scope->get_scope()) { visit_symbol(*a.second); @@ -239,7 +256,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { void visit_Program(const ASR::Program_t &x) { ASR::Program_t& xx = const_cast(x); - std::map current_scope_copy = current_scope; + std::unordered_map current_scope_copy = current_scope; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); @@ -267,7 +284,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { void visit_Module(const ASR::Module_t &x) { ASR::Module_t& xx = const_cast(x); - std::map current_scope_copy = current_scope; + std::unordered_map current_scope_copy = current_scope; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); @@ -295,7 +312,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { void visit_Function(const ASR::Function_t &x) { ASR::Function_t& xx = const_cast(x); - std::map current_scope_copy = current_scope; + std::unordered_map current_scope_copy = current_scope; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); @@ -359,7 +376,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map current_scope_copy = current_scope; + std::unordered_map current_scope_copy = current_scope; for (size_t i=0; i { if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map current_scope_copy = current_scope; + std::unordered_map current_scope_copy = current_scope; for (size_t i=0; i { if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map current_scope_copy = current_scope; + std::unordered_map current_scope_copy = current_scope; for (size_t i=0; i { if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map current_scope_copy = current_scope; + std::unordered_map current_scope_copy = current_scope; current_scope = x.m_symtab->get_scope(); for (auto &a : x.m_symtab->get_scope()) { visit_symbol(*a.second); @@ -511,7 +528,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map current_scope_copy = current_scope; + std::unordered_map current_scope_copy = current_scope; current_scope = x.m_symtab->get_scope(); for (auto &a : x.m_symtab->get_scope()) { visit_symbol(*a.second); @@ -531,7 +548,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map current_scope_copy = current_scope; + std::unordered_map current_scope_copy = current_scope; current_scope = x.m_symtab->get_scope(); for (auto &a : x.m_symtab->get_scope()) { visit_symbol(*a.second); @@ -551,7 +568,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map current_scope_copy = current_scope; + std::unordered_map current_scope_copy = current_scope; current_scope = x.m_symtab->get_scope(); for (auto &a : x.m_symtab->get_scope()) { visit_symbol(*a.second); @@ -571,7 +588,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::map current_scope_copy = current_scope; + std::unordered_map current_scope_copy = current_scope; current_scope = x.m_symtab->get_scope(); for (auto &a : x.m_symtab->get_scope()) { visit_symbol(*a.second); From bd2f8b85b01b3b333cd452078e2a240757f8ca59 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Sat, 15 Jul 2023 10:57:03 +0530 Subject: [PATCH 9/9] Add cmd line options --- integration_tests/CMakeLists.txt | 2 +- src/bin/lpython.cpp | 21 ++++++++++++++++- src/libasr/asr_scopes.cpp | 2 +- src/libasr/pass/unique_symbols.cpp | 38 ++++++++++++++++++------------ src/libasr/utils.h | 8 +++++++ 5 files changed, 53 insertions(+), 18 deletions(-) diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 665c154fcf..9a2c9e5110 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -589,7 +589,7 @@ RUN(NAME structs_13 LABELS llvm c RUN(NAME structs_14 LABELS cpython llvm c) RUN(NAME structs_15 LABELS cpython llvm c) RUN(NAME structs_16 LABELS cpython llvm c) -#RUN(NAME structs_17 LABELS cpython llvm c) +RUN(NAME structs_17 LABELS cpython llvm c) RUN(NAME structs_18 LABELS cpython llvm c EXTRAFILES structs_18b.c) RUN(NAME structs_19 LABELS cpython llvm c diff --git a/src/bin/lpython.cpp b/src/bin/lpython.cpp index 715b29eadd..bcdd839974 100644 --- a/src/bin/lpython.cpp +++ b/src/bin/lpython.cpp @@ -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); @@ -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); @@ -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); @@ -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; @@ -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"); @@ -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(): ""; if( compiler_options.fast && compiler_options.enable_bounds_checking ) { diff --git a/src/libasr/asr_scopes.cpp b/src/libasr/asr_scopes.cpp index 5cf6b81eb8..a47eba63d2 100644 --- a/src/libasr/asr_scopes.cpp +++ b/src/libasr/asr_scopes.cpp @@ -85,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; diff --git a/src/libasr/pass/unique_symbols.cpp b/src/libasr/pass/unique_symbols.cpp index 229f9e9f81..30578fb605 100644 --- a/src/libasr/pass/unique_symbols.cpp +++ b/src/libasr/pass/unique_symbols.cpp @@ -232,7 +232,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { public: std::unordered_map& sym_to_new_name; - std::unordered_map current_scope; + std::map current_scope; UniqueSymbolVisitor(Allocator& al_, std::unordered_map &sn) : al(al_), sym_to_new_name(sn){} @@ -240,7 +240,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { void visit_TranslationUnit(const ASR::TranslationUnit_t &x) { ASR::TranslationUnit_t& xx = const_cast(x); - std::unordered_map current_scope_copy = current_scope; + std::map current_scope_copy = current_scope; current_scope = x.m_global_scope->get_scope(); for (auto &a : xx.m_global_scope->get_scope()) { visit_symbol(*a.second); @@ -256,7 +256,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { void visit_Program(const ASR::Program_t &x) { ASR::Program_t& xx = const_cast(x); - std::unordered_map current_scope_copy = current_scope; + std::map current_scope_copy = current_scope; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); @@ -284,7 +284,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { void visit_Module(const ASR::Module_t &x) { ASR::Module_t& xx = const_cast(x); - std::unordered_map current_scope_copy = current_scope; + std::map current_scope_copy = current_scope; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); @@ -312,7 +312,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { void visit_Function(const ASR::Function_t &x) { ASR::Function_t& xx = const_cast(x); - std::unordered_map current_scope_copy = current_scope; + std::map current_scope_copy = current_scope; ASR::symbol_t *sym = ASR::down_cast((ASR::asr_t*)&x); if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); @@ -376,7 +376,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::unordered_map current_scope_copy = current_scope; + std::map current_scope_copy = current_scope; for (size_t i=0; i { if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::unordered_map current_scope_copy = current_scope; + std::map current_scope_copy = current_scope; for (size_t i=0; i { if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::unordered_map current_scope_copy = current_scope; + std::map current_scope_copy = current_scope; for (size_t i=0; i { if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::unordered_map current_scope_copy = current_scope; + std::map current_scope_copy = current_scope; current_scope = x.m_symtab->get_scope(); for (auto &a : x.m_symtab->get_scope()) { visit_symbol(*a.second); @@ -528,7 +528,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::unordered_map current_scope_copy = current_scope; + std::map current_scope_copy = current_scope; current_scope = x.m_symtab->get_scope(); for (auto &a : x.m_symtab->get_scope()) { visit_symbol(*a.second); @@ -548,7 +548,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::unordered_map current_scope_copy = current_scope; + std::map current_scope_copy = current_scope; current_scope = x.m_symtab->get_scope(); for (auto &a : x.m_symtab->get_scope()) { visit_symbol(*a.second); @@ -568,7 +568,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::unordered_map current_scope_copy = current_scope; + std::map current_scope_copy = current_scope; current_scope = x.m_symtab->get_scope(); for (auto &a : x.m_symtab->get_scope()) { visit_symbol(*a.second); @@ -588,7 +588,7 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { if (sym_to_new_name.find(sym) != sym_to_new_name.end()) { xx.m_name = s2c(al, sym_to_new_name[sym]); } - std::unordered_map current_scope_copy = current_scope; + std::map current_scope_copy = current_scope; current_scope = x.m_symtab->get_scope(); for (auto &a : x.m_symtab->get_scope()) { visit_symbol(*a.second); @@ -606,8 +606,16 @@ class UniqueSymbolVisitor: public ASR::BaseWalkVisitor { void pass_unique_symbols(Allocator &al, ASR::TranslationUnit_t &unit, - const LCompilers::PassOptions& /*pass_options*/) { - SymbolRenameVisitor v(true, true, true, true); + const LCompilers::PassOptions& pass_options) { + bool any_present = (pass_options.module_name_mangling || pass_options.global_symbols_mangling || + pass_options.intrinsic_symbols_mangling || pass_options.all_symbols_mangling); + if (!any_present || lcompilers_unique_ID.empty()) { + return; + } + SymbolRenameVisitor v(pass_options.module_name_mangling, + pass_options.global_symbols_mangling, + pass_options.intrinsic_symbols_mangling, + pass_options.all_symbols_mangling); v.visit_TranslationUnit(unit); UniqueSymbolVisitor u(al, v.sym_to_renamed); u.visit_TranslationUnit(unit); diff --git a/src/libasr/utils.h b/src/libasr/utils.h index 39f3422f05..768d689fa8 100644 --- a/src/libasr/utils.h +++ b/src/libasr/utils.h @@ -64,6 +64,10 @@ struct CompilerOptions { bool enable_cpython = false; bool enable_symengine = false; bool link_numpy = false; + bool module_name_mangling = false; + bool global_symbols_mangling = false; + bool intrinsic_symbols_mangling = false; + bool all_symbols_mangling = false; std::vector import_paths; Platform platform; @@ -93,6 +97,10 @@ namespace LCompilers { bool verbose = false; // For developer debugging bool pass_cumulative = false; // Apply passes cumulatively bool disable_main = false; + bool module_name_mangling = false; + bool global_symbols_mangling = false; + bool intrinsic_symbols_mangling = false; + bool all_symbols_mangling = false; }; }