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
{{ message }}
Repository navigation
Seg Fault for case involving symbolic lists through llvm backend #2450
After debugging this I see that the error occurs due to the extraction of element from the list ans[0].
The final ASR after all passes and the llvm IR look good to me.
So we can compare it against the CPtr version after the symbolic pass and see what the problem is. Things to try:
You can then try to reproduce it by explicitly writing Python code that uses CPtr (does not use the symbolic pass) and we need to figure out why it segfaults.
Use lpython d.py --show-c, compile the C code and see if it segfaults the same way. If it does, debug the C code only using clang. If it doesn't, figure out what is different to the LLVM code.
When debugging the C code, you can use lldb to figure out why it segfaults.
I was almost able to figure this out. Now that #2453 & #2454 have gone in, we have the llvm and c backend ready to deal with any symbolic container like a symbolic list.
So consider the following. We expect the program given above to transform to something like this (in python) after the symbolic and the subroutine_from_function pass
from lpython import S
from sympy import Symbol, pi
def mmrv(r: Out[list[S]]) -> None:
x: S = pi
l1: list[S] = [x]
r = l1
def test_mrv2():
ans: list[S]
temp: list[S]
mmrv(temp)
ans = temp
print(ans[0])
test_mrv2()
Now this gives us a segmentation fault both through the llvm and the c backend
So basically the subroutine_from_function pass, alters two steps which are
passing a temp variable (__libasr__created__var__0__func_call_res) to the SubRoutine
Assign the temp variable back to ans
Not that this is wrong but the step 2 here is responsible for the issues through both backends. Considering the C backend. All code is same for both the code snippets above except 3 lines
Wait it doesn't seg fault for you ? It does for me !
My thinking says it's still the deep copy that is at fault both through the llvm and the C backend. The working example I've provided above works both with the llvm and the c backend. If you want to try the C backend out ( the PR is here #2454 , the tests through macos fail for an unknown reason)
@certik@Thirumalai-Shaktivel
I have been trying to debug this for a couple of days but sadly haven't made progress yet.
I am not sure but I think I am stuck with a couple of issues.
Consider the following
from lpython import S
from sympy import Symbol, pi
def mmrv(r: Out[list[S]]) -> None:
x: S = pi
l1: list[S] = [x]
r = l1
def test_mrv2():
ans: list[S]
mmrv(ans)
x: S = ans[0]
print(x)
test_mrv2()
When I execute it through lpython, it works
(lf) anutosh491@spbhat68:~/lpython/lpython$ lpython --enable-symengine --backend=c integration_tests/test_gruntz.py
pi
But when I copy the generated C code (from test_gruntz_tmp__generated__.c to expr.c )
I thought this has something to do with list_deepcopy_CPtr but then I checked it out through multiple different scenarios and I think what might be at fault is the basic_str function. Consider the following C code
We are trying to print the exact same things from both the functions mmrv and test_mrv2. Even the reference being printed out is the same. And the result I get is this
Where _x is a local variable, which holds the storage of x. And the variable goes out of scope and then it segfaults, since the memory is not there anymore. An easy quick fix is to move the line int64_t _x; into global scope, then it works:
Then it works. There are two bugs. The constname0.data[0] is not initialized. The second bug is that we cannot copy using the C = operator, but rather we always have to use basic_assign for any kind of copy of symbolic objects, which automatically handles the reference counting underneath. Note that the variable that we assign to must be initialized with either basic_new_heap or with basic_new_stack.
To fix this, there are two bugs:
The simpler bug is that conceptually l1[0] = x has to be replaced by basic_assign(constname0.data[0], x);, not by constname0.data[0] = x;. In other words, we need to change constname0.data[0] = x into basic_assign(constname0.data[0], x).
The second bug is that l1[0] must be initialized using basic_new_heap. When the List gets initialized in C, it is done using the following code: struct list_CPtr constname0; list_init_CPtr(&constname0, 1); and that would be enough if the element type was an int. But because the element type is a symbolic expression, we also need to add there a line constname0.data[0] = basic_new_heap();. More likely it would be a loop over all elements and calling basic_new_heap() on all elements.
(Also it seems the list is currently leaking memory, it seems it is never destroyed. But once we implement list destorying, we also have to call basic_free_heap() on each element there.)
Specifically, to make progress, let's follow the usual approach. Take the code:
Just figure out how the code should look like, no ASR symbolic pass is needed. Make sure it works both in C and LLVM backends.
Once that works, then we add these translation operations into the symbolic ASR backend.
Note: in LPython, list assignment r = l1 is always a deep copy (unlike in CPython where it is just a refcount increase), so we should initialize r first using basic_new_heap and then use basic_assign to assign the elements. That can be done later. The LPython semantics requires that l1 is not used after it is assigned to r, to keep the semantics equal to CPython.
Activity
anutosh491 commentedon Jan 2, 2024
After debugging this I see that the error occurs due to the extraction of element from the list
ans[0].The final ASR after all passes and the llvm IR look good to me.
certik commentedon Jan 3, 2024
This works:
So we can compare it against the CPtr version after the symbolic pass and see what the problem is. Things to try:
lpython d.py --show-c, compile the C code and see if it segfaults the same way. If it does, debug the C code only using clang. If it doesn't, figure out what is different to the LLVM code.anutosh491 commentedon Jan 8, 2024
I was almost able to figure this out. Now that #2453 & #2454 have gone in, we have the llvm and c backend ready to deal with any symbolic container like a symbolic list.
So consider the following. We expect the program given above to transform to something like this (in python) after the symbolic and the subroutine_from_function pass
Now this gives us a segmentation fault both through the llvm and the c backend
But what works is this
anutosh491 commentedon Jan 8, 2024
So basically the subroutine_from_function pass, alters two steps which are
tempvariable (__libasr__created__var__0__func_call_res) to the SubRoutinetempvariable back toansNot that this is wrong but the step 2 here is responsible for the issues through both backends. Considering the C backend. All code is same for both the code snippets above except 3 lines
The first one works the second gives us a segmentation fault.
Thirumalai-Shaktivel commentedon Jan 10, 2024
Here, the following doesn't seg fault. I think the issue might be either with
printstatement orbasic_str. What do you think?anutosh491 commentedon Jan 10, 2024
Wait it doesn't seg fault for you ? It does for me !
My thinking says it's still the deep copy that is at fault both through the llvm and the C backend. The working example I've provided above works both with the llvm and the c backend. If you want to try the C backend out ( the PR is here #2454 , the tests through macos fail for an unknown reason)
certik commentedon Jan 10, 2024
On the last Python example, both of these things fail for me:
We need to debug it and fix it. Let's start with the C code, compile it using Clang, and figure out why it segfaults.
anutosh491 commentedon Jan 15, 2024
@certik @Thirumalai-Shaktivel
I have been trying to debug this for a couple of days but sadly haven't made progress yet.
I am not sure but I think I am stuck with a couple of issues.
When I execute it through lpython, it works
But when I copy the generated C code (from
test_gruntz_tmp__generated__.ctoexpr.c)Not sure why this might be happening. I think the commands I'm executing are correct
anutosh491 commentedon Jan 15, 2024
list_deepcopy_CPtrbut then I checked it out through multiple different scenarios and I think what might be at fault is thebasic_strfunction. Consider the following C codeWe are trying to print the exact same things from both the functions
mmrvandtest_mrv2. Even the reference being printed out is the same. And the result I get is thiscertik commentedon Jan 17, 2024
I compile the example like this:
Debug like this:
The problem is in this function:
Where
_xis a local variable, which holds the storage ofx. And the variable goes out of scope and then it segfaults, since the memory is not there anymore. An easy quick fix is to move the lineint64_t _x;into global scope, then it works:The corresponding Python function is:
The problem is with the line:
it needs to be replaced with:
Then it works. There are two bugs. The
constname0.data[0]is not initialized. The second bug is that we cannot copy using the C=operator, but rather we always have to usebasic_assignfor any kind of copy of symbolic objects, which automatically handles the reference counting underneath. Note that the variable that we assign to must be initialized with eitherbasic_new_heapor withbasic_new_stack.To fix this, there are two bugs:
l1[0] = xhas to be replaced bybasic_assign(constname0.data[0], x);, not byconstname0.data[0] = x;. In other words, we need to changeconstname0.data[0] = xintobasic_assign(constname0.data[0], x).l1[0]must be initialized usingbasic_new_heap. When the List gets initialized in C, it is done using the following code:struct list_CPtr constname0; list_init_CPtr(&constname0, 1);and that would be enough if the element type was an int. But because the element type is a symbolic expression, we also need to add there a lineconstname0.data[0] = basic_new_heap();. More likely it would be a loop over all elements and callingbasic_new_heap()on all elements.(Also it seems the list is currently leaking memory, it seems it is never destroyed. But once we implement list destorying, we also have to call
basic_free_heap()on each element there.)Specifically, to make progress, let's follow the usual approach. Take the code:
And figure out LPython code that uses the symengine API directly.
TODO:
Note: in LPython, list assignment
r = l1is always a deep copy (unlike in CPython where it is just a refcount increase), so we should initializerfirst usingbasic_new_heapand then usebasic_assignto assign the elements. That can be done later. The LPython semantics requires thatl1is not used after it is assigned tor, to keep the semantics equal to CPython.anutosh491 commentedon Feb 8, 2024
Can be closed as the issue has been correctly fixed through #2465 & #2477