authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2020-02-25 10:57:47-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-02-25 10:57:47-05:00
log416a547cdb8dbbf3d2e7ce32132f0a25f2a8607e
treef73bfab6414e9e35ae5f1d104b31274b774415ff
parent26b2e5fda86fe5424ebf86924d294d8dbb972eb6
parent770631cc79a655bc5e21184ca15bdb6192e905de
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4515 from mikdusan/stage1-gen-constants

stage1: free more heap after analysis

3 files changed, 30 insertions(+), 8 deletions(-)

src/ir.cpp+19
......@@ -14,6 +14,7 @@
1414#include "range_set.hpp"
1515#include "softfloat.hpp"
1616#include "util.hpp"
17#include "mem_list.hpp"
1718
1819#include <errno.h>
1920
......@@ -28,6 +29,9 @@ struct IrBuilderGen {
2829 CodeGen *codegen;
2930 IrExecutableGen *exec;
3031 IrBasicBlockGen *current_basic_block;
32
33 // track for immediate post-analysis destruction
34 mem::List<IrInstGenConst *> constants;
3135};
3236
3337struct IrAnalyze {
......@@ -725,6 +729,10 @@ static void ira_ref(IrAnalyze *ira) {
725729static void ira_deref(IrAnalyze *ira) {
726730 if (ira->ref_count > 1) {
727731 ira->ref_count -= 1;
732
733 // immediate destruction of dangling IrInstGenConst is not possible
734 // free tracking memory because it will never be used
735 ira->new_irb.constants.deinit(&heap::c_allocator);
728736 return;
729737 }
730738 assert(ira->ref_count != 0);
......@@ -742,6 +750,15 @@ static void ira_deref(IrAnalyze *ira) {
742750 heap::c_allocator.destroy(ira->old_irb.exec);
743751 ira->src_implicit_return_type_list.deinit();
744752 ira->resume_stack.deinit();
753
754 // destroy dangling IrInstGenConst
755 for (size_t i = 0; i < ira->new_irb.constants.length; i += 1) {
756 auto constant = ira->new_irb.constants.items[i];
757 if (constant->base.base.ref_count == 0 && !ir_inst_gen_has_side_effects(&constant->base))
758 destroy_instruction_gen(&constant->base);
759 }
760 ira->new_irb.constants.deinit(&heap::c_allocator);
761
745762 heap::c_allocator.destroy(ira);
746763}
747764
......@@ -12493,12 +12510,14 @@ static IrInstGen *ir_const(IrAnalyze *ira, IrInst *inst, ZigType *ty) {
1249312510 IrInstGen *new_instruction = &const_instruction->base;
1249412511 new_instruction->value->type = ty;
1249512512 new_instruction->value->special = ConstValSpecialStatic;
12513 ira->new_irb.constants.append(&heap::c_allocator, const_instruction);
1249612514 return new_instruction;
1249712515}
1249812516
1249912517static IrInstGen *ir_const_noval(IrAnalyze *ira, IrInst *old_instruction) {
1250012518 IrInstGenConst *const_instruction = ir_create_inst_noval<IrInstGenConst>(&ira->new_irb,
1250112519 old_instruction->scope, old_instruction->source_node);
12520 ira->new_irb.constants.append(&heap::c_allocator, const_instruction);
1250212521 return &const_instruction->base;
1250312522}
1250412523
src/mem_list.hpp+9-6
......@@ -14,11 +14,14 @@ namespace mem {
1414
1515template<typename T>
1616struct List {
17 void deinit(Allocator& allocator) {
18 allocator.deallocate<T>(items, capacity);
17 void deinit(Allocator *allocator) {
18 allocator->deallocate<T>(items, capacity);
19 items = nullptr;
20 length = 0;
21 capacity = 0;
1922 }
2023
21 void append(Allocator& allocator, const T& item) {
24 void append(Allocator *allocator, const T& item) {
2225 ensure_capacity(allocator, length + 1);
2326 items[length++] = item;
2427 }
......@@ -57,7 +60,7 @@ struct List {
5760 return items[length - 1];
5861 }
5962
60 void resize(Allocator& allocator, size_t new_length) {
63 void resize(Allocator *allocator, size_t new_length) {
6164 assert(new_length != SIZE_MAX);
6265 ensure_capacity(allocator, new_length);
6366 length = new_length;
......@@ -67,7 +70,7 @@ struct List {
6770 length = 0;
6871 }
6972
70 void ensure_capacity(Allocator& allocator, size_t new_capacity) {
73 void ensure_capacity(Allocator *allocator, size_t new_capacity) {
7174 if (capacity >= new_capacity)
7275 return;
7376
......@@ -76,7 +79,7 @@ struct List {
7679 better_capacity = better_capacity * 5 / 2 + 8;
7780 } while (better_capacity < new_capacity);
7881
79 items = allocator.reallocate_nonzero<T>(items, capacity, better_capacity);
82 items = allocator->reallocate_nonzero<T>(items, capacity, better_capacity);
8083 capacity = better_capacity;
8184 }
8285
src/mem_profile.cpp+2-2
......@@ -92,7 +92,7 @@ void Profile::print_report(FILE *file) {
9292 auto entry = it.next();
9393 if (!entry)
9494 break;
95 list.append(heap::bootstrap_allocator, &entry->value);
95 list.append(&heap::bootstrap_allocator, &entry->value);
9696 }
9797
9898 qsort(list.items, list.length, sizeof(const Entry *), entry_compare);
......@@ -143,7 +143,7 @@ void Profile::print_report(FILE *file) {
143143 fprintf(file, "\n Total calls alloc: %zu, dealloc: %zu, remain: %zu\n",
144144 total_calls_alloc, total_calls_dealloc, (total_calls_alloc - total_calls_dealloc));
145145
146 list.deinit(heap::bootstrap_allocator);
146 list.deinit(&heap::bootstrap_allocator);
147147}
148148
149149uint32_t Profile::usage_hash(UsageKey key) {