From 231ce2da59cd93b4e8d8a90daca2d2111fc3ecf8 Mon Sep 17 00:00:00 2001 From: Jon Lipstate Date: Fri, 29 Aug 2025 12:41:38 -0700 Subject: windows i386 support --- src/main.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index db4dee080..c4646bc9f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3617,6 +3617,11 @@ int main(int arg_count, char const **arg_ptr) { // print_usage_line(0, "%.*s 32-bit is not yet supported for this platform", LIT(args[0])); // return 1; // } + + // Warn about Windows i386 thread-local storage limitations + if (build_context.metrics.arch == TargetArch_i386 && build_context.metrics.os == TargetOs_windows) { + gb_printf_err("Warning: Thread-local storage is disabled on Windows i386.\n"); + } // Check chosen microarchitecture. If not found or ?, print list. bool print_microarch_list = true; -- cgit v1.2.3 From e0c4c5336241cb3106910bec64369888b937132b Mon Sep 17 00:00:00 2001 From: Jon Lipstate Date: Wed, 3 Sep 2025 22:32:33 -0700 Subject: add tls when we have crt --- base/runtime/core_builtin.odin | 4 ++-- core/testing/signal_handler_libc.odin | 4 ++-- src/main.cpp | 5 +++-- 3 files changed, 7 insertions(+), 6 deletions(-) (limited to 'src/main.cpp') diff --git a/base/runtime/core_builtin.odin b/base/runtime/core_builtin.odin index 33b600c3e..7e96c6784 100644 --- a/base/runtime/core_builtin.odin +++ b/base/runtime/core_builtin.odin @@ -54,8 +54,8 @@ container_of :: #force_inline proc "contextless" (ptr: $P/^$Field_Type, $T: type when !NO_DEFAULT_TEMP_ALLOCATOR { - when ODIN_ARCH == .i386 && ODIN_OS == .Windows { - // Thread-local storage is problematic on Windows i386 + when ODIN_ARCH == .i386 && ODIN_OS == .Windows && ODIN_NO_CRT { + // Thread-local storage doesn't work on Windows i386 without CRT global_default_temp_allocator_data: Default_Temp_Allocator } else { @thread_local global_default_temp_allocator_data: Default_Temp_Allocator diff --git a/core/testing/signal_handler_libc.odin b/core/testing/signal_handler_libc.odin index 961f5c7ce..7c50fbb09 100644 --- a/core/testing/signal_handler_libc.odin +++ b/core/testing/signal_handler_libc.odin @@ -24,8 +24,8 @@ import "core:terminal/ansi" @(private="file") stop_test_passed: libc.sig_atomic_t @(private="file") stop_test_alert: libc.sig_atomic_t -when ODIN_ARCH == .i386 && ODIN_OS == .Windows { - // Thread-local storage is problematic on Windows i386 +when ODIN_ARCH == .i386 && ODIN_OS == .Windows && ODIN_NO_CRT { + // Thread-local storage doesn't work on Windows i386 without CRT @(private="file") local_test_index: libc.sig_atomic_t @(private="file") diff --git a/src/main.cpp b/src/main.cpp index c4646bc9f..198706de2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3619,8 +3619,9 @@ int main(int arg_count, char const **arg_ptr) { // } // Warn about Windows i386 thread-local storage limitations - if (build_context.metrics.arch == TargetArch_i386 && build_context.metrics.os == TargetOs_windows) { - gb_printf_err("Warning: Thread-local storage is disabled on Windows i386.\n"); + if (build_context.metrics.arch == TargetArch_i386 && build_context.metrics.os == TargetOs_windows && build_context.no_crt) { + gb_printf_err("Warning: Thread-local storage is not supported on Windows i386 with -no-crt.\n"); + gb_printf_err(" Multi-threaded code will not work correctly.\n"); } // Check chosen microarchitecture. If not found or ?, print list. -- cgit v1.2.3 From 57bc45ae30736a891e4b65c7047a091e53cf60e3 Mon Sep 17 00:00:00 2001 From: Jon Lipstate Date: Wed, 3 Sep 2025 22:51:28 -0700 Subject: revert to working build --- base/runtime/core_builtin.odin | 4 ++-- core/testing/signal_handler_libc.odin | 4 ++-- src/main.cpp | 5 ++--- 3 files changed, 6 insertions(+), 7 deletions(-) (limited to 'src/main.cpp') diff --git a/base/runtime/core_builtin.odin b/base/runtime/core_builtin.odin index 7e96c6784..33b600c3e 100644 --- a/base/runtime/core_builtin.odin +++ b/base/runtime/core_builtin.odin @@ -54,8 +54,8 @@ container_of :: #force_inline proc "contextless" (ptr: $P/^$Field_Type, $T: type when !NO_DEFAULT_TEMP_ALLOCATOR { - when ODIN_ARCH == .i386 && ODIN_OS == .Windows && ODIN_NO_CRT { - // Thread-local storage doesn't work on Windows i386 without CRT + when ODIN_ARCH == .i386 && ODIN_OS == .Windows { + // Thread-local storage is problematic on Windows i386 global_default_temp_allocator_data: Default_Temp_Allocator } else { @thread_local global_default_temp_allocator_data: Default_Temp_Allocator diff --git a/core/testing/signal_handler_libc.odin b/core/testing/signal_handler_libc.odin index 7c50fbb09..961f5c7ce 100644 --- a/core/testing/signal_handler_libc.odin +++ b/core/testing/signal_handler_libc.odin @@ -24,8 +24,8 @@ import "core:terminal/ansi" @(private="file") stop_test_passed: libc.sig_atomic_t @(private="file") stop_test_alert: libc.sig_atomic_t -when ODIN_ARCH == .i386 && ODIN_OS == .Windows && ODIN_NO_CRT { - // Thread-local storage doesn't work on Windows i386 without CRT +when ODIN_ARCH == .i386 && ODIN_OS == .Windows { + // Thread-local storage is problematic on Windows i386 @(private="file") local_test_index: libc.sig_atomic_t @(private="file") diff --git a/src/main.cpp b/src/main.cpp index 198706de2..c4646bc9f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3619,9 +3619,8 @@ int main(int arg_count, char const **arg_ptr) { // } // Warn about Windows i386 thread-local storage limitations - if (build_context.metrics.arch == TargetArch_i386 && build_context.metrics.os == TargetOs_windows && build_context.no_crt) { - gb_printf_err("Warning: Thread-local storage is not supported on Windows i386 with -no-crt.\n"); - gb_printf_err(" Multi-threaded code will not work correctly.\n"); + if (build_context.metrics.arch == TargetArch_i386 && build_context.metrics.os == TargetOs_windows) { + gb_printf_err("Warning: Thread-local storage is disabled on Windows i386.\n"); } // Check chosen microarchitecture. If not found or ?, print list. -- cgit v1.2.3 From 1e0902677f905e752b42e2f48dcda53141b78eee Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 10 Sep 2025 17:29:11 +0100 Subject: Multithread min dep set by removing the set itself --- src/checker.cpp | 193 ++++++++++++++++++++++-------------------- src/checker.hpp | 7 +- src/entity.cpp | 1 + src/error.cpp | 16 ++-- src/llvm_backend.cpp | 17 ++-- src/llvm_backend_proc.cpp | 3 +- src/llvm_backend_stmt.cpp | 6 +- src/main.cpp | 3 +- src/name_canonicalization.cpp | 61 ++++++++++++- src/name_canonicalization.hpp | 2 + src/ptr_set.cpp | 38 +++++++++ src/threading.cpp | 38 +++++++++ src/types.cpp | 1 + 13 files changed, 263 insertions(+), 123 deletions(-) (limited to 'src/main.cpp') diff --git a/src/checker.cpp b/src/checker.cpp index 26430359c..b3a702cbd 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -1712,7 +1712,7 @@ gb_internal void check_remove_expr_info(CheckerContext *c, Ast *e) { } gb_internal isize type_info_index(CheckerInfo *info, TypeInfoPair pair, bool error_on_failure) { - mutex_lock(&info->minimum_dependency_type_info_mutex); + rw_mutex_shared_lock(&info->minimum_dependency_type_info_mutex); isize entry_index = -1; u64 hash = pair.hash; @@ -1720,7 +1720,7 @@ gb_internal isize type_info_index(CheckerInfo *info, TypeInfoPair pair, bool err if (found_entry_index) { entry_index = *found_entry_index; } - mutex_unlock(&info->minimum_dependency_type_info_mutex); + rw_mutex_shared_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(pair.type)); @@ -2377,11 +2377,8 @@ gb_internal void add_min_dep_type_info(Checker *c, Type *t) { return; } - { - MUTEX_GUARD(&c->info.minimum_dependency_type_info_mutex); - if (type_set_update(&c->info.min_dep_type_info_set, t)) { - return; - } + if (type_set_update_with_mutex(&c->info.min_dep_type_info_set, t, &c->info.min_dep_type_info_set_mutex)) { + return; } // Add nested types @@ -2555,13 +2552,15 @@ gb_internal void add_min_dep_type_info(Checker *c, Type *t) { } } +gb_internal void add_dependency_to_set_threaded(Checker *c, Entity *entity); + +gb_global std::atomic global_checker_ptr; + gb_internal void add_dependency_to_set(Checker *c, Entity *entity) { if (entity == nullptr) { return; } - CheckerInfo *info = &c->info; - if (entity->type != nullptr && is_type_polymorphic(entity->type)) { DeclInfo *decl = decl_info_of_entity(entity); @@ -2570,11 +2569,8 @@ gb_internal void add_dependency_to_set(Checker *c, Entity *entity) { } } - { - MUTEX_GUARD(&info->minimum_dependency_type_info_mutex); - if (ptr_set_update(&info->minimum_dependency_set, entity)) { - return; - } + if (entity->min_dep_count.fetch_add(1, std::memory_order_relaxed) > 0) { + return; } DeclInfo *decl = decl_info_of_entity(entity); @@ -2584,46 +2580,45 @@ gb_internal void add_dependency_to_set(Checker *c, Entity *entity) { for (TypeInfoPair const tt : decl->type_info_deps) { add_min_dep_type_info(c, tt.type); } - for (Entity *e : decl->deps) { - add_dependency_to_set(c, e); - if (e->kind == Entity_Procedure && e->Procedure.is_foreign) { - Entity *fl = e->Procedure.foreign_library; - if (fl != nullptr) { - GB_ASSERT_MSG(fl->kind == Entity_LibraryName && - (fl->flags&EntityFlag_Used), - "%.*s", LIT(entity->token.string)); - add_dependency_to_set(c, fl); + switch (e->kind) { + case Entity_Procedure: + if (e->Procedure.is_foreign) { + Entity *fl = e->Procedure.foreign_library; + if (fl != nullptr) { + GB_ASSERT_MSG(fl->kind == Entity_LibraryName && + (fl->flags&EntityFlag_Used), + "%.*s", LIT(entity->token.string)); + add_dependency_to_set(c, fl); + } } - } else if (e->kind == Entity_Variable && e->Variable.is_foreign) { - Entity *fl = e->Variable.foreign_library; - if (fl != nullptr) { - GB_ASSERT_MSG(fl->kind == Entity_LibraryName && - (fl->flags&EntityFlag_Used), - "%.*s", LIT(entity->token.string)); - add_dependency_to_set(c, fl); + break; + case Entity_Variable: + if (e->Variable.is_foreign) { + Entity *fl = e->Variable.foreign_library; + if (fl != nullptr) { + GB_ASSERT_MSG(fl->kind == Entity_LibraryName && + (fl->flags&EntityFlag_Used), + "%.*s", LIT(entity->token.string)); + add_dependency_to_set(c, fl); + } } + break; } } -} -struct AddDependecyToSetWorkerData { - Checker *c; - Entity *entity; -}; - -gb_internal void add_dependency_to_set_threaded(Checker *c, Entity *entity); + for (Entity *e : decl->deps) { + add_dependency_to_set(c, e); + } +} gb_internal WORKER_TASK_PROC(add_dependency_to_set_worker) { - AddDependecyToSetWorkerData *wd = cast(AddDependecyToSetWorkerData *)data; - Checker *c = wd->c; - Entity *entity = wd->entity; + Checker *c = global_checker_ptr.load(std::memory_order_relaxed); + Entity *entity = cast(Entity *)data; if (entity == nullptr) { return 0; } - CheckerInfo *info = &c->info; - if (entity->type != nullptr && is_type_polymorphic(entity->type)) { DeclInfo *decl = decl_info_of_entity(entity); @@ -2632,11 +2627,8 @@ gb_internal WORKER_TASK_PROC(add_dependency_to_set_worker) { } } - { - MUTEX_GUARD(&info->minimum_dependency_type_info_mutex); - if (ptr_set_update(&info->minimum_dependency_set, entity)) { - return 0; - } + if (entity->min_dep_count.fetch_add(1, std::memory_order_relaxed) > 0) { + return 0; } DeclInfo *decl = decl_info_of_entity(entity); @@ -2648,25 +2640,36 @@ gb_internal WORKER_TASK_PROC(add_dependency_to_set_worker) { } for (Entity *e : decl->deps) { - add_dependency_to_set(c, e); - if (e->kind == Entity_Procedure && e->Procedure.is_foreign) { - Entity *fl = e->Procedure.foreign_library; - if (fl != nullptr) { - GB_ASSERT_MSG(fl->kind == Entity_LibraryName && - (fl->flags&EntityFlag_Used), - "%.*s", LIT(entity->token.string)); - add_dependency_to_set_threaded(c, fl); + switch (e->kind) { + case Entity_Procedure: + if (e->Procedure.is_foreign) { + Entity *fl = e->Procedure.foreign_library; + if (fl != nullptr) { + GB_ASSERT_MSG(fl->kind == Entity_LibraryName && + (fl->flags&EntityFlag_Used), + "%.*s", LIT(entity->token.string)); + add_dependency_to_set_threaded(c, fl); + } } - } else if (e->kind == Entity_Variable && e->Variable.is_foreign) { - Entity *fl = e->Variable.foreign_library; - if (fl != nullptr) { - GB_ASSERT_MSG(fl->kind == Entity_LibraryName && - (fl->flags&EntityFlag_Used), - "%.*s", LIT(entity->token.string)); - add_dependency_to_set_threaded(c, fl); + break; + case Entity_Variable: + if (e->Variable.is_foreign) { + Entity *fl = e->Variable.foreign_library; + if (fl != nullptr) { + GB_ASSERT_MSG(fl->kind == Entity_LibraryName && + (fl->flags&EntityFlag_Used), + "%.*s", LIT(entity->token.string)); + add_dependency_to_set_threaded(c, fl); + } } + break; } } + + for (Entity *e : decl->deps) { + add_dependency_to_set_threaded(c, e); + } + return 0; } @@ -2675,11 +2678,7 @@ gb_internal void add_dependency_to_set_threaded(Checker *c, Entity *entity) { if (entity == nullptr) { return; } - - AddDependecyToSetWorkerData *wd = gb_alloc_item(temporary_allocator(), AddDependecyToSetWorkerData); - wd->c = c; - wd->entity = entity; - thread_pool_add_task(add_dependency_to_set_worker, wd); + thread_pool_add_task(add_dependency_to_set_worker, entity); } @@ -2732,27 +2731,35 @@ gb_internal void collect_testing_procedures_of_package(Checker *c, AstPackage *p } gb_internal void generate_minimum_dependency_set_internal(Checker *c, Entity *start) { + // auto const &add_to_set = add_dependency_to_set; + auto const &add_to_set = add_dependency_to_set_threaded; + + Scope *builtin_scope = builtin_pkg->scope; for_array(i, c->info.definitions) { Entity *e = c->info.definitions[i]; - if (e->scope == builtin_pkg->scope) { + if (e->scope == builtin_scope) { if (e->type == nullptr) { - add_dependency_to_set_threaded(c, e); + add_to_set(c, e); + } + } else if (e->kind == Entity_Procedure) { + if (e->Procedure.is_export) { + add_to_set(c, e); + } + } else if (e->kind == Entity_Variable) { + if (e->Variable.is_export) { + add_to_set(c, e); } - } else if (e->kind == Entity_Procedure && e->Procedure.is_export) { - add_dependency_to_set_threaded(c, e); - } else if (e->kind == Entity_Variable && e->Variable.is_export) { - add_dependency_to_set_threaded(c, e); } } for (Entity *e; mpsc_dequeue(&c->info.required_foreign_imports_through_force_queue, &e); /**/) { array_add(&c->info.required_foreign_imports_through_force, e); - add_dependency_to_set_threaded(c, e); + add_to_set(c, e); } for (Entity *e; mpsc_dequeue(&c->info.required_global_variable_queue, &e); /**/) { e->flags |= EntityFlag_Used; - add_dependency_to_set_threaded(c, e); + add_to_set(c, e); } for_array(i, c->info.entities) { @@ -2760,16 +2767,16 @@ gb_internal void generate_minimum_dependency_set_internal(Checker *c, Entity *st switch (e->kind) { case Entity_Variable: if (e->Variable.is_export) { - add_dependency_to_set_threaded(c, e); + add_to_set(c, e); } else if (e->flags & EntityFlag_Require) { - add_dependency_to_set_threaded(c, e); + add_to_set(c, e); } break; case Entity_Procedure: if (e->Procedure.is_export) { - add_dependency_to_set_threaded(c, e); + add_to_set(c, e); } else if (e->flags & EntityFlag_Require) { - add_dependency_to_set_threaded(c, e); + add_to_set(c, e); } if (e->flags & EntityFlag_Init) { Type *t = base_type(e->type); @@ -2809,7 +2816,7 @@ gb_internal void generate_minimum_dependency_set_internal(Checker *c, Entity *st if (is_init) { - add_dependency_to_set_threaded(c, e); + add_to_set(c, e); array_add(&c->info.init_procedures, e); } } else if (e->flags & EntityFlag_Fini) { @@ -2844,7 +2851,7 @@ gb_internal void generate_minimum_dependency_set_internal(Checker *c, Entity *st } if (is_fini) { - add_dependency_to_set_threaded(c, e); + add_to_set(c, e); array_add(&c->info.fini_procedures, e); } } @@ -2861,7 +2868,7 @@ gb_internal void generate_minimum_dependency_set_internal(Checker *c, Entity *st Entity *e = entry.value; if (e != nullptr) { e->flags |= EntityFlag_Used; - add_dependency_to_set_threaded(c, e); + add_to_set(c, e); } } @@ -2876,16 +2883,11 @@ gb_internal void generate_minimum_dependency_set_internal(Checker *c, Entity *st } } else if (start != nullptr) { start->flags |= EntityFlag_Used; - add_dependency_to_set_threaded(c, start); + add_to_set(c, start); } } gb_internal void generate_minimum_dependency_set(Checker *c, Entity *start) { - isize entity_count = c->info.entities.count; - 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); - #define FORCE_ADD_RUNTIME_ENTITIES(condition, ...) do { \ if (condition) { \ String entities[] = {__VA_ARGS__}; \ @@ -6267,8 +6269,10 @@ gb_internal void check_unchecked_bodies(Checker *c) { // use the `procs_to_check` array global_procedure_body_in_worker_queue = false; - for (Entity *e : c->info.minimum_dependency_set) { - check_procedure_later_from_entity(c, e, "check_unchecked_bodies"); + for (Entity *e : c->info.entities) { + if (e->min_dep_count.load(std::memory_order_relaxed) > 0) { + check_procedure_later_from_entity(c, e, "check_unchecked_bodies"); + } } if (!global_procedure_body_in_worker_queue) { @@ -7042,6 +7046,7 @@ gb_internal void check_merge_queues_into_arrays(Checker *c) { } check_add_entities_from_queues(c); check_add_definitions_from_queues(c); + thread_pool_wait(); } gb_internal GB_COMPARE_PROC(init_procedures_cmp) { @@ -7100,7 +7105,7 @@ gb_internal void add_type_info_for_type_definitions(Checker *c) { Entity *e = c->info.definitions[i]; if (e->kind == Entity_TypeName && e->type != nullptr && is_type_typed(e->type)) { i64 align = type_align_of(e->type); - if (align > 0 && ptr_set_exists(&c->info.minimum_dependency_set, e)) { + if (align > 0 && e->min_dep_count.load(std::memory_order_relaxed) > 0) { add_type_info_type(&c->builtin_ctx, e->type); } } @@ -7170,6 +7175,8 @@ gb_internal void check_update_dependency_tree_for_procedures(Checker *c) { gb_internal void check_parsed_files(Checker *c) { + global_checker_ptr.store(c, std::memory_order_relaxed); + TIME_SECTION("map full filepaths to scope"); add_type_info_type(&c->builtin_ctx, t_invalid); @@ -7312,11 +7319,9 @@ gb_internal void check_parsed_files(Checker *c) { check_unchecked_bodies(c); TIME_SECTION("check #soa types"); - check_merge_queues_into_arrays(c); - thread_pool_wait(); - TIME_SECTION("update minimum dependency set"); + TIME_SECTION("update minimum dependency set again"); generate_minimum_dependency_set_internal(c, c->info.entry_point); // NOTE(laytan): has to be ran after generate_minimum_dependency_set, diff --git a/src/checker.hpp b/src/checker.hpp index 1da46b74a..8b4d61ee2 100644 --- a/src/checker.hpp +++ b/src/checker.hpp @@ -449,11 +449,10 @@ struct CheckerInfo { Scope * init_scope; Entity * entry_point; - BlockingMutex minimum_dependency_set_mutex; - PtrSet minimum_dependency_set; - - BlockingMutex minimum_dependency_type_info_mutex; + RwMutex minimum_dependency_type_info_mutex; PtrMap min_dep_type_info_index_map; + + RWSpinLock min_dep_type_info_set_mutex; TypeSet min_dep_type_info_set; Array type_info_types_hash_map; // 2 * type_info_types.count diff --git a/src/entity.cpp b/src/entity.cpp index 6c0aa6ace..5ca3fa916 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -164,6 +164,7 @@ struct Entity { u64 id; std::atomic flags; std::atomic state; + std::atomic min_dep_count; Token token; Scope * scope; Type * type; diff --git a/src/error.cpp b/src/error.cpp index 006d5ae8d..10bf1caf5 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -86,7 +86,7 @@ gb_internal char *token_pos_to_string(TokenPos const &pos); gb_internal bool set_file_path_string(i32 index, String const &path) { bool ok = false; GB_ASSERT(index >= 0); - mutex_lock(&global_error_collector.path_mutex); + // mutex_lock(&global_error_collector.path_mutex); mutex_lock(&global_files_mutex); if (index >= global_file_path_strings.count) { @@ -99,14 +99,14 @@ gb_internal bool set_file_path_string(i32 index, String const &path) { } mutex_unlock(&global_files_mutex); - mutex_unlock(&global_error_collector.path_mutex); + // mutex_unlock(&global_error_collector.path_mutex); return ok; } gb_internal bool thread_safe_set_ast_file_from_id(i32 index, AstFile *file) { bool ok = false; GB_ASSERT(index >= 0); - mutex_lock(&global_error_collector.path_mutex); + // mutex_lock(&global_error_collector.path_mutex); mutex_lock(&global_files_mutex); if (index >= global_files.count) { @@ -118,13 +118,13 @@ gb_internal bool thread_safe_set_ast_file_from_id(i32 index, AstFile *file) { ok = true; } mutex_unlock(&global_files_mutex); - mutex_unlock(&global_error_collector.path_mutex); + // mutex_unlock(&global_error_collector.path_mutex); return ok; } gb_internal String get_file_path_string(i32 index) { GB_ASSERT(index >= 0); - mutex_lock(&global_error_collector.path_mutex); + // mutex_lock(&global_error_collector.path_mutex); mutex_lock(&global_files_mutex); String path = {}; @@ -133,13 +133,13 @@ gb_internal String get_file_path_string(i32 index) { } mutex_unlock(&global_files_mutex); - mutex_unlock(&global_error_collector.path_mutex); + // mutex_unlock(&global_error_collector.path_mutex); return path; } gb_internal AstFile *thread_safe_get_ast_file_from_id(i32 index) { GB_ASSERT(index >= 0); - mutex_lock(&global_error_collector.path_mutex); + // mutex_lock(&global_error_collector.path_mutex); mutex_lock(&global_files_mutex); AstFile *file = nullptr; @@ -148,7 +148,7 @@ gb_internal AstFile *thread_safe_get_ast_file_from_id(i32 index) { } mutex_unlock(&global_files_mutex); - mutex_unlock(&global_error_collector.path_mutex); + // mutex_unlock(&global_error_collector.path_mutex); return file; } diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index ff17e9c10..11b979774 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -2097,8 +2097,6 @@ gb_internal GB_COMPARE_PROC(llvm_global_entity_cmp) { } gb_internal void lb_create_global_procedures_and_types(lbGenerator *gen, CheckerInfo *info, bool do_threading) { - auto *min_dep_set = &info->minimum_dependency_set; - for (Entity *e : info->entities) { String name = e->token.string; Scope * scope = e->scope; @@ -2135,11 +2133,16 @@ gb_internal void lb_create_global_procedures_and_types(lbGenerator *gen, Checker } } - if (!polymorphic_struct && !ptr_set_exists(min_dep_set, e)) { + if (!polymorphic_struct && e->min_dep_count.load(std::memory_order_relaxed) == 0) { // NOTE(bill): Nothing depends upon it so doesn't need to be built continue; } + // if (!polymorphic_struct && !ptr_set_exists(min_dep_set, e)) { + // // NOTE(bill): Nothing depends upon it so doesn't need to be built + // continue; + // } + lbModule *m = &gen->default_module; if (USE_SEPARATE_MODULES) { m = lb_module_of_entity(gen, e); @@ -2845,8 +2848,6 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { lbModule *default_module = &gen->default_module; CheckerInfo *info = gen->info; - auto *min_dep_set = &info->minimum_dependency_set; - switch (build_context.metrics.arch) { case TargetArch_amd64: case TargetArch_i386: @@ -3184,10 +3185,14 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { continue; } - if (!ptr_set_exists(min_dep_set, e)) { + if (e->min_dep_count.load(std::memory_order_relaxed) == 0) { continue; } + // if (!ptr_set_exists(min_dep_set, e)) { + // continue; + // } + DeclInfo *decl = decl_info_of_entity(e); if (decl == nullptr) { continue; diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index f2e6662c8..06829efab 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -798,9 +798,8 @@ gb_internal void lb_end_procedure_body(lbProcedure *p) { gb_internal void lb_build_nested_proc(lbProcedure *p, AstProcLit *pd, Entity *e) { GB_ASSERT(pd->body != nullptr); lbModule *m = p->module; - auto *min_dep_set = &m->info->minimum_dependency_set; - if (ptr_set_exists(min_dep_set, e) == false) { + if (e->min_dep_count.load(std::memory_order_relaxed) == 0) { // NOTE(bill): Nothing depends upon it so doesn't need to be built return; } diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index 5481ca447..590920b59 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -3,8 +3,6 @@ gb_internal void lb_build_constant_value_decl(lbProcedure *p, AstValueDecl *vd) return; } - auto *min_dep_set = &p->module->info->minimum_dependency_set; - for (Ast *ident : vd->names) { GB_ASSERT(ident->kind == Ast_Ident); Entity *e = entity_of_node(ident); @@ -21,7 +19,7 @@ gb_internal void lb_build_constant_value_decl(lbProcedure *p, AstValueDecl *vd) } } - if (!polymorphic_struct && !ptr_set_exists(min_dep_set, e)) { + if (!polymorphic_struct && e->min_dep_count.load(std::memory_order_relaxed) == 0) { continue; } @@ -56,7 +54,7 @@ gb_internal void lb_build_constant_value_decl(lbProcedure *p, AstValueDecl *vd) if (gpd) { rw_mutex_shared_lock(&gpd->mutex); for (Entity *e : gpd->procs) { - if (!ptr_set_exists(min_dep_set, e)) { + if (e->min_dep_count.load(std::memory_order_relaxed) == 0) { continue; } DeclInfo *d = decl_info_of_entity(e); diff --git a/src/main.cpp b/src/main.cpp index db4dee080..184b1eaac 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3050,7 +3050,8 @@ gb_internal void print_show_unused(Checker *c) { if (e->token.string == "_") { continue; } - if (ptr_set_exists(&info->minimum_dependency_set, e)) { + + if (e->min_dep_count.load(std::memory_order_relaxed) > 0) { continue; } array_add(&unused, e); diff --git a/src/name_canonicalization.cpp b/src/name_canonicalization.cpp index 6a4538e26..8bacfabc6 100644 --- a/src/name_canonicalization.cpp +++ b/src/name_canonicalization.cpp @@ -57,10 +57,13 @@ gb_internal isize type_set__find(TypeSet *s, TypeInfoPair pair) { usize mask = s->capacity-1; usize hash_index = cast(usize)hash & mask; for (usize i = 0; i < s->capacity; i++) { - Type *key = s->keys[hash_index].type; - if (are_types_identical_unique_tuples(key, pair.type)) { + auto *e = &s->keys[hash_index]; + u64 hash = e->hash; + Type *key = e->type; + if (hash == pair.hash && + are_types_identical_unique_tuples(key, pair.type)) { return hash_index; - } else if (key == 0) { + } else if (key == nullptr) { return -1; } hash_index = (hash_index+1)&mask; @@ -164,6 +167,48 @@ gb_internal bool type_set_update(TypeSet *s, Type *ptr) { // returns true if it return type_set_update(s, pair); } +gb_internal bool type_set_update_with_mutex(TypeSet *s, TypeInfoPair pair, RWSpinLock *m) { // returns true if it previously existsed + rwlock_acquire_upgrade(m); + if (type_set_exists(s, pair)) { + rwlock_release_upgrade(m); + return true; + } + + rwlock_release_upgrade_and_acquire_write(m); + defer (rwlock_release_write(m)); + + if (s->keys == nullptr) { + type_set_init(s); + } else if (type_set__full(s)) { + type_set_grow(s); + } + GB_ASSERT(s->count < s->capacity); + GB_ASSERT(s->capacity >= 0); + + usize mask = s->capacity-1; + usize hash = cast(usize)pair.hash; + usize hash_index = (cast(usize)hash) & mask; + GB_ASSERT(hash_index < s->capacity); + for (usize i = 0; i < s->capacity; i++) { + TypeInfoPair *key = &s->keys[hash_index]; + GB_ASSERT(!are_types_identical_unique_tuples(key->type, pair.type)); + if (key->hash == TYPE_SET_TOMBSTONE || key->hash == 0) { + *key = pair; + s->count++; + return false; + } + hash_index = (hash_index+1)&mask; + } + + GB_PANIC("ptr set out of memory"); + return false; +} + +gb_internal bool type_set_update_with_mutex(TypeSet *s, Type *ptr, RWSpinLock *m) { // returns true if it previously existsed + TypeInfoPair pair = {ptr, type_hash_canonical_type(ptr)}; + return type_set_update_with_mutex(s, pair, m); +} + gb_internal Type *type_set_add(TypeSet *s, Type *ptr) { type_set_update(s, ptr); @@ -328,12 +373,20 @@ gb_internal u64 type_hash_canonical_type(Type *type) { if (type == nullptr) { return 0; } + u64 prev_hash = type->canonical_hash.load(std::memory_order_relaxed); + if (prev_hash != 0) { + return prev_hash; + } + u64 hash = fnv64a(nullptr, 0); TypeWriter w = {}; type_writer_make_hasher(&w, &hash); write_type_to_canonical_string(&w, type); + hash = hash ? hash : 1; + + type->canonical_hash.store(hash, std::memory_order_relaxed); - return hash ? hash : 1; + return hash; } gb_internal String type_to_canonical_string(gbAllocator allocator, Type *type) { diff --git a/src/name_canonicalization.hpp b/src/name_canonicalization.hpp index 304aff42e..00b450fbe 100644 --- a/src/name_canonicalization.hpp +++ b/src/name_canonicalization.hpp @@ -102,6 +102,8 @@ gb_internal Type *type_set_add (TypeSet *s, Type *ptr); gb_internal Type *type_set_add (TypeSet *s, TypeInfoPair pair); gb_internal bool type_set_update (TypeSet *s, Type *ptr); // returns true if it previously existed gb_internal bool type_set_update (TypeSet *s, TypeInfoPair pair); // returns true if it previously existed +gb_internal bool type_set_update_with_mutex(TypeSet *s, TypeInfoPair pair, RWSpinLock *m); +gb_internal bool type_set_update_with_mutex(TypeSet *s, Type *ptr, RWSpinLock *m); gb_internal bool type_set_exists (TypeSet *s, Type *ptr); gb_internal void type_set_remove (TypeSet *s, Type *ptr); gb_internal void type_set_clear (TypeSet *s); diff --git a/src/ptr_set.cpp b/src/ptr_set.cpp index 5097e2bb6..06c1e4a58 100644 --- a/src/ptr_set.cpp +++ b/src/ptr_set.cpp @@ -134,6 +134,44 @@ gb_internal bool ptr_set_update(PtrSet *s, T ptr) { // returns true if it pre return false; } +template +gb_internal bool ptr_set_update_with_mutex(PtrSet *s, T ptr, RWSpinLock *m) { // returns true if it previously existsed + rwlock_acquire_upgrade(m); + if (ptr_set_exists(s, ptr)) { + rwlock_release_upgrade(m); + return true; + } + + rwlock_release_upgrade_and_acquire_write(m); + defer (rwlock_release_write(m)); + + if (s->keys == nullptr) { + ptr_set_init(s); + } else if (ptr_set__full(s)) { + ptr_set_grow(s); + } + GB_ASSERT(s->count < s->capacity); + GB_ASSERT(s->capacity >= 0); + + usize mask = s->capacity-1; + u32 hash = ptr_map_hash_key(ptr); + usize hash_index = (cast(usize)hash) & mask; + GB_ASSERT(hash_index < s->capacity); + for (usize i = 0; i < s->capacity; i++) { + T *key = &s->keys[hash_index]; + GB_ASSERT(*key != ptr); + if (*key == (T)PtrSet::TOMBSTONE || *key == 0) { + *key = ptr; + s->count++; + return false; + } + hash_index = (hash_index+1)&mask; + } + + GB_PANIC("ptr set out of memory"); + return false; +} + template gb_internal T ptr_set_add(PtrSet *s, T ptr) { ptr_set_update(s, ptr); diff --git a/src/threading.cpp b/src/threading.cpp index a0d1c4049..f1d9264e3 100644 --- a/src/threading.cpp +++ b/src/threading.cpp @@ -448,6 +448,44 @@ gb_internal void semaphore_wait(Semaphore *s) { } #endif +static const int RWLOCK_WRITER = 1; +static const int RWLOCK_UPGRADED = 2; +static const int RWLOCK_READER = 4; +struct RWSpinLock { + Futex bits; +}; + +void rwlock_release_write(RWSpinLock *l) { + l->bits.fetch_and(~(RWLOCK_WRITER | RWLOCK_UPGRADED), std::memory_order_release); + futex_signal(&l->bits); +} + +bool rwlock_try_acquire_upgrade(RWSpinLock *l) { + int value = l->bits.fetch_or(RWLOCK_UPGRADED, std::memory_order_acquire); + return (value & (RWLOCK_UPGRADED | RWLOCK_WRITER)) == 0; +} + +void rwlock_acquire_upgrade(RWSpinLock *l) { + while (!rwlock_try_acquire_upgrade(l)) { + futex_wait(&l->bits, RWLOCK_UPGRADED); + } +} +void rwlock_release_upgrade(RWSpinLock *l) { + l->bits.fetch_add(-RWLOCK_UPGRADED, std::memory_order_acq_rel); +} + +bool rwlock_try_release_upgrade_and_acquire_write(RWSpinLock *l) { + int expect = RWLOCK_UPGRADED; + return l->bits.compare_exchange_strong(expect, RWLOCK_WRITER, std::memory_order_acq_rel); +} + +void rwlock_release_upgrade_and_acquire_write(RWSpinLock *l) { + while (!rwlock_try_release_upgrade_and_acquire_write(l)) { + futex_wait(&l->bits, RWLOCK_WRITER); + } +} + + struct Parker { Futex state; }; diff --git a/src/types.cpp b/src/types.cpp index 6b94b1690..44f9394c7 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -334,6 +334,7 @@ struct Type { // NOTE(bill): These need to be at the end to not affect the unionized data std::atomic cached_size; std::atomic cached_align; + std::atomic canonical_hash; std::atomic flags; // TypeFlag bool failure; }; -- cgit v1.2.3 From 738a72943bdb9d0998b11d38efb5300cd02d8190 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 18 Sep 2025 15:04:16 +0100 Subject: Try moving parapoly procs into a separate module when doing weak monomorphization --- src/build_settings.cpp | 1 + src/llvm_backend.cpp | 2 +- src/llvm_backend.hpp | 2 ++ src/llvm_backend_debug.cpp | 2 +- src/llvm_backend_general.cpp | 63 ++++++++++++++++++++++++++++++++++++++------ src/llvm_backend_proc.cpp | 2 +- src/main.cpp | 5 ++++ 7 files changed, 66 insertions(+), 11 deletions(-) (limited to 'src/main.cpp') diff --git a/src/build_settings.cpp b/src/build_settings.cpp index 077660f10..867c80ac1 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -554,6 +554,7 @@ struct BuildContext { bool internal_no_inline; bool internal_by_value; + bool internal_weak_monomorphization; bool no_threaded_checker; diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 11b979774..2bc18872e 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -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); diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index cc3dcaa4a..e4b8c07fa 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -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 diff --git a/src/llvm_backend_debug.cpp b/src/llvm_backend_debug.cpp index 182920fc7..3372165f2 100644 --- a/src/llvm_backend_debug.cpp +++ b/src/llvm_backend_debug.cpp @@ -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); diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 49a04641c..4907e114d 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -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) { diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 06829efab..20d627fa2 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -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); diff --git a/src/main.cpp b/src/main.cpp index 184b1eaac..130c3f31d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 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 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; -- cgit v1.2.3 From 9cf69576ab8cb220af5802a04a0aa53dc92046a5 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 18 Sep 2025 20:58:24 +0100 Subject: More improvements to minimize code gen size --- base/runtime/core_builtin.odin | 22 +++++++++++++------ base/runtime/dynamic_map_internal.odin | 3 +++ src/build_settings.cpp | 1 + src/llvm_backend.cpp | 39 ++++++++++++++++++++++++++-------- src/llvm_backend.hpp | 4 ++-- src/llvm_backend_general.cpp | 6 +++--- src/main.cpp | 6 ++++++ 7 files changed, 60 insertions(+), 21 deletions(-) (limited to 'src/main.cpp') diff --git a/base/runtime/core_builtin.odin b/base/runtime/core_builtin.odin index 497c4b147..903ea7ed7 100644 --- a/base/runtime/core_builtin.odin +++ b/base/runtime/core_builtin.odin @@ -166,11 +166,17 @@ remove_range :: proc(array: ^$D/[dynamic]$T, #any_int lo, hi: int, loc := #calle @builtin pop :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> (res: E) #no_bounds_check { assert(len(array) > 0, loc=loc) - res = array[len(array)-1] - (^Raw_Dynamic_Array)(array).len -= 1 + _pop_type_erased(&res, (^Raw_Dynamic_Array)(array), size_of(E)) return res } +_pop_type_erased :: proc(res: rawptr, array: ^Raw_Dynamic_Array, elem_size: int, loc := #caller_location) { + end := rawptr(uintptr(array.data) + uintptr(elem_size*(array.len-1))) + intrinsics.mem_copy_non_overlapping(res, end, elem_size) + array.len -= 1 +} + + // `pop_safe` trys to remove and return the end value of dynamic array `array` and reduces the length of `array` by 1. // If the operation is not possible, it will return false. @@ -387,16 +393,18 @@ make_slice :: proc($T: typeid/[]$E, #any_int len: int, allocator := context.allo // // Note: Prefer using the procedure group `make`. @(builtin, require_results) -make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error { - return make_dynamic_array_len_cap(T, 0, 0, allocator, loc) +make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_allocator_error { + err = _make_dynamic_array_len_cap((^Raw_Dynamic_Array)(&array), size_of(E), align_of(E), 0, 0, allocator, loc) + return } // `make_dynamic_array_len` allocates and initializes a dynamic array. Like `new`, the first argument is a type, not a value. // Unlike `new`, `make`'s return value is the same as the type of its argument, not a pointer to it. // // Note: Prefer using the procedure group `make`. @(builtin, require_results) -make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error { - return make_dynamic_array_len_cap(T, len, len, allocator, loc) +make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_allocator_error { + err = _make_dynamic_array_len_cap((^Raw_Dynamic_Array)(&array), size_of(E), align_of(E), len, len, allocator, loc) + return } // `make_dynamic_array_len_cap` allocates and initializes a dynamic array. Like `new`, the first argument is a type, not a value. // Unlike `new`, `make`'s return value is the same as the type of its argument, not a pointer to it. @@ -501,7 +509,7 @@ clear_map :: proc "contextless" (m: ^$T/map[$K]$V) { // Note: Prefer the procedure group `reserve` @builtin reserve_map :: proc(m: ^$T/map[$K]$V, #any_int capacity: int, loc := #caller_location) -> Allocator_Error { - return __dynamic_map_reserve((^Raw_Map)(m), map_info(T), uint(capacity), loc) if m != nil else nil + return __dynamic_map_reserve((^Raw_Map)(m), map_info(T), uint(capacity), loc) } // Shrinks the capacity of a map down to the current length. diff --git a/base/runtime/dynamic_map_internal.odin b/base/runtime/dynamic_map_internal.odin index 7b65a2fa0..e288d1f53 100644 --- a/base/runtime/dynamic_map_internal.odin +++ b/base/runtime/dynamic_map_internal.odin @@ -985,6 +985,9 @@ __dynamic_map_entry :: proc "odin" (#no_alias m: ^Raw_Map, #no_alias info: ^Map_ // IMPORTANT: USED WITHIN THE COMPILER @(private) __dynamic_map_reserve :: proc "odin" (#no_alias m: ^Raw_Map, #no_alias info: ^Map_Info, new_capacity: uint, loc := #caller_location) -> Allocator_Error { + if m == nil { + return nil + } return map_reserve_dynamic(m, info, uintptr(new_capacity), loc) } diff --git a/src/build_settings.cpp b/src/build_settings.cpp index 867c80ac1..54f11a42d 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -555,6 +555,7 @@ struct BuildContext { bool internal_no_inline; bool internal_by_value; bool internal_weak_monomorphization; + bool internal_ignore_llvm_verification; bool no_threaded_checker; diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 46c319a90..5d2accd90 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -8,7 +8,11 @@ #endif #ifndef LLVM_IGNORE_VERIFICATION -#define LLVM_IGNORE_VERIFICATION 0 +#define LLVM_IGNORE_VERIFICATION build_context.internal_ignore_llvm_verification +#endif + +#ifndef LLVM_WEAK_MONOMORPHIZATION +#define LLVM_WEAK_MONOMORPHIZATION build_context.internal_weak_monomorphization #endif @@ -620,6 +624,7 @@ gb_internal lbValue lb_hasher_proc_for_type(lbModule *m, Type *type) { #define LLVM_SET_VALUE_NAME(value, name) LLVMSetValueName2((value), (name), gb_count_of((name))-1); + gb_internal lbValue lb_map_get_proc_for_type(lbModule *m, Type *type) { GB_ASSERT(!build_context.dynamic_map_calls); type = base_type(type); @@ -634,6 +639,9 @@ gb_internal lbValue lb_map_get_proc_for_type(lbModule *m, Type *type) { lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_map_get_proc); string_map_set(&m->gen_procs, proc_name, p); + + p->internal_gen_type = type; + lb_begin_procedure_body(p); defer (lb_end_procedure_body(p)); @@ -1891,6 +1899,10 @@ gb_internal void lb_verify_function(lbModule *m, lbProcedure *p, bool dump_ll=fa } gb_internal WORKER_TASK_PROC(lb_llvm_module_verification_worker_proc) { + if (LLVM_IGNORE_VERIFICATION) { + return 0; + } + char *llvm_error = nullptr; defer (LLVMDisposeMessage(llvm_error)); lbModule *m = cast(lbModule *)data; @@ -2298,6 +2310,14 @@ gb_internal WORKER_TASK_PROC(lb_llvm_function_pass_per_module) { } +void lb_remove_unused_functions_and_globals(lbGenerator *gen) { + for (auto &entry : gen->modules) { + lbModule *m = entry.value; + lb_run_remove_unused_function_pass(m); + lb_run_remove_unused_globals_pass(m); + } +} + struct lbLLVMModulePassWorkerData { lbModule *m; LLVMTargetMachineRef target_machine; @@ -2307,9 +2327,6 @@ struct lbLLVMModulePassWorkerData { gb_internal WORKER_TASK_PROC(lb_llvm_module_pass_worker_proc) { auto wd = cast(lbLLVMModulePassWorkerData *)data; - lb_run_remove_unused_function_pass(wd->m); - lb_run_remove_unused_globals_pass(wd->m); - LLVMPassManagerRef module_pass_manager = LLVMCreatePassManager(); lb_populate_module_pass_manager(wd->target_machine, module_pass_manager, build_context.optimization_level); LLVMRunPassManager(module_pass_manager, wd->m->mod); @@ -2386,6 +2403,10 @@ gb_internal WORKER_TASK_PROC(lb_llvm_module_pass_worker_proc) { } #endif + if (LLVM_IGNORE_VERIFICATION) { + return 0; + } + if (wd->do_threading) { thread_pool_add_task(lb_llvm_module_verification_worker_proc, wd->m); } else { @@ -3464,12 +3485,14 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { TIME_SECTION("LLVM Function Pass"); lb_llvm_function_passes(gen, do_threading && !build_context.ODIN_DEBUG); + TIME_SECTION("LLVM Remove Unused Functions and Globals"); + lb_remove_unused_functions_and_globals(gen); + TIME_SECTION("LLVM Module Pass and Verification"); lb_llvm_module_passes_and_verification(gen, do_threading); - if (gen->module_verification_failed.load(std::memory_order_relaxed)) { - return false; - } + TIME_SECTION("LLVM Correct Entity Linkage"); + lb_correct_entity_linkage(gen); llvm_error = nullptr; defer (LLVMDisposeMessage(llvm_error)); @@ -3497,8 +3520,6 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { } } - TIME_SECTION("LLVM Correct Entity Linkage"); - lb_correct_entity_linkage(gen); //////////////////////////////////////////// for (auto const &entry: gen->modules) { diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 5d6f56433..698e7657f 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -241,8 +241,6 @@ struct lbGenerator : LinkerData { isize used_module_count; - std::atomic module_verification_failed; - lbProcedure *startup_runtime; lbProcedure *cleanup_runtime; lbProcedure *objc_names; @@ -409,6 +407,8 @@ struct lbProcedure { void (*generate_body)(lbModule *m, lbProcedure *p); Array *global_variables; lbProcedure *objc_names; + + Type *internal_gen_type; // map_set, map_get, etc. }; diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 3ac405e0b..fb9fa4cea 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -154,7 +154,7 @@ gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c) { map_set(&gen->modules, cast(void *)pkg, m); lb_init_module(m, c); - if (build_context.internal_weak_monomorphization) { + if (LLVM_WEAK_MONOMORPHIZATION) { auto pm = gb_alloc_item(permanent_allocator(), lbModule); pm->pkg = pkg; pm->gen = gen; @@ -181,7 +181,7 @@ gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c) { lb_init_module(m, c); - if (build_context.internal_weak_monomorphization) { + if (LLVM_WEAK_MONOMORPHIZATION) { auto pm = gb_alloc_item(permanent_allocator(), lbModule); pm->file = file; pm->pkg = pkg; @@ -469,7 +469,7 @@ gb_internal lbModule *lb_module_of_entity(lbGenerator *gen, Entity *e, lbModule 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 (USE_SEPARATE_MODULES) { if (e->kind == Entity_Procedure && e->Procedure.generated_from_polymorphic) { if (m->polymorphic_module) { return m->polymorphic_module; diff --git a/src/main.cpp b/src/main.cpp index 130c3f31d..bbaf6f23f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -404,6 +404,7 @@ enum BuildFlagKind { BuildFlag_InternalNoInline, BuildFlag_InternalByValue, BuildFlag_InternalWeakMonomorphization, + BuildFlag_InternalLLVMVerification, BuildFlag_Tilde, @@ -628,6 +629,7 @@ gb_internal bool parse_build_flags(Array args) { 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); + add_flag(&build_flags, BuildFlag_InternalLLVMVerification, str_lit("internal-ignore-llvm-verification"), BuildFlagParam_None, Command_all); #if ALLOW_TILDE add_flag(&build_flags, BuildFlag_Tilde, str_lit("tilde"), BuildFlagParam_None, Command__does_build); @@ -1589,6 +1591,10 @@ gb_internal bool parse_build_flags(Array args) { case BuildFlag_InternalWeakMonomorphization: build_context.internal_weak_monomorphization = true; break; + case BuildFlag_InternalLLVMVerification: + build_context.internal_ignore_llvm_verification = true; + break; + case BuildFlag_Tilde: build_context.tilde_backend = true; -- cgit v1.2.3 From ade4eafcad6de7462df6d26fccde86a36dea5883 Mon Sep 17 00:00:00 2001 From: samwega Date: Fri, 26 Sep 2025 11:53:53 +0300 Subject: -fix: typo --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index be0ea8b82..acc4773c0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2837,7 +2837,7 @@ gb_internal int print_show_help(String const arg0, String command, String option print_usage_line(2, "Errs on unneeded tokens, such as unneeded semicolons."); print_usage_line(2, "Errs on missing trailing commas followed by a newline."); print_usage_line(2, "Errs on deprecated syntax."); - print_usage_line(2, "Errs when the attached-brace style in not adhered to (also known as 1TBS)."); + print_usage_line(2, "Errs when the attached-brace style is not adhered to (also known as 1TBS)."); print_usage_line(2, "Errs when 'case' labels are not in the same column as the associated 'switch' token."); } } -- cgit v1.2.3 From 53f4fc1cbbd58396241264785dc1c8a75798f062 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 29 Sep 2025 14:03:32 +0100 Subject: Add `-para-poly-diagnostics` --- src/build_settings.cpp | 2 + src/check_expr.cpp | 1 + src/checker.hpp | 2 + src/llvm_backend.cpp | 4 + src/llvm_backend_utility.cpp | 180 +++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 7 ++ 6 files changed, 196 insertions(+) (limited to 'src/main.cpp') diff --git a/src/build_settings.cpp b/src/build_settings.cpp index a8d74d1da..abf8e6809 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -548,6 +548,8 @@ struct BuildContext { bool ignore_microsoft_magic; bool linker_map_file; + bool para_poly_diagnostics; + bool use_single_module; bool use_separate_modules; bool module_per_file; diff --git a/src/check_expr.cpp b/src/check_expr.cpp index a59f145c7..2a22e5c48 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -587,6 +587,7 @@ gb_internal bool find_or_generate_polymorphic_procedure(CheckerContext *old_c, E d->proc_lit = proc_lit; d->proc_checked_state = ProcCheckedState_Unchecked; d->defer_use_checked = false; + d->para_poly_original = old_decl->entity; Entity *entity = alloc_entity_procedure(nullptr, token, final_proc_type, tags); entity->state.store(EntityState_Resolved); diff --git a/src/checker.hpp b/src/checker.hpp index 9693c3e38..bda7b2746 100644 --- a/src/checker.hpp +++ b/src/checker.hpp @@ -218,6 +218,8 @@ struct DeclInfo { Ast * proc_lit; // Ast_ProcLit Type * gen_proc_type; // Precalculated + Entity * para_poly_original; + bool is_using; bool where_clauses_evaluated; bool foreign_require_results; diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index ec9630a47..b47e2788f 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -3556,6 +3556,10 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { TIME_SECTION("LLVM Correct Entity Linkage"); lb_correct_entity_linkage(gen); + if (build_context.para_poly_diagnostics) { + lb_do_para_poly_diagnostics(gen); + } + llvm_error = nullptr; defer (LLVMDisposeMessage(llvm_error)); diff --git a/src/llvm_backend_utility.cpp b/src/llvm_backend_utility.cpp index f7807364a..fd3214f24 100644 --- a/src/llvm_backend_utility.cpp +++ b/src/llvm_backend_utility.cpp @@ -2787,3 +2787,183 @@ gb_internal LLVMAtomicOrdering llvm_atomic_ordering_from_odin(Ast *expr) { ExactValue value = type_and_value_of_expr(expr).value; return llvm_atomic_ordering_from_odin(value); } + + + +struct lbParaPolyEntry { + Entity *entity; + String canonical_name; + isize count; + isize total_code_size; +}; + +gb_internal isize lb_total_code_size(lbProcedure *p) { + isize instruction_count = 0; + + LLVMBasicBlockRef first = LLVMGetFirstBasicBlock(p->value); + + for (LLVMBasicBlockRef block = first; block != nullptr; block = LLVMGetNextBasicBlock(block)) { + for (LLVMValueRef instr = LLVMGetFirstInstruction(block); instr != nullptr; instr = LLVMGetNextInstruction(instr)) { + instruction_count += 1; + } + } + return instruction_count; + +} + +gb_internal void lb_do_para_poly_diagnostics(lbGenerator *gen) { + PtrMap procs = {}; + map_init(&procs); + defer (map_destroy(&procs)); + + for (auto &entry : gen->modules) { + lbModule *m = entry.value; + for (lbProcedure *p : m->generated_procedures) { + Entity *e = p->entity; + if (e == nullptr) { + continue; + } + if (p->builder == nullptr) { + continue; + } + + DeclInfo *d = e->decl_info; + Entity *para_poly_parent = d->para_poly_original; + if (para_poly_parent == nullptr) { + continue; + } + + lbParaPolyEntry *entry = map_get(&procs, para_poly_parent); + if (entry == nullptr) { + lbParaPolyEntry entry = {}; + entry.entity = para_poly_parent; + entry.count = 0; + + + gbString w = string_canonical_entity_name(permanent_allocator(), entry.entity); + String name = make_string_c(w); + + for (isize i = 0; i < name.len; i++) { + String s = substring(name, i, name.len); + if (string_starts_with(s, str_lit(":proc"))) { + name = substring(name, 0, i); + break; + } + } + + entry.canonical_name = name; + + map_set(&procs, para_poly_parent, entry); + } + entry = map_get(&procs, para_poly_parent); + GB_ASSERT(entry != nullptr); + entry->count += 1; + entry->total_code_size += lb_total_code_size(p); + } + } + + + auto entries = array_make(heap_allocator(), 0, procs.count); + defer (array_free(&entries)); + + for (auto &entry : procs) { + array_add(&entries, entry.value); + } + + array_sort(entries, [](void const *a, void const *b) -> int { + lbParaPolyEntry *x = cast(lbParaPolyEntry *)a; + lbParaPolyEntry *y = cast(lbParaPolyEntry *)b; + if (x->total_code_size > y->total_code_size) { + return -1; + } + if (x->total_code_size < y->total_code_size) { + return +1; + } + return string_compare(x->canonical_name, y->canonical_name); + }); + + + gb_printf("Parametric Polymorphic Procedure Diagnostics\n"); + gb_printf("------------------------------------------------------------------------------------------\n"); + + gb_printf("Sorted by Total Instruction Count Descending (Top 100)\n\n"); + gb_printf("Total Instruction Count | Instantiation Count | Average Instruction Count | Procedure Name\n"); + + isize max_count = 100; + for (auto &entry : entries) { + isize code_size = entry.total_code_size; + isize count = entry.count; + String name = entry.canonical_name; + + f64 average = cast(f64)code_size / cast(f64)gb_max(count, 1); + + gb_printf("%23td | %19d | %25.2f | %.*s\n", code_size, count, average, LIT(name)); + if (max_count-- <= 0) { + break; + } + } + + gb_printf("------------------------------------------------------------------------------------------\n"); + + array_sort(entries, [](void const *a, void const *b) -> int { + lbParaPolyEntry *x = cast(lbParaPolyEntry *)a; + lbParaPolyEntry *y = cast(lbParaPolyEntry *)b; + if (x->count > y->count) { + return -1; + } + if (x->count < y->count) { + return +1; + } + + return string_compare(x->canonical_name, y->canonical_name); + }); + + gb_printf("Sorted by Total Instantiation Count Descending (Top 100)\n\n"); + gb_printf("Instantiation Count | Total Instruction Count | Average Instruction Count | Procedure Name\n"); + + max_count = 100; + for (auto &entry : entries) { + isize code_size = entry.total_code_size; + isize count = entry.count; + String name = entry.canonical_name; + + + f64 average = cast(f64)code_size / cast(f64)gb_max(count, 1); + + gb_printf("%19d | %23td | %25.2f | %.*s\n", count, code_size, average, LIT(name)); + if (max_count-- <= 0) { + break; + } + } + + gb_printf("------------------------------------------------------------------------------------------\n"); + + + array_sort(entries, [](void const *a, void const *b) -> int { + lbParaPolyEntry *x = cast(lbParaPolyEntry *)a; + lbParaPolyEntry *y = cast(lbParaPolyEntry *)b; + if (x->count < y->count) { + return -1; + } + if (x->count > y->count) { + return +1; + } + + return string_compare(x->canonical_name, y->canonical_name); + }); + + gb_printf("Single Instanced Parametric Polymorphic Procedures\n\n"); + gb_printf("Instruction Count | Procedure Name\n"); + for (auto &entry : entries) { + isize code_size = entry.total_code_size; + isize count = entry.count; + String name = entry.canonical_name; + if (count != 1) { + break; + } + + gb_printf("%17td | %.*s\n", code_size, LIT(name)); + } + + gb_printf("------------------------------------------------------------------------------------------\n"); +} diff --git a/src/main.cpp b/src/main.cpp index acc4773c0..707b85232 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -394,6 +394,8 @@ enum BuildFlagKind { BuildFlag_IntegerDivisionByZero, + BuildFlag_ParaPolyDiagnostics, + // internal use only BuildFlag_InternalFastISel, BuildFlag_InternalIgnoreLazy, @@ -619,6 +621,7 @@ gb_internal bool parse_build_flags(Array args) { add_flag(&build_flags, BuildFlag_IntegerDivisionByZero, str_lit("integer-division-by-zero"), BuildFlagParam_String, Command__does_check); + add_flag(&build_flags, BuildFlag_ParaPolyDiagnostics, str_lit("para-poly-diagnostics"), BuildFlagParam_None, Command__does_build); add_flag(&build_flags, BuildFlag_InternalFastISel, str_lit("internal-fast-isel"), BuildFlagParam_None, Command_all); add_flag(&build_flags, BuildFlag_InternalIgnoreLazy, str_lit("internal-ignore-lazy"), BuildFlagParam_None, Command_all); @@ -1562,6 +1565,10 @@ gb_internal bool parse_build_flags(Array args) { } break; + case BuildFlag_ParaPolyDiagnostics: + build_context.para_poly_diagnostics = true; + break; + case BuildFlag_InternalFastISel: build_context.fast_isel = true; break; -- cgit v1.2.3 From 9e8be055c1152bd42f533f544308355de87a361e Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 29 Sep 2025 16:16:19 +0100 Subject: Rename to `-build-diagnostics` --- src/build_settings.cpp | 2 +- src/llvm_backend.cpp | 4 ++-- src/llvm_backend_utility.cpp | 2 +- src/main.cpp | 8 ++++---- 4 files changed, 8 insertions(+), 8 deletions(-) (limited to 'src/main.cpp') diff --git a/src/build_settings.cpp b/src/build_settings.cpp index abf8e6809..53953600e 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -548,7 +548,7 @@ struct BuildContext { bool ignore_microsoft_magic; bool linker_map_file; - bool para_poly_diagnostics; + bool build_diagnostics; bool use_single_module; bool use_separate_modules; diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index bbfd91d30..ab0811085 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -3571,8 +3571,8 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { TIME_SECTION("LLVM Correct Entity Linkage"); lb_correct_entity_linkage(gen); - if (build_context.para_poly_diagnostics) { - lb_do_code_gen_diagnostics(gen); + if (build_context.build_diagnostics) { + lb_do_build_diagnostics(gen); } llvm_error = nullptr; diff --git a/src/llvm_backend_utility.cpp b/src/llvm_backend_utility.cpp index 244ad6d73..fa8f85d0f 100644 --- a/src/llvm_backend_utility.cpp +++ b/src/llvm_backend_utility.cpp @@ -3054,7 +3054,7 @@ gb_internal void lb_do_module_diagnostics(lbGenerator *gen) { } -gb_internal void lb_do_code_gen_diagnostics(lbGenerator *gen) { +gb_internal void lb_do_build_diagnostics(lbGenerator *gen) { lb_do_para_poly_diagnostics(gen); gb_printf("------------------------------------------------------------------------------------------\n"); gb_printf("------------------------------------------------------------------------------------------\n\n"); diff --git a/src/main.cpp b/src/main.cpp index 707b85232..6ad20cf26 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -394,7 +394,7 @@ enum BuildFlagKind { BuildFlag_IntegerDivisionByZero, - BuildFlag_ParaPolyDiagnostics, + BuildFlag_BuildDiagnostics, // internal use only BuildFlag_InternalFastISel, @@ -621,7 +621,7 @@ gb_internal bool parse_build_flags(Array args) { add_flag(&build_flags, BuildFlag_IntegerDivisionByZero, str_lit("integer-division-by-zero"), BuildFlagParam_String, Command__does_check); - add_flag(&build_flags, BuildFlag_ParaPolyDiagnostics, str_lit("para-poly-diagnostics"), BuildFlagParam_None, Command__does_build); + add_flag(&build_flags, BuildFlag_BuildDiagnostics, str_lit("build-diagnostics"), BuildFlagParam_None, Command__does_build); add_flag(&build_flags, BuildFlag_InternalFastISel, str_lit("internal-fast-isel"), BuildFlagParam_None, Command_all); add_flag(&build_flags, BuildFlag_InternalIgnoreLazy, str_lit("internal-ignore-lazy"), BuildFlagParam_None, Command_all); @@ -1565,8 +1565,8 @@ gb_internal bool parse_build_flags(Array args) { } break; - case BuildFlag_ParaPolyDiagnostics: - build_context.para_poly_diagnostics = true; + case BuildFlag_BuildDiagnostics: + build_context.build_diagnostics = true; break; case BuildFlag_InternalFastISel: -- cgit v1.2.3 From a672c68f03223efcab8f487d1bef05edaf5cf266 Mon Sep 17 00:00:00 2001 From: Yhya Ibrahim Date: Sat, 11 Oct 2025 15:26:01 +0300 Subject: Make `-target-features` flag support disabling of a feature --- src/llvm_backend.cpp | 5 ++++- src/main.cpp | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) (limited to 'src/main.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index c2decf091..9256acbb2 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -3055,7 +3055,10 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { } first = false; - llvm_features = gb_string_appendc(llvm_features, "+"); + if (*str.text != '+' && *str.text != '-') { + llvm_features = gb_string_appendc(llvm_features, "+"); + } + llvm_features = gb_string_append_length(llvm_features, str.text, str.len); } diff --git a/src/main.cpp b/src/main.cpp index 6ad20cf26..2d58cf218 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3719,6 +3719,11 @@ int main(int arg_count, char const **arg_ptr) { String item = string_split_iterator(&target_it, ','); if (item == "") break; + if (*item.text == '+' || *item.text == '-') { + item.text++; + item.len--; + } + String invalid; if (!check_target_feature_is_valid_for_target_arch(item, &invalid) && item != str_lit("help")) { if (item != str_lit("?")) { -- cgit v1.2.3 From 937161f68d39296e00215067c9d51737ac110a3e Mon Sep 17 00:00:00 2001 From: Harold Brenes Date: Sun, 12 Oct 2025 00:50:39 -0400 Subject: Add `-export-linked-libs-file` build flag. This build flag writes a list of the libraries that were linked during the build to the specified file. --- src/build_settings.cpp | 2 ++ src/main.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) (limited to 'src/main.cpp') diff --git a/src/build_settings.cpp b/src/build_settings.cpp index 53953600e..29a738c5a 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -575,6 +575,8 @@ struct BuildContext { bool min_link_libs; + String export_linked_libs_path; + bool print_linker_flags; RelocMode reloc_mode; diff --git a/src/main.cpp b/src/main.cpp index 6ad20cf26..1f619499c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -391,6 +391,7 @@ enum BuildFlagKind { BuildFlag_MinLinkLibs, BuildFlag_PrintLinkerFlags, + BuildFlag_ExportLinkedLibraries, BuildFlag_IntegerDivisionByZero, @@ -616,6 +617,7 @@ gb_internal bool parse_build_flags(Array args) { add_flag(&build_flags, BuildFlag_MaxErrorCount, str_lit("max-error-count"), BuildFlagParam_Integer, Command_all); add_flag(&build_flags, BuildFlag_MinLinkLibs, str_lit("min-link-libs"), BuildFlagParam_None, Command__does_build); + add_flag(&build_flags, BuildFlag_ExportLinkedLibraries, str_lit("export-linked-libs-file"), BuildFlagParam_String, Command__does_check); add_flag(&build_flags, BuildFlag_PrintLinkerFlags, str_lit("print-linker-flags"), BuildFlagParam_None, Command_build); @@ -1547,6 +1549,14 @@ gb_internal bool parse_build_flags(Array args) { build_context.min_link_libs = true; break; + case BuildFlag_ExportLinkedLibraries: + build_context.export_linked_libs_path = string_trim_whitespace(value.value_string); + if (build_context.export_linked_libs_path.len == 0) { + gb_printf_err("-%.*s specified an empty path\n", LIT(name)); + bad_flags = true; + } + break; + case BuildFlag_PrintLinkerFlags: build_context.print_linker_flags = true; break; @@ -2262,6 +2272,63 @@ gb_internal void export_dependencies(Checker *c) { } } +gb_internal void export_linked_libraries(LinkerData *gen) { + gbFile f = {}; + char * fileName = (char *)build_context.export_linked_libs_path.text; + gbFileError err = gb_file_open_mode(&f, gbFileMode_Write, fileName); + if (err != gbFileError_None) { + gb_printf_err("Failed to export linked library list to: %s\n", fileName); + exit_with_errors(); + return; + } + defer (gb_file_close(&f)); + + StringSet min_libs_set = {}; + string_set_init(&min_libs_set, 64); + defer (string_set_destroy(&min_libs_set)); + + for (auto *e : gen->foreign_libraries) { + GB_ASSERT(e->kind == Entity_LibraryName); + + for (auto lib_path : e->LibraryName.paths) { + lib_path = string_trim_whitespace(lib_path); + if (lib_path.len == 0) { + continue; + } + + if (string_set_update(&min_libs_set, lib_path)) { + continue; + } + + gb_fprintf(&f, "%.*s\t", LIT(lib_path)); + + String ext = path_extension(lib_path, false); + if (str_eq_ignore_case(ext, "a") || str_eq_ignore_case(ext, "lib") || + str_eq_ignore_case(ext, "o") || str_eq_ignore_case(ext, "obj") + ) { + gb_fprintf(&f, "static"); + } else { + gb_fprintf(&f, "dynamic"); + } + + gb_fprintf(&f, "\t"); + ast_node(imp, ForeignImportDecl, e->LibraryName.decl); + for (Ast* file_path : imp->filepaths) { + GB_ASSERT(file_path->kind == Ast_BasicLit); + + String file_path_str = file_path->BasicLit.token.string; + if (string_starts_with(file_path_str, str_lit("\"system:"))) { + gb_fprintf(&f, "system"); + } else { + gb_fprintf(&f, "user"); + } + } + + gb_fprintf(&f, "\n"); + } + } +} + gb_internal void remove_temp_files(lbGenerator *gen) { if (build_context.keep_temp_files) return; @@ -3935,6 +4002,10 @@ int main(int arg_count, char const **arg_ptr) { export_dependencies(checker); } return result; + } else { + if (build_context.export_linked_libs_path != "") { + export_linked_libraries(gen); + } } break; } -- cgit v1.2.3 From 0de3de72571214c2cecc21d76121510c92192ce3 Mon Sep 17 00:00:00 2001 From: Harold Brenes Date: Sun, 12 Oct 2025 01:57:29 -0400 Subject: Use exact value to determine system linked libs with `-export-linked-libs-file` --- src/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 1f619499c..945f0c3cf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2314,9 +2314,9 @@ gb_internal void export_linked_libraries(LinkerData *gen) { gb_fprintf(&f, "\t"); ast_node(imp, ForeignImportDecl, e->LibraryName.decl); for (Ast* file_path : imp->filepaths) { - GB_ASSERT(file_path->kind == Ast_BasicLit); + GB_ASSERT(file_path->tav.mode == Addressing_Constant && file_path->tav.value.kind == ExactValue_String); + String file_path_str = file_path->tav.value.value_string; - String file_path_str = file_path->BasicLit.token.string; if (string_starts_with(file_path_str, str_lit("\"system:"))) { gb_fprintf(&f, "system"); } else { -- cgit v1.2.3 From 612b219230db3e0d9a56087f18780dffa5a163d0 Mon Sep 17 00:00:00 2001 From: Harold Brenes Date: Sun, 12 Oct 2025 02:00:18 -0400 Subject: Fix `-export-linked-libs-file` system lib check --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 945f0c3cf..7a41abdf8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2317,7 +2317,7 @@ gb_internal void export_linked_libraries(LinkerData *gen) { GB_ASSERT(file_path->tav.mode == Addressing_Constant && file_path->tav.value.kind == ExactValue_String); String file_path_str = file_path->tav.value.value_string; - if (string_starts_with(file_path_str, str_lit("\"system:"))) { + if (string_starts_with(file_path_str, str_lit("system:"))) { gb_fprintf(&f, "system"); } else { gb_fprintf(&f, "user"); -- cgit v1.2.3 From 0605c7b0e3882edd940b85a41a0b8f3c859c28cc Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Wed, 22 Oct 2025 10:27:27 +0200 Subject: Add target triple and LLVM code mode debug prints (#5839) --- src/llvm_backend.cpp | 6 ++++++ src/main.cpp | 1 + 2 files changed, 7 insertions(+) (limited to 'src/main.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 23efa8d22..7742fb39d 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -3036,8 +3036,14 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { LLVMCodeModel code_mode = LLVMCodeModelDefault; if (is_arch_wasm()) { code_mode = LLVMCodeModelJITDefault; + debugf("LLVM code mode: LLVMCodeModelJITDefault\n"); } else if (is_arch_x86() && build_context.metrics.os == TargetOs_freestanding) { code_mode = LLVMCodeModelKernel; + debugf("LLVM code mode: LLVMCodeModelKernel\n"); + } + + if (code_mode == LLVMCodeModelDefault) { + debugf("LLVM code mode: LLVMCodeModelDefault\n"); } String llvm_cpu = get_final_microarchitecture(); diff --git a/src/main.cpp b/src/main.cpp index 5fcdedd99..4cc53f319 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3840,6 +3840,7 @@ int main(int arg_count, char const **arg_ptr) { if (build_context.show_debug_messages) { debugf("Selected microarch: %.*s\n", LIT(march)); debugf("Default microarch features: %.*s\n", LIT(default_features)); + debugf("Target triplet: %.*s\n", LIT(build_context.metrics.target_triplet)); for_array(i, build_context.build_paths) { String build_path = path_to_string(heap_allocator(), build_context.build_paths[i]); debugf("build_paths[%ld]: %.*s\n", i, LIT(build_path)); -- cgit v1.2.3 From f726f7aff4fca97afb348727cd26e56be2d0cb97 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Mon, 27 Oct 2025 16:23:54 +0100 Subject: Make Karl Zylinski happy --- src/build_settings.cpp | 1 + src/main.cpp | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) (limited to 'src/main.cpp') diff --git a/src/build_settings.cpp b/src/build_settings.cpp index 7a0952583..757df95f7 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -509,6 +509,7 @@ struct BuildContext { bool show_more_timings; bool show_defineables; String export_defineables_file; + bool ignore_unused_defineables; bool show_system_calls; bool keep_temp_files; bool ignore_unknown_attributes; diff --git a/src/main.cpp b/src/main.cpp index 4cc53f319..0af589339 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -334,6 +334,7 @@ enum BuildFlagKind { BuildFlag_ShowDefineables, BuildFlag_ExportDefineables, + BuildFlag_IgnoreUnusedDefineables, BuildFlag_Vet, BuildFlag_VetShadowing, @@ -563,6 +564,7 @@ gb_internal bool parse_build_flags(Array args) { add_flag(&build_flags, BuildFlag_ShowDefineables, str_lit("show-defineables"), BuildFlagParam_None, Command__does_check); add_flag(&build_flags, BuildFlag_ExportDefineables, str_lit("export-defineables"), BuildFlagParam_String, Command__does_check); + add_flag(&build_flags, BuildFlag_IgnoreUnusedDefineables, str_lit("ignore-unused-defineables"), BuildFlagParam_None, Command__does_check); add_flag(&build_flags, BuildFlag_Vet, str_lit("vet"), BuildFlagParam_None, Command__does_check); add_flag(&build_flags, BuildFlag_VetUnused, str_lit("vet-unused"), BuildFlagParam_None, Command__does_check); @@ -945,6 +947,11 @@ gb_internal bool parse_build_flags(Array args) { break; } + case BuildFlag_IgnoreUnusedDefineables: { + GB_ASSERT(value.kind == ExactValue_Invalid); + build_context.ignore_unused_defineables = true; + break; + } case BuildFlag_ShowSystemCalls: { GB_ASSERT(value.kind == ExactValue_Invalid); build_context.show_system_calls = true; @@ -2882,6 +2889,10 @@ gb_internal int print_show_help(String const arg0, String command, String option print_usage_line(2, "Shows an overview of all the #config/#defined usages in the project."); } + if (print_flag("-ignore-unused-defineables")) { + print_usage_line(2, "Silence warning/error if a -define doesn't have at least one #config/#defined usage."); + } + if (print_flag("-show-system-calls")) { print_usage_line(2, "Prints the whole command and arguments for calls to external tools like linker and assembler."); } @@ -3891,7 +3902,9 @@ int main(int arg_count, char const **arg_ptr) { MAIN_TIME_SECTION("type check"); check_parsed_files(checker); - check_defines(&build_context, checker); + if (!build_context.ignore_unused_defineables) { + check_defines(&build_context, checker); + } if (any_errors()) { print_all_errors(); return 1; -- cgit v1.2.3 From e05c21522d0613b585013af1ee892cb9ff139512 Mon Sep 17 00:00:00 2001 From: Harold Brenes Date: Tue, 28 Oct 2025 20:21:24 -0400 Subject: Fix `-export-linked-libs-file` issue where it multiple libraries in a single foreign export. --- src/main.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 4cc53f319..a0736d92a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2289,9 +2289,10 @@ gb_internal void export_linked_libraries(LinkerData *gen) { for (auto *e : gen->foreign_libraries) { GB_ASSERT(e->kind == Entity_LibraryName); + ast_node(imp, ForeignImportDecl, e->LibraryName.decl); - for (auto lib_path : e->LibraryName.paths) { - lib_path = string_trim_whitespace(lib_path); + for (isize i = 0; i < e->LibraryName.paths.count; i++) { + String lib_path = string_trim_whitespace(e->LibraryName.paths[i]); if (lib_path.len == 0) { continue; } @@ -2312,16 +2313,15 @@ gb_internal void export_linked_libraries(LinkerData *gen) { } gb_fprintf(&f, "\t"); - ast_node(imp, ForeignImportDecl, e->LibraryName.decl); - for (Ast* file_path : imp->filepaths) { - GB_ASSERT(file_path->tav.mode == Addressing_Constant && file_path->tav.value.kind == ExactValue_String); - String file_path_str = file_path->tav.value.value_string; - if (string_starts_with(file_path_str, str_lit("system:"))) { - gb_fprintf(&f, "system"); - } else { - gb_fprintf(&f, "user"); - } + Ast *file_path = imp->filepaths[i]; + GB_ASSERT(file_path->tav.mode == Addressing_Constant && file_path->tav.value.kind == ExactValue_String); + String file_path_str = file_path->tav.value.value_string; + + if (string_starts_with(file_path_str, str_lit("system:"))) { + gb_fprintf(&f, "system"); + } else { + gb_fprintf(&f, "user"); } gb_fprintf(&f, "\n"); -- cgit v1.2.3 From 411a5b6401d067d7f5d1f37dd08fee6f533ae89a Mon Sep 17 00:00:00 2001 From: A1029384756 Date: Sat, 22 Nov 2025 23:08:23 -0500 Subject: moved checking of -vet-unused-procedures and -vet-packages flags to after all flags are parsed --- src/main.cpp | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 83e7d688c..beec9ef31 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1309,23 +1309,17 @@ gb_internal bool parse_build_flags(Array args) { build_context.vet_flags |= VetFlag_All; break; - case BuildFlag_VetUnusedVariables: build_context.vet_flags |= VetFlag_UnusedVariables; break; - case BuildFlag_VetUnusedImports: build_context.vet_flags |= VetFlag_UnusedImports; break; - case BuildFlag_VetUnused: build_context.vet_flags |= VetFlag_Unused; break; - case BuildFlag_VetShadowing: build_context.vet_flags |= VetFlag_Shadowing; break; - case BuildFlag_VetUsingStmt: build_context.vet_flags |= VetFlag_UsingStmt; break; - case BuildFlag_VetUsingParam: build_context.vet_flags |= VetFlag_UsingParam; break; - case BuildFlag_VetStyle: build_context.vet_flags |= VetFlag_Style; break; - case BuildFlag_VetSemicolon: build_context.vet_flags |= VetFlag_Semicolon; break; - case BuildFlag_VetCast: build_context.vet_flags |= VetFlag_Cast; break; - case BuildFlag_VetTabs: build_context.vet_flags |= VetFlag_Tabs; break; - case BuildFlag_VetUnusedProcedures: - build_context.vet_flags |= VetFlag_UnusedProcedures; - if (!set_flags[BuildFlag_VetPackages]) { - gb_printf_err("-%.*s must be used with -vet-packages\n", LIT(name)); - bad_flags = true; - } - break; + case BuildFlag_VetUnusedVariables: build_context.vet_flags |= VetFlag_UnusedVariables; break; + case BuildFlag_VetUnusedImports: build_context.vet_flags |= VetFlag_UnusedImports; break; + case BuildFlag_VetUnused: build_context.vet_flags |= VetFlag_Unused; break; + case BuildFlag_VetShadowing: build_context.vet_flags |= VetFlag_Shadowing; break; + case BuildFlag_VetUsingStmt: build_context.vet_flags |= VetFlag_UsingStmt; break; + case BuildFlag_VetUsingParam: build_context.vet_flags |= VetFlag_UsingParam; break; + case BuildFlag_VetStyle: build_context.vet_flags |= VetFlag_Style; break; + case BuildFlag_VetSemicolon: build_context.vet_flags |= VetFlag_Semicolon; break; + case BuildFlag_VetCast: build_context.vet_flags |= VetFlag_Cast; break; + case BuildFlag_VetTabs: build_context.vet_flags |= VetFlag_Tabs; break; + case BuildFlag_VetUnusedProcedures: build_context.vet_flags |= VetFlag_UnusedProcedures; break; case BuildFlag_VetPackages: { @@ -1788,6 +1782,11 @@ gb_internal bool parse_build_flags(Array args) { } } + if (set_flags[BuildFlag_VetUnusedProcedures] && !set_flags[BuildFlag_VetPackages]) { + gb_printf_err("-vet-unused-procedures must be used with -vet-packages\n"); + bad_flags = true; + } + if ((!(build_context.export_timings_format == TimingsExportUnspecified)) && (build_context.export_timings_file.len == 0)) { gb_printf_err("`-export-timings:` requires `-export-timings-file:` to be specified as well\n"); bad_flags = true; -- cgit v1.2.3 From e72aad983bb683858a1aee935b2956ced40f69f8 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Sun, 30 Nov 2025 22:37:35 +0100 Subject: Remove `all-bits` from option help text, isn't supported. --- src/main.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index beec9ef31..544cd54bb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2680,7 +2680,6 @@ gb_internal int print_show_help(String const arg0, String command, String option print_usage_line(3, "-integer-division-by-zero:trap Trap on division/modulo/remainder by zero"); print_usage_line(3, "-integer-division-by-zero:zero x/0 == 0 and x%%0 == x and x%%%%0 == x"); print_usage_line(3, "-integer-division-by-zero:self x/0 == x and x%%0 == 0 and x%%%%0 == 0"); - print_usage_line(3, "-integer-division-by-zero:all-bits x/0 == ~T(0) and x%%0 == x and x%%%%0 == x"); } } -- cgit v1.2.3 From 0f0c40b96dc5d5b6e585c935186b342180747d06 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 1 Dec 2025 13:11:32 +0000 Subject: Fix `-integer-division-by-zero` modes and document `all-bits` --- src/llvm_backend_expr.cpp | 19 ++++++++++--------- src/llvm_backend_proc.cpp | 2 +- src/main.cpp | 6 ++++-- 3 files changed, 15 insertions(+), 12 deletions(-) (limited to 'src/main.cpp') diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp index d5658b9d5..9b8df5a37 100644 --- a/src/llvm_backend_expr.cpp +++ b/src/llvm_backend_expr.cpp @@ -1161,8 +1161,7 @@ gb_internal LLVMValueRef lb_integer_division(lbProcedure *p, LLVMValueRef lhs, L case IntegerDivisionByZero_Zero: return zero; case IntegerDivisionByZero_AllBits: - // return all_bits; - break; + return all_bits; } } else { if (!is_signed && lb_sizeof(type) <= 8) { @@ -1198,7 +1197,6 @@ gb_internal LLVMValueRef lb_integer_division(lbProcedure *p, LLVMValueRef lhs, L lb_start_block(p, edge_case_block); - switch (behaviour) { case IntegerDivisionByZero_Trap: lb_call_intrinsic(p, "llvm.trap", nullptr, 0, nullptr, 0); @@ -1214,17 +1212,17 @@ gb_internal LLVMValueRef lb_integer_division(lbProcedure *p, LLVMValueRef lhs, L incoming_values[1] = all_bits; break; } + LLVMValueRef res = nullptr; lb_emit_jump(p, done_block); lb_start_block(p, done_block); - LLVMValueRef res = incoming_values[0]; switch (behaviour) { case IntegerDivisionByZero_Trap: - case IntegerDivisionByZero_Self: res = incoming_values[0]; break; + case IntegerDivisionByZero_Self: case IntegerDivisionByZero_Zero: case IntegerDivisionByZero_AllBits: res = LLVMBuildPhi(p->builder, type, ""); @@ -1233,6 +1231,9 @@ gb_internal LLVMValueRef lb_integer_division(lbProcedure *p, LLVMValueRef lhs, L incoming_blocks[0] = p->curr_block->preds[0]->block; incoming_blocks[1] = p->curr_block->preds[1]->block; + GB_ASSERT(incoming_blocks[0] == safe_block->block); + GB_ASSERT(incoming_blocks[1] == edge_case_block->block); + LLVMAddIncoming(res, incoming_values, incoming_blocks, 2); break; } @@ -1240,7 +1241,7 @@ gb_internal LLVMValueRef lb_integer_division(lbProcedure *p, LLVMValueRef lhs, L return res; } -gb_internal LLVMValueRef lb_integer_division_intrinsics(lbProcedure *p, LLVMValueRef lhs, LLVMValueRef rhs, LLVMValueRef scale, Type *platform_type, char const *name) { +gb_internal LLVMValueRef lb_integer_division_fixed_point_intrinsics(lbProcedure *p, LLVMValueRef lhs, LLVMValueRef rhs, LLVMValueRef scale, Type *platform_type, char const *name) { LLVMTypeRef type = LLVMTypeOf(rhs); GB_ASSERT(LLVMTypeOf(lhs) == type); @@ -1310,13 +1311,13 @@ gb_internal LLVMValueRef lb_integer_division_intrinsics(lbProcedure *p, LLVMValu lb_emit_jump(p, done_block); lb_start_block(p, done_block); - LLVMValueRef res = incoming_values[0]; + LLVMValueRef res = nullptr; switch (behaviour) { case IntegerDivisionByZero_Trap: - case IntegerDivisionByZero_Self: res = incoming_values[0]; break; + case IntegerDivisionByZero_Self: case IntegerDivisionByZero_Zero: case IntegerDivisionByZero_AllBits: res = LLVMBuildPhi(p->builder, type, ""); @@ -1423,9 +1424,9 @@ gb_internal LLVMValueRef lb_integer_modulo(lbProcedure *p, LLVMValueRef lhs, LLV switch (behaviour) { case IntegerDivisionByZero_Trap: - case IntegerDivisionByZero_Self: res = incoming_values[0]; break; + case IntegerDivisionByZero_Self: case IntegerDivisionByZero_Zero: case IntegerDivisionByZero_AllBits: res = LLVMBuildPhi(p->builder, type, ""); diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 10e10564e..27167aefd 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -3305,7 +3305,7 @@ gb_internal lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValu if (id == BuiltinProc_fixed_point_div || id == BuiltinProc_fixed_point_div_sat) { - res.value = lb_integer_division_intrinsics(p, x.value, y.value, scale.value, platform_type, name); + res.value = lb_integer_division_fixed_point_intrinsics(p, x.value, y.value, scale.value, platform_type, name); } else { LLVMTypeRef types[1] = {lb_type(p->module, platform_type)}; diff --git a/src/main.cpp b/src/main.cpp index 544cd54bb..ca05bb177 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1570,8 +1570,10 @@ gb_internal bool parse_build_flags(Array args) { build_context.integer_division_by_zero_behaviour = IntegerDivisionByZero_Zero; } else if (str_eq_ignore_case(value.value_string, "self")) { build_context.integer_division_by_zero_behaviour = IntegerDivisionByZero_Self; - }else { - gb_printf_err("-integer-division-by-zero options are 'trap', 'zero', and 'self'.\n"); + } else if (str_eq_ignore_case(value.value_string, "all-bits")) { + build_context.integer_division_by_zero_behaviour = IntegerDivisionByZero_AllBits; + } else { + gb_printf_err("-integer-division-by-zero options are 'trap', 'zero', 'self', and 'all-bits'.\n"); bad_flags = true; } break; -- cgit v1.2.3 From ac61f083fae80197db37d286ac1259d32b587ba9 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Mon, 1 Dec 2025 14:15:56 +0100 Subject: Restore help line for all-bits --- src/main.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index ca05bb177..9a5df8aea 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2682,6 +2682,7 @@ gb_internal int print_show_help(String const arg0, String command, String option print_usage_line(3, "-integer-division-by-zero:trap Trap on division/modulo/remainder by zero"); print_usage_line(3, "-integer-division-by-zero:zero x/0 == 0 and x%%0 == x and x%%%%0 == x"); print_usage_line(3, "-integer-division-by-zero:self x/0 == x and x%%0 == 0 and x%%%%0 == 0"); + print_usage_line(3, "-integer-division-by-zero:all-bits x/0 == ~T(0) and x%%0 == x and x%%%%0 == x"); } } -- cgit v1.2.3