Use a cache when generating the map header to minimize stack wastage

This commit is contained in:
gingerBill
2022-09-17 12:56:03 +01:00
parent 7840c1b89f
commit 0ebc2add03
3 changed files with 51 additions and 39 deletions

View File

@@ -502,48 +502,58 @@ lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, A
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 h = lb_add_local_generated(p, t_map_header, false); // all the values will be initialzed later
map_type = base_type(map_type);
GB_ASSERT(map_type->kind == Type_Map);
Type *key_type = map_type->Map.key;
Type *val_type = map_type->Map.value;
gb_unused(val_type);
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
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);
i64 key_offset = type_offset_of(map_type->Map.entry_type, 2);
i64 key_size = type_size_of (map_type->Map.key);
Type *key_type = map_type->Map.key;
Type *val_type = map_type->Map.value;
gb_unused(val_type);
i64 value_offset = type_offset_of(map_type->Map.entry_type, 3);
i64 value_size = type_size_of (map_type->Map.value);
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);
// 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);
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);
i64 key_offset = type_offset_of(map_type->Map.entry_type, 2);
i64 key_size = type_size_of (map_type->Map.key);
i64 value_offset = type_offset_of(map_type->Map.entry_type, 3);
i64 value_size = type_size_of (map_type->Map.value);
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);
// 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);
map_set(&p->map_header_cache, map_val_ptr.value, h);
}
return lb_addr_load(p, h);
}

View File

@@ -308,6 +308,7 @@ struct lbProcedure {
PtrMap<Ast *, lbValue> selector_values;
PtrMap<Ast *, lbAddr> selector_addr;
PtrMap<LLVMValueRef, lbAddr> map_header_cache;
};

View File

@@ -121,8 +121,9 @@ lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool ignore_body)
p->branch_blocks.allocator = a;
p->context_stack.allocator = a;
p->scope_stack.allocator = a;
map_init(&p->selector_values, a, 0);
map_init(&p->selector_addr, a, 0);
map_init(&p->selector_values, a, 0);
map_init(&p->selector_addr, a, 0);
map_init(&p->map_header_cache, a, 0);
if (p->is_foreign) {
lb_add_foreign_library_path(p->module, entity->Procedure.foreign_library);