authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-09-17 13:24:29+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-09-23 12:36:44-07:00
logf16d1603ab199b6e605c5e83f2edaebcf89d5512
treed398a622a3b63befba2b4dc7817c97fdb07d3f8b
parent42226fc1b77ba042e72fcdfb1f3c1973650a7cc3

spirv: fix type_map use-after-realloc issues


1 files changed, 19 insertions(+), 36 deletions(-)

src/codegen/spirv.zig+19-36
......@@ -947,9 +947,10 @@ pub const DeclGen = struct {
947947 return try self.resolveType(union_obj.enum_tag_ty.toType(), .indirect);
948948 }
949949
950 // TODO: We need to add the active field to the key.
951 // const entry = try self.type_map.getOrPut(self.gpa, ty.toIntern());
952 // if (entry.found_existing) return entry.value_ptr.ty_ref;
950 // TODO: We need to add the active field to the key, somehow.
951 if (maybe_active_field == null) {
952 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
953 }
953954
954955 var member_types: [4]CacheRef = undefined;
955956 var member_names: [4]CacheString = undefined;
......@@ -986,10 +987,9 @@ pub const DeclGen = struct {
986987 .member_names = member_names[0..layout.total_fields],
987988 } });
988989
989 // entry.value_ptr.* = .{
990 // .ty_ref = ty_ref,
991 // };
992
990 if (maybe_active_field == null) {
991 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
992 }
993993 return ty_ref;
994994 }
995995
......@@ -1033,8 +1033,7 @@ pub const DeclGen = struct {
10331033 return try self.spv.resolve(.{ .float_type = .{ .bits = bits } });
10341034 },
10351035 .Array => {
1036 const entry = try self.type_map.getOrPut(self.gpa, ty.toIntern());
1037 if (entry.found_existing) return entry.value_ptr.ty_ref;
1036 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
10381037
10391038 const elem_ty = ty.childType(mod);
10401039 const elem_ty_ref = try self.resolveType(elem_ty, .indirect);
......@@ -1042,15 +1041,12 @@ pub const DeclGen = struct {
10421041 return self.fail("array type of {} elements is too large", .{ty.arrayLenIncludingSentinel(mod)});
10431042 };
10441043 const ty_ref = try self.spv.arrayType(total_len, elem_ty_ref);
1045 entry.value_ptr.* = .{
1046 .ty_ref = ty_ref,
1047 };
1044 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
10481045 return ty_ref;
10491046 },
10501047 .Fn => switch (repr) {
10511048 .direct => {
1052 const entry = try self.type_map.getOrPut(self.gpa, ty.toIntern());
1053 if (entry.found_existing) return entry.value_ptr.ty_ref;
1049 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
10541050
10551051 const fn_info = mod.typeToFunc(ty).?;
10561052 // TODO: Put this somewhere in Sema.zig
......@@ -1069,10 +1065,7 @@ pub const DeclGen = struct {
10691065 .parameters = param_ty_refs,
10701066 } });
10711067
1072 entry.value_ptr.* = .{
1073 .ty_ref = ty_ref,
1074 };
1075
1068 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
10761069 return ty_ref;
10771070 },
10781071 .indirect => {
......@@ -1119,8 +1112,7 @@ pub const DeclGen = struct {
11191112 } });
11201113 },
11211114 .Struct => {
1122 const entry = try self.type_map.getOrPut(self.gpa, ty.toIntern());
1123 if (entry.found_existing) return entry.value_ptr.ty_ref;
1115 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
11241116
11251117 const struct_type = switch (ip.indexToKey(ty.toIntern())) {
11261118 .anon_struct_type => |tuple| {
......@@ -1140,9 +1132,7 @@ pub const DeclGen = struct {
11401132 .member_types = member_types[0..member_index],
11411133 } });
11421134
1143 entry.value_ptr.* = .{
1144 .ty_ref = ty_ref,
1145 };
1135 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
11461136 return ty_ref;
11471137 },
11481138 .struct_type => |struct_type| struct_type,
......@@ -1151,6 +1141,7 @@ pub const DeclGen = struct {
11511141
11521142 if (struct_type.layout == .Packed) {
11531143 return try self.resolveType(struct_type.backingIntType(ip).toType(), .direct);
1144 }
11541145
11551146 var member_types = std.ArrayList(CacheRef).init(self.gpa);
11561147 defer member_types.deinit();
......@@ -1172,9 +1163,7 @@ pub const DeclGen = struct {
11721163 .member_names = member_names.items,
11731164 } });
11741165
1175 entry.value_ptr.* = .{
1176 .ty_ref = ty_ref,
1177 };
1166 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
11781167 return ty_ref;
11791168 },
11801169 .Optional => {
......@@ -1192,8 +1181,7 @@ pub const DeclGen = struct {
11921181 return payload_ty_ref;
11931182 }
11941183
1195 const entry = try self.type_map.getOrPut(self.gpa, ty.toIntern());
1196 if (entry.found_existing) return entry.value_ptr.ty_ref;
1184 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
11971185
11981186 const bool_ty_ref = try self.resolveType(Type.bool, .indirect);
11991187
......@@ -1205,9 +1193,7 @@ pub const DeclGen = struct {
12051193 },
12061194 } });
12071195
1208 entry.value_ptr.* = .{
1209 .ty_ref = ty_ref,
1210 };
1196 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
12111197 return ty_ref;
12121198 },
12131199 .Union => return try self.resolveUnionType(ty, null),
......@@ -1221,8 +1207,7 @@ pub const DeclGen = struct {
12211207 return error_ty_ref;
12221208 }
12231209
1224 const entry = try self.type_map.getOrPut(self.gpa, ty.toIntern());
1225 if (entry.found_existing) return entry.value_ptr.ty_ref;
1210 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
12261211
12271212 const payload_ty_ref = try self.resolveType(payload_ty, .indirect);
12281213
......@@ -1252,9 +1237,7 @@ pub const DeclGen = struct {
12521237 .member_names = &member_names,
12531238 } });
12541239
1255 entry.value_ptr.* = .{
1256 .ty_ref = ty_ref,
1257 };
1240 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
12581241 return ty_ref;
12591242 },
12601243