authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-03 21:40:56-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-03 21:40:56-04:00
log143d6ada8f93872b94a8f20927e11cae243028d9
tree779fd513b42ed24ecd9d37ba3122e78ad8ca0df8
parentd4054e35fe3a6ba4b4f0d1a0da0e0149f21b49a4
signaturelock-open Commit is signed but in an unrecognized format.

no-copy semantics for for loops

Note that only the index variable requires a stack allocation, and the memcpy for the element is gone. ```zig export fn entry() void { var buf: [10]i32 = undefined; for (buf) |x| {} } ``` ```llvm define void @entry() #2 !dbg !35 { Entry: %buf = alloca [10 x i32], align 4 %i = alloca i64, align 8 %0 = bitcast [10 x i32]* %buf to i8*, !dbg !47 call void @llvm.memset.p0i8.i64(i8* align 4 %0, i8 -86, i64 40, i1 false), !dbg !47 call void @llvm.dbg.declare(metadata [10 x i32]* %buf, metadata !39, metadata !DIExpression()), !dbg !47 store i64 0, i64* %i, align 8, !dbg !48 call void @llvm.dbg.declare(metadata i64* %i, metadata !45, metadata !DIExpression()), !dbg !48 br label %ForCond, !dbg !48 ForCond: ; preds = %ForBody, %Entry %1 = load i64, i64* %i, align 8, !dbg !48 %2 = icmp ult i64 %1, 10, !dbg !48 br i1 %2, label %ForBody, label %ForEnd, !dbg !48 ForBody: ; preds = %ForCond %3 = getelementptr inbounds [10 x i32], [10 x i32]* %buf, i64 0, i64 %1, !dbg !48 call void @llvm.dbg.declare(metadata i32* %3, metadata !46, metadata !DIExpression()), !dbg !49 %4 = add nuw i64 %1, 1, !dbg !48 store i64 %4, i64* %i, align 8, !dbg !48 br label %ForCond, !dbg !48 ForEnd: ; preds = %ForCond ret void, !dbg !50 } ```

5 files changed, 44 insertions(+), 178 deletions(-)

BRANCH_TODO-1
......@@ -3,7 +3,6 @@ Scratch pad for stuff to do before merging master
33
44migrate ir_build_var_decl_src to use ir_build_alloca_src and explicitly initialize
55
6 * for loops
76 * switch expression
87 * struct initializations
98 * function call parameters
src/all_types.hpp-14
......@@ -2191,8 +2191,6 @@ enum IrInstructionId {
21912191 IrInstructionIdUnionInit,
21922192 IrInstructionIdUnreachable,
21932193 IrInstructionIdTypeOf,
2194 IrInstructionIdToPtrType,
2195 IrInstructionIdPtrTypeChild,
21962194 IrInstructionIdSetCold,
21972195 IrInstructionIdSetRuntimeSafety,
21982196 IrInstructionIdSetFloatMode,
......@@ -2679,18 +2677,6 @@ struct IrInstructionTypeOf {
26792677 IrInstruction *value;
26802678};
26812679
2682struct IrInstructionToPtrType {
2683 IrInstruction base;
2684
2685 IrInstruction *ptr;
2686};
2687
2688struct IrInstructionPtrTypeChild {
2689 IrInstruction base;
2690
2691 IrInstruction *value;
2692};
2693
26942680struct IrInstructionSetCold {
26952681 IrInstruction base;
26962682
src/codegen.cpp-2
......@@ -5539,8 +5539,6 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
55395539 case IrInstructionIdInvalid:
55405540 case IrInstructionIdConst:
55415541 case IrInstructionIdTypeOf:
5542 case IrInstructionIdToPtrType:
5543 case IrInstructionIdPtrTypeChild:
55445542 case IrInstructionIdFieldPtr:
55455543 case IrInstructionIdSetCold:
55465544 case IrInstructionIdSetRuntimeSafety:
src/ir.cpp+44-143
......@@ -523,14 +523,6 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionTypeOf *) {
523523 return IrInstructionIdTypeOf;
524524}
525525
526static constexpr IrInstructionId ir_instruction_id(IrInstructionToPtrType *) {
527 return IrInstructionIdToPtrType;
528}
529
530static constexpr IrInstructionId ir_instruction_id(IrInstructionPtrTypeChild *) {
531 return IrInstructionIdPtrTypeChild;
532}
533
534526static constexpr IrInstructionId ir_instruction_id(IrInstructionSetCold *) {
535527 return IrInstructionIdSetCold;
536528}
......@@ -1657,27 +1649,6 @@ static IrInstruction *ir_build_typeof(IrBuilder *irb, Scope *scope, AstNode *sou
16571649 return &instruction->base;
16581650}
16591651
1660static IrInstruction *ir_build_to_ptr_type(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *ptr) {
1661 IrInstructionToPtrType *instruction = ir_build_instruction<IrInstructionToPtrType>(irb, scope, source_node);
1662 instruction->ptr = ptr;
1663
1664 ir_ref_instruction(ptr, irb->current_basic_block);
1665
1666 return &instruction->base;
1667}
1668
1669static IrInstruction *ir_build_ptr_type_child(IrBuilder *irb, Scope *scope, AstNode *source_node,
1670 IrInstruction *value)
1671{
1672 IrInstructionPtrTypeChild *instruction = ir_build_instruction<IrInstructionPtrTypeChild>(
1673 irb, scope, source_node);
1674 instruction->value = value;
1675
1676 ir_ref_instruction(value, irb->current_basic_block);
1677
1678 return &instruction->base;
1679}
1680
16811652static IrInstruction *ir_build_set_cold(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *is_cold) {
16821653 IrInstructionSetCold *instruction = ir_build_instruction<IrInstructionSetCold>(irb, scope, source_node);
16831654 instruction->is_cold = is_cold;
......@@ -5611,6 +5582,14 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
56115582 }
56125583}
56135584
5585static ResultLocVar *create_var_result_loc(IrInstruction *alloca, ZigVar *var) {
5586 ResultLocVar *result_loc_var = allocate<ResultLocVar>(1);
5587 result_loc_var->base.id = ResultLocIdVar;
5588 result_loc_var->base.source_instruction = alloca;
5589 result_loc_var->var = var;
5590 return result_loc_var;
5591}
5592
56145593static IrInstruction *ir_gen_var_decl(IrBuilder *irb, Scope *scope, AstNode *node) {
56155594 assert(node->type == NodeTypeVariableDeclaration);
56165595
......@@ -5669,10 +5648,7 @@ static IrInstruction *ir_gen_var_decl(IrBuilder *irb, Scope *scope, AstNode *nod
56695648 buf_ptr(variable_declaration->symbol), is_comptime);
56705649
56715650 // Create a result location for the initialization expression.
5672 ResultLocVar *result_loc_var = allocate<ResultLocVar>(1);
5673 result_loc_var->base.id = ResultLocIdVar;
5674 result_loc_var->base.source_instruction = alloca;
5675 result_loc_var->var = var;
5651 ResultLocVar *result_loc_var = create_var_result_loc(alloca, var);
56765652 ResultLoc *init_result_loc = (type_instruction == nullptr) ? &result_loc_var->base : nullptr;
56775653
56785654 // Temporarily set the name of the IrExecutable to the VariableDeclaration
......@@ -5975,73 +5951,62 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
59755951 if (array_val_ptr == irb->codegen->invalid_instruction)
59765952 return array_val_ptr;
59775953
5978 IrInstruction *pointer_type = ir_build_to_ptr_type(irb, parent_scope, array_node, array_val_ptr);
5979
5980 IrInstruction *elem_var_type;
5981 if (node->data.for_expr.elem_is_ptr) {
5982 elem_var_type = pointer_type;
5983 } else {
5984 elem_var_type = ir_build_ptr_type_child(irb, parent_scope, elem_node, pointer_type);
5985 }
5986
59875954 IrInstruction *is_comptime = ir_build_const_bool(irb, parent_scope, node,
59885955 ir_should_inline(irb->exec, parent_scope) || node->data.for_expr.is_inline);
59895956
5990 // TODO make it an error to write to element variable or i variable.
5991 Buf *elem_var_name = elem_node->data.symbol_expr.symbol;
5992 ZigVar *elem_var = ir_create_var(irb, elem_node, parent_scope, elem_var_name, true, false, false, is_comptime);
5993 Scope *child_scope = elem_var->child_scope;
5994
5995 IrInstruction *undef = ir_build_const_undefined(irb, parent_scope, elem_node);
5996 IrInstruction *undef_elem_var_type = ir_build_implicit_cast(irb, parent_scope, elem_node, elem_var_type, undef);
5997 ir_build_var_decl_src(irb, child_scope, elem_node, elem_var, nullptr, undef_elem_var_type);
5998 IrInstruction *elem_var_ptr = ir_build_var_ptr(irb, child_scope, node, elem_var);
5999
60005957 AstNode *index_var_source_node;
60015958 ZigVar *index_var;
5959 const char *index_var_name;
60025960 if (index_node) {
60035961 index_var_source_node = index_node;
6004 Buf *index_var_name = index_node->data.symbol_expr.symbol;
6005 index_var = ir_create_var(irb, index_node, child_scope, index_var_name, true, false, false, is_comptime);
5962 Buf *index_var_name_buf = index_node->data.symbol_expr.symbol;
5963 index_var = ir_create_var(irb, index_node, parent_scope, index_var_name_buf, true, false, false, is_comptime);
5964 index_var_name = buf_ptr(index_var_name_buf);
60065965 } else {
60075966 index_var_source_node = node;
6008 index_var = ir_create_var(irb, node, child_scope, nullptr, true, false, true, is_comptime);
5967 index_var = ir_create_var(irb, node, parent_scope, nullptr, true, false, true, is_comptime);
5968 index_var_name = "i";
60095969 }
6010 child_scope = index_var->child_scope;
5970 parent_scope = index_var->parent_scope;
60115971
6012 IrInstruction *zero = ir_build_const_usize(irb, child_scope, node, 0);
6013 IrInstruction *one = ir_build_const_usize(irb, child_scope, node, 1);
6014 ir_build_var_decl_src(irb, child_scope, index_var_source_node, index_var, nullptr, zero);
6015 IrInstruction *index_ptr = ir_build_var_ptr(irb, child_scope, node, index_var);
5972 IrInstruction *index_alloca = ir_build_alloca_src(irb, parent_scope, node, nullptr, index_var_name, is_comptime);
5973 ResultLocVar *var_result_loc = create_var_result_loc(index_alloca, index_var);
5974 IrInstruction *zero = ir_build_const_usize(irb, parent_scope, node, 0);
5975 ir_build_end_expr(irb, parent_scope, node, zero, &var_result_loc->base);
5976 ir_build_var_decl_src(irb, parent_scope, index_var_source_node, index_var, nullptr, index_alloca);
60165977
5978 IrInstruction *one = ir_build_const_usize(irb, parent_scope, node, 1);
5979 IrInstruction *index_ptr = ir_build_var_ptr(irb, parent_scope, node, index_var);
60175980
6018 IrBasicBlock *cond_block = ir_create_basic_block(irb, child_scope, "ForCond");
6019 IrBasicBlock *body_block = ir_create_basic_block(irb, child_scope, "ForBody");
6020 IrBasicBlock *end_block = ir_create_basic_block(irb, child_scope, "ForEnd");
6021 IrBasicBlock *else_block = else_node ? ir_create_basic_block(irb, child_scope, "ForElse") : end_block;
6022 IrBasicBlock *continue_block = ir_create_basic_block(irb, child_scope, "ForContinue");
5981
5982 IrBasicBlock *cond_block = ir_create_basic_block(irb, parent_scope, "ForCond");
5983 IrBasicBlock *body_block = ir_create_basic_block(irb, parent_scope, "ForBody");
5984 IrBasicBlock *end_block = ir_create_basic_block(irb, parent_scope, "ForEnd");
5985 IrBasicBlock *else_block = else_node ? ir_create_basic_block(irb, parent_scope, "ForElse") : end_block;
5986 IrBasicBlock *continue_block = ir_create_basic_block(irb, parent_scope, "ForContinue");
60235987
60245988 Buf *len_field_name = buf_create_from_str("len");
6025 IrInstruction *len_ref = ir_build_field_ptr(irb, child_scope, node, array_val_ptr, len_field_name);
6026 IrInstruction *len_val = ir_build_load_ptr(irb, child_scope, node, len_ref);
6027 ir_build_br(irb, child_scope, node, cond_block, is_comptime);
5989 IrInstruction *len_ref = ir_build_field_ptr(irb, parent_scope, node, array_val_ptr, len_field_name);
5990 IrInstruction *len_val = ir_build_load_ptr(irb, parent_scope, node, len_ref);
5991 ir_build_br(irb, parent_scope, node, cond_block, is_comptime);
60285992
60295993 ir_set_cursor_at_end_and_append_block(irb, cond_block);
6030 IrInstruction *index_val = ir_build_load_ptr(irb, child_scope, node, index_ptr);
6031 IrInstruction *cond = ir_build_bin_op(irb, child_scope, node, IrBinOpCmpLessThan, index_val, len_val, false);
5994 IrInstruction *index_val = ir_build_load_ptr(irb, parent_scope, node, index_ptr);
5995 IrInstruction *cond = ir_build_bin_op(irb, parent_scope, node, IrBinOpCmpLessThan, index_val, len_val, false);
60325996 IrBasicBlock *after_cond_block = irb->current_basic_block;
60335997 IrInstruction *void_else_value = else_node ? nullptr : ir_mark_gen(ir_build_const_void(irb, parent_scope, node));
6034 ir_mark_gen(ir_build_cond_br(irb, child_scope, node, cond, body_block, else_block, is_comptime));
5998 ir_mark_gen(ir_build_cond_br(irb, parent_scope, node, cond, body_block, else_block, is_comptime));
60355999
60366000 ir_set_cursor_at_end_and_append_block(irb, body_block);
6037 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, child_scope, node, array_val_ptr, index_val, false, PtrLenSingle);
6038 IrInstruction *elem_val;
6039 if (node->data.for_expr.elem_is_ptr) {
6040 elem_val = elem_ptr;
6041 } else {
6042 elem_val = ir_build_load_ptr(irb, child_scope, node, elem_ptr);
6043 }
6044 ir_mark_gen(ir_build_store_ptr(irb, child_scope, node, elem_var_ptr, elem_val));
6001 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, parent_scope, node, array_val_ptr, index_val, false, PtrLenSingle);
6002 // TODO make it an error to write to element variable or i variable.
6003 Buf *elem_var_name = elem_node->data.symbol_expr.symbol;
6004 ZigVar *elem_var = ir_create_var(irb, elem_node, parent_scope, elem_var_name, true, false, false, is_comptime);
6005 Scope *child_scope = elem_var->child_scope;
6006
6007 IrInstruction *var_ptr = node->data.for_expr.elem_is_ptr ?
6008 ir_build_ref(irb, parent_scope, elem_node, elem_ptr, true, false) : elem_ptr;
6009 ir_build_var_decl_src(irb, parent_scope, elem_node, elem_var, nullptr, var_ptr);
60456010
60466011 ZigList<IrInstruction *> incoming_values = {0};
60476012 ZigList<IrBasicBlock *> incoming_blocks = {0};
......@@ -16901,64 +16866,6 @@ static IrInstruction *ir_analyze_instruction_typeof(IrAnalyze *ira, IrInstructio
1690116866 return ir_const_type(ira, &typeof_instruction->base, type_entry);
1690216867}
1690316868
16904static IrInstruction *ir_analyze_instruction_to_ptr_type(IrAnalyze *ira,
16905 IrInstructionToPtrType *to_ptr_type_instruction)
16906{
16907 Error err;
16908 IrInstruction *ptr_ptr = to_ptr_type_instruction->ptr->child;
16909 if (type_is_invalid(ptr_ptr->value.type))
16910 return ira->codegen->invalid_instruction;
16911
16912 ZigType *ptr_ptr_type = ptr_ptr->value.type;
16913 assert(ptr_ptr_type->id == ZigTypeIdPointer);
16914 ZigType *type_entry = ptr_ptr_type->data.pointer.child_type;
16915
16916 ZigType *ptr_type;
16917 if (type_entry->id == ZigTypeIdArray) {
16918 ptr_type = get_pointer_to_type(ira->codegen, type_entry->data.array.child_type, ptr_ptr_type->data.pointer.is_const);
16919 } else if (is_array_ref(type_entry)) {
16920 ptr_type = get_pointer_to_type(ira->codegen,
16921 type_entry->data.pointer.child_type->data.array.child_type, type_entry->data.pointer.is_const);
16922 } else if (is_slice(type_entry)) {
16923 ZigType *slice_ptr_type = type_entry->data.structure.fields[0].type_entry;
16924 ptr_type = adjust_ptr_len(ira->codegen, slice_ptr_type, PtrLenSingle);
16925 // If the pointer is over-aligned, we may have to reduce it based on the alignment of the element type.
16926 if (slice_ptr_type->data.pointer.explicit_alignment != 0) {
16927 ZigType *elem_type = slice_ptr_type->data.pointer.child_type;
16928 if ((err = type_resolve(ira->codegen, elem_type, ResolveStatusAlignmentKnown)))
16929 return ira->codegen->invalid_instruction;
16930 uint32_t elem_align = get_abi_alignment(ira->codegen, elem_type);
16931 uint32_t reduced_align = min(elem_align, slice_ptr_type->data.pointer.explicit_alignment);
16932 ptr_type = adjust_ptr_align(ira->codegen, ptr_type, reduced_align);
16933 }
16934 } else if (type_entry->id == ZigTypeIdArgTuple) {
16935 zig_panic("TODO for loop on var args");
16936 } else {
16937 ir_add_error_node(ira, to_ptr_type_instruction->base.source_node,
16938 buf_sprintf("expected array type, found '%s'", buf_ptr(&type_entry->name)));
16939 return ira->codegen->invalid_instruction;
16940 }
16941
16942 return ir_const_type(ira, &to_ptr_type_instruction->base, ptr_type);
16943}
16944
16945static IrInstruction *ir_analyze_instruction_ptr_type_child(IrAnalyze *ira,
16946 IrInstructionPtrTypeChild *ptr_type_child_instruction)
16947{
16948 IrInstruction *type_value = ptr_type_child_instruction->value->child;
16949 ZigType *type_entry = ir_resolve_type(ira, type_value);
16950 if (type_is_invalid(type_entry))
16951 return ira->codegen->invalid_instruction;
16952
16953 if (type_entry->id != ZigTypeIdPointer) {
16954 ir_add_error_node(ira, ptr_type_child_instruction->base.source_node,
16955 buf_sprintf("expected pointer type, found '%s'", buf_ptr(&type_entry->name)));
16956 return ira->codegen->invalid_instruction;
16957 }
16958
16959 return ir_const_type(ira, &ptr_type_child_instruction->base, type_entry->data.pointer.child_type);
16960}
16961
1696216869static IrInstruction *ir_analyze_instruction_set_cold(IrAnalyze *ira, IrInstructionSetCold *instruction) {
1696316870 if (ira->new_irb.exec->is_inline) {
1696416871 // ignore setCold when running functions at compile time
......@@ -23751,10 +23658,6 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction
2375123658 return ir_analyze_instruction_phi(ira, (IrInstructionPhi *)instruction);
2375223659 case IrInstructionIdTypeOf:
2375323660 return ir_analyze_instruction_typeof(ira, (IrInstructionTypeOf *)instruction);
23754 case IrInstructionIdToPtrType:
23755 return ir_analyze_instruction_to_ptr_type(ira, (IrInstructionToPtrType *)instruction);
23756 case IrInstructionIdPtrTypeChild:
23757 return ir_analyze_instruction_ptr_type_child(ira, (IrInstructionPtrTypeChild *)instruction);
2375823661 case IrInstructionIdSetCold:
2375923662 return ir_analyze_instruction_set_cold(ira, (IrInstructionSetCold *)instruction);
2376023663 case IrInstructionIdSetRuntimeSafety:
......@@ -24152,8 +24055,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2415224055 case IrInstructionIdVarPtr:
2415324056 case IrInstructionIdReturnPtr:
2415424057 case IrInstructionIdTypeOf:
24155 case IrInstructionIdToPtrType:
24156 case IrInstructionIdPtrTypeChild:
2415724058 case IrInstructionIdStructFieldPtr:
2415824059 case IrInstructionIdUnionFieldPtr:
2415924060 case IrInstructionIdArrayType:
src/ir_print.cpp-18
......@@ -414,18 +414,6 @@ static void ir_print_typeof(IrPrint *irp, IrInstructionTypeOf *instruction) {
414414 fprintf(irp->f, ")");
415415}
416416
417static void ir_print_to_ptr_type(IrPrint *irp, IrInstructionToPtrType *instruction) {
418 fprintf(irp->f, "@toPtrType(");
419 ir_print_other_instruction(irp, instruction->ptr);
420 fprintf(irp->f, ")");
421}
422
423static void ir_print_ptr_type_child(IrPrint *irp, IrInstructionPtrTypeChild *instruction) {
424 fprintf(irp->f, "@ptrTypeChild(");
425 ir_print_other_instruction(irp, instruction->value);
426 fprintf(irp->f, ")");
427}
428
429417static void ir_print_field_ptr(IrPrint *irp, IrInstructionFieldPtr *instruction) {
430418 if (instruction->field_name_buffer) {
431419 fprintf(irp->f, "fieldptr ");
......@@ -1625,12 +1613,6 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
16251613 case IrInstructionIdTypeOf:
16261614 ir_print_typeof(irp, (IrInstructionTypeOf *)instruction);
16271615 break;
1628 case IrInstructionIdToPtrType:
1629 ir_print_to_ptr_type(irp, (IrInstructionToPtrType *)instruction);
1630 break;
1631 case IrInstructionIdPtrTypeChild:
1632 ir_print_ptr_type_child(irp, (IrInstructionPtrTypeChild *)instruction);
1633 break;
16341616 case IrInstructionIdFieldPtr:
16351617 ir_print_field_ptr(irp, (IrInstructionFieldPtr *)instruction);
16361618 break;