authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-09-17 17:29:38+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-09-23 12:36:56-07:00
log6f55a689644fa85a6f650e6d33bcb882b1f507cd
tree5ead3b6635483996c5dd2758938909d190d3e303
parent5dffbf32bfb0c88163d2a116b6b0f9d802dce8cc

spirv: air struct_field_ptr for unions


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

src/codegen/spirv.zig+20-2
......@@ -2651,17 +2651,35 @@ pub const DeclGen = struct {
26512651 object_ptr: IdRef,
26522652 field_index: u32,
26532653 ) !?IdRef {
2654 const result_ty_ref = try self.resolveType(result_ptr_ty, .direct);
2655
26542656 const mod = self.module;
26552657 const object_ty = object_ptr_ty.childType(mod);
26562658 switch (object_ty.zigTypeTag(mod)) {
26572659 .Struct => switch (object_ty.containerLayout(mod)) {
26582660 .Packed => unreachable, // TODO
26592661 else => {
2660 const result_ty_ref = try self.resolveType(result_ptr_ty, .direct);
26612662 return try self.accessChain(result_ty_ref, object_ptr, &.{field_index});
26622663 },
26632664 },
2664 else => unreachable, // TODO
2665 .Union => switch (object_ty.containerLayout(mod)) {
2666 .Packed => unreachable, // TODO
2667 else => {
2668 const storage_class = spvStorageClass(object_ptr_ty.ptrAddressSpace(mod));
2669 const un_active_ty_ref = try self.resolveUnionType(object_ty, field_index);
2670 const un_active_ptr_ty_ref = try self.spv.ptrType(un_active_ty_ref, storage_class);
2671
2672 const casted_id = self.spv.allocId();
2673 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
2674 .id_result_type = self.typeId(un_active_ptr_ty_ref),
2675 .id_result = casted_id,
2676 .operand = object_ptr,
2677 });
2678 const layout = self.unionLayout(object_ty, field_index);
2679 return try self.accessChain(result_ty_ref, casted_id, &.{layout.active_field_index});
2680 },
2681 },
2682 else => unreachable,
26652683 }
26662684 }
26672685
test/behavior/union.zig-1
......@@ -496,7 +496,6 @@ test "union initializer generates padding only if needed" {
496496 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
497497 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
498498 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
499 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
500499
501500 const U = union(enum) {
502501 A: u24,