Skip to content

Commit f0b2a49

Browse files
authored
Merge pull request #2412 from anutosh491/visit_return
Implemented visit_Return for freeing variables
2 parents b382eed + 7231fb0 commit f0b2a49

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

src/libasr/pass/replace_symbolic.cpp

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,29 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
8989
this->current_scope = current_scope_copy;
9090

9191
// freeing out variables
92-
std::string new_name = "basic_free_stack";
93-
ASR::symbol_t* basic_free_stack_sym = module_scope->get_symbol(new_name);
94-
Vec<ASR::stmt_t*> func_body;
95-
func_body.from_pointer_n_copy(al, xx.m_body, xx.n_body);
92+
if (!symbolic_vars_to_free.empty()) {
93+
std::string new_name = "basic_free_stack";
94+
ASR::symbol_t* basic_free_stack_sym = module_scope->get_symbol(new_name);
95+
Vec<ASR::stmt_t*> func_body;
96+
func_body.from_pointer_n_copy(al, xx.m_body, xx.n_body);
97+
98+
for (ASR::symbol_t* symbol : symbolic_vars_to_free) {
99+
if (symbolic_vars_to_omit.find(symbol) != symbolic_vars_to_omit.end()) continue;
100+
Vec<ASR::call_arg_t> call_args;
101+
call_args.reserve(al, 1);
102+
ASR::call_arg_t call_arg;
103+
call_arg.loc = xx.base.base.loc;
104+
call_arg.m_value = ASRUtils::EXPR(ASR::make_Var_t(al, xx.base.base.loc, symbol));
105+
call_args.push_back(al, call_arg);
106+
ASR::stmt_t* stmt = ASRUtils::STMT(ASR::make_SubroutineCall_t(al, xx.base.base.loc, basic_free_stack_sym,
107+
basic_free_stack_sym, call_args.p, call_args.n, nullptr));
108+
func_body.push_back(al, stmt);
109+
}
96110

97-
for (ASR::symbol_t* symbol : symbolic_vars_to_free) {
98-
if (symbolic_vars_to_omit.find(symbol) != symbolic_vars_to_omit.end()) continue;
99-
Vec<ASR::call_arg_t> call_args;
100-
call_args.reserve(al, 1);
101-
ASR::call_arg_t call_arg;
102-
call_arg.loc = xx.base.base.loc;
103-
call_arg.m_value = ASRUtils::EXPR(ASR::make_Var_t(al, xx.base.base.loc, symbol));
104-
call_args.push_back(al, call_arg);
105-
ASR::stmt_t* stmt = ASRUtils::STMT(ASR::make_SubroutineCall_t(al, xx.base.base.loc, basic_free_stack_sym,
106-
basic_free_stack_sym, call_args.p, call_args.n, nullptr));
107-
func_body.push_back(al, stmt);
111+
xx.n_body = func_body.size();
112+
xx.m_body = func_body.p;
113+
symbolic_vars_to_free.clear();
108114
}
109-
110-
xx.n_body = func_body.size();
111-
xx.m_body = func_body.p;
112-
symbolic_vars_to_free.clear();
113115
}
114116

115117
void visit_Variable(const ASR::Variable_t& x) {
@@ -121,7 +123,9 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
121123

122124
ASR::ttype_t *type1 = ASRUtils::TYPE(ASR::make_CPtr_t(al, xx.base.base.loc));
123125
xx.m_type = type1;
124-
symbolic_vars_to_free.insert(ASR::down_cast<ASR::symbol_t>((ASR::asr_t*)&xx));
126+
if (var_name != "_lpython_return_variable") {
127+
symbolic_vars_to_free.insert(ASR::down_cast<ASR::symbol_t>((ASR::asr_t*)&xx));
128+
}
125129
if(xx.m_intent == ASR::intentType::In){
126130
symbolic_vars_to_omit.insert(ASR::down_cast<ASR::symbol_t>((ASR::asr_t*)&xx));
127131
}
@@ -1762,6 +1766,30 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
17621766
}
17631767
}
17641768
}
1769+
1770+
void visit_Return(const ASR::Return_t &x) {
1771+
if (!symbolic_vars_to_free.empty()){
1772+
SymbolTable* module_scope = current_scope->parent;
1773+
// freeing out variables
1774+
std::string new_name = "basic_free_stack";
1775+
ASR::symbol_t* basic_free_stack_sym = module_scope->get_symbol(new_name);
1776+
1777+
for (ASR::symbol_t* symbol : symbolic_vars_to_free) {
1778+
if (symbolic_vars_to_omit.find(symbol) != symbolic_vars_to_omit.end()) continue;
1779+
Vec<ASR::call_arg_t> call_args;
1780+
call_args.reserve(al, 1);
1781+
ASR::call_arg_t call_arg;
1782+
call_arg.loc = x.base.base.loc;
1783+
call_arg.m_value = ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, symbol));
1784+
call_args.push_back(al, call_arg);
1785+
ASR::stmt_t* stmt = ASRUtils::STMT(ASR::make_SubroutineCall_t(al, x.base.base.loc, basic_free_stack_sym,
1786+
basic_free_stack_sym, call_args.p, call_args.n, nullptr));
1787+
pass_result.push_back(al, stmt);
1788+
}
1789+
symbolic_vars_to_free.clear();
1790+
pass_result.push_back(al, ASRUtils::STMT(ASR::make_Return_t(al, x.base.base.loc)));
1791+
}
1792+
}
17651793
};
17661794

17671795
void pass_replace_symbolic(Allocator &al, ASR::TranslationUnit_t &unit,

0 commit comments

Comments
 (0)