authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-30 22:05:28-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-30 23:57:48-05:00
log5026b1aad550bd85d12480e0a356302e858f8eef
treea0522c50f484d749760d610a477236e06c4704f3
parent951dc451d6d49fca499e9a722a3f543d6e8bf7c1
signaturelock-open Commit is signed but in an unrecognized format.

free IrAnalyze sometimes

Total bytes used in stage1 std lib tests: 3.443 GiB -> 3.418 GiB

3 files changed, 60 insertions(+), 22 deletions(-)

src/ir.cpp+54-19
......@@ -41,6 +41,7 @@ struct IrAnalyze {
4141 ZigList<IrInstruction *> src_implicit_return_type_list;
4242 ZigList<IrSuspendPosition> resume_stack;
4343 IrBasicBlock *const_predecessor_bb;
44 size_t ref_count;
4445};
4546
4647enum ConstCastResultId {
......@@ -252,6 +253,18 @@ static IrInstruction *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_n
252253 IrInstruction *source_instr, IrInstruction *container_ptr, ZigType *container_type);
253254static ResultLoc *no_result_loc(void);
254255
256static void ira_ref(IrAnalyze *ira) {
257 ira->ref_count += 1;
258}
259static void ira_deref(IrAnalyze *ira) {
260 if (ira->ref_count > 1) {
261 ira->ref_count -= 1;
262 return;
263 }
264 assert(ira->ref_count != 0);
265 destroy(ira, "IrAnalyze");
266}
267
255268static ZigValue *const_ptr_pointee_unchecked(CodeGen *g, ZigValue *const_val) {
256269 assert(get_src_ptr_type(const_val->type) != nullptr);
257270 assert(const_val->special == ConstValSpecialStatic);
......@@ -16011,8 +16024,8 @@ static IrInstruction *ir_analyze_instruction_error_union(IrAnalyze *ira,
1601116024 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type);
1601216025 result->value->special = ConstValSpecialLazy;
1601316026
16014 LazyValueErrUnionType *lazy_err_union_type = allocate<LazyValueErrUnionType>(1);
16015 lazy_err_union_type->ira = ira;
16027 LazyValueErrUnionType *lazy_err_union_type = allocate<LazyValueErrUnionType>(1, "LazyValueErrUnionType");
16028 lazy_err_union_type->ira = ira; ira_ref(ira);
1601616029 result->value->data.x_lazy = &lazy_err_union_type->base;
1601716030 lazy_err_union_type->base.id = LazyValueIdErrUnionType;
1601816031
......@@ -17863,8 +17876,8 @@ static IrInstruction *ir_analyze_optional_type(IrAnalyze *ira, IrInstructionUnOp
1786317876 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type);
1786417877 result->value->special = ConstValSpecialLazy;
1786517878
17866 LazyValueOptType *lazy_opt_type = allocate<LazyValueOptType>(1);
17867 lazy_opt_type->ira = ira;
17879 LazyValueOptType *lazy_opt_type = allocate<LazyValueOptType>(1, "LazyValueOptType");
17880 lazy_opt_type->ira = ira; ira_ref(ira);
1786817881 result->value->data.x_lazy = &lazy_opt_type->base;
1786917882 lazy_opt_type->base.id = LazyValueIdOptType;
1787017883
......@@ -19809,8 +19822,8 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1980919822 IrInstruction *result = ir_const(ira, &slice_type_instruction->base, ira->codegen->builtin_types.entry_type);
1981019823 result->value->special = ConstValSpecialLazy;
1981119824
19812 LazyValueSliceType *lazy_slice_type = allocate<LazyValueSliceType>(1);
19813 lazy_slice_type->ira = ira;
19825 LazyValueSliceType *lazy_slice_type = allocate<LazyValueSliceType>(1, "LazyValueSliceType");
19826 lazy_slice_type->ira = ira; ira_ref(ira);
1981419827 result->value->data.x_lazy = &lazy_slice_type->base;
1981519828 lazy_slice_type->base.id = LazyValueIdSliceType;
1981619829
......@@ -19969,8 +19982,8 @@ static IrInstruction *ir_analyze_instruction_size_of(IrAnalyze *ira, IrInstructi
1996919982 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_num_lit_int);
1997019983 result->value->special = ConstValSpecialLazy;
1997119984
19972 LazyValueSizeOf *lazy_size_of = allocate<LazyValueSizeOf>(1);
19973 lazy_size_of->ira = ira;
19985 LazyValueSizeOf *lazy_size_of = allocate<LazyValueSizeOf>(1, "LazyValueSizeOf");
19986 lazy_size_of->ira = ira; ira_ref(ira);
1997419987 result->value->data.x_lazy = &lazy_size_of->base;
1997519988 lazy_size_of->base.id = LazyValueIdSizeOf;
1997619989
......@@ -24647,8 +24660,8 @@ static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstruct
2464724660 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_num_lit_int);
2464824661 result->value->special = ConstValSpecialLazy;
2464924662
24650 LazyValueAlignOf *lazy_align_of = allocate<LazyValueAlignOf>(1);
24651 lazy_align_of->ira = ira;
24663 LazyValueAlignOf *lazy_align_of = allocate<LazyValueAlignOf>(1, "LazyValueAlignOf");
24664 lazy_align_of->ira = ira; ira_ref(ira);
2465224665 result->value->data.x_lazy = &lazy_align_of->base;
2465324666 lazy_align_of->base.id = LazyValueIdAlignOf;
2465424667
......@@ -25131,8 +25144,8 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct
2513125144 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type);
2513225145 result->value->special = ConstValSpecialLazy;
2513325146
25134 LazyValueFnType *lazy_fn_type = allocate<LazyValueFnType>(1);
25135 lazy_fn_type->ira = ira;
25147 LazyValueFnType *lazy_fn_type = allocate<LazyValueFnType>(1, "LazyValueFnType");
25148 lazy_fn_type->ira = ira; ira_ref(ira);
2513625149 result->value->data.x_lazy = &lazy_fn_type->base;
2513725150 lazy_fn_type->base.id = LazyValueIdFnType;
2513825151
......@@ -26172,8 +26185,8 @@ static IrInstruction *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstruct
2617226185 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type);
2617326186 result->value->special = ConstValSpecialLazy;
2617426187
26175 LazyValuePtrType *lazy_ptr_type = allocate<LazyValuePtrType>(1);
26176 lazy_ptr_type->ira = ira;
26188 LazyValuePtrType *lazy_ptr_type = allocate<LazyValuePtrType>(1, "LazyValuePtrType");
26189 lazy_ptr_type->ira = ira; ira_ref(ira);
2617726190 result->value->data.x_lazy = &lazy_ptr_type->base;
2617826191 lazy_ptr_type->base.id = LazyValueIdPtrType;
2617926192
......@@ -27646,7 +27659,8 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_
2764627659 assert(old_exec->first_err_trace_msg == nullptr);
2764727660 assert(expected_type == nullptr || !type_is_invalid(expected_type));
2764827661
27649 IrAnalyze *ira = allocate<IrAnalyze>(1);
27662 IrAnalyze *ira = allocate<IrAnalyze>(1, "IrAnalyze");
27663 ira->ref_count = 1;
2765027664 old_exec->analysis = ira;
2765127665 ira->codegen = codegen;
2765227666
......@@ -27713,6 +27727,7 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_
2771327727 ira->instruction_index += 1;
2771427728 }
2771527729
27730 ZigType *res_type;
2771627731 if (new_exec->first_err_trace_msg != nullptr) {
2771727732 codegen->trace_err = new_exec->first_err_trace_msg;
2771827733 if (codegen->trace_err != nullptr && new_exec->source_node != nullptr &&
......@@ -27722,13 +27737,18 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_
2772227737 codegen->trace_err = add_error_note(codegen, codegen->trace_err,
2772327738 new_exec->source_node, buf_create_from_str("referenced here"));
2772427739 }
27725 return ira->codegen->builtin_types.entry_invalid;
27740 res_type = ira->codegen->builtin_types.entry_invalid;
2772627741 } else if (ira->src_implicit_return_type_list.length == 0) {
27727 return codegen->builtin_types.entry_unreachable;
27742 res_type = codegen->builtin_types.entry_unreachable;
2772827743 } else {
27729 return ir_resolve_peer_types(ira, expected_type_source_node, expected_type, ira->src_implicit_return_type_list.items,
27744 res_type = ir_resolve_peer_types(ira, expected_type_source_node, expected_type, ira->src_implicit_return_type_list.items,
2773027745 ira->src_implicit_return_type_list.length);
2773127746 }
27747
27748 // It is now safe to free Pass 1 IR instructions.
27749 ira_deref(ira);
27750
27751 return res_type;
2773227752}
2773327753
2773427754bool ir_has_side_effects(IrInstruction *instruction) {
......@@ -28064,6 +28084,8 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ZigValue *val) {
2806428084 val->special = ConstValSpecialStatic;
2806528085 assert(val->type->id == ZigTypeIdComptimeInt || val->type->id == ZigTypeIdInt);
2806628086 bigint_init_unsigned(&val->data.x_bigint, align_in_bytes);
28087
28088 // We can't free the lazy value here, because multiple other ZigValues might be pointing to it.
2806728089 return ErrorNone;
2806828090 }
2806928091 case LazyValueIdSizeOf: {
......@@ -28119,6 +28141,8 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ZigValue *val) {
2811928141 val->special = ConstValSpecialStatic;
2812028142 assert(val->type->id == ZigTypeIdComptimeInt || val->type->id == ZigTypeIdInt);
2812128143 bigint_init_unsigned(&val->data.x_bigint, abi_size);
28144
28145 // We can't free the lazy value here, because multiple other ZigValues might be pointing to it.
2812228146 return ErrorNone;
2812328147 }
2812428148 case LazyValueIdSliceType: {
......@@ -28197,6 +28221,8 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ZigValue *val) {
2819728221 val->special = ConstValSpecialStatic;
2819828222 assert(val->type->id == ZigTypeIdMetaType);
2819928223 val->data.x_type = get_slice_type(ira->codegen, slice_ptr_type);
28224
28225 // We can't free the lazy value here, because multiple other ZigValues might be pointing to it.
2820028226 return ErrorNone;
2820128227 }
2820228228 case LazyValueIdPtrType: {
......@@ -28268,6 +28294,8 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ZigValue *val) {
2826828294 lazy_ptr_type->bit_offset_in_host, lazy_ptr_type->host_int_bytes,
2826928295 allow_zero, VECTOR_INDEX_NONE, nullptr, sentinel_val);
2827028296 val->special = ConstValSpecialStatic;
28297
28298 // We can't free the lazy value here, because multiple other ZigValues might be pointing to it.
2827128299 return ErrorNone;
2827228300 }
2827328301 case LazyValueIdOptType: {
......@@ -28290,16 +28318,21 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ZigValue *val) {
2829028318 assert(val->type->id == ZigTypeIdMetaType);
2829128319 val->data.x_type = get_optional_type(ira->codegen, payload_type);
2829228320 val->special = ConstValSpecialStatic;
28321
28322 // We can't free the lazy value here, because multiple other ZigValues might be pointing to it.
2829328323 return ErrorNone;
2829428324 }
2829528325 case LazyValueIdFnType: {
2829628326 LazyValueFnType *lazy_fn_type = reinterpret_cast<LazyValueFnType *>(val->data.x_lazy);
28297 ZigType *fn_type = ir_resolve_lazy_fn_type(lazy_fn_type->ira, source_node, lazy_fn_type);
28327 IrAnalyze *ira = lazy_fn_type->ira;
28328 ZigType *fn_type = ir_resolve_lazy_fn_type(ira, source_node, lazy_fn_type);
2829828329 if (fn_type == nullptr)
2829928330 return ErrorSemanticAnalyzeFail;
2830028331 val->special = ConstValSpecialStatic;
2830128332 assert(val->type->id == ZigTypeIdMetaType);
2830228333 val->data.x_type = fn_type;
28334
28335 // We can't free the lazy value here, because multiple other ZigValues might be pointing to it.
2830328336 return ErrorNone;
2830428337 }
2830528338 case LazyValueIdErrUnionType: {
......@@ -28328,6 +28361,8 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ZigValue *val) {
2832828361 assert(val->type->id == ZigTypeIdMetaType);
2832928362 val->data.x_type = get_error_union_type(ira->codegen, err_set_type, payload_type);
2833028363 val->special = ConstValSpecialStatic;
28364
28365 // We can't free the lazy value here, because multiple other ZigValues might be pointing to it.
2833128366 return ErrorNone;
2833228367 }
2833328368 }
src/memory_profiling.cpp+5-2
......@@ -35,7 +35,9 @@ static const char *get_default_name(const char *name_or_null, size_t type_size)
3535 if (name_or_null != nullptr) return name_or_null;
3636 if (type_size >= unknown_names.length) {
3737 table_active = false;
38 unknown_names.resize(type_size + 1);
38 while (type_size >= unknown_names.length) {
39 unknown_names.append(nullptr);
40 }
3941 table_active = true;
4042 }
4143 if (unknown_names.at(type_size) == nullptr) {
......@@ -66,7 +68,8 @@ void memprof_dealloc(const char *name, size_t count, size_t type_size) {
6668 name = get_default_name(name, type_size);
6769 auto existing_entry = usage_table.maybe_get(name);
6870 if (existing_entry == nullptr) {
69 zig_panic("deallocated more than allocated; compromised memory usage stats");
71 zig_panic("deallocated name '%s' (size %zu) not found in allocated table; compromised memory usage stats",
72 name, type_size);
7073 }
7174 if (existing_entry->value.type_size != type_size) {
7275 zig_panic("deallocated name '%s' does not match expected type size %zu", name, type_size);
src/util.hpp+1-1
......@@ -165,7 +165,7 @@ static inline void deallocate(T *old, size_t count, const char *name = nullptr)
165165
166166template<typename T>
167167static inline void destroy(T *old, const char *name = nullptr) {
168 return deallocate(old, 1);
168 return deallocate(old, 1, name);
169169}
170170
171171template <typename T, size_t n>