Skip to content

Commit afe126e

Browse files
[ASRPass] Add visit Module in passes
1 parent 2d7523e commit afe126e

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/libasr/pass/array_op.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,36 @@ class ArrayOpVisitor : public ASR::CallReplacerOnExpressionsVisitor<ArrayOpVisit
831831
current_scope = current_scope_copy;
832832
}
833833

834+
void visit_Module(const ASR::Module_t &x) {
835+
// FIXME: this is a hack, we need to pass in a non-const `x`,
836+
// which requires to generate a TransformVisitor.
837+
ASR::Module_t &xx = const_cast<ASR::Module_t&>(x);
838+
current_scope = xx.m_symtab;
839+
for (auto &item : x.m_symtab->get_scope()) {
840+
if (is_a<ASR::Function_t>(*item.second)) {
841+
ASR::Function_t *s = ASR::down_cast<ASR::Function_t>(item.second);
842+
if (s->m_return_var) {
843+
/*
844+
* A function which returns an array will be converted
845+
* to a subroutine with the destination array as the last
846+
* argument. This helps in avoiding deep copies and the
847+
* destination memory directly gets filled inside the subroutine.
848+
*/
849+
if( PassUtils::is_array(s->m_return_var) ) {
850+
ASR::symbol_t* s_sub = create_subroutine_from_function(s);
851+
// Update the symtab with this function changes
852+
xx.m_symtab->add_symbol(item.first, s_sub);
853+
}
854+
}
855+
}
856+
}
857+
858+
// Now visit everything else
859+
for (auto &item : x.m_symtab->get_scope()) {
860+
this->visit_symbol(*item.second);
861+
}
862+
}
863+
834864
void visit_Program(const ASR::Program_t &x) {
835865
// FIXME: this is a hack, we need to pass in a non-const `x`,
836866
// which requires to generate a TransformVisitor.

src/libasr/pass/subroutine_from_function.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,36 @@ class CreateFunctionFromSubroutine: public PassUtils::PassVisitor<CreateFunction
9595
}
9696
}
9797

98+
void visit_Module(const ASR::Module_t &x) {
99+
// FIXME: this is a hack, we need to pass in a non-const `x`,
100+
// which requires to generate a TransformVisitor.
101+
ASR::Module_t &xx = const_cast<ASR::Module_t&>(x);
102+
current_scope = xx.m_symtab;
103+
for (auto &item : x.m_symtab->get_scope()) {
104+
if (is_a<ASR::Function_t>(*item.second)) {
105+
ASR::Function_t *s = ASR::down_cast<ASR::Function_t>(item.second);
106+
if (s->m_return_var) {
107+
/*
108+
* A function which returns an array will be converted
109+
* to a subroutine with the destination array as the last
110+
* argument. This helps in avoiding deep copies and the
111+
* destination memory directly gets filled inside the subroutine.
112+
*/
113+
if( PassUtils::is_aggregate_type(s->m_return_var) ) {
114+
ASR::symbol_t* s_sub = create_subroutine_from_function(s);
115+
// Update the symtab with this function changes
116+
xx.m_symtab->add_symbol(item.first, s_sub);
117+
}
118+
}
119+
}
120+
}
121+
122+
// Now visit everything else
123+
for (auto &item : x.m_symtab->get_scope()) {
124+
this->visit_symbol(*item.second);
125+
}
126+
}
127+
98128
void visit_Program(const ASR::Program_t &x) {
99129
std::vector<std::pair<std::string, ASR::symbol_t*> > replace_vec;
100130
// FIXME: this is a hack, we need to pass in a non-const `x`,

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6284,7 +6284,13 @@ Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al, LocationManager
62846284
pass_options.run_fun = "_lpython_main_program";
62856285
pass_options.runtime_library_dir = get_runtime_library_dir();
62866286
pass_wrap_global_stmts_into_program(al, *tu, pass_options);
6287-
LCOMPILERS_ASSERT(asr_verify(*tu, true, diagnostics));
6287+
#if defined(WITH_LFORTRAN_ASSERT)
6288+
diag::Diagnostics diagnostics;
6289+
if (!asr_verify(*tu, true, diagnostics)) {
6290+
std::cerr << diagnostics.render2();
6291+
throw LCompilersException("Verify failed");
6292+
};
6293+
#endif
62886294
}
62896295
}
62906296

0 commit comments

Comments
 (0)