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 {...@@ -2651,17 +2651,35 @@ pub const DeclGen = struct {
2651 object_ptr: IdRef,2651 object_ptr: IdRef,
2652 field_index: u32,2652 field_index: u32,
2653 ) !?IdRef {2653 ) !?IdRef {
2654 const result_ty_ref = try self.resolveType(result_ptr_ty, .direct);
2655
2654 const mod = self.module;2656 const mod = self.module;
2655 const object_ty = object_ptr_ty.childType(mod);2657 const object_ty = object_ptr_ty.childType(mod);
2656 switch (object_ty.zigTypeTag(mod)) {2658 switch (object_ty.zigTypeTag(mod)) {
2657 .Struct => switch (object_ty.containerLayout(mod)) {2659 .Struct => switch (object_ty.containerLayout(mod)) {
2658 .Packed => unreachable, // TODO2660 .Packed => unreachable, // TODO
2659 else => {2661 else => {
2660 const result_ty_ref = try self.resolveType(result_ptr_ty, .direct);
2661 return try self.accessChain(result_ty_ref, object_ptr, &.{field_index});2662 return try self.accessChain(result_ty_ref, object_ptr, &.{field_index});
2662 },2663 },
2663 },2664 },
2664 else => unreachable, // TODO2665 .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,
2665 }2683 }
2666 }2684 }
26672685
test/behavior/union.zig-1
...@@ -496,7 +496,6 @@ test "union initializer generates padding only if needed" {...@@ -496,7 +496,6 @@ test "union initializer generates padding only if needed" {
496 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;496 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
497 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;497 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
498 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO498 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
499 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
500499
501 const U = union(enum) {500 const U = union(enum) {
502 A: u24,501 A: u24,