@@ -89,27 +89,29 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
89
89
this ->current_scope = current_scope_copy;
90
90
91
91
// 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
+ }
96
110
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 ();
108
114
}
109
-
110
- xx.n_body = func_body.size ();
111
- xx.m_body = func_body.p ;
112
- symbolic_vars_to_free.clear ();
113
115
}
114
116
115
117
void visit_Variable (const ASR::Variable_t& x) {
@@ -121,7 +123,9 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
121
123
122
124
ASR::ttype_t *type1 = ASRUtils::TYPE (ASR::make_CPtr_t (al, xx.base .base .loc ));
123
125
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
+ }
125
129
if (xx.m_intent == ASR::intentType::In){
126
130
symbolic_vars_to_omit.insert (ASR::down_cast<ASR::symbol_t >((ASR::asr_t *)&xx));
127
131
}
@@ -1762,6 +1766,30 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
1762
1766
}
1763
1767
}
1764
1768
}
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
+ }
1765
1793
};
1766
1794
1767
1795
void pass_replace_symbolic (Allocator &al, ASR::TranslationUnit_t &unit,
0 commit comments