[Mlir-commits] [mlir] fix a concurrent destruction issue for FallbackTypeIDResolver::registerImplicitTypeID due to function static variable (PR #85471)

Li Deng llvmlistbot at llvm.org
Sat Mar 16 16:40:45 PDT 2024


https://github.com/dengl11 updated https://github.com/llvm/llvm-project/pull/85471

>From e592933992857668de66e47cf5a170f6f4487c74 Mon Sep 17 00:00:00 2001
From: Li Deng <dengli at google.com>
Date: Fri, 15 Mar 2024 14:10:13 -0700
Subject: [PATCH] fix a concurrent destruction issue for
 FallbackTypeIDResolver::registerImplicitTypeID due to function static
 variable

---
 mlir/lib/Support/TypeID.cpp | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/mlir/lib/Support/TypeID.cpp b/mlir/lib/Support/TypeID.cpp
index e499e2f2836334..4c5bd75bfe1ec6 100644
--- a/mlir/lib/Support/TypeID.cpp
+++ b/mlir/lib/Support/TypeID.cpp
@@ -81,8 +81,11 @@ struct ImplicitTypeIDRegistry {
 } // end namespace
 
 TypeID detail::FallbackTypeIDResolver::registerImplicitTypeID(StringRef name) {
-  static ImplicitTypeIDRegistry registry;
-  return registry.lookupOrInsert(name);
+  // To prevent race conditions when one thread is accessing this `static`
+  // variable while other thread destructing it; construct the `registry`
+  // on the heap.
+  static auto *registry = new ImplicitTypeIDRegistry();
+  return registry->lookupOrInsert(name);
 }
 
 //===----------------------------------------------------------------------===//



More information about the Mlir-commits mailing list