diff --git a/openmp/runtime/src/kmp_runtime.cpp b/openmp/runtime/src/kmp_runtime.cpp index a242049286a74..c715a57d23e66 100644 --- a/openmp/runtime/src/kmp_runtime.cpp +++ b/openmp/runtime/src/kmp_runtime.cpp @@ -4431,8 +4431,10 @@ kmp_info_t *__kmp_allocate_thread(kmp_root_t *root, kmp_team_t *team, #endif KMP_MB(); - /* first, try to get one from the thread pool */ - if (__kmp_thread_pool) { + /* first, try to get one from the thread pool unless allocating thread is + * the main hidden helper thread. The hidden helper team should always + * allocate new OS threads. */ + if (__kmp_thread_pool && !KMP_HIDDEN_HELPER_TEAM(team)) { new_thr = CCAST(kmp_info_t *, __kmp_thread_pool); __kmp_thread_pool = (volatile kmp_info_t *)new_thr->th.th_next_pool; if (new_thr == __kmp_thread_pool_insert_pt) { @@ -4497,7 +4499,7 @@ kmp_info_t *__kmp_allocate_thread(kmp_root_t *root, kmp_team_t *team, } /* no, well fork a new one */ - KMP_ASSERT(__kmp_nth == __kmp_all_nth); + KMP_ASSERT(KMP_HIDDEN_HELPER_TEAM(team) || __kmp_nth == __kmp_all_nth); KMP_ASSERT(__kmp_all_nth < __kmp_threads_capacity); #if KMP_USE_MONITOR diff --git a/openmp/runtime/test/tasking/hidden_helper_task/issue-87117.c b/openmp/runtime/test/tasking/hidden_helper_task/issue-87117.c new file mode 100644 index 0000000000000..23080982f49e1 --- /dev/null +++ b/openmp/runtime/test/tasking/hidden_helper_task/issue-87117.c @@ -0,0 +1,36 @@ +// RUN: %libomp-compile +// RUN: env KMP_HOT_TEAMS_MODE=0 KMP_HOT_TEAMS_MAX_LEVEL=1 %libomp-run +// +// Force the defaults of: +// KMP_HOT_TEAMS_MODE=0 means free extra threads after parallel +// involving non-hot team +// KMP_HOT_TEAMS_MAX_LEVEL=1 means only the initial outer team +// is a hot team. + +#include +#include +#include + +int main() { + int a; + omp_set_max_active_levels(2); +// This nested parallel creates extra threads on the thread pool +#pragma omp parallel num_threads(2) + { +#pragma omp parallel num_threads(2) + { +#pragma omp atomic + a++; + } + } + +// Causes assert if hidden helper thread tries to allocate from thread pool +// instead of creating new OS threads +#pragma omp parallel num_threads(1) + { +#pragma omp target nowait + { a++; } + } + + return EXIT_SUCCESS; +}