lcompilers / lpython

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

Unhandled Exception for simple array allocation #2491

Open rebcabin opened 7 months ago

rebcabin commented 7 months ago

passes CPython but not lpython

import numpy
from numpy import empty

from lpython import (i16)

def main():
    A_nm: i16[15, 3] = empty((15, 3), dtype=numpy.int16)
    print ("hello, world!")

if __name__ == "__main__":
    main()
(lp) ┌─(~/Documents/GitHub/lpython/integration_tests)────(brian@MacBook-Pro:s001)─┐
└─(11:58:11 on vector-backend ✹ ✭)──> lpython ../ISSUES/UNHANDLED-EXCEPTIONS/Issue2491.py

Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/bin/lpython.cpp", line 1872
    err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir,
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/bin/lpython.cpp", line 786
    r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options,
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 7996
    auto res2 = body_visitor(al, lm, *ast_m, diagnostics, unit, main_module, module_name,
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 7932
    BodyVisitor b(al, lm, unit, diagnostics, main_module, module_name, ast_overload, allow_implicit_casting);
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4713
    visit_stmt(*x.m_body[i]);
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/python_ast.h", line 1883
    void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/python_ast.h", line 1751
    case stmtType::FunctionDef: { v.visit_FunctionDef((const FunctionDef_t &)x); return; }
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4814
    handle_fn(x, *f);
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4790
    transform_stmts(body, x.n_body, x.m_body);
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4667
    this->visit_stmt(*m_body[i]);
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/python_ast.h", line 1883
    void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/python_ast.h", line 1757
    case stmtType::AugAssign: { v.visit_AugAssign((const AugAssign_t &)x); return; }
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4885
    visit_AnnAssignUtil(x, var_name, init_expr);
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 2812
    this->visit_expr(*x.m_value);
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/python_ast.h", line 1910
    void visit_expr(const expr_t &b) { visit_expr_t(b, self()); }
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/python_ast.h", line 1799
    case exprType::Compare: { v.visit_Compare((const Compare_t &)x); return; }
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 7577
    LCOMPILERS_ASSERT(false);
AssertFailed: false
(lp) ┌─(~/Documents/GitHub/lpython/integration_tests)────(brian@MacBook-Pro:s001)─┐
└─(11:59:37 on vector-backend ✹ ✭)──> PYTHONPATH=".:../src/runtime/lpython:.." LPYTHON_PY_MOD_NAME="" LPYTHON_PY_MOD_PATH="_lpython-tmp-test-cpython" python ../ISSUES/UNHANDLED-EXCEPTIONS/Issue2491.py

hello, world!
rebcabin commented 7 months ago

I narrowed this down to dtype=numpy.int16 causes the exception, but dtype=int16 with from numpy import int16 avoids the error.

Here is a full program that avoids the error:

from numpy import empty, int16

from lpython import (i16, i32)

def main():
    ai16: i16[15, 3] = empty((15, 3), dtype=int16)

    # A_nm: i16[15, 3] = empty((15, 3), dtype=numpy.int16)
    print ("hello, world!")

if __name__ == "__main__":
    main()
tanay-man commented 6 months ago

@rebcabin If we compare the ast in both cases WIth error :

[(dtype
    (Attribute
         (Name
         numpy
         Load
         )
    int16
    Load
    ))]

Without error:

[(dtype
     (Name
     int16
     Load
     ))]

The error is thrown by line 7589 in python_ast_to_asr.cpp as the below function call returns false.

AST::is_a<AST::Name_t>(*x.m_keywords[0].m_value)

is_a function is defined as:

template <class T, class U>
inline bool is_a(const U &x)
{
    return T::class_type == x.type;
}

I think there is a need of another definition of is_a where such node (Name + attribute) is checked for equality.

tanay-man commented 6 months ago

Another code containing a similar (Name + attribute) node doesn't throw this exception

import math
print(math.sqrt(9.0))