Skip to content

[LLVM]: Implement Tuple/List Compare #1359

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 6 commits into from
Dec 21, 2022
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
30 changes: 30 additions & 0 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,21 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}
}

void visit_ListCompare(const ASR::ListCompare_t x) {
int64_t ptr_loads_copy = ptr_loads;
ptr_loads = 0;
this->visit_expr(*x.m_left);
llvm::Value* left = tmp;
this->visit_expr(*x.m_right);
llvm::Value* right = tmp;
ptr_loads = ptr_loads_copy;
tmp = llvm_utils->is_equal_by_value(left, right, *module,
ASRUtils::expr_type(x.m_left));
if (x.m_op == ASR::cmpopType::NotEq) {
tmp = builder->CreateNot(tmp);
}
}

void visit_DictLen(const ASR::DictLen_t& x) {
if (x.m_value) {
this->visit_expr(*x.m_value);
Expand Down Expand Up @@ -1671,6 +1686,21 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
list_api->list_clear(plist);
}

void visit_TupleCompare(const ASR::TupleCompare_t& x) {
int64_t ptr_loads_copy = ptr_loads;
ptr_loads = 0;
this->visit_expr(*x.m_left);
llvm::Value* left = tmp;
this->visit_expr(*x.m_right);
llvm::Value* right = tmp;
ptr_loads = ptr_loads_copy;
tmp = llvm_utils->is_equal_by_value(left, right, *module,
ASRUtils::expr_type(x.m_left));
if (x.m_op == ASR::cmpopType::NotEq) {
tmp = builder->CreateNot(tmp);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems good to me. Is this working?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, TupleCompare works fine. I will need to add the ListCompare to make the CI pass.

}

void visit_TupleLen(const ASR::TupleLen_t& x) {
LFORTRAN_ASSERT(x.m_value);
this->visit_expr(*x.m_value);
Expand Down
75 changes: 72 additions & 3 deletions src/libasr/codegen/llvm_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,10 @@ namespace LFortran {
switch( asr_type->type ) {
case ASR::ttypeType::Integer: {
return builder->CreateICmpEQ(left, right);
};
}
case ASR::ttypeType::Logical: {
return builder->CreateICmpEQ(left, right);
}
case ASR::ttypeType::Real: {
return builder->CreateFCmpOEQ(left, right);
}
Expand Down Expand Up @@ -291,6 +294,11 @@ namespace LFortran {
return tuple_api->check_tuple_equality(left, right, tuple_type, context,
builder, module);
}
case ASR::ttypeType::List: {
ASR::List_t* list_type = ASR::down_cast<ASR::List_t>(asr_type);
return list_api->check_list_equality(left, right, list_type->m_type,
context, builder, module);
}
default: {
throw LCompilersException("LLVMUtils::is_equal_by_value isn't implemented for " +
ASRUtils::type_to_str_python(asr_type));
Expand Down Expand Up @@ -2558,6 +2566,65 @@ namespace LFortran {
LLVM::lfortran_free(context, module, *builder, data);
}

llvm::Value* LLVMList::check_list_equality(llvm::Value* l1, llvm::Value* l2,
ASR::ttype_t* item_type,
llvm::LLVMContext& context,
llvm::IRBuilder<>* builder,
llvm::Module& module) {
llvm::AllocaInst *is_equal = builder->CreateAlloca(llvm::Type::getInt1Ty(context), nullptr);
LLVM::CreateStore(*builder, llvm::ConstantInt::get(context, llvm::APInt(1, 1)), is_equal);
llvm::Value *a_len = llvm_utils->list_api->len(l1);
llvm::Value *b_len = llvm_utils->list_api->len(l2);
llvm::Value *cond = builder->CreateICmpEQ(a_len, b_len);
llvm::Function *fn = builder->GetInsertBlock()->getParent();
llvm::BasicBlock *thenBB = llvm::BasicBlock::Create(context, "then", fn);
llvm::BasicBlock *elseBB = llvm::BasicBlock::Create(context, "else");
llvm::BasicBlock *mergeBB = llvm::BasicBlock::Create(context, "ifcont");
builder->CreateCondBr(cond, thenBB, elseBB);
builder->SetInsertPoint(thenBB);
llvm::AllocaInst *idx = builder->CreateAlloca(llvm::Type::getInt32Ty(context), nullptr);
LLVM::CreateStore(*builder, llvm::ConstantInt::get(
context, llvm::APInt(32, 0)), idx);
llvm::BasicBlock *loophead = llvm::BasicBlock::Create(context, "loop.head");
llvm::BasicBlock *loopbody = llvm::BasicBlock::Create(context, "loop.body");
llvm::BasicBlock *loopend = llvm::BasicBlock::Create(context, "loop.end");

// head
llvm_utils->start_new_block(loophead);
{
llvm::Value* i = LLVM::CreateLoad(*builder, idx);
llvm::Value* cnd = builder->CreateICmpSLT(i, a_len);
builder->CreateCondBr(cnd, loopbody, loopend);
}

// body
llvm_utils->start_new_block(loopbody);
{
llvm::Value* i = LLVM::CreateLoad(*builder, idx);
llvm::Value* left_arg = llvm_utils->list_api->read_item(l1, i,
false, module, LLVM::is_llvm_struct(item_type));
llvm::Value* right_arg = llvm_utils->list_api->read_item(l2, i,
false, module, LLVM::is_llvm_struct(item_type));
llvm::Value* res = llvm_utils->is_equal_by_value(left_arg, right_arg, module,
item_type);
res = builder->CreateAnd(LLVM::CreateLoad(*builder, is_equal), res);
LLVM::CreateStore(*builder, res, is_equal);
i = builder->CreateAdd(i, llvm::ConstantInt::get(llvm::Type::getInt32Ty(context),
llvm::APInt(32, 1)));
LLVM::CreateStore(*builder, i, idx);
}

builder->CreateBr(loophead);

// end
llvm_utils->start_new_block(loopend);

builder->CreateBr(mergeBB);
llvm_utils->start_new_block(elseBB);
LLVM::CreateStore(*builder, llvm::ConstantInt::get(context, llvm::APInt(1, 0)), is_equal);
llvm_utils->start_new_block(mergeBB);
return LLVM::CreateLoad(*builder, is_equal);
}

LLVMTuple::LLVMTuple(llvm::LLVMContext& context_,
LLVMUtils* llvm_utils_,
Expand Down Expand Up @@ -2619,8 +2686,10 @@ namespace LFortran {
llvm::Module& module) {
llvm::Value* is_equal = llvm::ConstantInt::get(context, llvm::APInt(1, 1));
for( size_t i = 0; i < tuple_type->n_type; i++ ) {
llvm::Value* t1i = llvm_utils->tuple_api->read_item(t1, i);
llvm::Value* t2i = llvm_utils->tuple_api->read_item(t2, i);
llvm::Value* t1i = llvm_utils->tuple_api->read_item(t1, i, LLVM::is_llvm_struct(
tuple_type->m_type[i]));
llvm::Value* t2i = llvm_utils->tuple_api->read_item(t2, i, LLVM::is_llvm_struct(
tuple_type->m_type[i]));
llvm::Value* is_t1_eq_t2 = llvm_utils->is_equal_by_value(t1i, t2i, module,
tuple_type->m_type[i]);
is_equal = builder->CreateAnd(is_equal, is_t1_eq_t2);
Expand Down
3 changes: 3 additions & 0 deletions src/libasr/codegen/llvm_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ namespace LFortran {
llvm::Module& module);

void free_data(llvm::Value* list, llvm::Module& module);

llvm::Value* check_list_equality(llvm::Value* l1, llvm::Value* l2, ASR::ttype_t *item_type,
llvm::LLVMContext& context, llvm::IRBuilder<>* builder, llvm::Module& module);
};

class LLVMTuple {
Expand Down
1 change: 0 additions & 1 deletion src/libasr/pass/pass_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ namespace LCompilers {
"select_case",
"inline_function_calls",
"unused_functions",
"pass_compare"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably this means we need backend specific list of passes. Because C/C++/Julia will need this pass but LLVM won't.

};

_with_optimization_passes = {
Expand Down