authorgravatar for alichraghi@proton.meAli Chraghi <alichraghi@proton.me> 2023-06-23 00:50:15+03:30
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-23 23:52:45-07:00
logff0a88b133b9c4f27528f39d05ff65a977756bee
tree43eb0435ffdda5d31a04838ac2ad82a2c6206d6a
parent1cf06706be7408a84c7b2ef0530c7a6af51ee41a

spirv: fix a few conflicts caused by intern-pool


1 files changed, 46 insertions(+), 32 deletions(-)

src/codegen/spirv.zig+46-32
......@@ -755,7 +755,7 @@ pub const DeclGen = struct {
755755 switch (aggregate.storage) {
756756 .bytes => |bytes| try self.addBytes(bytes),
757757 .elems, .repeated_elem => {
758 for (0..array_type.len) |i| {
758 for (0..@intCast(usize, array_type.len)) |i| {
759759 try self.lower(elem_ty, switch (aggregate.storage) {
760760 .bytes => unreachable,
761761 .elems => |elem_vals| elem_vals[@intCast(usize, i)].toValue(),
......@@ -771,16 +771,23 @@ pub const DeclGen = struct {
771771 .vector_type => return dg.todo("indirect constant of type {}", .{ty.fmt(mod)}),
772772 .struct_type => {
773773 const struct_ty = mod.typeToStruct(ty).?;
774
775774 if (struct_ty.layout == .Packed) {
776775 return dg.todo("packed struct constants", .{});
777776 }
778777
779778 const struct_begin = self.size;
780 const field_vals = val.castTag(.aggregate).?.data;
781779 for (struct_ty.fields.values(), 0..) |field, i| {
782780 if (field.is_comptime or !field.ty.hasRuntimeBits(mod)) continue;
783 try self.lower(field.ty, field_vals[i]);
781
782 const field_val = switch (aggregate.storage) {
783 .bytes => |bytes| try mod.intern_pool.get(mod.gpa, .{ .int = .{
784 .ty = field.ty.toIntern(),
785 .storage = .{ .u64 = bytes[i] },
786 } }),
787 .elems => |elems| elems[i],
788 .repeated_elem => |elem| elem,
789 };
790 try self.lower(field.ty, field_val.toValue());
784791
785792 // Add padding if required.
786793 // TODO: Add to type generation as well?
......@@ -974,6 +981,7 @@ pub const DeclGen = struct {
974981 /// This function should only be called during function code generation.
975982 fn constant(self: *DeclGen, ty: Type, val: Value, repr: Repr) !IdRef {
976983 const mod = self.module;
984 const target = self.getTarget();
977985 const result_ty_ref = try self.resolveType(ty, repr);
978986
979987 log.debug("constant: ty = {}, val = {}", .{ ty.fmt(self.module), val.fmtValue(ty, self.module) });
......@@ -990,9 +998,19 @@ pub const DeclGen = struct {
990998 return try self.spv.constInt(result_ty_ref, val.toUnsignedInt(mod));
991999 }
9921000 },
993 .Bool => {
994 @compileError("TODO merge conflict failure");
1001 .Bool => switch (repr) {
1002 .direct => return try self.spv.constBool(result_ty_ref, val.toBool()),
1003 .indirect => return try self.spv.constInt(result_ty_ref, @intFromBool(val.toBool())),
1004 },
1005 .Float => return switch (ty.floatBits(target)) {
1006 16 => try self.spv.resolveId(.{ .float = .{ .ty = result_ty_ref, .value = .{ .float16 = val.toFloat(f16, mod) } } }),
1007 32 => try self.spv.resolveId(.{ .float = .{ .ty = result_ty_ref, .value = .{ .float32 = val.toFloat(f32, mod) } } }),
1008 64 => try self.spv.resolveId(.{ .float = .{ .ty = result_ty_ref, .value = .{ .float64 = val.toFloat(f64, mod) } } }),
1009 80, 128 => unreachable, // TODO
1010 else => unreachable,
9951011 },
1012 .ErrorSet => @panic("TODO"),
1013 .ErrorUnion => @panic("TODO"),
9961014 // TODO: We can handle most pointers here (decl refs etc), because now they emit an extra
9971015 // OpVariable that is not really required.
9981016 else => {
......@@ -1189,12 +1207,12 @@ pub const DeclGen = struct {
11891207 if (fn_info.is_var_args)
11901208 return self.fail("VarArgs functions are unsupported for SPIR-V", .{});
11911209
1192 const param_ty_refs = try self.gpa.alloc(CacheRef, ty.fnParamLen());
1210 const param_ty_refs = try self.gpa.alloc(CacheRef, fn_info.param_types.len);
11931211 defer self.gpa.free(param_ty_refs);
11941212 for (param_ty_refs, 0..) |*param_type, i| {
1195 param_type.* = try self.resolveType(ty.fnParamType(i), .direct);
1213 param_type.* = try self.resolveType(fn_info.param_types[i].toType(), .direct);
11961214 }
1197 const return_ty_ref = try self.resolveType(ty.fnReturnType(), .direct);
1215 const return_ty_ref = try self.resolveType(fn_info.return_type.toType(), .direct);
11981216
11991217 return try self.spv.resolve(.{ .function_type = .{
12001218 .return_type = return_ty_ref,
......@@ -1245,17 +1263,18 @@ pub const DeclGen = struct {
12451263 } });
12461264 },
12471265 .Struct => {
1248 if (ty.isSimpleTupleOrAnonStruct()) {
1249 const tuple = ty.tupleFields();
1250 const member_types = try self.gpa.alloc(CacheRef, tuple.types.len);
1266 const struct_ty = mod.typeToStruct(ty).?;
1267 const fields = struct_ty.fields.values();
1268
1269 if (ty.isSimpleTupleOrAnonStruct(mod)) {
1270 const member_types = try self.gpa.alloc(CacheRef, fields.len);
12511271 defer self.gpa.free(member_types);
12521272
12531273 var member_index: usize = 0;
1254 for (tuple.types, 0..) |field_ty, i| {
1255 const field_val = tuple.values[i];
1256 if (field_val.ip_index != .unreachable_value or !field_ty.hasRuntimeBits(mod)) continue;
1274 for (fields) |field| {
1275 if (field.ty.ip_index != .unreachable_value or !field.ty.hasRuntimeBits(mod)) continue;
12571276
1258 member_types[member_index] = try self.resolveType(field_ty, .indirect);
1277 member_types[member_index] = try self.resolveType(field.ty, .indirect);
12591278 member_index += 1;
12601279 }
12611280
......@@ -1264,29 +1283,26 @@ pub const DeclGen = struct {
12641283 } });
12651284 }
12661285
1267 const struct_ty = mod.typeToStruct(ty).?;
1268
12691286 if (struct_ty.layout == .Packed) {
12701287 return try self.resolveType(struct_ty.backing_int_ty, .direct);
12711288 }
12721289
1273 const member_types = try self.gpa.alloc(CacheRef, struct_ty.fields.count());
1290 const member_types = try self.gpa.alloc(CacheRef, fields.len);
12741291 defer self.gpa.free(member_types);
12751292
1276 const member_names = try self.gpa.alloc(CacheString, struct_ty.fields.count());
1293 const member_names = try self.gpa.alloc(CacheString, fields.len);
12771294 defer self.gpa.free(member_names);
12781295
12791296 var member_index: usize = 0;
1280 const struct_obj = void; // TODO
1281 for (struct_obj.fields.values(), 0..) |field, i| {
1297 for (fields, 0..) |field, i| {
12821298 if (field.is_comptime or !field.ty.hasRuntimeBits(mod)) continue;
12831299
12841300 member_types[member_index] = try self.resolveType(field.ty, .indirect);
1285 member_names[member_index] = try self.spv.resolveString(struct_ty.fields.keys()[i]);
1301 member_names[member_index] = try self.spv.resolveString(mod.intern_pool.stringToSlice(struct_ty.fields.keys()[i]));
12861302 member_index += 1;
12871303 }
12881304
1289 const name = mod.intern_pool.stringToSlice(try struct_obj.getFullyQualifiedName(self.module));
1305 const name = mod.intern_pool.stringToSlice(try struct_ty.getFullyQualifiedName(self.module));
12901306
12911307 return try self.spv.resolve(.{ .struct_type = .{
12921308 .name = try self.spv.resolveString(name),
......@@ -1491,7 +1507,6 @@ pub const DeclGen = struct {
14911507 }
14921508
14931509 fn genDecl(self: *DeclGen) !void {
1494 if (true) @panic("TODO: update SPIR-V backend for InternPool changes");
14951510 const mod = self.module;
14961511 const decl = mod.declPtr(self.decl_index);
14971512 const spv_decl_index = try self.resolveDecl(self.decl_index);
......@@ -1947,7 +1962,7 @@ pub const DeclGen = struct {
19471962
19481963 const bool_ty_ref = try self.resolveType(Type.bool, .direct);
19491964
1950 const ov_ty = result_ty.tupleFields().types[1];
1965 const ov_ty = result_ty.structFieldType(1, self.module);
19511966 // Note: result is stored in a struct, so indirect representation.
19521967 const ov_ty_ref = try self.resolveType(ov_ty, .indirect);
19531968
......@@ -2160,7 +2175,7 @@ pub const DeclGen = struct {
21602175 const opcode: Opcode = opcode: {
21612176 const op_ty = switch (ty.zigTypeTag(mod)) {
21622177 .Int, .Bool, .Float => ty,
2163 .Enum => ty.intTagType(),
2178 .Enum => ty.intTagType(mod),
21642179 .ErrorSet => Type.u16,
21652180 .Pointer => blk: {
21662181 // Note that while SPIR-V offers OpPtrEqual and OpPtrNotEqual, they are
......@@ -2443,8 +2458,7 @@ pub const DeclGen = struct {
24432458 const slice_id = try self.resolve(bin_op.lhs);
24442459 const index_id = try self.resolve(bin_op.rhs);
24452460
2446 var slice_buf: Type.SlicePtrFieldTypeBuffer = undefined;
2447 const ptr_ty = slice_ty.slicePtrFieldType(&slice_buf, mod);
2461 const ptr_ty = slice_ty.slicePtrFieldType(mod);
24482462 const ptr_ty_ref = try self.resolveType(ptr_ty, .direct);
24492463
24502464 const slice_ptr = try self.extractField(ptr_ty, slice_id, 0);
......@@ -2497,8 +2511,8 @@ pub const DeclGen = struct {
24972511 // If we pass ptr_ty directly, it will attempt to load the entire array rather than
24982512 // just an element.
24992513 var elem_ptr_info = ptr_ty.ptrInfo(mod);
2500 elem_ptr_info.size = .One;
2501 const elem_ptr_ty = try Type.ptr(undefined, mod, elem_ptr_info);
2514 elem_ptr_info.flags.size = .One;
2515 const elem_ptr_ty = elem_ptr_info.child.toType();
25022516
25032517 return try self.load(elem_ptr_ty, elem_ptr_id);
25042518 }
......@@ -2514,7 +2528,7 @@ pub const DeclGen = struct {
25142528 const union_handle = try self.resolve(ty_op.operand);
25152529 if (layout.payload_size == 0) return union_handle;
25162530
2517 const tag_ty = un_ty.unionTagTypeSafety().?;
2531 const tag_ty = un_ty.unionTagTypeSafety(mod).?;
25182532 const tag_index = @intFromBool(layout.tag_align < layout.payload_align);
25192533 return try self.extractField(tag_ty, union_handle, tag_index);
25202534 }