[Openmp-commits] [openmp] [OpenMP] Move KMP_CANCEL_THREADS control from preprocessor to CMake (PR #193681)
Mark Zhuang via Openmp-commits
openmp-commits at lists.llvm.org
Wed Jun 17 07:36:23 PDT 2026
https://github.com/zqb-all updated https://github.com/llvm/llvm-project/pull/193681
>From 74c19dae184e854aa5313e51e3d1322ea3986da6 Mon Sep 17 00:00:00 2001
From: Mark Zhuang <mark.zhuang at spacemit.com>
Date: Thu, 23 Apr 2026 14:15:33 +0800
Subject: [PATCH 1/3] [OpenMP] Move KMP_CANCEL_THREADS control from
preprocessor to CMake
Replace the define-then-undef pattern in kmp.h with a proper CMake
cache variable LIBOMP_USE_CANCEL_THREADS. This moves platform detection
(Android, WASI) to CMakeLists.txt where it belongs and exposes a
user-facing -DLIBOMP_USE_CANCEL_THREADS=OFF knob for other platforms
that may lack pthread_cancel.
Assisted-by: Claude Opus 4.7
---
openmp/runtime/CMakeLists.txt | 10 ++++++++++
openmp/runtime/src/kmp.h | 14 ++------------
openmp/runtime/src/kmp_config.h.cmake | 4 ++++
3 files changed, 16 insertions(+), 12 deletions(-)
diff --git a/openmp/runtime/CMakeLists.txt b/openmp/runtime/CMakeLists.txt
index 3ccfa8463810f..13307faabe020 100644
--- a/openmp/runtime/CMakeLists.txt
+++ b/openmp/runtime/CMakeLists.txt
@@ -327,6 +327,16 @@ if(LIBOMP_STATS)
set(LIBOMP_USE_STDCPPLIB TRUE)
endif()
+# Thread cancellation via pthread_cancel. Disabled on platforms that lack it
+# (Android, WASI) but can also be forced off via -DLIBOMP_USE_CANCEL_THREADS=OFF.
+if(ANDROID OR WASM)
+ set(LIBOMP_USE_CANCEL_THREADS_DEFAULT FALSE)
+else()
+ set(LIBOMP_USE_CANCEL_THREADS_DEFAULT TRUE)
+endif()
+set(LIBOMP_USE_CANCEL_THREADS "${LIBOMP_USE_CANCEL_THREADS_DEFAULT}" CACHE BOOL
+ "Enable thread cancellation via pthread_cancel?")
+
# Shared library can be switched to a static library
set(LIBOMP_ENABLE_SHARED TRUE CACHE BOOL
"Shared library instead of static library?")
diff --git a/openmp/runtime/src/kmp.h b/openmp/runtime/src/kmp.h
index ce2ddb01ad1ae..3921ee2bd29aa 100644
--- a/openmp/runtime/src/kmp.h
+++ b/openmp/runtime/src/kmp.h
@@ -45,20 +45,10 @@
#define TASK_DETACHABLE 1
#define TASK_UNDETACHABLE 0
+#ifdef KMP_USE_CANCEL_THREADS
#define KMP_CANCEL_THREADS
-#define KMP_THREAD_ATTR
-
-// Android does not have pthread_cancel. Undefine KMP_CANCEL_THREADS if being
-// built on Android
-#if defined(__ANDROID__)
-#undef KMP_CANCEL_THREADS
-#endif
-
-// Some WASI targets (e.g., wasm32-wasi-threads) do not support thread
-// cancellation.
-#if KMP_OS_WASI
-#undef KMP_CANCEL_THREADS
#endif
+#define KMP_THREAD_ATTR
#if !KMP_OS_WASI
#include <signal.h>
diff --git a/openmp/runtime/src/kmp_config.h.cmake b/openmp/runtime/src/kmp_config.h.cmake
index 40f1087fd7f27..dfc17479b68de 100644
--- a/openmp/runtime/src/kmp_config.h.cmake
+++ b/openmp/runtime/src/kmp_config.h.cmake
@@ -96,6 +96,10 @@
#define KMP_HAVE_POSIX_MEMALIGN LIBOMP_HAVE_POSIX_MEMALIGN
#cmakedefine01 LIBOMP_HAVE__ALIGNED_MALLOC
#define KMP_HAVE__ALIGNED_MALLOC LIBOMP_HAVE__ALIGNED_MALLOC
+#cmakedefine01 LIBOMP_USE_CANCEL_THREADS
+#if LIBOMP_USE_CANCEL_THREADS
+# define KMP_USE_CANCEL_THREADS
+#endif
#cmakedefine01 OPENMP_ENABLE_LIBOMPTARGET
#define ENABLE_LIBOMPTARGET OPENMP_ENABLE_LIBOMPTARGET
>From eb8e54f3926fa02a7fd22c0f07f0054a706887ed Mon Sep 17 00:00:00 2001
From: Mark Zhuang <mark.zhuang at spacemit.com>
Date: Tue, 5 May 2026 17:56:36 +0800
Subject: [PATCH 2/3] [OpenMP] Simplify KMP_CANCEL_THREADS and detect
pthread_cancel via CMake
Replace the three-level KMP_USE_CANCEL_THREADS -> KMP_CANCEL_THREADS
indirection with a direct 0/1 #define in kmp_config.h.cmake, matching the
convention used by other KMP_ macros. Update #ifdef to #if accordingly.
Use check_symbol_exists(pthread_cancel) as the default for unknown
platforms. Android and WASM are still forced off unconditionally to guard
against cross-compilation where the build sysroot may be more complete than
the actual target runtime.
---
openmp/runtime/CMakeLists.txt | 8 +++++---
openmp/runtime/cmake/config-ix.cmake | 1 +
openmp/runtime/src/kmp.h | 3 ---
openmp/runtime/src/kmp_config.h.cmake | 4 +---
openmp/runtime/src/z_Linux_util.cpp | 10 +++++-----
5 files changed, 12 insertions(+), 14 deletions(-)
diff --git a/openmp/runtime/CMakeLists.txt b/openmp/runtime/CMakeLists.txt
index 13307faabe020..205d7efc10bf4 100644
--- a/openmp/runtime/CMakeLists.txt
+++ b/openmp/runtime/CMakeLists.txt
@@ -327,12 +327,14 @@ if(LIBOMP_STATS)
set(LIBOMP_USE_STDCPPLIB TRUE)
endif()
-# Thread cancellation via pthread_cancel. Disabled on platforms that lack it
-# (Android, WASI) but can also be forced off via -DLIBOMP_USE_CANCEL_THREADS=OFF.
+# Thread cancellation via pthread_cancel. Platforms known to lack it at the
+# ABI level (Android, WASM) are forced off; others default to symbol
+# availability but can be overridden via -DLIBOMP_USE_CANCEL_THREADS=OFF for
+# cases where the build sysroot is more complete than the target runtime.
if(ANDROID OR WASM)
set(LIBOMP_USE_CANCEL_THREADS_DEFAULT FALSE)
else()
- set(LIBOMP_USE_CANCEL_THREADS_DEFAULT TRUE)
+ set(LIBOMP_USE_CANCEL_THREADS_DEFAULT "${LIBOMP_HAVE_PTHREAD_CANCEL}")
endif()
set(LIBOMP_USE_CANCEL_THREADS "${LIBOMP_USE_CANCEL_THREADS_DEFAULT}" CACHE BOOL
"Enable thread cancellation via pthread_cancel?")
diff --git a/openmp/runtime/cmake/config-ix.cmake b/openmp/runtime/cmake/config-ix.cmake
index f076d4d43480d..d8d031a8c5aac 100644
--- a/openmp/runtime/cmake/config-ix.cmake
+++ b/openmp/runtime/cmake/config-ix.cmake
@@ -100,6 +100,7 @@ endif()
# Check non-posix pthread API here before CMAKE_REQUIRED_DEFINITIONS gets messed up
check_symbol_exists(pthread_setname_np "pthread.h" LIBOMP_HAVE_PTHREAD_SETNAME_NP)
check_symbol_exists(pthread_set_name_np "pthread.h;pthread_np.h" LIBOMP_HAVE_PTHREAD_SET_NAME_NP)
+check_symbol_exists(pthread_cancel "pthread.h" LIBOMP_HAVE_PTHREAD_CANCEL)
# Check for Unix shared memory
check_symbol_exists(shm_open "sys/mman.h" LIBOMP_HAVE_SHM_OPEN_NO_LRT)
diff --git a/openmp/runtime/src/kmp.h b/openmp/runtime/src/kmp.h
index 3921ee2bd29aa..ea642ddbf2d84 100644
--- a/openmp/runtime/src/kmp.h
+++ b/openmp/runtime/src/kmp.h
@@ -45,9 +45,6 @@
#define TASK_DETACHABLE 1
#define TASK_UNDETACHABLE 0
-#ifdef KMP_USE_CANCEL_THREADS
-#define KMP_CANCEL_THREADS
-#endif
#define KMP_THREAD_ATTR
#if !KMP_OS_WASI
diff --git a/openmp/runtime/src/kmp_config.h.cmake b/openmp/runtime/src/kmp_config.h.cmake
index dfc17479b68de..27953c8eeb128 100644
--- a/openmp/runtime/src/kmp_config.h.cmake
+++ b/openmp/runtime/src/kmp_config.h.cmake
@@ -97,9 +97,7 @@
#cmakedefine01 LIBOMP_HAVE__ALIGNED_MALLOC
#define KMP_HAVE__ALIGNED_MALLOC LIBOMP_HAVE__ALIGNED_MALLOC
#cmakedefine01 LIBOMP_USE_CANCEL_THREADS
-#if LIBOMP_USE_CANCEL_THREADS
-# define KMP_USE_CANCEL_THREADS
-#endif
+#define KMP_CANCEL_THREADS LIBOMP_USE_CANCEL_THREADS
#cmakedefine01 OPENMP_ENABLE_LIBOMPTARGET
#define ENABLE_LIBOMPTARGET OPENMP_ENABLE_LIBOMPTARGET
diff --git a/openmp/runtime/src/z_Linux_util.cpp b/openmp/runtime/src/z_Linux_util.cpp
index 70a57c3c8f8a5..f42fc68488c91 100644
--- a/openmp/runtime/src/z_Linux_util.cpp
+++ b/openmp/runtime/src/z_Linux_util.cpp
@@ -446,7 +446,7 @@ void __kmp_terminate_thread(int gtid) {
if (!th)
return;
-#ifdef KMP_CANCEL_THREADS
+#if KMP_CANCEL_THREADS
KA_TRACE(10, ("__kmp_terminate_thread: kill (%d)\n", gtid));
status = pthread_cancel(th->th.th_info.ds.ds_thread);
if (status != 0 && status != ESRCH) {
@@ -558,7 +558,7 @@ static void *__kmp_launch_worker(void *thr) {
__kmp_affinity_bind_init_mask(gtid);
#endif
-#ifdef KMP_CANCEL_THREADS
+#if KMP_CANCEL_THREADS
status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type);
KMP_CHECK_SYSFAIL("pthread_setcanceltype", status);
// josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads?
@@ -636,7 +636,7 @@ static void *__kmp_launch_monitor(void *thr) {
__kmp_check_stack_overlap((kmp_info_t *)thr);
-#ifdef KMP_CANCEL_THREADS
+#if KMP_CANCEL_THREADS
status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type);
KMP_CHECK_SYSFAIL("pthread_setcanceltype", status);
// josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads?
@@ -1274,7 +1274,7 @@ void __kmp_remove_signals(void) {
#endif // KMP_HANDLE_SIGNALS
void __kmp_enable(int new_state) {
-#ifdef KMP_CANCEL_THREADS
+#if KMP_CANCEL_THREADS
int status, old_state;
status = pthread_setcancelstate(new_state, &old_state);
KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
@@ -1283,7 +1283,7 @@ void __kmp_enable(int new_state) {
}
void __kmp_disable(int *old_state) {
-#ifdef KMP_CANCEL_THREADS
+#if KMP_CANCEL_THREADS
int status;
status = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, old_state);
KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
>From 30452d6e70cd810226a767565ed1f73c711702ca Mon Sep 17 00:00:00 2001
From: Mark Zhuang <mark.zhuang at spacemit.com>
Date: Wed, 17 Jun 2026 22:32:04 +0800
Subject: [PATCH 3/3] [OpenMP] Drop Android/WASM special-casing for
KMP_CANCEL_THREADS
---
openmp/runtime/CMakeLists.txt | 13 +++----------
1 file changed, 3 insertions(+), 10 deletions(-)
diff --git a/openmp/runtime/CMakeLists.txt b/openmp/runtime/CMakeLists.txt
index 205d7efc10bf4..cc665475231a7 100644
--- a/openmp/runtime/CMakeLists.txt
+++ b/openmp/runtime/CMakeLists.txt
@@ -327,16 +327,9 @@ if(LIBOMP_STATS)
set(LIBOMP_USE_STDCPPLIB TRUE)
endif()
-# Thread cancellation via pthread_cancel. Platforms known to lack it at the
-# ABI level (Android, WASM) are forced off; others default to symbol
-# availability but can be overridden via -DLIBOMP_USE_CANCEL_THREADS=OFF for
-# cases where the build sysroot is more complete than the target runtime.
-if(ANDROID OR WASM)
- set(LIBOMP_USE_CANCEL_THREADS_DEFAULT FALSE)
-else()
- set(LIBOMP_USE_CANCEL_THREADS_DEFAULT "${LIBOMP_HAVE_PTHREAD_CANCEL}")
-endif()
-set(LIBOMP_USE_CANCEL_THREADS "${LIBOMP_USE_CANCEL_THREADS_DEFAULT}" CACHE BOOL
+# Thread cancellation via pthread_cancel. Defaults to whether the symbol is
+# available, but can be overridden via -DLIBOMP_USE_CANCEL_THREADS=OFF.
+set(LIBOMP_USE_CANCEL_THREADS "${LIBOMP_HAVE_PTHREAD_CANCEL}" CACHE BOOL
"Enable thread cancellation via pthread_cancel?")
# Shared library can be switched to a static library
More information about the Openmp-commits
mailing list