lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.37k stars 157 forks source link

Severe Bug: Output of `--show-asr` differs from actual ASR constructed after transformation #2621

Closed kmr-srbh closed 3 months ago

kmr-srbh commented 3 months ago

I encountered this bug while working out the fix for #2613 . This bug caused me to wander A LOT trying to find the actual cause of the issue. It was only after executing std::cout << LCompilers::pickle(asr, true, false, false) << std::endl; in asr_to_llvm.cpp that I could see the actual ASR which was being constructed, matching the output I was receiving.

Minimum Reproducible Example

Note: The following example will not work after #2620 is merged.

l: list[str] = ["a", "b", "c"]
print(l[0], "...")
(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
...

ASR output for --show-asr

(TranslationUnit
    (SymbolTable
        1
        {
            __main__:
                (Module
                    (SymbolTable
                        2
                        {
                            __main__global_init:
                                (Function
                                    (SymbolTable
                                        3
                                        {

                                        })
                                    __main__global_init
                                    (FunctionType
                                        []
                                        ()
                                        Source
                                        Implementation
                                        ()
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        []
                                        .false.
                                    )
                                    []
                                    []
                                    [(=
                                        (Var 2 l)
                                        (ListConstant
                                            [(StringConstant
                                                "a"
                                                (Character 1 1 ())
                                            )
                                            (StringConstant
                                                "b"
                                                (Character 1 1 ())
                                            )
                                            (StringConstant
                                                "c"
                                                (Character 1 1 ())
                                            )]
                                            (List
                                                (Character 1 1 ())
                                            )
                                        )
                                        ()
                                    )]
                                    ()
                                    Public
                                    .false.
                                    .false.
                                    ()
                                ),
                            __main__global_stmts:
                                (Function
                                    (SymbolTable
                                        4
                                        {

                                        })
                                    __main__global_stmts
                                    (FunctionType
                                        []
                                        ()
                                        Source
                                        Implementation
                                        ()
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        []
                                        .false.
                                    )
                                    []
                                    []
                                    [(Print
                                        [(ListItem
                                            (Var 2 l)
                                            (IntegerConstant 0 (Integer 4))
                                            (Character 1 -2 ())
                                            ()
                                        )
                                        (StringConstant
                                            "..."
                                            (Character 1 3 ())
                                        )]
                                        ()
                                        ()
                                    )]
                                    ()
                                    Public
                                    .false.
                                    .false.
                                    ()
                                ),
                            l:
                                (Variable
                                    2
                                    l
                                    []
                                    Local
                                    ()
                                    ()
                                    Default
                                    (List
                                        (Character 1 -2 ())
                                    )
                                    ()
                                    Source
                                    Public
                                    Required
                                    .false.
                                )
                        })
                    __main__
                    []
                    .false.
                    .false.
                ),
            main_program:
                (Program
                    (SymbolTable
                        5
                        {
                            __main__global_init:
                                (ExternalSymbol
                                    5
                                    __main__global_init
                                    2 __main__global_init
                                    __main__
                                    []
                                    __main__global_init
                                    Public
                                ),
                            __main__global_stmts:
                                (ExternalSymbol
                                    5
                                    __main__global_stmts
                                    2 __main__global_stmts
                                    __main__
                                    []
                                    __main__global_stmts
                                    Public
                                )
                        })
                    main_program
                    [__main__]
                    [(SubroutineCall
                        5 __main__global_init
                        2 __main__global_init
                        []
                        ()
                    )
                    (SubroutineCall
                        5 __main__global_stmts
                        2 __main__global_stmts
                        []
                        ()
                    )]
                )
        })
    []
)

Actual ASR after transformation

(TranslationUnit
    (SymbolTable
        1
        {
            __main__:
                (Module
                    (SymbolTable
                        2
                        {
                            __main__global_init:
                                (Function
                                    (SymbolTable
                                        3
                                        {

                                        })
                                    __main__global_init
                                    (FunctionType
                                        []
                                        ()
                                        Source
                                        Implementation
                                        ()
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        []
                                        .false.
                                    )
                                    []
                                    []
                                    [(=
                                        (Var 2 l)
                                        (ListConstant
                                            [(StringConstant
                                                "a"
                                                (Character 1 1 ())
                                            )
                                            (StringConstant
                                                "b"
                                                (Character 1 1 ())
                                            )
                                            (StringConstant
                                                "c"
                                                (Character 1 1 ())
                                            )]
                                            (List
                                                (Character 1 1 ())
                                            )
                                        )
                                        ()
                                    )]
                                    ()
                                    Public
                                    .false.
                                    .false.
                                    ()
                                ),
                            __main__global_stmts:
                                (Function
                                    (SymbolTable
                                        4
                                        {

                                        })
                                    __main__global_stmts
                                    (FunctionType
                                        []
                                        ()
                                        Source
                                        Implementation
                                        ()
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        []
                                        .false.
                                    )
                                    []
                                    []
                                    [(Print
                                        [(StringConstant
                                            "..."
                                            (Character 1 3 ())
                                        )]
                                        ()
                                        ()
                                    )]
                                    ()
                                    Public
                                    .false.
                                    .false.
                                    ()
                                ),
                            l:
                                (Variable
                                    2
                                    l
                                    []
                                    Local
                                    ()
                                    ()
                                    Default
                                    (List
                                        (Character 1 -2 ())
                                    )
                                    ()
                                    Source
                                    Public
                                    Required
                                    .false.
                                )
                        })
                    __main__
                    []
                    .false.
                    .false.
                ),
            main_program:
                (Program
                    (SymbolTable
                        5
                        {
                            __main__global_init:
                                (ExternalSymbol
                                    5
                                    __main__global_init
                                    2 __main__global_init
                                    __main__
                                    []
                                    __main__global_init
                                    Public
                                ),
                            __main__global_stmts:
                                (ExternalSymbol
                                    5
                                    __main__global_stmts
                                    2 __main__global_stmts
                                    __main__
                                    []
                                    __main__global_stmts
                                    Public
                                )
                        })
                    main_program
                    [__main__]
                    [(SubroutineCall
                        5 __main__global_init
                        5 __main__global_init
                        []
                        ()
                    )
                    (SubroutineCall
                        5 __main__global_stmts
                        5 __main__global_stmts
                        []
                        ()
                    )]
                )
        })
    []
)
Shaikh-Ubaid commented 3 months ago

Thanks for looking into this. I think this is not a bug, but a feature. When you do --show-asr you can see the original ASR that was constructed. This ASR is then passed through several ASR->ASR passes. The backends then receive this transformed ASR.

Therefore the ASR that you see from the output of --show-asr and the ASR that the backends see are different.

To check the ASR that the backends see, you can do --dump-all-passes and you will get some files generated that contain the ASR output after each pass. You can simply check the ASR output after the last pass as that is the ASR that will be seen by the backends.

PS: You can also combine --visualize with --dump-all-passes (or --show-asr) if you want to see ASR visualization.

Shaikh-Ubaid commented 3 months ago

The ASR being constructed after the passes in the example shared here https://github.com/lcompilers/lpython/issues/2621#issue-2203815735 is definitely incorrect. I think we have an issue noted for it here https://github.com/lcompilers/lpython/issues/2613.

Shaikh-Ubaid commented 3 months ago

I am closing this issue for now.