Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 55c145b

Browse files
authored
Merge pull request #2569 from anutosh491/Fixing_returning_variables
Fixing issues with freeing variables
2 parents ae1bcd5 + 9a07c85 commit 55c145b

File tree

4 files changed

+43
-17
lines changed

4 files changed

+43
-17
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ RUN(NAME test_gruntz LABELS cpython_sym c_sym llvm_sym NOFAST)
729729
RUN(NAME symbolics_15 LABELS c_sym llvm_sym NOFAST)
730730
RUN(NAME symbolics_16 LABELS cpython_sym c_sym llvm_sym NOFAST)
731731
RUN(NAME symbolics_17 LABELS cpython_sym c_sym llvm_sym NOFAST)
732+
RUN(NAME symbolics_18 LABELS cpython_sym c_sym llvm_sym NOFAST)
732733

733734
RUN(NAME sizeof_01 LABELS llvm c
734735
EXTRAFILES sizeof_01b.c)

integration_tests/symbolics_18.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from lpython import S
2+
from sympy import Symbol, log
3+
4+
def func_01(e: S, x: S) -> S:
5+
print(e)
6+
if e == x:
7+
return x
8+
print(e)
9+
return e
10+
11+
def test_func_01():
12+
x: S = Symbol("x")
13+
ans: S = func_01(log(x), x)
14+
print(ans)
15+
16+
def func_02(e: S, x: S) -> list[S]:
17+
print(e)
18+
if e == x:
19+
list1: list[S] = [x]
20+
return list1
21+
else:
22+
print(e)
23+
list2: list[S] = func_02(x, x)
24+
return list2
25+
26+
def test_func_02():
27+
x: S = Symbol("x")
28+
ans: list[S] = func_02(log(x), x)
29+
ele: S = ans[0]
30+
print(ele)
31+
32+
def tests():
33+
test_func_01()
34+
test_func_02()
35+
36+
tests()

src/libasr/pass/pass_utils.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -830,16 +830,6 @@ namespace LCompilers {
830830
s_func_type->m_arg_types = arg_types.p;
831831
s_func_type->n_arg_types = arg_types.n;
832832
s_func_type->m_return_var_type = nullptr;
833-
834-
if (ASR::is_a<ASR::Return_t>(*x->m_body[x->n_body - 1])) {
835-
Vec<ASR::stmt_t*> func_body;
836-
func_body.reserve(al, x->n_body - 1);
837-
for (size_t i=0; i< x->n_body - 1; i++) {
838-
func_body.push_back(al, x->m_body[i]);
839-
}
840-
x->m_body = func_body.p;
841-
x->n_body = func_body.n;
842-
}
843833
}
844834
}
845835
for (auto &item : x->m_symtab->get_scope()) {

src/libasr/pass/replace_symbolic.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,6 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
323323
func_body.from_pointer_n_copy(al, xx.m_body, xx.n_body);
324324

325325
for (ASR::symbol_t* symbol : symbolic_vars_to_free) {
326-
if (symbolic_vars_to_omit.find(symbol) != symbolic_vars_to_omit.end()) continue;
327326
func_body.push_back(al, basic_free_stack(x.base.base.loc,
328327
ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, symbol))));
329328
}
@@ -352,7 +351,7 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
352351

353352
ASR::ttype_t *CPtr_type = ASRUtils::TYPE(ASR::make_CPtr_t(al, xx.base.base.loc));
354353
xx.m_type = CPtr_type;
355-
if (var_name != "_lpython_return_variable" && xx.m_intent != ASR::intentType::Out) {
354+
if (xx.m_intent == ASR::intentType::Local) {
356355
symbolic_vars_to_free.insert(ASR::down_cast<ASR::symbol_t>((ASR::asr_t*)&xx));
357356
}
358357
if(xx.m_intent == ASR::intentType::In){
@@ -524,7 +523,8 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
524523
void visit_Assignment(const ASR::Assignment_t &x) {
525524
if (ASR::is_a<ASR::Var_t>(*x.m_value) && ASR::is_a<ASR::CPtr_t>(*ASRUtils::expr_type(x.m_value))) {
526525
ASR::symbol_t *v = ASR::down_cast<ASR::Var_t>(x.m_value)->m_v;
527-
if (symbolic_vars_to_free.find(v) == symbolic_vars_to_free.end()) return;
526+
if ((symbolic_vars_to_free.find(v) == symbolic_vars_to_free.end()) &&
527+
(symbolic_vars_to_omit.find(v) == symbolic_vars_to_omit.end())) return;
528528
ASR::symbol_t* var_sym = ASR::down_cast<ASR::Var_t>(x.m_value)->m_v;
529529
pass_result.push_back(al, basic_assign(x.base.base.loc, x.m_target,
530530
ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, var_sym))));
@@ -784,7 +784,8 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
784784
ASR::expr_t* val = x.m_values[i];
785785
if (ASR::is_a<ASR::Var_t>(*val) && ASR::is_a<ASR::CPtr_t>(*ASRUtils::expr_type(val))) {
786786
ASR::symbol_t *v = ASR::down_cast<ASR::Var_t>(val)->m_v;
787-
if (symbolic_vars_to_free.find(v) == symbolic_vars_to_free.end()) return;
787+
if ((symbolic_vars_to_free.find(v) == symbolic_vars_to_free.end()) &&
788+
(symbolic_vars_to_omit.find(v) == symbolic_vars_to_omit.end())) return;
788789
print_tmp.push_back(basic_str(x.base.base.loc, val));
789790
} else if (ASR::is_a<ASR::IntrinsicScalarFunction_t>(*val)) {
790791
ASR::IntrinsicScalarFunction_t* intrinsic_func = ASR::down_cast<ASR::IntrinsicScalarFunction_t>(val);
@@ -1007,14 +1008,12 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
10071008
}
10081009

10091010
void visit_Return(const ASR::Return_t &x) {
1011+
// freeing out variables
10101012
if (!symbolic_vars_to_free.empty()){
10111013
for (ASR::symbol_t* symbol : symbolic_vars_to_free) {
1012-
if (symbolic_vars_to_omit.find(symbol) != symbolic_vars_to_omit.end()) continue;
1013-
// freeing out variables
10141014
pass_result.push_back(al, basic_free_stack(x.base.base.loc,
10151015
ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, symbol))));
10161016
}
1017-
symbolic_vars_to_free.clear();
10181017
pass_result.push_back(al, ASRUtils::STMT(ASR::make_Return_t(al, x.base.base.loc)));
10191018
}
10201019
}

0 commit comments

Comments
 (0)