lfortran / lfortran

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

(PRIMA) Handle comparison in abs intrinsic #5513

Open jinangshah21 opened 2 days ago

jinangshah21 commented 2 days ago

MRE :

program test 
  integer :: A(3) = [1,2,3]
  logical :: X(3)
  integer :: tol = 2
  X = abs(A) <= max(tol, tol * maxval(abs(A)))
  print *, X
end program

LFortran Error :

Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  Binary file "/mnt/c/lfortran/src/bin/lfortran", in _start
  File "/build/glibc-LcI20x/glibc-2.31/csu/../csu/libc-start.c", line 308, in __libc_start_main
  File "/mnt/c/lfortran/src/bin/lfortran.cpp", line 2689, in main
    return main_app(argc, argv);
  File "/mnt/c/lfortran/src/bin/lfortran.cpp", line 2628, in main_app(int, char**)
    err = compile_src_to_object_file(arg_file, tmp_o, false,
  File "/mnt/c/lfortran/src/bin/lfortran.cpp", line 1045, in (anonymous namespace)::compile_src_to_object_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool, LCompilers::CompilerOptions&, LCompilers::PassManager&)
    res = fe.get_llvm3(*asr, lpm, diagnostics, infile);
  File "/mnt/c/lfortran/src/lfortran/fortran_evaluator.cpp", line 382, in LCompilers::FortranEvaluator::get_llvm3(LCompilers::ASR::TranslationUnit_t&, LCompilers::PassManager&, LCompilers::diag::Diagnostics&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
    compiler_options, run_fn, infile);
  File "/mnt/c/lfortran/src/libasr/codegen/asr_to_llvm.cpp", line 10377, in LCompilers::asr_to_llvm(LCompilers::ASR::TranslationUnit_t&, LCompilers::diag::Diagnostics&, llvm::LLVMContext&, Allocator&, LCompilers::PassManager&, LCompilers::CompilerOptions&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
    pass_manager.apply_passes(al, &asr, co.po, diagnostics);
  File "/mnt/c/lfortran/src/libasr/pass/pass_manager.h", line 403, in LCompilers::PassManager::apply_passes(Allocator&, LCompilers::ASR::TranslationUnit_t*, LCompilers::PassOptions&, LCompilers::diag::Diagnostics&)
    apply_passes(al, asr, _passes, pass_options, diagnostics);
  File "/mnt/c/lfortran/src/libasr/pass/pass_manager.h", line 180, in LCompilers::PassManager::apply_passes(Allocator&, LCompilers::ASR::TranslationUnit_t*, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&, LCompilers::PassOptions&, LCompilers::diag::Diagnostics&)
    _passes_db[passes[i]](al, *asr, pass_options);
  File "/mnt/c/lfortran/src/libasr/pass/intrinsic_function.cpp", line 624, in LCompilers::pass_replace_intrinsic_function(Allocator&, LCompilers::ASR::TranslationUnit_t&, LCompilers::PassOptions const&)
    v.visit_TranslationUnit(unit);
  File "/mnt/c/lfortran/src/libasr/../libasr/asr.h", line 49583, in LCompilers::ASR::CallReplacerOnExpressionsVisitor<LCompilers::ReplaceIntrinsicFunctionsVisitor>::visit_TranslationUnit(LCompilers::ASR::TranslationUnit_t const&)
    this->visit_symbol(*a.second);
  File "/mnt/c/lfortran/src/libasr/../libasr/asr.h", line 5189, in LCompilers::ASR::BaseVisitor<LCompilers::ReplaceIntrinsicFunctionsVisitor>::visit_symbol(LCompilers::ASR::symbol_t const&)
    void visit_symbol(const symbol_t &b) { visit_symbol_t(b, self()); }
  File "/mnt/c/lfortran/src/libasr/../libasr/asr.h", line 4900, in void LCompilers::ASR::visit_symbol_t<LCompilers::ReplaceIntrinsicFunctionsVisitor>(LCompilers::ASR::symbol_t const&, LCompilers::ReplaceIntrinsicFunctionsVisitor&)
    case symbolType::Program: { v.visit_Program((const Program_t &)x); return; }
  File "/mnt/c/lfortran/src/libasr/../libasr/asr.h", line 49597, in LCompilers::ASR::CallReplacerOnExpressionsVisitor<LCompilers::ReplaceIntrinsicFunctionsVisitor>::visit_Program(LCompilers::ASR::Program_t const&)
    self().transform_stmts(xx.m_body, xx.n_body);
  File "/mnt/c/lfortran/src/libasr/../libasr/asr.h", line 49576, in LCompilers::ASR::CallReplacerOnExpressionsVisitor<LCompilers::ReplaceIntrinsicFunctionsVisitor>::transform_stmts(LCompilers::ASR::stmt_t**&, unsigned long&)
    self().visit_stmt(*m_body[i]);
  File "/mnt/c/lfortran/src/libasr/../libasr/asr.h", line 5206, in LCompilers::ASR::BaseVisitor<LCompilers::ReplaceIntrinsicFunctionsVisitor>::visit_stmt(LCompilers::ASR::stmt_t const&)
    void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
  File "/mnt/c/lfortran/src/libasr/../libasr/asr.h", line 4926, in void LCompilers::ASR::visit_stmt_t<LCompilers::ReplaceIntrinsicFunctionsVisitor>(LCompilers::ASR::stmt_t const&, LCompilers::ReplaceIntrinsicFunctionsVisitor&)
    case stmtType::Assignment: { v.visit_Assignment((const Assignment_t &)x); return; }
  File "/mnt/c/lfortran/src/libasr/../libasr/asr.h", line 49822, in LCompilers::ASR::CallReplacerOnExpressionsVisitor<LCompilers::ReplaceIntrinsicFunctionsVisitor>::visit_Assignment(LCompilers::ASR::Assignment_t const&)
    self().call_replacer();
  File "/mnt/c/lfortran/src/libasr/pass/intrinsic_function.cpp", line 172, in LCompilers::ReplaceIntrinsicFunctionsVisitor::call_replacer()
    replacer.replace_expr(*current_expr);
  File "/mnt/c/lfortran/src/libasr/../libasr/asr.h", line 48618, in LCompilers::ASR::BaseExprReplacer<LCompilers::ReplaceIntrinsicFunctions>::replace_expr(LCompilers::ASR::expr_t*)
    self().replace_RealBinOp(down_cast<ASR::RealBinOp_t>(x));
  File "/mnt/c/lfortran/src/libasr/../libasr/asr.h", line 47073, in LCompilers::ASR::BaseExprReplacer<LCompilers::ReplaceIntrinsicFunctions>::replace_RealBinOp(LCompilers::ASR::RealBinOp_t*)
    self().replace_expr(x->m_right);
  File "/mnt/c/lfortran/src/libasr/../libasr/asr.h", line 48858, in LCompilers::ASR::BaseExprReplacer<LCompilers::ReplaceIntrinsicFunctions>::replace_expr(LCompilers::ASR::expr_t*)
    self().replace_Cast(down_cast<ASR::Cast_t>(x));
  File "/mnt/c/lfortran/src/libasr/../libasr/asr.h", line 48059, in LCompilers::ASR::BaseExprReplacer<LCompilers::ReplaceIntrinsicFunctions>::replace_Cast(LCompilers::ASR::Cast_t*)
    self().replace_expr(x->m_arg);
  File "/mnt/c/lfortran/src/libasr/../libasr/asr.h", line 48538, in LCompilers::ASR::BaseExprReplacer<LCompilers::ReplaceIntrinsicFunctions>::replace_expr(LCompilers::ASR::expr_t*)
    self().replace_IntrinsicArrayFunction(down_cast<ASR::IntrinsicArrayFunction_t>(x));
  File "/mnt/c/lfortran/src/libasr/pass/intrinsic_function.cpp", line 112, in LCompilers::ReplaceIntrinsicFunctions::replace_IntrinsicArrayFunction(LCompilers::ASR::IntrinsicArrayFunction_t*)
    replace_expr(x->m_args[i]);
  File "/mnt/c/lfortran/src/libasr/../libasr/asr.h", line 48862, in LCompilers::ASR::BaseExprReplacer<LCompilers::ReplaceIntrinsicFunctions>::replace_expr(LCompilers::ASR::expr_t*)
    self().replace_ArrayPhysicalCast(down_cast<ASR::ArrayPhysicalCast_t>(x));
  File "/mnt/c/lfortran/src/libasr/../libasr/asr.h", line 48074, in LCompilers::ASR::BaseExprReplacer<LCompilers::ReplaceIntrinsicFunctions>::replace_ArrayPhysicalCast(LCompilers::ASR::ArrayPhysicalCast_t*)
    self().replace_expr(x->m_arg);
  File "/mnt/c/lfortran/src/libasr/../libasr/asr.h", line 48534, in LCompilers::ASR::BaseExprReplacer<LCompilers::ReplaceIntrinsicFunctions>::replace_expr(LCompilers::ASR::expr_t*)
    self().replace_IntrinsicElementalFunction(down_cast<ASR::IntrinsicElementalFunction_t>(x));
  File "/mnt/c/lfortran/src/libasr/pass/intrinsic_function.cpp", line 89, in LCompilers::ReplaceIntrinsicFunctions::replace_IntrinsicElementalFunction(LCompilers::ASR::IntrinsicElementalFunction_t*)
    ASR::expr_t* current_expr_ = instantiate_function(al, x->base.base.loc,
  File "/mnt/c/lfortran/src/libasr/../libasr/pass/intrinsic_functions.h", line 849, in LCompilers::ASRUtils::Abs::instantiate_Abs(Allocator&, LCompilers::Location const&, LCompilers::SymbolTable*, LCompilers::Vec<LCompilers::ASR::ttype_t*>&, LCompilers::ASR::ttype_t*, LCompilers::Vec<LCompilers::ASR::call_arg_t>&, long)
    body.push_back(al, b.If(b.GtE(args[0], b.i_t(0, arg_types[0])), {
LCompilersException: Expression type, i32[3] not yet supported
jinangshah21 commented 2 days ago

The Error is same as of #5285, Here change is I have just replaced abs(A) <= maxval(abs(A) with abs(A) <= max(tol , tol * maxval(abs(A)))

jinangshah21 commented 2 days ago
program test 
integer :: A(3,2) = reshape([1,2,3,4,5,6], [3,2])
integer :: X(2)
X = abs(sum(A, dim=1))
print *, X
end program

This Also fails with same error

AnshMehta1 commented 1 day ago

I will focus on this as I have some knowledge of it.