Skip to content

Commit 62fda8a

Browse files
authored
Merge pull request #2271 from Shaikh-Ubaid/llvm_fix_ifexp
LLVM: Fix visit_IfExp()
2 parents 7b55bee + 998298b commit 62fda8a

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ RUN(NAME test_global LABELS cpython llvm c)
626626
RUN(NAME test_global_decl LABELS cpython llvm c)
627627
RUN(NAME test_ifexp_01 LABELS cpython llvm c)
628628
RUN(NAME test_ifexp_02 LABELS cpython llvm c)
629+
RUN(NAME test_ifexp_03 LABELS cpython llvm c)
629630
RUN(NAME test_unary_op_01 LABELS cpython llvm c) # unary minus
630631
RUN(NAME test_unary_op_02 LABELS cpython llvm c) # unary plus
631632
RUN(NAME test_unary_op_03 LABELS cpython llvm c wasm) # unary bitinvert

integration_tests/test_ifexp_01.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
def f():
44
i: i32
55
i = 1 if True else 0
6+
print (i)
67
assert i == 1
8+
79
j: f32
810
j = f32(1.0 if 1.0 <= 0.0 else 0.0)
11+
print(j)
912
assert j == f32(0.0)
1013

1114
f()

integration_tests/test_ifexp_03.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from lpython import i32
2+
3+
def fib(n: i32) -> i32:
4+
return fib(n - 1) + fib(n - 2) if n >= 3 else 1
5+
6+
def main0():
7+
res: i32 = fib(30)
8+
print(res)
9+
assert res == 832040
10+
11+
main0()

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5127,11 +5127,16 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
51275127
// IfExp(expr test, expr body, expr orelse, ttype type, expr? value)
51285128
this->visit_expr_wrapper(x.m_test, true);
51295129
llvm::Value *cond = tmp;
5130-
this->visit_expr_wrapper(x.m_body, true);
5131-
llvm::Value *then_val = tmp;
5132-
this->visit_expr_wrapper(x.m_orelse, true);
5133-
llvm::Value *else_val = tmp;
5134-
tmp = builder->CreateSelect(cond, then_val, else_val);
5130+
llvm::Type* _type = llvm_utils->get_type_from_ttype_t_util(x.m_type, module.get());
5131+
llvm::Value* ifexp_res = builder->CreateAlloca(_type);
5132+
llvm_utils->create_if_else(cond, [&]() {
5133+
this->visit_expr_wrapper(x.m_body, true);
5134+
builder->CreateStore(tmp, ifexp_res);
5135+
}, [&]() {
5136+
this->visit_expr_wrapper(x.m_orelse, true);
5137+
builder->CreateStore(tmp, ifexp_res);
5138+
});
5139+
tmp = CreateLoad(ifexp_res);
51355140
}
51365141

51375142
// TODO: Implement visit_DooLoop

0 commit comments

Comments
 (0)