@@ -282,7 +282,8 @@ namespace LFortran {
282
282
llvm_utils (std::move(llvm_utils_)),
283
283
builder (std::move(builder_)),
284
284
pos_ptr (nullptr ), is_key_matching_var(nullptr ),
285
- are_iterators_set (false ) {
285
+ idx_ptr (nullptr ), are_iterators_set(false ),
286
+ is_dict_present (false ) {
286
287
}
287
288
288
289
llvm::Type* LLVMList::get_list_type (llvm::Type* el_type, std::string& type_code,
@@ -301,6 +302,7 @@ namespace LFortran {
301
302
llvm::Type* LLVMDict::get_dict_type (std::string key_type_code, std::string value_type_code,
302
303
int32_t key_type_size, int32_t value_type_size,
303
304
llvm::Type* key_type, llvm::Type* value_type) {
305
+ is_dict_present = true ;
304
306
std::pair<std::string, std::string> llvm_key = std::make_pair (key_type_code, value_type_code);
305
307
if ( typecode2dicttype.find (llvm_key) != typecode2dicttype.end () ) {
306
308
return std::get<0 >(typecode2dicttype[llvm_key]);
@@ -554,25 +556,37 @@ namespace LFortran {
554
556
}
555
557
556
558
void LLVMDict::set_iterators () {
557
- if ( are_iterators_set ) {
559
+ if ( are_iterators_set || !is_dict_present ) {
558
560
return ;
559
561
}
560
- pos_ptr = builder->CreateAlloca (llvm::Type::getInt32Ty (context), nullptr );
561
- is_key_matching_var = builder->CreateAlloca (llvm::Type::getInt1Ty (context), nullptr );
562
+ pos_ptr = builder->CreateAlloca (llvm::Type::getInt32Ty (context), nullptr , " pos_ptr" );
563
+ LLVM::CreateStore (*builder, llvm::ConstantInt::get (llvm::Type::getInt32Ty (context),
564
+ llvm::APInt (32 , 0 )), pos_ptr);
565
+ is_key_matching_var = builder->CreateAlloca (llvm::Type::getInt1Ty (context), nullptr ,
566
+ " is_key_matching_var" );
567
+ LLVM::CreateStore (*builder, llvm::ConstantInt::get (llvm::Type::getInt1Ty (context),
568
+ llvm::APInt (1 , 0 )), is_key_matching_var);
569
+ idx_ptr = builder->CreateAlloca (llvm::Type::getInt32Ty (context), nullptr , " idx_ptr" );
570
+ LLVM::CreateStore (*builder, llvm::ConstantInt::get (llvm::Type::getInt32Ty (context),
571
+ llvm::APInt (32 , 0 )), idx_ptr);
562
572
are_iterators_set = true ;
563
573
}
564
574
565
575
void LLVMDict::reset_iterators () {
566
576
pos_ptr = nullptr ;
567
577
is_key_matching_var = nullptr ;
578
+ idx_ptr = nullptr ;
568
579
are_iterators_set = false ;
569
580
}
570
581
571
582
void LLVMDict::linear_probing (llvm::Value* capacity, llvm::Value* key_hash,
572
583
llvm::Value* key, llvm::Value* key_list,
573
584
llvm::Value* key_mask, llvm::Module& module,
574
585
ASR::ttype_t * key_asr_type) {
575
- set_iterators ();
586
+ if ( !are_iterators_set ) {
587
+ pos_ptr = builder->CreateAlloca (llvm::Type::getInt32Ty (context), nullptr );
588
+ is_key_matching_var = builder->CreateAlloca (llvm::Type::getInt1Ty (context), nullptr );
589
+ }
576
590
LLVM::CreateStore (*builder, key_hash, pos_ptr);
577
591
578
592
llvm::BasicBlock *loophead = llvm::BasicBlock::Create (context, " loop.head" );
@@ -654,7 +668,6 @@ namespace LFortran {
654
668
LLVM::CreateStore (*builder,
655
669
llvm::ConstantInt::get (llvm::Type::getInt1Ty (context), llvm::APInt (1 , 1 )),
656
670
llvm_utils->create_ptr_gep (key_mask, pos));
657
- reset_iterators ();
658
671
}
659
672
660
673
llvm::Value* LLVMDict::linear_probing_for_read (llvm::Value* dict, llvm::Value* key_hash,
@@ -667,7 +680,6 @@ namespace LFortran {
667
680
linear_probing (capacity, key_hash, key, key_list, key_mask, module, key_asr_type);
668
681
llvm::Value* pos = LLVM::CreateLoad (*builder, pos_ptr);
669
682
llvm::Value* item = llvm_utils->list_api ->read_item (value_list, pos, true , false );
670
- reset_iterators ();
671
683
return item;
672
684
}
673
685
@@ -729,10 +741,9 @@ namespace LFortran {
729
741
new_key_mask = builder->CreateBitCast (new_key_mask, llvm::Type::getInt1PtrTy (context));
730
742
731
743
llvm::Value* current_capacity = LLVM::CreateLoad (*builder, get_pointer_to_capacity (dict));
732
- // TODO: Should be created outside the user loop and not here.
733
- // LLVMDict should treat them as data members and create them
734
- // only if they are NULL
735
- llvm::Value* idx_ptr = builder->CreateAlloca (llvm::Type::getInt32Ty (context), nullptr );
744
+ if ( !are_iterators_set ) {
745
+ idx_ptr = builder->CreateAlloca (llvm::Type::getInt32Ty (context), nullptr );
746
+ }
736
747
LLVM::CreateStore (*builder, llvm::ConstantInt::get (llvm::Type::getInt32Ty (context),
737
748
llvm::APInt (32 , 0 )), idx_ptr);
738
749
@@ -790,7 +801,6 @@ namespace LFortran {
790
801
791
802
// end
792
803
llvm_utils->start_new_block (loopend);
793
- reset_iterators ();
794
804
795
805
// TODO: Free key_list, value_list and key_mask
796
806
llvm_utils->list_api ->free_data (key_list, *module);
0 commit comments