lfortran / lfortran

Official main repository for LFortran
https://lfortran.org/
Other
906 stars 137 forks source link

Accessing an array from another module fails #617

Open certik opened 1 year ago

certik commented 1 year ago

Here is a minimal example:

module b
integer, parameter :: real_kinds(2) = [4, 8]
end module

program a
use b, only: real_kinds
print *, real_kinds(1)
end program a

This gives:

$ lfortran b.f90 
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  File "/Users/ondrej/repos/lfortran/lfortran/src/bin/lfortran.cpp", line 1724
    err = compile_to_object_file(arg_file, tmp_o, false,
  File "/Users/ondrej/repos/lfortran/lfortran/src/bin/lfortran.cpp", line 743
    res = fe.get_llvm3(*asr, lpm, diagnostics);
  File "/Users/ondrej/repos/lfortran/lfortran/src/lfortran/fortran_evaluator.cpp", line 318
    compiler_options.platform, run_fn);
  File "/Users/ondrej/repos/lfortran/lfortran/src/libasr/codegen/asr_to_llvm.cpp", line 5352
    v.nested_func_types = pass_find_nested_vars(asr, context,
  File "../libasr/asr.h", line 3728
  File "../libasr/asr.h", line 3706
  File "../libasr/asr.h", line 3729
  File "../libasr/asr.h", line 3505
  File "/Users/ondrej/repos/lfortran/lfortran/src/libasr/codegen/asr_to_llvm.cpp", line 1065
    visit_symbol(*item.second);
  File "../libasr/asr.h", line 3731
  File "../libasr/asr.h", line 3514
  File "/Users/ondrej/repos/lfortran/lfortran/src/libasr/codegen/asr_to_llvm.cpp", line 1673
    this->visit_stmt(*x.m_body[i]);
  File "../libasr/asr.h", line 3744
  File "../libasr/asr.h", line 3548
  File "/Users/ondrej/repos/lfortran/lfortran/src/libasr/codegen/asr_to_llvm.cpp", line 4486
    handle_print(x);
  File "/Users/ondrej/repos/lfortran/lfortran/src/libasr/codegen/asr_to_llvm.cpp", line 4536
    this->visit_expr_wrapper(x.m_values[i], true);
  File "/Users/ondrej/repos/lfortran/lfortran/src/libasr/codegen/asr_to_llvm.cpp", line 3070
    this->visit_expr(*x);
  File "../libasr/asr.h", line 3786
  File "../libasr/asr.h", line 3625
  File "/Users/ondrej/repos/lfortran/lfortran/src/libasr/codegen/asr_to_llvm.cpp", line 1302
    LFORTRAN_ASSERT(llvm_symtab.find(v_h) != llvm_symtab.end());
AssertFailed: llvm_symtab.find(v_h) != llvm_symtab.end()

The issue is this part of the code:

        if( ASR::is_a<ASR::Var_t>(*x.m_v) ) {
            ASR::Variable_t *v = ASRUtils::EXPR2VAR(x.m_v);
            if( ASR::is_a<ASR::Derived_t>(*v->m_type) ) {
                ASR::Derived_t* der_type = ASR::down_cast<ASR::Derived_t>(v->m_type);
                der_type_name = ASRUtils::symbol_name(ASRUtils::symbol_get_past_external(der_type->m_derived_type));
            }
            uint32_t v_h = get_hash((ASR::asr_t*)v);
            LFORTRAN_ASSERT(llvm_symtab.find(v_h) != llvm_symtab.end());
            array = llvm_symtab[v_h];
            is_argument = (v->m_intent == ASRUtils::intent_in)
                 || (v->m_intent == ASRUtils::intent_out)
                 || (v->m_intent == ASRUtils::intent_inout)
                 || (v->m_intent == ASRUtils::intent_unspecified);

The array does not seem to be generated in LLVM first.

certik commented 1 year ago

Here is an even simpler example:

module b
integer :: real_kinds(2)
end module

program a
use b, only: real_kinds
print *, real_kinds(1)
end program a

but it gives a different error message:

$ lfortran b.f90 
Assertion failed: (Ty && "Invalid GetElementPtrInst indices for type!"), function checkGEPType, file Instructions.h, line 897.
Traceback (most recent call last):
  File "/Users/ondrej/mambaforge/envs/lf/include/llvm/IR/Instructions.h", line 1074
    Type *PtrTy = PointerType::get(checkGEPType(getIndexedType(ElTy, IdxList)),
  File "/Users/ondrej/mambaforge/envs/lf/include/llvm/IR/Instructions.h", line 897
    assert(Ty && "Invalid GetElementPtrInst indices for type!");
  Binary file "/usr/lib/system/libsystem_c.dylib", local address: 0x18024972b
  Binary file "/usr/lib/system/libsystem_c.dylib", local address: 0x18024a313
  Binary file "/usr/lib/system/libsystem_pthread.dylib", local address: 0x18030ceaf
  Binary file "/usr/lib/system/libsystem_platform.dylib", local address: 0x1803244e3
Abort: Signal SIGABRT (abort) received

zsh: abort      lfortran b.f90

But it's a bit related, so we need to fix both.