Skip to content

Commit 2ed28bc

Browse files
committed
Fix segmentation fault and use list_api
1 parent 7b21b6e commit 2ed28bc

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/libasr/codegen/llvm_utils.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,8 +2573,8 @@ namespace LFortran {
25732573
llvm::Module& module) {
25742574
llvm::AllocaInst *is_equal = builder->CreateAlloca(llvm::Type::getInt1Ty(context), nullptr);
25752575
LLVM::CreateStore(*builder, llvm::ConstantInt::get(context, llvm::APInt(1, 1)), is_equal);
2576-
llvm::Value *a_len = len(l1);
2577-
llvm::Value *b_len = len(l2);
2576+
llvm::Value *a_len = llvm_utils->list_api->len(l1);
2577+
llvm::Value *b_len = llvm_utils->list_api->len(l2);
25782578
llvm::Value *cond = builder->CreateICmpEQ(a_len, b_len);
25792579
llvm::Function *fn = builder->GetInsertBlock()->getParent();
25802580
llvm::BasicBlock *thenBB = llvm::BasicBlock::Create(context, "then", fn);
@@ -2583,6 +2583,8 @@ namespace LFortran {
25832583
builder->CreateCondBr(cond, thenBB, elseBB);
25842584
builder->SetInsertPoint(thenBB);
25852585
llvm::AllocaInst *idx = builder->CreateAlloca(llvm::Type::getInt32Ty(context), nullptr);
2586+
LLVM::CreateStore(*builder, llvm::ConstantInt::get(
2587+
context, llvm::APInt(32, 0)), idx);
25862588
llvm::BasicBlock *loophead = llvm::BasicBlock::Create(context, "loop.head");
25872589
llvm::BasicBlock *loopbody = llvm::BasicBlock::Create(context, "loop.body");
25882590
llvm::BasicBlock *loopend = llvm::BasicBlock::Create(context, "loop.end");
@@ -2591,17 +2593,17 @@ namespace LFortran {
25912593
llvm_utils->start_new_block(loophead);
25922594
{
25932595
llvm::Value* i = LLVM::CreateLoad(*builder, idx);
2594-
cond = builder->CreateICmpNE(i, a_len);
2595-
builder->CreateCondBr(cond, loopbody, loopend);
2596+
llvm::Value* cnd = builder->CreateICmpSLT(i, a_len);
2597+
builder->CreateCondBr(cnd, loopbody, loopend);
25962598
}
25972599

25982600
// body
25992601
llvm_utils->start_new_block(loopbody);
26002602
{
26012603
llvm::Value* i = LLVM::CreateLoad(*builder, idx);
2602-
llvm::Value* left_arg = read_item(l1, i,
2604+
llvm::Value* left_arg = llvm_utils->list_api->read_item(l1, i,
26032605
false, module, LLVM::is_llvm_struct(item_type));
2604-
llvm::Value* right_arg = read_item(l2, i,
2606+
llvm::Value* right_arg = llvm_utils->list_api->read_item(l2, i,
26052607
false, module, LLVM::is_llvm_struct(item_type));
26062608
llvm::Value* res = llvm_utils->is_equal_by_value(left_arg, right_arg, module,
26072609
item_type);
@@ -2619,7 +2621,7 @@ namespace LFortran {
26192621

26202622
builder->CreateBr(mergeBB);
26212623
llvm_utils->start_new_block(elseBB);
2622-
builder->CreateStore(llvm::ConstantInt::get(context, llvm::APInt(1, 0)), is_equal);
2624+
LLVM::CreateStore(*builder, llvm::ConstantInt::get(context, llvm::APInt(1, 0)), is_equal);
26232625
llvm_utils->start_new_block(mergeBB);
26242626
return LLVM::CreateLoad(*builder, is_equal);
26252627
}

0 commit comments

Comments
 (0)