Skip to content

fix array symbol duplication in interactive mode #2734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2776,9 +2776,11 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
llvm::StructType* array_type = static_cast<llvm::StructType*>(
llvm_utils->get_type_from_ttype_t_util(x.m_type, module.get()));
llvm::Constant *ptr = module->getOrInsertGlobal(x.m_name, array_type);
module->getNamedGlobal(x.m_name)->setInitializer(
llvm::ConstantStruct::get(array_type,
llvm::Constant::getNullValue(array_type)));
if (!external) {
module->getNamedGlobal(x.m_name)->setInitializer(
llvm::ConstantStruct::get(array_type,
llvm::Constant::getNullValue(array_type)));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Vipul-Cariappa can you add a test that was failing previously in interactive mode and now works?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On main you will get the following error:

>>> i: i32[10]                                                                                                                         1,11  ]
>>> print(i)                                                                                                                           1,9   ]
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  Binary file "/home/vipul/Workspace/python/lpython/src/bin/lpython", in _start()
  Binary file "/lib64/libc.so.6", in __libc_start_main_alias_2()
  Binary file "/lib64/libc.so.6", in __libc_start_call_main()
  File "/home/vipul/Workspace/python/lpython/src/bin/lpython.cpp", line 2061, in main()
    return interactive_python_repl(lpython_pass_manager, compiler_options, arg_v);
  File "/home/vipul/Workspace/python/lpython/src/bin/lpython.cpp", line 883, in interactive_python_repl()
    res = fe.evaluate(code_string, verbose, lm, pass_manager, diagnostics);
  File "/home/vipul/Workspace/python/lpython/src/lpython/python_evaluator.cpp", line 128, in LCompilers::PythonCompiler::evaluate(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool, LCompilers::LocationManager&, LCompilers::PassManager&, LCompilers::diag::Diagnostics&)
    e->add_module(std::move(m));
  File "/home/vipul/Workspace/python/lpython/src/libasr/codegen/evaluator.cpp", line 252, in LCompilers::LLVMEvaluator::add_module(std::unique_ptr<LCompilers::LLVMModule, std::default_delete<LCompilers::LLVMModule> >)
    add_module(std::move(m->m_m));
LCompilersException: addModule() returned an error: Duplicate definition of symbol 'i'

I have added in the test.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue shared and fix in this PR might be real, but the test example shared in the above comment does not look right.

(lp) ubaid@ubaids-MacBook-Pro lpython % python examples/expr2.py 
Traceback (most recent call last):
  File "/Users/ubaid/Desktop/OpenSource/lpython/examples/expr2.py", line 4, in <module>
    print(i)
NameError: name 'i' is not defined. Did you mean: 'id'?

It does not work with CPython. We should only support tests or cases that work with CPython first, then work with LPython regular/normal mode and finally support them in LPython interactive mode.

llvm_symtab[h] = ptr;
} else if (x.m_type->type == ASR::ttypeType::Logical) {
llvm::Constant *ptr = module->getOrInsertGlobal(x.m_name,
Expand Down
17 changes: 17 additions & 0 deletions src/lpython/tests/test_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,23 @@ def my_concat(x: str, y: str) -> str:
CHECK(std::strcmp(r.result.str, "Python REPL") == 0);
}

TEST_CASE("PythonCompiler Array 1") {
CompilerOptions cu;
cu.po.disable_main = true;
cu.emit_debug_line_column = false;
cu.generate_object_code = false;
cu.interactive = true;
cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir();
PythonCompiler e(cu);
LCompilers::Result<PythonCompiler::EvalResult>
r = e.evaluate2("i: i32[10] = empty(10, dtype=int32)");
CHECK(r.ok);
CHECK(r.result.type == PythonCompiler::EvalResult::statement);
r = e.evaluate2("print(i)");
CHECK(r.ok);
CHECK(r.result.type == PythonCompiler::EvalResult::statement);
}

TEST_CASE("PythonCompiler asr verify 1") {
CompilerOptions cu;
cu.po.disable_main = true;
Expand Down
Loading