authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-22 12:15:31-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-22 12:15:31-07:00
log9f0359d78f9facc38418e32b0e8c1bf6f99f0d26
treec9427aa9886e8d7f84c50f2ef12407cc80fa61d1
parentfd2239bde9e2051a32fb25c50c732a40d4afccd0

Revert "Merge pull request #17657 from Snektron/spirv-recursive-ptrs"

This reverts commit b822e841cda0adabe3fec260ff51c18508f7ee32, reversing changes made to 0c99ba1eab63865592bb084feb271cd4e4b0357e. This caused a CI failure when it landed in master branch.

21 files changed, 487 insertions(+), 484 deletions(-)

src/codegen/spirv.zig+309-292
...@@ -209,10 +209,6 @@ const DeclGen = struct {...@@ -209,10 +209,6 @@ const DeclGen = struct {
209 /// See Object.type_map209 /// See Object.type_map
210 type_map: *TypeMap,210 type_map: *TypeMap,
211211
212 /// Child types of pointers that are currently in progress of being resolved. If a pointer
213 /// is already in this map, its recursive.
214 wip_pointers: std.AutoHashMapUnmanaged(struct { InternPool.Index, StorageClass }, CacheRef) = .{},
215
216 /// We need to keep track of result ids for block labels, as well as the 'incoming'212 /// We need to keep track of result ids for block labels, as well as the 'incoming'
217 /// blocks for a block.213 /// blocks for a block.
218 blocks: BlockMap = .{},214 blocks: BlockMap = .{},
...@@ -299,7 +295,6 @@ const DeclGen = struct {...@@ -299,7 +295,6 @@ const DeclGen = struct {
299 pub fn deinit(self: *DeclGen) void {295 pub fn deinit(self: *DeclGen) void {
300 self.args.deinit(self.gpa);296 self.args.deinit(self.gpa);
301 self.inst_results.deinit(self.gpa);297 self.inst_results.deinit(self.gpa);
302 self.wip_pointers.deinit(self.gpa);
303 self.blocks.deinit(self.gpa);298 self.blocks.deinit(self.gpa);
304 self.func.deinit(self.gpa);299 self.func.deinit(self.gpa);
305 self.base_line_stack.deinit(self.gpa);300 self.base_line_stack.deinit(self.gpa);
...@@ -363,7 +358,8 @@ const DeclGen = struct {...@@ -363,7 +358,8 @@ const DeclGen = struct {
363358
364 const mod = self.module;359 const mod = self.module;
365 const ty = mod.intern_pool.typeOf(val).toType();360 const ty = mod.intern_pool.typeOf(val).toType();
366 const ptr_ty_ref = try self.ptrType(ty, storage_class);361 const ty_ref = try self.resolveType(ty, .indirect);
362 const ptr_ty_ref = try self.spv.ptrType(ty_ref, storage_class);
367363
368 const var_id = self.spv.declPtr(spv_decl_index).result_id;364 const var_id = self.spv.declPtr(spv_decl_index).result_id;
369365
...@@ -586,41 +582,66 @@ const DeclGen = struct {...@@ -586,41 +582,66 @@ const DeclGen = struct {
586 }582 }
587583
588 /// Construct a struct at runtime.584 /// Construct a struct at runtime.
589 /// ty must be a struct type.585 /// result_ty_ref must be a struct type.
590 /// Constituents should be in `indirect` representation (as the elements of a struct should be).586 /// Constituents should be in `indirect` representation (as the elements of a struct should be).
591 /// Result is in `direct` representation.587 /// Result is in `direct` representation.
592 fn constructStruct(self: *DeclGen, ty: Type, types: []const Type, constituents: []const IdRef) !IdRef {588 fn constructStruct(self: *DeclGen, result_ty_ref: CacheRef, constituents: []const IdRef) !IdRef {
593 assert(types.len == constituents.len);
594 // The Khronos LLVM-SPIRV translator crashes because it cannot construct structs which'589 // The Khronos LLVM-SPIRV translator crashes because it cannot construct structs which'
595 // operands are not constant.590 // operands are not constant.
596 // See https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/1349591 // See https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/1349
597 // For now, just initialize the struct by setting the fields manually...592 // For now, just initialize the struct by setting the fields manually...
598 // TODO: Make this OpCompositeConstruct when we can593 // TODO: Make this OpCompositeConstruct when we can
599 const ptr_composite_id = try self.alloc(ty, .{ .storage_class = .Function });594 const ptr_ty_ref = try self.spv.ptrType(result_ty_ref, .Function);
600 for (constituents, types, 0..) |constitent_id, member_ty, index| {595 const ptr_composite_id = self.spv.allocId();
601 const ptr_member_ty_ref = try self.ptrType(member_ty, .Function);596 try self.func.prologue.emit(self.spv.gpa, .OpVariable, .{
597 .id_result_type = self.typeId(ptr_ty_ref),
598 .id_result = ptr_composite_id,
599 .storage_class = .Function,
600 });
601
602 const spv_composite_ty = self.spv.cache.lookup(result_ty_ref).struct_type;
603 const member_types = spv_composite_ty.member_types;
604
605 for (constituents, member_types, 0..) |constitent_id, member_ty_ref, index| {
606 const ptr_member_ty_ref = try self.spv.ptrType(member_ty_ref, .Function);
602 const ptr_id = try self.accessChain(ptr_member_ty_ref, ptr_composite_id, &.{@as(u32, @intCast(index))});607 const ptr_id = try self.accessChain(ptr_member_ty_ref, ptr_composite_id, &.{@as(u32, @intCast(index))});
603 try self.func.body.emit(self.spv.gpa, .OpStore, .{608 try self.func.body.emit(self.spv.gpa, .OpStore, .{
604 .pointer = ptr_id,609 .pointer = ptr_id,
605 .object = constitent_id,610 .object = constitent_id,
606 });611 });
607 }612 }
608 return try self.load(ty, ptr_composite_id, .{});613 const result_id = self.spv.allocId();
614 try self.func.body.emit(self.spv.gpa, .OpLoad, .{
615 .id_result_type = self.typeId(result_ty_ref),
616 .id_result = result_id,
617 .pointer = ptr_composite_id,
618 });
619 return result_id;
609 }620 }
610621
611 /// Construct an array at runtime.622 /// Construct an array at runtime.
612 /// ty must be an array type.623 /// result_ty_ref must be an array type.
613 /// Constituents should be in `indirect` representation (as the elements of an array should be).624 /// Constituents should be in `indirect` representation (as the elements of an array should be).
614 /// Result is in `direct` representation.625 /// Result is in `direct` representation.
615 fn constructArray(self: *DeclGen, ty: Type, constituents: []const IdRef) !IdRef {626 fn constructArray(self: *DeclGen, result_ty_ref: CacheRef, constituents: []const IdRef) !IdRef {
616 // The Khronos LLVM-SPIRV translator crashes because it cannot construct structs which'627 // The Khronos LLVM-SPIRV translator crashes because it cannot construct structs which'
617 // operands are not constant.628 // operands are not constant.
618 // See https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/1349629 // See https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/1349
619 // For now, just initialize the struct by setting the fields manually...630 // For now, just initialize the struct by setting the fields manually...
620 // TODO: Make this OpCompositeConstruct when we can631 // TODO: Make this OpCompositeConstruct when we can
621 const mod = self.module;632 // TODO: Make this Function storage type
622 const ptr_composite_id = try self.alloc(ty, .{ .storage_class = .Function });633 const ptr_ty_ref = try self.spv.ptrType(result_ty_ref, .Function);
623 const ptr_elem_ty_ref = try self.ptrType(ty.elemType2(mod), .Function);634 const ptr_composite_id = self.spv.allocId();
635 try self.func.prologue.emit(self.spv.gpa, .OpVariable, .{
636 .id_result_type = self.typeId(ptr_ty_ref),
637 .id_result = ptr_composite_id,
638 .storage_class = .Function,
639 });
640
641 const spv_composite_ty = self.spv.cache.lookup(result_ty_ref).array_type;
642 const elem_ty_ref = spv_composite_ty.element_type;
643 const ptr_elem_ty_ref = try self.spv.ptrType(elem_ty_ref, .Function);
644
624 for (constituents, 0..) |constitent_id, index| {645 for (constituents, 0..) |constitent_id, index| {
625 const ptr_id = try self.accessChain(ptr_elem_ty_ref, ptr_composite_id, &.{@as(u32, @intCast(index))});646 const ptr_id = try self.accessChain(ptr_elem_ty_ref, ptr_composite_id, &.{@as(u32, @intCast(index))});
626 try self.func.body.emit(self.spv.gpa, .OpStore, .{647 try self.func.body.emit(self.spv.gpa, .OpStore, .{
...@@ -628,8 +649,13 @@ const DeclGen = struct {...@@ -628,8 +649,13 @@ const DeclGen = struct {
628 .object = constitent_id,649 .object = constitent_id,
629 });650 });
630 }651 }
631652 const result_id = self.spv.allocId();
632 return try self.load(ty, ptr_composite_id, .{});653 try self.func.body.emit(self.spv.gpa, .OpLoad, .{
654 .id_result_type = self.typeId(result_ty_ref),
655 .id_result = result_id,
656 .pointer = ptr_composite_id,
657 });
658 return result_id;
633 }659 }
634660
635 /// This function generates a load for a constant in direct (ie, non-memory) representation.661 /// This function generates a load for a constant in direct (ie, non-memory) representation.
...@@ -740,18 +766,15 @@ const DeclGen = struct {...@@ -740,18 +766,15 @@ const DeclGen = struct {
740 }.toValue();766 }.toValue();
741767
742 var constituents: [2]IdRef = undefined;768 var constituents: [2]IdRef = undefined;
743 var types: [2]Type = undefined;
744 if (eu_layout.error_first) {769 if (eu_layout.error_first) {
745 constituents[0] = try self.constant(err_ty, err_val, .indirect);770 constituents[0] = try self.constant(err_ty, err_val, .indirect);
746 constituents[1] = try self.constant(payload_ty, payload_val, .indirect);771 constituents[1] = try self.constant(payload_ty, payload_val, .indirect);
747 types = .{ err_ty, payload_ty };
748 } else {772 } else {
749 constituents[0] = try self.constant(payload_ty, payload_val, .indirect);773 constituents[0] = try self.constant(payload_ty, payload_val, .indirect);
750 constituents[1] = try self.constant(err_ty, err_val, .indirect);774 constituents[1] = try self.constant(err_ty, err_val, .indirect);
751 types = .{ payload_ty, err_ty };
752 }775 }
753776
754 return try self.constructStruct(ty, &types, &constituents);777 return try self.constructStruct(result_ty_ref, &constituents);
755 },778 },
756 .enum_tag => {779 .enum_tag => {
757 const int_val = try val.intFromEnum(ty, mod);780 const int_val = try val.intFromEnum(ty, mod);
...@@ -769,11 +792,7 @@ const DeclGen = struct {...@@ -769,11 +792,7 @@ const DeclGen = struct {
769 }792 }
770793
771 const len_id = try self.constant(Type.usize, ptr.len.toValue(), .indirect);794 const len_id = try self.constant(Type.usize, ptr.len.toValue(), .indirect);
772 return try self.constructStruct(795 return try self.constructStruct(result_ty_ref, &.{ ptr_id, len_id });
773 ty,
774 &.{ ptr_ty, Type.usize },
775 &.{ ptr_id, len_id },
776 );
777 },796 },
778 .opt => {797 .opt => {
779 const payload_ty = ty.optionalChild(mod);798 const payload_ty = ty.optionalChild(mod);
...@@ -800,11 +819,7 @@ const DeclGen = struct {...@@ -800,11 +819,7 @@ const DeclGen = struct {
800 else819 else
801 try self.spv.constUndef(try self.resolveType(payload_ty, .indirect));820 try self.spv.constUndef(try self.resolveType(payload_ty, .indirect));
802821
803 return try self.constructStruct(822 return try self.constructStruct(result_ty_ref, &.{ payload_id, has_pl_id });
804 ty,
805 &.{ payload_ty, Type.bool },
806 &.{ payload_id, has_pl_id },
807 );
808 },823 },
809 .aggregate => |aggregate| switch (ip.indexToKey(ty.ip_index)) {824 .aggregate => |aggregate| switch (ip.indexToKey(ty.ip_index)) {
810 inline .array_type, .vector_type => |array_type, tag| {825 inline .array_type, .vector_type => |array_type, tag| {
...@@ -842,7 +857,7 @@ const DeclGen = struct {...@@ -842,7 +857,7 @@ const DeclGen = struct {
842 else => {},857 else => {},
843 }858 }
844859
845 return try self.constructArray(ty, constituents);860 return try self.constructArray(result_ty_ref, constituents);
846 },861 },
847 .struct_type => {862 .struct_type => {
848 const struct_type = mod.typeToStruct(ty).?;863 const struct_type = mod.typeToStruct(ty).?;
...@@ -850,9 +865,6 @@ const DeclGen = struct {...@@ -850,9 +865,6 @@ const DeclGen = struct {
850 return self.todo("packed struct constants", .{});865 return self.todo("packed struct constants", .{});
851 }866 }
852867
853 var types = std.ArrayList(Type).init(self.gpa);
854 defer types.deinit();
855
856 var constituents = std.ArrayList(IdRef).init(self.gpa);868 var constituents = std.ArrayList(IdRef).init(self.gpa);
857 defer constituents.deinit();869 defer constituents.deinit();
858870
...@@ -868,23 +880,22 @@ const DeclGen = struct {...@@ -868,23 +880,22 @@ const DeclGen = struct {
868 const field_val = try val.fieldValue(mod, field_index);880 const field_val = try val.fieldValue(mod, field_index);
869 const field_id = try self.constant(field_ty, field_val, .indirect);881 const field_id = try self.constant(field_ty, field_val, .indirect);
870882
871 try types.append(field_ty);
872 try constituents.append(field_id);883 try constituents.append(field_id);
873 }884 }
874885
875 return try self.constructStruct(ty, types.items, constituents.items);886 return try self.constructStruct(result_ty_ref, constituents.items);
876 },887 },
877 .anon_struct_type => unreachable, // TODO888 .anon_struct_type => unreachable, // TODO
878 else => unreachable,889 else => unreachable,
879 },890 },
880 .un => |un| {891 .un => |un| {
881 const active_field = ty.unionTagFieldIndex(un.tag.toValue(), mod).?;892 const active_field = ty.unionTagFieldIndex(un.tag.toValue(), mod).?;
882 const union_obj = mod.typeToUnion(ty).?;893 const layout = self.unionLayout(ty, active_field);
883 const field_ty = union_obj.field_types.get(ip)[active_field].toType();894 const payload = if (layout.active_field_size != 0)
884 const payload = if (field_ty.hasRuntimeBitsIgnoreComptime(mod))895 try self.constant(layout.active_field_ty, un.val.toValue(), .indirect)
885 try self.constant(field_ty, un.val.toValue(), .direct)
886 else896 else
887 null;897 null;
898
888 return try self.unionInit(ty, active_field, payload);899 return try self.unionInit(ty, active_field, payload);
889 },900 },
890 .memoized_call => unreachable,901 .memoized_call => unreachable,
...@@ -923,7 +934,8 @@ const DeclGen = struct {...@@ -923,7 +934,8 @@ const DeclGen = struct {
923934
924 // TODO: Can we consolidate this in ptrElemPtr?935 // TODO: Can we consolidate this in ptrElemPtr?
925 const elem_ty = parent_ptr_ty.elemType2(mod); // use elemType() so that we get T for *[N]T.936 const elem_ty = parent_ptr_ty.elemType2(mod); // use elemType() so that we get T for *[N]T.
926 const elem_ptr_ty_ref = try self.ptrType(elem_ty, spvStorageClass(parent_ptr_ty.ptrAddressSpace(mod)));937 const elem_ty_ref = try self.resolveType(elem_ty, .direct);
938 const elem_ptr_ty_ref = try self.spv.ptrType(elem_ty_ref, spvStorageClass(parent_ptr_ty.ptrAddressSpace(mod)));
927939
928 if (elem_ptr_ty_ref == result_ty_ref) {940 if (elem_ptr_ty_ref == result_ty_ref) {
929 return elem_ptr_id;941 return elem_ptr_id;
...@@ -985,7 +997,8 @@ const DeclGen = struct {...@@ -985,7 +997,8 @@ const DeclGen = struct {
985 };997 };
986998
987 const decl_id = try self.resolveAnonDecl(decl_val, actual_storage_class);999 const decl_id = try self.resolveAnonDecl(decl_val, actual_storage_class);
988 const decl_ptr_ty_ref = try self.ptrType(decl_ty, final_storage_class);1000 const decl_ty_ref = try self.resolveType(decl_ty, .indirect);
1001 const decl_ptr_ty_ref = try self.spv.ptrType(decl_ty_ref, final_storage_class);
9891002
990 const ptr_id = switch (final_storage_class) {1003 const ptr_id = switch (final_storage_class) {
991 .Generic => blk: {1004 .Generic => blk: {
...@@ -1041,7 +1054,8 @@ const DeclGen = struct {...@@ -1041,7 +1054,8 @@ const DeclGen = struct {
10411054
1042 const final_storage_class = spvStorageClass(decl.@"addrspace");1055 const final_storage_class = spvStorageClass(decl.@"addrspace");
10431056
1044 const decl_ptr_ty_ref = try self.ptrType(decl.ty, final_storage_class);1057 const decl_ty_ref = try self.resolveType(decl.ty, .indirect);
1058 const decl_ptr_ty_ref = try self.spv.ptrType(decl_ty_ref, final_storage_class);
10451059
1046 const ptr_id = switch (final_storage_class) {1060 const ptr_id = switch (final_storage_class) {
1047 .Generic => blk: {1061 .Generic => blk: {
...@@ -1109,52 +1123,29 @@ const DeclGen = struct {...@@ -1109,52 +1123,29 @@ const DeclGen = struct {
1109 return try self.intType(.unsigned, self.getTarget().ptrBitWidth());1123 return try self.intType(.unsigned, self.getTarget().ptrBitWidth());
1110 }1124 }
11111125
1112 fn ptrType(self: *DeclGen, child_ty: Type, storage_class: StorageClass) !CacheRef {1126 /// Generate a union type, optionally with a known field. If the tag alignment is greater
1113 const key = .{ child_ty.toIntern(), storage_class };1127 /// than that of the payload, a regular union (non-packed, with both tag and payload), will
1114 const entry = try self.wip_pointers.getOrPut(self.gpa, key);1128 /// be generated as follows:
1115 if (entry.found_existing) {1129 /// If the active field is known:
1116 const fwd_ref = entry.value_ptr.*;
1117 try self.spv.cache.recursive_ptrs.put(self.spv.gpa, fwd_ref, {});
1118 return fwd_ref;
1119 }
1120
1121 const fwd_ref = try self.spv.resolve(.{ .fwd_ptr_type = .{
1122 .zig_child_type = child_ty.toIntern(),
1123 .storage_class = storage_class,
1124 } });
1125 entry.value_ptr.* = fwd_ref;
1126
1127 const child_ty_ref = try self.resolveType(child_ty, .indirect);
1128 _ = try self.spv.resolve(.{ .ptr_type = .{
1129 .storage_class = storage_class,
1130 .child_type = child_ty_ref,
1131 .fwd = fwd_ref,
1132 } });
1133
1134 assert(self.wip_pointers.remove(key));
1135
1136 return fwd_ref;
1137 }
1138
1139 /// Generate a union type. Union types are always generated with the
1140 /// most aligned field active. If the tag alignment is greater
1141 /// than that of the payload, a regular union (non-packed, with both tag and
1142 /// payload), will be generated as follows:
1143 /// struct {1130 /// struct {
1144 /// tag: TagType,1131 /// tag: TagType,
1145 /// payload: MostAlignedFieldType,1132 /// payload: ActivePayloadType,
1146 /// payload_padding: [payload_size - @sizeOf(MostAlignedFieldType)]u8,1133 /// payload_padding: [payload_size - @sizeOf(ActivePayloadType)]u8,
1147 /// padding: [padding_size]u8,1134 /// padding: [padding_size]u8,
1148 /// }1135 /// }
1149 /// If the payload alignment is greater than that of the tag:1136 /// If the payload alignment is greater than that of the tag:
1150 /// struct {1137 /// struct {
1151 /// payload: MostAlignedFieldType,1138 /// payload: ActivePayloadType,
1152 /// payload_padding: [payload_size - @sizeOf(MostAlignedFieldType)]u8,1139 /// payload_padding: [payload_size - @sizeOf(ActivePayloadType)]u8,
1153 /// tag: TagType,1140 /// tag: TagType,
1154 /// padding: [padding_size]u8,1141 /// padding: [padding_size]u8,
1155 /// }1142 /// }
1143 /// If the active payload is unknown, it will default back to the most aligned field. This is
1144 /// to make sure that the overal struct has the correct alignment in spir-v.
1156 /// If any of the fields' size is 0, it will be omitted.1145 /// If any of the fields' size is 0, it will be omitted.
1157 fn resolveUnionType(self: *DeclGen, ty: Type) !CacheRef {1146 /// NOTE: When the active field is set to something other than the most aligned field, the
1147 /// resulting struct will be *underaligned*.
1148 fn resolveUnionType(self: *DeclGen, ty: Type, maybe_active_field: ?usize) !CacheRef {
1158 const mod = self.module;1149 const mod = self.module;
1159 const ip = &mod.intern_pool;1150 const ip = &mod.intern_pool;
1160 const union_obj = mod.typeToUnion(ty).?;1151 const union_obj = mod.typeToUnion(ty).?;
...@@ -1163,13 +1154,17 @@ const DeclGen = struct {...@@ -1163,13 +1154,17 @@ const DeclGen = struct {
1163 return self.todo("packed union types", .{});1154 return self.todo("packed union types", .{});
1164 }1155 }
11651156
1166 const layout = self.unionLayout(ty);1157 const layout = self.unionLayout(ty, maybe_active_field);
1167 if (!layout.has_payload) {1158
1159 if (layout.payload_size == 0) {
1168 // No payload, so represent this as just the tag type.1160 // No payload, so represent this as just the tag type.
1169 return try self.resolveType(union_obj.enum_tag_ty.toType(), .indirect);1161 return try self.resolveType(union_obj.enum_tag_ty.toType(), .indirect);
1170 }1162 }
11711163
1172 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;1164 // TODO: We need to add the active field to the key, somehow.
1165 if (maybe_active_field == null) {
1166 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1167 }
11731168
1174 var member_types: [4]CacheRef = undefined;1169 var member_types: [4]CacheRef = undefined;
1175 var member_names: [4]CacheString = undefined;1170 var member_names: [4]CacheString = undefined;
...@@ -1182,10 +1177,10 @@ const DeclGen = struct {...@@ -1182,10 +1177,10 @@ const DeclGen = struct {
1182 member_names[layout.tag_index] = try self.spv.resolveString("(tag)");1177 member_names[layout.tag_index] = try self.spv.resolveString("(tag)");
1183 }1178 }
11841179
1185 if (layout.payload_size != 0) {1180 if (layout.active_field_size != 0) {
1186 const payload_ty_ref = try self.resolveType(layout.payload_ty, .indirect);1181 const active_payload_ty_ref = try self.resolveType(layout.active_field_ty, .indirect);
1187 member_types[layout.payload_index] = payload_ty_ref;1182 member_types[layout.active_field_index] = active_payload_ty_ref;
1188 member_names[layout.payload_index] = try self.spv.resolveString("(payload)");1183 member_names[layout.active_field_index] = try self.spv.resolveString("(payload)");
1189 }1184 }
11901185
1191 if (layout.payload_padding_size != 0) {1186 if (layout.payload_padding_size != 0) {
...@@ -1206,7 +1201,9 @@ const DeclGen = struct {...@@ -1206,7 +1201,9 @@ const DeclGen = struct {
1206 .member_names = member_names[0..layout.total_fields],1201 .member_names = member_names[0..layout.total_fields],
1207 } });1202 } });
12081203
1209 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });1204 if (maybe_active_field == null) {
1205 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
1206 }
1210 return ty_ref;1207 return ty_ref;
1211 }1208 }
12121209
...@@ -1354,12 +1351,12 @@ const DeclGen = struct {...@@ -1354,12 +1351,12 @@ const DeclGen = struct {
1354 .Pointer => {1351 .Pointer => {
1355 const ptr_info = ty.ptrInfo(mod);1352 const ptr_info = ty.ptrInfo(mod);
13561353
1357 // Note: Don't cache this pointer type, it would mess up the recursive pointer functionality
1358 // in ptrType()!
1359
1360 const storage_class = spvStorageClass(ptr_info.flags.address_space);1354 const storage_class = spvStorageClass(ptr_info.flags.address_space);
1361 const ptr_ty_ref = try self.ptrType(ptr_info.child.toType(), storage_class);1355 const child_ty_ref = try self.resolveType(ptr_info.child.toType(), .indirect);
13621356 const ptr_ty_ref = try self.spv.resolve(.{ .ptr_type = .{
1357 .storage_class = storage_class,
1358 .child_type = child_ty_ref,
1359 } });
1363 if (ptr_info.flags.size != .Slice) {1360 if (ptr_info.flags.size != .Slice) {
1364 return ptr_ty_ref;1361 return ptr_ty_ref;
1365 }1362 }
...@@ -1474,7 +1471,7 @@ const DeclGen = struct {...@@ -1474,7 +1471,7 @@ const DeclGen = struct {
1474 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });1471 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
1475 return ty_ref;1472 return ty_ref;
1476 },1473 },
1477 .Union => return try self.resolveUnionType(ty),1474 .Union => return try self.resolveUnionType(ty, null),
1478 .ErrorSet => return try self.intType(.unsigned, 16),1475 .ErrorSet => return try self.intType(.unsigned, 16),
1479 .ErrorUnion => {1476 .ErrorUnion => {
1480 const payload_ty = ty.errorUnionPayload(mod);1477 const payload_ty = ty.errorUnionPayload(mod);
...@@ -1588,16 +1585,14 @@ const DeclGen = struct {...@@ -1588,16 +1585,14 @@ const DeclGen = struct {
1588 }1585 }
15891586
1590 const UnionLayout = struct {1587 const UnionLayout = struct {
1591 /// If false, this union is represented1588 active_field: u32,
1592 /// by only an integer of the tag type.1589 active_field_ty: Type,
1593 has_payload: bool,1590 payload_size: u32,
1591
1594 tag_size: u32,1592 tag_size: u32,
1595 tag_index: u32,1593 tag_index: u32,
1596 /// Note: This is the size of the payload type itself, NOT the size of the ENTIRE payload.1594 active_field_size: u32,
1597 /// Use `has_payload` instead!!1595 active_field_index: u32,
1598 payload_ty: Type,
1599 payload_size: u32,
1600 payload_index: u32,
1601 payload_padding_size: u32,1596 payload_padding_size: u32,
1602 payload_padding_index: u32,1597 payload_padding_index: u32,
1603 padding_size: u32,1598 padding_size: u32,
...@@ -1605,19 +1600,23 @@ const DeclGen = struct {...@@ -1605,19 +1600,23 @@ const DeclGen = struct {
1605 total_fields: u32,1600 total_fields: u32,
1606 };1601 };
16071602
1608 fn unionLayout(self: *DeclGen, ty: Type) UnionLayout {1603 fn unionLayout(self: *DeclGen, ty: Type, maybe_active_field: ?usize) UnionLayout {
1609 const mod = self.module;1604 const mod = self.module;
1610 const ip = &mod.intern_pool;1605 const ip = &mod.intern_pool;
1611 const layout = ty.unionGetLayout(self.module);1606 const layout = ty.unionGetLayout(self.module);
1612 const union_obj = mod.typeToUnion(ty).?;1607 const union_obj = mod.typeToUnion(ty).?;
16131608
1609 const active_field = maybe_active_field orelse layout.most_aligned_field;
1610 const active_field_ty = union_obj.field_types.get(ip)[active_field].toType();
1611
1614 var union_layout = UnionLayout{1612 var union_layout = UnionLayout{
1615 .has_payload = layout.payload_size != 0,1613 .active_field = @intCast(active_field),
1614 .active_field_ty = active_field_ty,
1615 .payload_size = @intCast(layout.payload_size),
1616 .tag_size = @intCast(layout.tag_size),1616 .tag_size = @intCast(layout.tag_size),
1617 .tag_index = undefined,1617 .tag_index = undefined,
1618 .payload_ty = undefined,1618 .active_field_size = undefined,
1619 .payload_size = undefined,1619 .active_field_index = undefined,
1620 .payload_index = undefined,
1621 .payload_padding_size = undefined,1620 .payload_padding_size = undefined,
1622 .payload_padding_index = undefined,1621 .payload_padding_index = undefined,
1623 .padding_size = @intCast(layout.padding),1622 .padding_size = @intCast(layout.padding),
...@@ -1625,16 +1624,11 @@ const DeclGen = struct {...@@ -1625,16 +1624,11 @@ const DeclGen = struct {
1625 .total_fields = undefined,1624 .total_fields = undefined,
1626 };1625 };
16271626
1628 if (union_layout.has_payload) {1627 union_layout.active_field_size = if (active_field_ty.hasRuntimeBitsIgnoreComptime(mod))
1629 const most_aligned_field = layout.most_aligned_field;1628 @intCast(active_field_ty.abiSize(mod))
1630 const most_aligned_field_ty = union_obj.field_types.get(ip)[most_aligned_field].toType();1629 else
1631 union_layout.payload_ty = most_aligned_field_ty;1630 0;
1632 union_layout.payload_size = @intCast(most_aligned_field_ty.abiSize(mod));1631 union_layout.payload_padding_size = @intCast(layout.payload_size - union_layout.active_field_size);
1633 } else {
1634 union_layout.payload_size = 0;
1635 }
1636
1637 union_layout.payload_padding_size = @intCast(layout.payload_size - union_layout.payload_size);
16381632
1639 const tag_first = layout.tag_align.compare(.gte, layout.payload_align);1633 const tag_first = layout.tag_align.compare(.gte, layout.payload_align);
1640 var field_index: u32 = 0;1634 var field_index: u32 = 0;
...@@ -1644,8 +1638,8 @@ const DeclGen = struct {...@@ -1644,8 +1638,8 @@ const DeclGen = struct {
1644 field_index += 1;1638 field_index += 1;
1645 }1639 }
16461640
1647 if (union_layout.payload_size != 0) {1641 if (union_layout.active_field_size != 0) {
1648 union_layout.payload_index = field_index;1642 union_layout.active_field_index = field_index;
1649 field_index += 1;1643 field_index += 1;
1650 }1644 }
16511645
...@@ -1689,7 +1683,7 @@ const DeclGen = struct {...@@ -1689,7 +1683,7 @@ const DeclGen = struct {
1689 /// the name of an error in the text executor.1683 /// the name of an error in the text executor.
1690 fn generateTestEntryPoint(self: *DeclGen, name: []const u8, spv_test_decl_index: SpvModule.Decl.Index) !void {1684 fn generateTestEntryPoint(self: *DeclGen, name: []const u8, spv_test_decl_index: SpvModule.Decl.Index) !void {
1691 const anyerror_ty_ref = try self.resolveType(Type.anyerror, .direct);1685 const anyerror_ty_ref = try self.resolveType(Type.anyerror, .direct);
1692 const ptr_anyerror_ty_ref = try self.ptrType(Type.anyerror, .CrossWorkgroup);1686 const ptr_anyerror_ty_ref = try self.spv.ptrType(anyerror_ty_ref, .CrossWorkgroup);
1693 const void_ty_ref = try self.resolveType(Type.void, .direct);1687 const void_ty_ref = try self.resolveType(Type.void, .direct);
16941688
1695 const kernel_proto_ty_ref = try self.spv.resolve(.{ .function_type = .{1689 const kernel_proto_ty_ref = try self.spv.resolve(.{ .function_type = .{
...@@ -1724,7 +1718,6 @@ const DeclGen = struct {...@@ -1724,7 +1718,6 @@ const DeclGen = struct {
1724 .id_result = error_id,1718 .id_result = error_id,
1725 .function = test_id,1719 .function = test_id,
1726 });1720 });
1727 // Note: Convert to direct not required.
1728 try section.emit(self.spv.gpa, .OpStore, .{1721 try section.emit(self.spv.gpa, .OpStore, .{
1729 .pointer = p_error_id,1722 .pointer = p_error_id,
1730 .object = error_id,1723 .object = error_id,
...@@ -1829,7 +1822,8 @@ const DeclGen = struct {...@@ -1829,7 +1822,8 @@ const DeclGen = struct {
1829 else => final_storage_class,1822 else => final_storage_class,
1830 };1823 };
18311824
1832 const ptr_ty_ref = try self.ptrType(decl.ty, actual_storage_class);1825 const ty_ref = try self.resolveType(decl.ty, .indirect);
1826 const ptr_ty_ref = try self.spv.ptrType(ty_ref, actual_storage_class);
18331827
1834 const begin = self.spv.beginGlobal();1828 const begin = self.spv.beginGlobal();
1835 try self.spv.globals.section.emit(self.spv.gpa, .OpVariable, .{1829 try self.spv.globals.section.emit(self.spv.gpa, .OpVariable, .{
...@@ -1934,15 +1928,11 @@ const DeclGen = struct {...@@ -1934,15 +1928,11 @@ const DeclGen = struct {
1934 return try self.convertToDirect(result_ty, result_id);1928 return try self.convertToDirect(result_ty, result_id);
1935 }1929 }
19361930
1937 const MemoryOptions = struct {1931 fn load(self: *DeclGen, value_ty: Type, ptr_id: IdRef, is_volatile: bool) !IdRef {
1938 is_volatile: bool = false,
1939 };
1940
1941 fn load(self: *DeclGen, value_ty: Type, ptr_id: IdRef, options: MemoryOptions) !IdRef {
1942 const indirect_value_ty_ref = try self.resolveType(value_ty, .indirect);1932 const indirect_value_ty_ref = try self.resolveType(value_ty, .indirect);
1943 const result_id = self.spv.allocId();1933 const result_id = self.spv.allocId();
1944 const access = spec.MemoryAccess.Extended{1934 const access = spec.MemoryAccess.Extended{
1945 .Volatile = options.is_volatile,1935 .Volatile = is_volatile,
1946 };1936 };
1947 try self.func.body.emit(self.spv.gpa, .OpLoad, .{1937 try self.func.body.emit(self.spv.gpa, .OpLoad, .{
1948 .id_result_type = self.typeId(indirect_value_ty_ref),1938 .id_result_type = self.typeId(indirect_value_ty_ref),
...@@ -1953,10 +1943,10 @@ const DeclGen = struct {...@@ -1953,10 +1943,10 @@ const DeclGen = struct {
1953 return try self.convertToDirect(value_ty, result_id);1943 return try self.convertToDirect(value_ty, result_id);
1954 }1944 }
19551945
1956 fn store(self: *DeclGen, value_ty: Type, ptr_id: IdRef, value_id: IdRef, options: MemoryOptions) !void {1946 fn store(self: *DeclGen, value_ty: Type, ptr_id: IdRef, value_id: IdRef, is_volatile: bool) !void {
1957 const indirect_value_id = try self.convertToIndirect(value_ty, value_id);1947 const indirect_value_id = try self.convertToIndirect(value_ty, value_id);
1958 const access = spec.MemoryAccess.Extended{1948 const access = spec.MemoryAccess.Extended{
1959 .Volatile = options.is_volatile,1949 .Volatile = is_volatile,
1960 };1950 };
1961 try self.func.body.emit(self.spv.gpa, .OpStore, .{1951 try self.func.body.emit(self.spv.gpa, .OpStore, .{
1962 .pointer = ptr_id,1952 .pointer = ptr_id,
...@@ -2128,7 +2118,9 @@ const DeclGen = struct {...@@ -2128,7 +2118,9 @@ const DeclGen = struct {
2128 constituent.* = try self.convertToIndirect(child_ty, result_id);2118 constituent.* = try self.convertToIndirect(child_ty, result_id);
2129 }2119 }
21302120
2131 return try self.constructArray(ty, constituents);2121 const result_ty = try self.resolveType(child_ty, .indirect);
2122 const result_ty_ref = try self.spv.arrayType(vector_len, result_ty);
2123 return try self.constructArray(result_ty_ref, constituents);
2132 }2124 }
21332125
2134 const result_id = self.spv.allocId();2126 const result_id = self.spv.allocId();
...@@ -2189,7 +2181,7 @@ const DeclGen = struct {...@@ -2189,7 +2181,7 @@ const DeclGen = struct {
21892181
2190 const info = try self.arithmeticTypeInfo(result_ty);2182 const info = try self.arithmeticTypeInfo(result_ty);
2191 // TODO: Use fmin for OpenCL2183 // TODO: Use fmin for OpenCL
2192 const cmp_id = try self.cmp(op, Type.bool, result_ty, lhs_id, rhs_id);2184 const cmp_id = try self.cmp(op, result_ty, lhs_id, rhs_id);
2193 const selection_id = switch (info.class) {2185 const selection_id = switch (info.class) {
2194 .float => blk: {2186 .float => blk: {
2195 // cmp uses OpFOrd. When we have 0 [<>] nan this returns false,2187 // cmp uses OpFOrd. When we have 0 [<>] nan this returns false,
...@@ -2324,7 +2316,7 @@ const DeclGen = struct {...@@ -2324,7 +2316,7 @@ const DeclGen = struct {
2324 constituent.* = try self.arithOp(child_ty, lhs_index_id, rhs_index_id, fop, sop, uop, modular);2316 constituent.* = try self.arithOp(child_ty, lhs_index_id, rhs_index_id, fop, sop, uop, modular);
2325 }2317 }
23262318
2327 return self.constructArray(ty, constituents);2319 return self.constructArray(result_ty_ref, constituents);
2328 }2320 }
23292321
2330 // Binary operations are generally applicable to both scalar and vector operations2322 // Binary operations are generally applicable to both scalar and vector operations
...@@ -2480,11 +2472,11 @@ const DeclGen = struct {...@@ -2480,11 +2472,11 @@ const DeclGen = struct {
2480 // Construct the struct that Zig wants as result.2472 // Construct the struct that Zig wants as result.
2481 // The value should already be the correct type.2473 // The value should already be the correct type.
2482 const ov_id = try self.intFromBool(ov_ty_ref, overflowed_id);2474 const ov_id = try self.intFromBool(ov_ty_ref, overflowed_id);
2483 return try self.constructStruct(2475 const result_ty_ref = try self.resolveType(result_ty, .direct);
2484 result_ty,2476 return try self.constructStruct(result_ty_ref, &.{
2485 &.{ operand_ty, ov_ty },2477 value_id,
2486 &.{ value_id, ov_id },2478 ov_id,
2487 );2479 });
2488 }2480 }
24892481
2490 fn airShuffle(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {2482 fn airShuffle(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
...@@ -2642,7 +2634,6 @@ const DeclGen = struct {...@@ -2642,7 +2634,6 @@ const DeclGen = struct {
2642 fn cmp(2634 fn cmp(
2643 self: *DeclGen,2635 self: *DeclGen,
2644 op: std.math.CompareOperator,2636 op: std.math.CompareOperator,
2645 result_ty: Type,
2646 ty: Type,2637 ty: Type,
2647 lhs_id: IdRef,2638 lhs_id: IdRef,
2648 rhs_id: IdRef,2639 rhs_id: IdRef,
...@@ -2683,7 +2674,7 @@ const DeclGen = struct {...@@ -2683,7 +2674,7 @@ const DeclGen = struct {
2683 if (ty.optionalReprIsPayload(mod)) {2674 if (ty.optionalReprIsPayload(mod)) {
2684 assert(payload_ty.hasRuntimeBitsIgnoreComptime(mod));2675 assert(payload_ty.hasRuntimeBitsIgnoreComptime(mod));
2685 assert(!payload_ty.isSlice(mod));2676 assert(!payload_ty.isSlice(mod));
2686 return self.cmp(op, Type.bool, payload_ty, lhs_id, rhs_id);2677 return self.cmp(op, payload_ty, lhs_id, rhs_id);
2687 }2678 }
26882679
2689 const lhs_valid_id = if (payload_ty.hasRuntimeBitsIgnoreComptime(mod))2680 const lhs_valid_id = if (payload_ty.hasRuntimeBitsIgnoreComptime(mod))
...@@ -2696,7 +2687,7 @@ const DeclGen = struct {...@@ -2696,7 +2687,7 @@ const DeclGen = struct {
2696 else2687 else
2697 try self.convertToDirect(Type.bool, rhs_id);2688 try self.convertToDirect(Type.bool, rhs_id);
26982689
2699 const valid_cmp_id = try self.cmp(op, Type.bool, Type.bool, lhs_valid_id, rhs_valid_id);2690 const valid_cmp_id = try self.cmp(op, Type.bool, lhs_valid_id, rhs_valid_id);
2700 if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {2691 if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
2701 return valid_cmp_id;2692 return valid_cmp_id;
2702 }2693 }
...@@ -2707,7 +2698,7 @@ const DeclGen = struct {...@@ -2707,7 +2698,7 @@ const DeclGen = struct {
2707 const lhs_pl_id = try self.extractField(payload_ty, lhs_id, 0);2698 const lhs_pl_id = try self.extractField(payload_ty, lhs_id, 0);
2708 const rhs_pl_id = try self.extractField(payload_ty, rhs_id, 0);2699 const rhs_pl_id = try self.extractField(payload_ty, rhs_id, 0);
27092700
2710 const pl_cmp_id = try self.cmp(op, Type.bool, payload_ty, lhs_pl_id, rhs_pl_id);2701 const pl_cmp_id = try self.cmp(op, payload_ty, lhs_pl_id, rhs_pl_id);
27112702
2712 // op == .eq => lhs_valid == rhs_valid && lhs_pl == rhs_pl2703 // op == .eq => lhs_valid == rhs_valid && lhs_pl == rhs_pl
2713 // op == .neq => lhs_valid != rhs_valid || lhs_pl != rhs_pl2704 // op == .neq => lhs_valid != rhs_valid || lhs_pl != rhs_pl
...@@ -2729,6 +2720,7 @@ const DeclGen = struct {...@@ -2729,6 +2720,7 @@ const DeclGen = struct {
2729 .Vector => {2720 .Vector => {
2730 const child_ty = ty.childType(mod);2721 const child_ty = ty.childType(mod);
2731 const vector_len = ty.vectorLen(mod);2722 const vector_len = ty.vectorLen(mod);
2723 const bool_ty_ref_indirect = try self.resolveType(Type.bool, .indirect);
27322724
2733 var constituents = try self.gpa.alloc(IdRef, vector_len);2725 var constituents = try self.gpa.alloc(IdRef, vector_len);
2734 defer self.gpa.free(constituents);2726 defer self.gpa.free(constituents);
...@@ -2736,11 +2728,12 @@ const DeclGen = struct {...@@ -2736,11 +2728,12 @@ const DeclGen = struct {
2736 for (constituents, 0..) |*constituent, i| {2728 for (constituents, 0..) |*constituent, i| {
2737 const lhs_index_id = try self.extractField(child_ty, cmp_lhs_id, @intCast(i));2729 const lhs_index_id = try self.extractField(child_ty, cmp_lhs_id, @intCast(i));
2738 const rhs_index_id = try self.extractField(child_ty, cmp_rhs_id, @intCast(i));2730 const rhs_index_id = try self.extractField(child_ty, cmp_rhs_id, @intCast(i));
2739 const result_id = try self.cmp(op, Type.bool, child_ty, lhs_index_id, rhs_index_id);2731 const result_id = try self.cmp(op, child_ty, lhs_index_id, rhs_index_id);
2740 constituent.* = try self.convertToIndirect(Type.bool, result_id);2732 constituent.* = try self.convertToIndirect(Type.bool, result_id);
2741 }2733 }
27422734
2743 return try self.constructArray(result_ty, constituents);2735 const result_ty_ref = try self.spv.arrayType(vector_len, bool_ty_ref_indirect);
2736 return try self.constructArray(result_ty_ref, constituents);
2744 },2737 },
2745 else => unreachable,2738 else => unreachable,
2746 };2739 };
...@@ -2813,9 +2806,8 @@ const DeclGen = struct {...@@ -2813,9 +2806,8 @@ const DeclGen = struct {
2813 const lhs_id = try self.resolve(bin_op.lhs);2806 const lhs_id = try self.resolve(bin_op.lhs);
2814 const rhs_id = try self.resolve(bin_op.rhs);2807 const rhs_id = try self.resolve(bin_op.rhs);
2815 const ty = self.typeOf(bin_op.lhs);2808 const ty = self.typeOf(bin_op.lhs);
2816 const result_ty = self.typeOfIndex(inst);
28172809
2818 return try self.cmp(op, result_ty, ty, lhs_id, rhs_id);2810 return try self.cmp(op, ty, lhs_id, rhs_id);
2819 }2811 }
28202812
2821 fn airVectorCmp(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {2813 fn airVectorCmp(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
...@@ -2827,9 +2819,8 @@ const DeclGen = struct {...@@ -2827,9 +2819,8 @@ const DeclGen = struct {
2827 const rhs_id = try self.resolve(vec_cmp.rhs);2819 const rhs_id = try self.resolve(vec_cmp.rhs);
2828 const op = vec_cmp.compareOperator();2820 const op = vec_cmp.compareOperator();
2829 const ty = self.typeOf(vec_cmp.lhs);2821 const ty = self.typeOf(vec_cmp.lhs);
2830 const result_ty = self.typeOfIndex(inst);
28312822
2832 return try self.cmp(op, result_ty, ty, lhs_id, rhs_id);2823 return try self.cmp(op, ty, lhs_id, rhs_id);
2833 }2824 }
28342825
2835 fn bitCast(2826 fn bitCast(
...@@ -2874,17 +2865,23 @@ const DeclGen = struct {...@@ -2874,17 +2865,23 @@ const DeclGen = struct {
2874 return result_id;2865 return result_id;
2875 }2866 }
28762867
2877 const dst_ptr_ty_ref = try self.ptrType(dst_ty, .Function);2868 const src_ptr_ty_ref = try self.spv.ptrType(src_ty_ref, .Function);
2869 const dst_ptr_ty_ref = try self.spv.ptrType(dst_ty_ref, .Function);
28782870
2879 const tmp_id = try self.alloc(src_ty, .{ .storage_class = .Function });2871 const tmp_id = self.spv.allocId();
2880 try self.store(src_ty, tmp_id, src_id, .{});2872 try self.func.prologue.emit(self.spv.gpa, .OpVariable, .{
2873 .id_result_type = self.typeId(src_ptr_ty_ref),
2874 .id_result = tmp_id,
2875 .storage_class = .Function,
2876 });
2877 try self.store(src_ty, tmp_id, src_id, false);
2881 const casted_ptr_id = self.spv.allocId();2878 const casted_ptr_id = self.spv.allocId();
2882 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{2879 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
2883 .id_result_type = self.typeId(dst_ptr_ty_ref),2880 .id_result_type = self.typeId(dst_ptr_ty_ref),
2884 .id_result = casted_ptr_id,2881 .id_result = casted_ptr_id,
2885 .operand = tmp_id,2882 .operand = tmp_id,
2886 });2883 });
2887 return try self.load(dst_ty, casted_ptr_id, .{});2884 return try self.load(dst_ty, casted_ptr_id, false);
2888 }2885 }
28892886
2890 fn airBitCast(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {2887 fn airBitCast(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
...@@ -3063,6 +3060,7 @@ const DeclGen = struct {...@@ -3063,6 +3060,7 @@ const DeclGen = struct {
3063 const elem_ptr_ty = slice_ty.slicePtrFieldType(mod);3060 const elem_ptr_ty = slice_ty.slicePtrFieldType(mod);
30643061
3065 const elem_ptr_ty_ref = try self.resolveType(elem_ptr_ty, .direct);3062 const elem_ptr_ty_ref = try self.resolveType(elem_ptr_ty, .direct);
3063 const slice_ty_ref = try self.resolveType(slice_ty, .direct);
3066 const size_ty_ref = try self.sizeType();3064 const size_ty_ref = try self.sizeType();
30673065
3068 const array_ptr_id = try self.resolve(ty_op.operand);3066 const array_ptr_id = try self.resolve(ty_op.operand);
...@@ -3075,11 +3073,7 @@ const DeclGen = struct {...@@ -3075,11 +3073,7 @@ const DeclGen = struct {
3075 // Convert the pointer-to-array to a pointer to the first element.3073 // Convert the pointer-to-array to a pointer to the first element.
3076 try self.accessChain(elem_ptr_ty_ref, array_ptr_id, &.{0});3074 try self.accessChain(elem_ptr_ty_ref, array_ptr_id, &.{0});
30773075
3078 return try self.constructStruct(3076 return try self.constructStruct(slice_ty_ref, &.{ elem_ptr_id, len_id });
3079 slice_ty,
3080 &.{ elem_ptr_ty, Type.usize },
3081 &.{ elem_ptr_id, len_id },
3082 );
3083 }3077 }
30843078
3085 fn airSlice(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {3079 fn airSlice(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
...@@ -3089,16 +3083,13 @@ const DeclGen = struct {...@@ -3089,16 +3083,13 @@ const DeclGen = struct {
3089 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;3083 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
3090 const ptr_id = try self.resolve(bin_op.lhs);3084 const ptr_id = try self.resolve(bin_op.lhs);
3091 const len_id = try self.resolve(bin_op.rhs);3085 const len_id = try self.resolve(bin_op.rhs);
3092 const ptr_ty = self.typeOf(bin_op.lhs);
3093 const slice_ty = self.typeOfIndex(inst);3086 const slice_ty = self.typeOfIndex(inst);
3087 const slice_ty_ref = try self.resolveType(slice_ty, .direct);
30943088
3095 // Note: Types should not need to be converted to direct, these types3089 return try self.constructStruct(slice_ty_ref, &.{
3096 // dont need to be converted.3090 ptr_id, // Note: Type should not need to be converted to direct.
3097 return try self.constructStruct(3091 len_id, // Note: Type should not need to be converted to direct.
3098 slice_ty,3092 });
3099 &.{ ptr_ty, Type.usize },
3100 &.{ ptr_id, len_id },
3101 );
3102 }3093 }
31033094
3104 fn airAggregateInit(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {3095 fn airAggregateInit(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
...@@ -3108,6 +3099,7 @@ const DeclGen = struct {...@@ -3108,6 +3099,7 @@ const DeclGen = struct {
3108 const ip = &mod.intern_pool;3099 const ip = &mod.intern_pool;
3109 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;3100 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
3110 const result_ty = self.typeOfIndex(inst);3101 const result_ty = self.typeOfIndex(inst);
3102 const result_ty_ref = try self.resolveType(result_ty, .direct);
3111 const len: usize = @intCast(result_ty.arrayLen(mod));3103 const len: usize = @intCast(result_ty.arrayLen(mod));
3112 const elements: []const Air.Inst.Ref = @ptrCast(self.air.extra[ty_pl.payload..][0..len]);3104 const elements: []const Air.Inst.Ref = @ptrCast(self.air.extra[ty_pl.payload..][0..len]);
31133105
...@@ -3119,8 +3111,6 @@ const DeclGen = struct {...@@ -3119,8 +3111,6 @@ const DeclGen = struct {
3119 unreachable; // TODO3111 unreachable; // TODO
3120 }3112 }
31213113
3122 const types = try self.gpa.alloc(Type, elements.len);
3123 defer self.gpa.free(types);
3124 const constituents = try self.gpa.alloc(IdRef, elements.len);3114 const constituents = try self.gpa.alloc(IdRef, elements.len);
3125 defer self.gpa.free(constituents);3115 defer self.gpa.free(constituents);
3126 var index: usize = 0;3116 var index: usize = 0;
...@@ -3132,7 +3122,6 @@ const DeclGen = struct {...@@ -3132,7 +3122,6 @@ const DeclGen = struct {
3132 assert(field_ty.toType().hasRuntimeBits(mod));3122 assert(field_ty.toType().hasRuntimeBits(mod));
31333123
3134 const id = try self.resolve(element);3124 const id = try self.resolve(element);
3135 types[index] = field_ty.toType();
3136 constituents[index] = try self.convertToIndirect(field_ty.toType(), id);3125 constituents[index] = try self.convertToIndirect(field_ty.toType(), id);
3137 index += 1;3126 index += 1;
3138 }3127 }
...@@ -3146,7 +3135,6 @@ const DeclGen = struct {...@@ -3146,7 +3135,6 @@ const DeclGen = struct {
3146 assert(field_ty.hasRuntimeBitsIgnoreComptime(mod));3135 assert(field_ty.hasRuntimeBitsIgnoreComptime(mod));
31473136
3148 const id = try self.resolve(element);3137 const id = try self.resolve(element);
3149 types[index] = field_ty;
3150 constituents[index] = try self.convertToIndirect(field_ty, id);3138 constituents[index] = try self.convertToIndirect(field_ty, id);
3151 index += 1;3139 index += 1;
3152 }3140 }
...@@ -3154,11 +3142,7 @@ const DeclGen = struct {...@@ -3154,11 +3142,7 @@ const DeclGen = struct {
3154 else => unreachable,3142 else => unreachable,
3155 }3143 }
31563144
3157 return try self.constructStruct(3145 return try self.constructStruct(result_ty_ref, constituents[0..index]);
3158 result_ty,
3159 types[0..index],
3160 constituents[0..index],
3161 );
3162 },3146 },
3163 .Array => {3147 .Array => {
3164 const array_info = result_ty.arrayInfo(mod);3148 const array_info = result_ty.arrayInfo(mod);
...@@ -3175,7 +3159,7 @@ const DeclGen = struct {...@@ -3175,7 +3159,7 @@ const DeclGen = struct {
3175 elem_ids[n_elems - 1] = try self.constant(array_info.elem_type, sentinel_val, .indirect);3159 elem_ids[n_elems - 1] = try self.constant(array_info.elem_type, sentinel_val, .indirect);
3176 }3160 }
31773161
3178 return try self.constructArray(result_ty, elem_ids);3162 return try self.constructArray(result_ty_ref, elem_ids);
3179 },3163 },
3180 else => unreachable,3164 else => unreachable,
3181 }3165 }
...@@ -3260,14 +3244,15 @@ const DeclGen = struct {...@@ -3260,14 +3244,15 @@ const DeclGen = struct {
32603244
3261 const slice_ptr = try self.extractField(ptr_ty, slice_id, 0);3245 const slice_ptr = try self.extractField(ptr_ty, slice_id, 0);
3262 const elem_ptr = try self.ptrAccessChain(ptr_ty_ref, slice_ptr, index_id, &.{});3246 const elem_ptr = try self.ptrAccessChain(ptr_ty_ref, slice_ptr, index_id, &.{});
3263 return try self.load(slice_ty.childType(mod), elem_ptr, .{ .is_volatile = slice_ty.isVolatilePtr(mod) });3247 return try self.load(slice_ty.childType(mod), elem_ptr, slice_ty.isVolatilePtr(mod));
3264 }3248 }
32653249
3266 fn ptrElemPtr(self: *DeclGen, ptr_ty: Type, ptr_id: IdRef, index_id: IdRef) !IdRef {3250 fn ptrElemPtr(self: *DeclGen, ptr_ty: Type, ptr_id: IdRef, index_id: IdRef) !IdRef {
3267 const mod = self.module;3251 const mod = self.module;
3268 // Construct new pointer type for the resulting pointer3252 // Construct new pointer type for the resulting pointer
3269 const elem_ty = ptr_ty.elemType2(mod); // use elemType() so that we get T for *[N]T.3253 const elem_ty = ptr_ty.elemType2(mod); // use elemType() so that we get T for *[N]T.
3270 const elem_ptr_ty_ref = try self.ptrType(elem_ty, spvStorageClass(ptr_ty.ptrAddressSpace(mod)));3254 const elem_ty_ref = try self.resolveType(elem_ty, .direct);
3255 const elem_ptr_ty_ref = try self.spv.ptrType(elem_ty_ref, spvStorageClass(ptr_ty.ptrAddressSpace(mod)));
3271 if (ptr_ty.isSinglePointer(mod)) {3256 if (ptr_ty.isSinglePointer(mod)) {
3272 // Pointer-to-array. In this case, the resulting pointer is not of the same type3257 // Pointer-to-array. In this case, the resulting pointer is not of the same type
3273 // as the ptr_ty (we want a *T, not a *[N]T), and hence we need to use accessChain.3258 // as the ptr_ty (we want a *T, not a *[N]T), and hence we need to use accessChain.
...@@ -3303,7 +3288,9 @@ const DeclGen = struct {...@@ -3303,7 +3288,9 @@ const DeclGen = struct {
3303 const mod = self.module;3288 const mod = self.module;
3304 const bin_op = self.air.instructions.items(.data)[inst].bin_op;3289 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
3305 const array_ty = self.typeOf(bin_op.lhs);3290 const array_ty = self.typeOf(bin_op.lhs);
3291 const array_ty_ref = try self.resolveType(array_ty, .direct);
3306 const elem_ty = array_ty.childType(mod);3292 const elem_ty = array_ty.childType(mod);
3293 const elem_ty_ref = try self.resolveType(elem_ty, .indirect);
3307 const array_id = try self.resolve(bin_op.lhs);3294 const array_id = try self.resolve(bin_op.lhs);
3308 const index_id = try self.resolve(bin_op.rhs);3295 const index_id = try self.resolve(bin_op.rhs);
33093296
...@@ -3311,12 +3298,22 @@ const DeclGen = struct {...@@ -3311,12 +3298,22 @@ const DeclGen = struct {
3311 // For now, just generate a temporary and use that.3298 // For now, just generate a temporary and use that.
3312 // TODO: This backend probably also should use isByRef from llvm...3299 // TODO: This backend probably also should use isByRef from llvm...
33133300
3314 const elem_ptr_ty_ref = try self.ptrType(elem_ty, .Function);3301 const array_ptr_ty_ref = try self.spv.ptrType(array_ty_ref, .Function);
3302 const elem_ptr_ty_ref = try self.spv.ptrType(elem_ty_ref, .Function);
3303
3304 const tmp_id = self.spv.allocId();
3305 try self.func.prologue.emit(self.spv.gpa, .OpVariable, .{
3306 .id_result_type = self.typeId(array_ptr_ty_ref),
3307 .id_result = tmp_id,
3308 .storage_class = .Function,
3309 });
3310 try self.func.body.emit(self.spv.gpa, .OpStore, .{
3311 .pointer = tmp_id,
3312 .object = array_id,
3313 });
33153314
3316 const tmp_id = try self.alloc(array_ty, .{ .storage_class = .Function });
3317 try self.store(array_ty, tmp_id, array_id, .{});
3318 const elem_ptr_id = try self.accessChainId(elem_ptr_ty_ref, tmp_id, &.{index_id});3315 const elem_ptr_id = try self.accessChainId(elem_ptr_ty_ref, tmp_id, &.{index_id});
3319 return try self.load(elem_ty, elem_ptr_id, .{});3316 return try self.load(elem_ty, elem_ptr_id, false);
3320 }3317 }
33213318
3322 fn airPtrElemVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {3319 fn airPtrElemVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
...@@ -3329,7 +3326,7 @@ const DeclGen = struct {...@@ -3329,7 +3326,7 @@ const DeclGen = struct {
3329 const ptr_id = try self.resolve(bin_op.lhs);3326 const ptr_id = try self.resolve(bin_op.lhs);
3330 const index_id = try self.resolve(bin_op.rhs);3327 const index_id = try self.resolve(bin_op.rhs);
3331 const elem_ptr_id = try self.ptrElemPtr(ptr_ty, ptr_id, index_id);3328 const elem_ptr_id = try self.ptrElemPtr(ptr_ty, ptr_id, index_id);
3332 return try self.load(elem_ty, elem_ptr_id, .{ .is_volatile = ptr_ty.isVolatilePtr(mod) });3329 return try self.load(elem_ty, elem_ptr_id, ptr_ty.isVolatilePtr(mod));
3333 }3330 }
33343331
3335 fn airSetUnionTag(self: *DeclGen, inst: Air.Inst.Index) !void {3332 fn airSetUnionTag(self: *DeclGen, inst: Air.Inst.Index) !void {
...@@ -3337,21 +3334,22 @@ const DeclGen = struct {...@@ -3337,21 +3334,22 @@ const DeclGen = struct {
3337 const bin_op = self.air.instructions.items(.data)[inst].bin_op;3334 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
3338 const un_ptr_ty = self.typeOf(bin_op.lhs);3335 const un_ptr_ty = self.typeOf(bin_op.lhs);
3339 const un_ty = un_ptr_ty.childType(mod);3336 const un_ty = un_ptr_ty.childType(mod);
3340 const layout = self.unionLayout(un_ty);3337 const layout = self.unionLayout(un_ty, null);
33413338
3342 if (layout.tag_size == 0) return;3339 if (layout.tag_size == 0) return;
33433340
3344 const tag_ty = un_ty.unionTagTypeSafety(mod).?;3341 const tag_ty = un_ty.unionTagTypeSafety(mod).?;
3345 const tag_ptr_ty_ref = try self.ptrType(tag_ty, spvStorageClass(un_ptr_ty.ptrAddressSpace(mod)));3342 const tag_ty_ref = try self.resolveType(tag_ty, .indirect);
3343 const tag_ptr_ty_ref = try self.spv.ptrType(tag_ty_ref, spvStorageClass(un_ptr_ty.ptrAddressSpace(mod)));
33463344
3347 const union_ptr_id = try self.resolve(bin_op.lhs);3345 const union_ptr_id = try self.resolve(bin_op.lhs);
3348 const new_tag_id = try self.resolve(bin_op.rhs);3346 const new_tag_id = try self.resolve(bin_op.rhs);
33493347
3350 if (!layout.has_payload) {3348 if (layout.payload_size == 0) {
3351 try self.store(tag_ty, union_ptr_id, new_tag_id, .{ .is_volatile = un_ptr_ty.isVolatilePtr(mod) });3349 try self.store(tag_ty, union_ptr_id, new_tag_id, un_ptr_ty.isVolatilePtr(mod));
3352 } else {3350 } else {
3353 const ptr_id = try self.accessChain(tag_ptr_ty_ref, union_ptr_id, &.{layout.tag_index});3351 const ptr_id = try self.accessChain(tag_ptr_ty_ref, union_ptr_id, &.{layout.tag_index});
3354 try self.store(tag_ty, ptr_id, new_tag_id, .{ .is_volatile = un_ptr_ty.isVolatilePtr(mod) });3352 try self.store(tag_ty, ptr_id, new_tag_id, un_ptr_ty.isVolatilePtr(mod));
3355 }3353 }
3356 }3354 }
33573355
...@@ -3362,11 +3360,11 @@ const DeclGen = struct {...@@ -3362,11 +3360,11 @@ const DeclGen = struct {
3362 const un_ty = self.typeOf(ty_op.operand);3360 const un_ty = self.typeOf(ty_op.operand);
33633361
3364 const mod = self.module;3362 const mod = self.module;
3365 const layout = self.unionLayout(un_ty);3363 const layout = self.unionLayout(un_ty, null);
3366 if (layout.tag_size == 0) return null;3364 if (layout.tag_size == 0) return null;
33673365
3368 const union_handle = try self.resolve(ty_op.operand);3366 const union_handle = try self.resolve(ty_op.operand);
3369 if (!layout.has_payload) return union_handle;3367 if (layout.payload_size == 0) return union_handle;
33703368
3371 const tag_ty = un_ty.unionTagTypeSafety(mod).?;3369 const tag_ty = un_ty.unionTagTypeSafety(mod).?;
3372 return try self.extractField(tag_ty, union_handle, layout.tag_index);3370 return try self.extractField(tag_ty, union_handle, layout.tag_index);
...@@ -3379,8 +3377,8 @@ const DeclGen = struct {...@@ -3379,8 +3377,8 @@ const DeclGen = struct {
3379 payload: ?IdRef,3377 payload: ?IdRef,
3380 ) !IdRef {3378 ) !IdRef {
3381 // To initialize a union, generate a temporary variable with the3379 // To initialize a union, generate a temporary variable with the
3382 // union type, then get the field pointer and pointer-cast it to the3380 // type that has the right field active, then pointer-cast and store
3383 // right type to store it. Finally load the entire union.3381 // the active field, and finally load and return the entire union.
33843382
3385 const mod = self.module;3383 const mod = self.module;
3386 const ip = &mod.intern_pool;3384 const ip = &mod.intern_pool;
...@@ -3391,7 +3389,7 @@ const DeclGen = struct {...@@ -3391,7 +3389,7 @@ const DeclGen = struct {
3391 }3389 }
33923390
3393 const maybe_tag_ty = ty.unionTagTypeSafety(mod);3391 const maybe_tag_ty = ty.unionTagTypeSafety(mod);
3394 const layout = self.unionLayout(ty);3392 const layout = self.unionLayout(ty, active_field);
33953393
3396 const tag_int = if (layout.tag_size != 0) blk: {3394 const tag_int = if (layout.tag_size != 0) blk: {
3397 const tag_ty = maybe_tag_ty.?;3395 const tag_ty = maybe_tag_ty.?;
...@@ -3402,34 +3400,42 @@ const DeclGen = struct {...@@ -3402,34 +3400,42 @@ const DeclGen = struct {
3402 break :blk tag_int_val.toUnsignedInt(mod);3400 break :blk tag_int_val.toUnsignedInt(mod);
3403 } else 0;3401 } else 0;
34043402
3405 if (!layout.has_payload) {3403 if (layout.payload_size == 0) {
3406 const tag_ty_ref = try self.resolveType(maybe_tag_ty.?, .direct);3404 const tag_ty_ref = try self.resolveType(maybe_tag_ty.?, .direct);
3407 return try self.constInt(tag_ty_ref, tag_int);3405 return try self.constInt(tag_ty_ref, tag_int);
3408 }3406 }
34093407
3410 const tmp_id = try self.alloc(ty, .{ .storage_class = .Function });3408 const un_active_ty_ref = try self.resolveUnionType(ty, active_field);
3409 const un_active_ptr_ty_ref = try self.spv.ptrType(un_active_ty_ref, .Function);
3410 const un_general_ty_ref = try self.resolveType(ty, .direct);
3411 const un_general_ptr_ty_ref = try self.spv.ptrType(un_general_ty_ref, .Function);
3412
3413 const tmp_id = self.spv.allocId();
3414 try self.func.prologue.emit(self.spv.gpa, .OpVariable, .{
3415 .id_result_type = self.typeId(un_active_ptr_ty_ref),
3416 .id_result = tmp_id,
3417 .storage_class = .Function,
3418 });
34113419
3412 if (layout.tag_size != 0) {3420 if (layout.tag_size != 0) {
3413 const tag_ty_ref = try self.resolveType(maybe_tag_ty.?, .direct);3421 const tag_ty_ref = try self.resolveType(maybe_tag_ty.?, .direct);
3414 const tag_ptr_ty_ref = try self.ptrType(maybe_tag_ty.?, .Function);3422 const tag_ptr_ty_ref = try self.spv.ptrType(tag_ty_ref, .Function);
3415 const ptr_id = try self.accessChain(tag_ptr_ty_ref, tmp_id, &.{@as(u32, @intCast(layout.tag_index))});3423 const ptr_id = try self.accessChain(tag_ptr_ty_ref, tmp_id, &.{@as(u32, @intCast(layout.tag_index))});
3416 const tag_id = try self.constInt(tag_ty_ref, tag_int);3424 const tag_id = try self.constInt(tag_ty_ref, tag_int);
3417 try self.store(maybe_tag_ty.?, ptr_id, tag_id, .{});3425 try self.func.body.emit(self.spv.gpa, .OpStore, .{
3426 .pointer = ptr_id,
3427 .object = tag_id,
3428 });
3418 }3429 }
34193430
3420 const payload_ty = union_ty.field_types.get(ip)[active_field].toType();3431 if (layout.active_field_size != 0) {
3421 if (payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {3432 const active_field_ty_ref = try self.resolveType(layout.active_field_ty, .indirect);
3422 const pl_ptr_ty_ref = try self.ptrType(layout.payload_ty, .Function);3433 const active_field_ptr_ty_ref = try self.spv.ptrType(active_field_ty_ref, .Function);
3423 const pl_ptr_id = try self.accessChain(pl_ptr_ty_ref, tmp_id, &.{layout.payload_index});3434 const ptr_id = try self.accessChain(active_field_ptr_ty_ref, tmp_id, &.{@as(u32, @intCast(layout.active_field_index))});
3424 const active_pl_ptr_ty_ref = try self.ptrType(payload_ty, .Function);3435 try self.func.body.emit(self.spv.gpa, .OpStore, .{
3425 const active_pl_ptr_id = self.spv.allocId();3436 .pointer = ptr_id,
3426 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{3437 .object = payload.?,
3427 .id_result_type = self.typeId(active_pl_ptr_ty_ref),
3428 .id_result = active_pl_ptr_id,
3429 .operand = pl_ptr_id,
3430 });3438 });
3431
3432 try self.store(payload_ty, active_pl_ptr_id, payload.?, .{});
3433 } else {3439 } else {
3434 assert(payload == null);3440 assert(payload == null);
3435 }3441 }
...@@ -3437,21 +3443,34 @@ const DeclGen = struct {...@@ -3437,21 +3443,34 @@ const DeclGen = struct {
3437 // Just leave the padding fields uninitialized...3443 // Just leave the padding fields uninitialized...
3438 // TODO: Or should we initialize them with undef explicitly?3444 // TODO: Or should we initialize them with undef explicitly?
34393445
3440 return try self.load(ty, tmp_id, .{});3446 // Now cast the pointer and load it as the 'generic' union type.
3447
3448 const casted_var_id = self.spv.allocId();
3449 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
3450 .id_result_type = self.typeId(un_general_ptr_ty_ref),
3451 .id_result = casted_var_id,
3452 .operand = tmp_id,
3453 });
3454
3455 const result_id = self.spv.allocId();
3456 try self.func.body.emit(self.spv.gpa, .OpLoad, .{
3457 .id_result_type = self.typeId(un_general_ty_ref),
3458 .id_result = result_id,
3459 .pointer = casted_var_id,
3460 });
3461
3462 return result_id;
3441 }3463 }
34423464
3443 fn airUnionInit(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {3465 fn airUnionInit(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
3444 if (self.liveness.isUnused(inst)) return null;3466 if (self.liveness.isUnused(inst)) return null;
34453467
3446 const mod = self.module;
3447 const ip = &mod.intern_pool;
3448 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;3468 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
3449 const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data;3469 const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data;
3450 const ty = self.typeOfIndex(inst);3470 const ty = self.typeOfIndex(inst);
3471 const layout = self.unionLayout(ty, extra.field_index);
34513472
3452 const union_obj = mod.typeToUnion(ty).?;3473 const payload = if (layout.active_field_size != 0)
3453 const field_ty = union_obj.field_types.get(ip)[extra.field_index].toType();
3454 const payload = if (field_ty.hasRuntimeBitsIgnoreComptime(mod))
3455 try self.resolve(extra.init)3474 try self.resolve(extra.init)
3456 else3475 else
3457 null;3476 null;
...@@ -3480,24 +3499,30 @@ const DeclGen = struct {...@@ -3480,24 +3499,30 @@ const DeclGen = struct {
3480 .Union => switch (object_ty.containerLayout(mod)) {3499 .Union => switch (object_ty.containerLayout(mod)) {
3481 .Packed => unreachable, // TODO3500 .Packed => unreachable, // TODO
3482 else => {3501 else => {
3483 // Store, ptr-elem-ptr, pointer-cast, load3502 // Store, pointer-cast, load
3484 const layout = self.unionLayout(object_ty);3503 const un_general_ty_ref = try self.resolveType(object_ty, .indirect);
3485 assert(layout.has_payload);3504 const un_general_ptr_ty_ref = try self.spv.ptrType(un_general_ty_ref, .Function);
34863505 const un_active_ty_ref = try self.resolveUnionType(object_ty, field_index);
3487 const tmp_id = try self.alloc(object_ty, .{ .storage_class = .Function });3506 const un_active_ptr_ty_ref = try self.spv.ptrType(un_active_ty_ref, .Function);
3488 try self.store(object_ty, tmp_id, object_id, .{});3507 const field_ty_ref = try self.resolveType(field_ty, .indirect);
34893508 const field_ptr_ty_ref = try self.spv.ptrType(field_ty_ref, .Function);
3490 const pl_ptr_ty_ref = try self.ptrType(layout.payload_ty, .Function);3509
3491 const pl_ptr_id = try self.accessChain(pl_ptr_ty_ref, tmp_id, &.{layout.payload_index});3510 const tmp_id = self.spv.allocId();
34923511 try self.func.prologue.emit(self.spv.gpa, .OpVariable, .{
3493 const active_pl_ptr_ty_ref = try self.ptrType(field_ty, .Function);3512 .id_result_type = self.typeId(un_general_ptr_ty_ref),
3494 const active_pl_ptr_id = self.spv.allocId();3513 .id_result = tmp_id,
3514 .storage_class = .Function,
3515 });
3516 try self.store(object_ty, tmp_id, object_id, false);
3517 const casted_tmp_id = self.spv.allocId();
3495 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{3518 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
3496 .id_result_type = self.typeId(active_pl_ptr_ty_ref),3519 .id_result_type = self.typeId(un_active_ptr_ty_ref),
3497 .id_result = active_pl_ptr_id,3520 .id_result = casted_tmp_id,
3498 .operand = pl_ptr_id,3521 .operand = tmp_id,
3499 });3522 });
3500 return try self.load(field_ty, active_pl_ptr_id, .{});3523 const layout = self.unionLayout(object_ty, field_index);
3524 const field_ptr_id = try self.accessChain(field_ptr_ty_ref, casted_tmp_id, &.{layout.active_field_index});
3525 return try self.load(field_ty, field_ptr_id, false);
3501 },3526 },
3502 },3527 },
3503 else => unreachable,3528 else => unreachable,
...@@ -3556,24 +3581,18 @@ const DeclGen = struct {...@@ -3556,24 +3581,18 @@ const DeclGen = struct {
3556 .Union => switch (object_ty.containerLayout(mod)) {3581 .Union => switch (object_ty.containerLayout(mod)) {
3557 .Packed => unreachable, // TODO3582 .Packed => unreachable, // TODO
3558 else => {3583 else => {
3559 const layout = self.unionLayout(object_ty);
3560 if (!layout.has_payload) {
3561 // Asked to get a pointer to a zero-sized field. Just lower this
3562 // to undefined, there is no reason to make it be a valid pointer.
3563 return try self.spv.constUndef(result_ty_ref);
3564 }
3565
3566 const storage_class = spvStorageClass(object_ptr_ty.ptrAddressSpace(mod));3584 const storage_class = spvStorageClass(object_ptr_ty.ptrAddressSpace(mod));
3567 const pl_ptr_ty_ref = try self.ptrType(layout.payload_ty, storage_class);3585 const un_active_ty_ref = try self.resolveUnionType(object_ty, field_index);
3568 const pl_ptr_id = try self.accessChain(pl_ptr_ty_ref, object_ptr, &.{layout.payload_index});3586 const un_active_ptr_ty_ref = try self.spv.ptrType(un_active_ty_ref, storage_class);
35693587
3570 const active_pl_ptr_id = self.spv.allocId();3588 const casted_id = self.spv.allocId();
3571 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{3589 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
3572 .id_result_type = self.typeId(result_ty_ref),3590 .id_result_type = self.typeId(un_active_ptr_ty_ref),
3573 .id_result = active_pl_ptr_id,3591 .id_result = casted_id,
3574 .operand = pl_ptr_id,3592 .operand = object_ptr,
3575 });3593 });
3576 return active_pl_ptr_id;3594 const layout = self.unionLayout(object_ty, field_index);
3595 return try self.accessChain(result_ty_ref, casted_id, &.{layout.active_field_index});
3577 },3596 },
3578 },3597 },
3579 else => unreachable,3598 else => unreachable,
...@@ -3589,13 +3608,23 @@ const DeclGen = struct {...@@ -3589,13 +3608,23 @@ const DeclGen = struct {
3589 return try self.structFieldPtr(result_ptr_ty, struct_ptr_ty, struct_ptr, field_index);3608 return try self.structFieldPtr(result_ptr_ty, struct_ptr_ty, struct_ptr, field_index);
3590 }3609 }
35913610
3592 const AllocOptions = struct {3611 /// We cannot use an OpVariable directly in an OpSpecConstantOp, but we can
3593 initializer: ?IdRef = null,3612 /// after we insert a dummy AccessChain...
3594 /// The final storage class of the pointer. This may be either `.Generic` or `.Function`.3613 /// TODO: Get rid of this
3595 /// In either case, the local is allocated in the `.Function` storage class, and optionally3614 fn makePointerConstant(
3596 /// cast back to `.Generic`.3615 self: *DeclGen,
3597 storage_class: StorageClass = .Generic,3616 section: *SpvSection,
3598 };3617 ptr_ty_ref: CacheRef,
3618 ptr_id: IdRef,
3619 ) !IdRef {
3620 const result_id = self.spv.allocId();
3621 try section.emitSpecConstantOp(self.spv.gpa, .OpInBoundsAccessChain, .{
3622 .id_result_type = self.typeId(ptr_ty_ref),
3623 .id_result = result_id,
3624 .base = ptr_id,
3625 });
3626 return result_id;
3627 }
35993628
3600 // Allocate a function-local variable, with possible initializer.3629 // Allocate a function-local variable, with possible initializer.
3601 // This function returns a pointer to a variable of type `ty_ref`,3630 // This function returns a pointer to a variable of type `ty_ref`,
...@@ -3603,36 +3632,30 @@ const DeclGen = struct {...@@ -3603,36 +3632,30 @@ const DeclGen = struct {
3603 // placed in the Function address space.3632 // placed in the Function address space.
3604 fn alloc(3633 fn alloc(
3605 self: *DeclGen,3634 self: *DeclGen,
3606 ty: Type,3635 ty_ref: CacheRef,
3607 options: AllocOptions,3636 initializer: ?IdRef,
3608 ) !IdRef {3637 ) !IdRef {
3609 const ptr_fn_ty_ref = try self.ptrType(ty, .Function);3638 const fn_ptr_ty_ref = try self.spv.ptrType(ty_ref, .Function);
3639 const general_ptr_ty_ref = try self.spv.ptrType(ty_ref, .Generic);
36103640
3611 // SPIR-V requires that OpVariable declarations for locals go into the first block, so we are just going to3641 // SPIR-V requires that OpVariable declarations for locals go into the first block, so we are just going to
3612 // directly generate them into func.prologue instead of the body.3642 // directly generate them into func.prologue instead of the body.
3613 const var_id = self.spv.allocId();3643 const var_id = self.spv.allocId();
3614 try self.func.prologue.emit(self.spv.gpa, .OpVariable, .{3644 try self.func.prologue.emit(self.spv.gpa, .OpVariable, .{
3615 .id_result_type = self.typeId(ptr_fn_ty_ref),3645 .id_result_type = self.typeId(fn_ptr_ty_ref),
3616 .id_result = var_id,3646 .id_result = var_id,
3617 .storage_class = .Function,3647 .storage_class = .Function,
3618 .initializer = options.initializer,3648 .initializer = initializer,
3619 });3649 });
36203650
3621 switch (options.storage_class) {3651 // Convert to a generic pointer
3622 .Generic => {3652 const result_id = self.spv.allocId();
3623 const ptr_gn_ty_ref = try self.ptrType(ty, .Generic);3653 try self.func.body.emit(self.spv.gpa, .OpPtrCastToGeneric, .{
3624 // Convert to a generic pointer3654 .id_result_type = self.typeId(general_ptr_ty_ref),
3625 const result_id = self.spv.allocId();3655 .id_result = result_id,
3626 try self.func.body.emit(self.spv.gpa, .OpPtrCastToGeneric, .{3656 .pointer = var_id,
3627 .id_result_type = self.typeId(ptr_gn_ty_ref),3657 });
3628 .id_result = result_id,3658 return result_id;
3629 .pointer = var_id,
3630 });
3631 return result_id;
3632 },
3633 .Function => return var_id,
3634 else => unreachable,
3635 }
3636 }3659 }
36373660
3638 fn airAlloc(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {3661 fn airAlloc(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
...@@ -3641,7 +3664,8 @@ const DeclGen = struct {...@@ -3641,7 +3664,8 @@ const DeclGen = struct {
3641 const ptr_ty = self.typeOfIndex(inst);3664 const ptr_ty = self.typeOfIndex(inst);
3642 assert(ptr_ty.ptrAddressSpace(mod) == .generic);3665 assert(ptr_ty.ptrAddressSpace(mod) == .generic);
3643 const child_ty = ptr_ty.childType(mod);3666 const child_ty = ptr_ty.childType(mod);
3644 return try self.alloc(child_ty, .{});3667 const child_ty_ref = try self.resolveType(child_ty, .indirect);
3668 return try self.alloc(child_ty_ref, null);
3645 }3669 }
36463670
3647 fn airArg(self: *DeclGen) IdRef {3671 fn airArg(self: *DeclGen) IdRef {
...@@ -3756,7 +3780,7 @@ const DeclGen = struct {...@@ -3756,7 +3780,7 @@ const DeclGen = struct {
3756 const operand = try self.resolve(ty_op.operand);3780 const operand = try self.resolve(ty_op.operand);
3757 if (!ptr_ty.isVolatilePtr(mod) and self.liveness.isUnused(inst)) return null;3781 if (!ptr_ty.isVolatilePtr(mod) and self.liveness.isUnused(inst)) return null;
37583782
3759 return try self.load(elem_ty, operand, .{ .is_volatile = ptr_ty.isVolatilePtr(mod) });3783 return try self.load(elem_ty, operand, ptr_ty.isVolatilePtr(mod));
3760 }3784 }
37613785
3762 fn airStore(self: *DeclGen, inst: Air.Inst.Index) !void {3786 fn airStore(self: *DeclGen, inst: Air.Inst.Index) !void {
...@@ -3766,7 +3790,7 @@ const DeclGen = struct {...@@ -3766,7 +3790,7 @@ const DeclGen = struct {
3766 const ptr = try self.resolve(bin_op.lhs);3790 const ptr = try self.resolve(bin_op.lhs);
3767 const value = try self.resolve(bin_op.rhs);3791 const value = try self.resolve(bin_op.rhs);
37683792
3769 try self.store(elem_ty, ptr, value, .{ .is_volatile = ptr_ty.isVolatilePtr(self.module) });3793 try self.store(elem_ty, ptr, value, ptr_ty.isVolatilePtr(self.module));
3770 }3794 }
37713795
3772 fn airLoop(self: *DeclGen, inst: Air.Inst.Index) !void {3796 fn airLoop(self: *DeclGen, inst: Air.Inst.Index) !void {
...@@ -3830,7 +3854,7 @@ const DeclGen = struct {...@@ -3830,7 +3854,7 @@ const DeclGen = struct {
3830 }3854 }
38313855
3832 const ptr = try self.resolve(un_op);3856 const ptr = try self.resolve(un_op);
3833 const value = try self.load(ret_ty, ptr, .{ .is_volatile = ptr_ty.isVolatilePtr(mod) });3857 const value = try self.load(ret_ty, ptr, ptr_ty.isVolatilePtr(mod));
3834 try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{3858 try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{
3835 .value = value,3859 .value = value,
3836 });3860 });
...@@ -3956,11 +3980,8 @@ const DeclGen = struct {...@@ -3956,11 +3980,8 @@ const DeclGen = struct {
3956 members[eu_layout.errorFieldIndex()] = operand_id;3980 members[eu_layout.errorFieldIndex()] = operand_id;
3957 members[eu_layout.payloadFieldIndex()] = try self.spv.constUndef(payload_ty_ref);3981 members[eu_layout.payloadFieldIndex()] = try self.spv.constUndef(payload_ty_ref);
39583982
3959 var types: [2]Type = undefined;3983 const err_union_ty_ref = try self.resolveType(err_union_ty, .direct);
3960 types[eu_layout.errorFieldIndex()] = Type.anyerror;3984 return try self.constructStruct(err_union_ty_ref, &members);
3961 types[eu_layout.payloadFieldIndex()] = payload_ty;
3962
3963 return try self.constructStruct(err_union_ty, &types, &members);
3964 }3985 }
39653986
3966 fn airWrapErrUnionPayload(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {3987 fn airWrapErrUnionPayload(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
...@@ -3981,11 +4002,8 @@ const DeclGen = struct {...@@ -3981,11 +4002,8 @@ const DeclGen = struct {
3981 members[eu_layout.errorFieldIndex()] = try self.constInt(err_ty_ref, 0);4002 members[eu_layout.errorFieldIndex()] = try self.constInt(err_ty_ref, 0);
3982 members[eu_layout.payloadFieldIndex()] = try self.convertToIndirect(payload_ty, operand_id);4003 members[eu_layout.payloadFieldIndex()] = try self.convertToIndirect(payload_ty, operand_id);
39834004
3984 var types: [2]Type = undefined;4005 const err_union_ty_ref = try self.resolveType(err_union_ty, .direct);
3985 types[eu_layout.errorFieldIndex()] = Type.anyerror;4006 return try self.constructStruct(err_union_ty_ref, &members);
3986 types[eu_layout.payloadFieldIndex()] = payload_ty;
3987
3988 return try self.constructStruct(err_union_ty, &types, &members);
3989 }4007 }
39904008
3991 fn airIsNull(self: *DeclGen, inst: Air.Inst.Index, pred: enum { is_null, is_non_null }) !?IdRef {4009 fn airIsNull(self: *DeclGen, inst: Air.Inst.Index, pred: enum { is_null, is_non_null }) !?IdRef {
...@@ -4019,7 +4037,7 @@ const DeclGen = struct {...@@ -4019,7 +4037,7 @@ const DeclGen = struct {
4019 .is_null => .eq,4037 .is_null => .eq,
4020 .is_non_null => .neq,4038 .is_non_null => .neq,
4021 };4039 };
4022 return try self.cmp(op, Type.bool, ptr_ty, ptr_id, null_id);4040 return try self.cmp(op, ptr_ty, ptr_id, null_id);
4023 }4041 }
40244042
4025 const is_non_null_id = if (payload_ty.hasRuntimeBitsIgnoreComptime(mod))4043 const is_non_null_id = if (payload_ty.hasRuntimeBitsIgnoreComptime(mod))
...@@ -4117,10 +4135,10 @@ const DeclGen = struct {...@@ -4117,10 +4135,10 @@ const DeclGen = struct {
4117 return operand_id;4135 return operand_id;
4118 }4136 }
41194137
4138 const optional_ty_ref = try self.resolveType(optional_ty, .direct);
4120 const payload_id = try self.convertToIndirect(payload_ty, operand_id);4139 const payload_id = try self.convertToIndirect(payload_ty, operand_id);
4121 const members = [_]IdRef{ payload_id, try self.constBool(true, .indirect) };4140 const members = [_]IdRef{ payload_id, try self.constBool(true, .indirect) };
4122 const types = [_]Type{ payload_ty, Type.bool };4141 return try self.constructStruct(optional_ty_ref, &members);
4123 return try self.constructStruct(optional_ty, &types, &members);
4124 }4142 }
41254143
4126 fn airSwitchBr(self: *DeclGen, inst: Air.Inst.Index) !void {4144 fn airSwitchBr(self: *DeclGen, inst: Air.Inst.Index) !void {
...@@ -4402,7 +4420,6 @@ const DeclGen = struct {...@@ -4402,7 +4420,6 @@ const DeclGen = struct {
4402 }4420 }
44034421
4404 // TODO: Multiple results4422 // TODO: Multiple results
4405 // TODO: Check that the output type from assembly is the same as the type actually expected by Zig.
4406 }4423 }
44074424
4408 return null;4425 return null;
src/codegen/spirv/Assembler.zig+4-10
...@@ -304,16 +304,10 @@ fn processTypeInstruction(self: *Assembler) !AsmValue {...@@ -304,16 +304,10 @@ fn processTypeInstruction(self: *Assembler) !AsmValue {
304 // and so some consideration must be taken when entering this in the type system.304 // and so some consideration must be taken when entering this in the type system.
305 return self.todo("process OpTypeArray", .{});305 return self.todo("process OpTypeArray", .{});
306 },306 },
307 .OpTypePointer => blk: {307 .OpTypePointer => try self.spv.ptrType(
308 break :blk try self.spv.resolve(.{308 try self.resolveTypeRef(operands[2].ref_id),
309 .ptr_type = .{309 @as(spec.StorageClass, @enumFromInt(operands[1].value)),
310 .storage_class = @enumFromInt(operands[1].value),310 ),
311 .child_type = try self.resolveTypeRef(operands[2].ref_id),
312 // TODO: This should be a proper reference resolved via OpTypeForwardPointer
313 .fwd = @enumFromInt(std.math.maxInt(u32)),
314 },
315 });
316 },
317 .OpTypeFunction => blk: {311 .OpTypeFunction => blk: {
318 const param_operands = operands[2..];312 const param_operands = operands[2..];
319 const param_types = try self.spv.gpa.alloc(CacheRef, param_operands.len);313 const param_types = try self.spv.gpa.alloc(CacheRef, param_operands.len);
src/codegen/spirv/Cache.zig+124-182
...@@ -22,8 +22,6 @@ const Opcode = spec.Opcode;...@@ -22,8 +22,6 @@ const Opcode = spec.Opcode;
22const IdResult = spec.IdResult;22const IdResult = spec.IdResult;
23const StorageClass = spec.StorageClass;23const StorageClass = spec.StorageClass;
2424
25const InternPool = @import("../../InternPool.zig");
26
27const Self = @This();25const Self = @This();
2826
29map: std.AutoArrayHashMapUnmanaged(void, void) = .{},27map: std.AutoArrayHashMapUnmanaged(void, void) = .{},
...@@ -33,8 +31,6 @@ extra: std.ArrayListUnmanaged(u32) = .{},...@@ -33,8 +31,6 @@ extra: std.ArrayListUnmanaged(u32) = .{},
33string_bytes: std.ArrayListUnmanaged(u8) = .{},31string_bytes: std.ArrayListUnmanaged(u8) = .{},
34strings: std.AutoArrayHashMapUnmanaged(void, u32) = .{},32strings: std.AutoArrayHashMapUnmanaged(void, u32) = .{},
3533
36recursive_ptrs: std.AutoHashMapUnmanaged(Ref, void) = .{},
37
38const Item = struct {34const Item = struct {
39 tag: Tag,35 tag: Tag,
40 /// The result-id that this item uses.36 /// The result-id that this item uses.
...@@ -66,21 +62,18 @@ const Tag = enum {...@@ -66,21 +62,18 @@ const Tag = enum {
66 /// Function (proto)type62 /// Function (proto)type
67 /// data is payload to FunctionType63 /// data is payload to FunctionType
68 type_function,64 type_function,
69 // /// Pointer type in the CrossWorkgroup storage class65 /// Pointer type in the CrossWorkgroup storage class
70 // /// data is child type66 /// data is child type
71 // type_ptr_generic,67 type_ptr_generic,
72 // /// Pointer type in the CrossWorkgroup storage class68 /// Pointer type in the CrossWorkgroup storage class
73 // /// data is child type69 /// data is child type
74 // type_ptr_crosswgp,70 type_ptr_crosswgp,
75 // /// Pointer type in the Function storage class71 /// Pointer type in the Function storage class
76 // /// data is child type72 /// data is child type
77 // type_ptr_function,73 type_ptr_function,
78 /// Simple pointer type that does not have any decorations.74 /// Simple pointer type that does not have any decorations.
79 /// data is payload to SimplePointerType75 /// data is payload to SimplePointerType
80 type_ptr_simple,76 type_ptr_simple,
81 /// A forward declaration for a pointer.
82 /// data is ForwardPointerType
83 type_fwd_ptr,
84 /// Simple structure type that does not have any decorations.77 /// Simple structure type that does not have any decorations.
85 /// data is payload to SimpleStructType78 /// data is payload to SimpleStructType
86 type_struct_simple,79 type_struct_simple,
...@@ -149,12 +142,6 @@ const Tag = enum {...@@ -149,12 +142,6 @@ const Tag = enum {
149 const SimplePointerType = struct {142 const SimplePointerType = struct {
150 storage_class: StorageClass,143 storage_class: StorageClass,
151 child_type: Ref,144 child_type: Ref,
152 fwd: Ref,
153 };
154
155 const ForwardPointerType = struct {
156 storage_class: StorageClass,
157 zig_child_type: InternPool.Index,
158 };145 };
159146
160 /// Trailing:147 /// Trailing:
...@@ -176,14 +163,14 @@ const Tag = enum {...@@ -176,14 +163,14 @@ const Tag = enum {
176 fn encode(value: f64) Float64 {163 fn encode(value: f64) Float64 {
177 const bits = @as(u64, @bitCast(value));164 const bits = @as(u64, @bitCast(value));
178 return .{165 return .{
179 .low = @truncate(bits),166 .low = @as(u32, @truncate(bits)),
180 .high = @truncate(bits >> 32),167 .high = @as(u32, @truncate(bits >> 32)),
181 };168 };
182 }169 }
183170
184 fn decode(self: Float64) f64 {171 fn decode(self: Float64) f64 {
185 const bits = @as(u64, self.low) | (@as(u64, self.high) << 32);172 const bits = @as(u64, self.low) | (@as(u64, self.high) << 32);
186 return @bitCast(bits);173 return @as(f64, @bitCast(bits));
187 }174 }
188 };175 };
189176
...@@ -205,8 +192,8 @@ const Tag = enum {...@@ -205,8 +192,8 @@ const Tag = enum {
205 fn encode(ty: Ref, value: u64) Int64 {192 fn encode(ty: Ref, value: u64) Int64 {
206 return .{193 return .{
207 .ty = ty,194 .ty = ty,
208 .low = @truncate(value),195 .low = @as(u32, @truncate(value)),
209 .high = @truncate(value >> 32),196 .high = @as(u32, @truncate(value >> 32)),
210 };197 };
211 }198 }
212199
...@@ -223,8 +210,8 @@ const Tag = enum {...@@ -223,8 +210,8 @@ const Tag = enum {
223 fn encode(ty: Ref, value: i64) Int64 {210 fn encode(ty: Ref, value: i64) Int64 {
224 return .{211 return .{
225 .ty = ty,212 .ty = ty,
226 .low = @truncate(@as(u64, @bitCast(value))),213 .low = @as(u32, @truncate(@as(u64, @bitCast(value)))),
227 .high = @truncate(@as(u64, @bitCast(value)) >> 32),214 .high = @as(u32, @truncate(@as(u64, @bitCast(value)) >> 32)),
228 };215 };
229 }216 }
230217
...@@ -250,7 +237,6 @@ pub const Key = union(enum) {...@@ -250,7 +237,6 @@ pub const Key = union(enum) {
250 array_type: ArrayType,237 array_type: ArrayType,
251 function_type: FunctionType,238 function_type: FunctionType,
252 ptr_type: PointerType,239 ptr_type: PointerType,
253 fwd_ptr_type: ForwardPointerType,
254 struct_type: StructType,240 struct_type: StructType,
255 opaque_type: OpaqueType,241 opaque_type: OpaqueType,
256242
...@@ -287,18 +273,12 @@ pub const Key = union(enum) {...@@ -287,18 +273,12 @@ pub const Key = union(enum) {
287 pub const PointerType = struct {273 pub const PointerType = struct {
288 storage_class: StorageClass,274 storage_class: StorageClass,
289 child_type: Ref,275 child_type: Ref,
290 fwd: Ref,
291 // TODO: Decorations:276 // TODO: Decorations:
292 // - Alignment277 // - Alignment
293 // - ArrayStride,278 // - ArrayStride,
294 // - MaxByteOffset,279 // - MaxByteOffset,
295 };280 };
296281
297 pub const ForwardPointerType = struct {
298 zig_child_type: InternPool.Index,
299 storage_class: StorageClass,
300 };
301
302 pub const StructType = struct {282 pub const StructType = struct {
303 // TODO: Decorations.283 // TODO: Decorations.
304 /// The name of the structure. Can be `.none`.284 /// The name of the structure. Can be `.none`.
...@@ -333,21 +313,21 @@ pub const Key = union(enum) {...@@ -333,21 +313,21 @@ pub const Key = union(enum) {
333 /// Turns this value into the corresponding 32-bit literal, 2s complement signed.313 /// Turns this value into the corresponding 32-bit literal, 2s complement signed.
334 fn toBits32(self: Int) u32 {314 fn toBits32(self: Int) u32 {
335 return switch (self.value) {315 return switch (self.value) {
336 .uint64 => |val| @intCast(val),316 .uint64 => |val| @as(u32, @intCast(val)),
337 .int64 => |val| if (val < 0) @bitCast(@as(i32, @intCast(val))) else @intCast(val),317 .int64 => |val| if (val < 0) @as(u32, @bitCast(@as(i32, @intCast(val)))) else @as(u32, @intCast(val)),
338 };318 };
339 }319 }
340320
341 fn toBits64(self: Int) u64 {321 fn toBits64(self: Int) u64 {
342 return switch (self.value) {322 return switch (self.value) {
343 .uint64 => |val| val,323 .uint64 => |val| val,
344 .int64 => |val| @bitCast(val),324 .int64 => |val| @as(u64, @bitCast(val)),
345 };325 };
346 }326 }
347327
348 fn to(self: Int, comptime T: type) T {328 fn to(self: Int, comptime T: type) T {
349 return switch (self.value) {329 return switch (self.value) {
350 inline else => |val| @intCast(val),330 inline else => |val| @as(T, @intCast(val)),
351 };331 };
352 }332 }
353 };333 };
...@@ -407,7 +387,7 @@ pub const Key = union(enum) {...@@ -407,7 +387,7 @@ pub const Key = union(enum) {
407 },387 },
408 inline else => |key| std.hash.autoHash(&hasher, key),388 inline else => |key| std.hash.autoHash(&hasher, key),
409 }389 }
410 return @truncate(hasher.final());390 return @as(u32, @truncate(hasher.final()));
411 }391 }
412392
413 fn eql(a: Key, b: Key) bool {393 fn eql(a: Key, b: Key) bool {
...@@ -439,7 +419,7 @@ pub const Key = union(enum) {...@@ -439,7 +419,7 @@ pub const Key = union(enum) {
439419
440 pub fn eql(ctx: @This(), a: Key, b_void: void, b_index: usize) bool {420 pub fn eql(ctx: @This(), a: Key, b_void: void, b_index: usize) bool {
441 _ = b_void;421 _ = b_void;
442 return ctx.self.lookup(@enumFromInt(b_index)).eql(a);422 return ctx.self.lookup(@as(Ref, @enumFromInt(b_index))).eql(a);
443 }423 }
444424
445 pub fn hash(ctx: @This(), a: Key) u32 {425 pub fn hash(ctx: @This(), a: Key) u32 {
...@@ -470,7 +450,6 @@ pub fn deinit(self: *Self, spv: *const Module) void {...@@ -470,7 +450,6 @@ pub fn deinit(self: *Self, spv: *const Module) void {
470 self.extra.deinit(spv.gpa);450 self.extra.deinit(spv.gpa);
471 self.string_bytes.deinit(spv.gpa);451 self.string_bytes.deinit(spv.gpa);
472 self.strings.deinit(spv.gpa);452 self.strings.deinit(spv.gpa);
473 self.recursive_ptrs.deinit(spv.gpa);
474}453}
475454
476/// Actually materialize the database into spir-v instructions.455/// Actually materialize the database into spir-v instructions.
...@@ -481,7 +460,7 @@ pub fn materialize(self: *const Self, spv: *Module) !Section {...@@ -481,7 +460,7 @@ pub fn materialize(self: *const Self, spv: *Module) !Section {
481 var section = Section{};460 var section = Section{};
482 errdefer section.deinit(spv.gpa);461 errdefer section.deinit(spv.gpa);
483 for (self.items.items(.result_id), 0..) |result_id, index| {462 for (self.items.items(.result_id), 0..) |result_id, index| {
484 try self.emit(spv, result_id, @enumFromInt(index), &section);463 try self.emit(spv, result_id, @as(Ref, @enumFromInt(index)), &section);
485 }464 }
486 return section;465 return section;
487}466}
...@@ -559,15 +538,6 @@ fn emit(...@@ -559,15 +538,6 @@ fn emit(
559 });538 });
560 // TODO: Decorations?539 // TODO: Decorations?
561 },540 },
562 .fwd_ptr_type => |fwd| {
563 // Only emit the OpTypeForwardPointer if its actually required.
564 if (self.recursive_ptrs.contains(ref)) {
565 try section.emit(spv.gpa, .OpTypeForwardPointer, .{
566 .pointer_type = result_id,
567 .storage_class = fwd.storage_class,
568 });
569 }
570 },
571 .struct_type => |struct_type| {541 .struct_type => |struct_type| {
572 try section.emitRaw(spv.gpa, .OpTypeStruct, 1 + struct_type.member_types.len);542 try section.emitRaw(spv.gpa, .OpTypeStruct, 1 + struct_type.member_types.len);
573 section.writeOperand(IdResult, result_id);543 section.writeOperand(IdResult, result_id);
...@@ -579,7 +549,7 @@ fn emit(...@@ -579,7 +549,7 @@ fn emit(
579 }549 }
580 for (struct_type.memberNames(), 0..) |member_name, i| {550 for (struct_type.memberNames(), 0..) |member_name, i| {
581 if (self.getString(member_name)) |name| {551 if (self.getString(member_name)) |name| {
582 try spv.memberDebugName(result_id, @intCast(i), name);552 try spv.memberDebugName(result_id, @as(u32, @intCast(i)), name);
583 }553 }
584 }554 }
585 // TODO: Decorations?555 // TODO: Decorations?
...@@ -655,12 +625,13 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {...@@ -655,12 +625,13 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {
655 const adapter: Key.Adapter = .{ .self = self };625 const adapter: Key.Adapter = .{ .self = self };
656 const entry = try self.map.getOrPutAdapted(spv.gpa, key, adapter);626 const entry = try self.map.getOrPutAdapted(spv.gpa, key, adapter);
657 if (entry.found_existing) {627 if (entry.found_existing) {
658 return @enumFromInt(entry.index);628 return @as(Ref, @enumFromInt(entry.index));
659 }629 }
630 const result_id = spv.allocId();
660 const item: Item = switch (key) {631 const item: Item = switch (key) {
661 inline .void_type, .bool_type => .{632 inline .void_type, .bool_type => .{
662 .tag = .type_simple,633 .tag = .type_simple,
663 .result_id = spv.allocId(),634 .result_id = result_id,
664 .data = @intFromEnum(key.toSimpleType()),635 .data = @intFromEnum(key.toSimpleType()),
665 },636 },
666 .int_type => |int| blk: {637 .int_type => |int| blk: {
...@@ -670,104 +641,87 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {...@@ -670,104 +641,87 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {
670 };641 };
671 break :blk .{642 break :blk .{
672 .tag = t,643 .tag = t,
673 .result_id = spv.allocId(),644 .result_id = result_id,
674 .data = int.bits,645 .data = int.bits,
675 };646 };
676 },647 },
677 .float_type => |float| .{648 .float_type => |float| .{
678 .tag = .type_float,649 .tag = .type_float,
679 .result_id = spv.allocId(),650 .result_id = result_id,
680 .data = float.bits,651 .data = float.bits,
681 },652 },
682 .vector_type => |vector| .{653 .vector_type => |vector| .{
683 .tag = .type_vector,654 .tag = .type_vector,
684 .result_id = spv.allocId(),655 .result_id = result_id,
685 .data = try self.addExtra(spv, vector),656 .data = try self.addExtra(spv, vector),
686 },657 },
687 .array_type => |array| .{658 .array_type => |array| .{
688 .tag = .type_array,659 .tag = .type_array,
689 .result_id = spv.allocId(),660 .result_id = result_id,
690 .data = try self.addExtra(spv, array),661 .data = try self.addExtra(spv, array),
691 },662 },
692 .function_type => |function| blk: {663 .function_type => |function| blk: {
693 const extra = try self.addExtra(spv, Tag.FunctionType{664 const extra = try self.addExtra(spv, Tag.FunctionType{
694 .param_len = @intCast(function.parameters.len),665 .param_len = @as(u32, @intCast(function.parameters.len)),
695 .return_type = function.return_type,666 .return_type = function.return_type,
696 });667 });
697 try self.extra.appendSlice(spv.gpa, @ptrCast(function.parameters));668 try self.extra.appendSlice(spv.gpa, @as([]const u32, @ptrCast(function.parameters)));
698 break :blk .{669 break :blk .{
699 .tag = .type_function,670 .tag = .type_function,
700 .result_id = spv.allocId(),671 .result_id = result_id,
701 .data = extra,672 .data = extra,
702 };673 };
703 },674 },
704 // .ptr_type => |ptr| switch (ptr.storage_class) {675 .ptr_type => |ptr| switch (ptr.storage_class) {
705 // .Generic => Item{676 .Generic => Item{
706 // .tag = .type_ptr_generic,677 .tag = .type_ptr_generic,
707 // .result_id = spv.allocId(),678 .result_id = result_id,
708 // .data = @intFromEnum(ptr.child_type),679 .data = @intFromEnum(ptr.child_type),
709 // },680 },
710 // .CrossWorkgroup => Item{681 .CrossWorkgroup => Item{
711 // .tag = .type_ptr_crosswgp,682 .tag = .type_ptr_crosswgp,
712 // .result_id = spv.allocId(),683 .result_id = result_id,
713 // .data = @intFromEnum(ptr.child_type),684 .data = @intFromEnum(ptr.child_type),
714 // },685 },
715 // .Function => Item{686 .Function => Item{
716 // .tag = .type_ptr_function,687 .tag = .type_ptr_function,
717 // .result_id = spv.allocId(),688 .result_id = result_id,
718 // .data = @intFromEnum(ptr.child_type),689 .data = @intFromEnum(ptr.child_type),
719 // },690 },
720 // else => |storage_class| Item{691 else => |storage_class| Item{
721 // .tag = .type_ptr_simple,692 .tag = .type_ptr_simple,
722 // .result_id = spv.allocId(),693 .result_id = result_id,
723 // .data = try self.addExtra(spv, Tag.SimplePointerType{694 .data = try self.addExtra(spv, Tag.SimplePointerType{
724 // .storage_class = storage_class,695 .storage_class = storage_class,
725 // .child_type = ptr.child_type,696 .child_type = ptr.child_type,
726 // }),697 }),
727 // },698 },
728 // },
729 .ptr_type => |ptr| Item{
730 .tag = .type_ptr_simple,
731 .result_id = self.resultId(ptr.fwd),
732 .data = try self.addExtra(spv, Tag.SimplePointerType{
733 .storage_class = ptr.storage_class,
734 .child_type = ptr.child_type,
735 .fwd = ptr.fwd,
736 }),
737 },
738 .fwd_ptr_type => |fwd| Item{
739 .tag = .type_fwd_ptr,
740 .result_id = spv.allocId(),
741 .data = try self.addExtra(spv, Tag.ForwardPointerType{
742 .zig_child_type = fwd.zig_child_type,
743 .storage_class = fwd.storage_class,
744 }),
745 },699 },
746 .struct_type => |struct_type| blk: {700 .struct_type => |struct_type| blk: {
747 const extra = try self.addExtra(spv, Tag.SimpleStructType{701 const extra = try self.addExtra(spv, Tag.SimpleStructType{
748 .name = struct_type.name,702 .name = struct_type.name,
749 .members_len = @intCast(struct_type.member_types.len),703 .members_len = @as(u32, @intCast(struct_type.member_types.len)),
750 });704 });
751 try self.extra.appendSlice(spv.gpa, @ptrCast(struct_type.member_types));705 try self.extra.appendSlice(spv.gpa, @as([]const u32, @ptrCast(struct_type.member_types)));
752706
753 if (struct_type.member_names) |member_names| {707 if (struct_type.member_names) |member_names| {
754 try self.extra.appendSlice(spv.gpa, @ptrCast(member_names));708 try self.extra.appendSlice(spv.gpa, @as([]const u32, @ptrCast(member_names)));
755 break :blk Item{709 break :blk Item{
756 .tag = .type_struct_simple_with_member_names,710 .tag = .type_struct_simple_with_member_names,
757 .result_id = spv.allocId(),711 .result_id = result_id,
758 .data = extra,712 .data = extra,
759 };713 };
760 } else {714 } else {
761 break :blk Item{715 break :blk Item{
762 .tag = .type_struct_simple,716 .tag = .type_struct_simple,
763 .result_id = spv.allocId(),717 .result_id = result_id,
764 .data = extra,718 .data = extra,
765 };719 };
766 }720 }
767 },721 },
768 .opaque_type => |opaque_type| Item{722 .opaque_type => |opaque_type| Item{
769 .tag = .type_opaque,723 .tag = .type_opaque,
770 .result_id = spv.allocId(),724 .result_id = result_id,
771 .data = @intFromEnum(opaque_type.name),725 .data = @intFromEnum(opaque_type.name),
772 },726 },
773 .int => |int| blk: {727 .int => |int| blk: {
...@@ -775,13 +729,13 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {...@@ -775,13 +729,13 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {
775 if (int_type.signedness == .unsigned and int_type.bits == 8) {729 if (int_type.signedness == .unsigned and int_type.bits == 8) {
776 break :blk .{730 break :blk .{
777 .tag = .uint8,731 .tag = .uint8,
778 .result_id = spv.allocId(),732 .result_id = result_id,
779 .data = int.to(u8),733 .data = int.to(u8),
780 };734 };
781 } else if (int_type.signedness == .unsigned and int_type.bits == 32) {735 } else if (int_type.signedness == .unsigned and int_type.bits == 32) {
782 break :blk .{736 break :blk .{
783 .tag = .uint32,737 .tag = .uint32,
784 .result_id = spv.allocId(),738 .result_id = result_id,
785 .data = int.to(u32),739 .data = int.to(u32),
786 };740 };
787 }741 }
...@@ -791,32 +745,32 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {...@@ -791,32 +745,32 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {
791 if (val >= 0 and val <= std.math.maxInt(u32)) {745 if (val >= 0 and val <= std.math.maxInt(u32)) {
792 break :blk .{746 break :blk .{
793 .tag = .uint_small,747 .tag = .uint_small,
794 .result_id = spv.allocId(),748 .result_id = result_id,
795 .data = try self.addExtra(spv, Tag.UInt32{749 .data = try self.addExtra(spv, Tag.UInt32{
796 .ty = int.ty,750 .ty = int.ty,
797 .value = @intCast(val),751 .value = @as(u32, @intCast(val)),
798 }),752 }),
799 };753 };
800 } else if (val >= std.math.minInt(i32) and val <= std.math.maxInt(i32)) {754 } else if (val >= std.math.minInt(i32) and val <= std.math.maxInt(i32)) {
801 break :blk .{755 break :blk .{
802 .tag = .int_small,756 .tag = .int_small,
803 .result_id = spv.allocId(),757 .result_id = result_id,
804 .data = try self.addExtra(spv, Tag.Int32{758 .data = try self.addExtra(spv, Tag.Int32{
805 .ty = int.ty,759 .ty = int.ty,
806 .value = @intCast(val),760 .value = @as(i32, @intCast(val)),
807 }),761 }),
808 };762 };
809 } else if (val < 0) {763 } else if (val < 0) {
810 break :blk .{764 break :blk .{
811 .tag = .int_large,765 .tag = .int_large,
812 .result_id = spv.allocId(),766 .result_id = result_id,
813 .data = try self.addExtra(spv, Tag.Int64.encode(int.ty, @intCast(val))),767 .data = try self.addExtra(spv, Tag.Int64.encode(int.ty, @as(i64, @intCast(val)))),
814 };768 };
815 } else {769 } else {
816 break :blk .{770 break :blk .{
817 .tag = .uint_large,771 .tag = .uint_large,
818 .result_id = spv.allocId(),772 .result_id = result_id,
819 .data = try self.addExtra(spv, Tag.UInt64.encode(int.ty, @intCast(val))),773 .data = try self.addExtra(spv, Tag.UInt64.encode(int.ty, @as(u64, @intCast(val)))),
820 };774 };
821 }775 }
822 },776 },
...@@ -825,29 +779,29 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {...@@ -825,29 +779,29 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {
825 .float => |float| switch (self.lookup(float.ty).float_type.bits) {779 .float => |float| switch (self.lookup(float.ty).float_type.bits) {
826 16 => .{780 16 => .{
827 .tag = .float16,781 .tag = .float16,
828 .result_id = spv.allocId(),782 .result_id = result_id,
829 .data = @as(u16, @bitCast(float.value.float16)),783 .data = @as(u16, @bitCast(float.value.float16)),
830 },784 },
831 32 => .{785 32 => .{
832 .tag = .float32,786 .tag = .float32,
833 .result_id = spv.allocId(),787 .result_id = result_id,
834 .data = @as(u32, @bitCast(float.value.float32)),788 .data = @as(u32, @bitCast(float.value.float32)),
835 },789 },
836 64 => .{790 64 => .{
837 .tag = .float64,791 .tag = .float64,
838 .result_id = spv.allocId(),792 .result_id = result_id,
839 .data = try self.addExtra(spv, Tag.Float64.encode(float.value.float64)),793 .data = try self.addExtra(spv, Tag.Float64.encode(float.value.float64)),
840 },794 },
841 else => unreachable,795 else => unreachable,
842 },796 },
843 .undef => |undef| .{797 .undef => |undef| .{
844 .tag = .undef,798 .tag = .undef,
845 .result_id = spv.allocId(),799 .result_id = result_id,
846 .data = @intFromEnum(undef.ty),800 .data = @intFromEnum(undef.ty),
847 },801 },
848 .null => |null_info| .{802 .null => |null_info| .{
849 .tag = .null,803 .tag = .null,
850 .result_id = spv.allocId(),804 .result_id = result_id,
851 .data = @intFromEnum(null_info.ty),805 .data = @intFromEnum(null_info.ty),
852 },806 },
853 .bool => |bool_info| .{807 .bool => |bool_info| .{
...@@ -855,13 +809,13 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {...@@ -855,13 +809,13 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {
855 true => Tag.bool_true,809 true => Tag.bool_true,
856 false => Tag.bool_false,810 false => Tag.bool_false,
857 },811 },
858 .result_id = spv.allocId(),812 .result_id = result_id,
859 .data = @intFromEnum(bool_info.ty),813 .data = @intFromEnum(bool_info.ty),
860 },814 },
861 };815 };
862 try self.items.append(spv.gpa, item);816 try self.items.append(spv.gpa, item);
863817
864 return @enumFromInt(entry.index);818 return @as(Ref, @enumFromInt(entry.index));
865}819}
866820
867/// Turn a Ref back into a Key.821/// Turn a Ref back into a Key.
...@@ -876,14 +830,14 @@ pub fn lookup(self: *const Self, ref: Ref) Key {...@@ -876,14 +830,14 @@ pub fn lookup(self: *const Self, ref: Ref) Key {
876 },830 },
877 .type_int_signed => .{ .int_type = .{831 .type_int_signed => .{ .int_type = .{
878 .signedness = .signed,832 .signedness = .signed,
879 .bits = @intCast(data),833 .bits = @as(u16, @intCast(data)),
880 } },834 } },
881 .type_int_unsigned => .{ .int_type = .{835 .type_int_unsigned => .{ .int_type = .{
882 .signedness = .unsigned,836 .signedness = .unsigned,
883 .bits = @intCast(data),837 .bits = @as(u16, @intCast(data)),
884 } },838 } },
885 .type_float => .{ .float_type = .{839 .type_float => .{ .float_type = .{
886 .bits = @intCast(data),840 .bits = @as(u16, @intCast(data)),
887 } },841 } },
888 .type_vector => .{ .vector_type = self.extraData(Tag.VectorType, data) },842 .type_vector => .{ .vector_type = self.extraData(Tag.VectorType, data) },
889 .type_array => .{ .array_type = self.extraData(Tag.ArrayType, data) },843 .type_array => .{ .array_type = self.extraData(Tag.ArrayType, data) },
...@@ -892,50 +846,40 @@ pub fn lookup(self: *const Self, ref: Ref) Key {...@@ -892,50 +846,40 @@ pub fn lookup(self: *const Self, ref: Ref) Key {
892 return .{846 return .{
893 .function_type = .{847 .function_type = .{
894 .return_type = payload.data.return_type,848 .return_type = payload.data.return_type,
895 .parameters = @ptrCast(self.extra.items[payload.trail..][0..payload.data.param_len]),849 .parameters = @as([]const Ref, @ptrCast(self.extra.items[payload.trail..][0..payload.data.param_len])),
896 },850 },
897 };851 };
898 },852 },
899 // .type_ptr_generic => .{853 .type_ptr_generic => .{
900 // .ptr_type = .{854 .ptr_type = .{
901 // .storage_class = .Generic,855 .storage_class = .Generic,
902 // .child_type = @enumFromInt(data),856 .child_type = @as(Ref, @enumFromInt(data)),
903 // },857 },
904 // },858 },
905 // .type_ptr_crosswgp => .{859 .type_ptr_crosswgp => .{
906 // .ptr_type = .{860 .ptr_type = .{
907 // .storage_class = .CrossWorkgroup,861 .storage_class = .CrossWorkgroup,
908 // .child_type = @enumFromInt(data),862 .child_type = @as(Ref, @enumFromInt(data)),
909 // },863 },
910 // },864 },
911 // .type_ptr_function => .{865 .type_ptr_function => .{
912 // .ptr_type = .{866 .ptr_type = .{
913 // .storage_class = .Function,867 .storage_class = .Function,
914 // .child_type = @enumFromInt(data),868 .child_type = @as(Ref, @enumFromInt(data)),
915 // },869 },
916 // },870 },
917 .type_ptr_simple => {871 .type_ptr_simple => {
918 const payload = self.extraData(Tag.SimplePointerType, data);872 const payload = self.extraData(Tag.SimplePointerType, data);
919 return .{873 return .{
920 .ptr_type = .{874 .ptr_type = .{
921 .storage_class = payload.storage_class,875 .storage_class = payload.storage_class,
922 .child_type = payload.child_type,876 .child_type = payload.child_type,
923 .fwd = payload.fwd,
924 },
925 };
926 },
927 .type_fwd_ptr => {
928 const payload = self.extraData(Tag.ForwardPointerType, data);
929 return .{
930 .fwd_ptr_type = .{
931 .zig_child_type = payload.zig_child_type,
932 .storage_class = payload.storage_class,
933 },877 },
934 };878 };
935 },879 },
936 .type_struct_simple => {880 .type_struct_simple => {
937 const payload = self.extraDataTrail(Tag.SimpleStructType, data);881 const payload = self.extraDataTrail(Tag.SimpleStructType, data);
938 const member_types: []const Ref = @ptrCast(self.extra.items[payload.trail..][0..payload.data.members_len]);882 const member_types = @as([]const Ref, @ptrCast(self.extra.items[payload.trail..][0..payload.data.members_len]));
939 return .{883 return .{
940 .struct_type = .{884 .struct_type = .{
941 .name = payload.data.name,885 .name = payload.data.name,
...@@ -947,8 +891,8 @@ pub fn lookup(self: *const Self, ref: Ref) Key {...@@ -947,8 +891,8 @@ pub fn lookup(self: *const Self, ref: Ref) Key {
947 .type_struct_simple_with_member_names => {891 .type_struct_simple_with_member_names => {
948 const payload = self.extraDataTrail(Tag.SimpleStructType, data);892 const payload = self.extraDataTrail(Tag.SimpleStructType, data);
949 const trailing = self.extra.items[payload.trail..];893 const trailing = self.extra.items[payload.trail..];
950 const member_types: []const Ref = @ptrCast(trailing[0..payload.data.members_len]);894 const member_types = @as([]const Ref, @ptrCast(trailing[0..payload.data.members_len]));
951 const member_names: []const String = @ptrCast(trailing[payload.data.members_len..][0..payload.data.members_len]);895 const member_names = @as([]const String, @ptrCast(trailing[payload.data.members_len..][0..payload.data.members_len]));
952 return .{896 return .{
953 .struct_type = .{897 .struct_type = .{
954 .name = payload.data.name,898 .name = payload.data.name,
...@@ -959,16 +903,16 @@ pub fn lookup(self: *const Self, ref: Ref) Key {...@@ -959,16 +903,16 @@ pub fn lookup(self: *const Self, ref: Ref) Key {
959 },903 },
960 .type_opaque => .{904 .type_opaque => .{
961 .opaque_type = .{905 .opaque_type = .{
962 .name = @enumFromInt(data),906 .name = @as(String, @enumFromInt(data)),
963 },907 },
964 },908 },
965 .float16 => .{ .float = .{909 .float16 => .{ .float = .{
966 .ty = self.get(.{ .float_type = .{ .bits = 16 } }),910 .ty = self.get(.{ .float_type = .{ .bits = 16 } }),
967 .value = .{ .float16 = @bitCast(@as(u16, @intCast(data))) },911 .value = .{ .float16 = @as(f16, @bitCast(@as(u16, @intCast(data)))) },
968 } },912 } },
969 .float32 => .{ .float = .{913 .float32 => .{ .float = .{
970 .ty = self.get(.{ .float_type = .{ .bits = 32 } }),914 .ty = self.get(.{ .float_type = .{ .bits = 32 } }),
971 .value = .{ .float32 = @bitCast(data) },915 .value = .{ .float32 = @as(f32, @bitCast(data)) },
972 } },916 } },
973 .float64 => .{ .float = .{917 .float64 => .{ .float = .{
974 .ty = self.get(.{ .float_type = .{ .bits = 64 } }),918 .ty = self.get(.{ .float_type = .{ .bits = 64 } }),
...@@ -1011,17 +955,17 @@ pub fn lookup(self: *const Self, ref: Ref) Key {...@@ -1011,17 +955,17 @@ pub fn lookup(self: *const Self, ref: Ref) Key {
1011 } };955 } };
1012 },956 },
1013 .undef => .{ .undef = .{957 .undef => .{ .undef = .{
1014 .ty = @enumFromInt(data),958 .ty = @as(Ref, @enumFromInt(data)),
1015 } },959 } },
1016 .null => .{ .null = .{960 .null => .{ .null = .{
1017 .ty = @enumFromInt(data),961 .ty = @as(Ref, @enumFromInt(data)),
1018 } },962 } },
1019 .bool_true => .{ .bool = .{963 .bool_true => .{ .bool = .{
1020 .ty = @enumFromInt(data),964 .ty = @as(Ref, @enumFromInt(data)),
1021 .value = true,965 .value = true,
1022 } },966 } },
1023 .bool_false => .{ .bool = .{967 .bool_false => .{ .bool = .{
1024 .ty = @enumFromInt(data),968 .ty = @as(Ref, @enumFromInt(data)),
1025 .value = false,969 .value = false,
1026 } },970 } },
1027 };971 };
...@@ -1037,7 +981,7 @@ pub fn resultId(self: Self, ref: Ref) IdResult {...@@ -1037,7 +981,7 @@ pub fn resultId(self: Self, ref: Ref) IdResult {
1037fn get(self: *const Self, key: Key) Ref {981fn get(self: *const Self, key: Key) Ref {
1038 const adapter: Key.Adapter = .{ .self = self };982 const adapter: Key.Adapter = .{ .self = self };
1039 const index = self.map.getIndexAdapted(key, adapter).?;983 const index = self.map.getIndexAdapted(key, adapter).?;
1040 return @enumFromInt(index);984 return @as(Ref, @enumFromInt(index));
1041}985}
1042986
1043fn addExtra(self: *Self, spv: *Module, extra: anytype) !u32 {987fn addExtra(self: *Self, spv: *Module, extra: anytype) !u32 {
...@@ -1047,16 +991,15 @@ fn addExtra(self: *Self, spv: *Module, extra: anytype) !u32 {...@@ -1047,16 +991,15 @@ fn addExtra(self: *Self, spv: *Module, extra: anytype) !u32 {
1047}991}
1048992
1049fn addExtraAssumeCapacity(self: *Self, extra: anytype) !u32 {993fn addExtraAssumeCapacity(self: *Self, extra: anytype) !u32 {
1050 const payload_offset: u32 = @intCast(self.extra.items.len);994 const payload_offset = @as(u32, @intCast(self.extra.items.len));
1051 inline for (@typeInfo(@TypeOf(extra)).Struct.fields) |field| {995 inline for (@typeInfo(@TypeOf(extra)).Struct.fields) |field| {
1052 const field_val = @field(extra, field.name);996 const field_val = @field(extra, field.name);
1053 const word: u32 = switch (field.type) {997 const word = switch (field.type) {
1054 u32 => field_val,998 u32 => field_val,
1055 i32 => @bitCast(field_val),999 i32 => @as(u32, @bitCast(field_val)),
1056 Ref => @intFromEnum(field_val),1000 Ref => @intFromEnum(field_val),
1057 StorageClass => @intFromEnum(field_val),1001 StorageClass => @intFromEnum(field_val),
1058 String => @intFromEnum(field_val),1002 String => @intFromEnum(field_val),
1059 InternPool.Index => @intFromEnum(field_val),
1060 else => @compileError("Invalid type: " ++ @typeName(field.type)),1003 else => @compileError("Invalid type: " ++ @typeName(field.type)),
1061 };1004 };
1062 self.extra.appendAssumeCapacity(word);1005 self.extra.appendAssumeCapacity(word);
...@@ -1075,11 +1018,10 @@ fn extraDataTrail(self: Self, comptime T: type, offset: u32) struct { data: T, t...@@ -1075,11 +1018,10 @@ fn extraDataTrail(self: Self, comptime T: type, offset: u32) struct { data: T, t
1075 const word = self.extra.items[offset + i];1018 const word = self.extra.items[offset + i];
1076 @field(result, field.name) = switch (field.type) {1019 @field(result, field.name) = switch (field.type) {
1077 u32 => word,1020 u32 => word,
1078 i32 => @bitCast(word),1021 i32 => @as(i32, @bitCast(word)),
1079 Ref => @enumFromInt(word),1022 Ref => @as(Ref, @enumFromInt(word)),
1080 StorageClass => @enumFromInt(word),1023 StorageClass => @as(StorageClass, @enumFromInt(word)),
1081 String => @enumFromInt(word),1024 String => @as(String, @enumFromInt(word)),
1082 InternPool.Index => @enumFromInt(word),
1083 else => @compileError("Invalid type: " ++ @typeName(field.type)),1025 else => @compileError("Invalid type: " ++ @typeName(field.type)),
1084 };1026 };
1085 }1027 }
...@@ -1107,7 +1049,7 @@ pub const String = enum(u32) {...@@ -1107,7 +1049,7 @@ pub const String = enum(u32) {
1107 _ = ctx;1049 _ = ctx;
1108 var hasher = std.hash.Wyhash.init(0);1050 var hasher = std.hash.Wyhash.init(0);
1109 hasher.update(a);1051 hasher.update(a);
1110 return @truncate(hasher.final());1052 return @as(u32, @truncate(hasher.final()));
1111 }1053 }
1112 };1054 };
1113};1055};
...@@ -1122,10 +1064,10 @@ pub fn addString(self: *Self, spv: *Module, str: []const u8) !String {...@@ -1122,10 +1064,10 @@ pub fn addString(self: *Self, spv: *Module, str: []const u8) !String {
1122 try self.string_bytes.ensureUnusedCapacity(spv.gpa, 1 + str.len);1064 try self.string_bytes.ensureUnusedCapacity(spv.gpa, 1 + str.len);
1123 self.string_bytes.appendSliceAssumeCapacity(str);1065 self.string_bytes.appendSliceAssumeCapacity(str);
1124 self.string_bytes.appendAssumeCapacity(0);1066 self.string_bytes.appendAssumeCapacity(0);
1125 entry.value_ptr.* = @intCast(offset);1067 entry.value_ptr.* = @as(u32, @intCast(offset));
1126 }1068 }
11271069
1128 return @enumFromInt(entry.index);1070 return @as(String, @enumFromInt(entry.index));
1129}1071}
11301072
1131pub fn getString(self: *const Self, ref: String) ?[]const u8 {1073pub fn getString(self: *const Self, ref: String) ?[]const u8 {
src/codegen/spirv/Module.zig+11
...@@ -507,6 +507,17 @@ pub fn arrayType(self: *Module, len: u32, elem_ty_ref: CacheRef) !CacheRef {...@@ -507,6 +507,17 @@ pub fn arrayType(self: *Module, len: u32, elem_ty_ref: CacheRef) !CacheRef {
507 } });507 } });
508}508}
509509
510pub fn ptrType(
511 self: *Module,
512 child: CacheRef,
513 storage_class: spec.StorageClass,
514) !CacheRef {
515 return try self.resolve(.{ .ptr_type = .{
516 .storage_class = storage_class,
517 .child_type = child,
518 } });
519}
520
510pub fn constInt(self: *Module, ty_ref: CacheRef, value: anytype) !IdRef {521pub fn constInt(self: *Module, ty_ref: CacheRef, value: anytype) !IdRef {
511 const ty = self.cache.lookup(ty_ref).int_type;522 const ty = self.cache.lookup(ty_ref).int_type;
512 const Value = Cache.Key.Int.Value;523 const Value = Cache.Key.Int.Value;
test/behavior/bugs/12000.zig+1
...@@ -9,6 +9,7 @@ test {...@@ -9,6 +9,7 @@ test {
9 if (builtin.zig_backend == .stage2_x86) return error.SkipZigTest; // TODO9 if (builtin.zig_backend == .stage2_x86) return error.SkipZigTest; // TODO
10 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO10 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
11 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO11 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
12 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1213
13 var t: T = .{ .next = null };14 var t: T = .{ .next = null };
14 try std.testing.expect(t.next == null);15 try std.testing.expect(t.next == null);
test/behavior/bugs/1735.zig+1
...@@ -44,6 +44,7 @@ const a = struct {...@@ -44,6 +44,7 @@ const a = struct {
44test "initialization" {44test "initialization" {
45 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;45 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
46 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO46 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
47 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
4748
48 var t = a.init();49 var t = a.init();
49 try std.testing.expect(t.foo.len == 0);50 try std.testing.expect(t.foo.len == 0);
test/behavior/bugs/1914.zig+4
...@@ -12,6 +12,8 @@ const b_list: []B = &[_]B{};...@@ -12,6 +12,8 @@ const b_list: []B = &[_]B{};
12const a = A{ .b_list_pointer = &b_list };12const a = A{ .b_list_pointer = &b_list };
1313
14test "segfault bug" {14test "segfault bug" {
15 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
16
15 const assert = std.debug.assert;17 const assert = std.debug.assert;
16 const obj = B{ .a_pointer = &a };18 const obj = B{ .a_pointer = &a };
17 assert(obj.a_pointer == &a); // this makes zig crash19 assert(obj.a_pointer == &a); // this makes zig crash
...@@ -28,5 +30,7 @@ pub const B2 = struct {...@@ -28,5 +30,7 @@ pub const B2 = struct {
28var b_value = B2{ .pointer_array = &[_]*A2{} };30var b_value = B2{ .pointer_array = &[_]*A2{} };
2931
30test "basic stuff" {32test "basic stuff" {
33 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
34
31 std.debug.assert(&b_value == &b_value);35 std.debug.assert(&b_value == &b_value);
32}36}
test/behavior/bugs/2006.zig+1
...@@ -7,6 +7,7 @@ const S = struct {...@@ -7,6 +7,7 @@ const S = struct {
7};7};
8test "bug 2006" {8test "bug 2006" {
9 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO9 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
10 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1011
11 var a: S = undefined;12 var a: S = undefined;
12 a = S{ .p = undefined };13 a = S{ .p = undefined };
test/behavior/bugs/3007.zig+1
...@@ -22,6 +22,7 @@ test "fixed" {...@@ -22,6 +22,7 @@ test "fixed" {
22 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;22 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
23 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO23 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
24 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO24 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
25 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
2526
26 default_foo = get_foo() catch null; // This Line27 default_foo = get_foo() catch null; // This Line
27 try std.testing.expect(!default_foo.?.free);28 try std.testing.expect(!default_foo.?.free);
test/behavior/bugs/6947.zig+1
...@@ -8,6 +8,7 @@ test {...@@ -8,6 +8,7 @@ test {
8 if (builtin.zig_backend == .stage2_x86) return error.SkipZigTest; // TODO8 if (builtin.zig_backend == .stage2_x86) return error.SkipZigTest; // TODO
9 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO9 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
10 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO10 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
11 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1112
12 var slice: []void = undefined;13 var slice: []void = undefined;
13 destroy(&slice[0]);14 destroy(&slice[0]);
test/behavior/bugs/7325.zig+1
...@@ -81,6 +81,7 @@ test {...@@ -81,6 +81,7 @@ test {
81 if (builtin.zig_backend == .stage2_x86) return error.SkipZigTest; // TODO81 if (builtin.zig_backend == .stage2_x86) return error.SkipZigTest; // TODO
82 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO82 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
83 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO83 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
84 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
8485
85 var param: ParamType = .{86 var param: ParamType = .{
86 .one_of = .{ .name = "name" },87 .one_of = .{ .name = "name" },
test/behavior/error.zig+1
...@@ -943,6 +943,7 @@ test "returning an error union containing a type with no runtime bits" {...@@ -943,6 +943,7 @@ test "returning an error union containing a type with no runtime bits" {
943test "try used in recursive function with inferred error set" {943test "try used in recursive function with inferred error set" {
944 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO944 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
945 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO945 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
946 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
946947
947 const Value = union(enum) {948 const Value = union(enum) {
948 values: []const @This(),949 values: []const @This(),
test/behavior/eval.zig+4
...@@ -391,6 +391,7 @@ test "return 0 from function that has u0 return type" {...@@ -391,6 +391,7 @@ test "return 0 from function that has u0 return type" {
391test "statically initialized struct" {391test "statically initialized struct" {
392 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO392 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
393 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO393 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
394 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
394395
395 st_init_str_foo.x += 1;396 st_init_str_foo.x += 1;
396 try expect(st_init_str_foo.x == 14);397 try expect(st_init_str_foo.x == 14);
...@@ -497,6 +498,7 @@ test "comptime shlWithOverflow" {...@@ -497,6 +498,7 @@ test "comptime shlWithOverflow" {
497test "const ptr to variable data changes at runtime" {498test "const ptr to variable data changes at runtime" {
498 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO499 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
499 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO500 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
501 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
500502
501 try expect(foo_ref.name[0] == 'a');503 try expect(foo_ref.name[0] == 'a');
502 foo_ref.name = "b";504 foo_ref.name = "b";
...@@ -1549,6 +1551,8 @@ test "comptime function turns function value to function pointer" {...@@ -1549,6 +1551,8 @@ test "comptime function turns function value to function pointer" {
1549}1551}
15501552
1551test "container level const and var have unique addresses" {1553test "container level const and var have unique addresses" {
1554 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1555
1552 const S = struct {1556 const S = struct {
1553 x: i32,1557 x: i32,
1554 y: i32,1558 y: i32,
test/behavior/generics.zig+1
...@@ -205,6 +205,7 @@ fn foo2(arg: anytype) bool {...@@ -205,6 +205,7 @@ fn foo2(arg: anytype) bool {
205205
206test "generic struct" {206test "generic struct" {
207 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO207 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
208 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
208209
209 var a1 = GenNode(i32){210 var a1 = GenNode(i32){
210 .value = 13,211 .value = 13,
test/behavior/null.zig+1
...@@ -185,6 +185,7 @@ test "unwrap optional which is field of global var" {...@@ -185,6 +185,7 @@ test "unwrap optional which is field of global var" {
185 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;185 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
186 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;186 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
187 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO187 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
188 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
188189
189 struct_with_optional.field = null;190 struct_with_optional.field = null;
190 if (struct_with_optional.field) |payload| {191 if (struct_with_optional.field) |payload| {
test/behavior/optional.zig+1
...@@ -193,6 +193,7 @@ test "nested orelse" {...@@ -193,6 +193,7 @@ test "nested orelse" {
193test "self-referential struct through a slice of optional" {193test "self-referential struct through a slice of optional" {
194 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO194 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
195 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO195 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
196 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
196197
197 const S = struct {198 const S = struct {
198 const Node = struct {199 const Node = struct {
test/behavior/ptrcast.zig+2
...@@ -130,6 +130,7 @@ test "lower reinterpreted comptime field ptr (with under-aligned fields)" {...@@ -130,6 +130,7 @@ test "lower reinterpreted comptime field ptr (with under-aligned fields)" {
130 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO130 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
131 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO131 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
132 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO132 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
133 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
133134
134 // Test lowering a field ptr135 // Test lowering a field ptr
135 comptime var bytes align(2) = [_]u8{ 1, 2, 3, 4, 5, 6 };136 comptime var bytes align(2) = [_]u8{ 1, 2, 3, 4, 5, 6 };
...@@ -152,6 +153,7 @@ test "lower reinterpreted comptime field ptr" {...@@ -152,6 +153,7 @@ test "lower reinterpreted comptime field ptr" {
152 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO153 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
153 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO154 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
154 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO155 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
156 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
155157
156 // Test lowering a field ptr158 // Test lowering a field ptr
157 comptime var bytes align(4) = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 };159 comptime var bytes align(4) = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
test/behavior/struct.zig+6
...@@ -292,6 +292,7 @@ const Val = struct {...@@ -292,6 +292,7 @@ const Val = struct {
292test "struct point to self" {292test "struct point to self" {
293 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;293 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
294 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO294 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
295 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
295296
296 var root: Node = undefined;297 var root: Node = undefined;
297 root.val.x = 1;298 root.val.x = 1;
...@@ -346,6 +347,7 @@ test "self-referencing struct via array member" {...@@ -346,6 +347,7 @@ test "self-referencing struct via array member" {
346 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;347 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
347 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO348 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
348 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO349 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
350 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
349351
350 const T = struct {352 const T = struct {
351 children: [1]*@This(),353 children: [1]*@This(),
...@@ -368,6 +370,7 @@ const EmptyStruct = struct {...@@ -368,6 +370,7 @@ const EmptyStruct = struct {
368370
369test "align 1 field before self referential align 8 field as slice return type" {371test "align 1 field before self referential align 8 field as slice return type" {
370 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO372 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
373 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
371374
372 const result = alloc(Expr);375 const result = alloc(Expr);
373 try expect(result.len == 0);376 try expect(result.len == 0);
...@@ -733,6 +736,7 @@ test "packed struct with u0 field access" {...@@ -733,6 +736,7 @@ test "packed struct with u0 field access" {
733test "access to global struct fields" {736test "access to global struct fields" {
734 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO737 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
735 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO738 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
739 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
736740
737 g_foo.bar.value = 42;741 g_foo.bar.value = 42;
738 try expect(g_foo.bar.value == 42);742 try expect(g_foo.bar.value == 42);
...@@ -1419,6 +1423,7 @@ test "fieldParentPtr of a zero-bit field" {...@@ -1419,6 +1423,7 @@ test "fieldParentPtr of a zero-bit field" {
14191423
1420test "struct field has a pointer to an aligned version of itself" {1424test "struct field has a pointer to an aligned version of itself" {
1421 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO1425 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1426 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
14221427
1423 const E = struct {1428 const E = struct {
1424 next: *align(1) @This(),1429 next: *align(1) @This(),
...@@ -1514,6 +1519,7 @@ test "function pointer in struct returns the struct" {...@@ -1514,6 +1519,7 @@ test "function pointer in struct returns the struct" {
15141519
1515test "no dependency loop on optional field wrapped in generic function" {1520test "no dependency loop on optional field wrapped in generic function" {
1516 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;1521 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
1522 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
15171523
1518 const S = struct {1524 const S = struct {
1519 fn Atomic(comptime T: type) type {1525 fn Atomic(comptime T: type) type {
test/behavior/struct_contains_null_ptr_itself.zig+1
...@@ -5,6 +5,7 @@ const builtin = @import("builtin");...@@ -5,6 +5,7 @@ const builtin = @import("builtin");
5test "struct contains null pointer which contains original struct" {5test "struct contains null pointer which contains original struct" {
6 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;6 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
7 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO7 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
8 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
89
9 var x: ?*NodeLineComment = null;10 var x: ?*NodeLineComment = null;
10 try expect(x == null);11 try expect(x == null);
test/behavior/struct_contains_slice_of_itself.zig+2
...@@ -13,6 +13,7 @@ const NodeAligned = struct {...@@ -13,6 +13,7 @@ const NodeAligned = struct {
1313
14test "struct contains slice of itself" {14test "struct contains slice of itself" {
15 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO15 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
16 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1617
17 var other_nodes = [_]Node{18 var other_nodes = [_]Node{
18 Node{19 Node{
...@@ -53,6 +54,7 @@ test "struct contains slice of itself" {...@@ -53,6 +54,7 @@ test "struct contains slice of itself" {
53test "struct contains aligned slice of itself" {54test "struct contains aligned slice of itself" {
54 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO55 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
55 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO56 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
57 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
5658
57 var other_nodes = [_]NodeAligned{59 var other_nodes = [_]NodeAligned{
58 NodeAligned{60 NodeAligned{
test/behavior/union.zig+10
...@@ -399,6 +399,7 @@ test "tagged union with no payloads" {...@@ -399,6 +399,7 @@ test "tagged union with no payloads" {
399 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;399 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
400 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;400 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
401 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO401 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
402 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
402403
403 const a = UnionEnumNoPayloads{ .B = {} };404 const a = UnionEnumNoPayloads{ .B = {} };
404 switch (a) {405 switch (a) {
...@@ -473,6 +474,7 @@ test "update the tag value for zero-sized unions" {...@@ -473,6 +474,7 @@ test "update the tag value for zero-sized unions" {
473 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;474 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
474 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;475 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
475 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO476 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
477 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
476478
477 const S = union(enum) {479 const S = union(enum) {
478 U0: void,480 U0: void,
...@@ -513,6 +515,7 @@ test "method call on an empty union" {...@@ -513,6 +515,7 @@ test "method call on an empty union" {
513 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;515 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
514 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;516 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
515 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO517 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
518 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
516519
517 const S = struct {520 const S = struct {
518 const MyUnion = union(MyUnionTag) {521 const MyUnion = union(MyUnionTag) {
...@@ -590,6 +593,7 @@ test "tagged union with all void fields but a meaningful tag" {...@@ -590,6 +593,7 @@ test "tagged union with all void fields but a meaningful tag" {
590 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;593 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
591 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;594 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
592 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO595 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
596 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
593597
594 const S = struct {598 const S = struct {
595 const B = union(enum) {599 const B = union(enum) {
...@@ -791,6 +795,7 @@ test "@unionInit stored to a const" {...@@ -791,6 +795,7 @@ test "@unionInit stored to a const" {
791 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO795 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
792 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO796 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
793 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO797 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
798 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
794799
795 const S = struct {800 const S = struct {
796 const U = union(enum) {801 const U = union(enum) {
...@@ -862,6 +867,7 @@ test "union no tag with struct member" {...@@ -862,6 +867,7 @@ test "union no tag with struct member" {
862 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO867 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
863 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO868 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
864 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO869 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
870 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
865871
866 const Struct = struct {};872 const Struct = struct {};
867 const Union = union {873 const Union = union {
...@@ -1073,6 +1079,7 @@ test "@unionInit on union with tag but no fields" {...@@ -1073,6 +1079,7 @@ test "@unionInit on union with tag but no fields" {
1073 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO1079 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1074 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO1080 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1075 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO1081 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
1082 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
10761083
1077 const S = struct {1084 const S = struct {
1078 const Type = enum(u8) { no_op = 105 };1085 const Type = enum(u8) { no_op = 105 };
...@@ -1121,6 +1128,7 @@ test "global variable struct contains union initialized to non-most-aligned fiel...@@ -1121,6 +1128,7 @@ test "global variable struct contains union initialized to non-most-aligned fiel
1121 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO1128 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1122 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO1129 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1123 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO1130 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
1131 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
11241132
1125 const T = struct {1133 const T = struct {
1126 const U = union(enum) {1134 const U = union(enum) {
...@@ -1340,6 +1348,7 @@ test "union field ptr - zero sized payload" {...@@ -1340,6 +1348,7 @@ test "union field ptr - zero sized payload" {
1340 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO1348 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1341 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO1349 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1342 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO1350 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
1351 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
13431352
1344 const U = union {1353 const U = union {
1345 foo: void,1354 foo: void,
...@@ -1354,6 +1363,7 @@ test "union field ptr - zero sized field" {...@@ -1354,6 +1363,7 @@ test "union field ptr - zero sized field" {
1354 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO1363 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1355 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO1364 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1356 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO1365 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
1366 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
13571367
1358 const U = union {1368 const U = union {
1359 foo: void,1369 foo: void,