From 31abd783e6ff567e2a85b3a154a87780fa880a74 Mon Sep 17 00:00:00 2001
From: George Karpenkov <cheshire@google.com>
Date: Fri, 10 Jan 2020 14:17:19 -0800
Subject: [PATCH] [TF/XLA] Remove an accidentally introduced race condition

cl/288939060 has introduced a race condition: previously, the lambda was only run once. This is the intend of the code.

Thanks to hyeontaek@ for discovering the bug.

PiperOrigin-RevId: 289163903
Change-Id: I35939dd84e5e789c5b72da0a8f2f28a0eaec0891
---
 tensorflow/compiler/tf2xla/xla_op_registry.cc | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/tensorflow/compiler/tf2xla/xla_op_registry.cc b/tensorflow/compiler/tf2xla/xla_op_registry.cc
index b16dd3086fe..a43608bd434 100644
--- a/tensorflow/compiler/tf2xla/xla_op_registry.cc
+++ b/tensorflow/compiler/tf2xla/xla_op_registry.cc
@@ -140,7 +140,7 @@ XlaOpRegistry::~XlaOpRegistry() = default;
 
   // Lazily register the CPU and GPU JIT devices the first time
   // GetCompilationDevice is called.
-  {
+  static void* registration_init = [&registry]() {
     MarkForCompilationPassFlags* flags = GetMarkForCompilationPassFlags();
     bool cpu_global_jit = flags->tf_xla_cpu_global_jit;
     VLOG(2) << "tf_xla_cpu_global_jit = " << cpu_global_jit;
@@ -162,7 +162,9 @@ XlaOpRegistry::~XlaOpRegistry() = default;
       registration.autoclustering_policy =
           XlaOpRegistry::AutoclusteringPolicy::kIfEnabledGlobally;
     }
-  }
+    return nullptr;
+  }();
+  (void)registration_init;
 
   mutex_lock lock(registry.mutex_);
   auto it = registry.compilation_devices_.find(device_name);