lcompilers / lpython

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

List does not work in the global scope #1376

Open Thirumalai-Shaktivel opened 1 year ago

Thirumalai-Shaktivel commented 1 year ago

Consider an example

x: list[i32] = [1]
print(x[0])

ASR

(TranslationUnit 
    (SymbolTable
        1
        {
            _lpython_main_program:
                (Function 
                    (SymbolTable
                        3
                        {

                        }) 
                    _lpython_main_program 
                    [] 
                    [] 
                    [(Print 
                        () 
                        [(ListItem 
                            (Var 1 x) 
                            (IntegerConstant 0 (Integer 4 [])) 
                            (Integer 4 []) 
                            ()
                        )] 
                        () 
                        ()
                    )] 
                    () 
                    Source 
                    Public 
                    Implementation 
                    () 
                    .false. 
                    .false. 
                    .false. 
                    .false. 
                    .false. 
                    [] 
                    [] 
                    .false.
                ), 
            main_program:
                (Program 
                    (SymbolTable
                        2
                        {

                        }) 
                    main_program 
                    [] 
                    [(SubroutineCall 
                        1 _lpython_main_program 
                        () 
                        [] 
                        ()
                    )]
                ), 
            x:
                (Variable 
                    1 
                    x 
                    [] 
                    Local 
                    (ListConstant 
                        [(IntegerConstant 1 (Integer 4 []))] 
                        (List 
                            (Integer 4 [])
                        )
                    ) 
                    () 
                    Default 
                    (List 
                        (Integer 4 [])
                    ) 
                    Source 
                    Public 
                    Required 
                    .false.
                )

        }) 
    []
)

From the above ASR, the variable x must be included in the _lpython_main_program SymbolTable, right? Is this a bug or we have not handled the global variable yet? or is this expected output?

$ lpython examples/expr2.py
...
  File "/home/thirumalai/Open_Source/lpython/src/libasr/../libasr/asr.h", line 4150, in LFortran::ASR::BaseVisitor<LFortran::ASRToLLVMVisitor>::visit_symbol(LFortran::ASR::symbol_t const&)
    void visit_symbol(const symbol_t &b) { visit_symbol_t(b, self()); }
  File "/home/thirumalai/Open_Source/lpython/src/libasr/../libasr/asr.h", line 3919, in ??
    case symbolType::Variable: { v.visit_Variable((const Variable_t &)x); return; }
  File "/home/thirumalai/Open_Source/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 1999, in LFortran::ASRToLLVMVisitor::visit_Variable(LFortran::ASR::Variable_t const&)
    this->visit_expr_wrapper(x.m_symbolic_value, true);
  File "/home/thirumalai/Open_Source/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 4144, in LFortran::ASRToLLVMVisitor::visit_expr_wrapper(LFortran::ASR::expr_t const*, bool)
    this->visit_expr(*x);
  File "/home/thirumalai/Open_Source/lpython/src/libasr/../libasr/asr.h", line 4208, in LFortran::ASR::BaseVisitor<LFortran::ASRToLLVMVisitor>::visit_expr(LFortran::ASR::expr_t const&)
    void visit_expr(const expr_t &b) { visit_expr_t(b, self()); }
  File "/home/thirumalai/Open_Source/lpython/src/libasr/../libasr/asr.h", line 4007, in ??
    case exprType::ListConstant: { v.visit_ListConstant((const ListConstant_t &)x); return; }
  File "/home/thirumalai/Open_Source/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 1390, in LFortran::ASRToLLVMVisitor::visit_ListConstant(LFortran::ASR::ListConstant_t const&)
    llvm::Value* const_list = builder->CreateAlloca(const_list_type, nullptr, "const_list");
  File "/home/thirumalai/mambaforge/envs/lp/include/llvm/IR/IRBuilder.h", line 1598, in llvm::IRBuilderBase::CreateAlloca(llvm::Type*, llvm::Value*, llvm::Twine const&)
    const DataLayout &DL = BB->getModule()->getDataLayout();
  File "/home/thirumalai/mambaforge/envs/lp/include/llvm/IR/BasicBlock.h", line 117, in llvm::BasicBlock::getModule()
    static_cast<const BasicBlock *>(this)->getModule());
  Binary file "/home/thirumalai/Open_Source/lpython/src/bin/lpython", in llvm::SymbolTableListTraits<llvm::Instruction>::toPtr(llvm::ValueSymbolTable&)
  Binary file "/lib/x86_64-linux-gnu/libc.so.6", in __GI___sigaction()
Segfault: Signal SIGSEGV (segmentation fault) received

Originally posted by @Thirumalai-Shaktivel in https://github.com/lcompilers/lpython/issues/1358#issuecomment-1348140087

certik commented 1 year ago

Yes, most statements do not work in global scope yet (I think for loops also don't work). We should fix it comprehensively.