Browse Source

Increase use of `temporary_allocator()` where possible

gingerBill 2 years ago
parent
commit
8dc70f797c
4 changed files with 79 additions and 38 deletions
  1. 42 21
      src/llvm_backend.cpp
  2. 23 8
      src/llvm_backend_expr.cpp
  3. 7 2
      src/llvm_backend_general.cpp
  4. 7 7
      src/llvm_backend_proc.cpp

+ 42 - 21
src/llvm_backend.cpp

@@ -96,10 +96,12 @@ gb_internal void lb_set_entity_from_other_modules_linkage_correctly(lbModule *ot
 }
 
 gb_internal void lb_emit_init_context(lbProcedure *p, lbAddr addr) {
+	TEMPORARY_ALLOCATOR_GUARD();
+
 	GB_ASSERT(addr.kind == lbAddr_Context);
 	GB_ASSERT(addr.ctx.sel.index.count == 0);
 
-	auto args = array_make<lbValue>(permanent_allocator(), 1);
+	auto args = array_make<lbValue>(temporary_allocator(), 1);
 	args[0] = addr.addr;
 	lb_emit_runtime_call(p, "__init_context", args);
 }
@@ -279,9 +281,11 @@ gb_internal lbValue lb_equal_proc_for_type(lbModule *m, Type *type) {
 }
 
 gb_internal lbValue lb_simple_compare_hash(lbProcedure *p, Type *type, lbValue data, lbValue seed) {
+	TEMPORARY_ALLOCATOR_GUARD();
+
 	GB_ASSERT_MSG(is_type_simple_compare(type), "%s", type_to_string(type));
 
-	auto args = array_make<lbValue>(permanent_allocator(), 3);
+	auto args = array_make<lbValue>(temporary_allocator(), 3);
 	args[0] = data;
 	args[1] = seed;
 	args[2] = lb_const_int(p->module, t_int, type_size_of(type));
@@ -334,11 +338,13 @@ gb_internal lbValue lb_hasher_proc_for_type(lbModule *m, Type *type) {
 		return {p->value, p->type};
 	}
 
+	TEMPORARY_ALLOCATOR_GUARD();
+
 	if (type->kind == Type_Struct)  {
 		type_set_offsets(type);
 		data = lb_emit_conv(p, data, t_u8_ptr);
 
-		auto args = array_make<lbValue>(permanent_allocator(), 2);
+		auto args = array_make<lbValue>(temporary_allocator(), 2);
 		for_array(i, type->Struct.fields) {
 			GB_ASSERT(type->Struct.offsets != nullptr);
 			i64 offset = type->Struct.offsets[i];
@@ -352,7 +358,7 @@ gb_internal lbValue lb_hasher_proc_for_type(lbModule *m, Type *type) {
 		}
 		LLVMBuildRet(p->builder, seed.value);
 	} else if (type->kind == Type_Union)  {
-		auto args = array_make<lbValue>(permanent_allocator(), 2);
+		auto args = array_make<lbValue>(temporary_allocator(), 2);
 
 		if (is_type_union_maybe_pointer(type)) {
 			Type *v = type->Union.variants[0];
@@ -396,7 +402,7 @@ gb_internal lbValue lb_hasher_proc_for_type(lbModule *m, Type *type) {
 		lbAddr pres = lb_add_local_generated(p, t_uintptr, false);
 		lb_addr_store(p, pres, seed);
 
-		auto args = array_make<lbValue>(permanent_allocator(), 2);
+		auto args = array_make<lbValue>(temporary_allocator(), 2);
 		lbValue elem_hasher = lb_hasher_proc_for_type(m, type->Array.elem);
 
 		auto loop_data = lb_loop_start(p, cast(isize)type->Array.count, t_i32);
@@ -417,7 +423,7 @@ gb_internal lbValue lb_hasher_proc_for_type(lbModule *m, Type *type) {
 		lbAddr res = lb_add_local_generated(p, t_uintptr, false);
 		lb_addr_store(p, res, seed);
 
-		auto args = array_make<lbValue>(permanent_allocator(), 2);
+		auto args = array_make<lbValue>(temporary_allocator(), 2);
 		lbValue elem_hasher = lb_hasher_proc_for_type(m, type->EnumeratedArray.elem);
 
 		auto loop_data = lb_loop_start(p, cast(isize)type->EnumeratedArray.count, t_i32);
@@ -435,14 +441,14 @@ gb_internal lbValue lb_hasher_proc_for_type(lbModule *m, Type *type) {
 		lbValue vres = lb_addr_load(p, res);
 		LLVMBuildRet(p->builder, vres.value);
 	} else if (is_type_cstring(type)) {
-		auto args = array_make<lbValue>(permanent_allocator(), 2);
+		auto args = array_make<lbValue>(temporary_allocator(), 2);
 		args[0] = data;
 		args[1] = seed;
 		lbValue res = lb_emit_runtime_call(p, "default_hasher_cstring", args);
 		lb_add_callsite_force_inline(p, res);
 		LLVMBuildRet(p->builder, res.value);
 	} else if (is_type_string(type)) {
-		auto args = array_make<lbValue>(permanent_allocator(), 2);
+		auto args = array_make<lbValue>(temporary_allocator(), 2);
 		args[0] = data;
 		args[1] = seed;
 		lbValue res = lb_emit_runtime_call(p, "default_hasher_string", args);
@@ -527,7 +533,8 @@ gb_internal lbValue lb_map_get_proc_for_type(lbModule *m, Type *type) {
 	lbValue mask = lb_emit_conv(p, lb_emit_arith(p, Token_Sub, capacity, lb_const_int(m, t_int, 1), t_int), t_uintptr);
 
 	{
-		auto args = array_make<lbValue>(heap_allocator(), 2);
+		TEMPORARY_ALLOCATOR_GUARD();
+		auto args = array_make<lbValue>(temporary_allocator(), 2);
 		args[0] = map;
 		args[1] = h;
 		lb_addr_store(p, pos, lb_emit_runtime_call(p, "map_desired_position", args));
@@ -554,7 +561,8 @@ gb_internal lbValue lb_map_get_proc_for_type(lbModule *m, Type *type) {
 
 	lb_start_block(p, probe_block);
 	{
-		auto args = array_make<lbValue>(heap_allocator(), 3);
+		TEMPORARY_ALLOCATOR_GUARD();
+		auto args = array_make<lbValue>(temporary_allocator(), 3);
 		args[0] = map;
 		args[1] = element_hash;
 		args[2] = lb_addr_load(p, pos);
@@ -612,6 +620,8 @@ gb_internal lbValue lb_map_get_proc_for_type(lbModule *m, Type *type) {
 // }
 
 gb_internal lbValue lb_map_set_proc_for_type(lbModule *m, Type *type) {
+	TEMPORARY_ALLOCATOR_GUARD();
+
 	GB_ASSERT(!build_context.dynamic_map_calls);
 	type = base_type(type);
 	GB_ASSERT(type->kind == Type_Map);
@@ -673,7 +683,7 @@ gb_internal lbValue lb_map_set_proc_for_type(lbModule *m, Type *type) {
 	{
 		lbValue map_get_proc = lb_map_get_proc_for_type(m, type);
 
-		auto args = array_make<lbValue>(permanent_allocator(), 3);
+		auto args = array_make<lbValue>(temporary_allocator(), 3);
 		args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
 		args[1] = hash;
 		args[2] = key_ptr;
@@ -699,7 +709,7 @@ gb_internal lbValue lb_map_set_proc_for_type(lbModule *m, Type *type) {
 	lbValue map_info = lb_gen_map_info_ptr(p->module, type);
 
 	{
-		auto args = array_make<lbValue>(permanent_allocator(), 3);
+		auto args = array_make<lbValue>(temporary_allocator(), 3);
 		args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
 		args[1] = map_info;
 		args[2] = lb_emit_load(p, location_ptr);
@@ -713,7 +723,7 @@ gb_internal lbValue lb_map_set_proc_for_type(lbModule *m, Type *type) {
 
 	lb_start_block(p, insert_block);
 	{
-		auto args = array_make<lbValue>(permanent_allocator(), 5);
+		auto args = array_make<lbValue>(temporary_allocator(), 5);
 		args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
 		args[1] = map_info;
 		args[2] = hash;
@@ -883,6 +893,8 @@ gb_internal lbValue lb_const_hash(lbModule *m, lbValue key, Type *key_type) {
 }
 
 gb_internal lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_type, lbValue *key_ptr_) {
+	TEMPORARY_ALLOCATOR_GUARD();
+
 	lbValue key_ptr = lb_address_from_load_or_generate_local(p, key);
 	key_ptr = lb_emit_conv(p, key_ptr, t_rawptr);
 
@@ -892,7 +904,7 @@ gb_internal lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_t
 	if (hashed_key.value == nullptr) {
 		lbValue hasher = lb_hasher_proc_for_type(p->module, key_type);
 
-		auto args = array_make<lbValue>(permanent_allocator(), 2);
+		auto args = array_make<lbValue>(temporary_allocator(), 2);
 		args[0] = key_ptr;
 		args[1] = lb_const_int(p->module, t_uintptr, 0);
 		hashed_key = lb_emit_call(p, hasher, args);
@@ -902,6 +914,8 @@ gb_internal lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_t
 }
 
 gb_internal lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key) {
+	TEMPORARY_ALLOCATOR_GUARD();
+
 	Type *map_type = base_type(type_deref(map_ptr.type));
 	GB_ASSERT(map_type->kind == Type_Map);
 
@@ -910,7 +924,7 @@ gb_internal lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue cons
 	lbValue hash = lb_gen_map_key_hash(p, key, map_type->Map.key, &key_ptr);
 
 	if (build_context.dynamic_map_calls) {
-		auto args = array_make<lbValue>(permanent_allocator(), 4);
+		auto args = array_make<lbValue>(temporary_allocator(), 4);
 		args[0] = lb_emit_transmute(p, map_ptr, t_raw_map_ptr);
 		args[1] = lb_gen_map_info_ptr(p->module, map_type);
 		args[2] = hash;
@@ -920,7 +934,7 @@ gb_internal lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue cons
 	} else {
 		lbValue map_get_proc = lb_map_get_proc_for_type(p->module, map_type);
 
-		auto args = array_make<lbValue>(permanent_allocator(), 3);
+		auto args = array_make<lbValue>(temporary_allocator(), 3);
 		args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
 		args[1] = hash;
 		args[2] = key_ptr;
@@ -932,6 +946,8 @@ gb_internal lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue cons
 
 gb_internal void lb_internal_dynamic_map_set(lbProcedure *p, lbValue const &map_ptr, Type *map_type,
                                              lbValue const &map_key, lbValue const &map_value, Ast *node) {
+	TEMPORARY_ALLOCATOR_GUARD();
+
 	map_type = base_type(map_type);
 	GB_ASSERT(map_type->kind == Type_Map);
 
@@ -942,7 +958,7 @@ gb_internal void lb_internal_dynamic_map_set(lbProcedure *p, lbValue const &map_
 	lbValue value_ptr = lb_address_from_load_or_generate_local(p, v);
 
 	if (build_context.dynamic_map_calls) {
-		auto args = array_make<lbValue>(permanent_allocator(), 6);
+		auto args = array_make<lbValue>(temporary_allocator(), 6);
 		args[0] = lb_emit_conv(p, map_ptr, t_raw_map_ptr);
 		args[1] = lb_gen_map_info_ptr(p->module, map_type);
 		args[2] = hash;
@@ -953,7 +969,7 @@ gb_internal void lb_internal_dynamic_map_set(lbProcedure *p, lbValue const &map_
 	} else {
 		lbValue map_set_proc = lb_map_set_proc_for_type(p->module, map_type);
 
-		auto args = array_make<lbValue>(permanent_allocator(), 5);
+		auto args = array_make<lbValue>(temporary_allocator(), 5);
 		args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
 		args[1] = hash;
 		args[2] = lb_emit_conv(p, key_ptr, t_rawptr);
@@ -967,12 +983,14 @@ gb_internal void lb_internal_dynamic_map_set(lbProcedure *p, lbValue const &map_
 gb_internal lbValue lb_dynamic_map_reserve(lbProcedure *p, lbValue const &map_ptr, isize const capacity, TokenPos const &pos) {
 	GB_ASSERT(!build_context.no_dynamic_literals);
 
+	TEMPORARY_ALLOCATOR_GUARD();
+
 	String proc_name = {};
 	if (p->entity) {
 		proc_name = p->entity->token.string;
 	}
 
-	auto args = array_make<lbValue>(permanent_allocator(), 4);
+	auto args = array_make<lbValue>(temporary_allocator(), 4);
 	args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
 	args[1] = lb_gen_map_info_ptr(p->module, type_deref(map_ptr.type));
 	args[2] = lb_const_int(p->module, t_uint, capacity);
@@ -1032,7 +1050,9 @@ gb_internal void lb_finalize_objc_names(lbProcedure *p) {
 	}
 	lbModule *m = p->module;
 
-	auto args = array_make<lbValue>(permanent_allocator(), 1);
+	TEMPORARY_ALLOCATOR_GUARD();
+
+	auto args = array_make<lbValue>(temporary_allocator(), 1);
 
 	LLVMSetLinkage(p->value, LLVMInternalLinkage);
 	lb_begin_procedure_body(p);
@@ -1811,7 +1831,8 @@ gb_internal lbProcedure *lb_create_main_procedure(lbModule *m, lbProcedure *star
 
 		lbValue runner = lb_find_package_value(m, str_lit("testing"), str_lit("runner"));
 
-		auto args = array_make<lbValue>(permanent_allocator(), 1);
+		TEMPORARY_ALLOCATOR_GUARD();
+		auto args = array_make<lbValue>(temporary_allocator(), 1);
 		args[0] = lb_addr_load(p, all_tests_slice);
 		lb_emit_call(p, runner, args);
 	} else {

+ 23 - 8
src/llvm_backend_expr.cpp

@@ -1072,7 +1072,9 @@ gb_internal lbValue lb_emit_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbV
 		Type *ft = base_complex_elem_type(type);
 
 		if (op == Token_Quo) {
-			auto args = array_make<lbValue>(permanent_allocator(), 2);
+			TEMPORARY_ALLOCATOR_GUARD();
+
+			auto args = array_make<lbValue>(temporary_allocator(), 2);
 			args[0] = lhs;
 			args[1] = rhs;
 
@@ -1146,7 +1148,9 @@ gb_internal lbValue lb_emit_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbV
 
 			return lb_addr_load(p, res);
 		} else if (op == Token_Mul) {
-			auto args = array_make<lbValue>(permanent_allocator(), 2);
+			TEMPORARY_ALLOCATOR_GUARD();
+
+			auto args = array_make<lbValue>(temporary_allocator(), 2);
 			args[0] = lhs;
 			args[1] = rhs;
 
@@ -1156,7 +1160,9 @@ gb_internal lbValue lb_emit_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbV
 			default: GB_PANIC("Unknown float type"); break;
 			}
 		} else if (op == Token_Quo) {
-			auto args = array_make<lbValue>(permanent_allocator(), 2);
+			TEMPORARY_ALLOCATOR_GUARD();
+
+			auto args = array_make<lbValue>(temporary_allocator(), 2);
 			args[0] = lhs;
 			args[1] = rhs;
 
@@ -1647,8 +1653,10 @@ gb_internal lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) {
 	}
 
 	if (are_types_identical(src, t_cstring) && are_types_identical(dst, t_string)) {
+		TEMPORARY_ALLOCATOR_GUARD();
+
 		lbValue c = lb_emit_conv(p, value, t_cstring);
-		auto args = array_make<lbValue>(permanent_allocator(), 1);
+		auto args = array_make<lbValue>(temporary_allocator(), 1);
 		args[0] = c;
 		lbValue s = lb_emit_runtime_call(p, "cstring_to_string", args);
 		return lb_emit_conv(p, s, dst);
@@ -1792,6 +1800,8 @@ gb_internal lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) {
 		}
 
 		if (is_type_integer_128bit(dst)) {
+			TEMPORARY_ALLOCATOR_GUARD();
+
 			auto args = array_make<lbValue>(temporary_allocator(), 1);
 			args[0] = value;
 			char const *call = "fixunsdfdi";
@@ -1825,6 +1835,8 @@ gb_internal lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) {
 		}
 
 		if (is_type_integer_128bit(src)) {
+			TEMPORARY_ALLOCATOR_GUARD();
+
 			auto args = array_make<lbValue>(temporary_allocator(), 1);
 			args[0] = value;
 			char const *call = "floattidf";
@@ -2219,16 +2231,17 @@ gb_internal lbValue lb_compare_records(lbProcedure *p, TokenKind op_kind, lbValu
 		}
 		GB_PANIC("invalid operator");
 	}
+	TEMPORARY_ALLOCATOR_GUARD();
 	if (is_type_simple_compare(type)) {
 		// TODO(bill): Test to see if this is actually faster!!!!
-		auto args = array_make<lbValue>(permanent_allocator(), 3);
+		auto args = array_make<lbValue>(temporary_allocator(), 3);
 		args[0] = lb_emit_conv(p, left_ptr, t_rawptr);
 		args[1] = lb_emit_conv(p, right_ptr, t_rawptr);
 		args[2] = lb_const_int(p->module, t_int, type_size_of(type));
 		res = lb_emit_runtime_call(p, "memory_equal", args);
 	} else {
 		lbValue value = lb_equal_proc_for_type(p->module, type);
-		auto args = array_make<lbValue>(permanent_allocator(), 2);
+		auto args = array_make<lbValue>(temporary_allocator(), 2);
 		args[0] = lb_emit_conv(p, left_ptr, t_rawptr);
 		args[1] = lb_emit_conv(p, right_ptr, t_rawptr);
 		res = lb_emit_call(p, value, args);
@@ -4052,6 +4065,8 @@ gb_internal lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) {
 
 	lbAddr v = lb_add_local_generated(p, type, true);
 
+	TEMPORARY_ALLOCATOR_GUARD();
+
 	Type *et = nullptr;
 	switch (bt->kind) {
 	case Type_Array:           et = bt->Array.elem;           break;
@@ -4247,7 +4262,7 @@ gb_internal lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) {
 		i64 item_count = gb_max(cl->max_count, cl->elems.count);
 		{
 
-			auto args = array_make<lbValue>(permanent_allocator(), 5);
+			auto args = array_make<lbValue>(temporary_allocator(), 5);
 			args[0] = lb_emit_conv(p, lb_addr_get_ptr(p, v), t_rawptr);
 			args[1] = size;
 			args[2] = align;
@@ -4267,7 +4282,7 @@ gb_internal lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) {
 		lb_build_addr_compound_lit_assign_array(p, temp_data);
 
 		{
-			auto args = array_make<lbValue>(permanent_allocator(), 6);
+			auto args = array_make<lbValue>(temporary_allocator(), 6);
 			args[0] = lb_emit_conv(p, v.addr, t_rawptr);
 			args[1] = size;
 			args[2] = align;

+ 7 - 2
src/llvm_backend_general.cpp

@@ -496,6 +496,8 @@ gb_internal void lb_emit_bounds_check(lbProcedure *p, Token token, lbValue index
 		return;
 	}
 
+	TEMPORARY_ALLOCATOR_GUARD();
+
 	index = lb_emit_conv(p, index, t_int);
 	len = lb_emit_conv(p, len, t_int);
 
@@ -503,7 +505,7 @@ gb_internal void lb_emit_bounds_check(lbProcedure *p, Token token, lbValue index
 	lbValue line = lb_const_int(p->module, t_i32, token.pos.line);
 	lbValue column = lb_const_int(p->module, t_i32, token.pos.column);
 
-	auto args = array_make<lbValue>(permanent_allocator(), 5);
+	auto args = array_make<lbValue>(temporary_allocator(), 5);
 	args[0] = file;
 	args[1] = line;
 	args[2] = column;
@@ -521,6 +523,8 @@ gb_internal void lb_emit_matrix_bounds_check(lbProcedure *p, Token token, lbValu
 		return;
 	}
 
+	TEMPORARY_ALLOCATOR_GUARD();
+
 	row_index = lb_emit_conv(p, row_index, t_int);
 	column_index = lb_emit_conv(p, column_index, t_int);
 	row_count = lb_emit_conv(p, row_count, t_int);
@@ -530,7 +534,7 @@ gb_internal void lb_emit_matrix_bounds_check(lbProcedure *p, Token token, lbValu
 	lbValue line = lb_const_int(p->module, t_i32, token.pos.line);
 	lbValue column = lb_const_int(p->module, t_i32, token.pos.column);
 
-	auto args = array_make<lbValue>(permanent_allocator(), 7);
+	auto args = array_make<lbValue>(temporary_allocator(), 7);
 	args[0] = file;
 	args[1] = line;
 	args[2] = column;
@@ -1336,6 +1340,7 @@ gb_internal void lb_emit_store_union_variant(lbProcedure *p, lbValue parent, lbV
 
 
 gb_internal void lb_clone_struct_type(LLVMTypeRef dst, LLVMTypeRef src) {
+	TEMPORARY_ALLOCATOR_GUARD();
 	unsigned field_count = LLVMCountStructElementTypes(src);
 	LLVMTypeRef *fields = gb_alloc_array(temporary_allocator(), LLVMTypeRef, field_count);
 	LLVMGetStructElementTypes(src, fields);

+ 7 - 7
src/llvm_backend_proc.cpp

@@ -776,16 +776,16 @@ gb_internal void lb_build_nested_proc(lbProcedure *p, AstProcLit *pd, Entity *e)
 
 
 
-gb_internal Array<lbValue> lb_value_to_array(lbProcedure *p, lbValue value) {
+gb_internal Array<lbValue> lb_value_to_array(lbProcedure *p, gbAllocator const &allocator, lbValue value) {
 	Array<lbValue> array = {};
 	Type *t = base_type(value.type);
 	if (t == nullptr) {
 		// Do nothing
 	} else if (is_type_tuple(t)) {
-		array = array_make<lbValue>(permanent_allocator(), 0, t->Tuple.variables.count);
+		array = array_make<lbValue>(allocator, 0, t->Tuple.variables.count);
 		lb_append_tuple_values(p, &array, value);
 	} else {
-		array = array_make<lbValue>(permanent_allocator(), 1);
+		array = array_make<lbValue>(allocator, 1);
 		array[0] = value;
 	}
 	return array;
@@ -1175,15 +1175,15 @@ gb_internal lbValue lb_emit_call(lbProcedure *p, lbValue value, Array<lbValue> c
 			case DeferredProcedure_none:
 				break;
 			case DeferredProcedure_in:
-				result_as_args = in_args;
+				result_as_args = array_clone(heap_allocator(), in_args);
 				break;
 			case DeferredProcedure_out:
-				result_as_args = lb_value_to_array(p, result);
+				result_as_args = lb_value_to_array(p, heap_allocator(), result);
 				break;
 			case DeferredProcedure_in_out:
 				{
-					auto out_args = lb_value_to_array(p, result);
-					array_init(&result_as_args, permanent_allocator(), in_args.count + out_args.count);
+					auto out_args = lb_value_to_array(p, heap_allocator(), result);
+					array_init(&result_as_args, heap_allocator(), in_args.count + out_args.count);
 					array_copy(&result_as_args, in_args, 0);
 					array_copy(&result_as_args, out_args, in_args.count);
 				}