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

This commit is contained in:
Robin Voetter 2023-09-26 23:15:59 +02:00
parent a7c3d5e4ec
commit 08ea9a9ff6
No known key found for this signature in database
2 changed files with 20 additions and 3 deletions

View File

@ -789,7 +789,25 @@ pub const DeclGen = struct {
const size_ty_ref = try self.sizeType();
const index_id = try self.constInt(size_ty_ref, elem_ptr.index);
return try self.ptrElemPtr(parent_ptr_ty, parent_ptr_id, index_id);
const elem_ptr_id = try self.ptrElemPtr(parent_ptr_ty, parent_ptr_id, index_id);
// TODO: Can we consolidate this in ptrElemPtr?
const elem_ty = parent_ptr_ty.elemType2(mod); // use elemType() so that we get T for *[N]T.
const elem_ty_ref = try self.resolveType(elem_ty, .direct);
const elem_ptr_ty_ref = try self.spv.ptrType(elem_ty_ref, spvStorageClass(parent_ptr_ty.ptrAddressSpace(mod)));
if (elem_ptr_ty_ref == result_ty_ref) {
return elem_ptr_id;
}
// This may happen when we have pointer-to-array and the result is
// another pointer-to-array instead of a pointer-to-element.
const result_id = self.spv.allocId();
try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
.id_result_type = self.typeId(result_ty_ref),
.id_result = result_id,
.operand = elem_ptr_id,
});
return result_id;
},
.field => unreachable, // TODO
}

View File

@ -183,8 +183,6 @@ test "slicing zero length array" {
}
test "slicing pointer by length" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
const ptr: [*]const u8 = @as([*]const u8, @ptrCast(&array));
const slice = ptr[1..][0..5];
@ -620,6 +618,7 @@ test "type coercion of pointer to anon struct literal to pointer to slice" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const S = struct {
const U = union {