authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-09-26 23:15:59+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-10-15 13:59:23+02:00
log08ea9a9ff6962c40eb87ab4951bc07a05b3831c9
tree26185fbbba4dd23e961b2524163db8b322c8385a
parenta7c3d5e4ec551e48166919ab55f618afe7c23b2a
signature Signed by SSH key SHA256:CQ99aPxq+RueiL9u7z0FEki5Fm7V6T8q4PrEGmINrA4

spirv: cast result of .elem pointers to right type if needed


2 files changed, 20 insertions(+), 3 deletions(-)

src/codegen/spirv.zig+19-1
......@@ -789,7 +789,25 @@ pub const DeclGen = struct {
789789 const size_ty_ref = try self.sizeType();
790790 const index_id = try self.constInt(size_ty_ref, elem_ptr.index);
791791
792 return try self.ptrElemPtr(parent_ptr_ty, parent_ptr_id, index_id);
792 const elem_ptr_id = try self.ptrElemPtr(parent_ptr_ty, parent_ptr_id, index_id);
793
794 // TODO: Can we consolidate this in ptrElemPtr?
795 const elem_ty = parent_ptr_ty.elemType2(mod); // use elemType() so that we get T for *[N]T.
796 const elem_ty_ref = try self.resolveType(elem_ty, .direct);
797 const elem_ptr_ty_ref = try self.spv.ptrType(elem_ty_ref, spvStorageClass(parent_ptr_ty.ptrAddressSpace(mod)));
798
799 if (elem_ptr_ty_ref == result_ty_ref) {
800 return elem_ptr_id;
801 }
802 // This may happen when we have pointer-to-array and the result is
803 // another pointer-to-array instead of a pointer-to-element.
804 const result_id = self.spv.allocId();
805 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
806 .id_result_type = self.typeId(result_ty_ref),
807 .id_result = result_id,
808 .operand = elem_ptr_id,
809 });
810 return result_id;
793811 },
794812 .field => unreachable, // TODO
795813 }
test/behavior/slice.zig+1-2
......@@ -183,8 +183,6 @@ test "slicing zero length array" {
183183}
184184
185185test "slicing pointer by length" {
186 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
187
188186 const array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
189187 const ptr: [*]const u8 = @as([*]const u8, @ptrCast(&array));
190188 const slice = ptr[1..][0..5];
......@@ -620,6 +618,7 @@ test "type coercion of pointer to anon struct literal to pointer to slice" {
620618 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
621619 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
622620 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
621 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
623622
624623 const S = struct {
625624 const U = union {