Thanks to visit codestin.com
Credit goes to github.com

Skip to content

LLVM: Fix visit_IfExp() #2271

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 2 commits into from
Aug 12, 2023
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
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@ RUN(NAME test_global LABELS cpython llvm c)
RUN(NAME test_global_decl LABELS cpython llvm c)
RUN(NAME test_ifexp_01 LABELS cpython llvm c)
RUN(NAME test_ifexp_02 LABELS cpython llvm c)
RUN(NAME test_ifexp_03 LABELS cpython llvm c)
RUN(NAME test_unary_op_01 LABELS cpython llvm c) # unary minus
RUN(NAME test_unary_op_02 LABELS cpython llvm c) # unary plus
RUN(NAME test_unary_op_03 LABELS cpython llvm c wasm) # unary bitinvert
Expand Down
3 changes: 3 additions & 0 deletions integration_tests/test_ifexp_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
def f():
i: i32
i = 1 if True else 0
print (i)
assert i == 1

j: f32
j = f32(1.0 if 1.0 <= 0.0 else 0.0)
print(j)
assert j == f32(0.0)

f()
11 changes: 11 additions & 0 deletions integration_tests/test_ifexp_03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from lpython import i32

def fib(n: i32) -> i32:
return fib(n - 1) + fib(n - 2) if n >= 3 else 1

def main0():
res: i32 = fib(30)

Choose a reason for hiding this comment

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

@Shaikh-Ubaid BTW, just in case if you don't know, you can use less than fib(30) value (smaller). This is to make test run faster. Even for fib(10) there will be a crash of recursion overflow in older (unfixed) code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thank you for sharing @polkovnikov! Yes, I agree fib(10) reproduces the error and could be faster that fib(30). I used fib(30), because:

  • the test case with fib(30) seems to execute quickly on my system
$ time python integration_tests/test_ifexp_03.py
832040
python integration_tests/test_ifexp_03.py  0.13s user 0.01s system 98% cpu 0.147 total
$ time lpython integration_tests/test_ifexp_03.py
832040
lpython integration_tests/test_ifexp_03.py  0.10s user 0.02s system 102% cpu 0.117 total
  • I think fib(30) might have much more recursion calls and therefore, huge stack space utilization due to it. Thus, I think the test also checks the working of the binary when there are many recursion calls and huge stack space utilization due to it.

Also thank you so much for trying LPython and reporting the bug! We appreciate it.

print(res)
assert res == 832040

main0()
15 changes: 10 additions & 5 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5127,11 +5127,16 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
// IfExp(expr test, expr body, expr orelse, ttype type, expr? value)
this->visit_expr_wrapper(x.m_test, true);
llvm::Value *cond = tmp;
this->visit_expr_wrapper(x.m_body, true);
llvm::Value *then_val = tmp;
this->visit_expr_wrapper(x.m_orelse, true);
llvm::Value *else_val = tmp;
tmp = builder->CreateSelect(cond, then_val, else_val);
llvm::Type* _type = llvm_utils->get_type_from_ttype_t_util(x.m_type, module.get());
llvm::Value* ifexp_res = builder->CreateAlloca(_type);
llvm_utils->create_if_else(cond, [&]() {
this->visit_expr_wrapper(x.m_body, true);
builder->CreateStore(tmp, ifexp_res);
}, [&]() {
this->visit_expr_wrapper(x.m_orelse, true);
builder->CreateStore(tmp, ifexp_res);
});
tmp = CreateLoad(ifexp_res);
}

// TODO: Implement visit_DooLoop
Expand Down