authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-10 19:49:24-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-10 19:49:24-04:00
log4582ec518f9984a26c68b8427a914bad6dc80c4a
treebffaa526c169c38d1918afad3aa0315c0e9c551f
parent9a324ecb42f69791d49bc8e62e935aec75b5e920
signature Commit is signed but in an unrecognized format.

result location semantics for vector to array

```zig export fn entry() void { var x: @Vector(4, i32) = undefined; var y: [4]i32 = x; } ``` ```llvm define void @entry() #2 !dbg !35 { Entry: %x = alloca <4 x i32>, align 16 %y = alloca [4 x i32], align 4 %0 = bitcast <4 x i32>* %x to i8*, !dbg !47 call void @llvm.memset.p0i8.i64(i8* align 16 %0, i8 -86, i64 16, i1 false), !dbg !47 call void @llvm.dbg.declare(metadata <4 x i32>* %x, metadata !39, metadata !DIExpression()), !dbg !47 %1 = load <4 x i32>, <4 x i32>* %x, align 16, !dbg !48 %2 = bitcast [4 x i32]* %y to <4 x i32>*, !dbg !48 store <4 x i32> %1, <4 x i32>* %2, align 16, !dbg !48 call void @llvm.dbg.declare(metadata [4 x i32]* %y, metadata !45, metadata !DIExpression()), !dbg !49 ret void, !dbg !50 } ```

4 files changed, 17 insertions(+), 16 deletions(-)

src/all_types.hpp+1-1
...@@ -3522,7 +3522,7 @@ struct IrInstructionVectorToArray {...@@ -3522,7 +3522,7 @@ struct IrInstructionVectorToArray {
3522 IrInstruction base;3522 IrInstruction base;
35233523
3524 IrInstruction *vector;3524 IrInstruction *vector;
3525 LLVMValueRef tmp_ptr;3525 IrInstruction *result_loc;
3526};3526};
35273527
3528struct IrInstructionAssertZero {3528struct IrInstructionAssertZero {
src/codegen.cpp+3-7
...@@ -5386,12 +5386,12 @@ static LLVMValueRef ir_render_vector_to_array(CodeGen *g, IrExecutable *executab...@@ -5386,12 +5386,12 @@ static LLVMValueRef ir_render_vector_to_array(CodeGen *g, IrExecutable *executab
5386 ZigType *array_type = instruction->base.value.type;5386 ZigType *array_type = instruction->base.value.type;
5387 assert(array_type->id == ZigTypeIdArray);5387 assert(array_type->id == ZigTypeIdArray);
5388 assert(handle_is_ptr(array_type));5388 assert(handle_is_ptr(array_type));
5389 assert(instruction->tmp_ptr);5389 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
5390 LLVMValueRef vector = ir_llvm_value(g, instruction->vector);5390 LLVMValueRef vector = ir_llvm_value(g, instruction->vector);
5391 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, instruction->tmp_ptr,5391 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, result_loc,
5392 LLVMPointerType(get_llvm_type(g, instruction->vector->value.type), 0), "");5392 LLVMPointerType(get_llvm_type(g, instruction->vector->value.type), 0), "");
5393 gen_store_untyped(g, vector, casted_ptr, 0, false);5393 gen_store_untyped(g, vector, casted_ptr, 0, false);
5394 return instruction->tmp_ptr;5394 return result_loc;
5395}5395}
53965396
5397static LLVMValueRef ir_render_array_to_vector(CodeGen *g, IrExecutable *executable,5397static LLVMValueRef ir_render_array_to_vector(CodeGen *g, IrExecutable *executable,
...@@ -6838,10 +6838,6 @@ static void do_code_gen(CodeGen *g) {...@@ -6838,10 +6838,6 @@ static void do_code_gen(CodeGen *g) {
6838 slot = &ref_instruction->tmp_ptr;6838 slot = &ref_instruction->tmp_ptr;
6839 assert(instruction->value.type->id == ZigTypeIdPointer);6839 assert(instruction->value.type->id == ZigTypeIdPointer);
6840 slot_type = instruction->value.type->data.pointer.child_type;6840 slot_type = instruction->value.type->data.pointer.child_type;
6841 } else if (instruction->id == IrInstructionIdVectorToArray) {
6842 IrInstructionVectorToArray *vector_to_array_instruction = (IrInstructionVectorToArray *)instruction;
6843 alignment_bytes = get_abi_alignment(g, vector_to_array_instruction->vector->value.type);
6844 slot = &vector_to_array_instruction->tmp_ptr;
6845 } else {6841 } else {
6846 zig_unreachable();6842 zig_unreachable();
6847 }6843 }
src/ir.cpp+11-7
...@@ -3131,16 +3131,16 @@ static IrInstruction *ir_build_check_runtime_scope(IrBuilder *irb, Scope *scope,...@@ -3131,16 +3131,16 @@ static IrInstruction *ir_build_check_runtime_scope(IrBuilder *irb, Scope *scope,
3131}3131}
31323132
3133static IrInstruction *ir_build_vector_to_array(IrAnalyze *ira, IrInstruction *source_instruction,3133static IrInstruction *ir_build_vector_to_array(IrAnalyze *ira, IrInstruction *source_instruction,
3134 IrInstruction *vector, ZigType *result_type)3134 ZigType *result_type, IrInstruction *vector, IrInstruction *result_loc)
3135{3135{
3136 IrInstructionVectorToArray *instruction = ir_build_instruction<IrInstructionVectorToArray>(&ira->new_irb,3136 IrInstructionVectorToArray *instruction = ir_build_instruction<IrInstructionVectorToArray>(&ira->new_irb,
3137 source_instruction->scope, source_instruction->source_node);3137 source_instruction->scope, source_instruction->source_node);
3138 instruction->base.value.type = result_type;3138 instruction->base.value.type = result_type;
3139 instruction->vector = vector;3139 instruction->vector = vector;
3140 instruction->result_loc = result_loc;
31403141
3141 ir_ref_instruction(vector, ira->new_irb.current_basic_block);3142 ir_ref_instruction(vector, ira->new_irb.current_basic_block);
31423143 ir_ref_instruction(result_loc, ira->new_irb.current_basic_block);
3143 ir_add_alloca(ira, &instruction->base, result_type);
31443144
3145 return &instruction->base;3145 return &instruction->base;
3146}3146}
...@@ -11985,7 +11985,7 @@ static IrInstruction *ir_analyze_array_to_vector(IrAnalyze *ira, IrInstruction *...@@ -11985,7 +11985,7 @@ static IrInstruction *ir_analyze_array_to_vector(IrAnalyze *ira, IrInstruction *
11985}11985}
1198611986
11987static IrInstruction *ir_analyze_vector_to_array(IrAnalyze *ira, IrInstruction *source_instr,11987static IrInstruction *ir_analyze_vector_to_array(IrAnalyze *ira, IrInstruction *source_instr,
11988 IrInstruction *vector, ZigType *array_type)11988 IrInstruction *vector, ZigType *array_type, ResultLoc *result_loc)
11989{11989{
11990 if (instr_is_comptime(vector)) {11990 if (instr_is_comptime(vector)) {
11991 // arrays and vectors have the same ConstExprValue representation11991 // arrays and vectors have the same ConstExprValue representation
...@@ -11994,7 +11994,11 @@ static IrInstruction *ir_analyze_vector_to_array(IrAnalyze *ira, IrInstruction *...@@ -11994,7 +11994,11 @@ static IrInstruction *ir_analyze_vector_to_array(IrAnalyze *ira, IrInstruction *
11994 result->value.type = array_type;11994 result->value.type = array_type;
11995 return result;11995 return result;
11996 }11996 }
11997 return ir_build_vector_to_array(ira, source_instr, vector, array_type);11997 IrInstruction *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, array_type, nullptr);
11998 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
11999 return result_loc_inst;
12000 }
12001 return ir_build_vector_to_array(ira, source_instr, array_type, vector, result_loc_inst);
11998}12002}
1199912003
12000static IrInstruction *ir_analyze_int_to_c_ptr(IrAnalyze *ira, IrInstruction *source_instr,12004static IrInstruction *ir_analyze_int_to_c_ptr(IrAnalyze *ira, IrInstruction *source_instr,
...@@ -12453,7 +12457,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -12453,7 +12457,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
12453 types_match_const_cast_only(ira, wanted_type->data.array.child_type,12457 types_match_const_cast_only(ira, wanted_type->data.array.child_type,
12454 actual_type->data.vector.elem_type, source_node, false).id == ConstCastResultIdOk)12458 actual_type->data.vector.elem_type, source_node, false).id == ConstCastResultIdOk)
12455 {12459 {
12456 return ir_analyze_vector_to_array(ira, source_instr, value, wanted_type);12460 return ir_analyze_vector_to_array(ira, source_instr, value, wanted_type, result_loc);
12457 }12461 }
1245812462
12459 // cast from [N]T to @Vector(N, T)12463 // cast from [N]T to @Vector(N, T)
...@@ -24484,6 +24488,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {...@@ -24484,6 +24488,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
24484 case IrInstructionIdPtrOfArrayToSlice:24488 case IrInstructionIdPtrOfArrayToSlice:
24485 case IrInstructionIdSliceGen:24489 case IrInstructionIdSliceGen:
24486 case IrInstructionIdOptionalWrap:24490 case IrInstructionIdOptionalWrap:
24491 case IrInstructionIdVectorToArray:
24487 return true;24492 return true;
2448824493
24489 case IrInstructionIdPhi:24494 case IrInstructionIdPhi:
...@@ -24578,7 +24583,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {...@@ -24578,7 +24583,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
24578 case IrInstructionIdFromBytes:24583 case IrInstructionIdFromBytes:
24579 case IrInstructionIdToBytes:24584 case IrInstructionIdToBytes:
24580 case IrInstructionIdEnumToInt:24585 case IrInstructionIdEnumToInt:
24581 case IrInstructionIdVectorToArray:
24582 case IrInstructionIdArrayToVector:24586 case IrInstructionIdArrayToVector:
24583 case IrInstructionIdHasDecl:24587 case IrInstructionIdHasDecl:
24584 case IrInstructionIdAllocaSrc:24588 case IrInstructionIdAllocaSrc:
src/ir_print.cpp+2-1
...@@ -1102,7 +1102,8 @@ static void ir_print_array_to_vector(IrPrint *irp, IrInstructionArrayToVector *i...@@ -1102,7 +1102,8 @@ static void ir_print_array_to_vector(IrPrint *irp, IrInstructionArrayToVector *i
1102static void ir_print_vector_to_array(IrPrint *irp, IrInstructionVectorToArray *instruction) {1102static void ir_print_vector_to_array(IrPrint *irp, IrInstructionVectorToArray *instruction) {
1103 fprintf(irp->f, "VectorToArray(");1103 fprintf(irp->f, "VectorToArray(");
1104 ir_print_other_instruction(irp, instruction->vector);1104 ir_print_other_instruction(irp, instruction->vector);
1105 fprintf(irp->f, ")");1105 fprintf(irp->f, ")result=");
1106 ir_print_other_instruction(irp, instruction->result_loc);
1106}1107}
11071108
1108static void ir_print_ptr_of_array_to_slice(IrPrint *irp, IrInstructionPtrOfArrayToSlice *instruction) {1109static void ir_print_ptr_of_array_to_slice(IrPrint *irp, IrInstructionPtrOfArrayToSlice *instruction) {