From 3037dd08052bfe7c3e8c5c340edea2c38a34c43b Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sat, 12 Aug 2023 22:04:47 +0530 Subject: [PATCH 1/2] LLVM: Fix visit_IfExp() --- src/libasr/codegen/asr_to_llvm.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index b7d6889304..c7eace7b74 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -5127,11 +5127,16 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor // 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 From 998298b22acbc6d493216e090e921b6f3355736a Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sat, 12 Aug 2023 22:05:33 +0530 Subject: [PATCH 2/2] TEST: LLVM: Add fib test for IfExp() --- integration_tests/CMakeLists.txt | 1 + integration_tests/test_ifexp_01.py | 3 +++ integration_tests/test_ifexp_03.py | 11 +++++++++++ 3 files changed, 15 insertions(+) create mode 100644 integration_tests/test_ifexp_03.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index fccd0f4e17..a272716733 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -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 diff --git a/integration_tests/test_ifexp_01.py b/integration_tests/test_ifexp_01.py index 11729d744b..fb2c0e06b3 100644 --- a/integration_tests/test_ifexp_01.py +++ b/integration_tests/test_ifexp_01.py @@ -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() diff --git a/integration_tests/test_ifexp_03.py b/integration_tests/test_ifexp_03.py new file mode 100644 index 0000000000..21c0424ac9 --- /dev/null +++ b/integration_tests/test_ifexp_03.py @@ -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) + print(res) + assert res == 832040 + +main0()