Browse Source

Optimize `#caller_location` and `#location` to use read only data section where possible

gingerBill 2 years ago
parent
commit
9da37ed394
4 changed files with 23 additions and 6 deletions
  1. 2 2
      src/llvm_backend.cpp
  2. 17 0
      src/llvm_backend_const.cpp
  3. 2 2
      src/llvm_backend_expr.cpp
  4. 2 2
      src/llvm_backend_proc.cpp

+ 2 - 2
src/llvm_backend.cpp

@@ -646,7 +646,7 @@ void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbValue const &map_ptr,
 	args[2] = key_hash;
 	args[3] = key_ptr;
 	args[4] = lb_emit_conv(p, value_addr.addr, t_rawptr);
-	args[5] = lb_emit_source_code_location(p, node);
+	args[5] = lb_emit_source_code_location_as_global(p, node);
 	lb_emit_runtime_call(p, "__dynamic_map_set", args);
 }
 
@@ -662,7 +662,7 @@ void lb_dynamic_map_reserve(lbProcedure *p, lbValue const &map_ptr, isize const
 	args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
 	args[1] = lb_gen_map_header_table_internal(p, type_deref(map_ptr.type));
 	args[2] = lb_const_int(p->module, t_int, capacity);
-	args[3] = lb_emit_source_code_location(p, proc_name, pos);
+	args[3] = lb_emit_source_code_location_as_global(p, proc_name, pos);
 	lb_emit_runtime_call(p, "__dynamic_map_reserve", args);
 }
 

+ 17 - 0
src/llvm_backend_const.cpp

@@ -283,6 +283,23 @@ lbValue lb_emit_source_code_location(lbProcedure *p, Ast *node) {
 	return lb_emit_source_code_location(p, proc_name, pos);
 }
 
+lbValue lb_emit_source_code_location_as_global(lbProcedure *p, String const &procedure, TokenPos const &pos) {
+	lbValue loc = lb_emit_source_code_location(p, procedure, pos);
+	lbAddr addr = lb_add_global_generated(p->module, loc.type, loc, nullptr);
+	lb_make_global_private_const(addr);
+	return lb_addr_load(p, addr);
+}
+
+
+lbValue lb_emit_source_code_location_as_global(lbProcedure *p, Ast *node) {
+	lbValue loc = lb_emit_source_code_location(p, node);
+	lbAddr addr = lb_add_global_generated(p->module, loc.type, loc, nullptr);
+	lb_make_global_private_const(addr);
+	return lb_addr_load(p, addr);
+}
+
+
+
 LLVMValueRef lb_build_constant_array_values(lbModule *m, Type *type, Type *elem_type, isize count, LLVMValueRef *values, bool allow_local) {
 	bool is_local = allow_local && m->curr_procedure != nullptr;
 	bool is_const = true;

+ 2 - 2
src/llvm_backend_expr.cpp

@@ -4232,7 +4232,7 @@ lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) {
 			args[1] = size;
 			args[2] = align;
 			args[3] = lb_const_int(p->module, t_int, item_count);
-			args[4] = lb_emit_source_code_location(p, proc_name, pos);
+			args[4] = lb_emit_source_code_location_as_global(p, proc_name, pos);
 			lb_emit_runtime_call(p, "__dynamic_array_reserve", args);
 		}
 
@@ -4253,7 +4253,7 @@ lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) {
 			args[2] = align;
 			args[3] = lb_emit_conv(p, items, t_rawptr);
 			args[4] = lb_const_int(p->module, t_int, item_count);
-			args[5] = lb_emit_source_code_location(p, proc_name, pos);
+			args[5] = lb_emit_source_code_location_as_global(p, proc_name, pos);
 			lb_emit_runtime_call(p, "__dynamic_array_append", args);
 		}
 		break;

+ 2 - 2
src/llvm_backend_proc.cpp

@@ -1503,7 +1503,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
 			pos = e->token.pos;
 
 		}
-		return lb_emit_source_code_location(p, procedure, pos);
+		return lb_emit_source_code_location_as_global(p, procedure, pos);
 	}
 
 	case BuiltinProc_type_info_of: {
@@ -2874,7 +2874,7 @@ lbValue lb_handle_param_value(lbProcedure *p, Type *parameter_type, ParameterVal
 			if (p->entity != nullptr) {
 				proc_name = p->entity->token.string;
 			}
-			return lb_emit_source_code_location(p, proc_name, pos);
+			return lb_emit_source_code_location_as_global(p, proc_name, pos);
 		}
 	case ParameterValue_Value:
 		return lb_build_expr(p, param_value.ast_value);