Browse Source

fix instrumentation in debug mode

Laytan 1 year ago
parent
commit
0e6dd56ac1
2 changed files with 17 additions and 4 deletions
  1. 2 0
      src/llvm_backend.hpp
  2. 15 4
      src/llvm_backend_opt.cpp

+ 2 - 0
src/llvm_backend.hpp

@@ -567,6 +567,8 @@ gb_internal LLVMTypeRef lb_type_internal_for_procedures_raw(lbModule *m, Type *t
 
 gb_internal lbValue lb_emit_source_code_location_as_global_ptr(lbProcedure *p, String const &procedure, TokenPos const &pos);
 
+gb_internal LLVMMetadataRef lb_debug_location_from_token_pos(lbProcedure *p, TokenPos pos);
+
 gb_internal LLVMTypeRef llvm_array_type(LLVMTypeRef ElementType, uint64_t ElementCount) {
 #if LB_USE_NEW_PASS_SYSTEM
 	return LLVMArrayType2(ElementType, ElementCount);

+ 15 - 4
src/llvm_backend_opt.cpp

@@ -380,9 +380,19 @@ gb_internal void lb_run_remove_dead_instruction_pass(lbProcedure *p) {
 	}
 }
 
-gb_internal LLVMValueRef lb_run_instrumentation_pass_insert_call(lbProcedure *p, Entity *entity, LLVMBuilderRef dummy_builder) {
+gb_internal LLVMValueRef lb_run_instrumentation_pass_insert_call(lbProcedure *p, Entity *entity, LLVMBuilderRef dummy_builder, bool is_enter) {
 	lbModule *m = p->module;
 
+	if (p->debug_info != nullptr) {
+		TokenPos pos = {};
+		if (is_enter) {
+			pos = ast_token(p->body).pos;
+		} else {
+			pos = ast_end_token(p->body).pos;
+		}
+		LLVMSetCurrentDebugLocation2(dummy_builder, lb_debug_location_from_token_pos(p, pos));
+	}
+
 	lbValue cc = lb_find_procedure_value_from_entity(m, entity);
 
 	LLVMValueRef args[3] = {};
@@ -430,7 +440,7 @@ gb_internal void lb_run_instrumentation_pass(lbProcedure *p) {
 
 	LLVMBasicBlockRef entry_bb = p->entry_block->block;
 	LLVMPositionBuilder(dummy_builder, entry_bb, LLVMGetFirstInstruction(entry_bb));
-	lb_run_instrumentation_pass_insert_call(p, enter, dummy_builder);
+	lb_run_instrumentation_pass_insert_call(p, enter, dummy_builder, true);
 	LLVMRemoveStringAttributeAtIndex(p->value, LLVMAttributeIndex_FunctionIndex, LLVM_V_NAME("instrument-function-entry"));
 
 	unsigned bb_count = LLVMCountBasicBlocks(p->value);
@@ -451,7 +461,7 @@ gb_internal void lb_run_instrumentation_pass(lbProcedure *p) {
 
 
 		LLVMPositionBuilderBefore(dummy_builder, terminator);
-		lb_run_instrumentation_pass_insert_call(p, exit, dummy_builder);
+		lb_run_instrumentation_pass_insert_call(p, exit, dummy_builder, false);
 	}
 
 	LLVMRemoveStringAttributeAtIndex(p->value, LLVMAttributeIndex_FunctionIndex, LLVM_V_NAME("instrument-function-exit"));
@@ -471,6 +481,8 @@ gb_internal void lb_run_function_pass_manager(LLVMPassManagerRef fpm, lbProcedur
 	// are not removed
 	lb_run_remove_dead_instruction_pass(p);
 
+	lb_run_instrumentation_pass(p);
+
 	switch (pass_manager_kind) {
 	case lbFunctionPassManager_none:
 	    return;
@@ -481,7 +493,6 @@ gb_internal void lb_run_function_pass_manager(LLVMPassManagerRef fpm, lbProcedur
 	    }
 	    break;
 	}
-	lb_run_instrumentation_pass(p);
 
 	LLVMRunFunctionPassManager(fpm, p->value);
 }