Suppress ThreadLocal hash override warning.

(cherry picked from commit 54f6fc7e604e146590892c0467e98764d761ae75)

Test: art/tools/buildbot-build.sh --host
Bug: 151947200
Merged-In: I9f80729c1ca0b770e4a1c85bd0a345cf5858b008
Change-Id: I426aa8ab0fc020458440968b39c7227779fad9d4
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index fbe1c0a..ec05939 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -718,7 +718,7 @@
                                                  "Ljava/lang/ThreadLocal;",
                                                  /*class_loader=*/ nullptr)
         : nullptr);
-    field_ = (klass_ != nullptr)
+    field_ = ((klass_ != nullptr) && klass_->IsVisiblyInitialized())
         ? klass_->FindDeclaredStaticField("nextHashCode",
                                           "Ljava/util/concurrent/atomic/AtomicInteger;")
         : nullptr;
@@ -742,7 +742,13 @@
       field_->SetObject</*kTransactionActive=*/ false>(klass_.Get(), new_field_value.Get());
     }
     if (apply && old_field_value_ == nullptr) {
-      LOG(ERROR) << "Failed to override ThreadLocal.nextHashCode";
+      if ((klass_ != nullptr) && klass_->IsVisiblyInitialized()) {
+        // This would mean that the implementation of ThreadLocal has changed
+        // and the code above is no longer applicable.
+        LOG(ERROR) << "Failed to override ThreadLocal.nextHashCode";
+      } else {
+        VLOG(compiler) << "ThreadLocal is not initialized in the primary boot image.";
+      }
     }
   }