From 6c7e5748a8ee80b01b99fe41192d4aac32651849 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 18 Jul 2022 12:49:29 +0100 Subject: Integrate numerous debug fixes from #1877 --- src/llvm_backend_stmt.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'src/llvm_backend_stmt.cpp') diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index 2afb5300b..d007ac747 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -1721,6 +1721,9 @@ void lb_build_for_stmt(lbProcedure *p, Ast *node) { ast_node(fs, ForStmt, node); lb_open_scope(p, fs->scope); // Open Scope here + if (p->debug_info != nullptr) { + LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_ast(p, node)); + } if (fs->init != nullptr) { #if 1 @@ -1971,14 +1974,9 @@ void lb_build_stmt(lbProcedure *p, Ast *node) { } } - LLVMMetadataRef prev_debug_location = nullptr; if (p->debug_info != nullptr) { - prev_debug_location = LLVMGetCurrentDebugLocation2(p->builder); LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_ast(p, node)); } - defer (if (prev_debug_location != nullptr) { - LLVMSetCurrentDebugLocation2(p->builder, prev_debug_location); - }); u16 prev_state_flags = p->state_flags; defer (p->state_flags = prev_state_flags); -- cgit v1.2.3 From fb167d1d0aa5b428dc1f477e8879d68664d155d9 Mon Sep 17 00:00:00 2001 From: Fabian Sperber Date: Mon, 11 Jul 2022 21:03:46 +0200 Subject: Fix loop var (init) becoming hidden while stepping on the for-line - set debug location of jumps between block (similar to clang to the location of 'for') - extend scope range to include all parts of the for-stmt --- src/llvm_backend_debug.cpp | 4 ++++ src/llvm_backend_stmt.cpp | 10 ++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'src/llvm_backend_stmt.cpp') diff --git a/src/llvm_backend_debug.cpp b/src/llvm_backend_debug.cpp index 207164c63..29e074473 100644 --- a/src/llvm_backend_debug.cpp +++ b/src/llvm_backend_debug.cpp @@ -43,6 +43,10 @@ LLVMMetadataRef lb_debug_location_from_ast(lbProcedure *p, Ast *node) { GB_ASSERT(node != nullptr); return lb_debug_location_from_token_pos(p, ast_token(node).pos); } +LLVMMetadataRef lb_debug_end_location_from_ast(lbProcedure *p, Ast *node) { + GB_ASSERT(node != nullptr); + return lb_debug_location_from_token_pos(p, ast_end_token(node).pos); +} LLVMMetadataRef lb_debug_type_internal_proc(lbModule *m, Type *type) { i64 size = type_size_of(type); // Check size diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index d007ac747..d2b9ac555 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -1744,11 +1744,14 @@ void lb_build_for_stmt(lbProcedure *p, Ast *node) { post = lb_create_block(p, "for.post"); } - lb_emit_jump(p, loop); lb_start_block(p, loop); if (loop != body) { + // right now the condition (all expressions) will not set it's debug location, so we will do it here + if (p->debug_info != nullptr) { + LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_ast(p, fs->cond)); + } lb_build_cond(p, fs->cond, body, done); lb_start_block(p, body); } @@ -1756,10 +1759,12 @@ void lb_build_for_stmt(lbProcedure *p, Ast *node) { lb_push_target_list(p, fs->label, done, post, nullptr); lb_build_stmt(p, fs->body); - lb_close_scope(p, lbDeferExit_Default, nullptr); lb_pop_target_list(p); + if (p->debug_info != nullptr) { + LLVMSetCurrentDebugLocation2(p->builder, lb_debug_end_location_from_ast(p, fs->body)); + } lb_emit_jump(p, post); if (fs->post != nullptr) { @@ -1769,6 +1774,7 @@ void lb_build_for_stmt(lbProcedure *p, Ast *node) { } lb_start_block(p, done); + lb_close_scope(p, lbDeferExit_Default, nullptr); } void lb_build_assign_stmt_array(lbProcedure *p, TokenKind op, lbAddr const &lhs, lbValue const &value) { -- cgit v1.2.3 From 9c059f1a128638f67a96715af1dbd8a668120a25 Mon Sep 17 00:00:00 2001 From: Fabian Sperber Date: Tue, 19 Jul 2022 00:14:45 +0200 Subject: Fix debug info for type switch variable to be only visible in correct switch case - use correct scopes for type switch --- src/llvm_backend_stmt.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/llvm_backend_stmt.cpp') diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index d2b9ac555..a8f543c1f 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -1210,8 +1210,8 @@ void lb_build_switch_stmt(lbProcedure *p, AstSwitchStmt *ss, Scope *scope) { } lb_emit_jump(p, done); - lb_close_scope(p, lbDeferExit_Default, done); lb_start_block(p, done); + lb_close_scope(p, lbDeferExit_Default, done); } void lb_store_type_case_implicit(lbProcedure *p, Ast *clause, lbValue value) { @@ -1253,7 +1253,6 @@ void lb_type_case_body(lbProcedure *p, Ast *label, Ast *clause, lbBlock *body, l ast_node(cc, CaseClause, clause); lb_push_target_list(p, label, done, nullptr, nullptr); - lb_open_scope(p, body->scope); lb_build_stmt_list(p, cc->stmts); lb_close_scope(p, lbDeferExit_Default, body); lb_pop_target_list(p); @@ -1263,6 +1262,7 @@ void lb_type_case_body(lbProcedure *p, Ast *label, Ast *clause, lbBlock *body, l void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss) { lbModule *m = p->module; + lb_open_scope(p, ss->scope); ast_node(as, AssignStmt, ss->tag); GB_ASSERT(as->lhs.count == 1); @@ -1321,6 +1321,7 @@ void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss) { for_array(i, body->stmts) { Ast *clause = body->stmts[i]; ast_node(cc, CaseClause, clause); + lb_open_scope(p, cc->scope); if (cc->list.count == 0) { lb_start_block(p, default_block); lb_store_type_case_implicit(p, clause, parent_value); @@ -1329,6 +1330,9 @@ void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss) { } lbBlock *body = lb_create_block(p, "typeswitch.body"); + if (p->debug_info != nullptr) { + LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_ast(p, clause)); + } Type *case_type = nullptr; for_array(type_index, cc->list) { case_type = type_of_expr(cc->list[type_index]); @@ -1375,6 +1379,7 @@ void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss) { lb_emit_jump(p, done); lb_start_block(p, done); + lb_close_scope(p, lbDeferExit_Default, done); } -- cgit v1.2.3 From e6ab4f48567175cc192a658fb6d7b067e38912d8 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 24 Jul 2022 20:22:50 +0100 Subject: Force memset instead of store zeroinitializer when the value is large --- src/llvm_backend.hpp | 4 ++++ src/llvm_backend_general.cpp | 12 +++++++++--- src/llvm_backend_stmt.cpp | 3 ++- src/llvm_backend_utility.cpp | 8 ++++---- 4 files changed, 19 insertions(+), 8 deletions(-) (limited to 'src/llvm_backend_stmt.cpp') diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 745dbbbc7..a09286d0b 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -482,7 +482,11 @@ LLVMValueRef llvm_basic_shuffle(lbProcedure *p, LLVMValueRef vector, LLVMValueRe void lb_mem_copy_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue len, bool is_volatile=false); void lb_mem_copy_non_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue len, bool is_volatile=false); +LLVMValueRef lb_mem_zero_ptr_internal(lbProcedure *p, LLVMValueRef ptr, LLVMValueRef len, unsigned alignment, bool is_volatile); +i64 lb_max_zero_init_size(void) { + return cast(i64)(4*build_context.word_size); +} #define LB_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime" #define LB_STARTUP_TYPE_INFO_PROC_NAME "__$startup_type_info" diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index a4c2ce370..52787d427 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -855,7 +855,11 @@ void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value) { if (LLVMIsNull(value.value)) { LLVMTypeRef src_t = LLVMGetElementType(LLVMTypeOf(ptr.value)); - LLVMBuildStore(p->builder, LLVMConstNull(src_t), ptr.value); + if (lb_sizeof(src_t) <= lb_max_zero_init_size()) { + LLVMBuildStore(p->builder, LLVMConstNull(src_t), ptr.value); + } else { + lb_mem_zero_ptr(p, ptr.value, a, LLVMGetAlignment(ptr.value)); + } return; } if (is_type_boolean(a)) { @@ -2737,13 +2741,15 @@ lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e, bool zero_init, i32 p if (!zero_init && !force_no_init) { // If there is any padding of any kind, just zero init regardless of zero_init parameter LLVMTypeKind kind = LLVMGetTypeKind(llvm_type); + if (kind == LLVMArrayTypeKind) { + kind = LLVMGetTypeKind(lb_type(p->module, core_array_type(type))); + } + if (kind == LLVMStructTypeKind) { i64 sz = type_size_of(type); if (type_size_of_struct_pretend_is_packed(type) != sz) { zero_init = true; } - } else if (kind == LLVMArrayTypeKind) { - zero_init = true; } } diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index a8f543c1f..f131bb3db 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -2082,7 +2082,8 @@ void lb_build_stmt(lbProcedure *p, Ast *node) { lbAddr lval = {}; if (!is_blank_ident(name)) { Entity *e = entity_of_node(name); - bool zero_init = true; // Always do it + // bool zero_init = true; // Always do it + bool zero_init = vd->values.count == 0; lval = lb_add_local(p, e->type, e, zero_init); } array_add(&lvals, lval); diff --git a/src/llvm_backend_utility.cpp b/src/llvm_backend_utility.cpp index 2fa43bb0a..88ec2f22c 100644 --- a/src/llvm_backend_utility.cpp +++ b/src/llvm_backend_utility.cpp @@ -50,14 +50,14 @@ lbValue lb_correct_endianness(lbProcedure *p, lbValue value) { return value; } -void lb_mem_zero_ptr_internal(lbProcedure *p, LLVMValueRef ptr, LLVMValueRef len, unsigned alignment, bool is_volatile) { +LLVMValueRef lb_mem_zero_ptr_internal(lbProcedure *p, LLVMValueRef ptr, LLVMValueRef len, unsigned alignment, bool is_volatile) { bool is_inlinable = false; i64 const_len = 0; if (LLVMIsConstant(len)) { const_len = cast(i64)LLVMConstIntGetSExtValue(len); // TODO(bill): Determine when it is better to do the `*.inline` versions - if (const_len <= 4*build_context.word_size) { + if (const_len <= lb_max_zero_init_size()) { is_inlinable = true; } } @@ -83,7 +83,7 @@ void lb_mem_zero_ptr_internal(lbProcedure *p, LLVMValueRef ptr, LLVMValueRef len args[2] = LLVMBuildIntCast2(p->builder, len, types[1], /*signed*/false, ""); args[3] = LLVMConstInt(LLVMInt1TypeInContext(p->module->ctx), is_volatile, false); - LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + return LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); } else { LLVMValueRef ip = lb_lookup_runtime_procedure(p->module, str_lit("memset")).value; @@ -92,7 +92,7 @@ void lb_mem_zero_ptr_internal(lbProcedure *p, LLVMValueRef ptr, LLVMValueRef len args[1] = LLVMConstInt(LLVMInt32TypeInContext(p->module->ctx), 0, false); args[2] = LLVMBuildIntCast2(p->builder, len, types[1], /*signed*/false, ""); - LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + return LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); } } -- cgit v1.2.3