authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-10-22 22:17:50+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-23 06:27:12-04:00
log4bf27da6a6f6cf476ca907dd661c0c40c7c68286
tree4958c6b354d0c134ceb13363027f11424779c528
parent6bf554f9a75b412f3d1f2306dff8c0036555f08c

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

This reverts commit 9f0359d78f9facc38418e32b0e8c1bf6f99f0d26 in an attempt to make the tests pass again. The CI failure from that merge should be unrelated to this commit.

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

src/codegen/spirv.zig+292-309
......@@ -209,6 +209,10 @@ const DeclGen = struct {
209209 /// See Object.type_map
210210 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
212216 /// We need to keep track of result ids for block labels, as well as the 'incoming'
213217 /// blocks for a block.
214218 blocks: BlockMap = .{},
......@@ -295,6 +299,7 @@ const DeclGen = struct {
295299 pub fn deinit(self: *DeclGen) void {
296300 self.args.deinit(self.gpa);
297301 self.inst_results.deinit(self.gpa);
302 self.wip_pointers.deinit(self.gpa);
298303 self.blocks.deinit(self.gpa);
299304 self.func.deinit(self.gpa);
300305 self.base_line_stack.deinit(self.gpa);
......@@ -358,8 +363,7 @@ const DeclGen = struct {
358363
359364 const mod = self.module;
360365 const ty = mod.intern_pool.typeOf(val).toType();
361 const ty_ref = try self.resolveType(ty, .indirect);
362 const ptr_ty_ref = try self.spv.ptrType(ty_ref, storage_class);
366 const ptr_ty_ref = try self.ptrType(ty, storage_class);
363367
364368 const var_id = self.spv.declPtr(spv_decl_index).result_id;
365369
......@@ -582,66 +586,41 @@ const DeclGen = struct {
582586 }
583587
584588 /// Construct a struct at runtime.
585 /// result_ty_ref must be a struct type.
589 /// ty must be a struct type.
586590 /// Constituents should be in `indirect` representation (as the elements of a struct should be).
587591 /// Result is in `direct` representation.
588 fn constructStruct(self: *DeclGen, result_ty_ref: CacheRef, constituents: []const IdRef) !IdRef {
592 fn constructStruct(self: *DeclGen, ty: Type, types: []const Type, constituents: []const IdRef) !IdRef {
593 assert(types.len == constituents.len);
589594 // The Khronos LLVM-SPIRV translator crashes because it cannot construct structs which'
590595 // operands are not constant.
591596 // See https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/1349
592597 // For now, just initialize the struct by setting the fields manually...
593598 // TODO: Make this OpCompositeConstruct when we can
594 const ptr_ty_ref = try self.spv.ptrType(result_ty_ref, .Function);
595 const ptr_composite_id = self.spv.allocId();
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);
599 const ptr_composite_id = try self.alloc(ty, .{ .storage_class = .Function });
600 for (constituents, types, 0..) |constitent_id, member_ty, index| {
601 const ptr_member_ty_ref = try self.ptrType(member_ty, .Function);
607602 const ptr_id = try self.accessChain(ptr_member_ty_ref, ptr_composite_id, &.{@as(u32, @intCast(index))});
608603 try self.func.body.emit(self.spv.gpa, .OpStore, .{
609604 .pointer = ptr_id,
610605 .object = constitent_id,
611606 });
612607 }
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;
608 return try self.load(ty, ptr_composite_id, .{});
620609 }
621610
622611 /// Construct an array at runtime.
623 /// result_ty_ref must be an array type.
612 /// ty must be an array type.
624613 /// Constituents should be in `indirect` representation (as the elements of an array should be).
625614 /// Result is in `direct` representation.
626 fn constructArray(self: *DeclGen, result_ty_ref: CacheRef, constituents: []const IdRef) !IdRef {
615 fn constructArray(self: *DeclGen, ty: Type, constituents: []const IdRef) !IdRef {
627616 // The Khronos LLVM-SPIRV translator crashes because it cannot construct structs which'
628617 // operands are not constant.
629618 // See https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/1349
630619 // For now, just initialize the struct by setting the fields manually...
631620 // TODO: Make this OpCompositeConstruct when we can
632 // TODO: Make this Function storage type
633 const ptr_ty_ref = try self.spv.ptrType(result_ty_ref, .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
621 const mod = self.module;
622 const ptr_composite_id = try self.alloc(ty, .{ .storage_class = .Function });
623 const ptr_elem_ty_ref = try self.ptrType(ty.elemType2(mod), .Function);
645624 for (constituents, 0..) |constitent_id, index| {
646625 const ptr_id = try self.accessChain(ptr_elem_ty_ref, ptr_composite_id, &.{@as(u32, @intCast(index))});
647626 try self.func.body.emit(self.spv.gpa, .OpStore, .{
......@@ -649,13 +628,8 @@ const DeclGen = struct {
649628 .object = constitent_id,
650629 });
651630 }
652 const result_id = self.spv.allocId();
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;
631
632 return try self.load(ty, ptr_composite_id, .{});
659633 }
660634
661635 /// This function generates a load for a constant in direct (ie, non-memory) representation.
......@@ -767,15 +741,18 @@ const DeclGen = struct {
767741 }.toValue();
768742
769743 var constituents: [2]IdRef = undefined;
744 var types: [2]Type = undefined;
770745 if (eu_layout.error_first) {
771746 constituents[0] = try self.constant(err_ty, err_val, .indirect);
772747 constituents[1] = try self.constant(payload_ty, payload_val, .indirect);
748 types = .{ err_ty, payload_ty };
773749 } else {
774750 constituents[0] = try self.constant(payload_ty, payload_val, .indirect);
775751 constituents[1] = try self.constant(err_ty, err_val, .indirect);
752 types = .{ payload_ty, err_ty };
776753 }
777754
778 return try self.constructStruct(result_ty_ref, &constituents);
755 return try self.constructStruct(ty, &types, &constituents);
779756 },
780757 .enum_tag => {
781758 const int_val = try val.intFromEnum(ty, mod);
......@@ -793,7 +770,11 @@ const DeclGen = struct {
793770 }
794771
795772 const len_id = try self.constant(Type.usize, ptr.len.toValue(), .indirect);
796 return try self.constructStruct(result_ty_ref, &.{ ptr_id, len_id });
773 return try self.constructStruct(
774 ty,
775 &.{ ptr_ty, Type.usize },
776 &.{ ptr_id, len_id },
777 );
797778 },
798779 .opt => {
799780 const payload_ty = ty.optionalChild(mod);
......@@ -820,7 +801,11 @@ const DeclGen = struct {
820801 else
821802 try self.spv.constUndef(try self.resolveType(payload_ty, .indirect));
822803
823 return try self.constructStruct(result_ty_ref, &.{ payload_id, has_pl_id });
804 return try self.constructStruct(
805 ty,
806 &.{ payload_ty, Type.bool },
807 &.{ payload_id, has_pl_id },
808 );
824809 },
825810 .aggregate => |aggregate| switch (ip.indexToKey(ty.ip_index)) {
826811 inline .array_type, .vector_type => |array_type, tag| {
......@@ -858,7 +843,7 @@ const DeclGen = struct {
858843 else => {},
859844 }
860845
861 return try self.constructArray(result_ty_ref, constituents);
846 return try self.constructArray(ty, constituents);
862847 },
863848 .struct_type => {
864849 const struct_type = mod.typeToStruct(ty).?;
......@@ -866,6 +851,9 @@ const DeclGen = struct {
866851 return self.todo("packed struct constants", .{});
867852 }
868853
854 var types = std.ArrayList(Type).init(self.gpa);
855 defer types.deinit();
856
869857 var constituents = std.ArrayList(IdRef).init(self.gpa);
870858 defer constituents.deinit();
871859
......@@ -881,22 +869,23 @@ const DeclGen = struct {
881869 const field_val = try val.fieldValue(mod, field_index);
882870 const field_id = try self.constant(field_ty, field_val, .indirect);
883871
872 try types.append(field_ty);
884873 try constituents.append(field_id);
885874 }
886875
887 return try self.constructStruct(result_ty_ref, constituents.items);
876 return try self.constructStruct(ty, types.items, constituents.items);
888877 },
889878 .anon_struct_type => unreachable, // TODO
890879 else => unreachable,
891880 },
892881 .un => |un| {
893882 const active_field = ty.unionTagFieldIndex(un.tag.toValue(), mod).?;
894 const layout = self.unionLayout(ty, active_field);
895 const payload = if (layout.active_field_size != 0)
896 try self.constant(layout.active_field_ty, un.val.toValue(), .indirect)
883 const union_obj = mod.typeToUnion(ty).?;
884 const field_ty = union_obj.field_types.get(ip)[active_field].toType();
885 const payload = if (field_ty.hasRuntimeBitsIgnoreComptime(mod))
886 try self.constant(field_ty, un.val.toValue(), .direct)
897887 else
898888 null;
899
900889 return try self.unionInit(ty, active_field, payload);
901890 },
902891 .memoized_call => unreachable,
......@@ -935,8 +924,7 @@ const DeclGen = struct {
935924
936925 // TODO: Can we consolidate this in ptrElemPtr?
937926 const elem_ty = parent_ptr_ty.elemType2(mod); // use elemType() so that we get T for *[N]T.
938 const elem_ty_ref = try self.resolveType(elem_ty, .direct);
939 const elem_ptr_ty_ref = try self.spv.ptrType(elem_ty_ref, spvStorageClass(parent_ptr_ty.ptrAddressSpace(mod)));
927 const elem_ptr_ty_ref = try self.ptrType(elem_ty, spvStorageClass(parent_ptr_ty.ptrAddressSpace(mod)));
940928
941929 if (elem_ptr_ty_ref == result_ty_ref) {
942930 return elem_ptr_id;
......@@ -998,8 +986,7 @@ const DeclGen = struct {
998986 };
999987
1000988 const decl_id = try self.resolveAnonDecl(decl_val, actual_storage_class);
1001 const decl_ty_ref = try self.resolveType(decl_ty, .indirect);
1002 const decl_ptr_ty_ref = try self.spv.ptrType(decl_ty_ref, final_storage_class);
989 const decl_ptr_ty_ref = try self.ptrType(decl_ty, final_storage_class);
1003990
1004991 const ptr_id = switch (final_storage_class) {
1005992 .Generic => blk: {
......@@ -1055,8 +1042,7 @@ const DeclGen = struct {
10551042
10561043 const final_storage_class = spvStorageClass(decl.@"addrspace");
10571044
1058 const decl_ty_ref = try self.resolveType(decl.ty, .indirect);
1059 const decl_ptr_ty_ref = try self.spv.ptrType(decl_ty_ref, final_storage_class);
1045 const decl_ptr_ty_ref = try self.ptrType(decl.ty, final_storage_class);
10601046
10611047 const ptr_id = switch (final_storage_class) {
10621048 .Generic => blk: {
......@@ -1124,29 +1110,52 @@ const DeclGen = struct {
11241110 return try self.intType(.unsigned, self.getTarget().ptrBitWidth());
11251111 }
11261112
1127 /// Generate a union type, optionally with a known field. If the tag alignment is greater
1128 /// than that of the payload, a regular union (non-packed, with both tag and payload), will
1129 /// be generated as follows:
1130 /// If the active field is known:
1113 fn ptrType(self: *DeclGen, child_ty: Type, storage_class: StorageClass) !CacheRef {
1114 const key = .{ child_ty.toIntern(), storage_class };
1115 const entry = try self.wip_pointers.getOrPut(self.gpa, key);
1116 if (entry.found_existing) {
1117 const fwd_ref = entry.value_ptr.*;
1118 try self.spv.cache.recursive_ptrs.put(self.spv.gpa, fwd_ref, {});
1119 return fwd_ref;
1120 }
1121
1122 const fwd_ref = try self.spv.resolve(.{ .fwd_ptr_type = .{
1123 .zig_child_type = child_ty.toIntern(),
1124 .storage_class = storage_class,
1125 } });
1126 entry.value_ptr.* = fwd_ref;
1127
1128 const child_ty_ref = try self.resolveType(child_ty, .indirect);
1129 _ = try self.spv.resolve(.{ .ptr_type = .{
1130 .storage_class = storage_class,
1131 .child_type = child_ty_ref,
1132 .fwd = fwd_ref,
1133 } });
1134
1135 assert(self.wip_pointers.remove(key));
1136
1137 return fwd_ref;
1138 }
1139
1140 /// Generate a union type. Union types are always generated with the
1141 /// most aligned field active. If the tag alignment is greater
1142 /// than that of the payload, a regular union (non-packed, with both tag and
1143 /// payload), will be generated as follows:
11311144 /// struct {
11321145 /// tag: TagType,
1133 /// payload: ActivePayloadType,
1134 /// payload_padding: [payload_size - @sizeOf(ActivePayloadType)]u8,
1146 /// payload: MostAlignedFieldType,
1147 /// payload_padding: [payload_size - @sizeOf(MostAlignedFieldType)]u8,
11351148 /// padding: [padding_size]u8,
11361149 /// }
11371150 /// If the payload alignment is greater than that of the tag:
11381151 /// struct {
1139 /// payload: ActivePayloadType,
1140 /// payload_padding: [payload_size - @sizeOf(ActivePayloadType)]u8,
1152 /// payload: MostAlignedFieldType,
1153 /// payload_padding: [payload_size - @sizeOf(MostAlignedFieldType)]u8,
11411154 /// tag: TagType,
11421155 /// padding: [padding_size]u8,
11431156 /// }
1144 /// If the active payload is unknown, it will default back to the most aligned field. This is
1145 /// to make sure that the overal struct has the correct alignment in spir-v.
11461157 /// If any of the fields' size is 0, it will be omitted.
1147 /// NOTE: When the active field is set to something other than the most aligned field, the
1148 /// resulting struct will be *underaligned*.
1149 fn resolveUnionType(self: *DeclGen, ty: Type, maybe_active_field: ?usize) !CacheRef {
1158 fn resolveUnionType(self: *DeclGen, ty: Type) !CacheRef {
11501159 const mod = self.module;
11511160 const ip = &mod.intern_pool;
11521161 const union_obj = mod.typeToUnion(ty).?;
......@@ -1155,17 +1164,13 @@ const DeclGen = struct {
11551164 return self.todo("packed union types", .{});
11561165 }
11571166
1158 const layout = self.unionLayout(ty, maybe_active_field);
1159
1160 if (layout.payload_size == 0) {
1167 const layout = self.unionLayout(ty);
1168 if (!layout.has_payload) {
11611169 // No payload, so represent this as just the tag type.
11621170 return try self.resolveType(union_obj.enum_tag_ty.toType(), .indirect);
11631171 }
11641172
1165 // TODO: We need to add the active field to the key, somehow.
1166 if (maybe_active_field == null) {
1167 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1168 }
1173 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
11691174
11701175 var member_types: [4]CacheRef = undefined;
11711176 var member_names: [4]CacheString = undefined;
......@@ -1178,10 +1183,10 @@ const DeclGen = struct {
11781183 member_names[layout.tag_index] = try self.spv.resolveString("(tag)");
11791184 }
11801185
1181 if (layout.active_field_size != 0) {
1182 const active_payload_ty_ref = try self.resolveType(layout.active_field_ty, .indirect);
1183 member_types[layout.active_field_index] = active_payload_ty_ref;
1184 member_names[layout.active_field_index] = try self.spv.resolveString("(payload)");
1186 if (layout.payload_size != 0) {
1187 const payload_ty_ref = try self.resolveType(layout.payload_ty, .indirect);
1188 member_types[layout.payload_index] = payload_ty_ref;
1189 member_names[layout.payload_index] = try self.spv.resolveString("(payload)");
11851190 }
11861191
11871192 if (layout.payload_padding_size != 0) {
......@@ -1202,9 +1207,7 @@ const DeclGen = struct {
12021207 .member_names = member_names[0..layout.total_fields],
12031208 } });
12041209
1205 if (maybe_active_field == null) {
1206 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
1207 }
1210 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
12081211 return ty_ref;
12091212 }
12101213
......@@ -1352,12 +1355,12 @@ const DeclGen = struct {
13521355 .Pointer => {
13531356 const ptr_info = ty.ptrInfo(mod);
13541357
1358 // Note: Don't cache this pointer type, it would mess up the recursive pointer functionality
1359 // in ptrType()!
1360
13551361 const storage_class = spvStorageClass(ptr_info.flags.address_space);
1356 const child_ty_ref = try self.resolveType(ptr_info.child.toType(), .indirect);
1357 const ptr_ty_ref = try self.spv.resolve(.{ .ptr_type = .{
1358 .storage_class = storage_class,
1359 .child_type = child_ty_ref,
1360 } });
1362 const ptr_ty_ref = try self.ptrType(ptr_info.child.toType(), storage_class);
1363
13611364 if (ptr_info.flags.size != .Slice) {
13621365 return ptr_ty_ref;
13631366 }
......@@ -1472,7 +1475,7 @@ const DeclGen = struct {
14721475 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
14731476 return ty_ref;
14741477 },
1475 .Union => return try self.resolveUnionType(ty, null),
1478 .Union => return try self.resolveUnionType(ty),
14761479 .ErrorSet => return try self.intType(.unsigned, 16),
14771480 .ErrorUnion => {
14781481 const payload_ty = ty.errorUnionPayload(mod);
......@@ -1586,14 +1589,16 @@ const DeclGen = struct {
15861589 }
15871590
15881591 const UnionLayout = struct {
1589 active_field: u32,
1590 active_field_ty: Type,
1591 payload_size: u32,
1592
1592 /// If false, this union is represented
1593 /// by only an integer of the tag type.
1594 has_payload: bool,
15931595 tag_size: u32,
15941596 tag_index: u32,
1595 active_field_size: u32,
1596 active_field_index: u32,
1597 /// Note: This is the size of the payload type itself, NOT the size of the ENTIRE payload.
1598 /// Use `has_payload` instead!!
1599 payload_ty: Type,
1600 payload_size: u32,
1601 payload_index: u32,
15971602 payload_padding_size: u32,
15981603 payload_padding_index: u32,
15991604 padding_size: u32,
......@@ -1601,23 +1606,19 @@ const DeclGen = struct {
16011606 total_fields: u32,
16021607 };
16031608
1604 fn unionLayout(self: *DeclGen, ty: Type, maybe_active_field: ?usize) UnionLayout {
1609 fn unionLayout(self: *DeclGen, ty: Type) UnionLayout {
16051610 const mod = self.module;
16061611 const ip = &mod.intern_pool;
16071612 const layout = ty.unionGetLayout(self.module);
16081613 const union_obj = mod.typeToUnion(ty).?;
16091614
1610 const active_field = maybe_active_field orelse layout.most_aligned_field;
1611 const active_field_ty = union_obj.field_types.get(ip)[active_field].toType();
1612
16131615 var union_layout = UnionLayout{
1614 .active_field = @intCast(active_field),
1615 .active_field_ty = active_field_ty,
1616 .payload_size = @intCast(layout.payload_size),
1616 .has_payload = layout.payload_size != 0,
16171617 .tag_size = @intCast(layout.tag_size),
16181618 .tag_index = undefined,
1619 .active_field_size = undefined,
1620 .active_field_index = undefined,
1619 .payload_ty = undefined,
1620 .payload_size = undefined,
1621 .payload_index = undefined,
16211622 .payload_padding_size = undefined,
16221623 .payload_padding_index = undefined,
16231624 .padding_size = @intCast(layout.padding),
......@@ -1625,11 +1626,16 @@ const DeclGen = struct {
16251626 .total_fields = undefined,
16261627 };
16271628
1628 union_layout.active_field_size = if (active_field_ty.hasRuntimeBitsIgnoreComptime(mod))
1629 @intCast(active_field_ty.abiSize(mod))
1630 else
1631 0;
1632 union_layout.payload_padding_size = @intCast(layout.payload_size - union_layout.active_field_size);
1629 if (union_layout.has_payload) {
1630 const most_aligned_field = layout.most_aligned_field;
1631 const most_aligned_field_ty = union_obj.field_types.get(ip)[most_aligned_field].toType();
1632 union_layout.payload_ty = most_aligned_field_ty;
1633 union_layout.payload_size = @intCast(most_aligned_field_ty.abiSize(mod));
1634 } else {
1635 union_layout.payload_size = 0;
1636 }
1637
1638 union_layout.payload_padding_size = @intCast(layout.payload_size - union_layout.payload_size);
16331639
16341640 const tag_first = layout.tag_align.compare(.gte, layout.payload_align);
16351641 var field_index: u32 = 0;
......@@ -1639,8 +1645,8 @@ const DeclGen = struct {
16391645 field_index += 1;
16401646 }
16411647
1642 if (union_layout.active_field_size != 0) {
1643 union_layout.active_field_index = field_index;
1648 if (union_layout.payload_size != 0) {
1649 union_layout.payload_index = field_index;
16441650 field_index += 1;
16451651 }
16461652
......@@ -1684,7 +1690,7 @@ const DeclGen = struct {
16841690 /// the name of an error in the text executor.
16851691 fn generateTestEntryPoint(self: *DeclGen, name: []const u8, spv_test_decl_index: SpvModule.Decl.Index) !void {
16861692 const anyerror_ty_ref = try self.resolveType(Type.anyerror, .direct);
1687 const ptr_anyerror_ty_ref = try self.spv.ptrType(anyerror_ty_ref, .CrossWorkgroup);
1693 const ptr_anyerror_ty_ref = try self.ptrType(Type.anyerror, .CrossWorkgroup);
16881694 const void_ty_ref = try self.resolveType(Type.void, .direct);
16891695
16901696 const kernel_proto_ty_ref = try self.spv.resolve(.{ .function_type = .{
......@@ -1719,6 +1725,7 @@ const DeclGen = struct {
17191725 .id_result = error_id,
17201726 .function = test_id,
17211727 });
1728 // Note: Convert to direct not required.
17221729 try section.emit(self.spv.gpa, .OpStore, .{
17231730 .pointer = p_error_id,
17241731 .object = error_id,
......@@ -1823,8 +1830,7 @@ const DeclGen = struct {
18231830 else => final_storage_class,
18241831 };
18251832
1826 const ty_ref = try self.resolveType(decl.ty, .indirect);
1827 const ptr_ty_ref = try self.spv.ptrType(ty_ref, actual_storage_class);
1833 const ptr_ty_ref = try self.ptrType(decl.ty, actual_storage_class);
18281834
18291835 const begin = self.spv.beginGlobal();
18301836 try self.spv.globals.section.emit(self.spv.gpa, .OpVariable, .{
......@@ -1929,11 +1935,15 @@ const DeclGen = struct {
19291935 return try self.convertToDirect(result_ty, result_id);
19301936 }
19311937
1932 fn load(self: *DeclGen, value_ty: Type, ptr_id: IdRef, is_volatile: bool) !IdRef {
1938 const MemoryOptions = struct {
1939 is_volatile: bool = false,
1940 };
1941
1942 fn load(self: *DeclGen, value_ty: Type, ptr_id: IdRef, options: MemoryOptions) !IdRef {
19331943 const indirect_value_ty_ref = try self.resolveType(value_ty, .indirect);
19341944 const result_id = self.spv.allocId();
19351945 const access = spec.MemoryAccess.Extended{
1936 .Volatile = is_volatile,
1946 .Volatile = options.is_volatile,
19371947 };
19381948 try self.func.body.emit(self.spv.gpa, .OpLoad, .{
19391949 .id_result_type = self.typeId(indirect_value_ty_ref),
......@@ -1944,10 +1954,10 @@ const DeclGen = struct {
19441954 return try self.convertToDirect(value_ty, result_id);
19451955 }
19461956
1947 fn store(self: *DeclGen, value_ty: Type, ptr_id: IdRef, value_id: IdRef, is_volatile: bool) !void {
1957 fn store(self: *DeclGen, value_ty: Type, ptr_id: IdRef, value_id: IdRef, options: MemoryOptions) !void {
19481958 const indirect_value_id = try self.convertToIndirect(value_ty, value_id);
19491959 const access = spec.MemoryAccess.Extended{
1950 .Volatile = is_volatile,
1960 .Volatile = options.is_volatile,
19511961 };
19521962 try self.func.body.emit(self.spv.gpa, .OpStore, .{
19531963 .pointer = ptr_id,
......@@ -2119,9 +2129,7 @@ const DeclGen = struct {
21192129 constituent.* = try self.convertToIndirect(child_ty, result_id);
21202130 }
21212131
2122 const result_ty = try self.resolveType(child_ty, .indirect);
2123 const result_ty_ref = try self.spv.arrayType(vector_len, result_ty);
2124 return try self.constructArray(result_ty_ref, constituents);
2132 return try self.constructArray(ty, constituents);
21252133 }
21262134
21272135 const result_id = self.spv.allocId();
......@@ -2182,7 +2190,7 @@ const DeclGen = struct {
21822190
21832191 const info = try self.arithmeticTypeInfo(result_ty);
21842192 // TODO: Use fmin for OpenCL
2185 const cmp_id = try self.cmp(op, result_ty, lhs_id, rhs_id);
2193 const cmp_id = try self.cmp(op, Type.bool, result_ty, lhs_id, rhs_id);
21862194 const selection_id = switch (info.class) {
21872195 .float => blk: {
21882196 // cmp uses OpFOrd. When we have 0 [<>] nan this returns false,
......@@ -2317,7 +2325,7 @@ const DeclGen = struct {
23172325 constituent.* = try self.arithOp(child_ty, lhs_index_id, rhs_index_id, fop, sop, uop, modular);
23182326 }
23192327
2320 return self.constructArray(result_ty_ref, constituents);
2328 return self.constructArray(ty, constituents);
23212329 }
23222330
23232331 // Binary operations are generally applicable to both scalar and vector operations
......@@ -2473,11 +2481,11 @@ const DeclGen = struct {
24732481 // Construct the struct that Zig wants as result.
24742482 // The value should already be the correct type.
24752483 const ov_id = try self.intFromBool(ov_ty_ref, overflowed_id);
2476 const result_ty_ref = try self.resolveType(result_ty, .direct);
2477 return try self.constructStruct(result_ty_ref, &.{
2478 value_id,
2479 ov_id,
2480 });
2484 return try self.constructStruct(
2485 result_ty,
2486 &.{ operand_ty, ov_ty },
2487 &.{ value_id, ov_id },
2488 );
24812489 }
24822490
24832491 fn airShuffle(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
......@@ -2635,6 +2643,7 @@ const DeclGen = struct {
26352643 fn cmp(
26362644 self: *DeclGen,
26372645 op: std.math.CompareOperator,
2646 result_ty: Type,
26382647 ty: Type,
26392648 lhs_id: IdRef,
26402649 rhs_id: IdRef,
......@@ -2675,7 +2684,7 @@ const DeclGen = struct {
26752684 if (ty.optionalReprIsPayload(mod)) {
26762685 assert(payload_ty.hasRuntimeBitsIgnoreComptime(mod));
26772686 assert(!payload_ty.isSlice(mod));
2678 return self.cmp(op, payload_ty, lhs_id, rhs_id);
2687 return self.cmp(op, Type.bool, payload_ty, lhs_id, rhs_id);
26792688 }
26802689
26812690 const lhs_valid_id = if (payload_ty.hasRuntimeBitsIgnoreComptime(mod))
......@@ -2688,7 +2697,7 @@ const DeclGen = struct {
26882697 else
26892698 try self.convertToDirect(Type.bool, rhs_id);
26902699
2691 const valid_cmp_id = try self.cmp(op, Type.bool, lhs_valid_id, rhs_valid_id);
2700 const valid_cmp_id = try self.cmp(op, Type.bool, Type.bool, lhs_valid_id, rhs_valid_id);
26922701 if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
26932702 return valid_cmp_id;
26942703 }
......@@ -2699,7 +2708,7 @@ const DeclGen = struct {
26992708 const lhs_pl_id = try self.extractField(payload_ty, lhs_id, 0);
27002709 const rhs_pl_id = try self.extractField(payload_ty, rhs_id, 0);
27012710
2702 const pl_cmp_id = try self.cmp(op, payload_ty, lhs_pl_id, rhs_pl_id);
2711 const pl_cmp_id = try self.cmp(op, Type.bool, payload_ty, lhs_pl_id, rhs_pl_id);
27032712
27042713 // op == .eq => lhs_valid == rhs_valid && lhs_pl == rhs_pl
27052714 // op == .neq => lhs_valid != rhs_valid || lhs_pl != rhs_pl
......@@ -2721,7 +2730,6 @@ const DeclGen = struct {
27212730 .Vector => {
27222731 const child_ty = ty.childType(mod);
27232732 const vector_len = ty.vectorLen(mod);
2724 const bool_ty_ref_indirect = try self.resolveType(Type.bool, .indirect);
27252733
27262734 var constituents = try self.gpa.alloc(IdRef, vector_len);
27272735 defer self.gpa.free(constituents);
......@@ -2729,12 +2737,11 @@ const DeclGen = struct {
27292737 for (constituents, 0..) |*constituent, i| {
27302738 const lhs_index_id = try self.extractField(child_ty, cmp_lhs_id, @intCast(i));
27312739 const rhs_index_id = try self.extractField(child_ty, cmp_rhs_id, @intCast(i));
2732 const result_id = try self.cmp(op, child_ty, lhs_index_id, rhs_index_id);
2740 const result_id = try self.cmp(op, Type.bool, child_ty, lhs_index_id, rhs_index_id);
27332741 constituent.* = try self.convertToIndirect(Type.bool, result_id);
27342742 }
27352743
2736 const result_ty_ref = try self.spv.arrayType(vector_len, bool_ty_ref_indirect);
2737 return try self.constructArray(result_ty_ref, constituents);
2744 return try self.constructArray(result_ty, constituents);
27382745 },
27392746 else => unreachable,
27402747 };
......@@ -2807,8 +2814,9 @@ const DeclGen = struct {
28072814 const lhs_id = try self.resolve(bin_op.lhs);
28082815 const rhs_id = try self.resolve(bin_op.rhs);
28092816 const ty = self.typeOf(bin_op.lhs);
2817 const result_ty = self.typeOfIndex(inst);
28102818
2811 return try self.cmp(op, ty, lhs_id, rhs_id);
2819 return try self.cmp(op, result_ty, ty, lhs_id, rhs_id);
28122820 }
28132821
28142822 fn airVectorCmp(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
......@@ -2820,8 +2828,9 @@ const DeclGen = struct {
28202828 const rhs_id = try self.resolve(vec_cmp.rhs);
28212829 const op = vec_cmp.compareOperator();
28222830 const ty = self.typeOf(vec_cmp.lhs);
2831 const result_ty = self.typeOfIndex(inst);
28232832
2824 return try self.cmp(op, ty, lhs_id, rhs_id);
2833 return try self.cmp(op, result_ty, ty, lhs_id, rhs_id);
28252834 }
28262835
28272836 fn bitCast(
......@@ -2866,23 +2875,17 @@ const DeclGen = struct {
28662875 return result_id;
28672876 }
28682877
2869 const src_ptr_ty_ref = try self.spv.ptrType(src_ty_ref, .Function);
2870 const dst_ptr_ty_ref = try self.spv.ptrType(dst_ty_ref, .Function);
2878 const dst_ptr_ty_ref = try self.ptrType(dst_ty, .Function);
28712879
2872 const tmp_id = self.spv.allocId();
2873 try self.func.prologue.emit(self.spv.gpa, .OpVariable, .{
2874 .id_result_type = self.typeId(src_ptr_ty_ref),
2875 .id_result = tmp_id,
2876 .storage_class = .Function,
2877 });
2878 try self.store(src_ty, tmp_id, src_id, false);
2880 const tmp_id = try self.alloc(src_ty, .{ .storage_class = .Function });
2881 try self.store(src_ty, tmp_id, src_id, .{});
28792882 const casted_ptr_id = self.spv.allocId();
28802883 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
28812884 .id_result_type = self.typeId(dst_ptr_ty_ref),
28822885 .id_result = casted_ptr_id,
28832886 .operand = tmp_id,
28842887 });
2885 return try self.load(dst_ty, casted_ptr_id, false);
2888 return try self.load(dst_ty, casted_ptr_id, .{});
28862889 }
28872890
28882891 fn airBitCast(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
......@@ -3061,7 +3064,6 @@ const DeclGen = struct {
30613064 const elem_ptr_ty = slice_ty.slicePtrFieldType(mod);
30623065
30633066 const elem_ptr_ty_ref = try self.resolveType(elem_ptr_ty, .direct);
3064 const slice_ty_ref = try self.resolveType(slice_ty, .direct);
30653067 const size_ty_ref = try self.sizeType();
30663068
30673069 const array_ptr_id = try self.resolve(ty_op.operand);
......@@ -3074,7 +3076,11 @@ const DeclGen = struct {
30743076 // Convert the pointer-to-array to a pointer to the first element.
30753077 try self.accessChain(elem_ptr_ty_ref, array_ptr_id, &.{0});
30763078
3077 return try self.constructStruct(slice_ty_ref, &.{ elem_ptr_id, len_id });
3079 return try self.constructStruct(
3080 slice_ty,
3081 &.{ elem_ptr_ty, Type.usize },
3082 &.{ elem_ptr_id, len_id },
3083 );
30783084 }
30793085
30803086 fn airSlice(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
......@@ -3084,13 +3090,16 @@ const DeclGen = struct {
30843090 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
30853091 const ptr_id = try self.resolve(bin_op.lhs);
30863092 const len_id = try self.resolve(bin_op.rhs);
3093 const ptr_ty = self.typeOf(bin_op.lhs);
30873094 const slice_ty = self.typeOfIndex(inst);
3088 const slice_ty_ref = try self.resolveType(slice_ty, .direct);
30893095
3090 return try self.constructStruct(slice_ty_ref, &.{
3091 ptr_id, // Note: Type should not need to be converted to direct.
3092 len_id, // Note: Type should not need to be converted to direct.
3093 });
3096 // Note: Types should not need to be converted to direct, these types
3097 // dont need to be converted.
3098 return try self.constructStruct(
3099 slice_ty,
3100 &.{ ptr_ty, Type.usize },
3101 &.{ ptr_id, len_id },
3102 );
30943103 }
30953104
30963105 fn airAggregateInit(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
......@@ -3100,7 +3109,6 @@ const DeclGen = struct {
31003109 const ip = &mod.intern_pool;
31013110 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
31023111 const result_ty = self.typeOfIndex(inst);
3103 const result_ty_ref = try self.resolveType(result_ty, .direct);
31043112 const len: usize = @intCast(result_ty.arrayLen(mod));
31053113 const elements: []const Air.Inst.Ref = @ptrCast(self.air.extra[ty_pl.payload..][0..len]);
31063114
......@@ -3112,6 +3120,8 @@ const DeclGen = struct {
31123120 unreachable; // TODO
31133121 }
31143122
3123 const types = try self.gpa.alloc(Type, elements.len);
3124 defer self.gpa.free(types);
31153125 const constituents = try self.gpa.alloc(IdRef, elements.len);
31163126 defer self.gpa.free(constituents);
31173127 var index: usize = 0;
......@@ -3123,6 +3133,7 @@ const DeclGen = struct {
31233133 assert(field_ty.toType().hasRuntimeBits(mod));
31243134
31253135 const id = try self.resolve(element);
3136 types[index] = field_ty.toType();
31263137 constituents[index] = try self.convertToIndirect(field_ty.toType(), id);
31273138 index += 1;
31283139 }
......@@ -3136,6 +3147,7 @@ const DeclGen = struct {
31363147 assert(field_ty.hasRuntimeBitsIgnoreComptime(mod));
31373148
31383149 const id = try self.resolve(element);
3150 types[index] = field_ty;
31393151 constituents[index] = try self.convertToIndirect(field_ty, id);
31403152 index += 1;
31413153 }
......@@ -3143,7 +3155,11 @@ const DeclGen = struct {
31433155 else => unreachable,
31443156 }
31453157
3146 return try self.constructStruct(result_ty_ref, constituents[0..index]);
3158 return try self.constructStruct(
3159 result_ty,
3160 types[0..index],
3161 constituents[0..index],
3162 );
31473163 },
31483164 .Array => {
31493165 const array_info = result_ty.arrayInfo(mod);
......@@ -3160,7 +3176,7 @@ const DeclGen = struct {
31603176 elem_ids[n_elems - 1] = try self.constant(array_info.elem_type, sentinel_val, .indirect);
31613177 }
31623178
3163 return try self.constructArray(result_ty_ref, elem_ids);
3179 return try self.constructArray(result_ty, elem_ids);
31643180 },
31653181 else => unreachable,
31663182 }
......@@ -3245,15 +3261,14 @@ const DeclGen = struct {
32453261
32463262 const slice_ptr = try self.extractField(ptr_ty, slice_id, 0);
32473263 const elem_ptr = try self.ptrAccessChain(ptr_ty_ref, slice_ptr, index_id, &.{});
3248 return try self.load(slice_ty.childType(mod), elem_ptr, slice_ty.isVolatilePtr(mod));
3264 return try self.load(slice_ty.childType(mod), elem_ptr, .{ .is_volatile = slice_ty.isVolatilePtr(mod) });
32493265 }
32503266
32513267 fn ptrElemPtr(self: *DeclGen, ptr_ty: Type, ptr_id: IdRef, index_id: IdRef) !IdRef {
32523268 const mod = self.module;
32533269 // Construct new pointer type for the resulting pointer
32543270 const elem_ty = ptr_ty.elemType2(mod); // use elemType() so that we get T for *[N]T.
3255 const elem_ty_ref = try self.resolveType(elem_ty, .direct);
3256 const elem_ptr_ty_ref = try self.spv.ptrType(elem_ty_ref, spvStorageClass(ptr_ty.ptrAddressSpace(mod)));
3271 const elem_ptr_ty_ref = try self.ptrType(elem_ty, spvStorageClass(ptr_ty.ptrAddressSpace(mod)));
32573272 if (ptr_ty.isSinglePointer(mod)) {
32583273 // Pointer-to-array. In this case, the resulting pointer is not of the same type
32593274 // as the ptr_ty (we want a *T, not a *[N]T), and hence we need to use accessChain.
......@@ -3289,9 +3304,7 @@ const DeclGen = struct {
32893304 const mod = self.module;
32903305 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
32913306 const array_ty = self.typeOf(bin_op.lhs);
3292 const array_ty_ref = try self.resolveType(array_ty, .direct);
32933307 const elem_ty = array_ty.childType(mod);
3294 const elem_ty_ref = try self.resolveType(elem_ty, .indirect);
32953308 const array_id = try self.resolve(bin_op.lhs);
32963309 const index_id = try self.resolve(bin_op.rhs);
32973310
......@@ -3299,22 +3312,12 @@ const DeclGen = struct {
32993312 // For now, just generate a temporary and use that.
33003313 // TODO: This backend probably also should use isByRef from llvm...
33013314
3302 const array_ptr_ty_ref = try self.spv.ptrType(array_ty_ref, .Function);
3303 const elem_ptr_ty_ref = try self.spv.ptrType(elem_ty_ref, .Function);
3304
3305 const tmp_id = self.spv.allocId();
3306 try self.func.prologue.emit(self.spv.gpa, .OpVariable, .{
3307 .id_result_type = self.typeId(array_ptr_ty_ref),
3308 .id_result = tmp_id,
3309 .storage_class = .Function,
3310 });
3311 try self.func.body.emit(self.spv.gpa, .OpStore, .{
3312 .pointer = tmp_id,
3313 .object = array_id,
3314 });
3315 const elem_ptr_ty_ref = try self.ptrType(elem_ty, .Function);
33153316
3317 const tmp_id = try self.alloc(array_ty, .{ .storage_class = .Function });
3318 try self.store(array_ty, tmp_id, array_id, .{});
33163319 const elem_ptr_id = try self.accessChainId(elem_ptr_ty_ref, tmp_id, &.{index_id});
3317 return try self.load(elem_ty, elem_ptr_id, false);
3320 return try self.load(elem_ty, elem_ptr_id, .{});
33183321 }
33193322
33203323 fn airPtrElemVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
......@@ -3327,7 +3330,7 @@ const DeclGen = struct {
33273330 const ptr_id = try self.resolve(bin_op.lhs);
33283331 const index_id = try self.resolve(bin_op.rhs);
33293332 const elem_ptr_id = try self.ptrElemPtr(ptr_ty, ptr_id, index_id);
3330 return try self.load(elem_ty, elem_ptr_id, ptr_ty.isVolatilePtr(mod));
3333 return try self.load(elem_ty, elem_ptr_id, .{ .is_volatile = ptr_ty.isVolatilePtr(mod) });
33313334 }
33323335
33333336 fn airSetUnionTag(self: *DeclGen, inst: Air.Inst.Index) !void {
......@@ -3335,22 +3338,21 @@ const DeclGen = struct {
33353338 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
33363339 const un_ptr_ty = self.typeOf(bin_op.lhs);
33373340 const un_ty = un_ptr_ty.childType(mod);
3338 const layout = self.unionLayout(un_ty, null);
3341 const layout = self.unionLayout(un_ty);
33393342
33403343 if (layout.tag_size == 0) return;
33413344
33423345 const tag_ty = un_ty.unionTagTypeSafety(mod).?;
3343 const tag_ty_ref = try self.resolveType(tag_ty, .indirect);
3344 const tag_ptr_ty_ref = try self.spv.ptrType(tag_ty_ref, spvStorageClass(un_ptr_ty.ptrAddressSpace(mod)));
3346 const tag_ptr_ty_ref = try self.ptrType(tag_ty, spvStorageClass(un_ptr_ty.ptrAddressSpace(mod)));
33453347
33463348 const union_ptr_id = try self.resolve(bin_op.lhs);
33473349 const new_tag_id = try self.resolve(bin_op.rhs);
33483350
3349 if (layout.payload_size == 0) {
3350 try self.store(tag_ty, union_ptr_id, new_tag_id, un_ptr_ty.isVolatilePtr(mod));
3351 if (!layout.has_payload) {
3352 try self.store(tag_ty, union_ptr_id, new_tag_id, .{ .is_volatile = un_ptr_ty.isVolatilePtr(mod) });
33513353 } else {
33523354 const ptr_id = try self.accessChain(tag_ptr_ty_ref, union_ptr_id, &.{layout.tag_index});
3353 try self.store(tag_ty, ptr_id, new_tag_id, un_ptr_ty.isVolatilePtr(mod));
3355 try self.store(tag_ty, ptr_id, new_tag_id, .{ .is_volatile = un_ptr_ty.isVolatilePtr(mod) });
33543356 }
33553357 }
33563358
......@@ -3361,11 +3363,11 @@ const DeclGen = struct {
33613363 const un_ty = self.typeOf(ty_op.operand);
33623364
33633365 const mod = self.module;
3364 const layout = self.unionLayout(un_ty, null);
3366 const layout = self.unionLayout(un_ty);
33653367 if (layout.tag_size == 0) return null;
33663368
33673369 const union_handle = try self.resolve(ty_op.operand);
3368 if (layout.payload_size == 0) return union_handle;
3370 if (!layout.has_payload) return union_handle;
33693371
33703372 const tag_ty = un_ty.unionTagTypeSafety(mod).?;
33713373 return try self.extractField(tag_ty, union_handle, layout.tag_index);
......@@ -3378,8 +3380,8 @@ const DeclGen = struct {
33783380 payload: ?IdRef,
33793381 ) !IdRef {
33803382 // To initialize a union, generate a temporary variable with the
3381 // type that has the right field active, then pointer-cast and store
3382 // the active field, and finally load and return the entire union.
3383 // union type, then get the field pointer and pointer-cast it to the
3384 // right type to store it. Finally load the entire union.
33833385
33843386 const mod = self.module;
33853387 const ip = &mod.intern_pool;
......@@ -3390,7 +3392,7 @@ const DeclGen = struct {
33903392 }
33913393
33923394 const maybe_tag_ty = ty.unionTagTypeSafety(mod);
3393 const layout = self.unionLayout(ty, active_field);
3395 const layout = self.unionLayout(ty);
33943396
33953397 const tag_int = if (layout.tag_size != 0) blk: {
33963398 const tag_ty = maybe_tag_ty.?;
......@@ -3401,42 +3403,34 @@ const DeclGen = struct {
34013403 break :blk tag_int_val.toUnsignedInt(mod);
34023404 } else 0;
34033405
3404 if (layout.payload_size == 0) {
3406 if (!layout.has_payload) {
34053407 const tag_ty_ref = try self.resolveType(maybe_tag_ty.?, .direct);
34063408 return try self.constInt(tag_ty_ref, tag_int);
34073409 }
34083410
3409 const un_active_ty_ref = try self.resolveUnionType(ty, active_field);
3410 const un_active_ptr_ty_ref = try self.spv.ptrType(un_active_ty_ref, .Function);
3411 const un_general_ty_ref = try self.resolveType(ty, .direct);
3412 const un_general_ptr_ty_ref = try self.spv.ptrType(un_general_ty_ref, .Function);
3413
3414 const tmp_id = self.spv.allocId();
3415 try self.func.prologue.emit(self.spv.gpa, .OpVariable, .{
3416 .id_result_type = self.typeId(un_active_ptr_ty_ref),
3417 .id_result = tmp_id,
3418 .storage_class = .Function,
3419 });
3411 const tmp_id = try self.alloc(ty, .{ .storage_class = .Function });
34203412
34213413 if (layout.tag_size != 0) {
34223414 const tag_ty_ref = try self.resolveType(maybe_tag_ty.?, .direct);
3423 const tag_ptr_ty_ref = try self.spv.ptrType(tag_ty_ref, .Function);
3415 const tag_ptr_ty_ref = try self.ptrType(maybe_tag_ty.?, .Function);
34243416 const ptr_id = try self.accessChain(tag_ptr_ty_ref, tmp_id, &.{@as(u32, @intCast(layout.tag_index))});
34253417 const tag_id = try self.constInt(tag_ty_ref, tag_int);
3426 try self.func.body.emit(self.spv.gpa, .OpStore, .{
3427 .pointer = ptr_id,
3428 .object = tag_id,
3429 });
3418 try self.store(maybe_tag_ty.?, ptr_id, tag_id, .{});
34303419 }
34313420
3432 if (layout.active_field_size != 0) {
3433 const active_field_ty_ref = try self.resolveType(layout.active_field_ty, .indirect);
3434 const active_field_ptr_ty_ref = try self.spv.ptrType(active_field_ty_ref, .Function);
3435 const ptr_id = try self.accessChain(active_field_ptr_ty_ref, tmp_id, &.{@as(u32, @intCast(layout.active_field_index))});
3436 try self.func.body.emit(self.spv.gpa, .OpStore, .{
3437 .pointer = ptr_id,
3438 .object = payload.?,
3421 const payload_ty = union_ty.field_types.get(ip)[active_field].toType();
3422 if (payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
3423 const pl_ptr_ty_ref = try self.ptrType(layout.payload_ty, .Function);
3424 const pl_ptr_id = try self.accessChain(pl_ptr_ty_ref, tmp_id, &.{layout.payload_index});
3425 const active_pl_ptr_ty_ref = try self.ptrType(payload_ty, .Function);
3426 const active_pl_ptr_id = self.spv.allocId();
3427 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
3428 .id_result_type = self.typeId(active_pl_ptr_ty_ref),
3429 .id_result = active_pl_ptr_id,
3430 .operand = pl_ptr_id,
34393431 });
3432
3433 try self.store(payload_ty, active_pl_ptr_id, payload.?, .{});
34403434 } else {
34413435 assert(payload == null);
34423436 }
......@@ -3444,34 +3438,21 @@ const DeclGen = struct {
34443438 // Just leave the padding fields uninitialized...
34453439 // TODO: Or should we initialize them with undef explicitly?
34463440
3447 // Now cast the pointer and load it as the 'generic' union type.
3448
3449 const casted_var_id = self.spv.allocId();
3450 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
3451 .id_result_type = self.typeId(un_general_ptr_ty_ref),
3452 .id_result = casted_var_id,
3453 .operand = tmp_id,
3454 });
3455
3456 const result_id = self.spv.allocId();
3457 try self.func.body.emit(self.spv.gpa, .OpLoad, .{
3458 .id_result_type = self.typeId(un_general_ty_ref),
3459 .id_result = result_id,
3460 .pointer = casted_var_id,
3461 });
3462
3463 return result_id;
3441 return try self.load(ty, tmp_id, .{});
34643442 }
34653443
34663444 fn airUnionInit(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
34673445 if (self.liveness.isUnused(inst)) return null;
34683446
3447 const mod = self.module;
3448 const ip = &mod.intern_pool;
34693449 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
34703450 const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data;
34713451 const ty = self.typeOfIndex(inst);
3472 const layout = self.unionLayout(ty, extra.field_index);
34733452
3474 const payload = if (layout.active_field_size != 0)
3453 const union_obj = mod.typeToUnion(ty).?;
3454 const field_ty = union_obj.field_types.get(ip)[extra.field_index].toType();
3455 const payload = if (field_ty.hasRuntimeBitsIgnoreComptime(mod))
34753456 try self.resolve(extra.init)
34763457 else
34773458 null;
......@@ -3500,30 +3481,24 @@ const DeclGen = struct {
35003481 .Union => switch (object_ty.containerLayout(mod)) {
35013482 .Packed => unreachable, // TODO
35023483 else => {
3503 // Store, pointer-cast, load
3504 const un_general_ty_ref = try self.resolveType(object_ty, .indirect);
3505 const un_general_ptr_ty_ref = try self.spv.ptrType(un_general_ty_ref, .Function);
3506 const un_active_ty_ref = try self.resolveUnionType(object_ty, field_index);
3507 const un_active_ptr_ty_ref = try self.spv.ptrType(un_active_ty_ref, .Function);
3508 const field_ty_ref = try self.resolveType(field_ty, .indirect);
3509 const field_ptr_ty_ref = try self.spv.ptrType(field_ty_ref, .Function);
3510
3511 const tmp_id = self.spv.allocId();
3512 try self.func.prologue.emit(self.spv.gpa, .OpVariable, .{
3513 .id_result_type = self.typeId(un_general_ptr_ty_ref),
3514 .id_result = tmp_id,
3515 .storage_class = .Function,
3516 });
3517 try self.store(object_ty, tmp_id, object_id, false);
3518 const casted_tmp_id = self.spv.allocId();
3484 // Store, ptr-elem-ptr, pointer-cast, load
3485 const layout = self.unionLayout(object_ty);
3486 assert(layout.has_payload);
3487
3488 const tmp_id = try self.alloc(object_ty, .{ .storage_class = .Function });
3489 try self.store(object_ty, tmp_id, object_id, .{});
3490
3491 const pl_ptr_ty_ref = try self.ptrType(layout.payload_ty, .Function);
3492 const pl_ptr_id = try self.accessChain(pl_ptr_ty_ref, tmp_id, &.{layout.payload_index});
3493
3494 const active_pl_ptr_ty_ref = try self.ptrType(field_ty, .Function);
3495 const active_pl_ptr_id = self.spv.allocId();
35193496 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
3520 .id_result_type = self.typeId(un_active_ptr_ty_ref),
3521 .id_result = casted_tmp_id,
3522 .operand = tmp_id,
3497 .id_result_type = self.typeId(active_pl_ptr_ty_ref),
3498 .id_result = active_pl_ptr_id,
3499 .operand = pl_ptr_id,
35233500 });
3524 const layout = self.unionLayout(object_ty, field_index);
3525 const field_ptr_id = try self.accessChain(field_ptr_ty_ref, casted_tmp_id, &.{layout.active_field_index});
3526 return try self.load(field_ty, field_ptr_id, false);
3501 return try self.load(field_ty, active_pl_ptr_id, .{});
35273502 },
35283503 },
35293504 else => unreachable,
......@@ -3582,18 +3557,24 @@ const DeclGen = struct {
35823557 .Union => switch (object_ty.containerLayout(mod)) {
35833558 .Packed => unreachable, // TODO
35843559 else => {
3560 const layout = self.unionLayout(object_ty);
3561 if (!layout.has_payload) {
3562 // Asked to get a pointer to a zero-sized field. Just lower this
3563 // to undefined, there is no reason to make it be a valid pointer.
3564 return try self.spv.constUndef(result_ty_ref);
3565 }
3566
35853567 const storage_class = spvStorageClass(object_ptr_ty.ptrAddressSpace(mod));
3586 const un_active_ty_ref = try self.resolveUnionType(object_ty, field_index);
3587 const un_active_ptr_ty_ref = try self.spv.ptrType(un_active_ty_ref, storage_class);
3568 const pl_ptr_ty_ref = try self.ptrType(layout.payload_ty, storage_class);
3569 const pl_ptr_id = try self.accessChain(pl_ptr_ty_ref, object_ptr, &.{layout.payload_index});
35883570
3589 const casted_id = self.spv.allocId();
3571 const active_pl_ptr_id = self.spv.allocId();
35903572 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
3591 .id_result_type = self.typeId(un_active_ptr_ty_ref),
3592 .id_result = casted_id,
3593 .operand = object_ptr,
3573 .id_result_type = self.typeId(result_ty_ref),
3574 .id_result = active_pl_ptr_id,
3575 .operand = pl_ptr_id,
35943576 });
3595 const layout = self.unionLayout(object_ty, field_index);
3596 return try self.accessChain(result_ty_ref, casted_id, &.{layout.active_field_index});
3577 return active_pl_ptr_id;
35973578 },
35983579 },
35993580 else => unreachable,
......@@ -3609,23 +3590,13 @@ const DeclGen = struct {
36093590 return try self.structFieldPtr(result_ptr_ty, struct_ptr_ty, struct_ptr, field_index);
36103591 }
36113592
3612 /// We cannot use an OpVariable directly in an OpSpecConstantOp, but we can
3613 /// after we insert a dummy AccessChain...
3614 /// TODO: Get rid of this
3615 fn makePointerConstant(
3616 self: *DeclGen,
3617 section: *SpvSection,
3618 ptr_ty_ref: CacheRef,
3619 ptr_id: IdRef,
3620 ) !IdRef {
3621 const result_id = self.spv.allocId();
3622 try section.emitSpecConstantOp(self.spv.gpa, .OpInBoundsAccessChain, .{
3623 .id_result_type = self.typeId(ptr_ty_ref),
3624 .id_result = result_id,
3625 .base = ptr_id,
3626 });
3627 return result_id;
3628 }
3593 const AllocOptions = struct {
3594 initializer: ?IdRef = null,
3595 /// The final storage class of the pointer. This may be either `.Generic` or `.Function`.
3596 /// In either case, the local is allocated in the `.Function` storage class, and optionally
3597 /// cast back to `.Generic`.
3598 storage_class: StorageClass = .Generic,
3599 };
36293600
36303601 // Allocate a function-local variable, with possible initializer.
36313602 // This function returns a pointer to a variable of type `ty_ref`,
......@@ -3633,30 +3604,36 @@ const DeclGen = struct {
36333604 // placed in the Function address space.
36343605 fn alloc(
36353606 self: *DeclGen,
3636 ty_ref: CacheRef,
3637 initializer: ?IdRef,
3607 ty: Type,
3608 options: AllocOptions,
36383609 ) !IdRef {
3639 const fn_ptr_ty_ref = try self.spv.ptrType(ty_ref, .Function);
3640 const general_ptr_ty_ref = try self.spv.ptrType(ty_ref, .Generic);
3610 const ptr_fn_ty_ref = try self.ptrType(ty, .Function);
36413611
36423612 // SPIR-V requires that OpVariable declarations for locals go into the first block, so we are just going to
36433613 // directly generate them into func.prologue instead of the body.
36443614 const var_id = self.spv.allocId();
36453615 try self.func.prologue.emit(self.spv.gpa, .OpVariable, .{
3646 .id_result_type = self.typeId(fn_ptr_ty_ref),
3616 .id_result_type = self.typeId(ptr_fn_ty_ref),
36473617 .id_result = var_id,
36483618 .storage_class = .Function,
3649 .initializer = initializer,
3619 .initializer = options.initializer,
36503620 });
36513621
3652 // Convert to a generic pointer
3653 const result_id = self.spv.allocId();
3654 try self.func.body.emit(self.spv.gpa, .OpPtrCastToGeneric, .{
3655 .id_result_type = self.typeId(general_ptr_ty_ref),
3656 .id_result = result_id,
3657 .pointer = var_id,
3658 });
3659 return result_id;
3622 switch (options.storage_class) {
3623 .Generic => {
3624 const ptr_gn_ty_ref = try self.ptrType(ty, .Generic);
3625 // Convert to a generic pointer
3626 const result_id = self.spv.allocId();
3627 try self.func.body.emit(self.spv.gpa, .OpPtrCastToGeneric, .{
3628 .id_result_type = self.typeId(ptr_gn_ty_ref),
3629 .id_result = result_id,
3630 .pointer = var_id,
3631 });
3632 return result_id;
3633 },
3634 .Function => return var_id,
3635 else => unreachable,
3636 }
36603637 }
36613638
36623639 fn airAlloc(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
......@@ -3665,8 +3642,7 @@ const DeclGen = struct {
36653642 const ptr_ty = self.typeOfIndex(inst);
36663643 assert(ptr_ty.ptrAddressSpace(mod) == .generic);
36673644 const child_ty = ptr_ty.childType(mod);
3668 const child_ty_ref = try self.resolveType(child_ty, .indirect);
3669 return try self.alloc(child_ty_ref, null);
3645 return try self.alloc(child_ty, .{});
36703646 }
36713647
36723648 fn airArg(self: *DeclGen) IdRef {
......@@ -3781,7 +3757,7 @@ const DeclGen = struct {
37813757 const operand = try self.resolve(ty_op.operand);
37823758 if (!ptr_ty.isVolatilePtr(mod) and self.liveness.isUnused(inst)) return null;
37833759
3784 return try self.load(elem_ty, operand, ptr_ty.isVolatilePtr(mod));
3760 return try self.load(elem_ty, operand, .{ .is_volatile = ptr_ty.isVolatilePtr(mod) });
37853761 }
37863762
37873763 fn airStore(self: *DeclGen, inst: Air.Inst.Index) !void {
......@@ -3791,7 +3767,7 @@ const DeclGen = struct {
37913767 const ptr = try self.resolve(bin_op.lhs);
37923768 const value = try self.resolve(bin_op.rhs);
37933769
3794 try self.store(elem_ty, ptr, value, ptr_ty.isVolatilePtr(self.module));
3770 try self.store(elem_ty, ptr, value, .{ .is_volatile = ptr_ty.isVolatilePtr(self.module) });
37953771 }
37963772
37973773 fn airLoop(self: *DeclGen, inst: Air.Inst.Index) !void {
......@@ -3855,7 +3831,7 @@ const DeclGen = struct {
38553831 }
38563832
38573833 const ptr = try self.resolve(un_op);
3858 const value = try self.load(ret_ty, ptr, ptr_ty.isVolatilePtr(mod));
3834 const value = try self.load(ret_ty, ptr, .{ .is_volatile = ptr_ty.isVolatilePtr(mod) });
38593835 try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{
38603836 .value = value,
38613837 });
......@@ -3981,8 +3957,11 @@ const DeclGen = struct {
39813957 members[eu_layout.errorFieldIndex()] = operand_id;
39823958 members[eu_layout.payloadFieldIndex()] = try self.spv.constUndef(payload_ty_ref);
39833959
3984 const err_union_ty_ref = try self.resolveType(err_union_ty, .direct);
3985 return try self.constructStruct(err_union_ty_ref, &members);
3960 var types: [2]Type = undefined;
3961 types[eu_layout.errorFieldIndex()] = Type.anyerror;
3962 types[eu_layout.payloadFieldIndex()] = payload_ty;
3963
3964 return try self.constructStruct(err_union_ty, &types, &members);
39863965 }
39873966
39883967 fn airWrapErrUnionPayload(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
......@@ -4003,8 +3982,11 @@ const DeclGen = struct {
40033982 members[eu_layout.errorFieldIndex()] = try self.constInt(err_ty_ref, 0);
40043983 members[eu_layout.payloadFieldIndex()] = try self.convertToIndirect(payload_ty, operand_id);
40053984
4006 const err_union_ty_ref = try self.resolveType(err_union_ty, .direct);
4007 return try self.constructStruct(err_union_ty_ref, &members);
3985 var types: [2]Type = undefined;
3986 types[eu_layout.errorFieldIndex()] = Type.anyerror;
3987 types[eu_layout.payloadFieldIndex()] = payload_ty;
3988
3989 return try self.constructStruct(err_union_ty, &types, &members);
40083990 }
40093991
40103992 fn airIsNull(self: *DeclGen, inst: Air.Inst.Index, pred: enum { is_null, is_non_null }) !?IdRef {
......@@ -4038,7 +4020,7 @@ const DeclGen = struct {
40384020 .is_null => .eq,
40394021 .is_non_null => .neq,
40404022 };
4041 return try self.cmp(op, ptr_ty, ptr_id, null_id);
4023 return try self.cmp(op, Type.bool, ptr_ty, ptr_id, null_id);
40424024 }
40434025
40444026 const is_non_null_id = if (payload_ty.hasRuntimeBitsIgnoreComptime(mod))
......@@ -4136,10 +4118,10 @@ const DeclGen = struct {
41364118 return operand_id;
41374119 }
41384120
4139 const optional_ty_ref = try self.resolveType(optional_ty, .direct);
41404121 const payload_id = try self.convertToIndirect(payload_ty, operand_id);
41414122 const members = [_]IdRef{ payload_id, try self.constBool(true, .indirect) };
4142 return try self.constructStruct(optional_ty_ref, &members);
4123 const types = [_]Type{ payload_ty, Type.bool };
4124 return try self.constructStruct(optional_ty, &types, &members);
41434125 }
41444126
41454127 fn airSwitchBr(self: *DeclGen, inst: Air.Inst.Index) !void {
......@@ -4421,6 +4403,7 @@ const DeclGen = struct {
44214403 }
44224404
44234405 // TODO: Multiple results
4406 // TODO: Check that the output type from assembly is the same as the type actually expected by Zig.
44244407 }
44254408
44264409 return null;
src/codegen/spirv/Assembler.zig+10-4
......@@ -304,10 +304,16 @@ fn processTypeInstruction(self: *Assembler) !AsmValue {
304304 // and so some consideration must be taken when entering this in the type system.
305305 return self.todo("process OpTypeArray", .{});
306306 },
307 .OpTypePointer => try self.spv.ptrType(
308 try self.resolveTypeRef(operands[2].ref_id),
309 @as(spec.StorageClass, @enumFromInt(operands[1].value)),
310 ),
307 .OpTypePointer => blk: {
308 break :blk try self.spv.resolve(.{
309 .ptr_type = .{
310 .storage_class = @enumFromInt(operands[1].value),
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 },
311317 .OpTypeFunction => blk: {
312318 const param_operands = operands[2..];
313319 const param_types = try self.spv.gpa.alloc(CacheRef, param_operands.len);
src/codegen/spirv/Cache.zig+182-124
......@@ -22,6 +22,8 @@ const Opcode = spec.Opcode;
2222const IdResult = spec.IdResult;
2323const StorageClass = spec.StorageClass;
2424
25const InternPool = @import("../../InternPool.zig");
26
2527const Self = @This();
2628
2729map: std.AutoArrayHashMapUnmanaged(void, void) = .{},
......@@ -31,6 +33,8 @@ extra: std.ArrayListUnmanaged(u32) = .{},
3133string_bytes: std.ArrayListUnmanaged(u8) = .{},
3234strings: std.AutoArrayHashMapUnmanaged(void, u32) = .{},
3335
36recursive_ptrs: std.AutoHashMapUnmanaged(Ref, void) = .{},
37
3438const Item = struct {
3539 tag: Tag,
3640 /// The result-id that this item uses.
......@@ -62,18 +66,21 @@ const Tag = enum {
6266 /// Function (proto)type
6367 /// data is payload to FunctionType
6468 type_function,
65 /// Pointer type in the CrossWorkgroup storage class
66 /// data is child type
67 type_ptr_generic,
68 /// Pointer type in the CrossWorkgroup storage class
69 /// data is child type
70 type_ptr_crosswgp,
71 /// Pointer type in the Function storage class
72 /// data is child type
73 type_ptr_function,
69 // /// Pointer type in the CrossWorkgroup storage class
70 // /// data is child type
71 // type_ptr_generic,
72 // /// Pointer type in the CrossWorkgroup storage class
73 // /// data is child type
74 // type_ptr_crosswgp,
75 // /// Pointer type in the Function storage class
76 // /// data is child type
77 // type_ptr_function,
7478 /// Simple pointer type that does not have any decorations.
7579 /// data is payload to SimplePointerType
7680 type_ptr_simple,
81 /// A forward declaration for a pointer.
82 /// data is ForwardPointerType
83 type_fwd_ptr,
7784 /// Simple structure type that does not have any decorations.
7885 /// data is payload to SimpleStructType
7986 type_struct_simple,
......@@ -142,6 +149,12 @@ const Tag = enum {
142149 const SimplePointerType = struct {
143150 storage_class: StorageClass,
144151 child_type: Ref,
152 fwd: Ref,
153 };
154
155 const ForwardPointerType = struct {
156 storage_class: StorageClass,
157 zig_child_type: InternPool.Index,
145158 };
146159
147160 /// Trailing:
......@@ -163,14 +176,14 @@ const Tag = enum {
163176 fn encode(value: f64) Float64 {
164177 const bits = @as(u64, @bitCast(value));
165178 return .{
166 .low = @as(u32, @truncate(bits)),
167 .high = @as(u32, @truncate(bits >> 32)),
179 .low = @truncate(bits),
180 .high = @truncate(bits >> 32),
168181 };
169182 }
170183
171184 fn decode(self: Float64) f64 {
172185 const bits = @as(u64, self.low) | (@as(u64, self.high) << 32);
173 return @as(f64, @bitCast(bits));
186 return @bitCast(bits);
174187 }
175188 };
176189
......@@ -192,8 +205,8 @@ const Tag = enum {
192205 fn encode(ty: Ref, value: u64) Int64 {
193206 return .{
194207 .ty = ty,
195 .low = @as(u32, @truncate(value)),
196 .high = @as(u32, @truncate(value >> 32)),
208 .low = @truncate(value),
209 .high = @truncate(value >> 32),
197210 };
198211 }
199212
......@@ -210,8 +223,8 @@ const Tag = enum {
210223 fn encode(ty: Ref, value: i64) Int64 {
211224 return .{
212225 .ty = ty,
213 .low = @as(u32, @truncate(@as(u64, @bitCast(value)))),
214 .high = @as(u32, @truncate(@as(u64, @bitCast(value)) >> 32)),
226 .low = @truncate(@as(u64, @bitCast(value))),
227 .high = @truncate(@as(u64, @bitCast(value)) >> 32),
215228 };
216229 }
217230
......@@ -237,6 +250,7 @@ pub const Key = union(enum) {
237250 array_type: ArrayType,
238251 function_type: FunctionType,
239252 ptr_type: PointerType,
253 fwd_ptr_type: ForwardPointerType,
240254 struct_type: StructType,
241255 opaque_type: OpaqueType,
242256
......@@ -273,12 +287,18 @@ pub const Key = union(enum) {
273287 pub const PointerType = struct {
274288 storage_class: StorageClass,
275289 child_type: Ref,
290 fwd: Ref,
276291 // TODO: Decorations:
277292 // - Alignment
278293 // - ArrayStride,
279294 // - MaxByteOffset,
280295 };
281296
297 pub const ForwardPointerType = struct {
298 zig_child_type: InternPool.Index,
299 storage_class: StorageClass,
300 };
301
282302 pub const StructType = struct {
283303 // TODO: Decorations.
284304 /// The name of the structure. Can be `.none`.
......@@ -313,21 +333,21 @@ pub const Key = union(enum) {
313333 /// Turns this value into the corresponding 32-bit literal, 2s complement signed.
314334 fn toBits32(self: Int) u32 {
315335 return switch (self.value) {
316 .uint64 => |val| @as(u32, @intCast(val)),
317 .int64 => |val| if (val < 0) @as(u32, @bitCast(@as(i32, @intCast(val)))) else @as(u32, @intCast(val)),
336 .uint64 => |val| @intCast(val),
337 .int64 => |val| if (val < 0) @bitCast(@as(i32, @intCast(val))) else @intCast(val),
318338 };
319339 }
320340
321341 fn toBits64(self: Int) u64 {
322342 return switch (self.value) {
323343 .uint64 => |val| val,
324 .int64 => |val| @as(u64, @bitCast(val)),
344 .int64 => |val| @bitCast(val),
325345 };
326346 }
327347
328348 fn to(self: Int, comptime T: type) T {
329349 return switch (self.value) {
330 inline else => |val| @as(T, @intCast(val)),
350 inline else => |val| @intCast(val),
331351 };
332352 }
333353 };
......@@ -387,7 +407,7 @@ pub const Key = union(enum) {
387407 },
388408 inline else => |key| std.hash.autoHash(&hasher, key),
389409 }
390 return @as(u32, @truncate(hasher.final()));
410 return @truncate(hasher.final());
391411 }
392412
393413 fn eql(a: Key, b: Key) bool {
......@@ -419,7 +439,7 @@ pub const Key = union(enum) {
419439
420440 pub fn eql(ctx: @This(), a: Key, b_void: void, b_index: usize) bool {
421441 _ = b_void;
422 return ctx.self.lookup(@as(Ref, @enumFromInt(b_index))).eql(a);
442 return ctx.self.lookup(@enumFromInt(b_index)).eql(a);
423443 }
424444
425445 pub fn hash(ctx: @This(), a: Key) u32 {
......@@ -450,6 +470,7 @@ pub fn deinit(self: *Self, spv: *const Module) void {
450470 self.extra.deinit(spv.gpa);
451471 self.string_bytes.deinit(spv.gpa);
452472 self.strings.deinit(spv.gpa);
473 self.recursive_ptrs.deinit(spv.gpa);
453474}
454475
455476/// Actually materialize the database into spir-v instructions.
......@@ -460,7 +481,7 @@ pub fn materialize(self: *const Self, spv: *Module) !Section {
460481 var section = Section{};
461482 errdefer section.deinit(spv.gpa);
462483 for (self.items.items(.result_id), 0..) |result_id, index| {
463 try self.emit(spv, result_id, @as(Ref, @enumFromInt(index)), &section);
484 try self.emit(spv, result_id, @enumFromInt(index), &section);
464485 }
465486 return section;
466487}
......@@ -538,6 +559,15 @@ fn emit(
538559 });
539560 // TODO: Decorations?
540561 },
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 },
541571 .struct_type => |struct_type| {
542572 try section.emitRaw(spv.gpa, .OpTypeStruct, 1 + struct_type.member_types.len);
543573 section.writeOperand(IdResult, result_id);
......@@ -549,7 +579,7 @@ fn emit(
549579 }
550580 for (struct_type.memberNames(), 0..) |member_name, i| {
551581 if (self.getString(member_name)) |name| {
552 try spv.memberDebugName(result_id, @as(u32, @intCast(i)), name);
582 try spv.memberDebugName(result_id, @intCast(i), name);
553583 }
554584 }
555585 // TODO: Decorations?
......@@ -625,13 +655,12 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {
625655 const adapter: Key.Adapter = .{ .self = self };
626656 const entry = try self.map.getOrPutAdapted(spv.gpa, key, adapter);
627657 if (entry.found_existing) {
628 return @as(Ref, @enumFromInt(entry.index));
658 return @enumFromInt(entry.index);
629659 }
630 const result_id = spv.allocId();
631660 const item: Item = switch (key) {
632661 inline .void_type, .bool_type => .{
633662 .tag = .type_simple,
634 .result_id = result_id,
663 .result_id = spv.allocId(),
635664 .data = @intFromEnum(key.toSimpleType()),
636665 },
637666 .int_type => |int| blk: {
......@@ -641,87 +670,104 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {
641670 };
642671 break :blk .{
643672 .tag = t,
644 .result_id = result_id,
673 .result_id = spv.allocId(),
645674 .data = int.bits,
646675 };
647676 },
648677 .float_type => |float| .{
649678 .tag = .type_float,
650 .result_id = result_id,
679 .result_id = spv.allocId(),
651680 .data = float.bits,
652681 },
653682 .vector_type => |vector| .{
654683 .tag = .type_vector,
655 .result_id = result_id,
684 .result_id = spv.allocId(),
656685 .data = try self.addExtra(spv, vector),
657686 },
658687 .array_type => |array| .{
659688 .tag = .type_array,
660 .result_id = result_id,
689 .result_id = spv.allocId(),
661690 .data = try self.addExtra(spv, array),
662691 },
663692 .function_type => |function| blk: {
664693 const extra = try self.addExtra(spv, Tag.FunctionType{
665 .param_len = @as(u32, @intCast(function.parameters.len)),
694 .param_len = @intCast(function.parameters.len),
666695 .return_type = function.return_type,
667696 });
668 try self.extra.appendSlice(spv.gpa, @as([]const u32, @ptrCast(function.parameters)));
697 try self.extra.appendSlice(spv.gpa, @ptrCast(function.parameters));
669698 break :blk .{
670699 .tag = .type_function,
671 .result_id = result_id,
700 .result_id = spv.allocId(),
672701 .data = extra,
673702 };
674703 },
675 .ptr_type => |ptr| switch (ptr.storage_class) {
676 .Generic => Item{
677 .tag = .type_ptr_generic,
678 .result_id = result_id,
679 .data = @intFromEnum(ptr.child_type),
680 },
681 .CrossWorkgroup => Item{
682 .tag = .type_ptr_crosswgp,
683 .result_id = result_id,
684 .data = @intFromEnum(ptr.child_type),
685 },
686 .Function => Item{
687 .tag = .type_ptr_function,
688 .result_id = result_id,
689 .data = @intFromEnum(ptr.child_type),
690 },
691 else => |storage_class| Item{
692 .tag = .type_ptr_simple,
693 .result_id = result_id,
694 .data = try self.addExtra(spv, Tag.SimplePointerType{
695 .storage_class = storage_class,
696 .child_type = ptr.child_type,
697 }),
698 },
704 // .ptr_type => |ptr| switch (ptr.storage_class) {
705 // .Generic => Item{
706 // .tag = .type_ptr_generic,
707 // .result_id = spv.allocId(),
708 // .data = @intFromEnum(ptr.child_type),
709 // },
710 // .CrossWorkgroup => Item{
711 // .tag = .type_ptr_crosswgp,
712 // .result_id = spv.allocId(),
713 // .data = @intFromEnum(ptr.child_type),
714 // },
715 // .Function => Item{
716 // .tag = .type_ptr_function,
717 // .result_id = spv.allocId(),
718 // .data = @intFromEnum(ptr.child_type),
719 // },
720 // else => |storage_class| Item{
721 // .tag = .type_ptr_simple,
722 // .result_id = spv.allocId(),
723 // .data = try self.addExtra(spv, Tag.SimplePointerType{
724 // .storage_class = storage_class,
725 // .child_type = ptr.child_type,
726 // }),
727 // },
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 }),
699745 },
700746 .struct_type => |struct_type| blk: {
701747 const extra = try self.addExtra(spv, Tag.SimpleStructType{
702748 .name = struct_type.name,
703 .members_len = @as(u32, @intCast(struct_type.member_types.len)),
749 .members_len = @intCast(struct_type.member_types.len),
704750 });
705 try self.extra.appendSlice(spv.gpa, @as([]const u32, @ptrCast(struct_type.member_types)));
751 try self.extra.appendSlice(spv.gpa, @ptrCast(struct_type.member_types));
706752
707753 if (struct_type.member_names) |member_names| {
708 try self.extra.appendSlice(spv.gpa, @as([]const u32, @ptrCast(member_names)));
754 try self.extra.appendSlice(spv.gpa, @ptrCast(member_names));
709755 break :blk Item{
710756 .tag = .type_struct_simple_with_member_names,
711 .result_id = result_id,
757 .result_id = spv.allocId(),
712758 .data = extra,
713759 };
714760 } else {
715761 break :blk Item{
716762 .tag = .type_struct_simple,
717 .result_id = result_id,
763 .result_id = spv.allocId(),
718764 .data = extra,
719765 };
720766 }
721767 },
722768 .opaque_type => |opaque_type| Item{
723769 .tag = .type_opaque,
724 .result_id = result_id,
770 .result_id = spv.allocId(),
725771 .data = @intFromEnum(opaque_type.name),
726772 },
727773 .int => |int| blk: {
......@@ -729,13 +775,13 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {
729775 if (int_type.signedness == .unsigned and int_type.bits == 8) {
730776 break :blk .{
731777 .tag = .uint8,
732 .result_id = result_id,
778 .result_id = spv.allocId(),
733779 .data = int.to(u8),
734780 };
735781 } else if (int_type.signedness == .unsigned and int_type.bits == 32) {
736782 break :blk .{
737783 .tag = .uint32,
738 .result_id = result_id,
784 .result_id = spv.allocId(),
739785 .data = int.to(u32),
740786 };
741787 }
......@@ -745,32 +791,32 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {
745791 if (val >= 0 and val <= std.math.maxInt(u32)) {
746792 break :blk .{
747793 .tag = .uint_small,
748 .result_id = result_id,
794 .result_id = spv.allocId(),
749795 .data = try self.addExtra(spv, Tag.UInt32{
750796 .ty = int.ty,
751 .value = @as(u32, @intCast(val)),
797 .value = @intCast(val),
752798 }),
753799 };
754800 } else if (val >= std.math.minInt(i32) and val <= std.math.maxInt(i32)) {
755801 break :blk .{
756802 .tag = .int_small,
757 .result_id = result_id,
803 .result_id = spv.allocId(),
758804 .data = try self.addExtra(spv, Tag.Int32{
759805 .ty = int.ty,
760 .value = @as(i32, @intCast(val)),
806 .value = @intCast(val),
761807 }),
762808 };
763809 } else if (val < 0) {
764810 break :blk .{
765811 .tag = .int_large,
766 .result_id = result_id,
767 .data = try self.addExtra(spv, Tag.Int64.encode(int.ty, @as(i64, @intCast(val)))),
812 .result_id = spv.allocId(),
813 .data = try self.addExtra(spv, Tag.Int64.encode(int.ty, @intCast(val))),
768814 };
769815 } else {
770816 break :blk .{
771817 .tag = .uint_large,
772 .result_id = result_id,
773 .data = try self.addExtra(spv, Tag.UInt64.encode(int.ty, @as(u64, @intCast(val)))),
818 .result_id = spv.allocId(),
819 .data = try self.addExtra(spv, Tag.UInt64.encode(int.ty, @intCast(val))),
774820 };
775821 }
776822 },
......@@ -779,29 +825,29 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {
779825 .float => |float| switch (self.lookup(float.ty).float_type.bits) {
780826 16 => .{
781827 .tag = .float16,
782 .result_id = result_id,
828 .result_id = spv.allocId(),
783829 .data = @as(u16, @bitCast(float.value.float16)),
784830 },
785831 32 => .{
786832 .tag = .float32,
787 .result_id = result_id,
833 .result_id = spv.allocId(),
788834 .data = @as(u32, @bitCast(float.value.float32)),
789835 },
790836 64 => .{
791837 .tag = .float64,
792 .result_id = result_id,
838 .result_id = spv.allocId(),
793839 .data = try self.addExtra(spv, Tag.Float64.encode(float.value.float64)),
794840 },
795841 else => unreachable,
796842 },
797843 .undef => |undef| .{
798844 .tag = .undef,
799 .result_id = result_id,
845 .result_id = spv.allocId(),
800846 .data = @intFromEnum(undef.ty),
801847 },
802848 .null => |null_info| .{
803849 .tag = .null,
804 .result_id = result_id,
850 .result_id = spv.allocId(),
805851 .data = @intFromEnum(null_info.ty),
806852 },
807853 .bool => |bool_info| .{
......@@ -809,13 +855,13 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {
809855 true => Tag.bool_true,
810856 false => Tag.bool_false,
811857 },
812 .result_id = result_id,
858 .result_id = spv.allocId(),
813859 .data = @intFromEnum(bool_info.ty),
814860 },
815861 };
816862 try self.items.append(spv.gpa, item);
817863
818 return @as(Ref, @enumFromInt(entry.index));
864 return @enumFromInt(entry.index);
819865}
820866
821867/// Turn a Ref back into a Key.
......@@ -830,14 +876,14 @@ pub fn lookup(self: *const Self, ref: Ref) Key {
830876 },
831877 .type_int_signed => .{ .int_type = .{
832878 .signedness = .signed,
833 .bits = @as(u16, @intCast(data)),
879 .bits = @intCast(data),
834880 } },
835881 .type_int_unsigned => .{ .int_type = .{
836882 .signedness = .unsigned,
837 .bits = @as(u16, @intCast(data)),
883 .bits = @intCast(data),
838884 } },
839885 .type_float => .{ .float_type = .{
840 .bits = @as(u16, @intCast(data)),
886 .bits = @intCast(data),
841887 } },
842888 .type_vector => .{ .vector_type = self.extraData(Tag.VectorType, data) },
843889 .type_array => .{ .array_type = self.extraData(Tag.ArrayType, data) },
......@@ -846,40 +892,50 @@ pub fn lookup(self: *const Self, ref: Ref) Key {
846892 return .{
847893 .function_type = .{
848894 .return_type = payload.data.return_type,
849 .parameters = @as([]const Ref, @ptrCast(self.extra.items[payload.trail..][0..payload.data.param_len])),
895 .parameters = @ptrCast(self.extra.items[payload.trail..][0..payload.data.param_len]),
850896 },
851897 };
852898 },
853 .type_ptr_generic => .{
854 .ptr_type = .{
855 .storage_class = .Generic,
856 .child_type = @as(Ref, @enumFromInt(data)),
857 },
858 },
859 .type_ptr_crosswgp => .{
860 .ptr_type = .{
861 .storage_class = .CrossWorkgroup,
862 .child_type = @as(Ref, @enumFromInt(data)),
863 },
864 },
865 .type_ptr_function => .{
866 .ptr_type = .{
867 .storage_class = .Function,
868 .child_type = @as(Ref, @enumFromInt(data)),
869 },
870 },
899 // .type_ptr_generic => .{
900 // .ptr_type = .{
901 // .storage_class = .Generic,
902 // .child_type = @enumFromInt(data),
903 // },
904 // },
905 // .type_ptr_crosswgp => .{
906 // .ptr_type = .{
907 // .storage_class = .CrossWorkgroup,
908 // .child_type = @enumFromInt(data),
909 // },
910 // },
911 // .type_ptr_function => .{
912 // .ptr_type = .{
913 // .storage_class = .Function,
914 // .child_type = @enumFromInt(data),
915 // },
916 // },
871917 .type_ptr_simple => {
872918 const payload = self.extraData(Tag.SimplePointerType, data);
873919 return .{
874920 .ptr_type = .{
875921 .storage_class = payload.storage_class,
876922 .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,
877933 },
878934 };
879935 },
880936 .type_struct_simple => {
881937 const payload = self.extraDataTrail(Tag.SimpleStructType, data);
882 const member_types = @as([]const Ref, @ptrCast(self.extra.items[payload.trail..][0..payload.data.members_len]));
938 const member_types: []const Ref = @ptrCast(self.extra.items[payload.trail..][0..payload.data.members_len]);
883939 return .{
884940 .struct_type = .{
885941 .name = payload.data.name,
......@@ -891,8 +947,8 @@ pub fn lookup(self: *const Self, ref: Ref) Key {
891947 .type_struct_simple_with_member_names => {
892948 const payload = self.extraDataTrail(Tag.SimpleStructType, data);
893949 const trailing = self.extra.items[payload.trail..];
894 const member_types = @as([]const Ref, @ptrCast(trailing[0..payload.data.members_len]));
895 const member_names = @as([]const String, @ptrCast(trailing[payload.data.members_len..][0..payload.data.members_len]));
950 const member_types: []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]);
896952 return .{
897953 .struct_type = .{
898954 .name = payload.data.name,
......@@ -903,16 +959,16 @@ pub fn lookup(self: *const Self, ref: Ref) Key {
903959 },
904960 .type_opaque => .{
905961 .opaque_type = .{
906 .name = @as(String, @enumFromInt(data)),
962 .name = @enumFromInt(data),
907963 },
908964 },
909965 .float16 => .{ .float = .{
910966 .ty = self.get(.{ .float_type = .{ .bits = 16 } }),
911 .value = .{ .float16 = @as(f16, @bitCast(@as(u16, @intCast(data)))) },
967 .value = .{ .float16 = @bitCast(@as(u16, @intCast(data))) },
912968 } },
913969 .float32 => .{ .float = .{
914970 .ty = self.get(.{ .float_type = .{ .bits = 32 } }),
915 .value = .{ .float32 = @as(f32, @bitCast(data)) },
971 .value = .{ .float32 = @bitCast(data) },
916972 } },
917973 .float64 => .{ .float = .{
918974 .ty = self.get(.{ .float_type = .{ .bits = 64 } }),
......@@ -955,17 +1011,17 @@ pub fn lookup(self: *const Self, ref: Ref) Key {
9551011 } };
9561012 },
9571013 .undef => .{ .undef = .{
958 .ty = @as(Ref, @enumFromInt(data)),
1014 .ty = @enumFromInt(data),
9591015 } },
9601016 .null => .{ .null = .{
961 .ty = @as(Ref, @enumFromInt(data)),
1017 .ty = @enumFromInt(data),
9621018 } },
9631019 .bool_true => .{ .bool = .{
964 .ty = @as(Ref, @enumFromInt(data)),
1020 .ty = @enumFromInt(data),
9651021 .value = true,
9661022 } },
9671023 .bool_false => .{ .bool = .{
968 .ty = @as(Ref, @enumFromInt(data)),
1024 .ty = @enumFromInt(data),
9691025 .value = false,
9701026 } },
9711027 };
......@@ -981,7 +1037,7 @@ pub fn resultId(self: Self, ref: Ref) IdResult {
9811037fn get(self: *const Self, key: Key) Ref {
9821038 const adapter: Key.Adapter = .{ .self = self };
9831039 const index = self.map.getIndexAdapted(key, adapter).?;
984 return @as(Ref, @enumFromInt(index));
1040 return @enumFromInt(index);
9851041}
9861042
9871043fn addExtra(self: *Self, spv: *Module, extra: anytype) !u32 {
......@@ -991,15 +1047,16 @@ fn addExtra(self: *Self, spv: *Module, extra: anytype) !u32 {
9911047}
9921048
9931049fn addExtraAssumeCapacity(self: *Self, extra: anytype) !u32 {
994 const payload_offset = @as(u32, @intCast(self.extra.items.len));
1050 const payload_offset: u32 = @intCast(self.extra.items.len);
9951051 inline for (@typeInfo(@TypeOf(extra)).Struct.fields) |field| {
9961052 const field_val = @field(extra, field.name);
997 const word = switch (field.type) {
1053 const word: u32 = switch (field.type) {
9981054 u32 => field_val,
999 i32 => @as(u32, @bitCast(field_val)),
1055 i32 => @bitCast(field_val),
10001056 Ref => @intFromEnum(field_val),
10011057 StorageClass => @intFromEnum(field_val),
10021058 String => @intFromEnum(field_val),
1059 InternPool.Index => @intFromEnum(field_val),
10031060 else => @compileError("Invalid type: " ++ @typeName(field.type)),
10041061 };
10051062 self.extra.appendAssumeCapacity(word);
......@@ -1018,10 +1075,11 @@ fn extraDataTrail(self: Self, comptime T: type, offset: u32) struct { data: T, t
10181075 const word = self.extra.items[offset + i];
10191076 @field(result, field.name) = switch (field.type) {
10201077 u32 => word,
1021 i32 => @as(i32, @bitCast(word)),
1022 Ref => @as(Ref, @enumFromInt(word)),
1023 StorageClass => @as(StorageClass, @enumFromInt(word)),
1024 String => @as(String, @enumFromInt(word)),
1078 i32 => @bitCast(word),
1079 Ref => @enumFromInt(word),
1080 StorageClass => @enumFromInt(word),
1081 String => @enumFromInt(word),
1082 InternPool.Index => @enumFromInt(word),
10251083 else => @compileError("Invalid type: " ++ @typeName(field.type)),
10261084 };
10271085 }
......@@ -1049,7 +1107,7 @@ pub const String = enum(u32) {
10491107 _ = ctx;
10501108 var hasher = std.hash.Wyhash.init(0);
10511109 hasher.update(a);
1052 return @as(u32, @truncate(hasher.final()));
1110 return @truncate(hasher.final());
10531111 }
10541112 };
10551113};
......@@ -1064,10 +1122,10 @@ pub fn addString(self: *Self, spv: *Module, str: []const u8) !String {
10641122 try self.string_bytes.ensureUnusedCapacity(spv.gpa, 1 + str.len);
10651123 self.string_bytes.appendSliceAssumeCapacity(str);
10661124 self.string_bytes.appendAssumeCapacity(0);
1067 entry.value_ptr.* = @as(u32, @intCast(offset));
1125 entry.value_ptr.* = @intCast(offset);
10681126 }
10691127
1070 return @as(String, @enumFromInt(entry.index));
1128 return @enumFromInt(entry.index);
10711129}
10721130
10731131pub fn getString(self: *const Self, ref: String) ?[]const u8 {
src/codegen/spirv/Module.zig-11
......@@ -507,17 +507,6 @@ pub fn arrayType(self: *Module, len: u32, elem_ty_ref: CacheRef) !CacheRef {
507507 } });
508508}
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
521510pub fn constInt(self: *Module, ty_ref: CacheRef, value: anytype) !IdRef {
522511 const ty = self.cache.lookup(ty_ref).int_type;
523512 const Value = Cache.Key.Int.Value;
test/behavior/bugs/12000.zig-1
......@@ -9,7 +9,6 @@ test {
99 if (builtin.zig_backend == .stage2_x86) return error.SkipZigTest; // TODO
1010 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1111 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
12 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1312
1413 var t: T = .{ .next = null };
1514 try std.testing.expect(t.next == null);
test/behavior/bugs/1735.zig-1
......@@ -44,7 +44,6 @@ const a = struct {
4444test "initialization" {
4545 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
4646 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
47 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
4847
4948 var t = a.init();
5049 try std.testing.expect(t.foo.len == 0);
test/behavior/bugs/1914.zig-4
......@@ -12,8 +12,6 @@ const b_list: []B = &[_]B{};
1212const a = A{ .b_list_pointer = &b_list };
1313
1414test "segfault bug" {
15 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
16
1715 const assert = std.debug.assert;
1816 const obj = B{ .a_pointer = &a };
1917 assert(obj.a_pointer == &a); // this makes zig crash
......@@ -30,7 +28,5 @@ pub const B2 = struct {
3028var b_value = B2{ .pointer_array = &[_]*A2{} };
3129
3230test "basic stuff" {
33 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
34
3531 std.debug.assert(&b_value == &b_value);
3632}
test/behavior/bugs/2006.zig-1
......@@ -7,7 +7,6 @@ const S = struct {
77};
88test "bug 2006" {
99 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
10 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1110
1211 var a: S = undefined;
1312 a = S{ .p = undefined };
test/behavior/bugs/3007.zig-1
......@@ -22,7 +22,6 @@ test "fixed" {
2222 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
2323 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
2424 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
25 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
2625
2726 default_foo = get_foo() catch null; // This Line
2827 try std.testing.expect(!default_foo.?.free);
test/behavior/bugs/6947.zig-1
......@@ -8,7 +8,6 @@ test {
88 if (builtin.zig_backend == .stage2_x86) return error.SkipZigTest; // TODO
99 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1010 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
11 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1211
1312 var slice: []void = undefined;
1413 destroy(&slice[0]);
test/behavior/bugs/7325.zig-1
......@@ -81,7 +81,6 @@ test {
8181 if (builtin.zig_backend == .stage2_x86) return error.SkipZigTest; // TODO
8282 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
8383 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
84 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
8584
8685 var param: ParamType = .{
8786 .one_of = .{ .name = "name" },
test/behavior/error.zig-1
......@@ -943,7 +943,6 @@ test "returning an error union containing a type with no runtime bits" {
943943test "try used in recursive function with inferred error set" {
944944 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
945945 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
946 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
947946
948947 const Value = union(enum) {
949948 values: []const @This(),
test/behavior/eval.zig-4
......@@ -391,7 +391,6 @@ test "return 0 from function that has u0 return type" {
391391test "statically initialized struct" {
392392 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
393393 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
394 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
395394
396395 st_init_str_foo.x += 1;
397396 try expect(st_init_str_foo.x == 14);
......@@ -498,7 +497,6 @@ test "comptime shlWithOverflow" {
498497test "const ptr to variable data changes at runtime" {
499498 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
500499 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
501 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
502500
503501 try expect(foo_ref.name[0] == 'a');
504502 foo_ref.name = "b";
......@@ -1551,8 +1549,6 @@ test "comptime function turns function value to function pointer" {
15511549}
15521550
15531551test "container level const and var have unique addresses" {
1554 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1555
15561552 const S = struct {
15571553 x: i32,
15581554 y: i32,
test/behavior/generics.zig-1
......@@ -205,7 +205,6 @@ fn foo2(arg: anytype) bool {
205205
206206test "generic struct" {
207207 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
208 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
209208
210209 var a1 = GenNode(i32){
211210 .value = 13,
test/behavior/null.zig-1
......@@ -185,7 +185,6 @@ test "unwrap optional which is field of global var" {
185185 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
186186 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
187187 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
188 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
189188
190189 struct_with_optional.field = null;
191190 if (struct_with_optional.field) |payload| {
test/behavior/optional.zig-1
......@@ -193,7 +193,6 @@ test "nested orelse" {
193193test "self-referential struct through a slice of optional" {
194194 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
195195 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
196 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
197196
198197 const S = struct {
199198 const Node = struct {
test/behavior/ptrcast.zig-2
......@@ -130,7 +130,6 @@ test "lower reinterpreted comptime field ptr (with under-aligned fields)" {
130130 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
131131 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
132132 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
133 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
134133
135134 // Test lowering a field ptr
136135 comptime var bytes align(2) = [_]u8{ 1, 2, 3, 4, 5, 6 };
......@@ -153,7 +152,6 @@ test "lower reinterpreted comptime field ptr" {
153152 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
154153 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
155154 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
156 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
157155
158156 // Test lowering a field ptr
159157 comptime var bytes align(4) = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
test/behavior/struct.zig-6
......@@ -292,7 +292,6 @@ const Val = struct {
292292test "struct point to self" {
293293 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
294294 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
295 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
296295
297296 var root: Node = undefined;
298297 root.val.x = 1;
......@@ -347,7 +346,6 @@ test "self-referencing struct via array member" {
347346 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
348347 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
349348 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
350 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
351349
352350 const T = struct {
353351 children: [1]*@This(),
......@@ -370,7 +368,6 @@ const EmptyStruct = struct {
370368
371369test "align 1 field before self referential align 8 field as slice return type" {
372370 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
373 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
374371
375372 const result = alloc(Expr);
376373 try expect(result.len == 0);
......@@ -736,7 +733,6 @@ test "packed struct with u0 field access" {
736733test "access to global struct fields" {
737734 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
738735 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
739 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
740736
741737 g_foo.bar.value = 42;
742738 try expect(g_foo.bar.value == 42);
......@@ -1423,7 +1419,6 @@ test "fieldParentPtr of a zero-bit field" {
14231419
14241420test "struct field has a pointer to an aligned version of itself" {
14251421 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1426 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
14271422
14281423 const E = struct {
14291424 next: *align(1) @This(),
......@@ -1519,7 +1514,6 @@ test "function pointer in struct returns the struct" {
15191514
15201515test "no dependency loop on optional field wrapped in generic function" {
15211516 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
1522 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
15231517
15241518 const S = struct {
15251519 fn Atomic(comptime T: type) type {
test/behavior/struct_contains_null_ptr_itself.zig-1
......@@ -5,7 +5,6 @@ const builtin = @import("builtin");
55test "struct contains null pointer which contains original struct" {
66 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
77 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
8 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
98
109 var x: ?*NodeLineComment = null;
1110 try expect(x == null);
test/behavior/struct_contains_slice_of_itself.zig-2
......@@ -13,7 +13,6 @@ const NodeAligned = struct {
1313
1414test "struct contains slice of itself" {
1515 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
16 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1716
1817 var other_nodes = [_]Node{
1918 Node{
......@@ -54,7 +53,6 @@ test "struct contains slice of itself" {
5453test "struct contains aligned slice of itself" {
5554 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
5655 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
57 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
5856
5957 var other_nodes = [_]NodeAligned{
6058 NodeAligned{
test/behavior/union.zig-10
......@@ -399,7 +399,6 @@ test "tagged union with no payloads" {
399399 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
400400 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
401401 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
402 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
403402
404403 const a = UnionEnumNoPayloads{ .B = {} };
405404 switch (a) {
......@@ -474,7 +473,6 @@ test "update the tag value for zero-sized unions" {
474473 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
475474 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
476475 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
477 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
478476
479477 const S = union(enum) {
480478 U0: void,
......@@ -515,7 +513,6 @@ test "method call on an empty union" {
515513 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
516514 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
517515 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
518 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
519516
520517 const S = struct {
521518 const MyUnion = union(MyUnionTag) {
......@@ -593,7 +590,6 @@ test "tagged union with all void fields but a meaningful tag" {
593590 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
594591 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
595592 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
596 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
597593
598594 const S = struct {
599595 const B = union(enum) {
......@@ -795,7 +791,6 @@ test "@unionInit stored to a const" {
795791 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
796792 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
797793 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
798 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
799794
800795 const S = struct {
801796 const U = union(enum) {
......@@ -867,7 +862,6 @@ test "union no tag with struct member" {
867862 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
868863 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
869864 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
870 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
871865
872866 const Struct = struct {};
873867 const Union = union {
......@@ -1079,7 +1073,6 @@ test "@unionInit on union with tag but no fields" {
10791073 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
10801074 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
10811075 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
1082 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
10831076
10841077 const S = struct {
10851078 const Type = enum(u8) { no_op = 105 };
......@@ -1128,7 +1121,6 @@ test "global variable struct contains union initialized to non-most-aligned fiel
11281121 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
11291122 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
11301123 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
1131 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
11321124
11331125 const T = struct {
11341126 const U = union(enum) {
......@@ -1348,7 +1340,6 @@ test "union field ptr - zero sized payload" {
13481340 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
13491341 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
13501342 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
1351 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
13521343
13531344 const U = union {
13541345 foo: void,
......@@ -1363,7 +1354,6 @@ test "union field ptr - zero sized field" {
13631354 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
13641355 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
13651356 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
1366 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
13671357
13681358 const U = union {
13691359 foo: void,