diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index e2bb2f812..1110f9e55 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -3041,13 +3041,23 @@ gb_internal void lb_generate_queued_procedures(lbGenerator *gen, bool do_threadi } } -gb_internal void lb_debug_info_complete_types_and_finalize(lbGenerator *gen) { - for (auto const &entry : gen->modules) { - lbModule *m = entry.value; - if (m->debug_builder != nullptr) { - LLVMDIBuilderFinalize(m->debug_builder); +gb_internal WORKER_TASK_PROC(lb_debug_info_finalize_worker_proc) { + lbModule *m = cast(lbModule *)data; + if (m->debug_builder != nullptr) { + LLVMDIBuilderFinalize(m->debug_builder); + } + return 0; +} + +gb_internal void lb_debug_info_complete_types_and_finalize(lbGenerator *gen, bool do_threading) { + for (lbModule *m : lb_modules_by_cost(gen)) { + if (do_threading) { + thread_pool_add_task(lb_debug_info_finalize_worker_proc, m); + } else { + lb_debug_info_finalize_worker_proc(m); } } + thread_pool_wait(); } gb_internal void lb_llvm_function_passes(lbGenerator *gen, bool do_threading) { @@ -3903,7 +3913,7 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { if (build_context.ODIN_DEBUG) { TIME_SECTION("LLVM Debug Info Complete Types and Finalize"); - lb_debug_info_complete_types_and_finalize(gen); + lb_debug_info_complete_types_and_finalize(gen, do_threading); // Custom `.raddbg` section for its debugger if (build_context.metrics.os == TargetOs_windows) { diff --git a/src/llvm_backend_debug.cpp b/src/llvm_backend_debug.cpp index ebd34955b..f376d6152 100644 --- a/src/llvm_backend_debug.cpp +++ b/src/llvm_backend_debug.cpp @@ -206,6 +206,28 @@ gb_internal LLVMMetadataRef lb_debug_basic_struct(lbModule *m, String const &nam return LLVMDIBuilderCreateStructType(m->debug_builder, scope, cast(char const *)name.text, name.len, file, 1, size_in_bits, align_in_bits, LLVMDIFlagZero, nullptr, elements, element_count, 0, nullptr, "", 0); } +// NOTE: only a named type can contain itself, so only it needs a placeholder for its members to refer to +gb_internal LLVMMetadataRef lb_debug_placeholder(lbModule *m, Type *type, unsigned tag, String const &name, LLVMMetadataRef scope, LLVMMetadataRef file, unsigned line, u64 size_in_bits, u32 align_in_bits) { + if (type->kind != Type_Named) { + return nullptr; + } + LLVMMetadataRef temp_forward_decl = LLVMDIBuilderCreateReplaceableCompositeType( + m->debug_builder, tag, + cast(char const *)name.text, cast(size_t)name.len, + scope, file, line, 0, size_in_bits, align_in_bits, LLVMDIFlagZero, "", 0 + ); + lb_set_llvm_metadata(m, type, temp_forward_decl); + return temp_forward_decl; +} + +gb_internal LLVMMetadataRef lb_debug_replace_placeholder(lbModule *m, Type *type, LLVMMetadataRef temp_forward_decl, LLVMMetadataRef final_decl) { + if (temp_forward_decl != nullptr) { + LLVMMetadataReplaceAllUsesWith(temp_forward_decl, final_decl); + } + lb_set_llvm_metadata(m, type, final_decl); + return final_decl; +} + gb_internal LLVMMetadataRef lb_debug_struct(lbModule *m, Type *type, Type *bt, String name, LLVMMetadataRef scope, LLVMMetadataRef file, unsigned line) { GB_ASSERT(bt->kind == Type_Struct); @@ -219,13 +241,7 @@ gb_internal LLVMMetadataRef lb_debug_struct(lbModule *m, Type *type, Type *bt, S u64 size_in_bits = 8*type_size_of(bt); u32 align_in_bits = 8*cast(u32)type_align_of(bt); - LLVMMetadataRef temp_forward_decl = LLVMDIBuilderCreateReplaceableCompositeType( - m->debug_builder, tag, - cast(char const *)name.text, cast(size_t)name.len, - scope, file, line, 0, size_in_bits, align_in_bits, LLVMDIFlagZero, "", 0 - ); - - lb_set_llvm_metadata(m, type, temp_forward_decl); + LLVMMetadataRef temp_forward_decl = lb_debug_placeholder(m, type, tag, name, scope, file, line, size_in_bits, align_in_bits); type_set_offsets(bt); @@ -282,9 +298,7 @@ gb_internal LLVMMetadataRef lb_debug_struct(lbModule *m, Type *type, Type *bt, S ); } - LLVMMetadataReplaceAllUsesWith(temp_forward_decl, final_decl); - lb_set_llvm_metadata(m, type, final_decl); - return final_decl; + return lb_debug_replace_placeholder(m, type, temp_forward_decl, final_decl); } gb_internal LLVMMetadataRef lb_debug_slice(lbModule *m, Type *type, String name, LLVMMetadataRef scope, LLVMMetadataRef file, unsigned line) { @@ -296,13 +310,7 @@ gb_internal LLVMMetadataRef lb_debug_slice(lbModule *m, Type *type, String name, u64 size_in_bits = 8*type_size_of(bt); u32 align_in_bits = 8*cast(u32)type_align_of(bt); - LLVMMetadataRef temp_forward_decl = LLVMDIBuilderCreateReplaceableCompositeType( - m->debug_builder, DW_TAG_structure_type, - cast(char const *)name.text, cast(size_t)name.len, - scope, file, line, 0, size_in_bits, align_in_bits, LLVMDIFlagZero, "", 0 - ); - - lb_set_llvm_metadata(m, type, temp_forward_decl); + LLVMMetadataRef temp_forward_decl = lb_debug_placeholder(m, type, DW_TAG_structure_type, name, scope, file, line, size_in_bits, align_in_bits); unsigned element_count = 2; LLVMMetadataRef elements[2]; @@ -342,9 +350,7 @@ gb_internal LLVMMetadataRef lb_debug_slice(lbModule *m, Type *type, String name, "", 0 ); - LLVMMetadataReplaceAllUsesWith(temp_forward_decl, final_decl); - lb_set_llvm_metadata(m, type, final_decl); - return final_decl; + return lb_debug_replace_placeholder(m, type, temp_forward_decl, final_decl); } gb_internal LLVMMetadataRef lb_debug_dynamic_array(lbModule *m, Type *type, String name, LLVMMetadataRef scope, LLVMMetadataRef file, unsigned line) { @@ -357,13 +363,7 @@ gb_internal LLVMMetadataRef lb_debug_dynamic_array(lbModule *m, Type *type, Stri u64 size_in_bits = 8*type_size_of(bt); u32 align_in_bits = 8*cast(u32)type_align_of(bt); - LLVMMetadataRef temp_forward_decl = LLVMDIBuilderCreateReplaceableCompositeType( - m->debug_builder, DW_TAG_structure_type, - cast(char const *)name.text, cast(size_t)name.len, - scope, file, line, 0, size_in_bits, align_in_bits, LLVMDIFlagZero, "", 0 - ); - - lb_set_llvm_metadata(m, type, temp_forward_decl); + LLVMMetadataRef temp_forward_decl = lb_debug_placeholder(m, type, DW_TAG_structure_type, name, scope, file, line, size_in_bits, align_in_bits); unsigned element_count = 4; LLVMMetadataRef elements[4]; @@ -421,9 +421,7 @@ gb_internal LLVMMetadataRef lb_debug_dynamic_array(lbModule *m, Type *type, Stri "", 0 ); - LLVMMetadataReplaceAllUsesWith(temp_forward_decl, final_decl); - lb_set_llvm_metadata(m, type, final_decl); - return final_decl; + return lb_debug_replace_placeholder(m, type, temp_forward_decl, final_decl); } gb_internal LLVMMetadataRef lb_debug_fixed_capacity_dynamic_array(lbModule *m, Type *type, String name, LLVMMetadataRef scope, LLVMMetadataRef file, unsigned line) { @@ -435,13 +433,7 @@ gb_internal LLVMMetadataRef lb_debug_fixed_capacity_dynamic_array(lbModule *m, T u64 size_in_bits = 8*type_size_of(bt); u32 align_in_bits = 8*cast(u32)type_align_of(bt); - LLVMMetadataRef temp_forward_decl = LLVMDIBuilderCreateReplaceableCompositeType( - m->debug_builder, DW_TAG_structure_type, - cast(char const *)name.text, cast(size_t)name.len, - scope, file, line, 0, size_in_bits, align_in_bits, LLVMDIFlagZero, "", 0 - ); - - lb_set_llvm_metadata(m, type, temp_forward_decl); + LLVMMetadataRef temp_forward_decl = lb_debug_placeholder(m, type, DW_TAG_structure_type, name, scope, file, line, size_in_bits, align_in_bits); unsigned element_count = 2; LLVMMetadataRef elements[2]; @@ -483,9 +475,7 @@ gb_internal LLVMMetadataRef lb_debug_fixed_capacity_dynamic_array(lbModule *m, T "", 0 ); - LLVMMetadataReplaceAllUsesWith(temp_forward_decl, final_decl); - lb_set_llvm_metadata(m, type, final_decl); - return final_decl; + return lb_debug_replace_placeholder(m, type, temp_forward_decl, final_decl); } @@ -498,13 +488,7 @@ gb_internal LLVMMetadataRef lb_debug_union(lbModule *m, Type *type, String name, u64 size_in_bits = 8*type_size_of(bt); u32 align_in_bits = 8*cast(u32)type_align_of(bt); - LLVMMetadataRef temp_forward_decl = LLVMDIBuilderCreateReplaceableCompositeType( - m->debug_builder, DW_TAG_union_type, - cast(char const *)name.text, cast(size_t)name.len, - scope, file, line, 0, size_in_bits, align_in_bits, LLVMDIFlagZero, "", 0 - ); - - lb_set_llvm_metadata(m, type, temp_forward_decl); + LLVMMetadataRef temp_forward_decl = lb_debug_placeholder(m, type, DW_TAG_union_type, name, scope, file, line, size_in_bits, align_in_bits); isize index_offset = 1; isize variant_offset = 1; @@ -568,9 +552,7 @@ gb_internal LLVMMetadataRef lb_debug_union(lbModule *m, Type *type, String name, "", 0 ); - LLVMMetadataReplaceAllUsesWith(temp_forward_decl, final_decl); - lb_set_llvm_metadata(m, type, final_decl); - return final_decl; + return lb_debug_replace_placeholder(m, type, temp_forward_decl, final_decl); } gb_internal LLVMMetadataRef lb_debug_bitset(lbModule *m, Type *type, String name, LLVMMetadataRef scope, LLVMMetadataRef file, unsigned line) { diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 827a4ba21..15a2996bf 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -364,7 +364,7 @@ gb_internal lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool i } } - if (m->debug_builder) { // Debug Information + if (m->debug_builder && p->body != nullptr) { // Debug Information Type *bt = base_type(p->type); unsigned line = cast(unsigned)entity->token.pos.line; @@ -390,33 +390,27 @@ gb_internal lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool i // LLVMBool is_local_to_unit = !entity->Procedure.is_export; LLVMBool is_local_to_unit = false; - LLVMBool is_definition = p->body != nullptr; + LLVMBool is_definition = true; unsigned scope_line = line; u32 flags = LLVMDIFlagStaticMember; LLVMBool is_optimized = false; if (bt->Proc.diverging) { flags |= LLVMDIFlagNoReturn; } - if (p->body == nullptr) { - flags |= LLVMDIFlagPrototyped; - is_optimized = false; - } - if (p->body != nullptr) { - // String debug_name = entity->token.string.text; - String debug_name = p->name; + // String debug_name = entity->token.string.text; + String debug_name = p->name; - p->debug_info = LLVMDIBuilderCreateFunction(m->debug_builder, scope, - cast(char const *)debug_name.text, debug_name.len, - cast(char const *)p->name.text, p->name.len, - file, line, type, - is_local_to_unit, is_definition, - scope_line, cast(LLVMDIFlags)flags, is_optimized - ); - GB_ASSERT(p->debug_info != nullptr); - LLVMSetSubprogram(p->value, p->debug_info); - lb_set_llvm_metadata(m, p, p->debug_info); - } + p->debug_info = LLVMDIBuilderCreateFunction(m->debug_builder, scope, + cast(char const *)debug_name.text, debug_name.len, + cast(char const *)p->name.text, p->name.len, + file, line, type, + is_local_to_unit, is_definition, + scope_line, cast(LLVMDIFlags)flags, is_optimized + ); + GB_ASSERT(p->debug_info != nullptr); + LLVMSetSubprogram(p->value, p->debug_info); + lb_set_llvm_metadata(m, p, p->debug_info); } if (p->body && entity->pkg && ((entity->pkg->kind == Package_Normal) || (entity->pkg->kind == Package_Init))) {