[Openmp-commits] [PATCH] D152883: [OpenMP] Fix the issue where `num_threads` still takes effect incorrectly

Shilei Tian via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Tue Jun 13 19:35:30 PDT 2023


tianshilei1992 created this revision.
tianshilei1992 added reviewers: jdoerfert, tlwilmar, jlpeyton.
Herald added subscribers: sunshaoce, guansong, yaxunl.
Herald added a project: All.
tianshilei1992 requested review of this revision.
Herald added subscribers: openmp-commits, jplehr, sstefan1.
Herald added a project: OpenMP.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152883

Files:
  openmp/runtime/src/kmp_runtime.cpp
  openmp/runtime/test/parallel/bug63197.c


Index: openmp/runtime/test/parallel/bug63197.c
===================================================================
--- /dev/null
+++ 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
Index: openmp/runtime/src/kmp_runtime.cpp
===================================================================
--- openmp/runtime/src/kmp_runtime.cpp
+++ openmp/runtime/src/kmp_runtime.cpp
@@ -1153,6 +1153,9 @@
   // 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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152883.531153.patch
Type: text/x-patch
Size: 1085 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20230614/acf594af/attachment.bin>


More information about the Openmp-commits mailing list