← 输入框|github.com|刷新
Skip to content

Discussing implementation for visit_assert in replace_symbolic.cpp pass #2401

Closed

Description

@anutosh491

Consider the following example

(lf) anutosh491@spbhat68:~/lpython/lpython$ cat examples/expr2.py 
from lpython import S
from sympy import pi

def main0():
    x: S = pi
    assert(x == pi)

What we have been doing since the beginning is we

  1. Transform the assert block using basic_str, so we basically compare strings here (where symengine_stack_variable0 stores pi)
    assert (basic_str(x) == basic_str(symengine_stack_variable0))

But what would be better is to do the following assert( basic_eq(x, symengine_stack_variable0) == 1)

Activity

  1. anutosh491 commented on Oct 31, 2023

    @anutosh491
    CollaboratorAuthor

    I think the following diff should work perfectly

    diff --git a/src/libasr/pass/replace_symbolic.cpp b/src/libasr/pass/replace_symbolic.cpp
    index 3bae7436f..8057f4022 100644
    --- a/src/libasr/pass/replace_symbolic.cpp
    +++ b/src/libasr/pass/replace_symbolic.cpp
    @@ -1474,14 +1474,25 @@ public:
             } else if (ASR::is_a<ASR::SymbolicCompare_t>(*x.m_test)) {
                 ASR::SymbolicCompare_t *s = ASR::down_cast<ASR::SymbolicCompare_t>(x.m_test);
                 SymbolTable* module_scope = current_scope->parent;
    -            ASR::expr_t* left_tmp = nullptr;
    -            ASR::expr_t* right_tmp = nullptr;
    -
    -            ASR::symbol_t* basic_str_sym = declare_basic_str_function(al, x.base.base.loc, module_scope);
    -            left_tmp = process_with_basic_str(al, x.base.base.loc, s->m_left, basic_str_sym);
    -            right_tmp = process_with_basic_str(al, x.base.base.loc, s->m_right, basic_str_sym);
    -            ASR::expr_t* test =  ASRUtils::EXPR(ASR::make_StringCompare_t(al, x.base.base.loc, left_tmp,
    -                s->m_op, right_tmp, s->m_type, s->m_value));
    +            ASR::symbol_t* basic_eq_sym = declare_basic_eq_function(al, x.base.base.loc, module_scope);
    +            ASR::expr_t* value1 = handle_argument(al, x.base.base.loc, s->m_left);
    +            ASR::expr_t* value2 = handle_argument(al, x.base.base.loc, s->m_right);
    +            Vec<ASR::call_arg_t> call_args;
    +            call_args.reserve(al, 1);
    +            ASR::call_arg_t call_arg1, call_arg2;
    +            call_arg1.loc = x.base.base.loc;
    +            call_arg1.m_value = value1;
    +            call_arg2.loc = x.base.base.loc;
    +            call_arg2.m_value = value2;
    +            call_args.push_back(al, call_arg1);
    +            call_args.push_back(al, call_arg2);
    +            ASR::expr_t* function_call = ASRUtils::EXPR(ASRUtils::make_FunctionCall_t_util(al, x.base.base.loc,
    +                basic_eq_sym, basic_eq_sym, call_args.p, call_args.n,
    +                ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc, 4)), nullptr, nullptr));
    +
    +            ASR::expr_t* test =  ASRUtils::EXPR(ASR::make_IntegerCompare_t(al, x.base.base.loc, function_call, ASR::cmpopType::Eq,
    +                ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, x.base.base.loc, 1, ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc, 4)))),
    +                ASRUtils::TYPE(ASR::make_Logical_t(al, x.base.base.loc, 4)), nullptr));

    And I think the execution through the C backend works perfectly

    (lf) anutosh491@spbhat68:~/lpython/lpython$ lpython --enable-symengine --backend=c examples/expr2.py 
    
  2. anutosh491 commented on Oct 31, 2023

    @anutosh491
    CollaboratorAuthor

    However there is something wrong with the LLVM backend.

    (lf) anutosh491@spbhat68:~/lpython/lpython$ lpython --enable-symengine --backend=llvm examples/expr2.py 
    lpython: /home/anutosh491/conda_root/envs/lf/include/llvm/IR/Instructions.h:1181: void llvm::ICmpInst::AssertOK(): Assertion `getOperand(0)->getType() == getOperand(1)->getType() && "Both operands to ICmp instruction are not of the same type!"' failed.
    Traceback (most recent call last):
      Binary file "/home/anutosh491/lpython/lpython/src/bin/lpython", in _start()
      Binary file "/lib/x86_64-linux-gnu/libc.so.6", in __libc_start_main()
      File "/home/anutosh491/lpython/lpython/src/bin/lpython.cpp", line 1869, in ??
        err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir,
      File "/home/anutosh491/lpython/lpython/src/bin/lpython.cpp", line 828, in ??
        res = fe.get_llvm3(*asr, pass_manager, diagnostics, infile);
      File "/home/anutosh491/lpython/lpython/src/lpython/python_evaluator.cpp", line 71, in LCompilers::PythonCompiler::get_llvm3(LCompilers::ASR::TranslationUnit_t&, LCompilers::PassManager&, LCompilers::diag::Diagnostics&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
        run_fn, infile);
      File "/home/anutosh491/lpython/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 8931, 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&)
        v.visit_asr((ASR::asr_t&)asr);
      File "/home/anutosh491/lpython/lpython/src/libasr/../libasr/asr.h", line 5037, in LCompilers::ASR::BaseVisitor<LCompilers::ASRToLLVMVisitor>::visit_asr(LCompilers::ASR::asr_t const&)
        void visit_asr(const asr_t &b) { visit_asr_t(b, self()); }
      File "/home/anutosh491/lpython/lpython/src/libasr/../libasr/asr.h", line 5013, in ??
        case asrType::unit: { v.visit_unit((const unit_t &)x); return; }
      File "/home/anutosh491/lpython/lpython/src/libasr/../libasr/asr.h", line 5038, in LCompilers::ASR::BaseVisitor<LCompilers::ASRToLLVMVisitor>::visit_unit(LCompilers::ASR::unit_t const&)
        void visit_unit(const unit_t &b) { visit_unit_t(b, self()); }
      File "/home/anutosh491/lpython/lpython/src/libasr/../libasr/asr.h", line 4747, in ??
        case unitType::TranslationUnit: { v.visit_TranslationUnit((const TranslationUnit_t &)x); return; }
      File "/home/anutosh491/lpython/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 874, in LCompilers::ASRToLLVMVisitor::visit_TranslationUnit(LCompilers::ASR::TranslationUnit_t const&)
        visit_symbol(*mod);
      File "/home/anutosh491/lpython/lpython/src/libasr/../libasr/asr.h", line 5040, in LCompilers::ASR::BaseVisitor<LCompilers::ASRToLLVMVisitor>::visit_symbol(LCompilers::ASR::symbol_t const&)
        void visit_symbol(const symbol_t &b) { visit_symbol_t(b, self()); }
      File "/home/anutosh491/lpython/lpython/src/libasr/../libasr/asr.h", line 4756, in ??
        case symbolType::Module: { v.visit_Module((const Module_t &)x); return; }
      File "/home/anutosh491/lpython/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 2899, in LCompilers::ASRToLLVMVisitor::visit_Module(LCompilers::ASR::Module_t const&)
        visit_procedures(x);
      File "/home/anutosh491/lpython/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 3775, in void LCompilers::ASRToLLVMVisitor::visit_procedures<LCompilers::ASR::Module_t>(LCompilers::ASR::Module_t const&)
        visit_Function(*s);
      File "/home/anutosh491/lpython/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 3545, in LCompilers::ASRToLLVMVisitor::visit_Function(LCompilers::ASR::Function_t const&)
        generate_function(x);
      File "/home/anutosh491/lpython/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 3727, in LCompilers::ASRToLLVMVisitor::generate_function(LCompilers::ASR::Function_t const&)
        this->visit_stmt(*x.m_body[i]);
      File "/home/anutosh491/lpython/lpython/src/libasr/../libasr/asr.h", line 5057, in LCompilers::ASR::BaseVisitor<LCompilers::ASRToLLVMVisitor>::visit_stmt(LCompilers::ASR::stmt_t const&)
        void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
      File "/home/anutosh491/lpython/lpython/src/libasr/../libasr/asr.h", line 4806, in ??
        case stmtType::Assert: { v.visit_Assert((const Assert_t &)x); return; }
      File "/home/anutosh491/lpython/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 6103, in LCompilers::ASRToLLVMVisitor::visit_Assert(LCompilers::ASR::Assert_t const&)
        this->visit_expr_wrapper(x.m_test, true);
      File "/home/anutosh491/lpython/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 4881, in LCompilers::ASRToLLVMVisitor::visit_expr_wrapper(LCompilers::ASR::expr_t*, bool)
        this->visit_expr(*x);
      File "/home/anutosh491/lpython/lpython/src/libasr/../libasr/asr.h", line 5104, in LCompilers::ASR::BaseVisitor<LCompilers::ASRToLLVMVisitor>::visit_expr(LCompilers::ASR::expr_t const&)
        void visit_expr(const expr_t &b) { visit_expr_t(b, self()); }
      File "/home/anutosh491/lpython/lpython/src/libasr/../libasr/asr.h", line 4846, in ??
        case exprType::IntegerCompare: { v.visit_IntegerCompare((const IntegerCompare_t &)x); return; }
      File "/home/anutosh491/lpython/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 5044, in LCompilers::ASRToLLVMVisitor::visit_IntegerCompare(LCompilers::ASR::IntegerCompare_t const&)
        tmp = builder->CreateICmpEQ(left, right);
      File "/home/anutosh491/conda_root/envs/lf/include/llvm/IR/IRBuilder.h", line 2169, in llvm::IRBuilderBase::CreateICmpEQ(llvm::Value*, llvm::Value*, llvm::Twine const&)
        return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
      File "/home/anutosh491/conda_root/envs/lf/include/llvm/IR/IRBuilder.h", line 2283, in llvm::IRBuilderBase::CreateICmp(llvm::CmpInst::Predicate, llvm::Value*, llvm::Value*, llvm::Twine const&)
        return Insert(new ICmpInst(P, LHS, RHS), Name);
      File "/home/anutosh491/conda_root/envs/lf/include/llvm/IR/Instructions.h", line 1236, in llvm::ICmpInst::ICmpInst(llvm::CmpInst::Predicate, llvm::Value*, llvm::Value*, llvm::Twine const&)
        AssertOK();
      File "/home/anutosh491/conda_root/envs/lf/include/llvm/IR/Instructions.h", line 1181, in llvm::ICmpInst::AssertOK()
        assert(getOperand(0)->getType() == getOperand(1)->getType() &&
      Binary file "/lib/x86_64-linux-gnu/libc.so.6", in __assert_fail()
      Binary file "/lib/x86_64-linux-gnu/libc.so.6", local address: 0x22728
      Binary file "/lib/x86_64-linux-gnu/libc.so.6", in abort()
      Binary file "/lib/x86_64-linux-gnu/libc.so.6", in gsignal()
      Binary file "/lib/x86_64-linux-gnu/libc.so.6", in killpg()
    Abort: Signal SIGABRT (abort) received
    
    Aborted
    

    If we could debug this I think we could switch to a better way to assert things in our tests.

  3. Thirumalai-Shaktivel commented on Oct 31, 2023

    @Thirumalai-Shaktivel
    Collaborator

    Instead of assert( basic_eq(x, symengine_stack_variable0) == 1), why not we just do assert( basic_eq(x, symengine_stack_variable0) ).

    As you can see, the declare has the return type bool in the LLVM IR

    declare i1 @basic_eq(void*, void*)
    

    Modify to above and see if it works?

  4. certik commented on Oct 31, 2023

    @certik
    Contributor

    Yes, we should not be comparing strings, but rather calling basic_eq.

  5. anutosh491 commented on Nov 1, 2023

    @anutosh491
    CollaboratorAuthor

    Thanks @Thirumalai-Shaktivel

    Instead of assert( basic_eq(x, symengine_stack_variable0) == 1), why not we just do assert( basic_eq(x, symengine_stack_variable0) ).

    I think this does it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions