authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-10-21 17:26:59+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-10-21 17:46:54+02:00
log6281ad91dfc0d799bfabced68009dfb4971545d7
tree51720234f60229b009045489366fdf41961753b1
parent6e955af8c84e1f9f75fef7f1a5820ab1b5bcff94
signaturebadge-check Signed by SSH key SHA256:CQ99aPxq+RueiL9u7z0FEki5Fm7V6T8q4PrEGmINrA4

spirv: self-referential pointers via new fwd_ptr_type

Its a little ugly but it works.

20 files changed, 226 insertions(+), 174 deletions(-)

src/codegen/spirv.zig+34-7
......@@ -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);
......@@ -1100,9 +1105,30 @@ const DeclGen = struct {
11001105 }
11011106
11021107 fn ptrType(self: *DeclGen, child_ty: Type, storage_class: StorageClass) !CacheRef {
1103 // TODO: This function will be rewritten so that forward declarations work properly
1108 const key = .{ child_ty.toIntern(), storage_class };
1109 const entry = try self.wip_pointers.getOrPut(self.gpa, key);
1110 if (entry.found_existing) {
1111 const fwd_ref = entry.value_ptr.*;
1112 try self.spv.cache.recursive_ptrs.put(self.spv.gpa, fwd_ref, {});
1113 return fwd_ref;
1114 }
1115
1116 const fwd_ref = try self.spv.resolve(.{ .fwd_ptr_type = .{
1117 .zig_child_type = child_ty.toIntern(),
1118 .storage_class = storage_class,
1119 } });
1120 entry.value_ptr.* = fwd_ref;
1121
11041122 const child_ty_ref = try self.resolveType(child_ty, .indirect);
1105 return try self.spv.ptrType(child_ty_ref, storage_class);
1123 _ = try self.spv.resolve(.{ .ptr_type = .{
1124 .storage_class = storage_class,
1125 .child_type = child_ty_ref,
1126 .fwd = fwd_ref,
1127 } });
1128
1129 assert(self.wip_pointers.remove(key));
1130
1131 return fwd_ref;
11061132 }
11071133
11081134 /// Generate a union type. Union types are always generated with the
......@@ -1323,12 +1349,12 @@ const DeclGen = struct {
13231349 .Pointer => {
13241350 const ptr_info = ty.ptrInfo(mod);
13251351
1352 // Note: Don't cache this pointer type, it would mess up the recursive pointer functionality
1353 // in ptrType()!
1354
13261355 const storage_class = spvStorageClass(ptr_info.flags.address_space);
1327 const child_ty_ref = try self.resolveType(ptr_info.child.toType(), .indirect);
1328 const ptr_ty_ref = try self.spv.resolve(.{ .ptr_type = .{
1329 .storage_class = storage_class,
1330 .child_type = child_ty_ref,
1331 } });
1356 const ptr_ty_ref = try self.ptrType(ptr_info.child.toType(), storage_class);
1357
13321358 if (ptr_info.flags.size != .Slice) {
13331359 return ptr_ty_ref;
13341360 }
......@@ -4371,6 +4397,7 @@ const DeclGen = struct {
43714397 }
43724398
43734399 // TODO: Multiple results
4400 // TODO: Check that the output type from assembly is the same as the type actually expected by Zig.
43744401 }
43754402
43764403 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-5
......@@ -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);
......@@ -1422,7 +1419,6 @@ test "fieldParentPtr of a zero-bit field" {
14221419
14231420test "struct field has a pointer to an aligned version of itself" {
14241421 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1425 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
14261422
14271423 const E = struct {
14281424 next: *align(1) @This(),
......@@ -1518,7 +1514,6 @@ test "function pointer in struct returns the struct" {
15181514
15191515test "no dependency loop on optional field wrapped in generic function" {
15201516 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
1521 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
15221517
15231518 const S = struct {
15241519 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{