authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-19 10:48:04-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-19 10:58:12-04:00
log005a54a853a77b9c28551490fc08dc37cd7d7715
tree7f13cd3b88f33f5103a5a539a37abd4f9fd1ceb1
parent01577a3af480cff02c5f78864f8056487b3d3b44
signaturelock-open Commit is signed but in an unrecognized format.

fixups for `@splat`

* Fix codegen for splat - instead of giving vectors of length N to shufflevector for both of the operands, it gives vectors of length 1. The mask vector is the only one that needs N elements. * Separate Splat into SplatSrc and SplatGen; the `len` is not needed once it gets to codegen since it is redundant with the result type. * Refactor compile error for wrong vector element type so that the compile error message is not duplicated in zig source code * Improve implementation to correctly handle comptime values such as undefined and lazy values. * Improve compile error for bad vector element type to point to the correct place. * Delete dead code. * Modify behavior test to use an array cast instead of vector element indexing since I'm merging this splat commit out-of-order from Shawn's patch set.

6 files changed, 101 insertions(+), 64 deletions(-)

src/all_types.hpp+9-2
......@@ -2432,7 +2432,8 @@ enum IrInstructionId {
24322432 IrInstructionIdIntType,
24332433 IrInstructionIdVectorType,
24342434 IrInstructionIdShuffleVector,
2435 IrInstructionIdSplat,
2435 IrInstructionIdSplatSrc,
2436 IrInstructionIdSplatGen,
24362437 IrInstructionIdBoolNot,
24372438 IrInstructionIdMemset,
24382439 IrInstructionIdMemcpy,
......@@ -3683,13 +3684,19 @@ struct IrInstructionShuffleVector {
36833684 IrInstruction *mask; // This is in zig-format, not llvm format
36843685};
36853686
3686struct IrInstructionSplat {
3687struct IrInstructionSplatSrc {
36873688 IrInstruction base;
36883689
36893690 IrInstruction *len;
36903691 IrInstruction *scalar;
36913692};
36923693
3694struct IrInstructionSplatGen {
3695 IrInstruction base;
3696
3697 IrInstruction *scalar;
3698};
3699
36933700struct IrInstructionAssertZero {
36943701 IrInstruction base;
36953702
src/codegen.cpp+13-14
......@@ -4619,18 +4619,16 @@ static LLVMValueRef ir_render_shuffle_vector(CodeGen *g, IrExecutable *executabl
46194619 llvm_mask_value, "");
46204620}
46214621
4622static LLVMValueRef ir_render_splat(CodeGen *g, IrExecutable *executable, IrInstructionSplat *instruction) {
4623 uint64_t len = bigint_as_u64(&instruction->len->value.data.x_bigint);
4624 LLVMValueRef wrapped_scalar_undef = LLVMGetUndef(instruction->base.value.type->llvm_type);
4625 LLVMValueRef wrapped_scalar = LLVMBuildInsertElement(g->builder, wrapped_scalar_undef,
4626 ir_llvm_value(g, instruction->scalar),
4627 LLVMConstInt(LLVMInt32Type(), 0, false),
4628 "");
4629 return LLVMBuildShuffleVector(g->builder,
4630 wrapped_scalar,
4631 wrapped_scalar_undef,
4632 LLVMConstNull(LLVMVectorType(g->builtin_types.entry_u32->llvm_type, (uint32_t)len)),
4633 "");
4622static LLVMValueRef ir_render_splat(CodeGen *g, IrExecutable *executable, IrInstructionSplatGen *instruction) {
4623 ZigType *result_type = instruction->base.value.type;
4624 src_assert(result_type->id == ZigTypeIdVector, instruction->base.source_node);
4625 uint32_t len = result_type->data.vector.len;
4626 LLVMTypeRef op_llvm_type = LLVMVectorType(get_llvm_type(g, instruction->scalar->value.type), 1);
4627 LLVMTypeRef mask_llvm_type = LLVMVectorType(LLVMInt32Type(), len);
4628 LLVMValueRef undef_vector = LLVMGetUndef(op_llvm_type);
4629 LLVMValueRef op_vector = LLVMBuildInsertElement(g->builder, undef_vector,
4630 ir_llvm_value(g, instruction->scalar), LLVMConstInt(LLVMInt32Type(), 0, false), "");
4631 return LLVMBuildShuffleVector(g->builder, op_vector, undef_vector, LLVMConstNull(mask_llvm_type), "");
46344632}
46354633
46364634static LLVMValueRef ir_render_pop_count(CodeGen *g, IrExecutable *executable, IrInstructionPopCount *instruction) {
......@@ -6000,6 +5998,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
60005998 case IrInstructionIdFrameSizeSrc:
60015999 case IrInstructionIdAllocaGen:
60026000 case IrInstructionIdAwaitSrc:
6001 case IrInstructionIdSplatSrc:
60036002 zig_unreachable();
60046003
60056004 case IrInstructionIdDeclVarGen:
......@@ -6160,8 +6159,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
61606159 return ir_render_spill_end(g, executable, (IrInstructionSpillEnd *)instruction);
61616160 case IrInstructionIdShuffleVector:
61626161 return ir_render_shuffle_vector(g, executable, (IrInstructionShuffleVector *) instruction);
6163 case IrInstructionIdSplat:
6164 return ir_render_splat(g, executable, (IrInstructionSplat *) instruction);
6162 case IrInstructionIdSplatGen:
6163 return ir_render_splat(g, executable, (IrInstructionSplatGen *) instruction);
61656164 }
61666165 zig_unreachable();
61676166}
src/ir.cpp+57-38
......@@ -721,8 +721,12 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionShuffleVector *)
721721 return IrInstructionIdShuffleVector;
722722}
723723
724static constexpr IrInstructionId ir_instruction_id(IrInstructionSplat *) {
725 return IrInstructionIdSplat;
724static constexpr IrInstructionId ir_instruction_id(IrInstructionSplatSrc *) {
725 return IrInstructionIdSplatSrc;
726}
727
728static constexpr IrInstructionId ir_instruction_id(IrInstructionSplatGen *) {
729 return IrInstructionIdSplatGen;
726730}
727731
728732static constexpr IrInstructionId ir_instruction_id(IrInstructionBoolNot *) {
......@@ -2304,10 +2308,10 @@ static IrInstruction *ir_build_shuffle_vector(IrBuilder *irb, Scope *scope, AstN
23042308 return &instruction->base;
23052309}
23062310
2307static IrInstruction *ir_build_splat(IrBuilder *irb, Scope *scope, AstNode *source_node,
2311static IrInstruction *ir_build_splat_src(IrBuilder *irb, Scope *scope, AstNode *source_node,
23082312 IrInstruction *len, IrInstruction *scalar)
23092313{
2310 IrInstructionSplat *instruction = ir_build_instruction<IrInstructionSplat>(irb, scope, source_node);
2314 IrInstructionSplatSrc *instruction = ir_build_instruction<IrInstructionSplatSrc>(irb, scope, source_node);
23112315 instruction->len = len;
23122316 instruction->scalar = scalar;
23132317
......@@ -2373,6 +2377,19 @@ static IrInstruction *ir_build_slice_src(IrBuilder *irb, Scope *scope, AstNode *
23732377 return &instruction->base;
23742378}
23752379
2380static IrInstruction *ir_build_splat_gen(IrAnalyze *ira, IrInstruction *source_instruction, ZigType *result_type,
2381 IrInstruction *scalar)
2382{
2383 IrInstructionSplatGen *instruction = ir_build_instruction<IrInstructionSplatGen>(
2384 &ira->new_irb, source_instruction->scope, source_instruction->source_node);
2385 instruction->base.value.type = result_type;
2386 instruction->scalar = scalar;
2387
2388 ir_ref_instruction(scalar, ira->new_irb.current_basic_block);
2389
2390 return &instruction->base;
2391}
2392
23762393static IrInstruction *ir_build_slice_gen(IrAnalyze *ira, IrInstruction *source_instruction, ZigType *slice_type,
23772394 IrInstruction *ptr, IrInstruction *start, IrInstruction *end, bool safety_check_on, IrInstruction *result_loc)
23782395{
......@@ -5014,7 +5031,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
50145031 if (arg1_value == irb->codegen->invalid_instruction)
50155032 return arg1_value;
50165033
5017 IrInstruction *splat = ir_build_splat(irb, scope, node,
5034 IrInstruction *splat = ir_build_splat_src(irb, scope, node,
50185035 arg0_value, arg1_value);
50195036 return ir_lval_wrap(irb, scope, splat, lval, result_loc);
50205037 }
......@@ -11082,16 +11099,23 @@ static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {
1108211099 return ir_resolve_const_type(ira->codegen, ira->new_irb.exec, type_value->source_node, val);
1108311100}
1108411101
11102static Error ir_validate_vector_elem_type(IrAnalyze *ira, IrInstruction *source_instr, ZigType *elem_type) {
11103 if (!is_valid_vector_elem_type(elem_type)) {
11104 ir_add_error(ira, source_instr,
11105 buf_sprintf("vector element type must be integer, float, bool, or pointer; '%s' is invalid",
11106 buf_ptr(&elem_type->name)));
11107 return ErrorSemanticAnalyzeFail;
11108 }
11109 return ErrorNone;
11110}
11111
1108511112static ZigType *ir_resolve_vector_elem_type(IrAnalyze *ira, IrInstruction *elem_type_value) {
11113 Error err;
1108611114 ZigType *elem_type = ir_resolve_type(ira, elem_type_value);
1108711115 if (type_is_invalid(elem_type))
1108811116 return ira->codegen->builtin_types.entry_invalid;
11089 if (!is_valid_vector_elem_type(elem_type)) {
11090 ir_add_error(ira, elem_type_value,
11091 buf_sprintf("vector element type must be integer, float, bool, or pointer; '%s' is invalid",
11092 buf_ptr(&elem_type->name)));
11117 if ((err = ir_validate_vector_elem_type(ira, elem_type_value, elem_type)))
1109311118 return ira->codegen->builtin_types.entry_invalid;
11094 }
1109511119 return elem_type;
1109611120}
1109711121
......@@ -22357,7 +22381,9 @@ static IrInstruction *ir_analyze_instruction_shuffle_vector(IrAnalyze *ira, IrIn
2235722381 return ir_analyze_shuffle_vector(ira, &instruction->base, scalar_type, a, b, mask);
2235822382}
2235922383
22360static IrInstruction *ir_analyze_instruction_splat(IrAnalyze *ira, IrInstructionSplat *instruction) {
22384static IrInstruction *ir_analyze_instruction_splat(IrAnalyze *ira, IrInstructionSplatSrc *instruction) {
22385 Error err;
22386
2236122387 IrInstruction *len = instruction->len->child;
2236222388 if (type_is_invalid(len->value.type))
2236322389 return ira->codegen->invalid_instruction;
......@@ -22366,41 +22392,32 @@ static IrInstruction *ir_analyze_instruction_splat(IrAnalyze *ira, IrInstruction
2236622392 if (type_is_invalid(scalar->value.type))
2236722393 return ira->codegen->invalid_instruction;
2236822394
22369 uint64_t len_int;
22370 if (!ir_resolve_unsigned(ira, len, ira->codegen->builtin_types.entry_u32, &len_int)) {
22371 ir_add_error(ira, len,
22372 buf_sprintf("splat length must be comptime"));
22395 uint64_t len_u64;
22396 if (!ir_resolve_unsigned(ira, len, ira->codegen->builtin_types.entry_u32, &len_u64))
2237322397 return ira->codegen->invalid_instruction;
22374 }
22398 uint32_t len_int = len_u64;
2237522399
22376 if (!is_valid_vector_elem_type(scalar->value.type)) {
22377 ir_add_error(ira, len,
22378 buf_sprintf("vector element type must be integer, float, bool, or pointer; '%s' is invalid",
22379 buf_ptr(&scalar->value.type->name)));
22400 if ((err = ir_validate_vector_elem_type(ira, scalar, scalar->value.type)))
2238022401 return ira->codegen->invalid_instruction;
22381 }
2238222402
2238322403 ZigType *return_type = get_vector_type(ira->codegen, len_int, scalar->value.type);
2238422404
2238522405 if (instr_is_comptime(scalar)) {
22386 IrInstruction *result = ir_const_undef(ira, scalar, return_type);
22387 result->value.data.x_array.data.s_none.elements =
22388 allocate<ConstExprValue>(len_int);
22389 for (uint32_t i = 0; i < len_int; i++) {
22390 result->value.data.x_array.data.s_none.elements[i] =
22391 scalar->value;
22406 ConstExprValue *scalar_val = ir_resolve_const(ira, scalar, UndefOk);
22407 if (scalar_val == nullptr)
22408 return ira->codegen->invalid_instruction;
22409 if (scalar_val->special == ConstValSpecialUndef)
22410 return ir_const_undef(ira, &instruction->base, return_type);
22411
22412 IrInstruction *result = ir_const(ira, &instruction->base, return_type);
22413 result->value.data.x_array.data.s_none.elements = create_const_vals(len_int);
22414 for (uint32_t i = 0; i < len_int; i += 1) {
22415 copy_const_val(&result->value.data.x_array.data.s_none.elements[i], scalar_val, false);
2239222416 }
22393 result->value.type = return_type;
22394 result->value.special = ConstValSpecialStatic;
2239522417 return result;
2239622418 }
2239722419
22398 IrInstruction *result = ir_build_splat(&ira->new_irb,
22399 instruction->base.scope, instruction->base.source_node,
22400 instruction->len->child, instruction->scalar->child);
22401 result->value.type = return_type;
22402 result->value.special = ConstValSpecialRuntime;
22403 return result;
22420 return ir_build_splat_gen(ira, &instruction->base, return_type, scalar);
2240422421}
2240522422
2240622423static IrInstruction *ir_analyze_instruction_bool_not(IrAnalyze *ira, IrInstructionBoolNot *instruction) {
......@@ -25857,6 +25874,7 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction
2585725874 case IrInstructionIdTestErrGen:
2585825875 case IrInstructionIdFrameSizeGen:
2585925876 case IrInstructionIdAwaitGen:
25877 case IrInstructionIdSplatGen:
2586025878 zig_unreachable();
2586125879
2586225880 case IrInstructionIdReturn:
......@@ -25987,8 +26005,8 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction
2598726005 return ir_analyze_instruction_vector_type(ira, (IrInstructionVectorType *)instruction);
2598826006 case IrInstructionIdShuffleVector:
2598926007 return ir_analyze_instruction_shuffle_vector(ira, (IrInstructionShuffleVector *)instruction);
25990 case IrInstructionIdSplat:
25991 return ir_analyze_instruction_splat(ira, (IrInstructionSplat *)instruction);
26008 case IrInstructionIdSplatSrc:
26009 return ir_analyze_instruction_splat(ira, (IrInstructionSplatSrc *)instruction);
2599226010 case IrInstructionIdBoolNot:
2599326011 return ir_analyze_instruction_bool_not(ira, (IrInstructionBoolNot *)instruction);
2599426012 case IrInstructionIdMemset:
......@@ -26325,7 +26343,8 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2632526343 case IrInstructionIdIntType:
2632626344 case IrInstructionIdVectorType:
2632726345 case IrInstructionIdShuffleVector:
26328 case IrInstructionIdSplat:
26346 case IrInstructionIdSplatSrc:
26347 case IrInstructionIdSplatGen:
2632926348 case IrInstructionIdBoolNot:
2633026349 case IrInstructionIdSliceSrc:
2633126350 case IrInstructionIdMemberCount:
src/ir_print.cpp+16-5
......@@ -44,8 +44,10 @@ static const char* ir_instruction_type_str(IrInstruction* instruction) {
4444 return "Invalid";
4545 case IrInstructionIdShuffleVector:
4646 return "Shuffle";
47 case IrInstructionIdSplat:
48 return "Splat";
47 case IrInstructionIdSplatSrc:
48 return "SplatSrc";
49 case IrInstructionIdSplatGen:
50 return "SplatGen";
4951 case IrInstructionIdDeclVarSrc:
5052 return "DeclVarSrc";
5153 case IrInstructionIdDeclVarGen:
......@@ -1224,7 +1226,7 @@ static void ir_print_shuffle_vector(IrPrint *irp, IrInstructionShuffleVector *in
12241226 fprintf(irp->f, ")");
12251227}
12261228
1227static void ir_print_splat(IrPrint *irp, IrInstructionSplat *instruction) {
1229static void ir_print_splat_src(IrPrint *irp, IrInstructionSplatSrc *instruction) {
12281230 fprintf(irp->f, "@splat(");
12291231 ir_print_other_instruction(irp, instruction->len);
12301232 fprintf(irp->f, ", ");
......@@ -1232,6 +1234,12 @@ static void ir_print_splat(IrPrint *irp, IrInstructionSplat *instruction) {
12321234 fprintf(irp->f, ")");
12331235}
12341236
1237static void ir_print_splat_gen(IrPrint *irp, IrInstructionSplatGen *instruction) {
1238 fprintf(irp->f, "@splat(");
1239 ir_print_other_instruction(irp, instruction->scalar);
1240 fprintf(irp->f, ")");
1241}
1242
12351243static void ir_print_bool_not(IrPrint *irp, IrInstructionBoolNot *instruction) {
12361244 fprintf(irp->f, "! ");
12371245 ir_print_other_instruction(irp, instruction->value);
......@@ -2170,8 +2178,11 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction, bool
21702178 case IrInstructionIdShuffleVector:
21712179 ir_print_shuffle_vector(irp, (IrInstructionShuffleVector *)instruction);
21722180 break;
2173 case IrInstructionIdSplat:
2174 ir_print_splat(irp, (IrInstructionSplat *)instruction);
2181 case IrInstructionIdSplatSrc:
2182 ir_print_splat_src(irp, (IrInstructionSplatSrc *)instruction);
2183 break;
2184 case IrInstructionIdSplatGen:
2185 ir_print_splat_gen(irp, (IrInstructionSplatGen *)instruction);
21752186 break;
21762187 case IrInstructionIdBoolNot:
21772188 ir_print_bool_not(irp, (IrInstructionBoolNot *)instruction);
test/compile_errors.zig+1-1
......@@ -6514,7 +6514,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
65146514 \\ var v = @splat(4, c);
65156515 \\}
65166516 ,
6517 "tmp.zig:3:20: error: vector element type must be integer, float, bool, or pointer; 'comptime_int' is invalid",
6517 "tmp.zig:3:23: error: vector element type must be integer, float, bool, or pointer; 'comptime_int' is invalid",
65186518 );
65196519
65206520 cases.add("compileLog of tagged enum doesn't crash the compiler",
test/stage1/behavior/vector.zig+5-4
......@@ -145,10 +145,11 @@ test "vector @splat" {
145145 var v: u32 = 5;
146146 var x = @splat(4, v);
147147 expect(@typeOf(x) == @Vector(4, u32));
148 expect(x[0] == 5);
149 expect(x[1] == 5);
150 expect(x[2] == 5);
151 expect(x[3] == 5);
148 var array_x: [4]u32 = x;
149 expect(array_x[0] == 5);
150 expect(array_x[1] == 5);
151 expect(array_x[2] == 5);
152 expect(array_x[3] == 5);
152153 }
153154 };
154155 S.doTheTest();