Browse Source

Temporary patch for `lb_gen_map_header`

gingerBill 2 years ago
parent
commit
5337413c56
1 changed files with 53 additions and 39 deletions
  1. 53 39
      src/llvm_backend.cpp

+ 53 - 39
src/llvm_backend.cpp

@@ -500,59 +500,73 @@ lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, A
 	return value;
 }
 
-lbValue lb_gen_map_header(lbProcedure *p, lbValue map_val_ptr, Type *map_type) {
-	GB_ASSERT_MSG(is_type_pointer(map_val_ptr.type), "%s", type_to_string(map_val_ptr.type));
+lbAddr lb_gen_map_header_internal(lbProcedure *p, lbValue map_val_ptr, Type *map_type) {
 	map_type = base_type(map_type);
 	GB_ASSERT(map_type->kind == Type_Map);
 
-	lbAddr h = {};
-	lbAddr *found = map_get(&p->map_header_cache, map_val_ptr.value);
-	if (found != nullptr) {
-		h = *found;
-	} else {
-		h = lb_add_local_generated(p, t_map_header, false); // all the values will be initialzed later
+	lbAddr h = lb_add_local_generated(p, t_map_header, true); // all the values will be initialzed later
+
+	Type *key_type = map_type->Map.key;
+	Type *val_type = map_type->Map.value;
+	gb_unused(val_type);
+
+	GB_ASSERT(map_type->Map.entry_type->kind == Type_Struct);
+	map_type->Map.entry_type->cached_size = -1;
+	map_type->Map.entry_type->Struct.are_offsets_set = false;
+
+	i64 entry_size   = type_size_of  (map_type->Map.entry_type);
+	i64 entry_align  = type_align_of (map_type->Map.entry_type);
 
-		Type *key_type = map_type->Map.key;
-		Type *val_type = map_type->Map.value;
-		gb_unused(val_type);
+	i64 key_offset = type_offset_of(map_type->Map.entry_type, 2);
+	i64 key_size   = type_size_of  (map_type->Map.key);
 
-		GB_ASSERT(map_type->Map.entry_type->kind == Type_Struct);
-		map_type->Map.entry_type->cached_size = -1;
-		map_type->Map.entry_type->Struct.are_offsets_set = false;
+	i64 value_offset = type_offset_of(map_type->Map.entry_type, 3);
+	i64 value_size   = type_size_of  (map_type->Map.value);
 
-		i64 entry_size   = type_size_of  (map_type->Map.entry_type);
-		i64 entry_align  = type_align_of (map_type->Map.entry_type);
 
-		i64 key_offset = type_offset_of(map_type->Map.entry_type, 2);
-		i64 key_size   = type_size_of  (map_type->Map.key);
+	Type *map_header_base = base_type(t_map_header);
+	GB_ASSERT(map_header_base->Struct.fields.count == 8);
+	Type *raw_map_ptr_type = map_header_base->Struct.fields[0]->type;
+	LLVMValueRef const_values[8] = {};
+	const_values[0] = LLVMConstNull(lb_type(p->module, raw_map_ptr_type));
+	const_values[1] = lb_get_equal_proc_for_type(p->module, key_type)    .value;
+	const_values[2] = lb_const_int(p->module, t_int,        entry_size)  .value;
+	const_values[3] = lb_const_int(p->module, t_int,        entry_align) .value;
+	const_values[4] = lb_const_int(p->module, t_uintptr,    key_offset)  .value;
+	const_values[5] = lb_const_int(p->module, t_int,        key_size)    .value;
+	const_values[6] = lb_const_int(p->module, t_uintptr,    value_offset).value;
+	const_values[7] = lb_const_int(p->module, t_int,        value_size)  .value;
 
-		i64 value_offset = type_offset_of(map_type->Map.entry_type, 3);
-		i64 value_size   = type_size_of  (map_type->Map.value);
+	LLVMValueRef const_value = llvm_const_named_struct(p->module, t_map_header, const_values, gb_count_of(const_values));
+	LLVMBuildStore(p->builder, const_value, h.addr.value);
 
+	// NOTE(bill): Removes unnecessary allocation if split gep
+	lbValue gep0 = lb_emit_struct_ep(p, h.addr, 0);
+	lbValue m = lb_emit_conv(p, map_val_ptr, type_deref(gep0.type));
+	lb_emit_store(p, gep0, m);
+	return h;
+}
 
-		Type *map_header_base = base_type(t_map_header);
-		GB_ASSERT(map_header_base->Struct.fields.count == 8);
-		Type *raw_map_ptr_type = map_header_base->Struct.fields[0]->type;
-		LLVMValueRef const_values[8] = {};
-		const_values[0] = LLVMConstNull(lb_type(p->module, raw_map_ptr_type));
-		const_values[1] = lb_get_equal_proc_for_type(p->module, key_type)    .value;
-		const_values[2] = lb_const_int(p->module, t_int,        entry_size)  .value;
-		const_values[3] = lb_const_int(p->module, t_int,        entry_align) .value;
-		const_values[4] = lb_const_int(p->module, t_uintptr,    key_offset)  .value;
-		const_values[5] = lb_const_int(p->module, t_int,        key_size)    .value;
-		const_values[6] = lb_const_int(p->module, t_uintptr,    value_offset).value;
-		const_values[7] = lb_const_int(p->module, t_int,        value_size)  .value;
 
-		LLVMValueRef const_value = llvm_const_named_struct(p->module, t_map_header, const_values, gb_count_of(const_values));
-		LLVMBuildStore(p->builder, const_value, h.addr.value);
+lbValue lb_gen_map_header(lbProcedure *p, lbValue map_val_ptr, Type *map_type) {
+	GB_ASSERT_MSG(is_type_pointer(map_val_ptr.type), "%s", type_to_string(map_val_ptr.type));
+	GB_ASSERT(is_type_map(map_type));
 
-		// NOTE(bill): Removes unnecessary allocation if split gep
-		lbValue gep0 = lb_emit_struct_ep(p, h.addr, 0);
-		lbValue m = lb_emit_conv(p, map_val_ptr, type_deref(gep0.type));
-		lb_emit_store(p, gep0, m);
 
+	// TODO(bill): this is a temporary fix since this caching is not working other platforms
+	bool allow_caching = build_context.metrics.os == TargetOs_windows || is_arch_wasm();
 
-		map_set(&p->map_header_cache, map_val_ptr.value, h);
+	lbAddr h = {};
+	if (!allow_caching) {
+		h = lb_gen_map_header_internal(p, map_val_ptr, map_type);
+	} else {
+		lbAddr *found = map_get(&p->map_header_cache, map_val_ptr.value);
+		if (found != nullptr) {
+			h = *found;
+		} else {
+			h = lb_gen_map_header_internal(p, map_val_ptr, map_type);
+			map_set(&p->map_header_cache, map_val_ptr.value, h);
+		}
 	}
 
 	return lb_addr_load(p, h);