authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-18 13:13:35-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-18 13:14:26-07:00
log5cbb642525fa92e16dd3c9e978a9f8a34734de4c
tree7c479d76abcebda8ff536871095cffd6c9474e57
parent17c066e9257d539cc1779eaaa9ad2679ec6b8881

stage1: small mem usage improvement for IR

move a boolean field to be represented implicitly with the enum tag. Just borrowing one of the many strategies of stage2. This simple change took the peak mem usage from std lib tests on my machine from 8.21 GiB to 8.11 GiB.

3 files changed, 40 insertions(+), 20 deletions(-)

src/stage1/all_types.hpp+2-2
...@@ -2629,7 +2629,8 @@ enum IrInstSrcId {...@@ -2629,7 +2629,8 @@ enum IrInstSrcId {
2629 IrInstSrcIdResolveResult,2629 IrInstSrcIdResolveResult,
2630 IrInstSrcIdResetResult,2630 IrInstSrcIdResetResult,
2631 IrInstSrcIdSetAlignStack,2631 IrInstSrcIdSetAlignStack,
2632 IrInstSrcIdArgType,2632 IrInstSrcIdArgTypeAllowVarFalse,
2633 IrInstSrcIdArgTypeAllowVarTrue,
2633 IrInstSrcIdExport,2634 IrInstSrcIdExport,
2634 IrInstSrcIdExtern,2635 IrInstSrcIdExtern,
2635 IrInstSrcIdErrorReturnTrace,2636 IrInstSrcIdErrorReturnTrace,
...@@ -4144,7 +4145,6 @@ struct IrInstSrcArgType {...@@ -4144,7 +4145,6 @@ struct IrInstSrcArgType {
41444145
4145 IrInstSrc *fn_type;4146 IrInstSrc *fn_type;
4146 IrInstSrc *arg_index;4147 IrInstSrc *arg_index;
4147 bool allow_var;
4148};4148};
41494149
4150struct IrInstSrcExport {4150struct IrInstSrcExport {
src/stage1/ir.cpp+22-13
...@@ -514,7 +514,8 @@ static void destroy_instruction_src(IrInstSrc *inst) {...@@ -514,7 +514,8 @@ static void destroy_instruction_src(IrInstSrc *inst) {
514 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcResetResult *>(inst));514 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcResetResult *>(inst));
515 case IrInstSrcIdSetAlignStack:515 case IrInstSrcIdSetAlignStack:
516 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSetAlignStack *>(inst));516 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSetAlignStack *>(inst));
517 case IrInstSrcIdArgType:517 case IrInstSrcIdArgTypeAllowVarFalse:
518 case IrInstSrcIdArgTypeAllowVarTrue:
518 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcArgType *>(inst));519 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcArgType *>(inst));
519 case IrInstSrcIdExport:520 case IrInstSrcIdExport:
520 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcExport *>(inst));521 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcExport *>(inst));
...@@ -1546,10 +1547,6 @@ static constexpr IrInstSrcId ir_inst_id(IrInstSrcSetAlignStack *) {...@@ -1546,10 +1547,6 @@ static constexpr IrInstSrcId ir_inst_id(IrInstSrcSetAlignStack *) {
1546 return IrInstSrcIdSetAlignStack;1547 return IrInstSrcIdSetAlignStack;
1547}1548}
15481549
1549static constexpr IrInstSrcId ir_inst_id(IrInstSrcArgType *) {
1550 return IrInstSrcIdArgType;
1551}
1552
1553static constexpr IrInstSrcId ir_inst_id(IrInstSrcExport *) {1550static constexpr IrInstSrcId ir_inst_id(IrInstSrcExport *) {
1554 return IrInstSrcIdExport;1551 return IrInstSrcIdExport;
1555}1552}
...@@ -4590,10 +4587,17 @@ static IrInstSrc *ir_build_set_align_stack(IrBuilderSrc *irb, Scope *scope, AstN...@@ -4590,10 +4587,17 @@ static IrInstSrc *ir_build_set_align_stack(IrBuilderSrc *irb, Scope *scope, AstN
4590static IrInstSrc *ir_build_arg_type(IrBuilderSrc *irb, Scope *scope, AstNode *source_node,4587static IrInstSrc *ir_build_arg_type(IrBuilderSrc *irb, Scope *scope, AstNode *source_node,
4591 IrInstSrc *fn_type, IrInstSrc *arg_index, bool allow_var)4588 IrInstSrc *fn_type, IrInstSrc *arg_index, bool allow_var)
4592{4589{
4593 IrInstSrcArgType *instruction = ir_build_instruction<IrInstSrcArgType>(irb, scope, source_node);4590 IrInstSrcArgType *instruction = heap::c_allocator.create<IrInstSrcArgType>();
4591 instruction->base.id = allow_var ?
4592 IrInstSrcIdArgTypeAllowVarTrue : IrInstSrcIdArgTypeAllowVarFalse;
4593 instruction->base.base.scope = scope;
4594 instruction->base.base.source_node = source_node;
4595 instruction->base.base.debug_id = exec_next_debug_id(irb->exec);
4596 instruction->base.owner_bb = irb->current_basic_block;
4597 ir_instruction_append(irb->current_basic_block, &instruction->base);
4598
4594 instruction->fn_type = fn_type;4599 instruction->fn_type = fn_type;
4595 instruction->arg_index = arg_index;4600 instruction->arg_index = arg_index;
4596 instruction->allow_var = allow_var;
45974601
4598 ir_ref_instruction(fn_type, irb->current_basic_block);4602 ir_ref_instruction(fn_type, irb->current_basic_block);
4599 ir_ref_instruction(arg_index, irb->current_basic_block);4603 ir_ref_instruction(arg_index, irb->current_basic_block);
...@@ -30976,7 +30980,9 @@ static IrInstGen *ir_analyze_instruction_set_align_stack(IrAnalyze *ira, IrInstS...@@ -30976,7 +30980,9 @@ static IrInstGen *ir_analyze_instruction_set_align_stack(IrAnalyze *ira, IrInstS
30976 return ir_const_void(ira, &instruction->base.base);30980 return ir_const_void(ira, &instruction->base.base);
30977}30981}
3097830982
30979static IrInstGen *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstSrcArgType *instruction) {30983static IrInstGen *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstSrcArgType *instruction,
30984 bool allow_var)
30985{
30980 IrInstGen *fn_type_inst = instruction->fn_type->child;30986 IrInstGen *fn_type_inst = instruction->fn_type->child;
30981 ZigType *fn_type = ir_resolve_type(ira, fn_type_inst);30987 ZigType *fn_type = ir_resolve_type(ira, fn_type_inst);
30982 if (type_is_invalid(fn_type))30988 if (type_is_invalid(fn_type))
...@@ -30998,7 +31004,7 @@ static IrInstGen *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstSrcArgTy...@@ -30998,7 +31004,7 @@ static IrInstGen *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstSrcArgTy
3099831004
30999 FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;31005 FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;
31000 if (arg_index >= fn_type_id->param_count) {31006 if (arg_index >= fn_type_id->param_count) {
31001 if (instruction->allow_var) {31007 if (allow_var) {
31002 // TODO remove this with var args31008 // TODO remove this with var args
31003 return ir_const_type(ira, &instruction->base.base, ira->codegen->builtin_types.entry_anytype);31009 return ir_const_type(ira, &instruction->base.base, ira->codegen->builtin_types.entry_anytype);
31004 }31010 }
...@@ -31013,7 +31019,7 @@ static IrInstGen *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstSrcArgTy...@@ -31013,7 +31019,7 @@ static IrInstGen *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstSrcArgTy
31013 // Args are only unresolved if our function is generic.31019 // Args are only unresolved if our function is generic.
31014 ir_assert(fn_type->data.fn.is_generic, &instruction->base.base);31020 ir_assert(fn_type->data.fn.is_generic, &instruction->base.base);
3101531021
31016 if (instruction->allow_var) {31022 if (allow_var) {
31017 return ir_const_type(ira, &instruction->base.base, ira->codegen->builtin_types.entry_anytype);31023 return ir_const_type(ira, &instruction->base.base, ira->codegen->builtin_types.entry_anytype);
31018 } else {31024 } else {
31019 ir_add_error(ira, &arg_index_inst->base,31025 ir_add_error(ira, &arg_index_inst->base,
...@@ -32383,8 +32389,10 @@ static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruc...@@ -32383,8 +32389,10 @@ static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruc
32383 return ir_analyze_instruction_reset_result(ira, (IrInstSrcResetResult *)instruction);32389 return ir_analyze_instruction_reset_result(ira, (IrInstSrcResetResult *)instruction);
32384 case IrInstSrcIdSetAlignStack:32390 case IrInstSrcIdSetAlignStack:
32385 return ir_analyze_instruction_set_align_stack(ira, (IrInstSrcSetAlignStack *)instruction);32391 return ir_analyze_instruction_set_align_stack(ira, (IrInstSrcSetAlignStack *)instruction);
32386 case IrInstSrcIdArgType:32392 case IrInstSrcIdArgTypeAllowVarFalse:
32387 return ir_analyze_instruction_arg_type(ira, (IrInstSrcArgType *)instruction);32393 return ir_analyze_instruction_arg_type(ira, (IrInstSrcArgType *)instruction, false);
32394 case IrInstSrcIdArgTypeAllowVarTrue:
32395 return ir_analyze_instruction_arg_type(ira, (IrInstSrcArgType *)instruction, true);
32388 case IrInstSrcIdExport:32396 case IrInstSrcIdExport:
32389 return ir_analyze_instruction_export(ira, (IrInstSrcExport *)instruction);32397 return ir_analyze_instruction_export(ira, (IrInstSrcExport *)instruction);
32390 case IrInstSrcIdExtern:32398 case IrInstSrcIdExtern:
...@@ -32826,7 +32834,8 @@ bool ir_inst_src_has_side_effects(IrInstSrc *instruction) {...@@ -32826,7 +32834,8 @@ bool ir_inst_src_has_side_effects(IrInstSrc *instruction) {
32826 case IrInstSrcIdAlignCast:32834 case IrInstSrcIdAlignCast:
32827 case IrInstSrcIdImplicitCast:32835 case IrInstSrcIdImplicitCast:
32828 case IrInstSrcIdResolveResult:32836 case IrInstSrcIdResolveResult:
32829 case IrInstSrcIdArgType:32837 case IrInstSrcIdArgTypeAllowVarFalse:
32838 case IrInstSrcIdArgTypeAllowVarTrue:
32830 case IrInstSrcIdErrorReturnTrace:32839 case IrInstSrcIdErrorReturnTrace:
32831 case IrInstSrcIdErrorUnion:32840 case IrInstSrcIdErrorUnion:
32832 case IrInstSrcIdFloatOp:32841 case IrInstSrcIdFloatOp:
src/stage1/ir_print.cpp+16-5
...@@ -308,8 +308,10 @@ const char* ir_inst_src_type_str(IrInstSrcId id) {...@@ -308,8 +308,10 @@ const char* ir_inst_src_type_str(IrInstSrcId id) {
308 return "SrcResetResult";308 return "SrcResetResult";
309 case IrInstSrcIdSetAlignStack:309 case IrInstSrcIdSetAlignStack:
310 return "SrcSetAlignStack";310 return "SrcSetAlignStack";
311 case IrInstSrcIdArgType:311 case IrInstSrcIdArgTypeAllowVarFalse:
312 return "SrcArgType";312 return "SrcArgTypeAllowVarFalse";
313 case IrInstSrcIdArgTypeAllowVarTrue:
314 return "SrcArgTypeAllowVarTrue";
313 case IrInstSrcIdExport:315 case IrInstSrcIdExport:
314 return "SrcExport";316 return "SrcExport";
315 case IrInstSrcIdExtern:317 case IrInstSrcIdExtern:
...@@ -2344,11 +2346,17 @@ static void ir_print_set_align_stack(IrPrintSrc *irp, IrInstSrcSetAlignStack *in...@@ -2344,11 +2346,17 @@ static void ir_print_set_align_stack(IrPrintSrc *irp, IrInstSrcSetAlignStack *in
2344 fprintf(irp->f, ")");2346 fprintf(irp->f, ")");
2345}2347}
23462348
2347static void ir_print_arg_type(IrPrintSrc *irp, IrInstSrcArgType *instruction) {2349static void ir_print_arg_type(IrPrintSrc *irp, IrInstSrcArgType *instruction, bool allow_var) {
2348 fprintf(irp->f, "@ArgType(");2350 fprintf(irp->f, "@ArgType(");
2349 ir_print_other_inst_src(irp, instruction->fn_type);2351 ir_print_other_inst_src(irp, instruction->fn_type);
2350 fprintf(irp->f, ",");2352 fprintf(irp->f, ",");
2351 ir_print_other_inst_src(irp, instruction->arg_index);2353 ir_print_other_inst_src(irp, instruction->arg_index);
2354 fprintf(irp->f, ",");
2355 if (allow_var) {
2356 fprintf(irp->f, "allow_var=true");
2357 } else {
2358 fprintf(irp->f, "allow_var=false");
2359 }
2352 fprintf(irp->f, ")");2360 fprintf(irp->f, ")");
2353}2361}
23542362
...@@ -2942,8 +2950,11 @@ static void ir_print_inst_src(IrPrintSrc *irp, IrInstSrc *instruction, bool trai...@@ -2942,8 +2950,11 @@ static void ir_print_inst_src(IrPrintSrc *irp, IrInstSrc *instruction, bool trai
2942 case IrInstSrcIdSetAlignStack:2950 case IrInstSrcIdSetAlignStack:
2943 ir_print_set_align_stack(irp, (IrInstSrcSetAlignStack *)instruction);2951 ir_print_set_align_stack(irp, (IrInstSrcSetAlignStack *)instruction);
2944 break;2952 break;
2945 case IrInstSrcIdArgType:2953 case IrInstSrcIdArgTypeAllowVarFalse:
2946 ir_print_arg_type(irp, (IrInstSrcArgType *)instruction);2954 ir_print_arg_type(irp, (IrInstSrcArgType *)instruction, false);
2955 break;
2956 case IrInstSrcIdArgTypeAllowVarTrue:
2957 ir_print_arg_type(irp, (IrInstSrcArgType *)instruction, true);
2947 break;2958 break;
2948 case IrInstSrcIdExport:2959 case IrInstSrcIdExport:
2949 ir_print_export(irp, (IrInstSrcExport *)instruction);2960 ir_print_export(irp, (IrInstSrcExport *)instruction);