[Openmp-commits] [openmp] 85592d3 - [OpenMP] Fix the issue where `num_threads` still takes effect incorrectly

Shilei Tian via Openmp-commits openmp-commits at lists.llvm.org
Wed Jun 14 08:46:19 PDT 2023


Author: Shilei Tian
Date: 2023-06-14T11:46:12-04:00
New Revision: 85592d3d4d402b99df32bcc711bca56a8a593c97

URL: https://github.com/llvm/llvm-project/commit/85592d3d4d402b99df32bcc711bca56a8a593c97
DIFF: https://github.com/llvm/llvm-project/commit/85592d3d4d402b99df32bcc711bca56a8a593c97.diff

LOG: [OpenMP] Fix the issue where `num_threads` still takes effect incorrectly

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

Added: 
    openmp/runtime/test/parallel/bug63197.c

Modified: 
    openmp/runtime/src/kmp_runtime.cpp

Removed: 
    


################################################################################
diff  --git a/openmp/runtime/src/kmp_runtime.cpp b/openmp/runtime/src/kmp_runtime.cpp
index 54d280534a33f..03956acdcb055 100644
--- a/openmp/runtime/src/kmp_runtime.cpp
+++ b/openmp/runtime/src/kmp_runtime.cpp
@@ -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);

diff  --git a/openmp/runtime/test/parallel/bug63197.c b/openmp/runtime/test/parallel/bug63197.c
new file mode 100644
index 0000000000000..8883443783b7a
--- /dev/null
+++ b/openmp/runtime/test/parallel/bug63197.c
@@ -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


        


More information about the Openmp-commits mailing list