|
11 | 11 |
|
12 | 12 | #include <unordered_map> |
13 | 13 |
|
| 14 | +#include "src/base/bounds.h" |
14 | 15 | #include "src/base/functional.h" |
15 | 16 | #include "src/wasm/value-type.h" |
16 | 17 | #include "src/wasm/wasm-module.h" |
@@ -144,137 +145,252 @@ class TypeCanonicalizer { |
144 | 145 | bool is_final = false; |
145 | 146 | bool is_shared = false; |
146 | 147 | uint8_t subtyping_depth = 0; |
147 | | - bool is_relative_supertype; |
148 | 148 |
|
149 | 149 | constexpr CanonicalType(const CanonicalSig* sig, |
150 | 150 | CanonicalTypeIndex supertype, bool is_final, |
151 | | - bool is_shared, bool is_relative_supertype) |
| 151 | + bool is_shared) |
152 | 152 | : function_sig(sig), |
153 | 153 | supertype(supertype), |
154 | 154 | kind(kFunction), |
155 | 155 | is_final(is_final), |
156 | | - is_shared(is_shared), |
157 | | - is_relative_supertype(is_relative_supertype) {} |
| 156 | + is_shared(is_shared) {} |
158 | 157 |
|
159 | 158 | constexpr CanonicalType(const CanonicalStructType* type, |
160 | 159 | CanonicalTypeIndex supertype, bool is_final, |
161 | | - bool is_shared, bool is_relative_supertype) |
| 160 | + bool is_shared) |
162 | 161 | : struct_type(type), |
163 | 162 | supertype(supertype), |
164 | 163 | kind(kStruct), |
165 | 164 | is_final(is_final), |
166 | | - is_shared(is_shared), |
167 | | - is_relative_supertype(is_relative_supertype) {} |
| 165 | + is_shared(is_shared) {} |
168 | 166 |
|
169 | 167 | constexpr CanonicalType(const CanonicalArrayType* type, |
170 | 168 | CanonicalTypeIndex supertype, bool is_final, |
171 | | - bool is_shared, bool is_relative_supertype) |
| 169 | + bool is_shared) |
172 | 170 | : array_type(type), |
173 | 171 | supertype(supertype), |
174 | 172 | kind(kArray), |
175 | 173 | is_final(is_final), |
176 | | - is_shared(is_shared), |
177 | | - is_relative_supertype(is_relative_supertype) {} |
| 174 | + is_shared(is_shared) {} |
178 | 175 |
|
179 | 176 | constexpr CanonicalType() = default; |
| 177 | + }; |
| 178 | + |
| 179 | + // Define the range of a recursion group; for use in {CanonicalHashing} and |
| 180 | + // {CanonicalEquality}. |
| 181 | + struct RecursionGroupRange { |
| 182 | + const CanonicalTypeIndex start; |
| 183 | + const CanonicalTypeIndex end; |
180 | 184 |
|
181 | | - bool operator==(const CanonicalType& other) const { |
182 | | - if (supertype != other.supertype) return false; |
183 | | - if (kind != other.kind) return false; |
184 | | - if (is_final != other.is_final) return false; |
185 | | - if (is_shared != other.is_shared) return false; |
186 | | - if (is_relative_supertype != other.is_relative_supertype) return false; |
187 | | - if (kind == kFunction) return *function_sig == *other.function_sig; |
188 | | - if (kind == kStruct) return *struct_type == *other.struct_type; |
189 | | - DCHECK_EQ(kArray, kind); |
190 | | - return *array_type == *other.array_type; |
| 185 | + bool Contains(CanonicalTypeIndex index) const { |
| 186 | + return base::IsInRange(index.index, start.index, end.index); |
191 | 187 | } |
192 | 188 |
|
193 | | - bool operator!=(const CanonicalType& other) const { |
194 | | - return !operator==(other); |
| 189 | + CanonicalTypeIndex RelativeIndex(CanonicalTypeIndex index) const { |
| 190 | + return Contains(index) |
| 191 | + // Make the value_type relative within the recursion group. |
| 192 | + ? CanonicalTypeIndex{index.index - start.index} |
| 193 | + : index; |
195 | 194 | } |
196 | 195 |
|
197 | | - size_t hash_value() const { |
198 | | - uint32_t metadata = (supertype.index << 2) | (is_final ? 2 : 0) | |
199 | | - (is_relative_supertype ? 1 : 0); |
200 | | - base::Hasher hasher; |
| 196 | + CanonicalValueType RelativeType(CanonicalValueType type) const { |
| 197 | + return type.has_index() |
| 198 | + ? CanonicalValueType::FromIndex( |
| 199 | + type.kind(), RelativeIndex(type.ref_index())) |
| 200 | + : type; |
| 201 | + } |
| 202 | + }; |
| 203 | + |
| 204 | + // Support for hashing of recursion groups, where type indexes have to be |
| 205 | + // hashed relative to the recursion group. |
| 206 | + struct CanonicalHashing { |
| 207 | + base::Hasher hasher; |
| 208 | + const RecursionGroupRange recgroup; |
| 209 | + |
| 210 | + explicit CanonicalHashing(RecursionGroupRange recgroup) |
| 211 | + : recgroup{recgroup} {} |
| 212 | + |
| 213 | + void Add(CanonicalType type) { |
| 214 | + CanonicalTypeIndex relative_supertype = |
| 215 | + recgroup.RelativeIndex(type.supertype); |
| 216 | + uint32_t metadata = |
| 217 | + (relative_supertype.index << 1) | (type.is_final ? 1 : 0); |
201 | 218 | hasher.Add(metadata); |
202 | | - if (kind == kFunction) { |
203 | | - hasher.Add(*function_sig); |
204 | | - } else if (kind == kStruct) { |
205 | | - hasher.Add(*struct_type); |
206 | | - } else { |
207 | | - DCHECK_EQ(kArray, kind); |
208 | | - hasher.Add(*array_type); |
| 219 | + switch (type.kind) { |
| 220 | + case CanonicalType::kFunction: |
| 221 | + Add(*type.function_sig); |
| 222 | + break; |
| 223 | + case CanonicalType::kStruct: |
| 224 | + Add(*type.struct_type); |
| 225 | + break; |
| 226 | + case CanonicalType::kArray: |
| 227 | + Add(*type.array_type); |
| 228 | + break; |
209 | 229 | } |
210 | | - return hasher.hash(); |
211 | 230 | } |
| 231 | + |
| 232 | + void Add(CanonicalValueType value_type) { |
| 233 | + hasher.Add(recgroup.RelativeType(value_type)); |
| 234 | + } |
| 235 | + |
| 236 | + void Add(const CanonicalSig& sig) { |
| 237 | + hasher.Add(sig.parameter_count()); |
| 238 | + for (CanonicalValueType type : sig.all()) Add(type); |
| 239 | + } |
| 240 | + |
| 241 | + void Add(const CanonicalStructType& struct_type) { |
| 242 | + hasher.AddRange(struct_type.mutabilities()); |
| 243 | + for (const ValueTypeBase& field : struct_type.fields()) { |
| 244 | + Add(CanonicalValueType{field}); |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + void Add(const CanonicalArrayType& array_type) { |
| 249 | + hasher.Add(array_type.mutability()); |
| 250 | + Add(array_type.element_type()); |
| 251 | + } |
| 252 | + |
| 253 | + size_t hash() const { return hasher.hash(); } |
212 | 254 | }; |
213 | | - struct CanonicalGroup { |
214 | | - CanonicalGroup(Zone* zone, size_t size) |
215 | | - : types(zone->AllocateVector<CanonicalType>(size)) {} |
216 | 255 |
|
217 | | - bool operator==(const CanonicalGroup& other) const { |
218 | | - return types == other.types; |
| 256 | + // Support for equality checking of recursion groups, where type indexes have |
| 257 | + // to be compared relative to their respective recursion group. |
| 258 | + struct CanonicalEquality { |
| 259 | + // Recursion group bounds for LHS and RHS. |
| 260 | + const RecursionGroupRange recgroup1; |
| 261 | + const RecursionGroupRange recgroup2; |
| 262 | + |
| 263 | + CanonicalEquality(RecursionGroupRange recgroup1, |
| 264 | + RecursionGroupRange recgroup2) |
| 265 | + : recgroup1{recgroup1}, recgroup2{recgroup2} {} |
| 266 | + |
| 267 | + bool EqualType(const CanonicalType& type1, |
| 268 | + const CanonicalType& type2) const { |
| 269 | + if (recgroup1.RelativeIndex(type1.supertype) != |
| 270 | + recgroup2.RelativeIndex(type2.supertype)) { |
| 271 | + return false; |
| 272 | + } |
| 273 | + if (type1.is_final != type2.is_final) return false; |
| 274 | + if (type1.is_shared != type2.is_shared) return false; |
| 275 | + switch (type1.kind) { |
| 276 | + case CanonicalType::kFunction: |
| 277 | + return type2.kind == CanonicalType::kFunction && |
| 278 | + EqualSig(*type1.function_sig, *type2.function_sig); |
| 279 | + case CanonicalType::kStruct: |
| 280 | + return type2.kind == CanonicalType::kStruct && |
| 281 | + EqualStructType(*type1.struct_type, *type2.struct_type); |
| 282 | + case CanonicalType::kArray: |
| 283 | + return type2.kind == CanonicalType::kArray && |
| 284 | + EqualArrayType(*type1.array_type, *type2.array_type); |
| 285 | + } |
219 | 286 | } |
220 | 287 |
|
221 | | - bool operator!=(const CanonicalGroup& other) const { |
222 | | - return types != other.types; |
| 288 | + bool EqualTypes(base::Vector<const CanonicalType> types1, |
| 289 | + base::Vector<const CanonicalType> types2) const { |
| 290 | + return std::equal(types1.begin(), types1.end(), types2.begin(), |
| 291 | + types2.end(), |
| 292 | + std::bind_front(&CanonicalEquality::EqualType, this)); |
| 293 | + } |
| 294 | + |
| 295 | + bool EqualValueType(CanonicalValueType type1, |
| 296 | + CanonicalValueType type2) const { |
| 297 | + return recgroup1.RelativeType(type1) == recgroup2.RelativeType(type2); |
| 298 | + } |
| 299 | + |
| 300 | + bool EqualSig(const CanonicalSig& sig1, const CanonicalSig& sig2) const { |
| 301 | + if (sig1.parameter_count() != sig2.parameter_count()) return false; |
| 302 | + return std::equal( |
| 303 | + sig1.all().begin(), sig1.all().end(), sig2.all().begin(), |
| 304 | + sig2.all().end(), |
| 305 | + std::bind_front(&CanonicalEquality::EqualValueType, this)); |
| 306 | + } |
| 307 | + |
| 308 | + bool EqualStructType(const CanonicalStructType& type1, |
| 309 | + const CanonicalStructType& type2) const { |
| 310 | + return std::equal( |
| 311 | + type1.fields().begin(), type1.fields().end(), type2.fields().begin(), |
| 312 | + type2.fields().end(), |
| 313 | + std::bind_front(&CanonicalEquality::EqualValueType, this)); |
| 314 | + } |
| 315 | + |
| 316 | + bool EqualArrayType(const CanonicalArrayType& type1, |
| 317 | + const CanonicalArrayType& type2) const { |
| 318 | + return type1.mutability() == type2.mutability() && |
| 319 | + EqualValueType(type1.element_type(), type2.element_type()); |
| 320 | + } |
| 321 | + }; |
| 322 | + |
| 323 | + struct CanonicalGroup { |
| 324 | + CanonicalGroup(Zone* zone, size_t size, CanonicalTypeIndex start) |
| 325 | + : types(zone->AllocateVector<CanonicalType>(size)), start(start) { |
| 326 | + // size >= 2; otherwise a `CanonicalSingletonGroup` should have been used. |
| 327 | + DCHECK_LE(2, size); |
| 328 | + } |
| 329 | + |
| 330 | + bool operator==(const CanonicalGroup& other) const { |
| 331 | + CanonicalTypeIndex end{start.index + |
| 332 | + static_cast<uint32_t>(types.size() - 1)}; |
| 333 | + CanonicalTypeIndex other_end{ |
| 334 | + other.start.index + static_cast<uint32_t>(other.types.size() - 1)}; |
| 335 | + CanonicalEquality equality{{start, end}, {other.start, other_end}}; |
| 336 | + return equality.EqualTypes(types, other.types); |
223 | 337 | } |
224 | 338 |
|
225 | 339 | size_t hash_value() const { |
226 | | - return base::Hasher{}.AddRange(types.begin(), types.end()).hash(); |
| 340 | + CanonicalTypeIndex end{start.index + static_cast<uint32_t>(types.size()) - |
| 341 | + 1}; |
| 342 | + CanonicalHashing hasher{{start, end}}; |
| 343 | + for (CanonicalType t : types) { |
| 344 | + hasher.Add(t); |
| 345 | + } |
| 346 | + return hasher.hash(); |
227 | 347 | } |
228 | 348 |
|
229 | 349 | // The storage of this vector is the TypeCanonicalizer's zone_. |
230 | | - base::Vector<CanonicalType> types; |
| 350 | + const base::Vector<CanonicalType> types; |
| 351 | + const CanonicalTypeIndex start; |
231 | 352 | }; |
232 | 353 |
|
233 | 354 | struct CanonicalSingletonGroup { |
234 | | - struct hash { |
235 | | - size_t operator()(const CanonicalSingletonGroup& group) const { |
236 | | - return group.hash_value(); |
237 | | - } |
238 | | - }; |
239 | | - |
240 | 355 | bool operator==(const CanonicalSingletonGroup& other) const { |
241 | | - return type == other.type; |
| 356 | + CanonicalEquality equality{{index, index}, {other.index, other.index}}; |
| 357 | + return equality.EqualType(type, other.type); |
242 | 358 | } |
243 | 359 |
|
244 | | - size_t hash_value() const { return type.hash_value(); } |
| 360 | + size_t hash_value() const { |
| 361 | + CanonicalHashing hasher{{index, index}}; |
| 362 | + hasher.Add(type); |
| 363 | + return hasher.hash(); |
| 364 | + } |
245 | 365 |
|
246 | 366 | CanonicalType type; |
| 367 | + CanonicalTypeIndex index; |
247 | 368 | }; |
248 | 369 |
|
249 | 370 | void AddPredefinedArrayTypes(); |
250 | 371 |
|
251 | 372 | CanonicalTypeIndex FindCanonicalGroup(const CanonicalGroup&) const; |
252 | 373 | CanonicalTypeIndex FindCanonicalGroup(const CanonicalSingletonGroup&) const; |
253 | 374 |
|
254 | | - // Canonicalize all types present in {type} (including supertype) according to |
255 | | - // {CanonicalizeValueType}. |
256 | | - CanonicalType CanonicalizeTypeDef(const WasmModule* module, |
257 | | - TypeDefinition type, |
258 | | - uint32_t recursive_group_start); |
259 | | - |
260 | | - // An indexed type gets mapped to a {CanonicalValueType::WithRelativeIndex} |
261 | | - // if its index points inside the new canonical group; otherwise, the index |
262 | | - // gets mapped to its canonical representative. |
263 | | - CanonicalValueType CanonicalizeValueType( |
264 | | - const WasmModule* module, ValueType type, |
265 | | - uint32_t recursive_group_start) const; |
| 375 | + // Canonicalize the module-specific type at `module_type_idx` within the |
| 376 | + // recursion group starting at `recursion_group_start`, using |
| 377 | + // `canonical_recgroup_start` as the start offset of types within the |
| 378 | + // recursion group. |
| 379 | + CanonicalType CanonicalizeTypeDef( |
| 380 | + const WasmModule* module, ModuleTypeIndex module_type_idx, |
| 381 | + ModuleTypeIndex recgroup_start, |
| 382 | + CanonicalTypeIndex canonical_recgroup_start); |
266 | 383 |
|
267 | 384 | CanonicalTypeIndex AddRecursiveGroup(CanonicalType type); |
268 | 385 |
|
269 | 386 | void CheckMaxCanonicalIndex() const; |
270 | 387 |
|
271 | 388 | std::vector<CanonicalTypeIndex> canonical_supertypes_; |
272 | | - // Maps groups of size >=2 to the canonical id of the first type. |
273 | | - std::unordered_map<CanonicalGroup, CanonicalTypeIndex, |
274 | | - base::hash<CanonicalGroup>> |
| 389 | + // Set of all known canonical recgroups of size >=2. |
| 390 | + std::unordered_set<CanonicalGroup, base::hash<CanonicalGroup>> |
275 | 391 | canonical_groups_; |
276 | | - // Maps group of size 1 to the canonical id of the type. |
277 | | - std::unordered_map<CanonicalSingletonGroup, CanonicalTypeIndex, |
| 392 | + // Set of all known canonical recgroups of size 1. |
| 393 | + std::unordered_set<CanonicalSingletonGroup, |
278 | 394 | base::hash<CanonicalSingletonGroup>> |
279 | 395 | canonical_singleton_groups_; |
280 | 396 | // Maps canonical indices back to the function signature. |
|
0 commit comments