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

Li Deng llvmlistbot at llvm.org
Fri Mar 15 14:42:36 PDT 2024


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

None

>From 096ee7bc13234afbd1933aff67a504d61a15ce20 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..0cf50bc2f721b7 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 threads are 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