You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
from lpython import S
from sympy import Symbol, log, E, Pow, exp, pi
def mmrv(e: S, x: S) -> list[S]:
if e.args[0] != E:
e1: S = E * pi
else:
list4: list[S] = [x]
return list4
def test_mrv():
x: S = Symbol("x")
ans4: list[S] = mmrv(exp(log(x)), x)
print(ans4[0])
test_mrv()
def mmrv(e: S, x: S) -> list[S]:
if e.args[0] != E:
a: S = E
b: S = pi
e1: S = a * b
else:
list4: list[S] = [x]
return list4
def test_mrv():
x: S = Symbol("x")
ans4: list[S] = mmrv(exp(log(x)), x)
print(ans4[0])
test_mrv()
(lf) anutosh491@spbhat68:~/lpython/lpython$ lpython --enable-symengine --backend=c integration_tests/test_gruntz.py
x
(lf) anutosh491@spbhat68:~/lpython/lpython$ lpython --enable-symengine integration_tests/test_gruntz.py
x
This hints that it might be something related to declaring or freeing of variables.
from lpython import S
from sympy import Symbol, E
def mmrv(e: S) -> S:
if False:
print(E)
else:
return e
def test_mrv():
x: S = Symbol("x")
ans: S = mmrv(x)
print(ans)
test_mrv()
The C code for this tells me that we are trying to free the variable (using basic_free_stack) even before creating a stack for it (using basic_new_stack), Hence the error I think
One approach that comes to my mind is the following,
i) We can maybe introduce a function in symengine's cwrapper.h (and it's corresponding implementation in cwrapper.cpp)
int basic_has_allocated_stack(basic s);
int basic_has_allocated_stack(basic s) {
return s != nullptr;
}
And then check everytime before freeing if it was allocated on the stack beforehand. Not sure if this is go how we should proceed.
ii) Or maybe something like basic_free_stack_if_allocated that first checks if the basic variable is NULL and if it's not then frees it !
iii) And even if we don't plan to make any changes through SymEngine just adding a statement like this
if (stack0 != NULL) {
basic_free_stack(stack0);
}
instead of just basic_free_stack(stack0) would work
Activity
anutosh491 commentedon Mar 18, 2024
What works
anutosh491 commentedon Mar 18, 2024
Also something similar which works
This hints that it might be something related to declaring or freeing of variables.
anutosh491 commentedon Mar 18, 2024
Smallest failing example
The C code for this tells me that we are trying to free the variable (using
basic_free_stack) even before creating a stack for it (usingbasic_new_stack), Hence the error I thinkanutosh491 commentedon Mar 18, 2024
One approach that comes to my mind is the following,
i) We can maybe introduce a function in symengine's cwrapper.h (and it's corresponding implementation in cwrapper.cpp)
And then check everytime before freeing if it was allocated on the stack beforehand. Not sure if this is go how we should proceed.
ii) Or maybe something like
basic_free_stack_if_allocatedthat first checks if thebasicvariable isNULLand if it's not then frees it !iii) And even if we don't plan to make any changes through SymEngine just adding a statement like this
instead of just
basic_free_stack(stack0)would workcertik commentedon Mar 20, 2024
The bug is in the else branch:
Where we are freeing the
stack0that has not been allocated, so it fails.certik commentedon Mar 20, 2024
I can see two approaches forward:
stack0as NULL at the beginning, and we only free it if it is not nullbasic_new_stack(stack0)every time we create a local variablestack0and then we always deallocate it.stack0inside the "if" statement where it is needed (the "then" branch of the if statement only)Here is the Python code:
Here is how 1. would be applied to the this code:
Here is how 2. would be applied to the above:
Here is how 3. would be applied to the above:
anutosh491 commentedon Mar 27, 2024
I think we can go for the first approach, I've raise a pr to symengine (symengine/symengine#2011) for the same.
certik commentedon Apr 3, 2024
Let's go with 1. for now. Let's write a function in the symbolic pass that will insert the equivalent of:
into ASR.