authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-18 15:41:56-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-18 16:15:19-04:00
log2038f4d45a597cc672380c0a5fc8dd98e928d24c
treedeb587d417cec959d90b9e8095aa04d1758e2698
parent193604c837df75ab0c3fa5860f8b234263fe5b50
signature Commit is signed but in an unrecognized format.

rework the implementation

* update documentation - move `@shuffle` to be sorted alphabetically - remove mention of LLVM - minor clarifications & rewording * introduce ir_resolve_vector_elem_type to avoid duplicate compile error message and duplicate vector element checking logic * rework ir_analyze_shuffle_vector to solve various issues * improve `@shuffle` to allow implicit cast of arrays * the shuffle tests weren't being run

6 files changed, 233 insertions(+), 233 deletions(-)

doc/langref.html.in+37-22
...@@ -7673,6 +7673,43 @@ test "@setRuntimeSafety" {...@@ -7673,6 +7673,43 @@ test "@setRuntimeSafety" {
7673 {#see_also|@shlExact|@shlWithOverflow#}7673 {#see_also|@shlExact|@shlWithOverflow#}
7674 {#header_close#}7674 {#header_close#}
76757675
7676 {#header_open|@shuffle#}
7677 <pre>{#syntax#}@shuffle(comptime E: type, a: @Vector(a_len, E), b: @Vector(b_len, E), comptime mask: @Vector(mask_len, i32)) @Vector(mask_len, E){#endsyntax#}</pre>
7678 <p>
7679 Constructs a new {#link|vector|Vectors#} by selecting elements from {#syntax#}a{#endsyntax#} and
7680 {#syntax#}b{#endsyntax#} based on {#syntax#}mask{#endsyntax#}.
7681 </p>
7682 <p>
7683 Each element in {#syntax#}mask{#endsyntax#} selects an element from either {#syntax#}a{#endsyntax#} or
7684 {#syntax#}b{#endsyntax#}. Positive numbers select from {#syntax#}a{#endsyntax#} starting at 0.
7685 Negative values select from {#syntax#}b{#endsyntax#}, starting at {#syntax#}-1{#endsyntax#} and going down.
7686 It is recommended to use the {#syntax#}~{#endsyntax#} operator from indexes from {#syntax#}b{#endsyntax#}
7687 so that both indexes can start from {#syntax#}0{#endsyntax#} (i.e. {#syntax#}~i32(0){#endsyntax#} is
7688 {#syntax#}-1{#endsyntax#}).
7689 </p>
7690 <p>
7691 For each element of {#syntax#}mask{#endsyntax#}, if it or the selected value from
7692 {#syntax#}a{#endsyntax#} or {#syntax#}b{#endsyntax#} is {#syntax#}undefined{#endsyntax#},
7693 then the resulting element is {#syntax#}undefined{#endsyntax#}.
7694 </p>
7695 <p>
7696 {#syntax#}a_len{#endsyntax#} and {#syntax#}b_len{#endsyntax#} may differ in length. Out-of-bounds element
7697 indexes in {#syntax#}mask{#endsyntax#} result in compile errors.
7698 </p>
7699 <p>
7700 If {#syntax#}a{#endsyntax#} or {#syntax#}b{#endsyntax#} is {#syntax#}undefined{#endsyntax#}, it
7701 is equivalent to a vector of all {#syntax#}undefined{#endsyntax#} with the same length as the other vector.
7702 If both vectors are {#syntax#}undefined{#endsyntax#}, {#syntax#}@shuffle{#endsyntax#} returns
7703 a vector with all elements {#syntax#}undefined{#endsyntax#}.
7704 </p>
7705 <p>
7706 {#syntax#}E{#endsyntax#} must be an {#link|integer|Integers#}, {#link|float|Floats#},
7707 {#link|pointer|Pointers#}, or {#syntax#}bool{#endsyntax#}. The mask may be any vector length, and its
7708 length determines the result length.
7709 </p>
7710 {#see_also|SIMD#}
7711 {#header_close#}
7712
7676 {#header_open|@sizeOf#}7713 {#header_open|@sizeOf#}
7677 <pre>{#syntax#}@sizeOf(comptime T: type) comptime_int{#endsyntax#}</pre>7714 <pre>{#syntax#}@sizeOf(comptime T: type) comptime_int{#endsyntax#}</pre>
7678 <p>7715 <p>
...@@ -8226,28 +8263,6 @@ fn foo(comptime T: type, ptr: *T) T {...@@ -8226,28 +8263,6 @@ fn foo(comptime T: type, ptr: *T) T {
8226 {#link|pointer|Pointers#}.8263 {#link|pointer|Pointers#}.
8227 </p>8264 </p>
8228 {#header_close#}8265 {#header_close#}
8229
8230 {#header_open|@shuffle#}
8231 <pre>{#syntax#}@shuffle(comptime ElemType: type, a: @Vector(_, ElemType), b: @Vector(_, ElemType), comptime mask: @Vector(_, u32)) @Vector(mask.len, ElemType){#endsyntax#}</pre>
8232 <p>
8233 Does the {#syntax#}shufflevector{#endsyntax#} instruction. Each element in {#syntax#}comptime{#endsyntax#}
8234 (and always {#syntax#}i32{#endsyntax#}) {#syntax#}mask{#endsyntax#} selects a element from either {#syntax#}a{#endsyntax#} or {#syntax#}b{#endsyntax#}.
8235 Positive numbers select from {#syntax#}a{#endsyntax#} (starting at 0), while negative values select
8236 from {#syntax#}b{#endsyntax#} (starting at -1 and going down). It is recommended to use the {#syntax#}~{#endsyntax#}
8237 operator from indexes from b so that both indexes can start from 0 (i.e. ~0 is -1). If either the {#syntax#}mask{#endsyntax#}
8238 value or the value from {#syntax#}a{#endsyntax#} or {#syntax#}b{#endsyntax#} that it selects are {#syntax#}undefined{#endsyntax#}
8239 then the resulting value is {#syntax#}undefined{#endsyntax#}. Also see {#link|SIMD#} and
8240 the relevent <a href="https://llvm.org/docs/LangRef.html#i-shufflevector">LLVM Documentation on
8241 {#syntax#}shufflevector{#endsyntax#}</a>, although note that the mask values are interpreted differently than in LLVM-IR.
8242 Also, unlike LLVM-IR, the number of elements in {#syntax#}a{#endsyntax#} and {#syntax#}b{#endsyntax#} do not have to match.
8243 The {#syntax#}undefined{#endsyntax#} identifier can be selected from up to the length of the other vector,
8244 and yields {#syntax#}undefined{#endsyntax#}. If both vectors are {#syntax#}undefined{#endsyntax#}, yields an
8245 {#syntax#}undefined{#endsyntax#} {#syntax#}ElemType{#endsyntax#} vector with length of {#syntax#}mask{#endsyntax#}.</p>
8246 <p>
8247 {#syntax#}ElemType{#endsyntax#} must be an {#link|integer|Integers#}, a {#link|float|Floats#}, or a
8248 {#link|pointer|Pointers#}. The mask may be any vector length that the target supports, and its' length determines the result length.
8249 </p>
8250 {#header_close#}
8251 {#header_close#}8266 {#header_close#}
82528267
8253 {#header_open|Build Mode#}8268 {#header_open|Build Mode#}
src/codegen.cpp+10-9
...@@ -4583,7 +4583,7 @@ static LLVMValueRef ir_render_ctz(CodeGen *g, IrExecutable *executable, IrInstru...@@ -4583,7 +4583,7 @@ static LLVMValueRef ir_render_ctz(CodeGen *g, IrExecutable *executable, IrInstru
45834583
4584static LLVMValueRef ir_render_shuffle_vector(CodeGen *g, IrExecutable *executable, IrInstructionShuffleVector *instruction) {4584static LLVMValueRef ir_render_shuffle_vector(CodeGen *g, IrExecutable *executable, IrInstructionShuffleVector *instruction) {
4585 uint64_t len_a = instruction->a->value.type->data.vector.len;4585 uint64_t len_a = instruction->a->value.type->data.vector.len;
4586 uint64_t len_c = instruction->mask->value.type->data.vector.len;4586 uint64_t len_mask = instruction->mask->value.type->data.vector.len;
45874587
4588 // LLVM uses integers larger than the length of the first array to4588 // LLVM uses integers larger than the length of the first array to
4589 // index into the second array. This was deemed unnecessarily fragile4589 // index into the second array. This was deemed unnecessarily fragile
...@@ -4591,23 +4591,24 @@ static LLVMValueRef ir_render_shuffle_vector(CodeGen *g, IrExecutable *executabl...@@ -4591,23 +4591,24 @@ static LLVMValueRef ir_render_shuffle_vector(CodeGen *g, IrExecutable *executabl
4591 // second vector. These start at -1 and go down, and are easiest to use4591 // second vector. These start at -1 and go down, and are easiest to use
4592 // with the ~ operator. Here we convert between the two formats.4592 // with the ~ operator. Here we convert between the two formats.
4593 IrInstruction *mask = instruction->mask;4593 IrInstruction *mask = instruction->mask;
4594 LLVMValueRef *values = allocate<LLVMValueRef>(len_c);4594 LLVMValueRef *values = allocate<LLVMValueRef>(len_mask);
4595 for (uint64_t i = 0;i < len_c;i++) {4595 for (uint64_t i = 0; i < len_mask; i++) {
4596 if (mask->value.data.x_array.data.s_none.elements[i].special == ConstValSpecialUndef) {4596 if (mask->value.data.x_array.data.s_none.elements[i].special == ConstValSpecialUndef) {
4597 values[i] = LLVMGetUndef(LLVMInt32Type());4597 values[i] = LLVMGetUndef(LLVMInt32Type());
4598 } else {4598 } else {
4599 int64_t v = bigint_as_signed(&mask->value.data.x_array.data.s_none.elements[i].data.x_bigint);4599 int32_t v = bigint_as_signed(&mask->value.data.x_array.data.s_none.elements[i].data.x_bigint);
4600 if (v < 0)4600 uint32_t index_val = (v >= 0) ? (uint32_t)v : (uint32_t)~v + (uint32_t)len_a;
4601 v = (uint32_t)~v + (uint32_t)len_a;4601 values[i] = LLVMConstInt(LLVMInt32Type(), index_val, false);
4602 values[i] = LLVMConstInt(LLVMInt32Type(), v, false);
4603 }4602 }
4604 }4603 }
46054604
4605 LLVMValueRef llvm_mask_value = LLVMConstVector(values, len_mask);
4606 free(values);
4607
4606 return LLVMBuildShuffleVector(g->builder,4608 return LLVMBuildShuffleVector(g->builder,
4607 ir_llvm_value(g, instruction->a),4609 ir_llvm_value(g, instruction->a),
4608 ir_llvm_value(g, instruction->b),4610 ir_llvm_value(g, instruction->b),
4609 LLVMConstVector(values, len_c),4611 llvm_mask_value, "");
4610 "");
4611}4612}
46124613
4613static LLVMValueRef ir_render_pop_count(CodeGen *g, IrExecutable *executable, IrInstructionPopCount *instruction) {4614static LLVMValueRef ir_render_pop_count(CodeGen *g, IrExecutable *executable, IrInstructionPopCount *instruction) {
src/ir.cpp+162-179
...@@ -11049,6 +11049,19 @@ static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {...@@ -11049,6 +11049,19 @@ static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {
11049 return ir_resolve_const_type(ira->codegen, ira->new_irb.exec, type_value->source_node, val);11049 return ir_resolve_const_type(ira->codegen, ira->new_irb.exec, type_value->source_node, val);
11050}11050}
1105111051
11052static ZigType *ir_resolve_vector_elem_type(IrAnalyze *ira, IrInstruction *elem_type_value) {
11053 ZigType *elem_type = ir_resolve_type(ira, elem_type_value);
11054 if (type_is_invalid(elem_type))
11055 return ira->codegen->builtin_types.entry_invalid;
11056 if (!is_valid_vector_elem_type(elem_type)) {
11057 ir_add_error(ira, elem_type_value,
11058 buf_sprintf("vector element type must be integer, float, bool, or pointer; '%s' is invalid",
11059 buf_ptr(&elem_type->name)));
11060 return ira->codegen->builtin_types.entry_invalid;
11061 }
11062 return elem_type;
11063}
11064
11052static ZigType *ir_resolve_int_type(IrAnalyze *ira, IrInstruction *type_value) {11065static ZigType *ir_resolve_int_type(IrAnalyze *ira, IrInstruction *type_value) {
11053 ZigType *ty = ir_resolve_type(ira, type_value);11066 ZigType *ty = ir_resolve_type(ira, type_value);
11054 if (type_is_invalid(ty))11067 if (type_is_invalid(ty))
...@@ -22096,242 +22109,212 @@ static IrInstruction *ir_analyze_instruction_vector_type(IrAnalyze *ira, IrInstr...@@ -22096,242 +22109,212 @@ static IrInstruction *ir_analyze_instruction_vector_type(IrAnalyze *ira, IrInstr
22096 if (!ir_resolve_unsigned(ira, instruction->len->child, ira->codegen->builtin_types.entry_u32, &len))22109 if (!ir_resolve_unsigned(ira, instruction->len->child, ira->codegen->builtin_types.entry_u32, &len))
22097 return ira->codegen->invalid_instruction;22110 return ira->codegen->invalid_instruction;
2209822111
22099 ZigType *elem_type = ir_resolve_type(ira, instruction->elem_type->child);22112 ZigType *elem_type = ir_resolve_vector_elem_type(ira, instruction->elem_type->child);
22100 if (type_is_invalid(elem_type))22113 if (type_is_invalid(elem_type))
22101 return ira->codegen->invalid_instruction;22114 return ira->codegen->invalid_instruction;
2210222115
22103 if (!is_valid_vector_elem_type(elem_type)) {
22104 ir_add_error(ira, instruction->elem_type,
22105 buf_sprintf("vector element type must be integer, float, bool, or pointer; '%s' is invalid",
22106 buf_ptr(&elem_type->name)));
22107 return ira->codegen->invalid_instruction;
22108 }
22109
22110 ZigType *vector_type = get_vector_type(ira->codegen, len, elem_type);22116 ZigType *vector_type = get_vector_type(ira->codegen, len, elem_type);
2211122117
22112 return ir_const_type(ira, &instruction->base, vector_type);22118 return ir_const_type(ira, &instruction->base, vector_type);
22113}22119}
2211422120
22115static IrInstruction *ir_analyze_shuffle_vector(IrAnalyze *ira, IrInstruction *source_instr,22121static IrInstruction *ir_analyze_shuffle_vector(IrAnalyze *ira, IrInstruction *source_instr,
22116 ZigType *scalar_type, IrInstruction *a, IrInstruction *b, IrInstruction *mask) {22122 ZigType *scalar_type, IrInstruction *a, IrInstruction *b, IrInstruction *mask)
22117 assert(source_instr && scalar_type && a && b && mask);22123{
22118 assert(scalar_type->id == ZigTypeIdBool ||22124 ir_assert(source_instr && scalar_type && a && b && mask, source_instr);
22119 scalar_type->id == ZigTypeIdInt ||22125 ir_assert(is_valid_vector_elem_type(scalar_type), source_instr);
22120 scalar_type->id == ZigTypeIdFloat ||22126
22121 scalar_type->id == ZigTypeIdPointer);22127 uint32_t len_mask;
2212222128 if (mask->value.type->id == ZigTypeIdVector) {
22123 ZigType *mask_type = mask->value.type;22129 len_mask = mask->value.type->data.vector.len;
22124 if (type_is_invalid(mask_type))22130 } else if (mask->value.type->id == ZigTypeIdArray) {
22131 len_mask = mask->value.type->data.array.len;
22132 } else {
22133 ir_add_error(ira, mask,
22134 buf_sprintf("expected vector or array, found '%s'",
22135 buf_ptr(&mask->value.type->name)));
22125 return ira->codegen->invalid_instruction;22136 return ira->codegen->invalid_instruction;
22126
22127 const char *shuffle_mask_fail_fmt = "@shuffle mask operand must be a vector of signed 32-bit integers, got '%s'";
22128
22129 if (mask_type->id == ZigTypeIdArray) {
22130 ZigType *vector_type = get_vector_type(ira->codegen, mask_type->data.array.len, mask_type->data.array.child_type);
22131 mask = ir_analyze_array_to_vector(ira, mask, mask, vector_type);
22132 if (!mask)
22133 return ira->codegen->invalid_instruction;
22134 mask_type = vector_type;
22135 }22137 }
22138 mask = ir_implicit_cast(ira, mask, get_vector_type(ira->codegen, len_mask,
22139 ira->codegen->builtin_types.entry_i32));
22140 if (type_is_invalid(mask->value.type))
22141 return ira->codegen->invalid_instruction;
2213622142
22137 if (mask_type->id != ZigTypeIdVector) {22143 uint32_t len_a;
22138 ir_add_error(ira, mask,22144 if (a->value.type->id == ZigTypeIdVector) {
22139 buf_sprintf(shuffle_mask_fail_fmt, buf_ptr(&mask->value.type->name)));22145 len_a = a->value.type->data.vector.len;
22146 } else if (a->value.type->id == ZigTypeIdArray) {
22147 len_a = a->value.type->data.array.len;
22148 } else if (a->value.type->id == ZigTypeIdUndefined) {
22149 len_a = UINT32_MAX;
22150 } else {
22151 ir_add_error(ira, a,
22152 buf_sprintf("expected vector or array with element type '%s', found '%s'",
22153 buf_ptr(&scalar_type->name),
22154 buf_ptr(&a->value.type->name)));
22140 return ira->codegen->invalid_instruction;22155 return ira->codegen->invalid_instruction;
22141 }22156 }
2214222157
22143 ZigType *mask_scalar_type = mask_type->data.array.child_type;22158 uint32_t len_b;
22144 if (mask_scalar_type->id != ZigTypeIdInt) {22159 if (b->value.type->id == ZigTypeIdVector) {
22145 ir_add_error(ira, mask,22160 len_b = b->value.type->data.vector.len;
22146 buf_sprintf(shuffle_mask_fail_fmt, buf_ptr(&mask->value.type->name)));22161 } else if (b->value.type->id == ZigTypeIdArray) {
22162 len_b = b->value.type->data.array.len;
22163 } else if (b->value.type->id == ZigTypeIdUndefined) {
22164 len_b = UINT32_MAX;
22165 } else {
22166 ir_add_error(ira, b,
22167 buf_sprintf("expected vector or array with element type '%s', found '%s'",
22168 buf_ptr(&scalar_type->name),
22169 buf_ptr(&b->value.type->name)));
22147 return ira->codegen->invalid_instruction;22170 return ira->codegen->invalid_instruction;
22148 }22171 }
2214922172
22150 if (mask_scalar_type->data.integral.bit_count != 32 ||22173 if (len_a == UINT32_MAX && len_b == UINT32_MAX) {
22151 mask_scalar_type->data.integral.is_signed == false) {22174 return ir_const_undef(ira, a, get_vector_type(ira->codegen, len_mask, scalar_type));
22152 ir_add_error(ira, mask,
22153 buf_sprintf(shuffle_mask_fail_fmt, buf_ptr(&mask->value.type->name)));
22154 return ira->codegen->invalid_instruction;
22155 }22175 }
2215622176
22157 uint64_t len_a, len_b, len_c = mask->value.type->data.vector.len;22177 if (len_a == UINT32_MAX) {
22158 if (a->value.type->id != ZigTypeIdVector) {22178 len_a = len_b;
22159 if (a->value.type->id != ZigTypeIdUndefined) {22179 a = ir_const_undef(ira, a, get_vector_type(ira->codegen, len_a, scalar_type));
22160 ir_add_error(ira, a,22180 } else {
22161 buf_sprintf("expected vector of element type '%s' got '%s'",22181 a = ir_implicit_cast(ira, a, get_vector_type(ira->codegen, len_a, scalar_type));
22162 buf_ptr(&scalar_type->name),22182 if (type_is_invalid(a->value.type))
22163 buf_ptr(&a->value.type->name)));
22164 return ira->codegen->invalid_instruction;22183 return ira->codegen->invalid_instruction;
22165 }22184 }
22185
22186 if (len_b == UINT32_MAX) {
22187 len_b = len_a;
22188 b = ir_const_undef(ira, b, get_vector_type(ira->codegen, len_b, scalar_type));
22166 } else {22189 } else {
22167 len_a = a->value.type->data.vector.len;22190 b = ir_implicit_cast(ira, b, get_vector_type(ira->codegen, len_b, scalar_type));
22191 if (type_is_invalid(b->value.type))
22192 return ira->codegen->invalid_instruction;
22168 }22193 }
2216922194
22170 if (b->value.type->id != ZigTypeIdVector) {22195 ConstExprValue *mask_val = ir_resolve_const(ira, mask, UndefOk);
22171 if (b->value.type->id != ZigTypeIdUndefined) {22196 if (mask_val == nullptr)
22172 ir_add_error(ira, b,22197 return ira->codegen->invalid_instruction;
22173 buf_sprintf("expected vector of element type '%s' got '%s'",22198
22174 buf_ptr(&scalar_type->name),22199 expand_undef_array(ira->codegen, mask_val);
22175 buf_ptr(&b->value.type->name)));22200
22201 for (uint32_t i = 0; i < len_mask; i += 1) {
22202 ConstExprValue *mask_elem_val = &mask_val->data.x_array.data.s_none.elements[i];
22203 if (mask_elem_val->special == ConstValSpecialUndef)
22204 continue;
22205 int32_t v_i32 = bigint_as_signed(&mask_elem_val->data.x_bigint);
22206 uint32_t v;
22207 IrInstruction *chosen_operand;
22208 if (v_i32 >= 0) {
22209 v = (uint32_t)v_i32;
22210 chosen_operand = a;
22211 } else {
22212 v = (uint32_t)~v_i32;
22213 chosen_operand = b;
22214 }
22215 if (v >= chosen_operand->value.type->data.vector.len) {
22216 ErrorMsg *msg = ir_add_error(ira, mask,
22217 buf_sprintf("mask index '%u' has out-of-bounds selection", i));
22218 add_error_note(ira->codegen, msg, chosen_operand->source_node,
22219 buf_sprintf("selected index '%u' out of bounds of %s", v,
22220 buf_ptr(&chosen_operand->value.type->name)));
22221 if (chosen_operand == a && v < len_a + len_b) {
22222 add_error_note(ira->codegen, msg, b->source_node,
22223 buf_create_from_str("selections from the second vector are specified with negative numbers"));
22224 }
22176 return ira->codegen->invalid_instruction;22225 return ira->codegen->invalid_instruction;
22177 }22226 }
22178 } else {
22179 len_b = b->value.type->data.vector.len;
22180 }22227 }
2218122228
22182 if (a->value.type->id == ZigTypeIdUndefined && b->value.type->id == ZigTypeIdUndefined) {22229 ZigType *result_type = get_vector_type(ira->codegen, len_mask, scalar_type);
22183 return ir_const_undef(ira, a, get_vector_type(ira->codegen, len_c, scalar_type));22230 if (instr_is_comptime(a) && instr_is_comptime(b)) {
22184 }22231 ConstExprValue *a_val = ir_resolve_const(ira, a, UndefOk);
22232 if (a_val == nullptr)
22233 return ira->codegen->invalid_instruction;
2218522234
22186 // undefined is a vector up to length of the other vector.22235 ConstExprValue *b_val = ir_resolve_const(ira, b, UndefOk);
22187 if (a->value.type->id == ZigTypeIdUndefined) {22236 if (b_val == nullptr)
22188 a = ir_const_undef(ira, a, b->value.type);22237 return ira->codegen->invalid_instruction;
22189 len_a = b->value.type->data.vector.len;
22190 } else if (b->value.type->id == ZigTypeIdUndefined) {
22191 b = ir_const_undef(ira, b, a->value.type);
22192 len_b = a->value.type->data.vector.len;
22193 }
2219422238
22195 // FIXME I think this needs to be more sophisticated22239 expand_undef_array(ira->codegen, a_val);
22196 if (a->value.type->data.vector.elem_type != scalar_type) {22240 expand_undef_array(ira->codegen, b_val);
22197 ir_add_error(ira, a,22241
22198 buf_sprintf("element type '%s' does not match '%s'",22242 IrInstruction *result = ir_const(ira, source_instr, result_type);
22199 buf_ptr(&a->value.type->data.vector.elem_type->name),22243 result->value.data.x_array.data.s_none.elements = create_const_vals(len_mask);
22200 buf_ptr(&scalar_type->name)));22244 for (uint32_t i = 0; i < mask_val->type->data.vector.len; i += 1) {
22201 return ira->codegen->invalid_instruction;22245 ConstExprValue *mask_elem_val = &mask_val->data.x_array.data.s_none.elements[i];
22202 }22246 ConstExprValue *result_elem_val = &result->value.data.x_array.data.s_none.elements[i];
22203 if (b->value.type->data.vector.elem_type != scalar_type) {22247 if (mask_elem_val->special == ConstValSpecialUndef) {
22204 ir_add_error(ira, b,22248 result_elem_val->special = ConstValSpecialUndef;
22205 buf_sprintf("element type '%s' does not match '%s'",22249 continue;
22206 buf_ptr(&b->value.type->data.vector.elem_type->name),22250 }
22207 buf_ptr(&scalar_type->name)));22251 int32_t v = bigint_as_signed(&mask_elem_val->data.x_bigint);
22208 return ira->codegen->invalid_instruction;22252 // We've already checked for and emitted compile errors for index out of bounds here.
22253 ConstExprValue *src_elem_val = (v >= 0) ?
22254 &a->value.data.x_array.data.s_none.elements[v] :
22255 &b->value.data.x_array.data.s_none.elements[~v];
22256 copy_const_val(result_elem_val, src_elem_val, false);
22257
22258 ir_assert(result_elem_val->special == ConstValSpecialStatic, source_instr);
22259 }
22260 result->value.special = ConstValSpecialStatic;
22261 return result;
22209 }22262 }
2221022263
22211 if (a->value.type != b->value.type) {22264 // All static analysis passed, and not comptime.
22212 assert(len_a != len_b);22265 // For runtime codegen, vectors a and b must be the same length. Here we
22213 uint32_t len_max = max(len_a, len_b), len_min = min(len_a, len_b);22266 // recursively @shuffle the smaller vector to append undefined elements
22214 bool expand_b = len_b < len_a;22267 // to it up to the length of the longer vector. This recursion terminates
22268 // in 1 call because these calls to ir_analyze_shuffle_vector guarantee
22269 // len_a == len_b.
22270 if (len_a != len_b) {
22271 uint32_t len_min = min(len_a, len_b);
22272 uint32_t len_max = max(len_a, len_b);
22273
22215 IrInstruction *expand_mask = ir_const(ira, mask,22274 IrInstruction *expand_mask = ir_const(ira, mask,
22216 get_vector_type(ira->codegen, len_max, ira->codegen->builtin_types.entry_i32));22275 get_vector_type(ira->codegen, len_max, ira->codegen->builtin_types.entry_i32));
22217 expand_mask->value.data.x_array.data.s_none.elements = create_const_vals(len_max);22276 expand_mask->value.data.x_array.data.s_none.elements = create_const_vals(len_max);
22218 uint32_t i = 0;22277 uint32_t i = 0;
22219 for (; i < len_min; i++)22278 for (; i < len_min; i += 1)
22220 bigint_init_unsigned(&expand_mask->value.data.x_array.data.s_none.elements[i].data.x_bigint, i);22279 bigint_init_unsigned(&expand_mask->value.data.x_array.data.s_none.elements[i].data.x_bigint, i);
22221 for (; i < len_max; i++)22280 for (; i < len_max; i += 1)
22222 bigint_init_signed(&expand_mask->value.data.x_array.data.s_none.elements[i].data.x_bigint, -1);22281 bigint_init_signed(&expand_mask->value.data.x_array.data.s_none.elements[i].data.x_bigint, -1);
22282
22223 IrInstruction *undef = ir_const_undef(ira, source_instr,22283 IrInstruction *undef = ir_const_undef(ira, source_instr,
22224 get_vector_type(ira->codegen, len_min, scalar_type));22284 get_vector_type(ira->codegen, len_min, scalar_type));
22225 if (expand_b) {
22226 if (instr_is_comptime(b)) {
22227 ConstExprValue *old = b->value.data.x_array.data.s_none.elements;
22228 b->value.data.x_array.data.s_none.elements =
22229 allocate<ConstExprValue>(len_a);
22230 memcpy(b->value.data.x_array.data.s_none.elements, old,
22231 b->value.type->data.vector.len * sizeof(ConstExprValue));
22232 } else {
22233 b = ir_build_shuffle_vector(&ira->new_irb,
22234 source_instr->scope, source_instr->source_node,
22235 nullptr, b, undef, expand_mask);
22236 b->value.special = ConstValSpecialRuntime;
22237 }
22238 b->value.type = get_vector_type(ira->codegen, len_max, scalar_type);
22239 } else {
22240 if (instr_is_comptime(a)) {
22241 ConstExprValue *old = a->value.data.x_array.data.s_none.elements;
22242 a->value.data.x_array.data.s_none.elements =
22243 allocate<ConstExprValue>(len_b);
22244 memcpy(a->value.data.x_array.data.s_none.elements, old,
22245 a->value.type->data.vector.len * sizeof(ConstExprValue));
22246 } else {
22247 a = ir_build_shuffle_vector(&ira->new_irb,
22248 source_instr->scope, source_instr->source_node,
22249 nullptr, a, undef, expand_mask);
22250 a->value.special = ConstValSpecialRuntime;
22251 }
22252 a->value.type = get_vector_type(ira->codegen, len_max, scalar_type);
22253 }
22254 }
22255 ConstExprValue *mask_val = ir_resolve_const(ira, mask, UndefOk);
22256 if (!mask_val) {
22257 ir_add_error(ira, mask,
22258 buf_sprintf("mask must be comptime"));
22259 return ira->codegen->invalid_instruction;
22260 }
22261 for (uint32_t i = 0;i < mask->value.type->data.vector.len;i++) {
22262 if (mask->value.data.x_array.data.s_none.elements[i].special == ConstValSpecialUndef)
22263 continue;
22264 int64_t v = bigint_as_signed(&mask->value.data.x_array.data.s_none.elements[i].data.x_bigint);
22265 if (v >= 0 && (uint64_t)v + 1 > len_a) {
22266 ErrorMsg *msg = ir_add_error(ira, mask,
22267 buf_sprintf("mask index out of bounds"));
22268 add_error_note(ira->codegen, msg, mask->source_node,
22269 buf_sprintf("when computing vector element at index %" ZIG_PRI_usize, (uintptr_t)i));
22270 if ((uint64_t)v <= len_a + len_b)
22271 add_error_note(ira->codegen, msg, mask->source_node,
22272 buf_sprintf("selections from the second vector are specified with negative numbers"));
22273 } else if (v < 0 && (uint64_t)~v + 1 > len_b) {
22274 ErrorMsg *msg = ir_add_error(ira, mask,
22275 buf_sprintf("mask index out of bounds"));
22276 add_error_note(ira->codegen, msg, mask->source_node,
22277 buf_sprintf("when computing vector element at index %" ZIG_PRI_usize, (uintptr_t)i));
22278 }
22279 else
22280 continue;
22281 return ira->codegen->invalid_instruction;
22282 }
2228322285
22284 ZigType *result_type = get_vector_type(ira->codegen, len_c, scalar_type);22286 if (len_b < len_a) {
22285 if (instr_is_comptime(a) &&22287 b = ir_analyze_shuffle_vector(ira, source_instr, scalar_type, b, undef, expand_mask);
22286 instr_is_comptime(b)) {22288 } else {
22287 IrInstruction *result = ir_const(ira, source_instr, result_type);22289 a = ir_analyze_shuffle_vector(ira, source_instr, scalar_type, a, undef, expand_mask);
22288 result->value.data.x_array.data.s_none.elements = create_const_vals(len_c);
22289 for (uint32_t i = 0;i < mask->value.type->data.vector.len;i++) {
22290 if (mask->value.data.x_array.data.s_none.elements[i].special == ConstValSpecialUndef)
22291 result->value.data.x_array.data.s_none.elements[i].special =
22292 ConstValSpecialUndef;
22293 int64_t v = bigint_as_signed(&mask->value.data.x_array.data.s_none.elements[i].data.x_bigint);
22294 if (v >= 0)
22295 result->value.data.x_array.data.s_none.elements[i] =
22296 a->value.data.x_array.data.s_none.elements[v];
22297 else if (v < 0)
22298 result->value.data.x_array.data.s_none.elements[i] =
22299 b->value.data.x_array.data.s_none.elements[~v];
22300 else
22301 zig_unreachable();
22302 result->value.data.x_array.data.s_none.elements[i].special =
22303 ConstValSpecialStatic;
22304 }22290 }
22305 result->value.special = ConstValSpecialStatic;
22306 return result;
22307 }22291 }
2230822292
22309 // All static analysis passed, and not comptime
22310 IrInstruction *result = ir_build_shuffle_vector(&ira->new_irb,22293 IrInstruction *result = ir_build_shuffle_vector(&ira->new_irb,
22311 source_instr->scope, source_instr->source_node,22294 source_instr->scope, source_instr->source_node,
22312 nullptr, a, b, mask);22295 nullptr, a, b, mask);
22313 result->value.type = result_type;22296 result->value.type = result_type;
22314 result->value.special = ConstValSpecialRuntime;
22315 return result;22297 return result;
22316}22298}
2231722299
22318static IrInstruction *ir_analyze_instruction_shuffle_vector(IrAnalyze *ira, IrInstructionShuffleVector *instruction) {22300static IrInstruction *ir_analyze_instruction_shuffle_vector(IrAnalyze *ira, IrInstructionShuffleVector *instruction) {
22319 ZigType *scalar_type = ir_resolve_type(ira, instruction->scalar_type);22301 ZigType *scalar_type = ir_resolve_vector_elem_type(ira, instruction->scalar_type);
22320 assert(scalar_type);
22321 if (type_is_invalid(scalar_type))22302 if (type_is_invalid(scalar_type))
22322 return ira->codegen->invalid_instruction;22303 return ira->codegen->invalid_instruction;
2232322304
22324 if (scalar_type->id != ZigTypeIdBool &&22305 IrInstruction *a = instruction->a->child;
22325 scalar_type->id != ZigTypeIdInt &&22306 if (type_is_invalid(a->value.type))
22326 scalar_type->id != ZigTypeIdFloat &&22307 return ira->codegen->invalid_instruction;
22327 scalar_type->id != ZigTypeIdPointer) {22308
22328 ir_add_error(ira, instruction->scalar_type,22309 IrInstruction *b = instruction->b->child;
22329 buf_sprintf("vector element type must be integer, float, bool, or pointer; '%s' is invalid",22310 if (type_is_invalid(b->value.type))
22330 buf_ptr(&scalar_type->name)));22311 return ira->codegen->invalid_instruction;
22312
22313 IrInstruction *mask = instruction->mask->child;
22314 if (type_is_invalid(mask->value.type))
22331 return ira->codegen->invalid_instruction;22315 return ira->codegen->invalid_instruction;
22332 }
2233322316
22334 return ir_analyze_shuffle_vector(ira, &instruction->base, scalar_type, instruction->a->child, instruction->b->child, instruction->mask->child);22317 return ir_analyze_shuffle_vector(ira, &instruction->base, scalar_type, a, b, mask);
22335}22318}
2233622319
22337static IrInstruction *ir_analyze_instruction_bool_not(IrAnalyze *ira, IrInstructionBoolNot *instruction) {22320static IrInstruction *ir_analyze_instruction_bool_not(IrAnalyze *ira, IrInstructionBoolNot *instruction) {
test/compile_errors.zig+7-7
...@@ -6485,16 +6485,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -6485,16 +6485,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
6485 );6485 );
64866486
6487 cases.addTest(6487 cases.addTest(
6488 "using LLVM syntax for @shuffle",6488 "@shuffle with selected index past first vector length",
6489 \\export fn entry() void {6489 \\export fn entry() void {
6490 \\ const v: @Vector(4, u32) = [4]u32{0, 1, 2, 3};6490 \\ const v: @Vector(4, u32) = [4]u32{ 10, 11, 12, 13 };
6491 \\ const x: @Vector(4, u32) = [4]u32{4, 5, 6, 7};6491 \\ const x: @Vector(4, u32) = [4]u32{ 14, 15, 16, 17 };
6492 \\ var z = @shuffle(u32, v, x, [8]i32{0, 1, 2, 3, 4, 5, 6, 7});6492 \\ var z = @shuffle(u32, v, x, [8]i32{ 0, 1, 2, 3, 7, 6, 5, 4 });
6493 \\}6493 \\}
6494 ,6494 ,
6495 "tmp.zig:4:39: error: mask index out of bounds",6495 "tmp.zig:4:39: error: mask index '4' has out-of-bounds selection",
6496 "tmp.zig:4:39: note: when computing vector element at index 4",6496 "tmp.zig:4:27: note: selected index '7' out of bounds of @Vector(4, u32)",
6497 "tmp.zig:4:39: note: selections from the second vector are specified with negative numbers",6497 "tmp.zig:4:30: note: selections from the second vector are specified with negative numbers",
6498 );6498 );
64996499
6500 cases.addTest(6500 cases.addTest(
test/stage1/behavior.zig+1
...@@ -80,6 +80,7 @@ comptime {...@@ -80,6 +80,7 @@ comptime {
80 _ = @import("behavior/pub_enum.zig");80 _ = @import("behavior/pub_enum.zig");
81 _ = @import("behavior/ref_var_in_if_after_if_2nd_switch_prong.zig");81 _ = @import("behavior/ref_var_in_if_after_if_2nd_switch_prong.zig");
82 _ = @import("behavior/reflection.zig");82 _ = @import("behavior/reflection.zig");
83 _ = @import("behavior/shuffle.zig");
83 _ = @import("behavior/sizeof_and_typeof.zig");84 _ = @import("behavior/sizeof_and_typeof.zig");
84 _ = @import("behavior/slice.zig");85 _ = @import("behavior/slice.zig");
85 _ = @import("behavior/slicetobytes.zig");86 _ = @import("behavior/slicetobytes.zig");
test/stage1/behavior/shuffle.zig+16-16
...@@ -7,46 +7,46 @@ test "@shuffle" {...@@ -7,46 +7,46 @@ test "@shuffle" {
7 fn doTheTest() void {7 fn doTheTest() void {
8 var v: @Vector(4, i32) = [4]i32{ 2147483647, -2, 30, 40 };8 var v: @Vector(4, i32) = [4]i32{ 2147483647, -2, 30, 40 };
9 var x: @Vector(4, i32) = [4]i32{ 1, 2147483647, 3, 4 };9 var x: @Vector(4, i32) = [4]i32{ 1, 2147483647, 3, 4 };
10 const mask: @Vector(4, i32) = [4]i32{ 0, ~i32(2), 3, ~i32(3)};10 const mask: @Vector(4, i32) = [4]i32{ 0, ~i32(2), 3, ~i32(3) };
11 var res = @shuffle(i32, v, x, mask);11 var res = @shuffle(i32, v, x, mask);
12 expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, 40, 4 }));12 expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, 40, 4 }));
1313
14 // Implicit cast from array (of mask)14 // Implicit cast from array (of mask)
15 res = @shuffle(i32, v, x, [4]i32{ 0, ~i32(2), 3, ~i32(3)});15 res = @shuffle(i32, v, x, [4]i32{ 0, ~i32(2), 3, ~i32(3) });
16 expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, 40, 4 }));16 expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, 40, 4 }));
1717
18 // Undefined18 // Undefined
19 const mask2: @Vector(4, i32) = [4]i32{ 3, 1, 2, 0};19 const mask2: @Vector(4, i32) = [4]i32{ 3, 1, 2, 0 };
20 res = @shuffle(i32, v, undefined, mask2);20 res = @shuffle(i32, v, undefined, mask2);
21 expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 40, -2, 30, 2147483647}));21 expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 40, -2, 30, 2147483647 }));
2222
23 // Upcasting of b23 // Upcasting of b
24 var v2: @Vector(2, i32) = [2]i32{ 2147483647, undefined};24 var v2: @Vector(2, i32) = [2]i32{ 2147483647, undefined };
25 const mask3: @Vector(4, i32) = [4]i32{ ~i32(0), 2, ~i32(0), 3};25 const mask3: @Vector(4, i32) = [4]i32{ ~i32(0), 2, ~i32(0), 3 };
26 res = @shuffle(i32, x, v2, mask3);26 res = @shuffle(i32, x, v2, mask3);
27 expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, 2147483647, 4 }));27 expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, 2147483647, 4 }));
2828
29 // Upcasting of a29 // Upcasting of a
30 var v3: @Vector(2, i32) = [2]i32{ 2147483647, -2};30 var v3: @Vector(2, i32) = [2]i32{ 2147483647, -2 };
31 const mask4: @Vector(4, i32) = [4]i32{ 0, ~i32(2), 1, ~i32(3)};31 const mask4: @Vector(4, i32) = [4]i32{ 0, ~i32(2), 1, ~i32(3) };
32 res = @shuffle(i32, v3, x, mask4);32 res = @shuffle(i32, v3, x, mask4);
33 expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, -2, 4 }));33 expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, -2, 4 }));
3434
35 // bool35 // bool
36 {36 {
37 var x2: @Vector(4, bool) = [4]bool{ false, true, false, true};37 var x2: @Vector(4, bool) = [4]bool{ false, true, false, true };
38 var v4: @Vector(2, bool) = [2]bool{ true, false};38 var v4: @Vector(2, bool) = [2]bool{ true, false };
39 const mask5: @Vector(4, i32) = [4]i32{ 0, ~i32(1), 1, 2};39 const mask5: @Vector(4, i32) = [4]i32{ 0, ~i32(1), 1, 2 };
40 var res2 = @shuffle(bool, x2, v4, mask5);40 var res2 = @shuffle(bool, x2, v4, mask5);
41 expect(mem.eql(bool, ([4]bool)(res2), [4]bool{ false, false, true, false }));41 expect(mem.eql(bool, ([4]bool)(res2), [4]bool{ false, false, true, false }));
42 }42 }
4343
44 // FIXME re-enable when LLVM codegen is fixed44 // TODO re-enable when LLVM codegen is fixed
45 // https://bugs.llvm.org/show_bug.cgi?id=4280345 // https://github.com/ziglang/zig/issues/3246
46 if (false) {46 if (false) {
47 var x2: @Vector(3, bool) = [3]bool{ false, true, false};47 var x2: @Vector(3, bool) = [3]bool{ false, true, false };
48 var v4: @Vector(2, bool) = [2]bool{ true, false};48 var v4: @Vector(2, bool) = [2]bool{ true, false };
49 const mask5: @Vector(4, i32) = [4]i32{ 0, ~i32(1), 1, 2};49 const mask5: @Vector(4, i32) = [4]i32{ 0, ~i32(1), 1, 2 };
50 var res2 = @shuffle(bool, x2, v4, mask5);50 var res2 = @shuffle(bool, x2, v4, mask5);
51 expect(mem.eql(bool, ([4]bool)(res2), [4]bool{ false, false, true, false }));51 expect(mem.eql(bool, ([4]bool)(res2), [4]bool{ false, false, true, false }));
52 }52 }