Try moving parapoly procs into a separate module when doing weak monomorphization

This commit is contained in:
gingerBill
2025-09-18 15:04:16 +01:00
parent 5b0510a6bd
commit 738a72943b
7 changed files with 66 additions and 11 deletions
+1
View File
@@ -554,6 +554,7 @@ struct BuildContext {
bool internal_no_inline;
bool internal_by_value;
bool internal_weak_monomorphization;
bool no_threaded_checker;
+1 -1
View File
@@ -2145,7 +2145,7 @@ gb_internal void lb_create_global_procedures_and_types(lbGenerator *gen, Checker
lbModule *m = &gen->default_module;
if (USE_SEPARATE_MODULES) {
m = lb_module_of_entity(gen, e);
m = lb_module_of_entity(gen, e, m);
}
GB_ASSERT(m != nullptr);
+2
View File
@@ -150,6 +150,8 @@ struct lbModule {
struct lbGenerator *gen;
LLVMTargetMachineRef target_machine;
lbModule *polymorphic_module;
CheckerInfo *info;
AstPackage *pkg; // possibly associated
AstFile *file; // possibly associated
+1 -1
View File
@@ -1327,7 +1327,7 @@ gb_internal void lb_add_debug_info_for_global_constant_from_entity(lbGenerator *
}
lbModule *m = &gen->default_module;
if (USE_SEPARATE_MODULES) {
m = lb_module_of_entity(gen, e);
m = lb_module_of_entity(gen, e, m);
}
GB_ASSERT(m != nullptr);
+55 -8
View File
@@ -46,6 +46,12 @@ gb_internal void lb_init_module(lbModule *m, Checker *c) {
}
module_name = gb_string_appendc(module_name, "builtin");
}
if (m->polymorphic_module == m) {
if (gb_string_length(module_name)) {
module_name = gb_string_appendc(module_name, "-");
}
module_name = gb_string_appendc(module_name, "$parapoly");
}
m->module_name = module_name;
m->ctx = LLVMContextCreate();
@@ -147,17 +153,42 @@ gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c) {
m->gen = gen;
map_set(&gen->modules, cast(void *)pkg, m);
lb_init_module(m, c);
if (build_context.internal_weak_monomorphization) {
auto pm = gb_alloc_item(permanent_allocator(), lbModule);
pm->pkg = pkg;
pm->gen = gen;
m->polymorphic_module = pm;
pm->polymorphic_module = pm;
map_set(&gen->modules, cast(void *)pm, pm); // point to itself just add it to the list
lb_init_module(pm, c);
}
if (!module_per_file) {
continue;
}
// NOTE(bill): Probably per file is not a good idea, so leave this for later
for (AstFile *file : pkg->files) {
auto m = gb_alloc_item(permanent_allocator(), lbModule);
auto m = gb_alloc_item(permanent_allocator(), lbModule);
m->file = file;
m->pkg = pkg;
m->gen = gen;
m->pkg = pkg;
m->gen = gen;
map_set(&gen->modules, cast(void *)file, m);
lb_init_module(m, c);
if (build_context.internal_weak_monomorphization) {
auto pm = gb_alloc_item(permanent_allocator(), lbModule);
pm->file = file;
pm->gen = gen;
pm->polymorphic_module = pm;
map_set(&gen->modules, cast(void *)pm, pm); // point to itself just add it to the list
lb_init_module(pm, c);
}
}
}
}
@@ -403,9 +434,9 @@ gb_internal lbModule *lb_module_of_expr(lbGenerator *gen, Ast *expr) {
return &gen->default_module;
}
gb_internal lbModule *lb_module_of_entity(lbGenerator *gen, Entity *e) {
GB_ASSERT(e != nullptr);
gb_internal lbModule *lb_module_of_entity_internal(lbGenerator *gen, Entity *e, lbModule *curr_module) {
lbModule **found = nullptr;
if (e->kind == Entity_Procedure &&
e->decl_info &&
e->decl_info->code_gen_module) {
@@ -428,6 +459,22 @@ gb_internal lbModule *lb_module_of_entity(lbGenerator *gen, Entity *e) {
return &gen->default_module;
}
gb_internal lbModule *lb_module_of_entity(lbGenerator *gen, Entity *e, lbModule *curr_module) {
GB_ASSERT(e != nullptr);
GB_ASSERT(curr_module != nullptr);
lbModule *m = lb_module_of_entity_internal(gen, e, curr_module);
if (USE_SEPARATE_MODULES && build_context.internal_weak_monomorphization) {
if (e->kind == Entity_Procedure && e->Procedure.generated_from_polymorphic) {
if (m->polymorphic_module) {
return m->polymorphic_module;
}
}
}
return m;
}
gb_internal lbAddr lb_addr(lbValue addr) {
lbAddr v = {lbAddr_Default, addr};
return v;
@@ -2914,7 +2961,7 @@ gb_internal lbValue lb_find_ident(lbProcedure *p, lbModule *m, Entity *e, Ast *e
return lb_find_procedure_value_from_entity(m, e);
}
if (USE_SEPARATE_MODULES) {
lbModule *other_module = lb_module_of_entity(m->gen, e);
lbModule *other_module = lb_module_of_entity(m->gen, e, m);
if (other_module != m) {
String name = lb_get_entity_name(other_module, e);
@@ -2962,7 +3009,7 @@ gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e)
lbModule *other_module = m;
if (USE_SEPARATE_MODULES) {
other_module = lb_module_of_entity(gen, e);
other_module = lb_module_of_entity(gen, e, m);
}
if (other_module == m) {
debugf("Missing Procedure (lb_find_procedure_value_from_entity): %.*s module %p\n", LIT(e->token.string), m);
@@ -3145,7 +3192,7 @@ gb_internal lbValue lb_find_value_from_entity(lbModule *m, Entity *e) {
}
if (USE_SEPARATE_MODULES) {
lbModule *other_module = lb_module_of_entity(m->gen, e);
lbModule *other_module = lb_module_of_entity(m->gen, e, m);
bool is_external = other_module != m;
if (!is_external) {
+1 -1
View File
@@ -84,7 +84,7 @@ gb_internal lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool i
String link_name = {};
if (ignore_body) {
lbModule *other_module = lb_module_of_entity(m->gen, entity);
lbModule *other_module = lb_module_of_entity(m->gen, entity, m);
link_name = lb_get_entity_name(other_module, entity);
} else {
link_name = lb_get_entity_name(m, entity);
+5
View File
@@ -403,6 +403,7 @@ enum BuildFlagKind {
BuildFlag_InternalCached,
BuildFlag_InternalNoInline,
BuildFlag_InternalByValue,
BuildFlag_InternalWeakMonomorphization,
BuildFlag_Tilde,
@@ -626,6 +627,7 @@ gb_internal bool parse_build_flags(Array<String> args) {
add_flag(&build_flags, BuildFlag_InternalCached, str_lit("internal-cached"), BuildFlagParam_None, Command_all);
add_flag(&build_flags, BuildFlag_InternalNoInline, str_lit("internal-no-inline"), BuildFlagParam_None, Command_all);
add_flag(&build_flags, BuildFlag_InternalByValue, str_lit("internal-by-value"), BuildFlagParam_None, Command_all);
add_flag(&build_flags, BuildFlag_InternalWeakMonomorphization, str_lit("internal-weak-monomorphization"), BuildFlagParam_None, Command_all);
#if ALLOW_TILDE
add_flag(&build_flags, BuildFlag_Tilde, str_lit("tilde"), BuildFlagParam_None, Command__does_build);
@@ -1584,6 +1586,9 @@ gb_internal bool parse_build_flags(Array<String> args) {
case BuildFlag_InternalByValue:
build_context.internal_by_value = true;
break;
case BuildFlag_InternalWeakMonomorphization:
build_context.internal_weak_monomorphization = true;
break;
case BuildFlag_Tilde:
build_context.tilde_backend = true;