Skip to content

Supporting nested dict #1111

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
25 changes: 25 additions & 0 deletions integration_tests/test_dict_06.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from ltypes import i32, f64

def test_dict():
graph: dict[i32, dict[i32, f64]] = {0: {2: 1.0/2.0}, 1: {3: 1.0/4.0}}
i: i32; j: i32; nodes: i32; eps: f64 = 1e-12
nodes = 100

assert abs(graph[0][2] - 0.5) <= eps
assert abs(graph[1][3] - 0.25) <= eps

for i in range(1, nodes):
graph[i] = {}
for j in range(1, nodes):
graph[i][j] = 1.0/float(j + i)

for i in range(1, nodes):
for j in range(1, nodes):
assert abs( graph[i][j] - 1.0/float(j + i) ) <= eps

for i in range(1, nodes):
for j in range(1, nodes):
assert abs( graph[i].pop(j) - 1.0/float(j + i) ) <= eps
graph.pop(i)

test_dict()
32 changes: 32 additions & 0 deletions integration_tests/test_dict_07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from ltypes import i32, f64
from math import sin

def test_dict():
friends: dict[tuple[i32, str], dict[tuple[i32, str], tuple[f64, str]]] = {}
eps: f64 = 1e-12
nodes: i32 = 100
i: i32; j: i32;
t: tuple[f64, str]

friends[(0, "0")] = {(1, "1"): (sin(-1.0), "01")}
assert friends[(0, "0")][(1, "1")][0] - sin(-1.0) <= eps

for i in range(nodes):
friends[(i, str(i))] = {}
for j in range(i, nodes):
friends[(i, str(i))][(j, str(j))] = (sin(float(i - j)), str(i) + str(j))

for i in range(nodes):
for j in range(i, nodes):
t = friends[(i, str(i))][(j, str(j))]
assert abs( t[0] - sin(float(i - j)) ) <= eps
assert t[1] == str(i) + str(j)

for i in range(nodes):
for j in range(i, nodes):
t = friends[(i, str(i))].pop((j, str(j)))
assert abs( t[0] - sin(float(i - j)) ) <= eps
assert t[1] == str(i) + str(j)
friends.pop((i, str(i)))

test_dict()
32 changes: 32 additions & 0 deletions integration_tests/test_dict_08.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from ltypes import i32, f64
from math import sin

def test_dict():
friends: dict[str, dict[i32, dict[str, f64]]] = {}
eps: f64 = 1e-12
nodes: i32 = 9
i: i32; j: i32; k: i32;

friends["0"] = {1: {"2": sin(3.0)}}
assert friends["0"][1]["2"] - sin(3.0) <= eps

for i in range(nodes):
friends[str(i)] = {}
for j in range(nodes):
friends[str(i)][j] = {}
for k in range(nodes):
friends[str(i)][j][str(k)] = sin(float(i + j + k))

for i in range(nodes):
for j in range(nodes):
for k in range(nodes):
abs( friends[str(i)][j][str(k)] - sin(float(i + j + k)) ) <= eps

for i in range(nodes):
for j in range(nodes):
for k in range(nodes):
abs( friends[str(i)][j].pop(str(k)) - sin(float(i + j + k)) ) <= eps
friends[str(i)].pop(j)
friends.pop(str(i))

test_dict()
59 changes: 34 additions & 25 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1167,18 +1167,22 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
this->visit_expr(*x.m_args[i]);
llvm::Value* item = tmp;
llvm::Value* pos = llvm::ConstantInt::get(context, llvm::APInt(32, i));
list_api->write_item(const_list, pos, item, list_type->m_type, *module);
list_api->write_item(const_list, pos, item, list_type->m_type, module.get());
}
ptr_loads = ptr_loads_copy;
tmp = const_list;
}

void set_dict_api(ASR::Dict_t* dict_type) {
if( ASR::is_a<ASR::Character_t>(*dict_type->m_key_type) ) {
llvm_utils->dict_api = dict_api_sc.get();
} else {
llvm_utils->dict_api = dict_api_lp.get();
}
// if( llvm_utils->dict_api != nullptr ) {
// return ;
// }
// if( ASR::is_a<ASR::Character_t>(*dict_type->m_key_type) ) {
// llvm_utils->dict_api = dict_api_sc.get();
// } else {
// llvm_utils->dict_api = dict_api_lp.get();
// }
llvm_utils->dict_api = dict_api_lp.get();
}

void visit_DictConstant(const ASR::DictConstant_t& x) {
Expand Down Expand Up @@ -1267,7 +1271,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
llvm::Value *item = tmp;
ptr_loads = ptr_loads_copy;

list_api->append(plist, item, asr_list->m_type, *module);
list_api->append(plist, item, asr_list->m_type, module.get());
}

void visit_ListItem(const ASR::ListItem_t& x) {
Expand Down Expand Up @@ -1303,7 +1307,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>

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

void visit_DictPop(const ASR::DictPop_t& x) {
Expand Down Expand Up @@ -1370,7 +1375,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
llvm::Value *item = tmp;
ptr_loads = ptr_loads_copy;

list_api->insert_item(plist, pos, item, asr_list->m_type, *module);
list_api->insert_item(plist, pos, item, asr_list->m_type, module.get());
}

void visit_DictInsert(const ASR::DictInsert_t& x) {
Expand Down Expand Up @@ -1864,27 +1869,29 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
return a_kind;
}

llvm::Type* get_dict_type(ASR::ttype_t* asr_type) {
llvm::Type* get_dict_type(ASR::ttype_t* asr_type, bool is_nested_call=false) {
ASR::Dict_t* asr_dict = ASR::down_cast<ASR::Dict_t>(asr_type);
if( !is_nested_call ) {
set_dict_api(asr_dict);
}
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);
is_local_array_type, is_local_malloc_array_type,
is_local_list, local_m_dims, local_n_dims,
local_a_kind, true);
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);
local_a_kind, true);
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);
set_dict_api(asr_dict);
return llvm_utils->dict_api->get_dict_type(key_type_code, value_type_code, key_type_size,
value_type_size, key_llvm_type, value_llvm_type);
}
Expand All @@ -1893,7 +1900,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
ASR::storage_typeType m_storage,
bool& is_array_type, bool& is_malloc_array_type,
bool& is_list, ASR::dimension_t*& m_dims,
int& n_dims, int& a_kind) {
int& n_dims, int& a_kind, bool is_nested_call=false) {
llvm::Type* llvm_type = nullptr;
switch (asr_type->type) {
case (ASR::ttypeType::Integer) : {
Expand Down Expand Up @@ -2013,7 +2020,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
ASR::ttype_t *t2 = ASR::down_cast<ASR::Pointer_t>(asr_type)->m_type;
llvm_type = get_type_from_ttype_t(t2, m_storage, is_array_type,
is_malloc_array_type, is_list, m_dims,
n_dims, a_kind);
n_dims, a_kind, is_nested_call);
llvm_type = llvm_type->getPointerTo();
break;
}
Expand All @@ -2023,7 +2030,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
llvm::Type* el_llvm_type = get_type_from_ttype_t(asr_list->m_type, m_storage,
is_array_type, is_malloc_array_type,
is_list, m_dims, n_dims,
a_kind);
a_kind, is_nested_call);
std::string el_type_code = ASRUtils::get_type_code(asr_list->m_type);
int32_t type_size = -1;
if( LLVM::is_llvm_struct(asr_list->m_type) ||
Expand All @@ -2038,7 +2045,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
break;
}
case (ASR::ttypeType::Dict): {
llvm_type = get_dict_type(asr_type);
llvm_type = get_dict_type(asr_type, is_nested_call);
break;
}
case (ASR::ttypeType::Tuple) : {
Expand All @@ -2055,7 +2062,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
ASR::storage_typeType local_m_storage = ASR::storage_typeType::Default;
llvm_el_types.push_back(get_type_from_ttype_t(asr_tuple->m_type[i], local_m_storage,
is_local_array_type, is_local_malloc_array_type,
is_local_list, local_m_dims, local_n_dims, local_a_kind));
is_local_list, local_m_dims, local_n_dims, local_a_kind,
is_nested_call));
}
llvm_type = tuple_api->get_tuple_type(type_code, llvm_el_types);
break;
Expand Down Expand Up @@ -3191,7 +3199,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
ASRUtils::expr_type(x.m_value));
std::string value_type_code = ASRUtils::get_type_code(value_asr_list->m_type);
list_api->list_deepcopy(value_list, target_list,
value_asr_list, *module);
value_asr_list, module.get());
return ;
} else if( is_target_tuple && is_value_tuple ) {
uint64_t ptr_loads_copy = ptr_loads;
Expand Down Expand Up @@ -3220,7 +3228,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
llvm::Value* llvm_tuple_i = builder->CreateAlloca(llvm_tuple_i_type, nullptr);
ptr_loads = !LLVM::is_llvm_struct(asr_tuple_i_type);
visit_expr(*asr_value_tuple->m_elements[i]);
llvm_utils->deepcopy(tmp, llvm_tuple_i, asr_tuple_i_type, *module);
llvm_utils->deepcopy(tmp, llvm_tuple_i, asr_tuple_i_type, module.get());
src_deepcopies.push_back(al, llvm_tuple_i);
}
ASR::TupleConstant_t* asr_target_tuple = ASR::down_cast<ASR::TupleConstant_t>(x.m_target);
Expand All @@ -3244,7 +3252,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
std::string type_code = ASRUtils::get_type_code(value_tuple_type->m_type,
value_tuple_type->n_type);
tuple_api->tuple_deepcopy(value_tuple, target_tuple,
value_tuple_type, *module);
value_tuple_type, module.get());
}
return ;
} else if( is_target_dict && is_value_dict ) {
Expand Down Expand Up @@ -3277,7 +3285,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
if( x.m_target->type == ASR::exprType::ArrayItem ||
x.m_target->type == ASR::exprType::ArraySection ||
x.m_target->type == ASR::exprType::DerivedRef ||
x.m_target->type == ASR::exprType::ListItem ) {
x.m_target->type == ASR::exprType::ListItem ||
x.m_target->type == ASR::exprType::DictItem ) {
is_assignment_target = true;
this->visit_expr(*x.m_target);
is_assignment_target = false;
Expand Down Expand Up @@ -5295,7 +5304,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}
if( ASR::is_a<ASR::Tuple_t>(*arg_type) ||
ASR::is_a<ASR::List_t>(*arg_type) ) {
llvm_utils->deepcopy(value, target, arg_type, *module);
llvm_utils->deepcopy(value, target, arg_type, module.get());
} else {
builder->CreateStore(value, target);
}
Expand Down
Loading