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 {...@@ -41,6 +41,7 @@ struct IrAnalyze {
41 ZigList<IrInstruction *> src_implicit_return_type_list;41 ZigList<IrInstruction *> src_implicit_return_type_list;
42 ZigList<IrSuspendPosition> resume_stack;42 ZigList<IrSuspendPosition> resume_stack;
43 IrBasicBlock *const_predecessor_bb;43 IrBasicBlock *const_predecessor_bb;
44 size_t ref_count;
44};45};
4546
46enum ConstCastResultId {47enum ConstCastResultId {
...@@ -252,6 +253,18 @@ static IrInstruction *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_n...@@ -252,6 +253,18 @@ static IrInstruction *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_n
252 IrInstruction *source_instr, IrInstruction *container_ptr, ZigType *container_type);253 IrInstruction *source_instr, IrInstruction *container_ptr, ZigType *container_type);
253static ResultLoc *no_result_loc(void);254static 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
255static ZigValue *const_ptr_pointee_unchecked(CodeGen *g, ZigValue *const_val) {268static ZigValue *const_ptr_pointee_unchecked(CodeGen *g, ZigValue *const_val) {
256 assert(get_src_ptr_type(const_val->type) != nullptr);269 assert(get_src_ptr_type(const_val->type) != nullptr);
257 assert(const_val->special == ConstValSpecialStatic);270 assert(const_val->special == ConstValSpecialStatic);
...@@ -16011,8 +16024,8 @@ static IrInstruction *ir_analyze_instruction_error_union(IrAnalyze *ira,...@@ -16011,8 +16024,8 @@ static IrInstruction *ir_analyze_instruction_error_union(IrAnalyze *ira,
16011 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type);16024 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type);
16012 result->value->special = ConstValSpecialLazy;16025 result->value->special = ConstValSpecialLazy;
1601316026
16014 LazyValueErrUnionType *lazy_err_union_type = allocate<LazyValueErrUnionType>(1);16027 LazyValueErrUnionType *lazy_err_union_type = allocate<LazyValueErrUnionType>(1, "LazyValueErrUnionType");
16015 lazy_err_union_type->ira = ira;16028 lazy_err_union_type->ira = ira; ira_ref(ira);
16016 result->value->data.x_lazy = &lazy_err_union_type->base;16029 result->value->data.x_lazy = &lazy_err_union_type->base;
16017 lazy_err_union_type->base.id = LazyValueIdErrUnionType;16030 lazy_err_union_type->base.id = LazyValueIdErrUnionType;
1601816031
...@@ -17863,8 +17876,8 @@ static IrInstruction *ir_analyze_optional_type(IrAnalyze *ira, IrInstructionUnOp...@@ -17863,8 +17876,8 @@ static IrInstruction *ir_analyze_optional_type(IrAnalyze *ira, IrInstructionUnOp
17863 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type);17876 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type);
17864 result->value->special = ConstValSpecialLazy;17877 result->value->special = ConstValSpecialLazy;
1786517878
17866 LazyValueOptType *lazy_opt_type = allocate<LazyValueOptType>(1);17879 LazyValueOptType *lazy_opt_type = allocate<LazyValueOptType>(1, "LazyValueOptType");
17867 lazy_opt_type->ira = ira;17880 lazy_opt_type->ira = ira; ira_ref(ira);
17868 result->value->data.x_lazy = &lazy_opt_type->base;17881 result->value->data.x_lazy = &lazy_opt_type->base;
17869 lazy_opt_type->base.id = LazyValueIdOptType;17882 lazy_opt_type->base.id = LazyValueIdOptType;
1787017883
...@@ -19809,8 +19822,8 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,...@@ -19809,8 +19822,8 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
19809 IrInstruction *result = ir_const(ira, &slice_type_instruction->base, ira->codegen->builtin_types.entry_type);19822 IrInstruction *result = ir_const(ira, &slice_type_instruction->base, ira->codegen->builtin_types.entry_type);
19810 result->value->special = ConstValSpecialLazy;19823 result->value->special = ConstValSpecialLazy;
1981119824
19812 LazyValueSliceType *lazy_slice_type = allocate<LazyValueSliceType>(1);19825 LazyValueSliceType *lazy_slice_type = allocate<LazyValueSliceType>(1, "LazyValueSliceType");
19813 lazy_slice_type->ira = ira;19826 lazy_slice_type->ira = ira; ira_ref(ira);
19814 result->value->data.x_lazy = &lazy_slice_type->base;19827 result->value->data.x_lazy = &lazy_slice_type->base;
19815 lazy_slice_type->base.id = LazyValueIdSliceType;19828 lazy_slice_type->base.id = LazyValueIdSliceType;
1981619829
...@@ -19969,8 +19982,8 @@ static IrInstruction *ir_analyze_instruction_size_of(IrAnalyze *ira, IrInstructi...@@ -19969,8 +19982,8 @@ static IrInstruction *ir_analyze_instruction_size_of(IrAnalyze *ira, IrInstructi
19969 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_num_lit_int);19982 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_num_lit_int);
19970 result->value->special = ConstValSpecialLazy;19983 result->value->special = ConstValSpecialLazy;
1997119984
19972 LazyValueSizeOf *lazy_size_of = allocate<LazyValueSizeOf>(1);19985 LazyValueSizeOf *lazy_size_of = allocate<LazyValueSizeOf>(1, "LazyValueSizeOf");
19973 lazy_size_of->ira = ira;19986 lazy_size_of->ira = ira; ira_ref(ira);
19974 result->value->data.x_lazy = &lazy_size_of->base;19987 result->value->data.x_lazy = &lazy_size_of->base;
19975 lazy_size_of->base.id = LazyValueIdSizeOf;19988 lazy_size_of->base.id = LazyValueIdSizeOf;
1997619989
...@@ -24647,8 +24660,8 @@ static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstruct...@@ -24647,8 +24660,8 @@ static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstruct
24647 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_num_lit_int);24660 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_num_lit_int);
24648 result->value->special = ConstValSpecialLazy;24661 result->value->special = ConstValSpecialLazy;
2464924662
24650 LazyValueAlignOf *lazy_align_of = allocate<LazyValueAlignOf>(1);24663 LazyValueAlignOf *lazy_align_of = allocate<LazyValueAlignOf>(1, "LazyValueAlignOf");
24651 lazy_align_of->ira = ira;24664 lazy_align_of->ira = ira; ira_ref(ira);
24652 result->value->data.x_lazy = &lazy_align_of->base;24665 result->value->data.x_lazy = &lazy_align_of->base;
24653 lazy_align_of->base.id = LazyValueIdAlignOf;24666 lazy_align_of->base.id = LazyValueIdAlignOf;
2465424667
...@@ -25131,8 +25144,8 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct...@@ -25131,8 +25144,8 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct
25131 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type);25144 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type);
25132 result->value->special = ConstValSpecialLazy;25145 result->value->special = ConstValSpecialLazy;
2513325146
25134 LazyValueFnType *lazy_fn_type = allocate<LazyValueFnType>(1);25147 LazyValueFnType *lazy_fn_type = allocate<LazyValueFnType>(1, "LazyValueFnType");
25135 lazy_fn_type->ira = ira;25148 lazy_fn_type->ira = ira; ira_ref(ira);
25136 result->value->data.x_lazy = &lazy_fn_type->base;25149 result->value->data.x_lazy = &lazy_fn_type->base;
25137 lazy_fn_type->base.id = LazyValueIdFnType;25150 lazy_fn_type->base.id = LazyValueIdFnType;
2513825151
...@@ -26172,8 +26185,8 @@ static IrInstruction *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstruct...@@ -26172,8 +26185,8 @@ static IrInstruction *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstruct
26172 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type);26185 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type);
26173 result->value->special = ConstValSpecialLazy;26186 result->value->special = ConstValSpecialLazy;
2617426187
26175 LazyValuePtrType *lazy_ptr_type = allocate<LazyValuePtrType>(1);26188 LazyValuePtrType *lazy_ptr_type = allocate<LazyValuePtrType>(1, "LazyValuePtrType");
26176 lazy_ptr_type->ira = ira;26189 lazy_ptr_type->ira = ira; ira_ref(ira);
26177 result->value->data.x_lazy = &lazy_ptr_type->base;26190 result->value->data.x_lazy = &lazy_ptr_type->base;
26178 lazy_ptr_type->base.id = LazyValueIdPtrType;26191 lazy_ptr_type->base.id = LazyValueIdPtrType;
2617926192
...@@ -27646,7 +27659,8 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_...@@ -27646,7 +27659,8 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_
27646 assert(old_exec->first_err_trace_msg == nullptr);27659 assert(old_exec->first_err_trace_msg == nullptr);
27647 assert(expected_type == nullptr || !type_is_invalid(expected_type));27660 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;
27650 old_exec->analysis = ira;27664 old_exec->analysis = ira;
27651 ira->codegen = codegen;27665 ira->codegen = codegen;
2765227666
...@@ -27713,6 +27727,7 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_...@@ -27713,6 +27727,7 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_
27713 ira->instruction_index += 1;27727 ira->instruction_index += 1;
27714 }27728 }
2771527729
27730 ZigType *res_type;
27716 if (new_exec->first_err_trace_msg != nullptr) {27731 if (new_exec->first_err_trace_msg != nullptr) {
27717 codegen->trace_err = new_exec->first_err_trace_msg;27732 codegen->trace_err = new_exec->first_err_trace_msg;
27718 if (codegen->trace_err != nullptr && new_exec->source_node != nullptr &&27733 if (codegen->trace_err != nullptr && new_exec->source_node != nullptr &&
...@@ -27722,13 +27737,18 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_...@@ -27722,13 +27737,18 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_
27722 codegen->trace_err = add_error_note(codegen, codegen->trace_err,27737 codegen->trace_err = add_error_note(codegen, codegen->trace_err,
27723 new_exec->source_node, buf_create_from_str("referenced here"));27738 new_exec->source_node, buf_create_from_str("referenced here"));
27724 }27739 }
27725 return ira->codegen->builtin_types.entry_invalid;27740 res_type = ira->codegen->builtin_types.entry_invalid;
27726 } else if (ira->src_implicit_return_type_list.length == 0) {27741 } 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;
27728 } else {27743 } 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,
27730 ira->src_implicit_return_type_list.length);27745 ira->src_implicit_return_type_list.length);
27731 }27746 }
27747
27748 // It is now safe to free Pass 1 IR instructions.
27749 ira_deref(ira);
27750
27751 return res_type;
27732}27752}
2773327753
27734bool ir_has_side_effects(IrInstruction *instruction) {27754bool ir_has_side_effects(IrInstruction *instruction) {
...@@ -28064,6 +28084,8 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ZigValue *val) {...@@ -28064,6 +28084,8 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ZigValue *val) {
28064 val->special = ConstValSpecialStatic;28084 val->special = ConstValSpecialStatic;
28065 assert(val->type->id == ZigTypeIdComptimeInt || val->type->id == ZigTypeIdInt);28085 assert(val->type->id == ZigTypeIdComptimeInt || val->type->id == ZigTypeIdInt);
28066 bigint_init_unsigned(&val->data.x_bigint, align_in_bytes);28086 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.
28067 return ErrorNone;28089 return ErrorNone;
28068 }28090 }
28069 case LazyValueIdSizeOf: {28091 case LazyValueIdSizeOf: {
...@@ -28119,6 +28141,8 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ZigValue *val) {...@@ -28119,6 +28141,8 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ZigValue *val) {
28119 val->special = ConstValSpecialStatic;28141 val->special = ConstValSpecialStatic;
28120 assert(val->type->id == ZigTypeIdComptimeInt || val->type->id == ZigTypeIdInt);28142 assert(val->type->id == ZigTypeIdComptimeInt || val->type->id == ZigTypeIdInt);
28121 bigint_init_unsigned(&val->data.x_bigint, abi_size);28143 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.
28122 return ErrorNone;28146 return ErrorNone;
28123 }28147 }
28124 case LazyValueIdSliceType: {28148 case LazyValueIdSliceType: {
...@@ -28197,6 +28221,8 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ZigValue *val) {...@@ -28197,6 +28221,8 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ZigValue *val) {
28197 val->special = ConstValSpecialStatic;28221 val->special = ConstValSpecialStatic;
28198 assert(val->type->id == ZigTypeIdMetaType);28222 assert(val->type->id == ZigTypeIdMetaType);
28199 val->data.x_type = get_slice_type(ira->codegen, slice_ptr_type);28223 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.
28200 return ErrorNone;28226 return ErrorNone;
28201 }28227 }
28202 case LazyValueIdPtrType: {28228 case LazyValueIdPtrType: {
...@@ -28268,6 +28294,8 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ZigValue *val) {...@@ -28268,6 +28294,8 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ZigValue *val) {
28268 lazy_ptr_type->bit_offset_in_host, lazy_ptr_type->host_int_bytes,28294 lazy_ptr_type->bit_offset_in_host, lazy_ptr_type->host_int_bytes,
28269 allow_zero, VECTOR_INDEX_NONE, nullptr, sentinel_val);28295 allow_zero, VECTOR_INDEX_NONE, nullptr, sentinel_val);
28270 val->special = ConstValSpecialStatic;28296 val->special = ConstValSpecialStatic;
28297
28298 // We can't free the lazy value here, because multiple other ZigValues might be pointing to it.
28271 return ErrorNone;28299 return ErrorNone;
28272 }28300 }
28273 case LazyValueIdOptType: {28301 case LazyValueIdOptType: {
...@@ -28290,16 +28318,21 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ZigValue *val) {...@@ -28290,16 +28318,21 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ZigValue *val) {
28290 assert(val->type->id == ZigTypeIdMetaType);28318 assert(val->type->id == ZigTypeIdMetaType);
28291 val->data.x_type = get_optional_type(ira->codegen, payload_type);28319 val->data.x_type = get_optional_type(ira->codegen, payload_type);
28292 val->special = ConstValSpecialStatic;28320 val->special = ConstValSpecialStatic;
28321
28322 // We can't free the lazy value here, because multiple other ZigValues might be pointing to it.
28293 return ErrorNone;28323 return ErrorNone;
28294 }28324 }
28295 case LazyValueIdFnType: {28325 case LazyValueIdFnType: {
28296 LazyValueFnType *lazy_fn_type = reinterpret_cast<LazyValueFnType *>(val->data.x_lazy);28326 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);
28298 if (fn_type == nullptr)28329 if (fn_type == nullptr)
28299 return ErrorSemanticAnalyzeFail;28330 return ErrorSemanticAnalyzeFail;
28300 val->special = ConstValSpecialStatic;28331 val->special = ConstValSpecialStatic;
28301 assert(val->type->id == ZigTypeIdMetaType);28332 assert(val->type->id == ZigTypeIdMetaType);
28302 val->data.x_type = fn_type;28333 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.
28303 return ErrorNone;28336 return ErrorNone;
28304 }28337 }
28305 case LazyValueIdErrUnionType: {28338 case LazyValueIdErrUnionType: {
...@@ -28328,6 +28361,8 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ZigValue *val) {...@@ -28328,6 +28361,8 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ZigValue *val) {
28328 assert(val->type->id == ZigTypeIdMetaType);28361 assert(val->type->id == ZigTypeIdMetaType);
28329 val->data.x_type = get_error_union_type(ira->codegen, err_set_type, payload_type);28362 val->data.x_type = get_error_union_type(ira->codegen, err_set_type, payload_type);
28330 val->special = ConstValSpecialStatic;28363 val->special = ConstValSpecialStatic;
28364
28365 // We can't free the lazy value here, because multiple other ZigValues might be pointing to it.
28331 return ErrorNone;28366 return ErrorNone;
28332 }28367 }
28333 }28368 }
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)...@@ -35,7 +35,9 @@ static const char *get_default_name(const char *name_or_null, size_t type_size)
35 if (name_or_null != nullptr) return name_or_null;35 if (name_or_null != nullptr) return name_or_null;
36 if (type_size >= unknown_names.length) {36 if (type_size >= unknown_names.length) {
37 table_active = false;37 table_active = false;
38 unknown_names.resize(type_size + 1);38 while (type_size >= unknown_names.length) {
39 unknown_names.append(nullptr);
40 }
39 table_active = true;41 table_active = true;
40 }42 }
41 if (unknown_names.at(type_size) == nullptr) {43 if (unknown_names.at(type_size) == nullptr) {
...@@ -66,7 +68,8 @@ void memprof_dealloc(const char *name, size_t count, size_t type_size) {...@@ -66,7 +68,8 @@ void memprof_dealloc(const char *name, size_t count, size_t type_size) {
66 name = get_default_name(name, type_size);68 name = get_default_name(name, type_size);
67 auto existing_entry = usage_table.maybe_get(name);69 auto existing_entry = usage_table.maybe_get(name);
68 if (existing_entry == nullptr) {70 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);
70 }73 }
71 if (existing_entry->value.type_size != type_size) {74 if (existing_entry->value.type_size != type_size) {
72 zig_panic("deallocated name '%s' does not match expected type size %zu", name, type_size);75 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)...@@ -165,7 +165,7 @@ static inline void deallocate(T *old, size_t count, const char *name = nullptr)
165165
166template<typename T>166template<typename T>
167static inline void destroy(T *old, const char *name = nullptr) {167static inline void destroy(T *old, const char *name = nullptr) {
168 return deallocate(old, 1);168 return deallocate(old, 1, name);
169}169}
170170
171template <typename T, size_t n>171template <typename T, size_t n>