Skip to content

Add list.pop #1845

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 13 commits into from
Jun 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
2 changes: 2 additions & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@ RUN(NAME test_list_section LABELS cpython llvm c NOFAST)
RUN(NAME test_list_count LABELS cpython llvm)
RUN(NAME test_list_index LABELS cpython llvm)
RUN(NAME test_list_reverse LABELS cpython llvm)
RUN(NAME test_list_pop LABELS cpython llvm NOFAST) # TODO: Remove NOFAST from here.
RUN(NAME test_list_pop2 LABELS cpython llvm NOFAST) # TODO: Remove NOFAST from here.
RUN(NAME test_tuple_01 LABELS cpython llvm c)
RUN(NAME test_tuple_02 LABELS cpython llvm c NOFAST)
RUN(NAME test_tuple_03 LABELS cpython llvm c)
Expand Down
68 changes: 68 additions & 0 deletions integration_tests/test_list_pop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from lpython import i32, f64

def test_list_pop():
l1: list[i32]
l2: list[tuple[i32, f64]]
l3: list[list[str]]
i: i32
j: i32
total: i32
x: tuple[i32, f64]

l1 = [1, 2, 3]
assert l1.pop() == 3
assert l1 == [1, 2]

l1 = []
total = 10
for i in range(total):
l1.append(i)
if i % 2 == 1:
assert l1.pop() == i
for i in range(total // 2):
assert l1[i] == 2 * i

l2 = [(1, 2.0)]
x = l2.pop()
assert x == (1, 2.0)
assert len(l2) == 0
l2.append((2, 3.0))
assert x == (1, 2.0)

l3 = []
for i in range(total):
l3.insert(0, ["a"])
for j in range(len(l3)):
l3[j] += ["a"]
while len(l3) > 0:
total = len(l3)
assert len(l3.pop()) == total + 1
assert len(l3) == 0

l1 = [0, 1, 2, 3, 4]
assert l1.pop(3) == 3
assert l1.pop(0) == 0
assert l1.pop(len(l1) - 1) == 4
assert l1 == [1, 2]

total = 10
l1 = []
for i in range(total):
l1.append(i)
j = 0
for i in range(total):
assert l1.pop(j - i) == i
j += 1
assert len(l1) == 0

total = 10
l2 = []
for i in range(total):
l2.append((i, f64(i * i)))
j = 0
for i in range(total):
assert l2.pop(j - i) == (i, f64(i * i))
j += 1
assert len(l2) == 0

test_list_pop()
22 changes: 22 additions & 0 deletions integration_tests/test_list_pop2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from lpython import i32, f64, InOut

def pop_wrapper(l: InOut[list[tuple[i32, f64]]], idx: i32) -> tuple[i32, f64]:
return l.pop(idx)

def test_list_pop():
l1: list[tuple[i32, f64]]
x: tuple[i32, f64]
i: i32

l1 = [(1, 1.0), (2, 4.0), (3, 6.0)]

x = pop_wrapper(l1, 0)
assert x == (1, 1.0)

l1.append((4, 8.0))
assert x == (1, 1.0)

for i in range(len(l1)):
assert l1[i] == (i + 2, 2.0 * f64(i + 2))

test_list_pop()
1 change: 0 additions & 1 deletion src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ expr
| ListItem(expr a, expr pos, ttype type, expr? value)
| TupleItem(expr a, expr pos, ttype type, expr? value)
| ListSection(expr a, array_index section, ttype type, expr? value)
| ListPop(expr a, expr? index, ttype type, expr? value)
| DictPop(expr a, expr key, ttype type, expr? value)
| SetPop(expr a, ttype type, expr? value)
| IntegerBitLen(expr a, ttype type, expr? value)
Expand Down
45 changes: 41 additions & 4 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,32 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
list_api->reverse(plist, asr_el_type, *module);
}

void generate_ListPop_0(ASR::expr_t* m_arg) {
ASR::ttype_t* asr_el_type = ASRUtils::get_contained_type(ASRUtils::expr_type(m_arg));
int64_t ptr_loads_copy = ptr_loads;
ptr_loads = 0;
this->visit_expr(*m_arg);
llvm::Value* plist = tmp;

ptr_loads = !LLVM::is_llvm_struct(asr_el_type);
ptr_loads = ptr_loads_copy;
tmp = list_api->pop_last(plist, asr_el_type, *module);
}

void generate_ListPop_1(ASR::expr_t* m_arg, ASR::expr_t* m_ele) {
ASR::ttype_t* asr_el_type = ASRUtils::get_contained_type(ASRUtils::expr_type(m_arg));
int64_t ptr_loads_copy = ptr_loads;
ptr_loads = 0;
this->visit_expr(*m_arg);
llvm::Value* plist = tmp;

ptr_loads = 2;
this->visit_expr_wrapper(m_ele, true);
ptr_loads = ptr_loads_copy;
llvm::Value *pos = tmp;
tmp = list_api->pop_position(plist, pos, asr_el_type, module.get(), name2memidx);
}

void visit_IntrinsicFunction(const ASR::IntrinsicFunction_t& x) {
switch (static_cast<ASRUtils::IntrinsicFunctions>(x.m_intrinsic_id)) {
case ASRUtils::IntrinsicFunctions::ListIndex: {
Expand All @@ -2038,6 +2064,21 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}
break ;
}
case ASRUtils::IntrinsicFunctions::ListReverse: {
generate_ListReverse(x.m_args[0]);
break;
}
case ASRUtils::IntrinsicFunctions::ListPop: {
switch(x.m_overload_id) {
case 0:
generate_ListPop_0(x.m_args[0]);
break;
case 1:
generate_ListPop_1(x.m_args[0], x.m_args[1]);
break;
}
break;
}
case ASRUtils::IntrinsicFunctions::Exp: {
switch (x.m_overload_id) {
case 0: {
Expand Down Expand Up @@ -2080,10 +2121,6 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}
break ;
}
case ASRUtils::IntrinsicFunctions::ListReverse: {
generate_ListReverse(x.m_args[0]);
break;
}
default: {
throw CodeGenError( ASRUtils::IntrinsicFunctionRegistry::
get_intrinsic_function_name(x.m_intrinsic_id) +
Expand Down
105 changes: 105 additions & 0 deletions src/libasr/codegen/llvm_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2915,6 +2915,111 @@ namespace LCompilers {
builder->CreateStore(end_point, end_point_ptr);
}

llvm::Value* LLVMList::pop_last(llvm::Value* list, ASR::ttype_t* list_type, llvm::Module& module) {
// If list is empty, output error

llvm::Value* end_point_ptr = get_pointer_to_current_end_point(list);
llvm::Value* end_point = LLVM::CreateLoad(*builder, end_point_ptr);

llvm::Value* cond = builder->CreateICmpEQ(llvm::ConstantInt::get(
context, llvm::APInt(32, 0)), end_point);
llvm_utils->create_if_else(cond, [&]() {
std::string message = "pop from empty list";
llvm::Value *fmt_ptr = builder->CreateGlobalStringPtr("IndexError: %s\n");
llvm::Value *fmt_ptr2 = builder->CreateGlobalStringPtr(message);
print_error(context, module, *builder, {fmt_ptr, fmt_ptr2});
int exit_code_int = 1;
llvm::Value *exit_code = llvm::ConstantInt::get(context,
llvm::APInt(32, exit_code_int));
exit(context, module, *builder, exit_code);
}, [=]() {
});

// Get last element of list
llvm::Value* tmp = builder->CreateSub(end_point, llvm::ConstantInt::get(
context, llvm::APInt(32, 1)));
tmp = read_item(list, tmp, false, module, LLVM::is_llvm_struct(list_type));

// Decrement end point by one
end_point = builder->CreateSub(end_point, llvm::ConstantInt::get(
context, llvm::APInt(32, 1)));
builder->CreateStore(end_point, end_point_ptr);
return tmp;
}

llvm::Value* LLVMList::pop_position(llvm::Value* list, llvm::Value* pos,
ASR::ttype_t* list_element_type, llvm::Module* module,
std::map<std::string, std::map<std::string, int>>& name2memidx) {

/* Equivalent in C++:
* while(end_point > pos) {
* tmp = pos + 1;
* list[pos] = list[tmp];
* pos = tmp;
* }
*/

llvm::Value* end_point_ptr = get_pointer_to_current_end_point(list);
llvm::Value* end_point = LLVM::CreateLoad(*builder, end_point_ptr);

llvm::AllocaInst *pos_ptr = builder->CreateAlloca(
llvm::Type::getInt32Ty(context), nullptr);
LLVM::CreateStore(*builder, pos, pos_ptr);
llvm::Value* tmp = nullptr;

// Get element to return
llvm::Value* item = read_item(list, LLVM::CreateLoad(*builder, pos_ptr),
true, *module, LLVM::is_llvm_struct(list_element_type));
// TODO: Create a macro for the following code to allocate auxiliary variables
// on stack.
if( LLVM::is_llvm_struct(list_element_type) ) {
std::string list_element_type_code = ASRUtils::get_type_code(list_element_type);
llvm::BasicBlock &entry_block = builder->GetInsertBlock()->getParent()->getEntryBlock();
llvm::IRBuilder<> builder0(context);
builder0.SetInsertPoint(&entry_block, entry_block.getFirstInsertionPt());
LCOMPILERS_ASSERT(typecode2listtype.find(list_element_type_code) != typecode2listtype.end());
llvm::AllocaInst *target = builder0.CreateAlloca(
std::get<2>(typecode2listtype[list_element_type_code]), nullptr,
"pop_position_item");
llvm_utils->deepcopy(item, target, list_element_type, module, name2memidx);
item = target;
}

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 *cond = builder->CreateICmpSGT(end_point,
LLVM::CreateLoad(*builder, pos_ptr));
builder->CreateCondBr(cond, loopbody, loopend);
}

// body
llvm_utils->start_new_block(loopbody);
{
tmp = builder->CreateAdd(
LLVM::CreateLoad(*builder, pos_ptr),
llvm::ConstantInt::get(context, llvm::APInt(32, 1)));
write_item(list, LLVM::CreateLoad(*builder, pos_ptr),
read_item(list, tmp, false, *module, false), false, *module);
LLVM::CreateStore(*builder, tmp, pos_ptr);
}
builder->CreateBr(loophead);

// end
llvm_utils->start_new_block(loopend);

// Decrement end point by one
end_point = builder->CreateSub(end_point, llvm::ConstantInt::get(
context, llvm::APInt(32, 1)));
builder->CreateStore(end_point, end_point_ptr);

return item;
}

void LLVMList::list_clear(llvm::Value* list) {
llvm::Value* end_point_ptr = get_pointer_to_current_end_point(list);
llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::getInt32Ty(context),
Expand Down
6 changes: 6 additions & 0 deletions src/libasr/codegen/llvm_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ namespace LCompilers {
void remove(llvm::Value* list, llvm::Value* item,
ASR::ttype_t* item_type, llvm::Module& module);

llvm::Value* pop_position(llvm::Value* list, llvm::Value* pos,
ASR::ttype_t* list_type, llvm::Module* module,
std::map<std::string, std::map<std::string, int>>& name2memidx);

llvm::Value* pop_last(llvm::Value* list, ASR::ttype_t* list_type, llvm::Module& module);

void list_clear(llvm::Value* list);

void reverse(llvm::Value* list, ASR::ttype_t* list_type, llvm::Module& module);
Expand Down
Loading