Skip to content

Initial support for dict data structure #975

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
Aug 17, 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
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ RUN(NAME test_list_06 LABELS cpython llvm)
RUN(NAME test_list_07 LABELS cpython llvm)
RUN(NAME test_tuple_01 LABELS cpython llvm)
RUN(NAME test_tuple_02 LABELS cpython llvm)
RUN(NAME test_dict_01 LABELS cpython llvm)
RUN(NAME modules_01 LABELS cpython llvm)
RUN(NAME modules_02 LABELS cpython llvm)
RUN(NAME test_math LABELS cpython llvm)
Expand Down
16 changes: 16 additions & 0 deletions integration_tests/test_dict_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from ltypes import i32, f64

def test_dict():
rollnumber2cpi: dict[i32, f64] = {0: 1.1}
i: i32
size: i32 = 1000

for i in range(1000, 1000 + size):
rollnumber2cpi[i] = float(i/100.0 + 5.0)

for i in range(1000 + size - 1, 1001, -1):
assert abs(rollnumber2cpi[i] - i/100.0 - 5.0) <= 1e-12

assert abs(rollnumber2cpi[0] - 1.1) <= 1e-12

test_dict()
131 changes: 131 additions & 0 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
std::unique_ptr<LLVMUtils> llvm_utils;
std::unique_ptr<LLVMList> list_api;
std::unique_ptr<LLVMTuple> tuple_api;
std::unique_ptr<LLVMDict> dict_api;
std::unique_ptr<LLVMArrUtils::Descriptor> arr_descr;

uint64_t ptr_loads;
Expand All @@ -237,6 +238,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
llvm_utils(std::make_unique<LLVMUtils>(context, builder.get())),
list_api(std::make_unique<LLVMList>(context, llvm_utils.get(), builder.get())),
tuple_api(std::make_unique<LLVMTuple>(context, llvm_utils.get(), builder.get())),
dict_api(std::make_unique<LLVMDict>(context, llvm_utils.get(), builder.get())),
arr_descr(LLVMArrUtils::Descriptor::get_descriptor(context,
builder.get(),
llvm_utils.get(),
Expand All @@ -246,6 +248,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
{
llvm_utils->tuple_api = tuple_api.get();
llvm_utils->list_api = list_api.get();
llvm_utils->dict_api = dict_api.get();
}

llvm::Value* CreateLoad(llvm::Value *x) {
Expand Down Expand Up @@ -1155,6 +1158,30 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
tmp = const_list;
}

void visit_DictConstant(const ASR::DictConstant_t& x) {
llvm::Type* const_dict_type = get_dict_type(x.m_type);
llvm::Value* const_dict = builder->CreateAlloca(const_dict_type, nullptr, "const_dict");
ASR::Dict_t* x_dict = ASR::down_cast<ASR::Dict_t>(x.m_type);
std::string key_type_code = ASRUtils::get_type_code(x_dict->m_key_type);
std::string value_type_code = ASRUtils::get_type_code(x_dict->m_value_type);
dict_api->dict_init(key_type_code, value_type_code, const_dict, module.get(), x.n_keys);
uint64_t ptr_loads_key = LLVM::is_llvm_struct(x_dict->m_key_type) ? 0 : 2;
uint64_t ptr_loads_value = LLVM::is_llvm_struct(x_dict->m_value_type) ? 0 : 2;
uint64_t ptr_loads_copy = ptr_loads;
for( size_t i = 0; i < x.n_keys; i++ ) {
ptr_loads = ptr_loads_key;
visit_expr(*x.m_keys[i]);
llvm::Value* key = tmp;
ptr_loads = ptr_loads_value;
visit_expr(*x.m_values[i]);
llvm::Value* value = tmp;
dict_api->write_item(const_dict, key, value, module.get(),
x_dict->m_key_type, x_dict->m_value_type);
}
ptr_loads = ptr_loads_copy;
tmp = const_dict;
}

void visit_TupleConstant(const ASR::TupleConstant_t& x) {
ASR::Tuple_t* tuple_type = ASR::down_cast<ASR::Tuple_t>(x.m_type);
std::string type_code = ASRUtils::get_type_code(tuple_type->m_type,
Expand Down Expand Up @@ -1235,6 +1262,23 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
tmp = list_api->read_item(plist, pos, LLVM::is_llvm_struct(el_type));
}

void visit_DictItem(const ASR::DictItem_t& x) {
ASR::Dict_t* dict_type = ASR::down_cast<ASR::Dict_t>(
ASRUtils::expr_type(x.m_a));
uint64_t ptr_loads_copy = ptr_loads;
ptr_loads = 0;
this->visit_expr(*x.m_a);
llvm::Value* pdict = tmp;

ptr_loads = !LLVM::is_llvm_struct(dict_type->m_key_type);
this->visit_expr_wrapper(x.m_key, true);
ptr_loads = ptr_loads_copy;
llvm::Value *key = tmp;

tmp = dict_api->read_item(pdict, key, *module, dict_type->m_key_type,
LLVM::is_llvm_struct(dict_type->m_value_type));
}

void visit_ListLen(const ASR::ListLen_t& x) {
if (x.m_value) {
this->visit_expr(*x.m_value);
Expand All @@ -1248,6 +1292,20 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}
}

void visit_DictLen(const ASR::DictLen_t& x) {
if (x.m_value) {
this->visit_expr(*x.m_value);
return ;
}

uint64_t ptr_loads_copy = ptr_loads;
ptr_loads = 0;
this->visit_expr(*x.m_arg);
ptr_loads = ptr_loads_copy;
llvm::Value* pdict = tmp;
tmp = dict_api->len(pdict);
}

void visit_ListInsert(const ASR::ListInsert_t& x) {
ASR::List_t* asr_list = ASR::down_cast<ASR::List_t>(
ASRUtils::expr_type(x.m_a));
Expand All @@ -1268,6 +1326,26 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
list_api->insert_item(plist, pos, item, asr_list->m_type, *module);
}

void visit_DictInsert(const ASR::DictInsert_t& x) {
ASR::Dict_t* dict_type = ASR::down_cast<ASR::Dict_t>(
ASRUtils::expr_type(x.m_a));
uint64_t ptr_loads_copy = ptr_loads;
ptr_loads = 0;
this->visit_expr(*x.m_a);
llvm::Value* pdict = tmp;

ptr_loads = !LLVM::is_llvm_struct(dict_type->m_key_type);
this->visit_expr_wrapper(x.m_key, true);
llvm::Value *key = tmp;
this->visit_expr_wrapper(x.m_value, true);
llvm::Value *value = tmp;
ptr_loads = ptr_loads_copy;

dict_api->write_item(pdict, key, value, module.get(),
dict_type->m_key_type,
dict_type->m_value_type);
}

void visit_ListRemove(const ASR::ListRemove_t& x) {
ASR::ttype_t* asr_el_type = ASRUtils::get_contained_type(ASRUtils::expr_type(x.m_a));
uint64_t ptr_loads_copy = ptr_loads;
Expand Down Expand Up @@ -1717,6 +1795,41 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
return false;
}

int32_t get_type_size(ASR::ttype_t* asr_type, llvm::Type* llvm_type,
int32_t a_kind) {
if( LLVM::is_llvm_struct(asr_type) ||
ASR::is_a<ASR::Character_t>(*asr_type) ||
ASR::is_a<ASR::Complex_t>(*asr_type) ) {
llvm::DataLayout data_layout(module.get());
return data_layout.getTypeAllocSize(llvm_type);
}
return a_kind;
}

llvm::Type* get_dict_type(ASR::ttype_t* asr_type) {
ASR::Dict_t* asr_dict = ASR::down_cast<ASR::Dict_t>(asr_type);
bool is_local_array_type = false, is_local_malloc_array_type = false;
bool is_local_list = false;
ASR::dimension_t* local_m_dims = nullptr;
int local_n_dims = 0;
int local_a_kind = -1;
ASR::storage_typeType local_m_storage = ASR::storage_typeType::Default;
llvm::Type* key_llvm_type = get_type_from_ttype_t(asr_dict->m_key_type, local_m_storage,
is_local_array_type, is_local_malloc_array_type,
is_local_list, local_m_dims, local_n_dims,
local_a_kind);
int32_t key_type_size = get_type_size(asr_dict->m_key_type, key_llvm_type, local_a_kind);
llvm::Type* value_llvm_type = get_type_from_ttype_t(asr_dict->m_value_type, local_m_storage,
is_local_array_type, is_local_malloc_array_type,
is_local_list, local_m_dims, local_n_dims,
local_a_kind);
int32_t value_type_size = get_type_size(asr_dict->m_value_type, value_llvm_type, local_a_kind);
std::string key_type_code = ASRUtils::get_type_code(asr_dict->m_key_type);
std::string value_type_code = ASRUtils::get_type_code(asr_dict->m_value_type);
return dict_api->get_dict_type(key_type_code, value_type_code, key_type_size,
value_type_size, key_llvm_type, value_llvm_type);
}

llvm::Type* get_type_from_ttype_t(ASR::ttype_t* asr_type,
ASR::storage_typeType m_storage,
bool& is_array_type, bool& is_malloc_array_type,
Expand Down Expand Up @@ -1865,6 +1978,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
llvm_type = list_api->get_list_type(el_llvm_type, el_type_code, type_size);
break;
}
case (ASR::ttypeType::Dict): {
llvm_type = get_dict_type(asr_type);
break;
}
case (ASR::ttypeType::Tuple) : {
ASR::Tuple_t* asr_tuple = ASR::down_cast<ASR::Tuple_t>(asr_type);
std::string type_code = ASRUtils::get_type_code(asr_tuple->m_type,
Expand Down Expand Up @@ -2959,6 +3076,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
bool is_value_list = ASR::is_a<ASR::List_t>(*asr_value_type);
bool is_target_tuple = ASR::is_a<ASR::Tuple_t>(*asr_target_type);
bool is_value_tuple = ASR::is_a<ASR::Tuple_t>(*asr_value_type);
bool is_target_dict = ASR::is_a<ASR::Dict_t>(*asr_target_type);
bool is_value_dict = ASR::is_a<ASR::Dict_t>(*asr_value_type);
if( is_target_list && is_value_list ) {
uint64_t ptr_loads_copy = ptr_loads;
ptr_loads = 0;
Expand Down Expand Up @@ -3014,6 +3133,18 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}
}
return ;
} else if( is_target_dict && is_value_dict ) {
uint64_t ptr_loads_copy = ptr_loads;
ptr_loads = 0;
this->visit_expr(*x.m_value);
llvm::Value* value_dict = tmp;
this->visit_expr(*x.m_target);
llvm::Value* target_dict = tmp;
ptr_loads = ptr_loads_copy;
ASR::Dict_t* value_dict_type = ASR::down_cast<ASR::Dict_t>(asr_value_type);
dict_api->dict_deepcopy(value_dict, target_dict,
value_dict_type, module.get());
return ;
}
if( ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type(x.m_target)) &&
ASR::is_a<ASR::GetPointer_t>(*x.m_value) ) {
Expand Down
Loading