From cb5d290f33d28ca3921d5a341be48a6d8099ce57 Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Wed, 30 Sep 2020 17:09:18 +0300 Subject: [PATCH 1/2] Added a few ZigList deinit calls --- src/stage1/codegen.cpp | 3 +++ src/stage1/ir.cpp | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/stage1/codegen.cpp b/src/stage1/codegen.cpp index 2fc85b42fe..61139648af 100644 --- a/src/stage1/codegen.cpp +++ b/src/stage1/codegen.cpp @@ -4428,6 +4428,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutableGen *executable, IrIn gen_assign_raw(g, arg_ptr, get_pointer_to_type(g, gen_param_types.at(arg_i), true), gen_param_values.at(arg_i)); } + gen_param_types.deinit(); if (instruction->modifier == CallModifierAsync) { gen_resume(g, fn_val, frame_result_loc, ResumeIdCall); @@ -4505,6 +4506,8 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutableGen *executable, IrIn LLVMValueRef result_ptr = LLVMBuildStructGEP(g->builder, frame_result_loc, frame_ret_start + 2, ""); return LLVMBuildLoad(g->builder, result_ptr, ""); } + } else { + gen_param_types.deinit(); } if (instruction->new_stack == nullptr || instruction->is_async_call_builtin) { diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp index bb4ca8dbf3..fbdae69bb6 100644 --- a/src/stage1/ir.cpp +++ b/src/stage1/ir.cpp @@ -9606,6 +9606,7 @@ static IrInstSrc *ir_gen_continue(IrBuilderSrc *irb, Scope *continue_scope, AstN ScopeRuntime *scope_runtime = runtime_scopes.at(i); ir_mark_gen(ir_build_check_runtime_scope(irb, continue_scope, node, scope_runtime->is_comptime, is_comptime)); } + runtime_scopes.deinit(); IrBasicBlockSrc *dest_block = loop_scope->continue_block; if (!ir_gen_defers_for_block(irb, continue_scope, dest_block->scope, nullptr, nullptr)) @@ -21526,6 +21527,7 @@ static IrInstGen *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstSrcPhi *phi_i predecessor->instruction_list.append(instrs_to_move.pop()); } predecessor->instruction_list.append(branch_instruction); + instrs_to_move.deinit(); } } @@ -21576,7 +21578,10 @@ static IrInstGen *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstSrcPhi *phi_i } if (new_incoming_blocks.length == 1) { - return new_incoming_values.at(0); + IrInstGen *incoming_value = new_incoming_values.at(0); + new_incoming_blocks.deinit(); + new_incoming_values.deinit(); + return incoming_value; } ZigType *resolved_type = nullptr; @@ -24154,6 +24159,7 @@ static IrInstGen *ir_analyze_container_init_fields(IrAnalyze *ira, IrInst *sourc } } + const_ptrs.deinit(); IrInstGen *result = ir_get_deref(ira, source_instr, result_loc, nullptr); if (is_comptime && !instr_is_comptime(result)) { From 82f0ede3ad4150a80ab745419664f1dce4a6be9c Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Wed, 30 Sep 2020 19:59:49 +0300 Subject: [PATCH 2/2] Added some c_allocator.deallocate calls --- src/stage1/analyze.cpp | 6 ++---- src/stage1/codegen.cpp | 33 ++++++++++++++++++++++++++------- src/stage1/ir.cpp | 4 ++++ src/stage1/os.cpp | 2 ++ 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/stage1/analyze.cpp b/src/stage1/analyze.cpp index 369c284684..ce9a459046 100644 --- a/src/stage1/analyze.cpp +++ b/src/stage1/analyze.cpp @@ -3120,12 +3120,9 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { bool create_enum_type = is_auto_enum || (!is_explicit_enum && want_safety); bool *covered_enum_fields; bool *is_zero_bits = heap::c_allocator.allocate(field_count); - ZigLLVMDIEnumerator **di_enumerators; if (create_enum_type) { occupied_tag_values.init(field_count); - di_enumerators = heap::c_allocator.allocate(field_count); - ZigType *tag_int_type; if (enum_type_node != nullptr) { tag_int_type = analyze_type_expr(g, scope, enum_type_node); @@ -3269,7 +3266,6 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { } if (create_enum_type) { - di_enumerators[i] = ZigLLVMCreateDebugEnumerator(g->dbuilder, buf_ptr(union_field->name), i); union_field->enum_field = &tag_type->data.enumeration.fields[i]; union_field->enum_field->name = union_field->name; union_field->enum_field->decl_index = i; @@ -3336,6 +3332,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { gen_field_index += 1; } } + heap::c_allocator.deallocate(is_zero_bits, field_count); bool src_have_tag = is_auto_enum || is_explicit_enum; @@ -3403,6 +3400,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { union_type->data.unionation.resolve_status = ResolveStatusInvalid; } } + heap::c_allocator.deallocate(covered_enum_fields, tag_type->data.enumeration.src_field_count); } if (union_type->data.unionation.resolve_status == ResolveStatusInvalid) { diff --git a/src/stage1/codegen.cpp b/src/stage1/codegen.cpp index 61139648af..2c2c697603 100644 --- a/src/stage1/codegen.cpp +++ b/src/stage1/codegen.cpp @@ -4414,6 +4414,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutableGen *executable, IrIn } } LLVMTypeRef frame_with_args_type = LLVMStructType(field_types, field_count, false); + heap::c_allocator.deallocate(field_types, field_count); LLVMTypeRef ptr_frame_with_args_type = LLVMPointerType(frame_with_args_type, 0); casted_frame = LLVMBuildBitCast(g->builder, frame_result_loc, ptr_frame_with_args_type, ""); @@ -4825,12 +4826,15 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, IrExecutableGen *executable, I ret_type = get_llvm_type(g, instruction->base.value->type); } LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, (unsigned)input_and_output_count, false); + heap::c_allocator.deallocate(param_types, input_and_output_count); bool is_volatile = instruction->has_side_effects || (asm_expr->output_list.length == 0); LLVMValueRef asm_fn = LLVMGetInlineAsm(function_type, buf_ptr(&llvm_template), buf_len(&llvm_template), buf_ptr(&constraint_buf), buf_len(&constraint_buf), is_volatile, false, LLVMInlineAsmDialectATT); - return LLVMBuildCall(g->builder, asm_fn, param_values, (unsigned)input_and_output_count, ""); + LLVMValueRef built_call = LLVMBuildCall(g->builder, asm_fn, param_values, (unsigned)input_and_output_count, ""); + heap::c_allocator.deallocate(param_values, input_and_output_count); + return built_call; } static LLVMValueRef gen_non_null_bit(CodeGen *g, ZigType *maybe_type, LLVMValueRef maybe_handle) { @@ -5083,6 +5087,8 @@ static LLVMValueRef ir_render_phi(CodeGen *g, IrExecutableGen *executable, IrIns incoming_blocks[i] = instruction->incoming_blocks[i]->llvm_exit_block; } LLVMAddIncoming(phi, incoming_values, incoming_blocks, (unsigned)instruction->incoming_count); + heap::c_allocator.deallocate(incoming_values, instruction->incoming_count); + heap::c_allocator.deallocate(incoming_blocks, instruction->incoming_count); return phi; } @@ -7459,10 +7465,14 @@ static LLVMValueRef gen_const_val(CodeGen *g, ZigValue *const_val, const char *n } } if (make_unnamed_struct) { - return LLVMConstStruct(fields, type_entry->data.structure.gen_field_count, + LLVMValueRef unnamed_struct = LLVMConstStruct(fields, type_entry->data.structure.gen_field_count, type_entry->data.structure.layout == ContainerLayoutPacked); + heap::c_allocator.deallocate(fields, type_entry->data.structure.gen_field_count); + return unnamed_struct; } else { - return LLVMConstNamedStruct(get_llvm_type(g, type_entry), fields, type_entry->data.structure.gen_field_count); + LLVMValueRef named_struct = LLVMConstNamedStruct(get_llvm_type(g, type_entry), fields, type_entry->data.structure.gen_field_count); + heap::c_allocator.deallocate(fields, type_entry->data.structure.gen_field_count); + return named_struct; } } case ZigTypeIdArray: @@ -7487,9 +7497,13 @@ static LLVMValueRef gen_const_val(CodeGen *g, ZigValue *const_val, const char *n values[len] = gen_const_val(g, type_entry->data.array.sentinel, ""); } if (make_unnamed_struct) { - return LLVMConstStruct(values, full_len, true); + LLVMValueRef unnamed_struct = LLVMConstStruct(values, full_len, true); + heap::c_allocator.deallocate(values, full_len); + return unnamed_struct; } else { - return LLVMConstArray(element_type_ref, values, (unsigned)full_len); + LLVMValueRef array = LLVMConstArray(element_type_ref, values, (unsigned)full_len); + heap::c_allocator.deallocate(values, full_len); + return array; } } case ConstArraySpecialBuf: { @@ -7511,7 +7525,9 @@ static LLVMValueRef gen_const_val(CodeGen *g, ZigValue *const_val, const char *n ZigValue *elem_value = &const_val->data.x_array.data.s_none.elements[i]; values[i] = gen_const_val(g, elem_value, ""); } - return LLVMConstVector(values, len); + LLVMValueRef vector = LLVMConstVector(values, len); + heap::c_allocator.deallocate(values, len); + return vector; } case ConstArraySpecialBuf: { Buf *buf = const_val->data.x_array.data.s_buf; @@ -7520,7 +7536,9 @@ static LLVMValueRef gen_const_val(CodeGen *g, ZigValue *const_val, const char *n for (uint64_t i = 0; i < len; i += 1) { values[i] = LLVMConstInt(g->builtin_types.entry_u8->llvm_type, buf_ptr(buf)[i], false); } - return LLVMConstVector(values, len); + LLVMValueRef vector = LLVMConstVector(values, len); + heap::c_allocator.deallocate(values, len); + return vector; } } zig_unreachable(); @@ -7742,6 +7760,7 @@ static void generate_error_name_table(CodeGen *g) { } LLVMValueRef err_name_table_init = LLVMConstArray(get_llvm_type(g, str_type), values, (unsigned)g->errors_by_index.length); + heap::c_allocator.deallocate(values, g->errors_by_index.length); g->err_name_table = LLVMAddGlobal(g->module, LLVMTypeOf(err_name_table_init), get_mangled_name(g, buf_ptr(buf_create_from_str("__zig_err_name_table")))); diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp index fbdae69bb6..2aac9e954a 100644 --- a/src/stage1/ir.cpp +++ b/src/stage1/ir.cpp @@ -24144,6 +24144,7 @@ static IrInstGen *ir_analyze_container_init_fields(IrAnalyze *ira, IrInst *sourc first_non_const_instruction = result_loc; } } + heap::c_allocator.deallocate(field_assign_nodes, actual_field_count); if (any_missing) return ira->codegen->invalid_inst_gen; @@ -29840,6 +29841,7 @@ static IrInstGen *ir_analyze_bit_cast(IrAnalyze *ira, IrInst* source_instr, IrIn buf_write_value_bytes(ira->codegen, buf, val); if ((err = buf_read_value_bytes(ira, ira->codegen, source_instr->source_node, buf, result->value))) return ira->codegen->invalid_inst_gen; + heap::c_allocator.deallocate(buf, src_size_bytes); return result; } @@ -30877,6 +30879,8 @@ static IrInstGen *ir_analyze_instruction_bit_reverse(IrAnalyze *ira, IrInstSrcBi ira->codegen->is_big_endian, int_type->data.integral.is_signed); + heap::c_allocator.deallocate(comptime_buf, buf_size); + heap::c_allocator.deallocate(result_buf, buf_size); return result; } diff --git a/src/stage1/os.cpp b/src/stage1/os.cpp index 33d98fd416..8994405dfd 100644 --- a/src/stage1/os.cpp +++ b/src/stage1/os.cpp @@ -127,6 +127,7 @@ static void os_spawn_process_posix(ZigList &args, Termination *ter int status; waitpid(pid, &status, 0); populate_termination(term, status); + heap::c_allocator.deallocate(argv, args.length + 1); } #endif @@ -748,6 +749,7 @@ static Buf os_path_resolve_posix(Buf **paths_ptr, size_t paths_len) { Buf return_value = BUF_INIT; buf_init_from_mem(&return_value, (char *)result_ptr, result_index); + heap::c_allocator.deallocate(result_ptr, result_len); return return_value; } #endif