authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-05-29 19:25:48+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-05-30 19:43:37+02:00
loga72179fed0f20619f6787c760fae00ece16e9d20
treee4f24ef094feecd5a44f7baae72831982e6017dd
parent05f1392d8bd1e5dc2e05bf21ba64111b02fdc51e
signaturelock-open Commit is signed but in an unrecognized format.

spirv: translate structs to cache key


2 files changed, 69 insertions(+), 3 deletions(-)

src/codegen/spirv/Module.zig+10
......@@ -918,3 +918,13 @@ pub fn debugName(self: *Module, target: IdResult, comptime fmt: []const u8, args
918918 .name = name,
919919 });
920920}
921
922pub fn memberDebugName(self: *Module, target: IdResult, member: u32, comptime fmt: []const u8, args: anytype) !void {
923 const name = try std.fmt.allocPrint(self.gpa, fmt, args);
924 defer self.gpa.free(name);
925 try self.sections.debug_names.emit(self.gpa, .OpMemberName, .{
926 .type = target,
927 .member = member,
928 .name = name,
929 });
930}
src/codegen/spirv/TypeConstantCache.zig+59-3
......@@ -11,6 +11,7 @@
1111//! vectors) must have a _unique_ representation in the final binary.
1212
1313const std = @import("std");
14const assert = std.debug.assert;
1415const Allocator = std.mem.Allocator;
1516
1617const Section = @import("Section.zig");
......@@ -68,8 +69,11 @@ const Tag = enum {
6869 /// data is child type
6970 type_ptr_function,
7071 /// Simple pointer type that does not have any decorations.
71 /// data is SimplePointerType
72 /// data is payload to SimplePointerType
7273 type_ptr_simple,
74 /// Simple structure type that does not have any decorations.
75 /// data is payload to SimpleStructType
76 type_struct_simple,
7377
7478 // -- Values
7579 /// Value of type u8
......@@ -107,7 +111,7 @@ const Tag = enum {
107111 const ArrayType = Key.ArrayType;
108112
109113 // Trailing:
110 // - [param_len]Ref: parameter types
114 // - [param_len]Ref: parameter types.
111115 const FunctionType = struct {
112116 param_len: u32,
113117 return_type: Ref,
......@@ -118,6 +122,13 @@ const Tag = enum {
118122 child_type: Ref,
119123 };
120124
125 /// Trailing:
126 /// - [members_len]Ref: Member types.
127 const SimpleStructType = struct {
128 /// Number of members that this struct has.
129 members_len: u32,
130 };
131
121132 const Float64 = struct {
122133 // Low-order 32 bits of the value.
123134 low: u32,
......@@ -201,6 +212,7 @@ pub const Key = union(enum) {
201212 array_type: ArrayType,
202213 function_type: FunctionType,
203214 ptr_type: PointerType,
215 struct_type: StructType,
204216
205217 // -- values
206218 int: Int,
......@@ -238,6 +250,12 @@ pub const Key = union(enum) {
238250 // - MaxByteOffset,
239251 };
240252
253 pub const StructType = struct {
254 // TODO: Decorations.
255 /// The type of each member.
256 member_types: []const Ref,
257 };
258
241259 pub const Int = struct {
242260 /// The type: any bitness integer.
243261 ty: Ref,
......@@ -304,6 +322,11 @@ pub const Key = union(enum) {
304322 std.hash.autoHash(&hasher, param_type);
305323 }
306324 },
325 .struct_type => |struct_type| {
326 for (struct_type.member_types) |member_type| {
327 std.hash.autoHash(&hasher, member_type);
328 }
329 },
307330 inline else => |key| std.hash.autoHash(&hasher, key),
308331 }
309332 return @truncate(u32, hasher.final());
......@@ -318,10 +341,14 @@ pub const Key = union(enum) {
318341 }
319342 return switch (a) {
320343 .function_type => |a_func| {
321 const b_func = a.function_type;
344 const b_func = b.function_type;
322345 return a_func.return_type == b_func.return_type and
323346 std.mem.eql(Ref, a_func.parameters, b_func.parameters);
324347 },
348 .struct_type => |a_struct| {
349 const b_struct = b.struct_type;
350 return std.mem.eql(Ref, a_struct.member_types, b_struct.member_types);
351 },
325352 // TODO: Unroll?
326353 else => std.meta.eql(a, b),
327354 };
......@@ -442,6 +469,14 @@ fn emit(
442469 });
443470 // TODO: Decorations?
444471 },
472 .struct_type => |struct_type| {
473 try section.emitRaw(spv.gpa, .OpTypeStruct, 1 + struct_type.member_types.len);
474 section.writeOperand(IdResult, result_id);
475 for (struct_type.member_types) |member_type| {
476 section.writeOperand(IdResult, self.resultId(member_type));
477 }
478 // TODO: Decorations?
479 },
445480 .int => |int| {
446481 const int_type = self.lookup(int.ty).int_type;
447482 const ty_id = self.resultId(int.ty);
......@@ -552,6 +587,18 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {
552587 }),
553588 },
554589 },
590 .struct_type => |struct_type| blk: {
591 const extra = try self.addExtra(spv, Tag.SimpleStructType{
592 .members_len = @intCast(u32, struct_type.member_types.len),
593 });
594 try self.extra.appendSlice(spv.gpa, @ptrCast([]const u32, struct_type.member_types));
595
596 break :blk Item{
597 .tag = .type_struct_simple,
598 .result_id = result_id,
599 .data = extra,
600 };
601 },
555602 .int => |int| blk: {
556603 const int_type = self.lookup(int.ty).int_type;
557604 if (int_type.signedness == .unsigned and int_type.bits == 8) {
......@@ -687,6 +734,15 @@ pub fn lookup(self: *const Self, ref: Ref) Key {
687734 },
688735 };
689736 },
737 .type_struct_simple => {
738 const payload = self.extraDataTrail(Tag.SimpleStructType, data);
739 const member_types = @ptrCast([]const Ref, self.extra.items[payload.trail..][0..payload.data.members_len]);
740 return .{
741 .struct_type = .{
742 .member_types = member_types,
743 },
744 };
745 },
690746 .float16 => .{ .float = .{
691747 .ty = self.get(.{ .float_type = .{ .bits = 16 } }),
692748 .value = .{ .float16 = @bitCast(f16, @intCast(u16, data)) },