Skip to content

Commit

Permalink
[OpenMP] Fix the issue where num_threads still takes effect incorre…
Browse files Browse the repository at this point in the history
…ctly

This patch fixes the issue that, if we have a compile-time serialized parallel
region (such as `if (0)`) with `num_threads`, followed by a regular parallel
region, the regular parallel region will pick up the value set in the serialized
parallel region incorrectly. The reason is, in the front end, if we can prove a
parallel region has to serialized, instead of emitting `__kmpc_fork_call`, the
front end directly emits `__kmpc_serialized_parallel`, body, and `__kmpc_end_serialized_parallel`.
However, this "optimization" doesn't consider the case where `num_threads` is
used such that `__kmpc_push_num_threads` is still emitted. Since we don't reset
the value in `__kmpc_serialized_parallel`, it will affect the next parallel region
followed by it.

Fix #63197.

Reviewed By: tlwilmar

Differential Revision: https://reviews.llvm.org/D152883
  • Loading branch information
shiltian committed Jun 14, 2023
1 parent 27fac4a commit 85592d3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions openmp/runtime/src/kmp_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,9 @@ void __kmp_serialized_parallel(ident_t *loc, kmp_int32 global_tid) {
// Reset for next parallel region
this_thr->th.th_set_proc_bind = proc_bind_default;

// Reset num_threads for next parallel region
this_thr->th.th_set_nproc = 0;

#if OMPT_SUPPORT
ompt_data_t ompt_parallel_data = ompt_data_none;
void *codeptr = OMPT_LOAD_RETURN_ADDRESS(global_tid);
Expand Down
17 changes: 17 additions & 0 deletions openmp/runtime/test/parallel/bug63197.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %libomp-compile-and-run | FileCheck %s

#include <omp.h>
#include <stdio.h>

int main(int argc, char *argv[]) {
#pragma omp parallel num_threads(3) if (false)
#pragma omp single
{ printf("BBB %2d\n", omp_get_num_threads()); }

#pragma omp parallel
#pragma omp single
{ printf("CCC %2d\n", omp_get_num_threads()); }
return 0;
}

// CHECK-NOT: CCC 3

0 comments on commit 85592d3

Please sign in to comment.