@@ -17,14 +17,25 @@ TypeCanonicalizer* GetTypeCanonicalizer() {
1717
1818TypeCanonicalizer::TypeCanonicalizer () { AddPredefinedArrayTypes (); }
1919
20- // For convenience, limit canonicalized type indices to Smi range.
21- // We could squeeze out a few more bits if necessary by passing them
22- // from compiled wrappers to runtime functions as Smi-tagged unsigned ints.
23- // That would give us "uint31" range on 32-bit platforms, and allow
24- // uint32_t (or even more) on 64-bit platforms. But we probably don't want
25- // to store that many types in the TypeCanonicalizer anyway.
26- static constexpr size_t kMaxCanonicalTypes = kSmiMaxValue ;
20+ // Inside the TypeCanonicalizer, we use ValueType instances constructed
21+ // from canonical type indices, so we can't let them get bigger than what
22+ // we have storage space for. Code outside the TypeCanonicalizer already
23+ // supports up to Smi range for canonical type indices.
24+ // TODO(jkummerow): Raise this limit. Possible options:
25+ // - increase the size of ValueType::HeapTypeField, using currently-unused bits.
26+ // - change the encoding of ValueType: one bit says whether it's a ref type,
27+ // the other bits then encode the index or the kind of non-ref type.
28+ // - refactor the TypeCanonicalizer's internals to no longer use ValueTypes
29+ // and related infrastructure, and use a wider encoding of canonicalized
30+ // type indices only here.
31+ // - wait for 32-bit platforms to no longer be relevant, and increase the
32+ // size of ValueType to 64 bits.
33+ // None of this seems urgent, as we have no evidence of the current limit
34+ // being an actual limitation in practice.
35+ static constexpr size_t kMaxCanonicalTypes = kV8MaxWasmTypes ;
36+ // We don't want any valid modules to fail canonicalization.
2737static_assert (kMaxCanonicalTypes >= kV8MaxWasmTypes );
38+ // We want the invalid index to fail any range checks.
2839static_assert (kInvalidCanonicalIndex > kMaxCanonicalTypes );
2940
3041void TypeCanonicalizer::CheckMaxCanonicalIndex () const {
@@ -215,6 +226,7 @@ ValueType TypeCanonicalizer::CanonicalizeValueType(
215226 const WasmModule* module , ValueType type,
216227 uint32_t recursive_group_start) const {
217228 if (!type.has_index ()) return type;
229+ static_assert (kMaxCanonicalTypes <= (1u << ValueType::kHeapTypeBits ));
218230 return type.ref_index () >= recursive_group_start
219231 ? ValueType::CanonicalWithRelativeIndex (
220232 type.kind (), type.ref_index () - recursive_group_start)
0 commit comments