authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2019-11-25 15:04:49-05:00
committergravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2019-11-25 15:04:49-05:00
loga647a88dfc6cdef66316399f10084b35f738b093
tree8b2ea8968a6e273bf4a7b052a8bceb0eadcab1b6
parent8f3e972da6badf6df82c88ba0c60aca7b545f62c
signaturelock-open Commit is signed but in an unrecognized format.

const interning for 1-possible-value types


9 files changed, 118 insertions(+), 23 deletions(-)

src/all_types.hpp+7
......@@ -2021,6 +2021,13 @@ struct CodeGen {
20212021 ZigType *entry_any_frame;
20222022 } builtin_types;
20232023
2024 struct {
2025 ZigValue x_undefined;
2026 ZigValue x_void;
2027 ZigValue x_null;
2028 ZigValue x_unreachable;
2029 } intern_values;
2030
20242031 ZigType *align_amt_type;
20252032 ZigType *stack_trace_type;
20262033 ZigType *err_tag_type;
src/bigint.cpp+5
......@@ -195,6 +195,11 @@ void bigint_init_bigint(BigInt *dest, const BigInt *src) {
195195 memcpy(dest->data.digits, src->data.digits, sizeof(uint64_t) * dest->digit_count);
196196}
197197
198void bigint_deinit(BigInt *bi) {
199 if (bi->digit_count > 1)
200 deallocate<uint64_t>(bi->data.digits, bi->digit_count);
201}
202
198203void bigint_init_bigfloat(BigInt *dest, const BigFloat *op) {
199204 float128_t zero;
200205 ui32_to_f128M(0, &zero);
src/bigint.hpp+1
......@@ -34,6 +34,7 @@ void bigint_init_signed(BigInt *dest, int64_t x);
3434void bigint_init_bigint(BigInt *dest, const BigInt *src);
3535void bigint_init_bigfloat(BigInt *dest, const BigFloat *op);
3636void bigint_init_data(BigInt *dest, const uint64_t *digits, size_t digit_count, bool is_negative);
37void bigint_deinit(BigInt *bi);
3738
3839// panics if number won't fit
3940uint64_t bigint_as_u64(const BigInt *bigint);
src/codegen.cpp+28
......@@ -8008,6 +8008,33 @@ static void define_builtin_types(CodeGen *g) {
80088008 }
80098009}
80108010
8011static void define_intern_values(CodeGen *g) {
8012 {
8013 auto& value = g->intern_values.x_undefined;
8014 value.type = g->builtin_types.entry_undef;
8015 value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.undefined");
8016 value.special = ConstValSpecialStatic;
8017 }
8018 {
8019 auto& value = g->intern_values.x_void;
8020 value.type = g->builtin_types.entry_void;
8021 value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.void");
8022 value.special = ConstValSpecialStatic;
8023 }
8024 {
8025 auto& value = g->intern_values.x_null;
8026 value.type = g->builtin_types.entry_null;
8027 value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.null");
8028 value.special = ConstValSpecialStatic;
8029 }
8030 {
8031 auto& value = g->intern_values.x_unreachable;
8032 value.type = g->builtin_types.entry_unreachable;
8033 value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.unreachable");
8034 value.special = ConstValSpecialStatic;
8035 }
8036}
8037
80118038static BuiltinFnEntry *create_builtin_fn(CodeGen *g, BuiltinFnId id, const char *name, size_t count) {
80128039 BuiltinFnEntry *builtin_fn = allocate<BuiltinFnEntry>(1);
80138040 buf_init_from_str(&builtin_fn->name, name);
......@@ -8629,6 +8656,7 @@ static void init(CodeGen *g) {
86298656 g->dummy_di_file = nullptr;
86308657
86318658 define_builtin_types(g);
8659 define_intern_values(g);
86328660
86338661 IrInstruction *sentinel_instructions = allocate<IrInstruction>(2);
86348662 g->invalid_instruction = &sentinel_instructions[0];
src/hash_map.hpp+6-6
......@@ -23,10 +23,10 @@ public:
2323 }
2424
2525 struct Entry {
26 bool used;
27 int distance_from_start_index;
2826 K key;
2927 V value;
28 bool used;
29 int distance_from_start_index;
3030 };
3131
3232 void clear() {
......@@ -187,10 +187,10 @@ private:
187187 if (distance_from_start_index > _max_distance_from_start_index)
188188 _max_distance_from_start_index = distance_from_start_index;
189189 *entry = {
190 true,
191 distance_from_start_index,
192190 key,
193191 value,
192 true,
193 distance_from_start_index,
194194 };
195195 key = tmp.key;
196196 value = tmp.value;
......@@ -208,10 +208,10 @@ private:
208208 if (distance_from_start_index > _max_distance_from_start_index)
209209 _max_distance_from_start_index = distance_from_start_index;
210210 *entry = {
211 true,
212 distance_from_start_index,
213211 key,
214212 value,
213 true,
214 distance_from_start_index,
215215 };
216216 return;
217217 }
src/ir.cpp+51-12
......@@ -1161,6 +1161,22 @@ static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_no
11611161 return special_instruction;
11621162}
11631163
1164template<typename T>
1165static T *ir_create_instruction_noval(IrBuilder *irb, Scope *scope, AstNode *source_node) {
1166 const char *name = nullptr;
1167#ifdef ZIG_ENABLE_MEM_PROFILE
1168 T *dummy = nullptr;
1169 name = ir_instruction_type_str(ir_instruction_id(dummy));
1170#endif
1171 T *special_instruction = allocate<T>(1, name);
1172 special_instruction->base.id = ir_instruction_id(special_instruction);
1173 special_instruction->base.scope = scope;
1174 special_instruction->base.source_node = source_node;
1175 special_instruction->base.debug_id = exec_next_debug_id(irb->exec);
1176 special_instruction->base.owner_bb = irb->current_basic_block;
1177 return special_instruction;
1178}
1179
11641180template<typename T>
11651181static T *ir_build_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) {
11661182 T *special_instruction = ir_create_instruction<T>(irb, scope, source_node);
......@@ -1214,16 +1230,22 @@ static IrInstruction *ir_build_return(IrBuilder *irb, Scope *scope, AstNode *sou
12141230}
12151231
12161232static IrInstruction *ir_build_const_void(IrBuilder *irb, Scope *scope, AstNode *source_node) {
1217 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
1218 const_instruction->base.value->type = irb->codegen->builtin_types.entry_void;
1219 const_instruction->base.value->special = ConstValSpecialStatic;
1233#ifdef ZIG_ENABLE_MEM_PROFILE
1234 memprof_intern_count.x_void += 1;
1235#endif
1236 IrInstructionConst *const_instruction = ir_create_instruction_noval<IrInstructionConst>(irb, scope, source_node);
1237 ir_instruction_append(irb->current_basic_block, &const_instruction->base);
1238 const_instruction->base.value = &irb->codegen->intern_values.x_void;
12201239 return &const_instruction->base;
12211240}
12221241
12231242static IrInstruction *ir_build_const_undefined(IrBuilder *irb, Scope *scope, AstNode *source_node) {
1224 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
1225 const_instruction->base.value->special = ConstValSpecialUndef;
1226 const_instruction->base.value->type = irb->codegen->builtin_types.entry_undef;
1243#ifdef ZIG_ENABLE_MEM_PROFILE
1244 memprof_intern_count.x_undefined += 1;
1245#endif
1246 IrInstructionConst *const_instruction = ir_create_instruction_noval<IrInstructionConst>(irb, scope, source_node);
1247 ir_instruction_append(irb->current_basic_block, &const_instruction->base);
1248 const_instruction->base.value = &irb->codegen->intern_values.x_undefined;
12271249 return &const_instruction->base;
12281250}
12291251
......@@ -1252,9 +1274,12 @@ static IrInstruction *ir_build_const_bigfloat(IrBuilder *irb, Scope *scope, AstN
12521274}
12531275
12541276static IrInstruction *ir_build_const_null(IrBuilder *irb, Scope *scope, AstNode *source_node) {
1255 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
1256 const_instruction->base.value->type = irb->codegen->builtin_types.entry_null;
1257 const_instruction->base.value->special = ConstValSpecialStatic;
1277#ifdef ZIG_ENABLE_MEM_PROFILE
1278 memprof_intern_count.x_null += 1;
1279#endif
1280 IrInstructionConst *const_instruction = ir_create_instruction_noval<IrInstructionConst>(irb, scope, source_node);
1281 ir_instruction_append(irb->current_basic_block, &const_instruction->base);
1282 const_instruction->base.value = &irb->codegen->intern_values.x_null;
12581283 return &const_instruction->base;
12591284}
12601285
......@@ -11113,6 +11138,12 @@ static IrInstruction *ir_const(IrAnalyze *ira, IrInstruction *old_instruction, Z
1111311138 return new_instruction;
1111411139}
1111511140
11141static IrInstruction *ir_const_noval(IrAnalyze *ira, IrInstruction *old_instruction) {
11142 IrInstructionConst *const_instruction = ir_create_instruction_noval<IrInstructionConst>(&ira->new_irb,
11143 old_instruction->scope, old_instruction->source_node);
11144 return &const_instruction->base;
11145}
11146
1111611147static IrInstruction *ir_resolve_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,
1111711148 ZigType *wanted_type, CastOp cast_op)
1111811149{
......@@ -11429,13 +11460,21 @@ static IrInstruction *ir_const_undef(IrAnalyze *ira, IrInstruction *source_instr
1142911460}
1143011461
1143111462static IrInstruction *ir_const_unreachable(IrAnalyze *ira, IrInstruction *source_instruction) {
11432 IrInstruction *result = ir_const(ira, source_instruction, ira->codegen->builtin_types.entry_unreachable);
11433 result->value->special = ConstValSpecialStatic;
11463#ifdef ZIG_ENABLE_MEM_PROFILE
11464 memprof_intern_count.x_unreachable += 1;
11465#endif
11466 IrInstruction *result = ir_const_noval(ira, source_instruction);
11467 result->value = &ira->codegen->intern_values.x_unreachable;
1143411468 return result;
1143511469}
1143611470
1143711471static IrInstruction *ir_const_void(IrAnalyze *ira, IrInstruction *source_instruction) {
11438 return ir_const(ira, source_instruction, ira->codegen->builtin_types.entry_void);
11472#ifdef ZIG_ENABLE_MEM_PROFILE
11473 memprof_intern_count.x_void += 1;
11474#endif
11475 IrInstruction *result = ir_const_noval(ira, source_instruction);
11476 result->value = &ira->codegen->intern_values.x_void;
11477 return result;
1143911478}
1144011479
1144111480static IrInstruction *ir_const_unsigned(IrAnalyze *ira, IrInstruction *source_instruction, uint64_t value) {
src/memory_profiling.cpp+8-1
......@@ -6,6 +6,8 @@
66
77#ifdef ZIG_ENABLE_MEM_PROFILE
88
9MemprofInternCount memprof_intern_count;
10
911static bool str_eql_str(const char *a, const char *b) {
1012 return strcmp(a, b) == 0;
1113}
......@@ -29,7 +31,6 @@ ZigList<const char *> unknown_names = {};
2931HashMap<const char *, CountAndSize, str_hash, str_eql_str> usage_table = {};
3032bool table_active = false;
3133
32
3334static const char *get_default_name(const char *name_or_null, size_t type_size) {
3435 if (name_or_null != nullptr) return name_or_null;
3536 if (type_size >= unknown_names.length) {
......@@ -134,6 +135,12 @@ void memprof_dump_stats(FILE *file) {
134135
135136 list.deinit();
136137 table_active = true;
138
139 fprintf(stderr, "\n");
140 fprintf(stderr, "undefined: interned %zu times\n", memprof_intern_count.x_undefined);
141 fprintf(stderr, "void: interned %zu times\n", memprof_intern_count.x_void);
142 fprintf(stderr, "null: interned %zu times\n", memprof_intern_count.x_null);
143 fprintf(stderr, "unreachable: interned %zu times\n", memprof_intern_count.x_unreachable);
137144}
138145
139146#endif
src/memory_profiling.hpp+8
......@@ -13,6 +13,14 @@
1313#include <stddef.h>
1414#include <stdio.h>
1515
16struct MemprofInternCount {
17 size_t x_undefined;
18 size_t x_void;
19 size_t x_null;
20 size_t x_unreachable;
21};
22extern MemprofInternCount memprof_intern_count;
23
1624void memprof_init(void);
1725
1826void memprof_alloc(const char *name, size_t item_count, size_t type_size);
src/util.cpp+4-4
......@@ -121,18 +121,18 @@ SplitIterator memSplit(Slice<uint8_t> buffer, Slice<uint8_t> split_bytes) {
121121
122122void zig_pretty_print_bytes(FILE *f, double n) {
123123 if (n > 1024.0 * 1024.0 * 1024.0) {
124 fprintf(f, "%.02f GiB", n / 1024.0 / 1024.0 / 1024.0);
124 fprintf(f, "%.03f GiB", n / 1024.0 / 1024.0 / 1024.0);
125125 return;
126126 }
127127 if (n > 1024.0 * 1024.0) {
128 fprintf(f, "%.02f MiB", n / 1024.0 / 1024.0);
128 fprintf(f, "%.03f MiB", n / 1024.0 / 1024.0);
129129 return;
130130 }
131131 if (n > 1024.0) {
132 fprintf(f, "%.02f KiB", n / 1024.0);
132 fprintf(f, "%.03f KiB", n / 1024.0);
133133 return;
134134 }
135 fprintf(f, "%.02f bytes", n );
135 fprintf(f, "%.03f bytes", n );
136136 return;
137137}
138138