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" {
76737673 {#see_also|@shlExact|@shlWithOverflow#}
76747674 {#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
76767713 {#header_open|@sizeOf#}
76777714 <pre>{#syntax#}@sizeOf(comptime T: type) comptime_int{#endsyntax#}</pre>
76787715 <p>
......@@ -8226,28 +8263,6 @@ fn foo(comptime T: type, ptr: *T) T {
82268263 {#link|pointer|Pointers#}.
82278264 </p>
82288265 {#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#}
82518266 {#header_close#}
82528267
82538268 {#header_open|Build Mode#}
src/codegen.cpp+10-9
......@@ -4583,7 +4583,7 @@ static LLVMValueRef ir_render_ctz(CodeGen *g, IrExecutable *executable, IrInstru
45834583
45844584static LLVMValueRef ir_render_shuffle_vector(CodeGen *g, IrExecutable *executable, IrInstructionShuffleVector *instruction) {
45854585 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
45884588 // LLVM uses integers larger than the length of the first array to
45894589 // index into the second array. This was deemed unnecessarily fragile
......@@ -4591,23 +4591,24 @@ static LLVMValueRef ir_render_shuffle_vector(CodeGen *g, IrExecutable *executabl
45914591 // second vector. These start at -1 and go down, and are easiest to use
45924592 // with the ~ operator. Here we convert between the two formats.
45934593 IrInstruction *mask = instruction->mask;
4594 LLVMValueRef *values = allocate<LLVMValueRef>(len_c);
4595 for (uint64_t i = 0;i < len_c;i++) {
4594 LLVMValueRef *values = allocate<LLVMValueRef>(len_mask);
4595 for (uint64_t i = 0; i < len_mask; i++) {
45964596 if (mask->value.data.x_array.data.s_none.elements[i].special == ConstValSpecialUndef) {
45974597 values[i] = LLVMGetUndef(LLVMInt32Type());
45984598 } else {
4599 int64_t v = bigint_as_signed(&mask->value.data.x_array.data.s_none.elements[i].data.x_bigint);
4600 if (v < 0)
4601 v = (uint32_t)~v + (uint32_t)len_a;
4602 values[i] = LLVMConstInt(LLVMInt32Type(), v, false);
4599 int32_t v = bigint_as_signed(&mask->value.data.x_array.data.s_none.elements[i].data.x_bigint);
4600 uint32_t index_val = (v >= 0) ? (uint32_t)v : (uint32_t)~v + (uint32_t)len_a;
4601 values[i] = LLVMConstInt(LLVMInt32Type(), index_val, false);
46034602 }
46044603 }
46054604
4605 LLVMValueRef llvm_mask_value = LLVMConstVector(values, len_mask);
4606 free(values);
4607
46064608 return LLVMBuildShuffleVector(g->builder,
46074609 ir_llvm_value(g, instruction->a),
46084610 ir_llvm_value(g, instruction->b),
4609 LLVMConstVector(values, len_c),
4610 "");
4611 llvm_mask_value, "");
46114612}
46124613
46134614static 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) {
1104911049 return ir_resolve_const_type(ira->codegen, ira->new_irb.exec, type_value->source_node, val);
1105011050}
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
1105211065static ZigType *ir_resolve_int_type(IrAnalyze *ira, IrInstruction *type_value) {
1105311066 ZigType *ty = ir_resolve_type(ira, type_value);
1105411067 if (type_is_invalid(ty))
......@@ -22096,242 +22109,212 @@ static IrInstruction *ir_analyze_instruction_vector_type(IrAnalyze *ira, IrInstr
2209622109 if (!ir_resolve_unsigned(ira, instruction->len->child, ira->codegen->builtin_types.entry_u32, &len))
2209722110 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);
2210022113 if (type_is_invalid(elem_type))
2210122114 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
2211022116 ZigType *vector_type = get_vector_type(ira->codegen, len, elem_type);
2211122117
2211222118 return ir_const_type(ira, &instruction->base, vector_type);
2211322119}
2211422120
2211522121static IrInstruction *ir_analyze_shuffle_vector(IrAnalyze *ira, IrInstruction *source_instr,
22116 ZigType *scalar_type, IrInstruction *a, IrInstruction *b, IrInstruction *mask) {
22117 assert(source_instr && scalar_type && a && b && mask);
22118 assert(scalar_type->id == ZigTypeIdBool ||
22119 scalar_type->id == ZigTypeIdInt ||
22120 scalar_type->id == ZigTypeIdFloat ||
22121 scalar_type->id == ZigTypeIdPointer);
22122
22123 ZigType *mask_type = mask->value.type;
22124 if (type_is_invalid(mask_type))
22122 ZigType *scalar_type, IrInstruction *a, IrInstruction *b, IrInstruction *mask)
22123{
22124 ir_assert(source_instr && scalar_type && a && b && mask, source_instr);
22125 ir_assert(is_valid_vector_elem_type(scalar_type), source_instr);
22126
22127 uint32_t len_mask;
22128 if (mask->value.type->id == ZigTypeIdVector) {
22129 len_mask = mask->value.type->data.vector.len;
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)));
2212522136 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;
2213522137 }
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) {
22138 ir_add_error(ira, mask,
22139 buf_sprintf(shuffle_mask_fail_fmt, buf_ptr(&mask->value.type->name)));
22143 uint32_t len_a;
22144 if (a->value.type->id == ZigTypeIdVector) {
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)));
2214022155 return ira->codegen->invalid_instruction;
2214122156 }
2214222157
22143 ZigType *mask_scalar_type = mask_type->data.array.child_type;
22144 if (mask_scalar_type->id != ZigTypeIdInt) {
22145 ir_add_error(ira, mask,
22146 buf_sprintf(shuffle_mask_fail_fmt, buf_ptr(&mask->value.type->name)));
22158 uint32_t len_b;
22159 if (b->value.type->id == ZigTypeIdVector) {
22160 len_b = b->value.type->data.vector.len;
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)));
2214722170 return ira->codegen->invalid_instruction;
2214822171 }
2214922172
22150 if (mask_scalar_type->data.integral.bit_count != 32 ||
22151 mask_scalar_type->data.integral.is_signed == false) {
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;
22173 if (len_a == UINT32_MAX && len_b == UINT32_MAX) {
22174 return ir_const_undef(ira, a, get_vector_type(ira->codegen, len_mask, scalar_type));
2215522175 }
2215622176
22157 uint64_t len_a, len_b, len_c = mask->value.type->data.vector.len;
22158 if (a->value.type->id != ZigTypeIdVector) {
22159 if (a->value.type->id != ZigTypeIdUndefined) {
22160 ir_add_error(ira, a,
22161 buf_sprintf("expected vector of element type '%s' got '%s'",
22162 buf_ptr(&scalar_type->name),
22163 buf_ptr(&a->value.type->name)));
22177 if (len_a == UINT32_MAX) {
22178 len_a = len_b;
22179 a = ir_const_undef(ira, a, get_vector_type(ira->codegen, len_a, scalar_type));
22180 } else {
22181 a = ir_implicit_cast(ira, a, get_vector_type(ira->codegen, len_a, scalar_type));
22182 if (type_is_invalid(a->value.type))
2216422183 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));
2216622189 } 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;
2216822193 }
2216922194
22170 if (b->value.type->id != ZigTypeIdVector) {
22171 if (b->value.type->id != ZigTypeIdUndefined) {
22172 ir_add_error(ira, b,
22173 buf_sprintf("expected vector of element type '%s' got '%s'",
22174 buf_ptr(&scalar_type->name),
22175 buf_ptr(&b->value.type->name)));
22195 ConstExprValue *mask_val = ir_resolve_const(ira, mask, UndefOk);
22196 if (mask_val == nullptr)
22197 return ira->codegen->invalid_instruction;
22198
22199 expand_undef_array(ira->codegen, mask_val);
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 }
2217622225 return ira->codegen->invalid_instruction;
2217722226 }
22178 } else {
22179 len_b = b->value.type->data.vector.len;
2218022227 }
2218122228
22182 if (a->value.type->id == ZigTypeIdUndefined && b->value.type->id == ZigTypeIdUndefined) {
22183 return ir_const_undef(ira, a, get_vector_type(ira->codegen, len_c, scalar_type));
22184 }
22229 ZigType *result_type = get_vector_type(ira->codegen, len_mask, scalar_type);
22230 if (instr_is_comptime(a) && instr_is_comptime(b)) {
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.
22187 if (a->value.type->id == ZigTypeIdUndefined) {
22188 a = ir_const_undef(ira, a, b->value.type);
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 }
22235 ConstExprValue *b_val = ir_resolve_const(ira, b, UndefOk);
22236 if (b_val == nullptr)
22237 return ira->codegen->invalid_instruction;
2219422238
22195 // FIXME I think this needs to be more sophisticated
22196 if (a->value.type->data.vector.elem_type != scalar_type) {
22197 ir_add_error(ira, a,
22198 buf_sprintf("element type '%s' does not match '%s'",
22199 buf_ptr(&a->value.type->data.vector.elem_type->name),
22200 buf_ptr(&scalar_type->name)));
22201 return ira->codegen->invalid_instruction;
22202 }
22203 if (b->value.type->data.vector.elem_type != scalar_type) {
22204 ir_add_error(ira, b,
22205 buf_sprintf("element type '%s' does not match '%s'",
22206 buf_ptr(&b->value.type->data.vector.elem_type->name),
22207 buf_ptr(&scalar_type->name)));
22208 return ira->codegen->invalid_instruction;
22239 expand_undef_array(ira->codegen, a_val);
22240 expand_undef_array(ira->codegen, b_val);
22241
22242 IrInstruction *result = ir_const(ira, source_instr, result_type);
22243 result->value.data.x_array.data.s_none.elements = create_const_vals(len_mask);
22244 for (uint32_t i = 0; i < mask_val->type->data.vector.len; i += 1) {
22245 ConstExprValue *mask_elem_val = &mask_val->data.x_array.data.s_none.elements[i];
22246 ConstExprValue *result_elem_val = &result->value.data.x_array.data.s_none.elements[i];
22247 if (mask_elem_val->special == ConstValSpecialUndef) {
22248 result_elem_val->special = ConstValSpecialUndef;
22249 continue;
22250 }
22251 int32_t v = bigint_as_signed(&mask_elem_val->data.x_bigint);
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;
2220922262 }
2221022263
22211 if (a->value.type != b->value.type) {
22212 assert(len_a != len_b);
22213 uint32_t len_max = max(len_a, len_b), len_min = min(len_a, len_b);
22214 bool expand_b = len_b < len_a;
22264 // All static analysis passed, and not comptime.
22265 // For runtime codegen, vectors a and b must be the same length. Here we
22266 // recursively @shuffle the smaller vector to append undefined elements
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
2221522274 IrInstruction *expand_mask = ir_const(ira, mask,
2221622275 get_vector_type(ira->codegen, len_max, ira->codegen->builtin_types.entry_i32));
2221722276 expand_mask->value.data.x_array.data.s_none.elements = create_const_vals(len_max);
2221822277 uint32_t i = 0;
22219 for (; i < len_min; i++)
22278 for (; i < len_min; i += 1)
2222022279 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)
2222222281 bigint_init_signed(&expand_mask->value.data.x_array.data.s_none.elements[i].data.x_bigint, -1);
22282
2222322283 IrInstruction *undef = ir_const_undef(ira, source_instr,
2222422284 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);
22285 if (instr_is_comptime(a) &&
22286 instr_is_comptime(b)) {
22287 IrInstruction *result = ir_const(ira, source_instr, result_type);
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;
22286 if (len_b < len_a) {
22287 b = ir_analyze_shuffle_vector(ira, source_instr, scalar_type, b, undef, expand_mask);
22288 } else {
22289 a = ir_analyze_shuffle_vector(ira, source_instr, scalar_type, a, undef, expand_mask);
2230422290 }
22305 result->value.special = ConstValSpecialStatic;
22306 return result;
2230722291 }
2230822292
22309 // All static analysis passed, and not comptime
2231022293 IrInstruction *result = ir_build_shuffle_vector(&ira->new_irb,
2231122294 source_instr->scope, source_instr->source_node,
2231222295 nullptr, a, b, mask);
2231322296 result->value.type = result_type;
22314 result->value.special = ConstValSpecialRuntime;
2231522297 return result;
2231622298}
2231722299
2231822300static IrInstruction *ir_analyze_instruction_shuffle_vector(IrAnalyze *ira, IrInstructionShuffleVector *instruction) {
22319 ZigType *scalar_type = ir_resolve_type(ira, instruction->scalar_type);
22320 assert(scalar_type);
22301 ZigType *scalar_type = ir_resolve_vector_elem_type(ira, instruction->scalar_type);
2232122302 if (type_is_invalid(scalar_type))
2232222303 return ira->codegen->invalid_instruction;
2232322304
22324 if (scalar_type->id != ZigTypeIdBool &&
22325 scalar_type->id != ZigTypeIdInt &&
22326 scalar_type->id != ZigTypeIdFloat &&
22327 scalar_type->id != ZigTypeIdPointer) {
22328 ir_add_error(ira, instruction->scalar_type,
22329 buf_sprintf("vector element type must be integer, float, bool, or pointer; '%s' is invalid",
22330 buf_ptr(&scalar_type->name)));
22305 IrInstruction *a = instruction->a->child;
22306 if (type_is_invalid(a->value.type))
22307 return ira->codegen->invalid_instruction;
22308
22309 IrInstruction *b = instruction->b->child;
22310 if (type_is_invalid(b->value.type))
22311 return ira->codegen->invalid_instruction;
22312
22313 IrInstruction *mask = instruction->mask->child;
22314 if (type_is_invalid(mask->value.type))
2233122315 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);
2233522318}
2233622319
2233722320static 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 {
64856485 );
64866486
64876487 cases.addTest(
6488 "using LLVM syntax for @shuffle",
6488 "@shuffle with selected index past first vector length",
64896489 \\export fn entry() void {
6490 \\ const v: @Vector(4, u32) = [4]u32{0, 1, 2, 3};
6491 \\ const x: @Vector(4, u32) = [4]u32{4, 5, 6, 7};
6492 \\ var z = @shuffle(u32, v, x, [8]i32{0, 1, 2, 3, 4, 5, 6, 7});
6490 \\ const v: @Vector(4, u32) = [4]u32{ 10, 11, 12, 13 };
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, 7, 6, 5, 4 });
64936493 \\}
64946494 ,
6495 "tmp.zig:4:39: error: mask index out of bounds",
6496 "tmp.zig:4:39: note: when computing vector element at index 4",
6497 "tmp.zig:4:39: note: selections from the second vector are specified with negative numbers",
6495 "tmp.zig:4:39: error: mask index '4' has out-of-bounds selection",
6496 "tmp.zig:4:27: note: selected index '7' out of bounds of @Vector(4, u32)",
6497 "tmp.zig:4:30: note: selections from the second vector are specified with negative numbers",
64986498 );
64996499
65006500 cases.addTest(
test/stage1/behavior.zig+1
......@@ -80,6 +80,7 @@ comptime {
8080 _ = @import("behavior/pub_enum.zig");
8181 _ = @import("behavior/ref_var_in_if_after_if_2nd_switch_prong.zig");
8282 _ = @import("behavior/reflection.zig");
83 _ = @import("behavior/shuffle.zig");
8384 _ = @import("behavior/sizeof_and_typeof.zig");
8485 _ = @import("behavior/slice.zig");
8586 _ = @import("behavior/slicetobytes.zig");
test/stage1/behavior/shuffle.zig+16-16
......@@ -7,46 +7,46 @@ test "@shuffle" {
77 fn doTheTest() void {
88 var v: @Vector(4, i32) = [4]i32{ 2147483647, -2, 30, 40 };
99 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) };
1111 var res = @shuffle(i32, v, x, mask);
1212 expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, 40, 4 }));
1313
1414 // 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) });
1616 expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, 40, 4 }));
1717
1818 // 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 };
2020 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
2323 // Upcasting of b
24 var v2: @Vector(2, i32) = [2]i32{ 2147483647, undefined};
25 const mask3: @Vector(4, i32) = [4]i32{ ~i32(0), 2, ~i32(0), 3};
24 var v2: @Vector(2, i32) = [2]i32{ 2147483647, undefined };
25 const mask3: @Vector(4, i32) = [4]i32{ ~i32(0), 2, ~i32(0), 3 };
2626 res = @shuffle(i32, x, v2, mask3);
2727 expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, 2147483647, 4 }));
2828
2929 // Upcasting of a
30 var v3: @Vector(2, i32) = [2]i32{ 2147483647, -2};
31 const mask4: @Vector(4, i32) = [4]i32{ 0, ~i32(2), 1, ~i32(3)};
30 var v3: @Vector(2, i32) = [2]i32{ 2147483647, -2 };
31 const mask4: @Vector(4, i32) = [4]i32{ 0, ~i32(2), 1, ~i32(3) };
3232 res = @shuffle(i32, v3, x, mask4);
3333 expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, -2, 4 }));
3434
3535 // bool
3636 {
37 var x2: @Vector(4, bool) = [4]bool{ false, true, false, true};
38 var v4: @Vector(2, bool) = [2]bool{ true, false};
39 const mask5: @Vector(4, i32) = [4]i32{ 0, ~i32(1), 1, 2};
37 var x2: @Vector(4, bool) = [4]bool{ false, true, false, true };
38 var v4: @Vector(2, bool) = [2]bool{ true, false };
39 const mask5: @Vector(4, i32) = [4]i32{ 0, ~i32(1), 1, 2 };
4040 var res2 = @shuffle(bool, x2, v4, mask5);
4141 expect(mem.eql(bool, ([4]bool)(res2), [4]bool{ false, false, true, false }));
4242 }
4343
44 // FIXME re-enable when LLVM codegen is fixed
45 // https://bugs.llvm.org/show_bug.cgi?id=42803
44 // TODO re-enable when LLVM codegen is fixed
45 // https://github.com/ziglang/zig/issues/3246
4646 if (false) {
47 var x2: @Vector(3, bool) = [3]bool{ false, 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};
47 var x2: @Vector(3, bool) = [3]bool{ false, 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 };
5050 var res2 = @shuffle(bool, x2, v4, mask5);
5151 expect(mem.eql(bool, ([4]bool)(res2), [4]bool{ false, false, true, false }));
5252 }