[OpenMP] libomp: fix UB when LIBOMP_NUM_HIDDEN_HELPER_THREADS=1.

The __kmp_hidden_helper_threads_num set to N+1 if user requested N threads.
Thus number of worker hidden helper threads corresponds to user request,
main thread of helper team excluded as it does not participate in actual work.
This also fixes divide-by-0 issue in the code.

Fixes #48656

Differential Revision: https://reviews.llvm.org/D119586
This commit is contained in:
AndreyChurbanov 2022-02-12 03:00:38 +03:00
parent 7fbabe6ee4
commit cb1bee4725
2 changed files with 34 additions and 1 deletions

View File

@ -1245,13 +1245,25 @@ static void __kmp_stg_parse_num_hidden_helper_threads(char const *name,
// task
if (__kmp_hidden_helper_threads_num == 0) {
__kmp_enable_hidden_helper = FALSE;
} else {
// Since the main thread of hidden helper team dooes not participate
// in tasks execution let's increment the number of threads by one
// so that requested number of threads do actual job.
__kmp_hidden_helper_threads_num++;
}
} // __kmp_stg_parse_num_hidden_helper_threads
static void __kmp_stg_print_num_hidden_helper_threads(kmp_str_buf_t *buffer,
char const *name,
void *data) {
__kmp_stg_print_int(buffer, name, __kmp_hidden_helper_threads_num);
if (__kmp_hidden_helper_threads_num == 0) {
__kmp_stg_print_int(buffer, name, __kmp_hidden_helper_threads_num);
} else {
KMP_DEBUG_ASSERT(__kmp_hidden_helper_threads_num > 1);
// Let's exclude the main thread of hidden helper team and print
// number of worker threads those do actual job.
__kmp_stg_print_int(buffer, name, __kmp_hidden_helper_threads_num - 1);
}
} // __kmp_stg_print_num_hidden_helper_threads
static void __kmp_stg_parse_use_hidden_helper(char const *name,

View File

@ -0,0 +1,21 @@
// RUN: %libomp-compile && env LIBOMP_NUM_HIDDEN_HELPER_THREADS=1 %libomp-run
// The test checks that "devide-by-0" bug fixed in runtime.
// The fix is to increment number of threads by 1 if positive,
// so that operation
// (gtid) % (__kmp_hidden_helper_threads_num - 1)
// does not cause crash.
#include <stdio.h>
#include <omp.h>
int main(){
#pragma omp target nowait
{
printf("----- in target region\n");
}
printf("------ before taskwait\n");
#pragma omp taskwait
printf("passed\n");
return 0;
}