diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index 0d92f7377b..85a447825c 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -533,6 +533,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor std::string runtime_func_name, llvm::Type* complex_type=nullptr) { + get_builder0() if( complex_type == nullptr ) { complex_type = complex_type_4; } @@ -548,14 +549,14 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor llvm::Function::ExternalLinkage, runtime_func_name, *module); } - llvm::AllocaInst *pleft_arg = builder->CreateAlloca(complex_type, + llvm::AllocaInst *pleft_arg = builder0.CreateAlloca(complex_type, nullptr); builder->CreateStore(left_arg, pleft_arg); - llvm::AllocaInst *pright_arg = builder->CreateAlloca(complex_type, + llvm::AllocaInst *pright_arg = builder0.CreateAlloca(complex_type, nullptr); builder->CreateStore(right_arg, pright_arg); - llvm::AllocaInst *presult = builder->CreateAlloca(complex_type, + llvm::AllocaInst *presult = builder0.CreateAlloca(complex_type, nullptr); std::vector args = {pleft_arg, pright_arg, presult}; builder->CreateCall(fn, args); @@ -566,6 +567,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor llvm::Value* lfortran_strop(llvm::Value* left_arg, llvm::Value* right_arg, std::string runtime_func_name) { + get_builder0() llvm::Function *fn = module->getFunction(runtime_func_name); if (!fn) { llvm::FunctionType *function_type = llvm::FunctionType::get( @@ -577,13 +579,13 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor fn = llvm::Function::Create(function_type, llvm::Function::ExternalLinkage, runtime_func_name, *module); } - llvm::AllocaInst *pleft_arg = builder->CreateAlloca(character_type, + llvm::AllocaInst *pleft_arg = builder0.CreateAlloca(character_type, nullptr); builder->CreateStore(left_arg, pleft_arg); - llvm::AllocaInst *pright_arg = builder->CreateAlloca(character_type, + llvm::AllocaInst *pright_arg = builder0.CreateAlloca(character_type, nullptr); builder->CreateStore(right_arg, pright_arg); - llvm::AllocaInst *presult = builder->CreateAlloca(character_type, + llvm::AllocaInst *presult = builder0.CreateAlloca(character_type, nullptr); std::vector args = {pleft_arg, pright_arg, presult}; builder->CreateCall(fn, args); @@ -594,6 +596,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor llvm::Value* lfortran_str_cmp(llvm::Value* left_arg, llvm::Value* right_arg, std::string runtime_func_name) { + get_builder0() llvm::Function *fn = module->getFunction(runtime_func_name); if(!fn) { llvm::FunctionType *function_type = llvm::FunctionType::get( @@ -604,10 +607,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor fn = llvm::Function::Create(function_type, llvm::Function::ExternalLinkage, runtime_func_name, *module); } - llvm::AllocaInst *pleft_arg = builder->CreateAlloca(character_type, + llvm::AllocaInst *pleft_arg = builder0.CreateAlloca(character_type, nullptr); builder->CreateStore(left_arg, pleft_arg); - llvm::AllocaInst *pright_arg = builder->CreateAlloca(character_type, + llvm::AllocaInst *pright_arg = builder0.CreateAlloca(character_type, nullptr); builder->CreateStore(right_arg, pright_arg); std::vector args = {pleft_arg, pright_arg}; @@ -616,6 +619,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor llvm::Value* lfortran_strrepeat(llvm::Value* left_arg, llvm::Value* right_arg) { + get_builder0() std::string runtime_func_name = "_lfortran_strrepeat"; llvm::Function *fn = module->getFunction(runtime_func_name); if (!fn) { @@ -628,10 +632,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor fn = llvm::Function::Create(function_type, llvm::Function::ExternalLinkage, runtime_func_name, *module); } - llvm::AllocaInst *pleft_arg = builder->CreateAlloca(character_type, + llvm::AllocaInst *pleft_arg = builder0.CreateAlloca(character_type, nullptr); builder->CreateStore(left_arg, pleft_arg); - llvm::AllocaInst *presult = builder->CreateAlloca(character_type, + llvm::AllocaInst *presult = builder0.CreateAlloca(character_type, nullptr); std::vector args = {pleft_arg, right_arg, presult}; builder->CreateCall(fn, args); @@ -785,13 +789,14 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor // float complex_re(complex a) // And it extracts the real part of the complex number llvm::Value *complex_re(llvm::Value *c, llvm::Type* complex_type=nullptr) { + get_builder0() if( complex_type == nullptr ) { complex_type = complex_type_4; } if( c->getType()->isPointerTy() ) { c = CreateLoad(c); } - llvm::AllocaInst *pc = builder->CreateAlloca(complex_type, nullptr); + llvm::AllocaInst *pc = builder0.CreateAlloca(complex_type, nullptr); builder->CreateStore(c, pc); std::vector idx = { llvm::ConstantInt::get(context, llvm::APInt(32, 0)), @@ -801,10 +806,11 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } llvm::Value *complex_im(llvm::Value *c, llvm::Type* complex_type=nullptr) { + get_builder0() if( complex_type == nullptr ) { complex_type = complex_type_4; } - llvm::AllocaInst *pc = builder->CreateAlloca(complex_type, nullptr); + llvm::AllocaInst *pc = builder0.CreateAlloca(complex_type, nullptr); builder->CreateStore(c, pc); std::vector idx = { llvm::ConstantInt::get(context, llvm::APInt(32, 0)), @@ -815,10 +821,11 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor llvm::Value *complex_from_floats(llvm::Value *re, llvm::Value *im, llvm::Type* complex_type=nullptr) { + get_builder0() if( complex_type == nullptr ) { complex_type = complex_type_4; } - llvm::AllocaInst *pres = builder->CreateAlloca(complex_type, nullptr); + llvm::AllocaInst *pres = builder0.CreateAlloca(complex_type, nullptr); std::vector idx1 = { llvm::ConstantInt::get(context, llvm::APInt(32, 0)), llvm::ConstantInt::get(context, llvm::APInt(32, 0))}; @@ -852,8 +859,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor */ llvm::Value* lfortran_intrinsic(llvm::Function *fn, llvm::Value* pa, int a_kind) { + get_builder0() llvm::Type *presult_type = llvm_utils->getFPType(a_kind); - llvm::AllocaInst *presult = builder->CreateAlloca(presult_type, nullptr); + llvm::AllocaInst *presult = builder0.CreateAlloca(presult_type, nullptr); llvm::Value *a = CreateLoad(pa); std::vector args = {a, presult}; builder->CreateCall(fn, args); @@ -1061,8 +1069,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } inline void call_lfortran_free(llvm::Function* fn, llvm::Type* llvm_data_type) { + get_builder0() llvm::Value* arr = CreateLoad(arr_descr->get_pointer_to_data(tmp)); - llvm::AllocaInst *arg_arr = builder->CreateAlloca(character_type, nullptr); + llvm::AllocaInst *arg_arr = builder0.CreateAlloca(character_type, nullptr); builder->CreateStore(builder->CreateBitCast(arr, character_type), arg_arr); std::vector args = {CreateLoad(arg_arr)}; builder->CreateCall(fn, args); @@ -1113,6 +1122,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor template void visit_Deallocate(const T& x) { + get_builder0() llvm::Function* free_fn = _Deallocate(); for( size_t i = 0; i < x.n_vars; i++ ) { const ASR::expr_t* tmp_expr = x.m_vars[i]; @@ -1223,6 +1233,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_ListConstant(const ASR::ListConstant_t& x) { + get_builder0() ASR::List_t* list_type = ASR::down_cast(x.m_type); bool is_array_type_local = false, is_malloc_array_type_local = false; bool is_list_local = false; @@ -1243,7 +1254,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor type_size = ASRUtils::extract_kind_from_ttype_t(list_type->m_type); } llvm::Type* const_list_type = list_api->get_list_type(llvm_el_type, type_code, type_size); - llvm::Value* const_list = builder->CreateAlloca(const_list_type, nullptr, "const_list"); + 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); int64_t ptr_loads_copy = ptr_loads; for( size_t i = 0; i < x.n_args; i++ ) { @@ -1263,8 +1274,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_DictConstant(const ASR::DictConstant_t& x) { + get_builder0() llvm::Type* const_dict_type = llvm_utils->get_dict_type(x.m_type, module.get()); - llvm::Value* const_dict = builder->CreateAlloca(const_dict_type, nullptr, "const_dict"); + llvm::Value* const_dict = builder0.CreateAlloca(const_dict_type, nullptr, "const_dict"); ASR::Dict_t* x_dict = ASR::down_cast(x.m_type); llvm_utils->set_dict_api(x_dict); std::string key_type_code = ASRUtils::get_type_code(x_dict->m_key_type); @@ -1288,8 +1300,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_SetConstant(const ASR::SetConstant_t& x) { + get_builder0() llvm::Type* const_set_type = llvm_utils->get_set_type(x.m_type, module.get()); - llvm::Value* const_set = builder->CreateAlloca(const_set_type, nullptr, "const_set"); + llvm::Value* const_set = builder0.CreateAlloca(const_set_type, nullptr, "const_set"); ASR::Set_t* x_set = ASR::down_cast(x.m_type); llvm_utils->set_set_api(x_set); std::string el_type_code = ASRUtils::get_type_code(x_set->m_type); @@ -1308,6 +1321,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_TupleConstant(const ASR::TupleConstant_t& x) { + get_builder0() ASR::Tuple_t* tuple_type = ASR::down_cast(x.m_type); std::string type_code = ASRUtils::get_type_code(tuple_type->m_type, tuple_type->n_type); @@ -1323,7 +1337,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor is_list, m_dims, n_dims, a_kind, module.get())); } llvm::Type* const_tuple_type = tuple_api->get_tuple_type(type_code, llvm_el_types); - llvm::Value* const_tuple = builder->CreateAlloca(const_tuple_type, nullptr, "const_tuple"); + llvm::Value* const_tuple = builder0.CreateAlloca(const_tuple_type, nullptr, "const_tuple"); std::vector init_values; int64_t ptr_loads_copy = ptr_loads; for( size_t i = 0; i < x.n_elements; i++ ) { @@ -1541,6 +1555,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_DictItem(const ASR::DictItem_t& x) { + get_builder0() ASR::Dict_t* dict_type = ASR::down_cast( ASRUtils::expr_type(x.m_a)); int64_t ptr_loads_copy = ptr_loads; @@ -1554,7 +1569,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor llvm::Value *key = tmp; if (x.m_default) { llvm::Type *val_type = llvm_utils->get_type_from_ttype_t_util(dict_type->m_value_type, module.get()); - llvm::Value *def_value_ptr = builder->CreateAlloca(val_type, nullptr); + llvm::Value *def_value_ptr = builder0.CreateAlloca(val_type, nullptr); ptr_loads = !LLVM::is_llvm_struct(dict_type->m_value_type); this->visit_expr_wrapper(x.m_default, true); ptr_loads = ptr_loads_copy; @@ -1929,6 +1944,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void generate_DictElems(ASR::expr_t* m_arg, bool key_or_value) { + get_builder0() ASR::Dict_t* dict_type = ASR::down_cast( ASRUtils::expr_type(m_arg)); ASR::ttype_t* el_type = key_or_value == 0 ? @@ -1960,7 +1976,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor type_size = ASRUtils::extract_kind_from_ttype_t(el_type); } llvm::Type* el_list_type = list_api->get_list_type(llvm_el_type, type_code, type_size); - llvm::Value* el_list = builder->CreateAlloca(el_list_type, nullptr, key_or_value == 0 ? + llvm::Value* el_list = builder0.CreateAlloca(el_list_type, nullptr, key_or_value == 0 ? "keys_list" : "values_list"); list_api->list_init(type_code, el_list, *module, 0, 0); @@ -2199,6 +2215,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_ListRepeat(const ASR::ListRepeat_t& x) { + get_builder0() this->visit_expr_wrapper(x.m_left, true); llvm::Value *left = tmp; ptr_loads = 2; // right is int always @@ -2225,7 +2242,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor type_size = ASRUtils::extract_kind_from_ttype_t(list_type->m_type); } llvm::Type* repeat_list_type = list_api->get_list_type(llvm_el_type, type_code, type_size); - llvm::Value* repeat_list = builder->CreateAlloca(repeat_list_type, nullptr, "repeat_list"); + llvm::Value* repeat_list = builder0.CreateAlloca(repeat_list_type, nullptr, "repeat_list"); llvm::Value* left_len = list_api->len(left); llvm::Value* capacity = builder->CreateMul(left_len, right); list_api->list_init(type_code, repeat_list, *module, @@ -2289,6 +2306,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_TupleConcat(const ASR::TupleConcat_t& x) { + get_builder0() int64_t ptr_loads_copy = ptr_loads; ptr_loads = 0; this->visit_expr(*x.m_left); @@ -2329,7 +2347,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor v_type.push_back(al, tuple_type_right->m_type[i]); } llvm::Type* concat_tuple_type = tuple_api->get_tuple_type(type_code, llvm_el_types); - llvm::Value* concat_tuple = builder->CreateAlloca(concat_tuple_type, nullptr, "concat_tuple"); + llvm::Value* concat_tuple = builder0.CreateAlloca(concat_tuple_type, nullptr, "concat_tuple"); ASR::Tuple_t* tuple_type = (ASR::Tuple_t*)(ASR::make_Tuple_t( al, x.base.base.loc, v_type.p, v_type.n)); tuple_api->concat(left, right, tuple_type_left, tuple_type_right, concat_tuple, @@ -2338,6 +2356,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_ArrayItem(const ASR::ArrayItem_t& x) { + get_builder0() if (x.m_value) { this->visit_expr_wrapper(x.m_value, true); return; @@ -2464,6 +2483,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_ArraySection(const ASR::ArraySection_t& x) { + get_builder0() if (x.m_value) { this->visit_expr_wrapper(x.m_value, true); return; @@ -2527,7 +2547,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor llvm::IRBuilder<> builder0(context); builder0.SetInsertPoint(&entry_block, entry_block.getFirstInsertionPt()); llvm::Type* target_type = llvm_utils->get_type_from_ttype_t_util(x_m_array_type, module.get()); - llvm::Value *target = builder0.CreateAlloca( + llvm::Value *target = builder->CreateAlloca( target_type, nullptr, "fixed_size_reshaped_array"); llvm::Value* target_ = llvm_utils->create_gep(target, 0); ASR::dimension_t* asr_dims = nullptr; @@ -3464,6 +3484,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor template void declare_vars(const T &x, bool create_vtabs=true) { + get_builder0() llvm::Value *target_var; uint32_t debug_arg_count = 0; std::vector var_order = ASRUtils::determine_variable_declaration_order(x.m_symtab); @@ -4244,6 +4265,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor void visit_CPtrToPointer(const ASR::CPtrToPointer_t& x) { + get_builder0() ASR::expr_t *cptr = x.m_cptr, *fptr = x.m_ptr, *shape = x.m_shape; int reduce_loads = 0; if( ASR::is_a(*cptr) ) { @@ -4269,7 +4291,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor ASR::ttype_t* fptr_type = ASRUtils::expr_type(fptr); llvm::Type* llvm_fptr_type = llvm_utils->get_type_from_ttype_t_util( ASRUtils::get_contained_type(fptr_type), module.get()); - llvm::Value* fptr_array = builder->CreateAlloca(llvm_fptr_type); + llvm::Value* fptr_array = builder0.CreateAlloca(llvm_fptr_type); builder->CreateStore(llvm::ConstantInt::get(context, llvm::APInt(32, 0)), arr_descr->get_offset(fptr_array, false)); ASR::dimension_t* fptr_dims; @@ -4277,7 +4299,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor ASRUtils::expr_type(fptr), fptr_dims); llvm::Value* llvm_rank = llvm::ConstantInt::get(context, llvm::APInt(32, fptr_rank)); - llvm::Value* dim_des = builder->CreateAlloca(arr_descr->get_dimension_descriptor_type(), llvm_rank); + llvm::Value* dim_des = builder0.CreateAlloca(arr_descr->get_dimension_descriptor_type(), llvm_rank); builder->CreateStore(dim_des, arr_descr->get_pointer_to_dimension_descriptor_array(fptr_array, false)); arr_descr->set_rank(fptr_array, llvm_rank); builder->CreateStore(fptr_array, llvm_fptr); @@ -4498,6 +4520,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_Associate(const ASR::Associate_t& x) { + get_builder0() if( ASR::is_a(*x.m_value) ) { handle_array_section_association_to_pointer(x); } else { @@ -4572,7 +4595,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor llvm_value = llvm_utils->create_gep(llvm_value, 0); } llvm::Type* llvm_target_type = llvm_utils->get_type_from_ttype_t_util(target_type_, module.get()); - llvm::Value* llvm_target_ = builder->CreateAlloca(llvm_target_type); + llvm::Value* llvm_target_ = builder0.CreateAlloca(llvm_target_type); ASR::dimension_t* m_dims = nullptr; size_t n_dims = ASRUtils::extract_dimensions_from_ttype(value_type, m_dims); ASR::ttype_t* data_type = ASRUtils::duplicate_type_without_dims( @@ -4688,6 +4711,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_Assignment(const ASR::Assignment_t &x) { + get_builder0() if (compiler_options.emit_debug_info) debug_emit_loc(x); if( x.m_overloaded ) { this->visit_stmt(*x.m_overloaded); @@ -4754,7 +4778,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor for( size_t i = 0; i < asr_value_tuple->n_elements; i++ ) { ASR::ttype_t* asr_tuple_i_type = ASRUtils::expr_type(asr_value_tuple->m_elements[i]); llvm::Type* llvm_tuple_i_type = llvm_utils->get_type_from_ttype_t_util(asr_tuple_i_type, module.get()); - llvm::Value* llvm_tuple_i = builder->CreateAlloca(llvm_tuple_i_type, nullptr); + llvm::Value* llvm_tuple_i = builder0.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.get(), name2memidx); @@ -5926,6 +5950,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_ForEach(const ASR::ForEach_t &x) { + get_builder0() llvm::Value **strings_to_be_deallocated_copy = strings_to_be_deallocated.p; size_t n = strings_to_be_deallocated.n; strings_to_be_deallocated.reserve(al, 1); @@ -5948,7 +5973,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor llvm::Value *key_mask = LLVM::CreateLoad(*builder, llvm_utils->dict_api->get_pointer_to_keymask(pcontainer)); llvm::Value *key_list = llvm_utils->dict_api->get_key_list(pcontainer); - llvm::AllocaInst *idx_ptr = builder->CreateAlloca( + llvm::AllocaInst *idx_ptr = builder0.CreateAlloca( llvm::Type::getInt32Ty(context), nullptr); LLVM::CreateStore(*builder, llvm::ConstantInt::get( llvm::Type::getInt32Ty(context), llvm::APInt(32, 0)), idx_ptr); @@ -5958,7 +5983,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor llvm_utils->dict_api->get_pointer_to_key_value_pairs(pcontainer)); llvm::Type* kv_pair_type = llvm_utils->dict_api->get_key_value_pair_type(key_type, dict_type->m_value_type); - llvm::AllocaInst *chain_itr = builder->CreateAlloca( + llvm::AllocaInst *chain_itr = builder0.CreateAlloca( llvm::Type::getInt8PtrTy(context), nullptr); create_loop(nullptr, [=](){ @@ -6058,7 +6083,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor ASRUtils::expr_type(x.m_container)); ASR::ttype_t *el_type = set_type->m_type; - llvm::AllocaInst *idx_ptr = builder->CreateAlloca( + llvm::AllocaInst *idx_ptr = builder0.CreateAlloca( llvm::Type::getInt32Ty(context), nullptr); LLVM::CreateStore(*builder, llvm::ConstantInt::get( llvm::Type::getInt32Ty(context), llvm::APInt(32, 0)), idx_ptr); @@ -6287,6 +6312,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_StringLen(const ASR::StringLen_t &x) { + get_builder0() if (x.m_value) { this->visit_expr_wrapper(x.m_value, true); return; @@ -6295,7 +6321,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor ptr_loads = 2 - LLVM::is_llvm_pointer(*ASRUtils::expr_type(x.m_arg)); this->visit_expr_wrapper(x.m_arg, true); ptr_loads = ptr_loads_copy; - llvm::AllocaInst *parg = builder->CreateAlloca(character_type, nullptr); + llvm::AllocaInst *parg = builder0.CreateAlloca(character_type, nullptr); builder->CreateStore(tmp, parg); ASR::ttype_t* arg_type = ASRUtils::get_contained_type(ASRUtils::expr_type(x.m_arg)); tmp = builder->CreateSExtOrTrunc( @@ -6304,12 +6330,13 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_StringOrd(const ASR::StringOrd_t &x) { + get_builder0() if (x.m_value) { this->visit_expr_wrapper(x.m_value, true); return; } this->visit_expr_wrapper(x.m_arg, true); - llvm::AllocaInst *parg = builder->CreateAlloca(character_type, nullptr); + llvm::AllocaInst *parg = builder0.CreateAlloca(character_type, nullptr); builder->CreateStore(tmp, parg); tmp = lfortran_str_ord(parg); } @@ -7177,6 +7204,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_ComplexIm(const ASR::ComplexIm_t &x) { + get_builder0() if (x.m_value) { this->visit_expr_wrapper(x.m_value, true); return; @@ -7195,13 +7223,13 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor runtime_func_name = "_lfortran_complex_aimag_32"; ret_type = llvm::Type::getFloatTy(context); complex_type = complex_type_4; - arg = builder->CreateAlloca(complex_type_4, + arg = builder0.CreateAlloca(complex_type_4, nullptr); } else { runtime_func_name = "_lfortran_complex_aimag_64"; ret_type = llvm::Type::getDoubleTy(context); complex_type = complex_type_8; - arg = builder->CreateAlloca(complex_type_8, + arg = builder0.CreateAlloca(complex_type_8, nullptr); } fn = module->getFunction(runtime_func_name); @@ -7216,7 +7244,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } this->visit_expr_wrapper(x.m_arg, true); builder->CreateStore(tmp, arg); - llvm::AllocaInst *result = builder->CreateAlloca(ret_type, nullptr); + llvm::AllocaInst *result = builder0.CreateAlloca(ret_type, nullptr); std::vector args = {arg, result}; builder->CreateCall(fn, args); tmp = CreateLoad(result); @@ -7238,6 +7266,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_Cast(const ASR::Cast_t &x) { + get_builder0() if (x.m_value) { this->visit_expr_wrapper(x.m_value, true); return; @@ -7354,13 +7383,13 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor break; } case (ASR::cast_kindType::CharacterToLogical) : { - llvm::AllocaInst *parg = builder->CreateAlloca(character_type, nullptr); + llvm::AllocaInst *parg = builder0.CreateAlloca(character_type, nullptr); builder->CreateStore(tmp, parg); tmp = builder->CreateICmpNE(lfortran_str_len(parg), builder->getInt32(0)); break; } case (ASR::cast_kindType::CharacterToInteger) : { - llvm::AllocaInst *parg = builder->CreateAlloca(character_type, nullptr); + llvm::AllocaInst *parg = builder0.CreateAlloca(character_type, nullptr); builder->CreateStore(tmp, parg); tmp = lfortran_str_to_int(parg); break; @@ -7729,6 +7758,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_FileRead(const ASR::FileRead_t &x) { + get_builder0() if( x.m_overloaded ) { this->visit_stmt(*x.m_overloaded); return ; @@ -7753,7 +7783,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor ptr_loads = ptr_copy; iostat = tmp; } else { - iostat = builder->CreateAlloca( + iostat = builder0.CreateAlloca( llvm::Type::getInt32Ty(context), nullptr); } @@ -7764,7 +7794,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor ptr_loads = ptr_copy; read_size = tmp; } else { - read_size = builder->CreateAlloca( + read_size = builder0.CreateAlloca( llvm::Type::getInt32Ty(context), nullptr); } @@ -7912,6 +7942,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_FileInquire(const ASR::FileInquire_t &x) { + get_builder0() llvm::Value *exist_val = nullptr, *f_name = nullptr, *unit = nullptr, *opened_val = nullptr; if (x.m_file) { @@ -7927,7 +7958,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor exist_val = tmp; ptr_loads = ptr_loads_copy; } else { - exist_val = builder->CreateAlloca( + exist_val = builder0.CreateAlloca( llvm::Type::getInt1Ty(context), nullptr); } @@ -7945,7 +7976,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor opened_val = tmp; ptr_loads = ptr_loads_copy; } else { - opened_val = builder->CreateAlloca( + opened_val = builder0.CreateAlloca( llvm::Type::getInt1Ty(context), nullptr); } @@ -8034,6 +8065,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_FileWrite(const ASR::FileWrite_t &x) { + get_builder0() if( x.m_overloaded ) { this->visit_stmt(*x.m_overloaded); return ; @@ -8076,7 +8108,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor ptr_loads = ptr_copy; iostat = tmp; } else { - iostat = builder->CreateAlloca( + iostat = builder0.CreateAlloca( llvm::Type::getInt32Ty(context), nullptr); } @@ -8434,6 +8466,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor template std::vector convert_call_args(const T &x, bool is_method) { + get_builder0() std::vector args; for (size_t i=0; i if( x.m_args[i].m_value == nullptr ) { LCOMPILERS_ASSERT(orig_arg != nullptr); llvm::Type* llvm_orig_arg_type = llvm_utils->get_type_from_ttype_t_util(orig_arg->m_type, module.get()); - llvm::Value* llvm_arg = builder->CreateAlloca(llvm_orig_arg_type); + llvm::Value* llvm_arg = builder0.CreateAlloca(llvm_orig_arg_type); args.push_back(llvm_arg); continue ; } @@ -8899,6 +8932,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor llvm::Value* convert_to_polymorphic_arg(llvm::Value* dt, ASR::ttype_t* s_m_args0_type, ASR::ttype_t* arg_type) { + get_builder0() if( !ASR::is_a(*ASRUtils::type_get_past_array(s_m_args0_type)) ) { return dt; } @@ -8906,10 +8940,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor if( ASRUtils::is_abstract_class_type(s_m_args0_type) ) { if( ASRUtils::is_array(s_m_args0_type) ) { llvm::Type* array_type = llvm_utils->get_type_from_ttype_t_util(s_m_args0_type, module.get()); - llvm::Value* abstract_array = builder->CreateAlloca(array_type); + llvm::Value* abstract_array = builder0.CreateAlloca(array_type); llvm::Type* array_data_type = llvm_utils->get_el_type( ASRUtils::type_get_past_array(s_m_args0_type), module.get()); - llvm::Value* array_data = builder->CreateAlloca(array_data_type); + llvm::Value* array_data = builder0.CreateAlloca(array_data_type); builder->CreateStore(array_data, arr_descr->get_pointer_to_data(abstract_array)); arr_descr->fill_array_details(dt, abstract_array, s_m_args0_type, true); @@ -8929,7 +8963,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor return abstract_array; } else { llvm::Type* _type = llvm_utils->get_type_from_ttype_t_util(s_m_args0_type, module.get()); - llvm::Value* abstract_ = builder->CreateAlloca(_type); + llvm::Value* abstract_ = builder0.CreateAlloca(_type); llvm::Value* polymorphic_addr = llvm_utils->create_gep(abstract_, 1); builder->CreateStore( builder->CreateBitCast(dt, llvm::Type::getVoidTy(context)->getPointerTo()), @@ -8949,7 +8983,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor type2vtab[struct_sym].find(current_scope) == type2vtab[struct_sym].end() ) { create_vtab_for_struct_type(struct_sym, current_scope); } - llvm::Value* dt_polymorphic = builder->CreateAlloca( + llvm::Value* dt_polymorphic = builder0.CreateAlloca( llvm_utils->getClassType(s_m_args0_type, true)); llvm::Value* hash_ptr = llvm_utils->create_gep(dt_polymorphic, 0); llvm::Value* hash = llvm::ConstantInt::get(llvm_utils->getIntType(8), llvm::APInt(64, get_class_hash(struct_sym))); @@ -8962,6 +8996,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_SubroutineCall(const ASR::SubroutineCall_t &x) { + get_builder0() if (compiler_options.emit_debug_info) debug_emit_loc(x); if( ASRUtils::is_intrinsic_optimization(x.m_name) ) { ASR::Function_t* routine = ASR::down_cast( @@ -9069,7 +9104,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor ASRUtils::type_get_past_pointer(ASRUtils::expr_type(s->m_args[0]))); } // Convert to polymorphic argument - dt_polymorphic = builder->CreateAlloca( + dt_polymorphic = builder0.CreateAlloca( llvm_utils->getClassType(s_m_args0_type, true)); llvm::Value* hash_ptr = llvm_utils->create_gep(dt_polymorphic, 0); llvm::Value* hash = llvm::ConstantInt::get( @@ -9252,13 +9287,14 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor llvm::Value* CreatePointerToStructTypeReturnValue(llvm::FunctionType* fnty, llvm::Value* return_value, ASR::ttype_t* asr_return_type) { + get_builder0() if( !LLVM::is_llvm_struct(asr_return_type) ) { return return_value; } // Call to LLVM APIs not needed to fetch the return type of the function. // We can use asr_return_type as well but anyways for compactness I did it here. - llvm::Value* pointer_to_struct = builder->CreateAlloca(fnty->getReturnType(), nullptr); + llvm::Value* pointer_to_struct = builder0.CreateAlloca(fnty->getReturnType(), nullptr); LLVM::CreateStore(*builder, return_value, pointer_to_struct); return pointer_to_struct; } @@ -9277,6 +9313,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_RuntimePolymorphicSubroutineCall(const ASR::SubroutineCall_t& x, std::string proc_sym_name) { + get_builder0() std::vector> vtabs; ASR::Struct_t* dt_sym_type = nullptr; ASR::ttype_t* dt_ttype_t = ASRUtils::type_get_past_allocatable(ASRUtils::type_get_past_pointer( @@ -9337,7 +9374,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor ASR::Struct_t* struct_type_t = ASR::down_cast(type_sym); llvm::Type* target_dt_type = llvm_utils->getStructType(struct_type_t, module.get(), true); llvm::Type* target_class_dt_type = llvm_utils->getClassType(struct_type_t); - llvm::Value* target_dt = builder->CreateAlloca(target_class_dt_type); + llvm::Value* target_dt = builder0.CreateAlloca(target_class_dt_type); llvm::Value* target_dt_hash_ptr = llvm_utils->create_gep(target_dt, 0); builder->CreateStore(vptr_int_hash, target_dt_hash_ptr); llvm::Value* target_dt_data_ptr = llvm_utils->create_gep(target_dt, 1); @@ -9363,6 +9400,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_RuntimePolymorphicFunctionCall(const ASR::FunctionCall_t& x, std::string proc_sym_name) { + get_builder0() std::vector> vtabs; ASR::Struct_t* dt_sym_type = nullptr; ASR::ttype_t* dt_ttype_t = ASRUtils::type_get_past_allocatable(ASRUtils::type_get_past_pointer( @@ -9396,7 +9434,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor this->visit_expr_wrapper(x.m_dt); ptr_loads = ptr_loads_copy; llvm::Value* llvm_dt = tmp; - tmp = builder->CreateAlloca(llvm_utils->get_type_from_ttype_t_util(x.m_type, module.get())); + tmp = builder0.CreateAlloca(llvm_utils->get_type_from_ttype_t_util(x.m_type, module.get())); llvm::BasicBlock *mergeBB = llvm::BasicBlock::Create(context, "ifcont"); for( size_t i = 0; i < vtabs.size(); i++ ) { llvm::Function *fn = builder->GetInsertBlock()->getParent(); @@ -9424,7 +9462,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor ASR::Struct_t* struct_type_t = ASR::down_cast(type_sym); llvm::Type* target_dt_type = llvm_utils->getStructType(struct_type_t, module.get(), true); llvm::Type* target_class_dt_type = llvm_utils->getClassType(struct_type_t); - llvm::Value* target_dt = builder->CreateAlloca(target_class_dt_type); + llvm::Value* target_dt = builder0.CreateAlloca(target_class_dt_type); llvm::Value* target_dt_hash_ptr = llvm_utils->create_gep(target_dt, 0); builder->CreateStore(vptr_int_hash, target_dt_hash_ptr); llvm::Value* target_dt_data_ptr = llvm_utils->create_gep(target_dt, 1); @@ -9454,6 +9492,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_FunctionCall(const ASR::FunctionCall_t &x) { + get_builder0() if ( compiler_options.emit_debug_info ) debug_emit_loc(x); if( ASRUtils::is_intrinsic_optimization(x.m_name) ) { ASR::Function_t* routine = ASR::down_cast( @@ -9578,7 +9617,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor ASRUtils::expr_type(s->m_args[0]))); } // Convert to polymorphic argument - llvm::Value* dt_polymorphic = builder->CreateAlloca( + llvm::Value* dt_polymorphic = builder0.CreateAlloca( llvm_utils->getClassType(s_m_args0_type, true)); llvm::Value* hash_ptr = llvm_utils->create_gep(dt_polymorphic, 0); llvm::Value* hash = llvm::ConstantInt::get( @@ -9728,7 +9767,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor // i64 llvm::Type* type_fx2 = llvm::Type::getInt64Ty(context); // Convert i64 to i64* - llvm::AllocaInst *p_fx2 = builder->CreateAlloca(type_fx2, nullptr); + llvm::AllocaInst *p_fx2 = builder0.CreateAlloca(type_fx2, nullptr); builder->CreateStore(tmp, p_fx2); // Convert i64* to {float,float}* using bitcast tmp = builder->CreateBitCast(p_fx2, complex_type_4->getPointerTo()); @@ -9742,7 +9781,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor // <2 x float> llvm::Type* type_fx2 = FIXED_VECTOR_TYPE::get(llvm::Type::getFloatTy(context), 2); // Convert <2 x float> to <2 x float>* - llvm::AllocaInst *p_fx2 = builder->CreateAlloca(type_fx2, nullptr); + llvm::AllocaInst *p_fx2 = builder0.CreateAlloca(type_fx2, nullptr); builder->CreateStore(tmp, p_fx2); // Convert <2 x float>* to {float,float}* using bitcast tmp = builder->CreateBitCast(p_fx2, complex_type_4->getPointerTo()); @@ -10064,13 +10103,14 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_ArrayBroadcast(const ASR::ArrayBroadcast_t &x) { + get_builder0() this->visit_expr_wrapper(x.m_array, true); llvm::Value *value = tmp; llvm::Type* ele_type = llvm_utils->get_type_from_ttype_t_util( ASRUtils::type_get_past_array(x.m_type), module.get()); size_t n_eles = ASRUtils::get_fixed_size_of_array(x.m_type); llvm::Type* vec_type = FIXED_VECTOR_TYPE::get(ele_type, n_eles); - llvm::AllocaInst *vec = builder->CreateAlloca(vec_type, nullptr); + llvm::AllocaInst *vec = builder0.CreateAlloca(vec_type, nullptr); for (size_t i=0; i < n_eles; i++) { builder->CreateStore(value, llvm_utils->create_gep(vec, i)); } diff --git a/src/libasr/codegen/llvm_utils.cpp b/src/libasr/codegen/llvm_utils.cpp index e5cd47ace4..bfbf88bb11 100644 --- a/src/libasr/codegen/llvm_utils.cpp +++ b/src/libasr/codegen/llvm_utils.cpp @@ -3305,7 +3305,7 @@ namespace LCompilers { llvm_utils->create_ptr_gep(key_mask, key_hash)); llvm::Value* is_prob_not_neeeded = builder->CreateICmpEQ(key_mask_value, llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), llvm::APInt(8, 1))); - llvm::AllocaInst *flag_ptr = builder->CreateAlloca(llvm::Type::getInt1Ty(context), nullptr); + llvm::AllocaInst *flag_ptr = builder0.CreateAlloca(llvm::Type::getInt1Ty(context), nullptr); LLVM::CreateStore(*builder, llvm::ConstantInt::get(llvm::Type::getInt1Ty(context), 0), flag_ptr); LLVM::CreateStore(*builder, llvm::ConstantInt::get(llvm::Type::getInt32Ty(context), 0), pos_ptr); builder->CreateCondBr(is_prob_not_neeeded, thenBB, elseBB); @@ -3347,7 +3347,7 @@ namespace LCompilers { llvm_utils->start_new_block(mergeBB); llvm::Value *flag = LLVM::CreateLoad(*builder, flag_ptr); llvm::Value *pos = LLVM::CreateLoad(*builder, pos_ptr); - llvm::AllocaInst *is_key_matching_ptr = builder->CreateAlloca(llvm::Type::getInt1Ty(context), nullptr); + llvm::AllocaInst *is_key_matching_ptr = builder0.CreateAlloca(llvm::Type::getInt1Ty(context), nullptr); llvm_utils->create_if_else(flag, [&](){ LLVM::CreateStore(*builder, llvm::ConstantInt::get(llvm::Type::getInt1Ty(context), 0), is_key_matching_ptr); @@ -6497,7 +6497,7 @@ namespace LCompilers { llvm_utils->create_ptr_gep(el_mask, el_hash)); llvm::Value* is_prob_not_needed = builder->CreateICmpEQ(el_mask_value, llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), llvm::APInt(8, 1))); - llvm::AllocaInst *flag_ptr = builder->CreateAlloca(llvm::Type::getInt1Ty(context), nullptr); + llvm::AllocaInst *flag_ptr = builder0.CreateAlloca(llvm::Type::getInt1Ty(context), nullptr); LLVM::CreateStore(*builder, llvm::ConstantInt::get(llvm::Type::getInt32Ty(context), 0), pos_ptr); LLVM::CreateStore(*builder, llvm::ConstantInt::get(llvm::Type::getInt1Ty(context), 0), flag_ptr); builder->CreateCondBr(is_prob_not_needed, thenBB, elseBB); @@ -6535,7 +6535,7 @@ namespace LCompilers { } llvm_utils->start_new_block(mergeBB); llvm::Value *flag = LLVM::CreateLoad(*builder, flag_ptr); - llvm::AllocaInst *is_el_matching_ptr = builder->CreateAlloca(llvm::Type::getInt1Ty(context), nullptr); + llvm::AllocaInst *is_el_matching_ptr = builder0.CreateAlloca(llvm::Type::getInt1Ty(context), nullptr); llvm_utils->create_if_else(flag, [&](){ LLVM::CreateStore(*builder, llvm::ConstantInt::get(llvm::Type::getInt1Ty(context), 0), is_el_matching_ptr);