Skip to content

Commit e21a0a9

Browse files
committed
Use default value in LLVM and enable tests
1 parent f91c43c commit e21a0a9

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ RUN(NAME test_dict_07 LABELS cpython llvm)
294294
RUN(NAME test_dict_08 LABELS cpython llvm c)
295295
RUN(NAME test_dict_09 LABELS cpython llvm c)
296296
RUN(NAME test_dict_10 LABELS cpython llvm) # TODO: Add support of dict with string in C backend
297-
RUN(NAME test_dict_11 LABELS cpython c) # TODO: Add LLVM support
297+
RUN(NAME test_dict_11 LABELS cpython llvm c)
298298
RUN(NAME test_for_loop LABELS cpython llvm c)
299299
RUN(NAME modules_01 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
300300
RUN(NAME modules_02 LABELS cpython llvm c wasm wasm_x86 wasm_x64)

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,10 +1823,20 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
18231823
ptr_loads = !LLVM::is_llvm_struct(dict_type->m_key_type);
18241824
this->visit_expr_wrapper(x.m_key, true);
18251825
ptr_loads = ptr_loads_copy;
1826-
llvm::Value *key = tmp;
1827-
1826+
llvm::Value *key = tmp, *def_value;
1827+
llvm::Type *val_type = get_type_from_ttype_t_util(dict_type->m_value_type);
1828+
llvm::Value *def_value_ptr = builder->CreateAlloca(val_type, nullptr);
1829+
if (x.m_default) {
1830+
ptr_loads = !LLVM::is_llvm_struct(dict_type->m_value_type);
1831+
this->visit_expr_wrapper(x.m_default, true);
1832+
ptr_loads = ptr_loads_copy;
1833+
def_value = tmp;
1834+
} else {
1835+
def_value = llvm::Constant::getNullValue(val_type);
1836+
}
1837+
builder->CreateStore(def_value, def_value_ptr);
18281838
set_dict_api(dict_type);
1829-
tmp = llvm_utils->dict_api->read_item(pdict, key, *module, dict_type,
1839+
tmp = llvm_utils->dict_api->read_item(pdict, key, *module, dict_type, def_value_ptr,
18301840
LLVM::is_llvm_struct(dict_type->m_value_type));
18311841
}
18321842

@@ -3664,20 +3674,20 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
36643674
bool is_list = false;
36653675
ASR::dimension_t* m_dims = nullptr;
36663676
llvm::Type* key_llvm_type = get_type_from_ttype_t(asr_dict->m_key_type, m_storage,
3667-
is_array_type,
3677+
is_array_type,
36683678
is_malloc_array_type,
36693679
is_list, m_dims, n_dims,
36703680
a_kind, m_abi);
36713681
llvm::Type* value_llvm_type = get_type_from_ttype_t(asr_dict->m_value_type, m_storage,
3672-
is_array_type,
3682+
is_array_type,
36733683
is_malloc_array_type,
36743684
is_list, m_dims, n_dims,
36753685
a_kind, m_abi);
36763686
int32_t key_type_size = get_type_size(asr_dict->m_key_type, key_llvm_type, a_kind);
36773687
int32_t value_type_size = get_type_size(asr_dict->m_value_type, value_llvm_type, a_kind);
36783688
set_dict_api(asr_dict);
3679-
type = llvm_utils->dict_api->get_dict_type(key_type_code, value_type_code,
3680-
key_type_size, value_type_size,
3689+
type = llvm_utils->dict_api->get_dict_type(key_type_code, value_type_code,
3690+
key_type_size, value_type_size,
36813691
key_llvm_type, value_llvm_type)->getPointerTo();
36823692
break;
36833693
}
@@ -4128,7 +4138,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
41284138
ASR::Dict_t* asr_dict = ASR::down_cast<ASR::Dict_t>(return_var_type0);
41294139
std::string key_type_code = ASRUtils::get_type_code(asr_dict->m_key_type);
41304140
std::string value_type_code = ASRUtils::get_type_code(asr_dict->m_value_type);
4131-
4141+
41324142
bool is_local_array_type = false, is_local_malloc_array_type = false;
41334143
bool is_local_list = false;
41344144
ASR::dimension_t* local_m_dims = nullptr;

0 commit comments

Comments
 (0)