Skip to content
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

Free up memory at the end of scope #2798

Closed
wants to merge 3 commits into from
Closed
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
58 changes: 58 additions & 0 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
std::unique_ptr<LLVMSetInterface> set_api_sc;
std::unique_ptr<LLVMArrUtils::Descriptor> arr_descr;
std::vector<llvm::Value*> heap_arrays;
std::map<llvm::Value*, ASR::ttype_t*> lists;
std::map<llvm::Value*, ASR::ttype_t*> dictionaries;
std::map<llvm::Value*, ASR::ttype_t*> sets;
std::map<llvm::Value*, llvm::Value*> strings_to_be_allocated; // (array, size)
Vec<llvm::Value*> strings_to_be_deallocated;

Expand Down Expand Up @@ -1120,6 +1123,18 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
// }
}

inline void free_heap_structures() {
for (auto &value : lists) {
llvm_utils->free_data(value.first, value.second, module.get());
}
for (auto &value : dictionaries) {
llvm_utils->free_data(value.first, value.second, module.get());
}
for (auto &value : sets) {
llvm_utils->free_data(value.first, value.second, module.get());
}
}

llvm::Function* _Allocate(bool realloc_lhs) {
std::string func_name = "_lfortran_alloc";
if( realloc_lhs ) {
Expand Down Expand Up @@ -1274,6 +1289,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
llvm::Type* const_list_type = list_api->get_list_type(llvm_el_type, type_code, type_size);
llvm::Value* const_list = builder0.CreateAlloca(const_list_type, nullptr, "const_list");
list_api->list_init(type_code, const_list, *module, x.n_args, x.n_args);
lists[const_list] = x.m_type;
int64_t ptr_loads_copy = ptr_loads;
for( size_t i = 0; i < x.n_args; i++ ) {
if (is_argument_of_type_CPtr(x.m_args[i])) {
Expand All @@ -1300,6 +1316,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
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);
llvm_utils->dict_api->dict_init(key_type_code, value_type_code, const_dict, module.get(), x.n_keys);
dictionaries[const_dict] = x.m_type;
int64_t ptr_loads_key = !LLVM::is_llvm_struct(x_dict->m_key_type);
int64_t ptr_loads_value = !LLVM::is_llvm_struct(x_dict->m_value_type);
int64_t ptr_loads_copy = ptr_loads;
Expand All @@ -1325,6 +1342,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
llvm_utils->set_set_api(x_set);
std::string el_type_code = ASRUtils::get_type_code(x_set->m_type);
llvm_utils->set_api->set_init(el_type_code, const_set, module.get(), x.n_elements);
sets[const_set] = x.m_type;
int64_t ptr_loads_el = !LLVM::is_llvm_struct(x_set->m_type);
int64_t ptr_loads_copy = ptr_loads;
ptr_loads = ptr_loads_el;
Expand Down Expand Up @@ -2270,6 +2288,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
ptr_loads = 1;
list_api->list_repeat_copy(repeat_list, left, right, left_len, module.get());
ptr_loads = ptr_loads_copy;
lists[repeat_list] = x.m_type;
tmp = repeat_list;
}

Expand Down Expand Up @@ -3185,6 +3204,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
loop_or_block_end.clear();
loop_or_block_end_names.clear();
heap_arrays.clear();
lists.clear();
dictionaries.clear();
sets.clear();
strings_to_be_deallocated.reserve(al, 1);
SymbolTable* current_scope_copy = current_scope;
current_scope = x.m_symtab;
Expand Down Expand Up @@ -3260,6 +3282,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
LLVM::lfortran_free(context, *module, *builder, value);
}
call_lcompilers_free_strings();
free_heap_structures();

llvm::Value *ret_val2 = llvm::ConstantInt::get(context,
llvm::APInt(32, 0));
Expand All @@ -3277,6 +3300,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
loop_or_block_end.clear();
loop_or_block_end_names.clear();
heap_arrays.clear();
lists.clear();
dictionaries.clear();
sets.clear();
strings_to_be_deallocated.reserve(al, 1);
}

Expand Down Expand Up @@ -3767,6 +3793,14 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}
}

if (ASR::is_a<ASR::List_t>(*v->m_type)) {
lists[ptr] = v->m_type;
} else if (ASR::is_a<ASR::Dict_t>(*v->m_type)) {
dictionaries[ptr] = v->m_type;
} else if (ASR::is_a<ASR::Set_t>(*v->m_type)) {
sets[ptr] = v->m_type;
}

llvm_symtab[h] = ptr;
if( (ASRUtils::is_array(v->m_type) &&
((ASRUtils::extract_physical_type(v->m_type) == ASR::array_physical_typeType::DescriptorArray) ||
Expand Down Expand Up @@ -3955,6 +3989,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
loop_or_block_end.clear();
loop_or_block_end_names.clear();
heap_arrays.clear();
lists.clear();
dictionaries.clear();
sets.clear();
strings_to_be_deallocated.reserve(al, 1);
SymbolTable* current_scope_copy = current_scope;
current_scope = x.m_symtab;
Expand Down Expand Up @@ -3989,6 +4026,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
loop_or_block_end.clear();
loop_or_block_end_names.clear();
heap_arrays.clear();
sets.clear();
dictionaries.clear();
lists.clear();
strings_to_be_deallocated.reserve(al, 1);
}

Expand Down Expand Up @@ -4139,13 +4179,18 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
ret_val2 = tmp;
}
}
lists.erase(ret_val);
dictionaries.erase(ret_val);
sets.erase(ret_val);
free_heap_structures();
for( auto& value: heap_arrays ) {
LLVM::lfortran_free(context, *module, *builder, value);
}
call_lcompilers_free_strings();
builder->CreateRet(ret_val2);
} else {
start_new_block(proc_return);
free_heap_structures();
for( auto& value: heap_arrays ) {
LLVM::lfortran_free(context, *module, *builder, value);
}
Expand Down Expand Up @@ -5416,6 +5461,15 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
std::vector<llvm::Value*> heap_arrays_copy;
heap_arrays_copy = heap_arrays;
heap_arrays.clear();
std::map<llvm::Value*, ASR::ttype_t*> lists_copy;
lists_copy = lists;
lists.clear();
std::map<llvm::Value*, ASR::ttype_t*> dictionaries_copy;
dictionaries_copy = dictionaries;
dictionaries.clear();
std::map<llvm::Value*, ASR::ttype_t*> sets_copy;
sets_copy = sets;
sets.clear();
if( x.m_label != -1 ) {
if( llvm_goto_targets.find(x.m_label) == llvm_goto_targets.end() ) {
llvm::BasicBlock *new_target = llvm::BasicBlock::Create(context, "goto_target");
Expand Down Expand Up @@ -5456,7 +5510,11 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
for( auto& value: heap_arrays ) {
LLVM::lfortran_free(context, *module, *builder, value);
}
free_heap_structures();
heap_arrays = heap_arrays_copy;
lists = lists_copy;
dictionaries = dictionaries_copy;
sets = sets_copy;
if (block_terminator == nullptr) {
// The previous block is not terminated --- terminate it by jumping
// to blockend
Expand Down
Loading