lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.51k stars 164 forks source link

Support to print dataclasses and tuples in REPL #2785

Closed Vipul-Cariappa closed 3 months ago

Vipul-Cariappa commented 3 months ago

Example:

>>> x: tuple[i32, f64] = (2, 2.4)
>>> x
(2, 2.400000)
>>> (2, 2.4, "LPython")
(2, 2.400000, "LPython")
>>> @dataclass
... class MyClass:
...   x: bool
...   y: i32
...   z: str
...
>>> MyClass(True, 12, "LPython")
MyClass(x=True, y=12, z="LPython")
Vipul-Cariappa commented 3 months ago

On further investigation, it looks like when we get the offset of each member field of the struct from LLVM, it returns a random value (reference: https://github.com/lcompilers/lpython/blob/c58d31cce1dd64d0bbdcf20b0a1b8b3a0064e0d7/src/lpython/python_evaluator.cpp#L534-L535).

I am printing out the offsets, and just before it seg faults the offset values are random and high.

(lp16) C:\Users\vipul\Documents\Workspace\lpython\build16>ctest --rerun-failed --output-on-failure
Test project C:/Users/vipul/Documents/Workspace/lpython/build16
    Start 2: test_lpython
1/1 Test #2: test_lpython .....................***Failed    0.68 sec
[doctest] doctest version is "2.4.8"
[doctest] run with "--help" for options
0 0 0 0 0 0 0 0 0 0
0
4
8
0
4
8
0
4
8
0
4
8
0
4
0
4
8
0
4
8
16
0
4
8
16
24
0
2
8
0
0
0
8
0
8
16
0
10376344761626795937
2841615990688
140698833649665
140375097361
===============================================================================
C:\Users\vipul\Documents\Workspace\lpython\src\lpython\tests\test_llvm.cpp(1556):
TEST CASE:  PythonCompiler classes

C:\Users\vipul\Documents\Workspace\lpython\src\lpython\tests\test_llvm.cpp(1556): FATAL ERROR: test case CRASHED: SIGSEGV - Segmentation violation signal

===============================================================================
[doctest] test cases:  55 |  54 passed | 1 failed | 17 skipped
[doctest] assertions: 488 | 488 passed | 0 failed |
[doctest] Status: FAILURE!

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.69 sec

The following tests FAILED:
          2 - test_lpython (Failed)
Errors while running CTest

Is this a bug in LLVM?

cc @Shaikh-Ubaid @certik

Shaikh-Ubaid commented 3 months ago

I am printing out the offsets, and just before it seg faults the offset values are random and high.

Could you please share the test code and the offset printing modification you used here?

Is this a bug in LLVM?

Which LLVM version are you currently using? Could you please check with other LLVM versions?

Vipul-Cariappa commented 3 months ago

I have tested it with both LLVM 11 and 16. It fails similarly in both.

Code used to debug: debugging_segfault_windows.patch

You can uncomment if (___flag) { std::cout << m->str() << std::endl; }, if you want to see the LLVM IR.

Vipul-Cariappa commented 3 months ago

@Shaikh-Ubaid, please have a look at the PR. I believe it is ready to be merged. I have commented out the failing test and opened an issue #2793.