[VM] Rehash constant tables after cid-renumbering during app-jit training

When sorting classes during app-jit training we permute the class ids.

This makes canonical hash codes which depend directly (or indirectly) on
class ids change.  We already clear out the cached canonical hash codes
in type-related instances in the heap.

Though the cid renumbering makes the non-cached canonical hashcodes of
other objects, such as canonical Instance and Array objects, also change.

This means any data structures which use canonical hash codes of
type related objects or Instance/Array objects need to re-hash.

-> This CL makes us rehash the constant tables after cid renumbering.

Fixes https://github.com/dart-lang/sdk/issues/34849

Change-Id: Icfb5add3f14e2c62c5bca94f296133121ad0bc55
Reviewed-on: https://dart-review.googlesource.com/c/80801
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Vyacheslav Egorov <vegorov@google.com>
diff --git a/runtime/vm/class_finalizer.cc b/runtime/vm/class_finalizer.cc
index 6eca6cc..5d40a65 100644
--- a/runtime/vm/class_finalizer.cc
+++ b/runtime/vm/class_finalizer.cc
@@ -3603,6 +3603,45 @@
 #endif
 }
 
+// Clears the cached canonicalized hash codes for all instances which directly
+// (or indirectly) depend on class ids.
+//
+// In the Dart VM heap the following instances directly use cids for the
+// computation of canonical hash codes:
+//
+//    * RawType (due to RawType::type_class_id_)
+//    * RawTypeParameter (due to RawTypeParameter::parameterized_class_id_)
+//
+// The following instances use cids for the computation of canonical hash codes
+// indirectly:
+//
+//    * RawTypeRef (due to RawTypeRef::type_->type_class_id)
+//    * RawType (due to RawType::signature_'s result/parameter types)
+//    * RawBoundedType (due to RawBoundedType::type_parameter_)
+//    * RawTypeArguments (due to type references)
+//    * RawInstance (due to instance fields)
+//    * RawArray (due to type arguments & array entries)
+//
+// Caching of the canonical hash codes happens for:
+//
+//    * RawType::hash_
+//    * RawTypeParameter::hash_
+//    * RawBoundedType::hash_
+//    * RawTypeArguments::hash_
+//
+// No caching of canonical hash codes (i.e. it gets re-computed every time)
+// happens for:
+//
+//    * RawTypeRef (computed via RawTypeRef::type_->type_class_id)
+//    * RawInstance (computed via size & fields)
+//    * RawArray (computed via type arguments & array entries)
+//
+// Usages of canonical hash codes are:
+//
+//   * ObjectStore::canonical_types()
+//   * ObjectStore::canonical_type_arguments()
+//   * Class::constants()
+//
 class ClearTypeHashVisitor : public ObjectVisitor {
  public:
   explicit ClearTypeHashVisitor(Zone* zone)
@@ -3688,6 +3727,10 @@
     typeargs_table.Release();
   }
 
+  // The canonical constant tables use canonical hashcodes which can change
+  // due to cid-renumbering.
+  I->RehashConstants();
+
   dict_size = Utils::RoundUpToPowerOfTwo(typeargs.Length() * 4 / 3);
   typeargs_array =
       HashTables::New<CanonicalTypeArgumentsSet>(dict_size, Heap::kOld);