authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-05 13:39:56-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-05 13:40:57-05:00
logcb8af1c6d48d5fdb26484a070b8af069c4152049
tree0c3baee6423b40eda3ba57e9d41041f6c9fbd3b8
parenta26e9fa7238e972301f761db80434fa0205d1016
parent9a08de93b64a332af9f581ef15b208e153fa19f0
signaturelock-open Commit is signed but in an unrecognized format.

Merge branch 'vector-element-access'

This introduces the concept of vector index being part of a pointer type. This avoids vectors having well-defined in-memory layout, and allows vectors of any integer bit width to work the same way. When a vector is indexed with a scalar, this is vector element access, which this branch implements. When a vector is indexed with a vector, this is gather/scatter, which is not implemented in this branch. closes #3575 closes #3580

8 files changed, 422 insertions(+), 41 deletions(-)

src/all_types.hpp+30-1
......@@ -1183,13 +1183,22 @@ struct FnTypeId {
11831183uint32_t fn_type_id_hash(FnTypeId*);
11841184bool fn_type_id_eql(FnTypeId *a, FnTypeId *b);
11851185
1186static const uint32_t VECTOR_INDEX_NONE = UINT32_MAX;
1187static const uint32_t VECTOR_INDEX_RUNTIME = UINT32_MAX - 1;
1188
11861189struct ZigTypePointer {
11871190 ZigType *child_type;
11881191 ZigType *slice_parent;
1192
11891193 PtrLen ptr_len;
11901194 uint32_t explicit_alignment; // 0 means use ABI alignment
1195
11911196 uint32_t bit_offset_in_host;
1192 uint32_t host_int_bytes; // size of host integer. 0 means no host integer; this field is aligned
1197 // size of host integer. 0 means no host integer; this field is aligned
1198 // when vector_index != VECTOR_INDEX_NONE this is the len of the containing vector
1199 uint32_t host_int_bytes;
1200
1201 uint32_t vector_index; // see the VECTOR_INDEX_* constants
11931202 bool is_const;
11941203 bool is_volatile;
11951204 bool allow_zero;
......@@ -1732,8 +1741,11 @@ struct TypeId {
17321741 ZigType *child_type;
17331742 PtrLen ptr_len;
17341743 uint32_t alignment;
1744
17351745 uint32_t bit_offset_in_host;
17361746 uint32_t host_int_bytes;
1747
1748 uint32_t vector_index;
17371749 bool is_const;
17381750 bool is_volatile;
17391751 bool allow_zero;
......@@ -2414,6 +2426,7 @@ enum IrInstructionId {
24142426 IrInstructionIdLoadPtr,
24152427 IrInstructionIdLoadPtrGen,
24162428 IrInstructionIdStorePtr,
2429 IrInstructionIdVectorStoreElem,
24172430 IrInstructionIdFieldPtr,
24182431 IrInstructionIdStructFieldPtr,
24192432 IrInstructionIdUnionFieldPtr,
......@@ -2563,6 +2576,7 @@ enum IrInstructionId {
25632576 IrInstructionIdResume,
25642577 IrInstructionIdSpillBegin,
25652578 IrInstructionIdSpillEnd,
2579 IrInstructionIdVectorExtractElem,
25662580};
25672581
25682582struct IrInstruction {
......@@ -2757,6 +2771,14 @@ struct IrInstructionStorePtr {
27572771 IrInstruction *value;
27582772};
27592773
2774struct IrInstructionVectorStoreElem {
2775 IrInstruction base;
2776
2777 IrInstruction *vector_ptr;
2778 IrInstruction *index;
2779 IrInstruction *value;
2780};
2781
27602782struct IrInstructionFieldPtr {
27612783 IrInstruction base;
27622784
......@@ -3890,6 +3912,13 @@ struct IrInstructionSpillEnd {
38903912 IrInstructionSpillBegin *begin;
38913913};
38923914
3915struct IrInstructionVectorExtractElem {
3916 IrInstruction base;
3917
3918 IrInstruction *vector;
3919 IrInstruction *index;
3920};
3921
38933922enum ResultLocId {
38943923 ResultLocIdInvalid,
38953924 ResultLocIdNone,
src/analyze.cpp+57-14
......@@ -480,9 +480,10 @@ ZigType *get_fn_frame_type(CodeGen *g, ZigFn *fn) {
480480 return entry;
481481}
482482
483ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_const,
483ZigType *get_pointer_to_type_extra2(CodeGen *g, ZigType *child_type, bool is_const,
484484 bool is_volatile, PtrLen ptr_len, uint32_t byte_alignment,
485 uint32_t bit_offset_in_host, uint32_t host_int_bytes, bool allow_zero)
485 uint32_t bit_offset_in_host, uint32_t host_int_bytes, bool allow_zero,
486 uint32_t vector_index)
486487{
487488 assert(ptr_len != PtrLenC || allow_zero);
488489 assert(!type_is_invalid(child_type));
......@@ -494,7 +495,7 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
494495 byte_alignment = 0;
495496 }
496497
497 if (host_int_bytes != 0) {
498 if (host_int_bytes != 0 && vector_index == VECTOR_INDEX_NONE) {
498499 uint32_t child_type_bits = type_size_bits(g, child_type);
499500 if (host_int_bytes * 8 == child_type_bits) {
500501 assert(bit_offset_in_host == 0);
......@@ -504,7 +505,9 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
504505
505506 TypeId type_id = {};
506507 ZigType **parent_pointer = nullptr;
507 if (host_int_bytes != 0 || is_volatile || byte_alignment != 0 || ptr_len != PtrLenSingle || allow_zero) {
508 if (host_int_bytes != 0 || is_volatile || byte_alignment != 0 || ptr_len != PtrLenSingle ||
509 allow_zero || vector_index != VECTOR_INDEX_NONE)
510 {
508511 type_id.id = ZigTypeIdPointer;
509512 type_id.data.pointer.child_type = child_type;
510513 type_id.data.pointer.is_const = is_const;
......@@ -514,6 +517,7 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
514517 type_id.data.pointer.host_int_bytes = host_int_bytes;
515518 type_id.data.pointer.ptr_len = ptr_len;
516519 type_id.data.pointer.allow_zero = allow_zero;
520 type_id.data.pointer.vector_index = vector_index;
517521
518522 auto existing_entry = g->type_table.maybe_get(type_id);
519523 if (existing_entry)
......@@ -540,19 +544,36 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
540544 allow_zero_str = allow_zero ? "allowzero " : "";
541545 }
542546 buf_resize(&entry->name, 0);
543 if (host_int_bytes == 0 && byte_alignment == 0) {
547 if (host_int_bytes == 0 && byte_alignment == 0 && vector_index == VECTOR_INDEX_NONE) {
544548 buf_appendf(&entry->name, "%s%s%s%s%s",
545549 star_str, const_str, volatile_str, allow_zero_str, buf_ptr(&child_type->name));
546 } else if (host_int_bytes == 0) {
550 } else if (host_int_bytes == 0 && vector_index == VECTOR_INDEX_NONE) {
547551 buf_appendf(&entry->name, "%salign(%" PRIu32 ") %s%s%s%s", star_str, byte_alignment,
548552 const_str, volatile_str, allow_zero_str, buf_ptr(&child_type->name));
549553 } else if (byte_alignment == 0) {
550 buf_appendf(&entry->name, "%salign(:%" PRIu32 ":%" PRIu32 ") %s%s%s%s", star_str,
551 bit_offset_in_host, host_int_bytes, const_str, volatile_str, allow_zero_str,
554 assert(vector_index == VECTOR_INDEX_NONE);
555 buf_appendf(&entry->name, "%salign(:%" PRIu32 ":%" PRIu32 ") %s%s%s%s",
556 star_str,
557 bit_offset_in_host, host_int_bytes,
558 const_str, volatile_str, allow_zero_str,
559 buf_ptr(&child_type->name));
560 } else if (vector_index == VECTOR_INDEX_NONE) {
561 buf_appendf(&entry->name, "%salign(%" PRIu32 ":%" PRIu32 ":%" PRIu32 ") %s%s%s%s",
562 star_str, byte_alignment,
563 bit_offset_in_host, host_int_bytes,
564 const_str, volatile_str, allow_zero_str,
565 buf_ptr(&child_type->name));
566 } else if (vector_index == VECTOR_INDEX_RUNTIME) {
567 buf_appendf(&entry->name, "%salign(%" PRIu32 ":%" PRIu32 ":%" PRIu32 ":?) %s%s%s%s",
568 star_str, byte_alignment,
569 bit_offset_in_host, host_int_bytes,
570 const_str, volatile_str, allow_zero_str,
552571 buf_ptr(&child_type->name));
553572 } else {
554 buf_appendf(&entry->name, "%salign(%" PRIu32 ":%" PRIu32 ":%" PRIu32 ") %s%s%s%s", star_str, byte_alignment,
555 bit_offset_in_host, host_int_bytes, const_str, volatile_str, allow_zero_str,
573 buf_appendf(&entry->name, "%salign(%" PRIu32 ":%" PRIu32 ":%" PRIu32 ":%" PRIu32 ") %s%s%s%s",
574 star_str, byte_alignment,
575 bit_offset_in_host, host_int_bytes, vector_index,
576 const_str, volatile_str, allow_zero_str,
556577 buf_ptr(&child_type->name));
557578 }
558579
......@@ -581,6 +602,7 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
581602 entry->data.pointer.bit_offset_in_host = bit_offset_in_host;
582603 entry->data.pointer.host_int_bytes = host_int_bytes;
583604 entry->data.pointer.allow_zero = allow_zero;
605 entry->data.pointer.vector_index = vector_index;
584606
585607 if (parent_pointer) {
586608 *parent_pointer = entry;
......@@ -590,8 +612,17 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
590612 return entry;
591613}
592614
615ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_const,
616 bool is_volatile, PtrLen ptr_len, uint32_t byte_alignment,
617 uint32_t bit_offset_in_host, uint32_t host_int_bytes, bool allow_zero)
618{
619 return get_pointer_to_type_extra2(g, child_type, is_const, is_volatile, ptr_len,
620 byte_alignment, bit_offset_in_host, host_int_bytes, allow_zero, VECTOR_INDEX_NONE);
621}
622
593623ZigType *get_pointer_to_type(CodeGen *g, ZigType *child_type, bool is_const) {
594 return get_pointer_to_type_extra(g, child_type, is_const, false, PtrLenSingle, 0, 0, 0, false);
624 return get_pointer_to_type_extra2(g, child_type, is_const, false, PtrLenSingle, 0, 0, 0, false,
625 VECTOR_INDEX_NONE);
595626}
596627
597628ZigType *get_optional_type(CodeGen *g, ZigType *child_type) {
......@@ -6910,6 +6941,7 @@ uint32_t type_id_hash(TypeId x) {
69106941 (x.data.pointer.allow_zero ? (uint32_t)3324284834 : (uint32_t)3584904923) +
69116942 (((uint32_t)x.data.pointer.alignment) ^ (uint32_t)0x777fbe0e) +
69126943 (((uint32_t)x.data.pointer.bit_offset_in_host) ^ (uint32_t)2639019452) +
6944 (((uint32_t)x.data.pointer.vector_index) ^ (uint32_t)0x19199716) +
69136945 (((uint32_t)x.data.pointer.host_int_bytes) ^ (uint32_t)529908881);
69146946 case ZigTypeIdArray:
69156947 return hash_ptr(x.data.array.child_type) +
......@@ -6962,6 +6994,7 @@ bool type_id_eql(TypeId a, TypeId b) {
69626994 a.data.pointer.allow_zero == b.data.pointer.allow_zero &&
69636995 a.data.pointer.alignment == b.data.pointer.alignment &&
69646996 a.data.pointer.bit_offset_in_host == b.data.pointer.bit_offset_in_host &&
6997 a.data.pointer.vector_index == b.data.pointer.vector_index &&
69656998 a.data.pointer.host_int_bytes == b.data.pointer.host_int_bytes;
69666999 case ZigTypeIdArray:
69677000 return a.data.array.child_type == b.data.array.child_type &&
......@@ -8266,11 +8299,21 @@ static void resolve_llvm_types_pointer(CodeGen *g, ZigType *type, ResolveStatus
82668299
82678300 if (type->data.pointer.is_const || type->data.pointer.is_volatile ||
82688301 type->data.pointer.explicit_alignment != 0 || type->data.pointer.ptr_len != PtrLenSingle ||
8269 type->data.pointer.bit_offset_in_host != 0 || type->data.pointer.allow_zero)
8302 type->data.pointer.bit_offset_in_host != 0 || type->data.pointer.allow_zero ||
8303 type->data.pointer.vector_index != VECTOR_INDEX_NONE)
82708304 {
82718305 assertNoError(type_resolve(g, elem_type, ResolveStatusLLVMFwdDecl));
8272 ZigType *peer_type = get_pointer_to_type_extra(g, elem_type, false, false,
8273 PtrLenSingle, 0, 0, type->data.pointer.host_int_bytes, false);
8306 ZigType *peer_type;
8307 if (type->data.pointer.vector_index == VECTOR_INDEX_NONE) {
8308 peer_type = get_pointer_to_type_extra2(g, elem_type, false, false,
8309 PtrLenSingle, 0, 0, type->data.pointer.host_int_bytes, false,
8310 VECTOR_INDEX_NONE);
8311 } else {
8312 uint32_t host_vec_len = type->data.pointer.host_int_bytes;
8313 ZigType *host_vec_type = get_vector_type(g, host_vec_len, elem_type);
8314 peer_type = get_pointer_to_type_extra2(g, host_vec_type, false, false,
8315 PtrLenSingle, 0, 0, 0, false, VECTOR_INDEX_NONE);
8316 }
82748317 type->llvm_type = get_llvm_type(g, peer_type);
82758318 type->llvm_di_type = get_llvm_di_type(g, peer_type);
82768319 assertNoError(type_resolve(g, elem_type, wanted_resolve_status));
src/analyze.hpp+6-2
......@@ -17,10 +17,14 @@ ErrorMsg *add_error_note(CodeGen *g, ErrorMsg *parent_msg, const AstNode *node,
1717ZigType *new_type_table_entry(ZigTypeId id);
1818ZigType *get_fn_frame_type(CodeGen *g, ZigFn *fn);
1919ZigType *get_pointer_to_type(CodeGen *g, ZigType *child_type, bool is_const);
20ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_const,
21 bool is_volatile, PtrLen ptr_len,
20ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type,
21 bool is_const, bool is_volatile, PtrLen ptr_len,
2222 uint32_t byte_alignment, uint32_t bit_offset, uint32_t unaligned_bit_count,
2323 bool allow_zero);
24ZigType *get_pointer_to_type_extra2(CodeGen *g, ZigType *child_type,
25 bool is_const, bool is_volatile, PtrLen ptr_len,
26 uint32_t byte_alignment, uint32_t bit_offset, uint32_t unaligned_bit_count,
27 bool allow_zero, uint32_t vector_index);
2428uint64_t type_size(CodeGen *g, ZigType *type_entry);
2529uint64_t type_size_bits(CodeGen *g, ZigType *type_entry);
2630ZigType *get_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits);
src/codegen.cpp+68-14
......@@ -838,6 +838,11 @@ static LLVMValueRef get_handle_value(CodeGen *g, LLVMValueRef ptr, ZigType *type
838838 }
839839}
840840
841static void ir_assert(bool ok, IrInstruction *source_instruction) {
842 if (ok) return;
843 src_assert(ok, source_instruction->source_node);
844}
845
841846static bool ir_want_fast_math(CodeGen *g, IrInstruction *instruction) {
842847 // TODO memoize
843848 Scope *scope = instruction->scope;
......@@ -1635,6 +1640,17 @@ static void gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_type,
16351640 return;
16361641 }
16371642
1643 assert(ptr_type->data.pointer.vector_index != VECTOR_INDEX_RUNTIME);
1644 if (ptr_type->data.pointer.vector_index != VECTOR_INDEX_NONE) {
1645 LLVMValueRef index_val = LLVMConstInt(LLVMInt32Type(),
1646 ptr_type->data.pointer.vector_index, false);
1647 LLVMValueRef loaded_vector = gen_load(g, ptr, ptr_type, "");
1648 LLVMValueRef new_vector = LLVMBuildInsertElement(g->builder, loaded_vector, value,
1649 index_val, "");
1650 gen_store(g, new_vector, ptr, ptr_type);
1651 return;
1652 }
1653
16381654 uint32_t host_int_bytes = ptr_type->data.pointer.host_int_bytes;
16391655 if (host_int_bytes == 0) {
16401656 gen_store(g, value, ptr, ptr_type);
......@@ -1695,11 +1711,11 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
16951711 }
16961712 if (instruction->spill != nullptr) {
16971713 ZigType *ptr_type = instruction->spill->value.type;
1698 src_assert(ptr_type->id == ZigTypeIdPointer, instruction->source_node);
1714 ir_assert(ptr_type->id == ZigTypeIdPointer, instruction);
16991715 return get_handle_value(g, ir_llvm_value(g, instruction->spill),
17001716 ptr_type->data.pointer.child_type, instruction->spill->value.type);
17011717 }
1702 src_assert(instruction->value.special != ConstValSpecialRuntime, instruction->source_node);
1718 ir_assert(instruction->value.special != ConstValSpecialRuntime, instruction);
17031719 assert(instruction->value.type);
17041720 render_const_val(g, &instruction->value, "");
17051721 // we might have to do some pointer casting here due to the way union
......@@ -2428,8 +2444,7 @@ static LLVMValueRef ir_render_return(CodeGen *g, IrExecutable *executable, IrIns
24282444 return nullptr;
24292445 }
24302446 assert(g->cur_ret_ptr);
2431 src_assert(instruction->operand->value.special != ConstValSpecialRuntime,
2432 instruction->base.source_node);
2447 ir_assert(instruction->operand->value.special != ConstValSpecialRuntime, &instruction->base);
24332448 LLVMValueRef value = ir_llvm_value(g, instruction->operand);
24342449 ZigType *return_type = instruction->operand->value.type;
24352450 gen_assign_raw(g, g->cur_ret_ptr, get_pointer_to_type(g, return_type, false), value);
......@@ -3399,7 +3414,9 @@ static LLVMValueRef ir_render_decl_var(CodeGen *g, IrExecutable *executable, IrI
33993414 return nullptr;
34003415}
34013416
3402static LLVMValueRef ir_render_load_ptr(CodeGen *g, IrExecutable *executable, IrInstructionLoadPtrGen *instruction) {
3417static LLVMValueRef ir_render_load_ptr(CodeGen *g, IrExecutable *executable,
3418 IrInstructionLoadPtrGen *instruction)
3419{
34033420 ZigType *child_type = instruction->base.value.type;
34043421 if (!type_has_bits(child_type))
34053422 return nullptr;
......@@ -3408,6 +3425,14 @@ static LLVMValueRef ir_render_load_ptr(CodeGen *g, IrExecutable *executable, IrI
34083425 ZigType *ptr_type = instruction->ptr->value.type;
34093426 assert(ptr_type->id == ZigTypeIdPointer);
34103427
3428 ir_assert(ptr_type->data.pointer.vector_index != VECTOR_INDEX_RUNTIME, &instruction->base);
3429 if (ptr_type->data.pointer.vector_index != VECTOR_INDEX_NONE) {
3430 LLVMValueRef index_val = LLVMConstInt(LLVMInt32Type(),
3431 ptr_type->data.pointer.vector_index, false);
3432 LLVMValueRef loaded_vector = LLVMBuildLoad(g->builder, ptr, "");
3433 return LLVMBuildExtractElement(g->builder, loaded_vector, index_val, "");
3434 }
3435
34113436 uint32_t host_int_bytes = ptr_type->data.pointer.host_int_bytes;
34123437 if (host_int_bytes == 0)
34133438 return get_handle_value(g, ptr, child_type, ptr_type);
......@@ -3619,6 +3644,19 @@ static LLVMValueRef ir_render_store_ptr(CodeGen *g, IrExecutable *executable, Ir
36193644 return nullptr;
36203645}
36213646
3647static LLVMValueRef ir_render_vector_store_elem(CodeGen *g, IrExecutable *executable,
3648 IrInstructionVectorStoreElem *instruction)
3649{
3650 LLVMValueRef vector_ptr = ir_llvm_value(g, instruction->vector_ptr);
3651 LLVMValueRef index = ir_llvm_value(g, instruction->index);
3652 LLVMValueRef value = ir_llvm_value(g, instruction->value);
3653
3654 LLVMValueRef loaded_vector = gen_load(g, vector_ptr, instruction->vector_ptr->value.type, "");
3655 LLVMValueRef modified_vector = LLVMBuildInsertElement(g->builder, loaded_vector, value, index, "");
3656 gen_store(g, modified_vector, vector_ptr, instruction->vector_ptr->value.type);
3657 return nullptr;
3658}
3659
36223660static LLVMValueRef ir_render_var_ptr(CodeGen *g, IrExecutable *executable, IrInstructionVarPtr *instruction) {
36233661 if (instruction->base.value.special != ConstValSpecialRuntime)
36243662 return ir_llvm_value(g, &instruction->base);
......@@ -3636,7 +3674,7 @@ static LLVMValueRef ir_render_return_ptr(CodeGen *g, IrExecutable *executable,
36363674{
36373675 if (!type_has_bits(instruction->base.value.type))
36383676 return nullptr;
3639 src_assert(g->cur_ret_ptr != nullptr, instruction->base.source_node);
3677 ir_assert(g->cur_ret_ptr != nullptr, &instruction->base);
36403678 return g->cur_ret_ptr;
36413679}
36423680
......@@ -3645,7 +3683,6 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
36453683 ZigType *array_ptr_type = instruction->array_ptr->value.type;
36463684 assert(array_ptr_type->id == ZigTypeIdPointer);
36473685 ZigType *array_type = array_ptr_type->data.pointer.child_type;
3648 LLVMValueRef array_ptr = get_handle_value(g, array_ptr_ptr, array_type, array_ptr_type);
36493686 LLVMValueRef subscript_value = ir_llvm_value(g, instruction->elem_index);
36503687 assert(subscript_value);
36513688
......@@ -3657,6 +3694,7 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
36573694 if (array_type->id == ZigTypeIdArray ||
36583695 (array_type->id == ZigTypeIdPointer && array_type->data.pointer.ptr_len == PtrLenSingle))
36593696 {
3697 LLVMValueRef array_ptr = get_handle_value(g, array_ptr_ptr, array_type, array_ptr_type);
36603698 if (array_type->id == ZigTypeIdPointer) {
36613699 assert(array_type->data.pointer.child_type->id == ZigTypeIdArray);
36623700 array_type = array_type->data.pointer.child_type;
......@@ -3696,12 +3734,14 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
36963734 };
36973735 return LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");
36983736 } else if (array_type->id == ZigTypeIdPointer) {
3737 LLVMValueRef array_ptr = get_handle_value(g, array_ptr_ptr, array_type, array_ptr_type);
36993738 assert(LLVMGetTypeKind(LLVMTypeOf(array_ptr)) == LLVMPointerTypeKind);
37003739 LLVMValueRef indices[] = {
37013740 subscript_value
37023741 };
37033742 return LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 1, "");
37043743 } else if (array_type->id == ZigTypeIdStruct) {
3744 LLVMValueRef array_ptr = get_handle_value(g, array_ptr_ptr, array_type, array_ptr_type);
37053745 assert(array_type->data.structure.is_slice);
37063746
37073747 ZigType *ptr_type = instruction->base.value.type;
......@@ -3729,6 +3769,8 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
37293769 LLVMValueRef ptr_ptr = LLVMBuildStructGEP(g->builder, array_ptr, (unsigned)ptr_index, "");
37303770 LLVMValueRef ptr = gen_load_untyped(g, ptr_ptr, 0, false, "");
37313771 return LLVMBuildInBoundsGEP(g->builder, ptr, &subscript_value, 1, "");
3772 } else if (array_type->id == ZigTypeIdVector) {
3773 return array_ptr_ptr;
37323774 } else {
37333775 zig_unreachable();
37343776 }
......@@ -3917,9 +3959,9 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
39173959 if (instruction->modifier == CallModifierAsync) {
39183960 frame_result_loc = result_loc;
39193961 } else {
3920 src_assert(instruction->frame_result_loc != nullptr, instruction->base.source_node);
3962 ir_assert(instruction->frame_result_loc != nullptr, &instruction->base);
39213963 frame_result_loc_uncasted = ir_llvm_value(g, instruction->frame_result_loc);
3922 src_assert(instruction->fn_entry != nullptr, instruction->base.source_node);
3964 ir_assert(instruction->fn_entry != nullptr, &instruction->base);
39233965 frame_result_loc = LLVMBuildBitCast(g->builder, frame_result_loc_uncasted,
39243966 LLVMPointerType(get_llvm_type(g, instruction->fn_entry->frame_type), 0), "");
39253967 }
......@@ -4271,10 +4313,10 @@ static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, IrExecutable *executa
42714313 if ((err = type_resolve(g, struct_type, ResolveStatusLLVMFull)))
42724314 codegen_report_errors_and_exit(g);
42734315
4274 src_assert(field->gen_index != SIZE_MAX, instruction->base.source_node);
4316 ir_assert(field->gen_index != SIZE_MAX, &instruction->base);
42754317 LLVMValueRef field_ptr_val = LLVMBuildStructGEP(g->builder, struct_ptr, (unsigned)field->gen_index, "");
42764318 ZigType *res_type = instruction->base.value.type;
4277 src_assert(res_type->id == ZigTypeIdPointer, instruction->base.source_node);
4319 ir_assert(res_type->id == ZigTypeIdPointer, &instruction->base);
42784320 if (res_type->data.pointer.host_int_bytes != 0) {
42794321 // We generate packed structs with get_llvm_type_of_n_bytes, which is
42804322 // u8 for 1 byte or [n]u8 for multiple bytes. But the pointer to the type
......@@ -4684,7 +4726,7 @@ static LLVMValueRef ir_render_shuffle_vector(CodeGen *g, IrExecutable *executabl
46844726
46854727static LLVMValueRef ir_render_splat(CodeGen *g, IrExecutable *executable, IrInstructionSplatGen *instruction) {
46864728 ZigType *result_type = instruction->base.value.type;
4687 src_assert(result_type->id == ZigTypeIdVector, instruction->base.source_node);
4729 ir_assert(result_type->id == ZigTypeIdVector, &instruction->base);
46884730 uint32_t len = result_type->data.vector.len;
46894731 LLVMTypeRef op_llvm_type = LLVMVectorType(get_llvm_type(g, instruction->scalar->value.type), 1);
46904732 LLVMTypeRef mask_llvm_type = LLVMVectorType(LLVMInt32Type(), len);
......@@ -5039,8 +5081,8 @@ static LLVMValueRef ir_render_cmpxchg(CodeGen *g, IrExecutable *executable, IrIn
50395081 }
50405082
50415083 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
5042 src_assert(result_loc != nullptr, instruction->base.source_node);
5043 src_assert(type_has_bits(child_type), instruction->base.source_node);
5084 ir_assert(result_loc != nullptr, &instruction->base);
5085 ir_assert(type_has_bits(child_type), &instruction->base);
50445086
50455087 LLVMValueRef payload_val = LLVMBuildExtractValue(g->builder, result_val, 0, "");
50465088 LLVMValueRef val_ptr = LLVMBuildStructGEP(g->builder, result_loc, maybe_child_index, "");
......@@ -5973,6 +6015,14 @@ static LLVMValueRef ir_render_spill_end(CodeGen *g, IrExecutable *executable, Ir
59736015 zig_unreachable();
59746016}
59756017
6018static LLVMValueRef ir_render_vector_extract_elem(CodeGen *g, IrExecutable *executable,
6019 IrInstructionVectorExtractElem *instruction)
6020{
6021 LLVMValueRef vector = ir_llvm_value(g, instruction->vector);
6022 LLVMValueRef index = ir_llvm_value(g, instruction->index);
6023 return LLVMBuildExtractElement(g->builder, vector, index, "");
6024}
6025
59766026static void set_debug_location(CodeGen *g, IrInstruction *instruction) {
59776027 AstNode *source_node = instruction->source_node;
59786028 Scope *scope = instruction->scope;
......@@ -6093,6 +6143,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
60936143 return ir_render_load_ptr(g, executable, (IrInstructionLoadPtrGen *)instruction);
60946144 case IrInstructionIdStorePtr:
60956145 return ir_render_store_ptr(g, executable, (IrInstructionStorePtr *)instruction);
6146 case IrInstructionIdVectorStoreElem:
6147 return ir_render_vector_store_elem(g, executable, (IrInstructionVectorStoreElem *)instruction);
60966148 case IrInstructionIdVarPtr:
60976149 return ir_render_var_ptr(g, executable, (IrInstructionVarPtr *)instruction);
60986150 case IrInstructionIdReturnPtr:
......@@ -6233,6 +6285,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
62336285 return ir_render_shuffle_vector(g, executable, (IrInstructionShuffleVector *) instruction);
62346286 case IrInstructionIdSplatGen:
62356287 return ir_render_splat(g, executable, (IrInstructionSplatGen *) instruction);
6288 case IrInstructionIdVectorExtractElem:
6289 return ir_render_vector_extract_elem(g, executable, (IrInstructionVectorExtractElem *) instruction);
62366290 }
62376291 zig_unreachable();
62386292}
src/ir.cpp+111-10
......@@ -491,6 +491,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionStorePtr *) {
491491 return IrInstructionIdStorePtr;
492492}
493493
494static constexpr IrInstructionId ir_instruction_id(IrInstructionVectorStoreElem *) {
495 return IrInstructionIdVectorStoreElem;
496}
497
494498static constexpr IrInstructionId ir_instruction_id(IrInstructionFieldPtr *) {
495499 return IrInstructionIdFieldPtr;
496500}
......@@ -1083,6 +1087,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionSpillEnd *) {
10831087 return IrInstructionIdSpillEnd;
10841088}
10851089
1090static constexpr IrInstructionId ir_instruction_id(IrInstructionVectorExtractElem *) {
1091 return IrInstructionIdVectorExtractElem;
1092}
1093
10861094template<typename T>
10871095static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) {
10881096 const char *name = nullptr;
......@@ -1627,6 +1635,23 @@ static IrInstructionStorePtr *ir_build_store_ptr(IrBuilder *irb, Scope *scope, A
16271635 return instruction;
16281636}
16291637
1638static IrInstruction *ir_build_vector_store_elem(IrAnalyze *ira, IrInstruction *source_instruction,
1639 IrInstruction *vector_ptr, IrInstruction *index, IrInstruction *value)
1640{
1641 IrInstructionVectorStoreElem *inst = ir_build_instruction<IrInstructionVectorStoreElem>(
1642 &ira->new_irb, source_instruction->scope, source_instruction->source_node);
1643 inst->base.value.type = ira->codegen->builtin_types.entry_void;
1644 inst->vector_ptr = vector_ptr;
1645 inst->index = index;
1646 inst->value = value;
1647
1648 ir_ref_instruction(vector_ptr, ira->new_irb.current_basic_block);
1649 ir_ref_instruction(index, ira->new_irb.current_basic_block);
1650 ir_ref_instruction(value, ira->new_irb.current_basic_block);
1651
1652 return &inst->base;
1653}
1654
16301655static IrInstruction *ir_build_var_decl_src(IrBuilder *irb, Scope *scope, AstNode *source_node,
16311656 ZigVar *var, IrInstruction *align_value, IrInstruction *ptr)
16321657{
......@@ -3419,6 +3444,21 @@ static IrInstruction *ir_build_spill_end(IrBuilder *irb, Scope *scope, AstNode *
34193444 return &instruction->base;
34203445}
34213446
3447static IrInstruction *ir_build_vector_extract_elem(IrAnalyze *ira, IrInstruction *source_instruction,
3448 IrInstruction *vector, IrInstruction *index)
3449{
3450 IrInstructionVectorExtractElem *instruction = ir_build_instruction<IrInstructionVectorExtractElem>(
3451 &ira->new_irb, source_instruction->scope, source_instruction->source_node);
3452 instruction->base.value.type = vector->value.type->data.vector.elem_type;
3453 instruction->vector = vector;
3454 instruction->index = index;
3455
3456 ir_ref_instruction(vector, ira->new_irb.current_basic_block);
3457 ir_ref_instruction(index, ira->new_irb.current_basic_block);
3458
3459 return &instruction->base;
3460}
3461
34223462static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) {
34233463 results[ReturnKindUnconditional] = 0;
34243464 results[ReturnKindError] = 0;
......@@ -12908,18 +12948,18 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc
1290812948 ResultLoc *result_loc)
1290912949{
1291012950 Error err;
12911 ZigType *type_entry = ptr->value.type;
12912 if (type_is_invalid(type_entry))
12951 ZigType *ptr_type = ptr->value.type;
12952 if (type_is_invalid(ptr_type))
1291312953 return ira->codegen->invalid_instruction;
1291412954
12915 if (type_entry->id != ZigTypeIdPointer) {
12955 if (ptr_type->id != ZigTypeIdPointer) {
1291612956 ir_add_error_node(ira, source_instruction->source_node,
1291712957 buf_sprintf("attempt to dereference non-pointer type '%s'",
12918 buf_ptr(&type_entry->name)));
12958 buf_ptr(&ptr_type->name)));
1291912959 return ira->codegen->invalid_instruction;
1292012960 }
1292112961
12922 ZigType *child_type = type_entry->data.pointer.child_type;
12962 ZigType *child_type = ptr_type->data.pointer.child_type;
1292312963 // if the child type has one possible value, the deref is comptime
1292412964 switch (type_has_one_possible_value(ira->codegen, child_type)) {
1292512965 case OnePossibleValueInvalid:
......@@ -12949,14 +12989,36 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc
1294912989 }
1295012990 }
1295112991 }
12992
1295212993 // if the instruction is a const ref instruction we can skip it
1295312994 if (ptr->id == IrInstructionIdRef) {
1295412995 IrInstructionRef *ref_inst = reinterpret_cast<IrInstructionRef *>(ptr);
1295512996 return ref_inst->value;
1295612997 }
1295712998
12999 // If the instruction is a element pointer instruction to a vector, we emit
13000 // vector element extract instruction rather than load pointer. If the
13001 // pointer type has non-VECTOR_INDEX_RUNTIME value, it would have been
13002 // possible to implement this in the codegen for IrInstructionLoadPtrGen.
13003 // However if it has VECTOR_INDEX_RUNTIME then we must emit a compile error
13004 // if the vector index cannot be determined right here, right now, because
13005 // the type information does not contain enough information to actually
13006 // perform a dereference.
13007 if (ptr_type->data.pointer.vector_index == VECTOR_INDEX_RUNTIME) {
13008 if (ptr->id == IrInstructionIdElemPtr) {
13009 IrInstructionElemPtr *elem_ptr = (IrInstructionElemPtr *)ptr;
13010 IrInstruction *vector_loaded = ir_get_deref(ira, elem_ptr->array_ptr,
13011 elem_ptr->array_ptr, nullptr);
13012 IrInstruction *elem_index = elem_ptr->elem_index;
13013 return ir_build_vector_extract_elem(ira, source_instruction, vector_loaded, elem_index);
13014 }
13015 ir_add_error(ira, ptr,
13016 buf_sprintf("unable to determine vector element index of type '%s'", buf_ptr(&ptr_type->name)));
13017 return ira->codegen->invalid_instruction;
13018 }
13019
1295813020 IrInstruction *result_loc_inst;
12959 if (type_entry->data.pointer.host_int_bytes != 0 && handle_is_ptr(child_type)) {
13021 if (ptr_type->data.pointer.host_int_bytes != 0 && handle_is_ptr(child_type)) {
1296013022 if (result_loc == nullptr) result_loc = no_result_loc();
1296113023 result_loc_inst = ir_resolve_result(ira, source_instruction, result_loc, child_type, nullptr,
1296213024 true, false, true);
......@@ -16085,6 +16147,24 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
1608516147 mark_comptime_value_escape(ira, source_instr, &value->value);
1608616148 }
1608716149
16150 // If this is a store to a pointer with a runtime-known vector index,
16151 // we have to figure out the IrInstruction which represents the index and
16152 // emit a IrInstructionVectorStoreElem, or emit a compile error
16153 // explaining why it is impossible for this store to work. Which is that
16154 // the pointer address is of the vector; without the element index being known
16155 // we cannot properly perform the insertion.
16156 if (ptr->value.type->data.pointer.vector_index == VECTOR_INDEX_RUNTIME) {
16157 if (ptr->id == IrInstructionIdElemPtr) {
16158 IrInstructionElemPtr *elem_ptr = (IrInstructionElemPtr *)ptr;
16159 return ir_build_vector_store_elem(ira, source_instr, elem_ptr->array_ptr,
16160 elem_ptr->elem_index, value);
16161 }
16162 ir_add_error(ira, ptr,
16163 buf_sprintf("unable to determine vector element index of type '%s'",
16164 buf_ptr(&ptr->value.type->name)));
16165 return ira->codegen->invalid_instruction;
16166 }
16167
1608816168 IrInstructionStorePtr *store_ptr = ir_build_store_ptr(&ira->new_irb, source_instr->scope,
1608916169 source_instr->source_node, ptr, value);
1609016170 return &store_ptr->base;
......@@ -17488,6 +17568,9 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1748817568 return ir_get_const_ptr(ira, &elem_ptr_instruction->base, &ira->codegen->const_void_val,
1748917569 ira->codegen->builtin_types.entry_void, ConstPtrMutComptimeConst, is_const, is_volatile, 0);
1749017570 }
17571 } else if (array_type->id == ZigTypeIdVector) {
17572 // This depends on whether the element index is comptime, so it is computed later.
17573 return_type = nullptr;
1749117574 } else {
1749217575 ir_add_error_node(ira, elem_ptr_instruction->base.source_node,
1749317576 buf_sprintf("array access of non-array type '%s'", buf_ptr(&array_type->name)));
......@@ -17512,8 +17595,14 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1751217595 }
1751317596 safety_check_on = false;
1751417597 }
17515
17516 if (return_type->data.pointer.explicit_alignment != 0) {
17598 if (array_type->id == ZigTypeIdVector) {
17599 ZigType *elem_type = array_type->data.vector.elem_type;
17600 uint32_t host_vec_len = array_type->data.vector.len;
17601 return_type = get_pointer_to_type_extra2(ira->codegen, elem_type,
17602 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
17603 elem_ptr_instruction->ptr_len,
17604 get_ptr_align(ira->codegen, ptr_type), 0, host_vec_len, false, (uint32_t)index);
17605 } else if (return_type->data.pointer.explicit_alignment != 0) {
1751717606 // figure out the largest alignment possible
1751817607
1751917608 if ((err = type_resolve(ira->codegen, return_type->data.pointer.child_type, ResolveStatusSizeKnown)))
......@@ -17551,7 +17640,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1755117640 return ira->codegen->invalid_instruction;
1755217641
1755317642 if (array_ptr_val->special == ConstValSpecialUndef && elem_ptr_instruction->init_array_type != nullptr) {
17554 if (array_type->id == ZigTypeIdArray) {
17643 if (array_type->id == ZigTypeIdArray || array_type->id == ZigTypeIdVector) {
1755517644 array_ptr_val->data.x_array.special = ConstArraySpecialNone;
1755617645 array_ptr_val->data.x_array.data.s_none.elements = create_const_vals(array_type->data.array.len);
1755717646 array_ptr_val->special = ConstValSpecialStatic;
......@@ -17720,7 +17809,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1772017809 zig_panic("TODO elem ptr on a slice has a null pointer");
1772117810 }
1772217811 return result;
17723 } else if (array_type->id == ZigTypeIdArray) {
17812 } else if (array_type->id == ZigTypeIdArray || array_type->id == ZigTypeIdVector) {
1772417813 IrInstruction *result;
1772517814 if (orig_array_ptr_val->data.x_ptr.mut == ConstPtrMutInfer) {
1772617815 result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope,
......@@ -17742,6 +17831,14 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1774217831 }
1774317832 }
1774417833 }
17834 } else if (array_type->id == ZigTypeIdVector) {
17835 // runtime known element index
17836 ZigType *elem_type = array_type->data.vector.elem_type;
17837 uint32_t host_vec_len = array_type->data.vector.len;
17838 return_type = get_pointer_to_type_extra2(ira->codegen, elem_type,
17839 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
17840 elem_ptr_instruction->ptr_len,
17841 get_ptr_align(ira->codegen, ptr_type), 0, host_vec_len, false, VECTOR_INDEX_RUNTIME);
1774517842 } else {
1774617843 // runtime known element index
1774717844 switch (type_requires_comptime(ira->codegen, return_type)) {
......@@ -26004,6 +26101,8 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction
2600426101 case IrInstructionIdFrameSizeGen:
2600526102 case IrInstructionIdAwaitGen:
2600626103 case IrInstructionIdSplatGen:
26104 case IrInstructionIdVectorExtractElem:
26105 case IrInstructionIdVectorStoreElem:
2600726106 zig_unreachable();
2600826107
2600926108 case IrInstructionIdReturn:
......@@ -26387,6 +26486,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2638726486 case IrInstructionIdDeclVarSrc:
2638826487 case IrInstructionIdDeclVarGen:
2638926488 case IrInstructionIdStorePtr:
26489 case IrInstructionIdVectorStoreElem:
2639026490 case IrInstructionIdCallSrc:
2639126491 case IrInstructionIdCallGen:
2639226492 case IrInstructionIdReturn:
......@@ -26539,6 +26639,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2653926639 case IrInstructionIdAllocaSrc:
2654026640 case IrInstructionIdAllocaGen:
2654126641 case IrInstructionIdSpillEnd:
26642 case IrInstructionIdVectorExtractElem:
2654226643 return false;
2654326644
2654426645 case IrInstructionIdAsm:
src/ir_print.cpp+27
......@@ -78,6 +78,8 @@ const char* ir_instruction_type_str(IrInstructionId id) {
7878 return "LoadPtrGen";
7979 case IrInstructionIdStorePtr:
8080 return "StorePtr";
81 case IrInstructionIdVectorStoreElem:
82 return "VectorStoreElem";
8183 case IrInstructionIdFieldPtr:
8284 return "FieldPtr";
8385 case IrInstructionIdStructFieldPtr:
......@@ -370,6 +372,8 @@ const char* ir_instruction_type_str(IrInstructionId id) {
370372 return "SpillBegin";
371373 case IrInstructionIdSpillEnd:
372374 return "SpillEnd";
375 case IrInstructionIdVectorExtractElem:
376 return "VectorExtractElem";
373377 }
374378 zig_unreachable();
375379}
......@@ -788,6 +792,15 @@ static void ir_print_store_ptr(IrPrint *irp, IrInstructionStorePtr *instruction)
788792 ir_print_other_instruction(irp, instruction->value);
789793}
790794
795static void ir_print_vector_store_elem(IrPrint *irp, IrInstructionVectorStoreElem *instruction) {
796 fprintf(irp->f, "vector_ptr=");
797 ir_print_var_instruction(irp, instruction->vector_ptr);
798 fprintf(irp->f, ",index=");
799 ir_print_var_instruction(irp, instruction->index);
800 fprintf(irp->f, ",value=");
801 ir_print_other_instruction(irp, instruction->value);
802}
803
791804static void ir_print_typeof(IrPrint *irp, IrInstructionTypeOf *instruction) {
792805 fprintf(irp->f, "@typeOf(");
793806 ir_print_other_instruction(irp, instruction->value);
......@@ -1969,6 +1982,14 @@ static void ir_print_spill_end(IrPrint *irp, IrInstructionSpillEnd *instruction)
19691982 fprintf(irp->f, ")");
19701983}
19711984
1985static void ir_print_vector_extract_elem(IrPrint *irp, IrInstructionVectorExtractElem *instruction) {
1986 fprintf(irp->f, "@vectorExtractElem(");
1987 ir_print_other_instruction(irp, instruction->vector);
1988 fprintf(irp->f, ",");
1989 ir_print_other_instruction(irp, instruction->index);
1990 fprintf(irp->f, ")");
1991}
1992
19721993static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction, bool trailing) {
19731994 ir_print_prefix(irp, instruction, trailing);
19741995 switch (instruction->id) {
......@@ -2037,6 +2058,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction, bool
20372058 case IrInstructionIdStorePtr:
20382059 ir_print_store_ptr(irp, (IrInstructionStorePtr *)instruction);
20392060 break;
2061 case IrInstructionIdVectorStoreElem:
2062 ir_print_vector_store_elem(irp, (IrInstructionVectorStoreElem *)instruction);
2063 break;
20402064 case IrInstructionIdTypeOf:
20412065 ir_print_typeof(irp, (IrInstructionTypeOf *)instruction);
20422066 break;
......@@ -2466,6 +2490,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction, bool
24662490 case IrInstructionIdSpillEnd:
24672491 ir_print_spill_end(irp, (IrInstructionSpillEnd *)instruction);
24682492 break;
2493 case IrInstructionIdVectorExtractElem:
2494 ir_print_vector_extract_elem(irp, (IrInstructionVectorExtractElem *)instruction);
2495 break;
24692496 }
24702497 fprintf(irp->f, "\n");
24712498}
test/compile_errors.zig+32
......@@ -24,6 +24,38 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
2424 "tmp.zig:4:20: note: referenced here",
2525 );
2626
27 cases.add(
28 "store vector pointer with unknown runtime index",
29 \\export fn entry() void {
30 \\ var v: @Vector(4, i32) = [_]i32{ 1, 5, 3, undefined };
31 \\
32 \\ var i: u32 = 0;
33 \\ storev(&v[i], 42);
34 \\}
35 \\
36 \\fn storev(ptr: var, val: i32) void {
37 \\ ptr.* = val;
38 \\}
39 ,
40 "tmp.zig:9:8: error: unable to determine vector element index of type '*align(16:0:4:?) i32",
41 );
42
43 cases.add(
44 "load vector pointer with unknown runtime index",
45 \\export fn entry() void {
46 \\ var v: @Vector(4, i32) = [_]i32{ 1, 5, 3, undefined };
47 \\
48 \\ var i: u32 = 0;
49 \\ var x = loadv(&v[i]);
50 \\}
51 \\
52 \\fn loadv(ptr: var) i32 {
53 \\ return ptr.*;
54 \\}
55 ,
56 "tmp.zig:9:12: error: unable to determine vector element index of type '*align(16:0:4:?) i32",
57 );
58
2759 cases.add(
2860 "using an unknown len ptr type instead of array",
2961 \\const resolutions = [*][*]const u8{
test/stage1/behavior/vector.zig+91
......@@ -159,3 +159,94 @@ test "vector @splat" {
159159 S.doTheTest();
160160 comptime S.doTheTest();
161161}
162
163test "load vector elements via comptime index" {
164 const S = struct {
165 fn doTheTest() void {
166 var v: @Vector(4, i32) = [_]i32{ 1, 2, 3, undefined };
167 expect(v[0] == 1);
168 expect(v[1] == 2);
169 expect(loadv(&v[2]) == 3);
170 }
171 fn loadv(ptr: var) i32 {
172 return ptr.*;
173 }
174 };
175
176 S.doTheTest();
177 comptime S.doTheTest();
178}
179
180test "store vector elements via comptime index" {
181 const S = struct {
182 fn doTheTest() void {
183 var v: @Vector(4, i32) = [_]i32{ 1, 5, 3, undefined };
184
185 v[2] = 42;
186 expect(v[1] == 5);
187 v[3] = -364;
188 expect(v[2] == 42);
189 expect(-364 == v[3]);
190
191 storev(&v[0], 100);
192 expect(v[0] == 100);
193 }
194 fn storev(ptr: var, x: i32) void {
195 ptr.* = x;
196 }
197 };
198
199 S.doTheTest();
200 comptime S.doTheTest();
201}
202
203test "load vector elements via runtime index" {
204 const S = struct {
205 fn doTheTest() void {
206 var v: @Vector(4, i32) = [_]i32{ 1, 2, 3, undefined };
207 var i: u32 = 0;
208 expect(v[i] == 1);
209 i += 1;
210 expect(v[i] == 2);
211 i += 1;
212 expect(v[i] == 3);
213 }
214 };
215
216 S.doTheTest();
217 comptime S.doTheTest();
218}
219
220test "store vector elements via runtime index" {
221 const S = struct {
222 fn doTheTest() void {
223 var v: @Vector(4, i32) = [_]i32{ 1, 5, 3, undefined };
224 var i: u32 = 2;
225 v[i] = 1;
226 expect(v[1] == 5);
227 expect(v[2] == 1);
228 i += 1;
229 v[i] = -364;
230 expect(-364 == v[3]);
231 }
232 };
233
234 S.doTheTest();
235 comptime S.doTheTest();
236}
237
238test "initialize vector which is a struct field" {
239 const Vec4Obj = struct {
240 data: @Vector(4, f32),
241 };
242
243 const S = struct {
244 fn doTheTest() void {
245 var foo = Vec4Obj{
246 .data = [_]f32{ 1, 2, 3, 4 },
247 };
248 }
249 };
250 S.doTheTest();
251 comptime S.doTheTest();
252}