zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 6f55a689644fa85a6f650e6d33bcb882b1f507cd (tree)
parent 5dffbf32bfb0c88163d2a116b6b0f9d802dce8cc
Author: Robin Voetter <robin@voetter.nl>
Date:   Sun, 17 Sep 2023 17:29:38 +0200

spirv: air struct_field_ptr for unions

Diffstat:
Msrc/codegen/spirv.zig | 22++++++++++++++++++++--
Mtest/behavior/union.zig | 1-
2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig @@ -2651,17 +2651,35 @@ pub const DeclGen = struct { object_ptr: IdRef, field_index: u32, ) !?IdRef { + const result_ty_ref = try self.resolveType(result_ptr_ty, .direct); + const mod = self.module; const object_ty = object_ptr_ty.childType(mod); switch (object_ty.zigTypeTag(mod)) { .Struct => switch (object_ty.containerLayout(mod)) { .Packed => unreachable, // TODO else => { - const result_ty_ref = try self.resolveType(result_ptr_ty, .direct); return try self.accessChain(result_ty_ref, object_ptr, &.{field_index}); }, }, - else => unreachable, // TODO + .Union => switch (object_ty.containerLayout(mod)) { + .Packed => unreachable, // TODO + else => { + const storage_class = spvStorageClass(object_ptr_ty.ptrAddressSpace(mod)); + const un_active_ty_ref = try self.resolveUnionType(object_ty, field_index); + const un_active_ptr_ty_ref = try self.spv.ptrType(un_active_ty_ref, storage_class); + + const casted_id = self.spv.allocId(); + try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ + .id_result_type = self.typeId(un_active_ptr_ty_ref), + .id_result = casted_id, + .operand = object_ptr, + }); + const layout = self.unionLayout(object_ty, field_index); + return try self.accessChain(result_ty_ref, casted_id, &.{layout.active_field_index}); + }, + }, + else => unreachable, } } diff --git a/test/behavior/union.zig b/test/behavior/union.zig @@ -496,7 +496,6 @@ test "union initializer generates padding only if needed" { if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const U = union(enum) { A: u24,