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

Mehdi Amini llvmlistbot at llvm.org
Sat Mar 16 16:25:58 PDT 2024


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

>From 10009c61706e4c9201aa98e1b05fa02b7902626f 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 1/2] 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);
 }
 
 //===----------------------------------------------------------------------===//

>From 9cd3599afa0b927b6310cce9215b548a06e70994 Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Sat, 16 Mar 2024 16:25:53 -0700
Subject: [PATCH 2/2] Update mlir/lib/Support/TypeID.cpp

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

diff --git a/mlir/lib/Support/TypeID.cpp b/mlir/lib/Support/TypeID.cpp
index 4c5bd75bfe1ec6..1eae8e7954653e 100644
--- a/mlir/lib/Support/TypeID.cpp
+++ b/mlir/lib/Support/TypeID.cpp
@@ -82,8 +82,8 @@ struct ImplicitTypeIDRegistry {
 
 TypeID detail::FallbackTypeIDResolver::registerImplicitTypeID(StringRef name) {
   // To prevent race conditions when one thread is accessing this `static`
-  // variable while other thread destructing it; construct the `registry`
-  // on the heap.
+  // variable while the program is exiting and another thread already destroyed
+  // it; intentionally construct the `registry` on the heap and never delete it.
   static auto *registry = new ImplicitTypeIDRegistry();
   return registry->lookupOrInsert(name);
 }



More information about the Mlir-commits mailing list