[Openmp-commits] [openmp] [libomp] Atomically claim library unregistration (PR #216713)

Robert Imschweiler via Openmp-commits openmp-commits at lists.llvm.org
Mon Aug 17 05:12:49 PDT 2026


https://github.com/ro-i created https://github.com/llvm/llvm-project/pull/216713

Claude assisted with this patch.

Follow-up to https://github.com/llvm/llvm-project/pull/215988.

>From 4887cfe8db6f8172b0583b83df90e0d6e8b99518 Mon Sep 17 00:00:00 2001
From: Robert Imschweiler <robert.imschweiler at amd.com>
Date: Mon, 17 Aug 2026 02:27:58 -0500
Subject: [PATCH] [libomp] Atomically claim library unregistration

Claude assisted with this patch.
---
 openmp/runtime/src/kmp_runtime.cpp | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/openmp/runtime/src/kmp_runtime.cpp b/openmp/runtime/src/kmp_runtime.cpp
index f16bcf5e2cbbb..22ec2605718a3 100644
--- a/openmp/runtime/src/kmp_runtime.cpp
+++ b/openmp/runtime/src/kmp_runtime.cpp
@@ -6927,12 +6927,17 @@ void __kmp_register_library_startup(void) {
 
 void __kmp_unregister_library(void) {
 
-  // The library can be torn down before it ever registered itself, e.g. when
-  // __kmp_abort_process() runs for a fatal error raised during environment
-  // parsing. There is nothing to unregister then, and __kmp_registration_str
-  // is still NULL, so the strcmp() below would dereference it.
-  if (__kmp_registration_flag == 0)
+  // Claim the unregistration. Teardown can be entered concurrently from
+  // library shutdown, __kmp_abort_process() and the signal handler, none of
+  // which share a lock, and the library may never have registered itself at
+  // all (e.g. a fatal error raised before registration). Atomically take
+  // ownership of __kmp_registration_str so exactly one caller runs the
+  // teardown below; the others return without touching the freed string.
+  char *reg_str = __kmp_registration_str;
+  if (reg_str == NULL ||
+      !KMP_COMPARE_AND_STORE_PTR(&__kmp_registration_str, reg_str, NULL))
     return;
+  __kmp_registration_flag = 0;
 
   char *name = __kmp_reg_status_name();
   char *value = NULL;
@@ -6968,9 +6973,7 @@ void __kmp_unregister_library(void) {
   value = __kmp_env_get(name);
 #endif
 
-  KMP_DEBUG_ASSERT(__kmp_registration_flag != 0);
-  KMP_DEBUG_ASSERT(__kmp_registration_str != NULL);
-  if (value != NULL && strcmp(value, __kmp_registration_str) == 0) {
+  if (value != NULL && strcmp(value, reg_str) == 0) {
 //  Ok, this is our variable. Delete it.
 #if defined(KMP_USE_SHM)
     if (__kmp_shm_available) {
@@ -6992,13 +6995,10 @@ void __kmp_unregister_library(void) {
     KMP_INTERNAL_FREE(temp_reg_status_file_name);
 #endif
 
-  KMP_INTERNAL_FREE(__kmp_registration_str);
+  KMP_INTERNAL_FREE(reg_str);
   KMP_INTERNAL_FREE(value);
   KMP_INTERNAL_FREE(name);
 
-  __kmp_registration_flag = 0;
-  __kmp_registration_str = NULL;
-
 } // __kmp_unregister_library
 
 // End of Library registration stuff.



More information about the Openmp-commits mailing list