Browse Source

Localize `GenProcsData` to the entity itself

gingerBill 2 years ago
parent
commit
12e42d92d3
5 changed files with 22 additions and 24 deletions
  1. 14 15
      src/check_expr.cpp
  2. 2 2
      src/checker.cpp
  3. 0 2
      src/checker.hpp
  4. 4 1
      src/entity.cpp
  5. 2 4
      src/llvm_backend_stmt.cpp

+ 14 - 15
src/check_expr.cpp

@@ -366,8 +366,6 @@ gb_internal bool find_or_generate_polymorphic_procedure(CheckerContext *old_c, E
 		GB_ASSERT(dst == nullptr);
 		GB_ASSERT(dst == nullptr);
 	}
 	}
 
 
-	// MUTEX_GUARD(&info->gen_procs_mutex);
-
 	if (!src->Proc.is_polymorphic || src->Proc.is_poly_specialized) {
 	if (!src->Proc.is_polymorphic || src->Proc.is_poly_specialized) {
 		return false;
 		return false;
 	}
 	}
@@ -434,20 +432,21 @@ gb_internal bool find_or_generate_polymorphic_procedure(CheckerContext *old_c, E
 
 
 	GenProcsData *gen_procs = nullptr;
 	GenProcsData *gen_procs = nullptr;
 
 
-	// @@GPM //////////////////////////
-	mutex_lock(&info->gen_procs_mutex);
-	///////////////////////////////////
-	auto *found = map_get(&info->gen_procs, base_entity->identifier.load());
-	if (found) {
-		gen_procs = *found;
+	GB_ASSERT(base_entity->identifier.load()->kind == Ast_Ident);
+	GB_ASSERT(base_entity->kind == Entity_Procedure);
+
+
+	mutex_lock(&base_entity->Procedure.gen_procs_mutex); // @entity-mutex
+
+	gen_procs = base_entity->Procedure.gen_procs;
+	if (gen_procs) {
 		rw_mutex_shared_lock(&gen_procs->mutex); // @local-mutex
 		rw_mutex_shared_lock(&gen_procs->mutex); // @local-mutex
+
 		for (Entity *other : gen_procs->procs) {
 		for (Entity *other : gen_procs->procs) {
 			Type *pt = base_type(other->type);
 			Type *pt = base_type(other->type);
 			if (are_types_identical(pt, final_proc_type)) {
 			if (are_types_identical(pt, final_proc_type)) {
 				rw_mutex_shared_unlock(&gen_procs->mutex); // @local-mutex
 				rw_mutex_shared_unlock(&gen_procs->mutex); // @local-mutex
-				// @@GPM ////////////////////////////
-				mutex_unlock(&info->gen_procs_mutex);
-				/////////////////////////////////////
+				mutex_unlock(&base_entity->Procedure.gen_procs_mutex); // @entity-mutex
 
 
 				if (poly_proc_data) {
 				if (poly_proc_data) {
 					poly_proc_data->gen_entity = other;
 					poly_proc_data->gen_entity = other;
@@ -455,15 +454,15 @@ gb_internal bool find_or_generate_polymorphic_procedure(CheckerContext *old_c, E
 				return true;
 				return true;
 			}
 			}
 		}
 		}
+
 		rw_mutex_shared_unlock(&gen_procs->mutex); // @local-mutex
 		rw_mutex_shared_unlock(&gen_procs->mutex); // @local-mutex
 	} else {
 	} else {
 		gen_procs = gb_alloc_item(permanent_allocator(), GenProcsData);
 		gen_procs = gb_alloc_item(permanent_allocator(), GenProcsData);
 		gen_procs->procs.allocator = heap_allocator();
 		gen_procs->procs.allocator = heap_allocator();
-		map_set(&info->gen_procs, base_entity->identifier.load(), gen_procs);
+		base_entity->Procedure.gen_procs = gen_procs;
 	}
 	}
-	// @@GPM ////////////////////////////
-	mutex_unlock(&info->gen_procs_mutex);
-	/////////////////////////////////////
+
+	mutex_unlock(&base_entity->Procedure.gen_procs_mutex); // @entity-mutex
 
 
 	{
 	{
 		// LEAK TODO(bill): This is technically a memory leak as it has to generate the type twice
 		// LEAK TODO(bill): This is technically a memory leak as it has to generate the type twice

+ 2 - 2
src/checker.cpp

@@ -1143,7 +1143,7 @@ gb_internal void init_checker_info(CheckerInfo *i) {
 	array_init(&i->entities,      a);
 	array_init(&i->entities,      a);
 	map_init(&i->global_untyped);
 	map_init(&i->global_untyped);
 	string_map_init(&i->foreigns);
 	string_map_init(&i->foreigns);
-	map_init(&i->gen_procs);
+	// map_init(&i->gen_procs);
 	map_init(&i->gen_types);
 	map_init(&i->gen_types);
 	array_init(&i->type_info_types, a);
 	array_init(&i->type_info_types, a);
 	map_init(&i->type_info_map);
 	map_init(&i->type_info_map);
@@ -1172,7 +1172,7 @@ gb_internal void destroy_checker_info(CheckerInfo *i) {
 	array_free(&i->entities);
 	array_free(&i->entities);
 	map_destroy(&i->global_untyped);
 	map_destroy(&i->global_untyped);
 	string_map_destroy(&i->foreigns);
 	string_map_destroy(&i->foreigns);
-	map_destroy(&i->gen_procs);
+	// map_destroy(&i->gen_procs);
 	map_destroy(&i->gen_types);
 	map_destroy(&i->gen_types);
 	array_free(&i->type_info_types);
 	array_free(&i->type_info_types);
 	map_destroy(&i->type_info_map);
 	map_destroy(&i->type_info_map);

+ 0 - 2
src/checker.hpp

@@ -368,9 +368,7 @@ struct CheckerInfo {
 
 
 	RecursiveMutex lazy_mutex; // Mutex required for lazy type checking of specific files
 	RecursiveMutex lazy_mutex; // Mutex required for lazy type checking of specific files
 
 
-	BlockingMutex gen_procs_mutex;
 	RwMutex       gen_types_mutex;
 	RwMutex       gen_types_mutex;
-	PtrMap<Ast *, GenProcsData *> gen_procs; // Key: Ast * | Identifier -> Entity
 	PtrMap<Type *, GenTypesData > gen_types;
 	PtrMap<Type *, GenTypesData > gen_types;
 
 
 	BlockingMutex type_info_mutex; // NOT recursive
 	BlockingMutex type_info_mutex; // NOT recursive

+ 4 - 1
src/entity.cpp

@@ -130,7 +130,7 @@ enum EntityConstantFlags : u32 {
 	EntityConstantFlag_ImplicitEnumValue = 1<<0,
 	EntityConstantFlag_ImplicitEnumValue = 1<<0,
 };
 };
 
 
-enum ProcedureOptimizationMode : u32 {
+enum ProcedureOptimizationMode : u8 {
 	ProcedureOptimizationMode_Default,
 	ProcedureOptimizationMode_Default,
 	ProcedureOptimizationMode_None,
 	ProcedureOptimizationMode_None,
 	ProcedureOptimizationMode_Minimal,
 	ProcedureOptimizationMode_Minimal,
@@ -233,6 +233,9 @@ struct Entity {
 			String  link_name;
 			String  link_name;
 			String  link_prefix;
 			String  link_prefix;
 			DeferredProcedure deferred_procedure;
 			DeferredProcedure deferred_procedure;
+
+			struct GenProcsData *gen_procs;
+			BlockingMutex gen_procs_mutex;
 			ProcedureOptimizationMode optimization_mode;
 			ProcedureOptimizationMode optimization_mode;
 			bool    is_foreign                 : 1;
 			bool    is_foreign                 : 1;
 			bool    is_export                  : 1;
 			bool    is_export                  : 1;

+ 2 - 4
src/llvm_backend_stmt.cpp

@@ -50,13 +50,11 @@ gb_internal void lb_build_constant_value_decl(lbProcedure *p, AstValueDecl *vd)
 			continue; // It's an alias
 			continue; // It's an alias
 		}
 		}
 
 
-		CheckerInfo *info = p->module->info;
 		DeclInfo *decl = decl_info_of_entity(e);
 		DeclInfo *decl = decl_info_of_entity(e);
 		ast_node(pl, ProcLit, decl->proc_lit);
 		ast_node(pl, ProcLit, decl->proc_lit);
 		if (pl->body != nullptr) {
 		if (pl->body != nullptr) {
-			auto *found = map_get(&info->gen_procs, ident);
-			if (found) {
-				GenProcsData *gpd = *found;
+			GenProcsData *gpd = e->Procedure.gen_procs;
+			if (gpd) {
 				rw_mutex_shared_lock(&gpd->mutex);
 				rw_mutex_shared_lock(&gpd->mutex);
 				for (Entity *e : gpd->procs) {
 				for (Entity *e : gpd->procs) {
 					if (!ptr_set_exists(min_dep_set, e)) {
 					if (!ptr_set_exists(min_dep_set, e)) {