diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2025-09-19 16:15:04 +0100 |
|---|---|---|
| committer | gingerBill <gingerBill@users.noreply.github.com> | 2025-09-19 16:15:04 +0100 |
| commit | 9b8771b475a2e0a75205408b2615f69d65a329bd (patch) | |
| tree | 0040a390f8c5cfc2f2da1610886eb2b5ff26d7f0 | |
| parent | 655176e5e7ab10b66f7fd936a23f7472a818e5f2 (diff) | |
Handle missing procedures better
| -rw-r--r-- | src/llvm_backend.cpp | 16 | ||||
| -rw-r--r-- | src/llvm_backend.hpp | 6 | ||||
| -rw-r--r-- | src/llvm_backend_general.cpp | 9 | ||||
| -rw-r--r-- | src/llvm_backend_proc.cpp | 1 |
4 files changed, 21 insertions, 11 deletions
diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 02df76243..1742271b6 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -12,7 +12,7 @@ #endif #ifndef LLVM_WEAK_MONOMORPHIZATION -#define LLVM_WEAK_MONOMORPHIZATION build_context.internal_weak_monomorphization +#define LLVM_WEAK_MONOMORPHIZATION 1 #endif @@ -2478,8 +2478,14 @@ gb_internal void lb_generate_procedures(lbGenerator *gen, bool do_threading) { gb_internal WORKER_TASK_PROC(lb_generate_missing_procedures_to_check_worker_proc) { lbModule *m = cast(lbModule *)data; for (lbProcedure *p = nullptr; mpsc_dequeue(&m->missing_procedures_to_check, &p); /**/) { - debugf("Generate missing procedure: %.*s module %p\n", LIT(p->name), m); - lb_generate_procedure(m, p); + if (!p->is_done.load(std::memory_order_relaxed)) { + debugf("Generate missing procedure: %.*s module %p\n", LIT(p->name), m); + lb_generate_procedure(m, p); + } + + for (lbProcedure *nested = nullptr; mpsc_dequeue(&m->procedures_to_generate, &nested); /**/) { + mpsc_enqueue(&m->missing_procedures_to_check, nested); + } } return 0; } @@ -2860,7 +2866,7 @@ gb_internal lbProcedure *lb_create_main_procedure(lbModule *m, lbProcedure *star } gb_internal void lb_generate_procedure(lbModule *m, lbProcedure *p) { - if (p->is_done) { + if (p->is_done.load(std::memory_order_relaxed)) { return; } @@ -2869,7 +2875,7 @@ gb_internal void lb_generate_procedure(lbModule *m, lbProcedure *p) { lb_begin_procedure_body(p); lb_build_stmt(p, p->body); lb_end_procedure_body(p); - p->is_done = true; + p->is_done.store(true, std::memory_order_relaxed); m->curr_procedure = nullptr; } else if (p->generate_body != nullptr) { p->generate_body(m, p); diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 559ec8300..cee46701f 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -366,9 +366,9 @@ struct lbProcedure { lbFunctionType *abi_function_type; - LLVMValueRef value; - LLVMBuilderRef builder; - bool is_done; + LLVMValueRef value; + LLVMBuilderRef builder; + std::atomic<bool> is_done; lbAddr return_ptr; Array<lbDefer> defer_stmts; diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index f42087f6b..02f67b1b8 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -3082,9 +3082,12 @@ gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e) if (found == nullptr) { // THIS IS THE RACE CONDITION lbProcedure *missing_proc_in_other_module = lb_create_procedure(other_module, e, false); - mpsc_enqueue(&other_module->missing_procedures_to_check, missing_proc_in_other_module); + if (!missing_proc_in_other_module->is_done.load(std::memory_order_relaxed)) { + mpsc_enqueue(&other_module->missing_procedures_to_check, missing_proc_in_other_module); + } } } else { + GB_PANIC("missing procedure: %.*s", LIT(missing_proc->name)); mpsc_enqueue(&m->missing_procedures_to_check, missing_proc); } @@ -3157,7 +3160,9 @@ gb_internal lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &pr rw_mutex_shared_unlock(&target_module->values_mutex); if (found == nullptr) { lbProcedure *missing_proc_in_target_module = lb_create_procedure(target_module, e, false); - mpsc_enqueue(&target_module->missing_procedures_to_check, missing_proc_in_target_module); + if (!missing_proc_in_target_module->is_done.load(std::memory_order_relaxed)) { + mpsc_enqueue(&target_module->missing_procedures_to_check, missing_proc_in_target_module); + } } lbProcedure *p = lb_create_procedure(m, e, true); diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 3b8a829b7..ee17ef771 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -99,7 +99,6 @@ gb_internal lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool i } } - lbProcedure *p = gb_alloc_item(permanent_allocator(), lbProcedure); p->module = m; |