[Openmp-commits] [PATCH] D159369: [OpenMP] Fix a wrong assertion in `__kmp_get_global_thread_id`

Shilei Tian via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Fri Sep 1 10:32:57 PDT 2023


tianshilei1992 created this revision.
tianshilei1992 added reviewers: protze.joachim, jlpeyton.
Herald added subscribers: guansong, yaxunl.
Herald added a project: All.
tianshilei1992 requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added subscribers: openmp-commits, jplehr, sstefan1.
Herald added a project: OpenMP.

The function assumes that `__kmp_gtid_get_specific` always returns a valid gtid.
That is not always true, because when creating the key for thread-specific data,
a destructor is assigned. The dtor will be called at thread exit. However, before
the dtor is called, the thread-specific data will be reset to NULL first
(https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_key_create.html):

> At thread exit, if a key value has a non-NULL destructor pointer, and the thread
> has a non-NULL value associated with that key, the value of the key is set to NULL.

This will lead to that `__kmp_gtid_get_specific` returns `KMP_GTID_DNE`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159369

Files:
  openmp/runtime/src/kmp_runtime.cpp


Index: openmp/runtime/src/kmp_runtime.cpp
===================================================================
--- openmp/runtime/src/kmp_runtime.cpp
+++ openmp/runtime/src/kmp_runtime.cpp
@@ -178,7 +178,12 @@
       if (stack_diff <= stack_size) {
         /* The only way we can be closer than the allocated */
         /* stack size is if we are running on this thread. */
-        KMP_DEBUG_ASSERT(__kmp_gtid_get_specific() == i);
+        // __kmp_gtid_get_specific can return KMP_GTID_DNE because this function
+        // can be called by thread destructor. However, before the thread
+        // destructor is called, the value of the corresponding thread-specific
+        // data will be reset to NULL.
+        KMP_DEBUG_ASSERT(__kmp_gtid_get_specific() == KMP_GTID_DNE ||
+                         __kmp_gtid_get_specific() == i);
         return i;
       }
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159369.555436.patch
Type: text/x-patch
Size: 877 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20230901/d284c468/attachment.bin>


More information about the Openmp-commits mailing list