From 98eaf5c6c014be7cf159ec11175c15dc170253ce Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 17 Sep 2022 10:20:04 +0100 Subject: Fix #2054 Differing behaviours with defer statements for single vs multiple return values caused by naïve ABI optimization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/llvm_backend_proc.cpp | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) (limited to 'src/llvm_backend_proc.cpp') diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 82e577032..8bbbb0c56 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -577,20 +577,13 @@ void lb_begin_procedure_body(lbProcedure *p) { if (e->token.string != "") { GB_ASSERT(!is_blank_ident(e->token)); - lbAddr res = {}; - if (return_ptr_value.value != nullptr) { - lbValue ptr = return_ptr_value; - if (results->variables.count != 1) { - ptr = lb_emit_struct_ep(p, ptr, cast(i32)i); - } - - res = lb_addr(ptr); - lb_add_entity(p->module, e, ptr); - lb_add_debug_local_variable(p, ptr.value, e->type, e->token); - } else { - res = lb_add_local(p, e->type, e); - } - + // NOTE(bill): Don't even bother trying to optimize this with the return ptr value + // This will violate the defer rules if you do: + // foo :: proc() -> (x, y: T) { + // defer x = ... // defer is executed after the `defer` + // return // the values returned should be zeroed + // } + lbAddr res = lb_add_local(p, e->type, e); if (e->Variable.param_value.kind != ParameterValue_Invalid) { lbValue c = lb_handle_param_value(p, e->type, e->Variable.param_value, e->token.pos); lb_addr_store(p, res, c); -- cgit v1.2.3 From 0ebc2add030d224d59c0b8961dfa75a1f9d13b91 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 17 Sep 2022 12:56:03 +0100 Subject: Use a cache when generating the map header to minimize stack wastage --- src/llvm_backend.cpp | 84 ++++++++++++++++++++++++++--------------------- src/llvm_backend.hpp | 1 + src/llvm_backend_proc.cpp | 5 +-- 3 files changed, 51 insertions(+), 39 deletions(-) (limited to 'src/llvm_backend_proc.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 126bcef11..142ecc348 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -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); } diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index e5bb9455f..d622f3661 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -308,6 +308,7 @@ struct lbProcedure { PtrMap selector_values; PtrMap selector_addr; + PtrMap map_header_cache; }; diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 8bbbb0c56..9e8158a88 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -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); -- cgit v1.2.3 From 4d512c2cf647bdd46f30746cd3544632bfaa8273 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 17 Sep 2022 13:40:29 +0100 Subject: Correct `lb_gen_map_header` initialization --- src/llvm_backend_proc.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/llvm_backend_proc.cpp') diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 9e8158a88..17ed9c2a6 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -381,6 +381,8 @@ lbProcedure *lb_create_dummy_procedure(lbModule *m, String link_name, Type *type lb_add_proc_attribute_at_index(p, offset+parameter_index, "nocapture"); } + map_init(&p->map_header_cache, heap_allocator(), 0); + return p; } -- cgit v1.2.3 From ff97a731521dbeb9a6457a889f30f4fa69e080f7 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 21 Sep 2022 13:03:13 +0100 Subject: Reduce unnecessary map gets --- core/runtime/core_builtin.odin | 3 +- core/runtime/dynamic_map_internal.odin | 28 +++++++++---- src/llvm_backend.cpp | 75 ++++++++++++++++++++++++++++------ src/llvm_backend.hpp | 7 +++- src/llvm_backend_expr.cpp | 22 ++++------ src/llvm_backend_general.cpp | 20 ++------- src/llvm_backend_proc.cpp | 2 +- 7 files changed, 103 insertions(+), 54 deletions(-) (limited to 'src/llvm_backend_proc.cpp') diff --git a/core/runtime/core_builtin.odin b/core/runtime/core_builtin.odin index f8e39b8b2..568deed3b 100644 --- a/core/runtime/core_builtin.odin +++ b/core/runtime/core_builtin.odin @@ -296,7 +296,8 @@ clear_map :: proc "contextless" (m: ^$T/map[$K]$V) { @builtin reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int, loc := #caller_location) { if m != nil { - __dynamic_map_reserve(__get_map_header(m), uint(capacity), loc) + h := __get_map_header_table(T) + __dynamic_map_reserve(m, h, uint(capacity), loc) } } diff --git a/core/runtime/dynamic_map_internal.odin b/core/runtime/dynamic_map_internal.odin index 5a683f234..011513162 100644 --- a/core/runtime/dynamic_map_internal.odin +++ b/core/runtime/dynamic_map_internal.odin @@ -57,17 +57,20 @@ Map_Header :: struct { } // USED INTERNALLY BY THE COMPILER -__dynamic_map_get :: proc "contextless" (h: Map_Header, key_hash: uintptr, key_ptr: rawptr) -> rawptr { - index := __dynamic_map_find(h, key_hash, key_ptr).entry_index - if index != MAP_SENTINEL { - data := uintptr(__dynamic_map_get_entry(h, index)) - return rawptr(data + h.value_offset) +__dynamic_map_get :: proc "contextless" (m: rawptr, table: Map_Header_Table, key_hash: uintptr, key_ptr: rawptr) -> rawptr { + if m != nil { + h := Map_Header{(^Raw_Map)(m), table} + index := __dynamic_map_find(h, key_hash, key_ptr).entry_index + if index != MAP_SENTINEL { + data := uintptr(__dynamic_map_get_entry(h, index)) + return rawptr(data + h.value_offset) + } } return nil } // USED INTERNALLY BY THE COMPILER -__dynamic_map_set :: proc "odin" (h: Map_Header, key_hash: uintptr, key_ptr: rawptr, value: rawptr, loc := #caller_location) -> ^Map_Entry_Header #no_bounds_check { +__dynamic_map_set :: proc "odin" (m: rawptr, table: Map_Header_Table, key_hash: uintptr, key_ptr: rawptr, value: rawptr, loc := #caller_location) -> ^Map_Entry_Header #no_bounds_check { add_entry :: proc "odin" (h: Map_Header, key_hash: uintptr, key_ptr: rawptr, loc := #caller_location) -> Map_Index { prev := Map_Index(h.m.entries.len) c := Map_Index(__dynamic_array_append_nothing(&h.m.entries, h.entry_size, h.entry_align, loc)) @@ -79,11 +82,14 @@ __dynamic_map_set :: proc "odin" (h: Map_Header, key_hash: uintptr, key_ptr: raw } return prev } + assert(condition = m != nil) + + h := Map_Header{(^Raw_Map)(m), table} index := MAP_SENTINEL if len(h.m.hashes) == 0 { - __dynamic_map_reserve(h, INITIAL_MAP_CAP, loc) + __dynamic_map_reserve(m, table, INITIAL_MAP_CAP, loc) __dynamic_map_grow(h, loc) } @@ -119,7 +125,11 @@ __dynamic_map_set :: proc "odin" (h: Map_Header, key_hash: uintptr, key_ptr: raw } // USED INTERNALLY BY THE COMPILER -__dynamic_map_reserve :: proc "odin" (h: Map_Header, cap: uint, loc := #caller_location) { +__dynamic_map_reserve :: proc "odin" (m: rawptr, table: Map_Header_Table, cap: uint, loc := #caller_location) { + assert(condition = m != nil) + + h := Map_Header{(^Raw_Map)(m), table} + c := context if h.m.entries.allocator.procedure != nil { c.allocator = h.m.entries.allocator @@ -352,7 +362,7 @@ ceil_to_pow2 :: proc "contextless" (n: uint) -> uint { __dynamic_map_grow :: proc "odin" (h: Map_Header, loc := #caller_location) { new_count := max(uint(h.m.entries.cap) * 2, INITIAL_MAP_CAP) // Rehash through Reserve - __dynamic_map_reserve(h, new_count, loc) + __dynamic_map_reserve(h.m, h.table, new_count, loc) } __dynamic_map_full :: #force_inline proc "contextless" (h: Map_Header) -> bool { diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 65e6f3eed..6dbfb7331 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -500,10 +500,15 @@ lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, A return value; } -LLVMValueRef lb_gen_map_header_table_internal(lbModule *m, Type *map_type) { +lbAddr lb_gen_map_header_table_internal(lbModule *m, Type *map_type) { map_type = base_type(map_type); GB_ASSERT(map_type->kind == Type_Map); + lbAddr *found = map_get(&m->map_header_table_map, map_type); + if (found) { + return *found; + } + GB_ASSERT(map_type->Map.entry_type->kind == Type_Struct); i64 entry_size = type_size_of (map_type->Map.entry_type); i64 entry_align = type_align_of (map_type->Map.entry_type); @@ -531,7 +536,18 @@ LLVMValueRef lb_gen_map_header_table_internal(lbModule *m, Type *map_type) { const_values[5] = lb_const_int(m, t_uintptr, value_offset).value; const_values[6] = lb_const_int(m, t_int, value_size) .value; - return llvm_const_named_struct(m, t_map_header_table, const_values, gb_count_of(const_values)); + LLVMValueRef llvm_res = llvm_const_named_struct(m, t_map_header_table, const_values, gb_count_of(const_values)); + lbValue res = {llvm_res, t_map_header_table}; + + lbAddr addr = lb_add_global_generated(m, t_map_header_table, res, nullptr); + LLVMValueRef global_data = addr.addr.value; + + LLVMSetLinkage(global_data, LLVMPrivateLinkage); + LLVMSetUnnamedAddress(global_data, LLVMGlobalUnnamedAddr); + LLVMSetGlobalConstant(global_data, true); + + map_set(&m->map_header_table_map, map_type, addr); + return addr; } @@ -550,7 +566,7 @@ lbAddr lb_gen_map_header_internal(lbProcedure *p, lbValue map_val_ptr, Type *map Type *raw_map_ptr_type = map_header_base->Struct.fields[0]->type; LLVMValueRef const_values[2] = {}; const_values[0] = LLVMConstNull(lb_type(p->module, raw_map_ptr_type)); - const_values[1] = lb_gen_map_header_table_internal(p->module, map_type); + const_values[1] = lb_addr_load(p, lb_gen_map_header_table_internal(p->module, map_type)).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); @@ -653,12 +669,30 @@ lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_type, lbValue return hashed_key; } -void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbAddr addr, Type *map_type, - lbValue map_key, lbValue map_value, Ast *node) { +lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key) { + if (p->name == "main.map_type") { + gb_printf_err("HERE!\n"); + } + + Type *map_type = base_type(type_deref(map_ptr.type)); + + lbValue key_ptr = {}; + auto args = array_make(permanent_allocator(), 4); + args[0] = lb_emit_conv(p, map_ptr, t_rawptr); + args[1] = lb_addr_load(p, lb_gen_map_header_table_internal(p->module, map_type)); + args[2] = lb_gen_map_key_hash(p, key, map_type->Map.key, &key_ptr); + args[3] = key_ptr; + + lbValue ptr = lb_emit_runtime_call(p, "__dynamic_map_get", args); + + return lb_emit_conv(p, ptr, alloc_type_pointer(map_type->Map.value)); +} + +void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbValue const &map_ptr, Type *map_type, + lbValue const &map_key, lbValue const &map_value, Ast *node) { map_type = base_type(map_type); GB_ASSERT(map_type->kind == Type_Map); - lbValue h = lb_gen_map_header(p, addr.addr, map_type); lbValue key_ptr = {}; lbValue key_hash = lb_gen_map_key_hash(p, map_key, map_type->Map.key, &key_ptr); lbValue v = lb_emit_conv(p, map_value, map_type->Map.value); @@ -666,15 +700,32 @@ void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbAddr addr, Type *map_ lbAddr value_addr = lb_add_local_generated(p, v.type, false); lb_addr_store(p, value_addr, v); - auto args = array_make(permanent_allocator(), 5); - args[0] = h; - args[1] = key_hash; - args[2] = key_ptr; - args[3] = lb_emit_conv(p, value_addr.addr, t_rawptr); - args[4] = lb_emit_source_code_location(p, node); + auto args = array_make(permanent_allocator(), 6); + args[0] = lb_emit_conv(p, map_ptr, t_rawptr); + args[1] = lb_addr_load(p, lb_gen_map_header_table_internal(p->module, map_type)); + 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); lb_emit_runtime_call(p, "__dynamic_map_set", args); } +void lb_dynamic_map_reserve(lbProcedure *p, lbValue const &map_ptr, isize const capacity, TokenPos const &pos) { + GB_ASSERT(!build_context.no_dynamic_literals); + + String proc_name = {}; + if (p->entity) { + proc_name = p->entity->token.string; + } + + auto args = array_make(permanent_allocator(), 4); + args[0] = lb_emit_conv(p, map_ptr, t_rawptr); + args[1] = lb_addr_load(p, lb_gen_map_header_table_internal(p->module, 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); + lb_emit_runtime_call(p, "__dynamic_map_reserve", args); +} + struct lbGlobalVariable { lbValue var; diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index d622f3661..4447f9a0b 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -159,6 +159,8 @@ struct lbModule { StringMap objc_classes; StringMap objc_selectors; + + PtrMap map_header_table_map; }; struct lbGenerator { @@ -446,7 +448,10 @@ lbValue lb_generate_local_array(lbProcedure *p, Type *elem_type, i64 count, bool lbValue lb_generate_global_array(lbModule *m, Type *elem_type, i64 count, String prefix, i64 id); lbValue lb_gen_map_header(lbProcedure *p, lbValue map_val_ptr, Type *map_type); lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_type, lbValue *key_ptr_); -void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbAddr addr, Type *map_type, lbValue map_key, lbValue map_value, Ast *node); + +lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key); +void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbValue const &map_ptr, Type *map_type, lbValue const &map_key, lbValue const &map_value, Ast *node); +void lb_dynamic_map_reserve(lbProcedure *p, lbValue const &map_ptr, isize const capacity, TokenPos const &pos); lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e); lbValue lb_find_value_from_entity(lbModule *m, Entity *e); diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp index 3ab73a27b..e8138d0a2 100644 --- a/src/llvm_backend_expr.cpp +++ b/src/llvm_backend_expr.cpp @@ -3670,16 +3670,14 @@ lbAddr lb_build_addr_index_expr(lbProcedure *p, Ast *expr) { if (is_type_map(t)) { lbAddr map_addr = lb_build_addr(p, ie->expr); - lbValue map_val = lb_addr_load(p, map_addr); - if (deref) { - map_val = lb_emit_load(p, map_val); - } - lbValue key = lb_build_expr(p, ie->index); key = lb_emit_conv(p, key, t->Map.key); Type *result_type = type_of_expr(expr); - lbValue map_ptr = lb_address_from_load_or_generate_local(p, map_val); + lbValue map_ptr = lb_addr_get_ptr(p, map_addr); + if (is_type_pointer(type_deref(map_ptr.type))) { + map_ptr = lb_emit_load(p, map_ptr); + } return lb_addr_map(map_ptr, key, t, result_type); } @@ -4130,20 +4128,16 @@ lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) { break; } GB_ASSERT(!build_context.no_dynamic_literals); - { - auto args = array_make(permanent_allocator(), 3); - args[0] = lb_gen_map_header(p, v.addr, type); - args[1] = lb_const_int(p->module, t_int, 2*cl->elems.count); - args[2] = lb_emit_source_code_location(p, proc_name, pos); - lb_emit_runtime_call(p, "__dynamic_map_reserve", args); - } + + lb_dynamic_map_reserve(p, v.addr, 2*cl->elems.count, pos); + for_array(field_index, cl->elems) { Ast *elem = cl->elems[field_index]; ast_node(fv, FieldValue, elem); lbValue key = lb_build_expr(p, fv->field); lbValue value = lb_build_expr(p, fv->value); - lb_insert_dynamic_map_key_and_value(p, v, type, key, value, elem); + lb_insert_dynamic_map_key_and_value(p, v.addr, type, key, value, elem); } break; } diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 55b09cbfc..ba1c501ad 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -74,6 +74,9 @@ void lb_init_module(lbModule *m, Checker *c) { string_map_init(&m->objc_classes, a); string_map_init(&m->objc_selectors, a); + + map_init(&m->map_header_table_map, a, 0); + } bool lb_init_generator(lbGenerator *gen, Checker *c) { @@ -383,21 +386,6 @@ Type *lb_addr_type(lbAddr const &addr) { return type_deref(addr.addr.type); } -lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key) { - Type *map_type = base_type(type_deref(map_ptr.type)); - lbValue h = lb_gen_map_header(p, map_ptr, map_type); - - lbValue key_ptr = {}; - auto args = array_make(permanent_allocator(), 3); - args[0] = h; - args[1] = lb_gen_map_key_hash(p, key, map_type->Map.key, &key_ptr); - args[2] = key_ptr; - - lbValue ptr = lb_emit_runtime_call(p, "__dynamic_map_get", args); - - return lb_emit_conv(p, ptr, alloc_type_pointer(map_type->Map.value)); -} - lbValue lb_addr_get_ptr(lbProcedure *p, lbAddr const &addr) { if (addr.addr.value == nullptr) { GB_PANIC("Illegal addr -> nullptr"); @@ -715,7 +703,7 @@ void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value) { return; } else if (addr.kind == lbAddr_Map) { - lb_insert_dynamic_map_key_and_value(p, addr, addr.map.type, addr.map.key, value, p->curr_stmt); + lb_insert_dynamic_map_key_and_value(p, addr.addr, addr.map.type, addr.map.key, value, p->curr_stmt); return; } else if (addr.kind == lbAddr_Context) { lbAddr old_addr = lb_find_or_generate_context_ptr(p); diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 17ed9c2a6..f2cbef5e3 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -889,7 +889,7 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array const &args, GB_ASSERT(param_count-1 <= args.count); param_count -= 1; } else { - GB_ASSERT_MSG(param_count == args.count, "%td == %td", param_count, args.count); + GB_ASSERT_MSG(param_count == args.count, "%td == %td (%s)", param_count, args.count, LLVMPrintValueToString(value.value)); } lbValue result = {}; -- cgit v1.2.3 From 831620bfc4f1210900669ce501723a64f04c1807 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 21 Sep 2022 13:06:02 +0100 Subject: Remove header cache code --- core/runtime/dynamic_map_internal.odin | 3 --- src/llvm_backend.cpp | 25 ------------------------- src/llvm_backend.hpp | 2 -- src/llvm_backend_proc.cpp | 4 ---- 4 files changed, 34 deletions(-) (limited to 'src/llvm_backend_proc.cpp') diff --git a/core/runtime/dynamic_map_internal.odin b/core/runtime/dynamic_map_internal.odin index 011513162..abe58fc5a 100644 --- a/core/runtime/dynamic_map_internal.odin +++ b/core/runtime/dynamic_map_internal.odin @@ -82,7 +82,6 @@ __dynamic_map_set :: proc "odin" (m: rawptr, table: Map_Header_Table, key_hash: } return prev } - assert(condition = m != nil) h := Map_Header{(^Raw_Map)(m), table} @@ -126,8 +125,6 @@ __dynamic_map_set :: proc "odin" (m: rawptr, table: Map_Header_Table, key_hash: // USED INTERNALLY BY THE COMPILER __dynamic_map_reserve :: proc "odin" (m: rawptr, table: Map_Header_Table, cap: uint, loc := #caller_location) { - assert(condition = m != nil) - h := Map_Header{(^Raw_Map)(m), table} c := context diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index b340b9b7a..adccb2869 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -578,31 +578,6 @@ lbAddr lb_gen_map_header_internal(lbProcedure *p, lbValue map_val_ptr, Type *map return h; } - -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)); - - - // 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(); - - 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); -} - lbValue lb_const_hash(lbModule *m, lbValue key, Type *key_type) { if (true) { return {}; diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 4447f9a0b..e69d3a6ed 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -310,7 +310,6 @@ struct lbProcedure { PtrMap selector_values; PtrMap selector_addr; - PtrMap map_header_cache; }; @@ -446,7 +445,6 @@ String lb_get_const_string(lbModule *m, lbValue value); lbValue lb_generate_local_array(lbProcedure *p, Type *elem_type, i64 count, bool zero_init=true); lbValue lb_generate_global_array(lbModule *m, Type *elem_type, i64 count, String prefix, i64 id); -lbValue lb_gen_map_header(lbProcedure *p, lbValue map_val_ptr, Type *map_type); lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_type, lbValue *key_ptr_); lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key); diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index f2cbef5e3..56ffe3fe9 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -123,7 +123,6 @@ lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool ignore_body) p->scope_stack.allocator = a; 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); @@ -380,9 +379,6 @@ lbProcedure *lb_create_dummy_procedure(lbModule *m, String link_name, Type *type lb_add_proc_attribute_at_index(p, offset+parameter_index, "nonnull"); lb_add_proc_attribute_at_index(p, offset+parameter_index, "nocapture"); } - - map_init(&p->map_header_cache, heap_allocator(), 0); - return p; } -- cgit v1.2.3