Skip to content

Commit

Permalink
Simplify type info table construction
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerBill committed Feb 17, 2025
1 parent b8f0579 commit 4a29d9b
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 85 deletions.
162 changes: 101 additions & 61 deletions src/checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -828,9 +828,15 @@ gb_internal void add_dependency(CheckerInfo *info, DeclInfo *d, Entity *e) {
rw_mutex_unlock(&d->deps_mutex);
}
gb_internal void add_type_info_dependency(CheckerInfo *info, DeclInfo *d, Type *type) {
if (d == nullptr) {
if (d == nullptr || type == nullptr) {
return;
}
if (type->kind == Type_Named) {
Entity *e = type->Named.type_name;
if (e->TypeName.is_type_alias) {
type = type->Named.base;
}
}
rw_mutex_lock(&d->type_info_deps_mutex);
ptr_set_add(&d->type_info_deps, type);
rw_mutex_unlock(&d->type_info_deps_mutex);
Expand Down Expand Up @@ -1361,9 +1367,12 @@ gb_internal void init_checker_info(CheckerInfo *i) {
string_map_init(&i->foreigns);
// map_init(&i->gen_procs);
map_init(&i->gen_types);

array_init(&i->type_info_types, a);
map_init(&i->type_info_map);
type_set_init(&i->type_info_set);
type_set_init(&i->min_dep_type_info_set);
map_init(&i->minimum_dependency_type_info_index_map);

// map_init(&i->type_info_map);
string_map_init(&i->files);
string_map_init(&i->packages);
array_init(&i->variable_init_order, a);
Expand Down Expand Up @@ -1396,9 +1405,11 @@ gb_internal void destroy_checker_info(CheckerInfo *i) {
string_map_destroy(&i->foreigns);
// map_destroy(&i->gen_procs);
map_destroy(&i->gen_types);

array_free(&i->type_info_types);
map_destroy(&i->type_info_map);
type_set_destroy(&i->type_info_set);
type_set_destroy(&i->min_dep_type_info_set);
map_destroy(&i->minimum_dependency_type_info_index_map);

string_map_destroy(&i->files);
string_map_destroy(&i->packages);
array_free(&i->variable_init_order);
Expand Down Expand Up @@ -1632,41 +1643,36 @@ gb_internal void check_remove_expr_info(CheckerContext *c, Ast *e) {
}
}


gb_internal isize type_info_index(CheckerInfo *info, Type *type, bool error_on_failure) {
type = default_type(type);
if (type == t_llvm_bool) {
type = t_bool;
}

mutex_lock(&info->type_info_mutex);
gb_internal isize type_info_index(CheckerInfo *info, TypeInfoPair pair, bool error_on_failure) {
mutex_lock(&info->minimum_dependency_type_info_mutex);

isize entry_index = -1;
isize *found_entry_index = map_get(&info->type_info_map, type);
uintptr hash = cast(uintptr)pair.hash;
isize *found_entry_index = map_get(&info->minimum_dependency_type_info_index_map, hash);
if (found_entry_index) {
entry_index = *found_entry_index;
}
if (entry_index < 0) {
// NOTE(bill): Do manual linear search
for (auto const &e : info->type_info_map) {
if (are_types_identical_unique_tuples(e.key, type)) {
entry_index = e.value;
// NOTE(bill): Add it to the search map
map_set(&info->type_info_map, type, entry_index);
break;
}
}
}

mutex_unlock(&info->type_info_mutex);
mutex_unlock(&info->minimum_dependency_type_info_mutex);

if (error_on_failure && entry_index < 0) {
compiler_error("Type_Info for '%s' could not be found", type_to_string(type));
compiler_error("Type_Info for '%s' could not be found", type_to_string(pair.type));
}
return entry_index;
}


gb_internal isize type_info_index(CheckerInfo *info, Type *type, bool error_on_failure) {
type = default_type(type);
if (type == t_llvm_bool) {
type = t_bool;
}

u64 hash = type_hash_canonical_type(type);
return type_info_index(info, {type, hash}, error_on_failure);
}



gb_internal void add_untyped(CheckerContext *c, Ast *expr, AddressingMode mode, Type *type, ExactValue const &value) {
if (expr == nullptr) {
return;
Expand Down Expand Up @@ -2018,8 +2024,12 @@ gb_internal void add_type_info_type_internal(CheckerContext *c, Type *t) {
}

add_type_info_dependency(c->info, c->decl, t);

#if 0
MUTEX_GUARD_BLOCK(&c->info->type_info_mutex) {
if (type_set_update(&c->info->type_info_set, t)) {
// return;
}

auto found = map_get(&c->info->type_info_map, t);
if (found != nullptr) {
// Types have already been added
Expand Down Expand Up @@ -2238,6 +2248,7 @@ gb_internal void add_type_info_type_internal(CheckerContext *c, Type *t) {
GB_PANIC("Unhandled type: %*.s %d", LIT(type_strings[bt->kind]), bt->kind);
break;
}
#endif
}


Expand Down Expand Up @@ -2295,11 +2306,10 @@ gb_internal void add_min_dep_type_info(Checker *c, Type *t) {
return;
}

if (type_set_update(&c->info.type_info_set, t)) {
// return;
if (type_set_update(&c->info.min_dep_type_info_set, t)) {
return;
}


// Add nested types
if (t->kind == Type_Named) {
// NOTE(bill): Just in case
Expand Down Expand Up @@ -2697,7 +2707,6 @@ gb_internal void generate_minimum_dependency_set(Checker *c, Entity *start) {
isize min_dep_set_cap = next_pow2_isize(entity_count*4); // empirically determined factor

ptr_set_init(&c->info.minimum_dependency_set, min_dep_set_cap);
map_init(&c->info.minimum_dependency_type_info_set);

#define FORCE_ADD_RUNTIME_ENTITIES(condition, ...) do { \
if (condition) { \
Expand Down Expand Up @@ -6720,39 +6729,70 @@ gb_internal void check_parsed_files(Checker *c) {
add_type_and_value(&c->builtin_ctx, u.expr, u.info->mode, u.info->type, u.info->value);
}

TIME_SECTION("check for type hash collisions");
TIME_SECTION("initilize type info array");
{
PtrSet<uintptr> found = {};
ptr_set_init(&found, c->info.type_info_types.count);
defer (ptr_set_destroy(&found));
for (auto const &tt : c->info.type_info_types) {
if (ptr_set_update(&found, cast(uintptr)tt.hash)) {
Type *other_type = nullptr;
for (auto const &other : c->info.type_info_types) {
if (&tt == &other) {
continue;
}
if (cast(uintptr)other.hash == cast(uintptr)tt.hash &&
!are_types_identical(tt.type, other.type)) {
other_type = other.type;
break;
}
}
if (other_type != nullptr) {
String ts = type_to_canonical_string(temporary_allocator(), tt.type);
String os = type_to_canonical_string(temporary_allocator(), other_type);
if (ts != os) {
compiler_error("%s found type hash collision with %s (hash = %llu)\n"
"%s vs %s\n",
type_to_string(tt.type), type_to_string(other_type), cast(unsigned long long)tt.hash,
temp_canonical_string(tt.type),
temp_canonical_string(other_type)
);
for (auto const &tt : c->info.min_dep_type_info_set) {
array_add(&c->info.type_info_types, tt);
}
array_sort(c->info.type_info_types, type_info_pair_cmp);

map_reserve(&c->info.minimum_dependency_type_info_index_map, c->info.type_info_types.count);

for_array(i, c->info.type_info_types) {
auto const &tt = c->info.type_info_types[i];
bool exists = map_set_if_not_previously_exists(&c->info.minimum_dependency_type_info_index_map, cast(uintptr)tt.hash, i);
if (exists) {
for (auto const &entry : c->info.minimum_dependency_type_info_index_map) {
if (entry.key == cast(uintptr)tt.hash) {
auto const &other = c->info.type_info_types[entry.value];
if (!are_types_identical_unique_tuples(tt.type, other.type)) {
gbString t = temp_canonical_string(tt.type);
gbString o = temp_canonical_string(other.type);
GB_PANIC("%s (%s) %llu vs %s (%s) %llu",
type_to_string(tt.type, false), t, cast(unsigned long long)tt.hash,
type_to_string(other.type, false), o, cast(unsigned long long)other.hash);
}
}
}
}
}
}

GB_ASSERT(c->info.minimum_dependency_type_info_index_map.count <= c->info.type_info_types.count);
}

// TIME_SECTION("check for type hash collisions");
// {
// PtrSet<uintptr> found = {};
// ptr_set_init(&found, c->info.type_info_types.count);
// defer (ptr_set_destroy(&found));
// for (auto const &tt : c->info.type_info_types) {
// if (ptr_set_update(&found, cast(uintptr)tt.hash)) {
// Type *other_type = nullptr;
// for (auto const &other : c->info.type_info_types) {
// if (&tt == &other) {
// continue;
// }
// if (cast(uintptr)other.hash == cast(uintptr)tt.hash &&
// !are_types_identical(tt.type, other.type)) {
// other_type = other.type;
// break;
// }
// }
// if (other_type != nullptr) {
// String ts = type_to_canonical_string(temporary_allocator(), tt.type);
// String os = type_to_canonical_string(temporary_allocator(), other_type);
// if (ts != os) {
// compiler_error("%s found type hash collision with %s (hash = %llu)\n"
// "%s vs %s\n",
// type_to_string(tt.type), type_to_string(other_type), cast(unsigned long long)tt.hash,
// temp_canonical_string(tt.type),
// temp_canonical_string(other_type)
// );
// }
// }
// }
// }
// }



Expand Down
17 changes: 10 additions & 7 deletions src/checker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ struct DeclInfo {
PtrSet<Entity *> deps;

RwMutex type_info_deps_mutex;
PtrSet<Type *> type_info_deps;
PtrSet<Type *> type_info_deps; // TODO(bill): Use TypeSet

BlockingMutex type_and_value_mutex;

Expand Down Expand Up @@ -444,8 +444,10 @@ struct CheckerInfo {
Scope * init_scope;
Entity * entry_point;
PtrSet<Entity *> minimum_dependency_set;
PtrMap</*type info index*/isize, /*min dep index*/isize> minimum_dependency_type_info_set;

BlockingMutex minimum_dependency_type_info_mutex;
PtrMap</*type info hash*/uintptr, /*min dep index*/isize> minimum_dependency_type_info_index_map;
TypeSet min_dep_type_info_set;
Array<TypeInfoPair> type_info_types; // sorted after filled


Array<Entity *> testing_procedures;
Expand Down Expand Up @@ -473,10 +475,10 @@ struct CheckerInfo {
BlockingMutex gen_types_mutex;
PtrMap<Type *, GenTypesData *> gen_types;

BlockingMutex type_info_mutex; // NOT recursive
Array<TypeInfoPair> type_info_types;
PtrMap<Type *, isize> type_info_map;
TypeSet type_info_set;
// BlockingMutex type_info_mutex; // NOT recursive
// Array<TypeInfoPair> type_info_types;
// PtrMap<Type *, isize> type_info_map;
// TypeSet type_info_set;

BlockingMutex foreign_mutex; // NOT recursive
StringMap<Entity *> foreigns;
Expand Down Expand Up @@ -595,6 +597,7 @@ gb_internal DeclInfo * decl_info_of_entity (Entity * e);
gb_internal AstFile * ast_file_of_filename (CheckerInfo *i, String filename);
// IMPORTANT: Only to use once checking is done
gb_internal isize type_info_index (CheckerInfo *i, Type *type, bool error_on_failure);
gb_internal isize type_info_index (CheckerInfo *info, TypeInfoPair pair, bool error_on_failure);

// Will return nullptr if not found
gb_internal Entity *entity_of_node(Ast *expr);
Expand Down
4 changes: 3 additions & 1 deletion src/llvm_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3154,7 +3154,9 @@ gb_internal bool lb_generate_code(lbGenerator *gen) {
lbModule *m = default_module;

{ // Add type info data
isize max_type_info_count = info->minimum_dependency_type_info_set.count+1;
GB_ASSERT_MSG(info->minimum_dependency_type_info_index_map.count == info->type_info_types.count, "%tu vs %tu", info->minimum_dependency_type_info_index_map.count, info->type_info_types.count);

isize max_type_info_count = info->minimum_dependency_type_info_index_map.count+1;
Type *t = alloc_type_array(t_type_info_ptr, max_type_info_count);

// IMPORTANT NOTE(bill): As LLVM does not have a union type, an array of unions cannot be initialized
Expand Down
25 changes: 13 additions & 12 deletions src/llvm_backend_type.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
gb_internal isize lb_type_info_index(CheckerInfo *info, Type *type, bool err_on_not_found=true) {
auto *set = &info->minimum_dependency_type_info_set;
isize index = type_info_index(info, type, err_on_not_found);

gb_internal isize lb_type_info_index(CheckerInfo *info, TypeInfoPair pair, bool err_on_not_found=true) {
isize index = type_info_index(info, pair, err_on_not_found);
if (index >= 0) {
auto *found = map_get(set, index+1);
if (found) {
GB_ASSERT(*found >= 0);
return *found + 1;
}
return index+1;
}
if (err_on_not_found) {
gb_printf_err("NOT FOUND lb_type_info_index:\n\t%s\n\t@ index %td\n\tmax count: %u\nFound:\n", type_to_string(type), index, set->count);
for (auto const &entry : *set) {
gb_printf_err("NOT FOUND lb_type_info_index:\n\t%s\n\t@ index %td\n\tmax count: %u\nFound:\n", type_to_string(pair.type), index, info->minimum_dependency_type_info_index_map.count);
for (auto const &entry : info->minimum_dependency_type_info_index_map) {
isize type_info_index = entry.key;
gb_printf_err("\t%s\n", type_to_string(info->type_info_types[type_info_index].type));
}
Expand All @@ -19,6 +15,10 @@ gb_internal isize lb_type_info_index(CheckerInfo *info, Type *type, bool err_on_
return -1;
}

gb_internal isize lb_type_info_index(CheckerInfo *info, Type *type, bool err_on_not_found=true) {
return lb_type_info_index(info, {type, type_hash_canonical_type(type)}, err_on_not_found);
}

gb_internal u64 lb_typeid_kind(lbModule *m, Type *type, u64 id=0) {
GB_ASSERT(!build_context.no_rtti);

Expand Down Expand Up @@ -280,12 +280,13 @@ gb_internal void lb_setup_type_info_data_giant_array(lbModule *m, i64 global_typ
LLVMTypeRef *modified_types = lb_setup_modified_types_for_type_info(m, global_type_info_data_entity_count);
defer (gb_free(heap_allocator(), modified_types));
for_array(type_info_type_index, info->type_info_types) {
Type *t = info->type_info_types[type_info_type_index].type;
auto const &tt = info->type_info_types[type_info_type_index];
Type *t = tt.type;
if (t == nullptr || t == t_invalid) {
continue;
}

isize entry_index = lb_type_info_index(info, t, false);
isize entry_index = lb_type_info_index(info, tt, false);
if (entry_index <= 0) {
continue;
}
Expand Down
Loading

0 comments on commit 4a29d9b

Please sign in to comment.