]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
openmp: Add GOMP_has_masked_thread_num
authorPaul-Antoine Arras <parras@baylibre.com>
Tue, 16 Jun 2026 15:54:54 +0000 (17:54 +0200)
committerPaul-Antoine Arras <parras@baylibre.com>
Wed, 24 Jun 2026 13:24:42 +0000 (15:24 +0200)
For the `omp master' and `omp masked filter(x)' constructs, the compiler lowers
the filter check by emitting omp_get_thread_num() == filter. Because
omp_get_thread_num is a public OpenMP API function, OMPT cannot distinguish this
internal check from a user-level thread-number query.

Introduce GOMP_has_masked_thread_num(int tid), a new libgomp entry point that
encapsulates the thread id check, and update lower_omp_master to call it instead
of building the comparison inline.

gcc/ChangeLog:

* omp-builtins.def (BUILT_IN_GOMP_HAS_MASKED_THREAD_NUM): New builtin.
* omp-low.cc (lower_omp_master): Call it.

libgomp/ChangeLog:

* libgomp.map (GOMP_6.0.2): New symbol version. Export
GOMP_has_masked_thread_num.
* libgomp_g.h (GOMP_has_masked_thread_num): New declaration.
* parallel.c (GOMP_has_masked_thread_num): New function.

gcc/testsuite/ChangeLog:

* c-c++-common/gomp/masked-1.c: Add scan directives verifying that
GOMP_has_masked_thread_num is emitted and omp_get_thread_num is not.
* g++.dg/gomp/master-3.C: Update scan-tree-dump-times to look for
GOMP_has_masked_thread_num instead of omp_get_thread_num.
* gcc.dg/gomp/master-3.c: Likewise.
* gfortran.dg/gomp/masked-1.f90: Add scan directive for
GOMP_has_masked_thread_num.

gcc/omp-builtins.def
gcc/omp-low.cc
gcc/testsuite/c-c++-common/gomp/masked-1.c
gcc/testsuite/g++.dg/gomp/master-3.C
gcc/testsuite/gcc.dg/gomp/master-3.c
gcc/testsuite/gfortran.dg/gomp/masked-1.f90
libgomp/libgomp.map
libgomp/libgomp_g.h
libgomp/parallel.c

index f5b16c5d29e69372063602854fcbdfbc433d0ed0..c7ddea9880adc8ccd5e106b5f2e8d21b7c78a861 100644 (file)
@@ -496,3 +496,6 @@ DEF_GOMP_BUILTIN (BUILT_IN_GOMP_WARNING, "GOMP_warning",
                  BT_FN_VOID_CONST_PTR_SIZE, ATTR_NOTHROW_LEAF_LIST)
 DEF_GOMP_BUILTIN (BUILT_IN_GOMP_ERROR, "GOMP_error",
                  BT_FN_VOID_CONST_PTR_SIZE, ATTR_COLD_NORETURN_NOTHROW_LEAF_LIST)
+DEF_GOMP_BUILTIN (BUILT_IN_GOMP_HAS_MASKED_THREAD_NUM,
+                 "GOMP_has_masked_thread_num", BT_FN_BOOL_INT,
+                 ATTR_CONST_NOTHROW_LEAF_LIST)
index 6e4cff502d22b3274e8bf0b2eb98957d8360d0a5..9ac0da173a514f7d5bc206a52ee8d7cce31cd488 100644 (file)
@@ -9068,9 +9068,8 @@ lower_omp_master (gimple_stmt_iterator *gsi_p, omp_context *ctx)
   gsi_replace (gsi_p, bind, true);
   gimple_bind_add_stmt (bind, stmt);
 
-  bfn_decl = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
-  x = build_call_expr_loc (loc, bfn_decl, 0);
-  x = build2 (EQ_EXPR, boolean_type_node, x, filter);
+  bfn_decl = builtin_decl_explicit (BUILT_IN_GOMP_HAS_MASKED_THREAD_NUM);
+  x = build_call_expr_loc (loc, bfn_decl, 1, filter);
   x = build3 (COND_EXPR, void_type_node, x, NULL, build_and_jump (&lab));
   tseq = NULL;
   gimplify_and_add (x, &tseq);
index 36c2e4928923cc75bf2ecbb6cacf878e2e731122..8de87b551e30212b5f0eaf3cd8cc915331dc51a9 100644 (file)
@@ -1,3 +1,6 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-fdump-tree-omplower" } */
+
 void bar (void);
 
 void
@@ -21,3 +24,6 @@ foo (int x, int *a)
   #pragma omp masked filter (x)
   ;
 }
+
+/* { dg-final { scan-tree-dump "GOMP_has_masked_thread_num" "omplower" } } */
+/* { dg-final { scan-tree-dump-not "omp_get_thread_num" "omplower" } } */
index c396128afdb0ac32bef589425d922ec4e7cc3610..3d27b936d64d8de05cf2df1fad5420556695e317 100644 (file)
@@ -9,4 +9,4 @@ void foo (void)
     bar(0);
 }
 
-/* { dg-final { scan-tree-dump-times "omp_get_thread_num" 1 "omplower" } } */
+/* { dg-final { scan-tree-dump-times "GOMP_has_masked_thread_num" 1 "omplower" } } */
index 5c0544dfa3cdaacb8950b310f6b77c61b7fe18dc..0d5836748ce8de9d28fb01217868bceaaf6271ee 100644 (file)
@@ -9,4 +9,4 @@ void foo (void)
     bar(0);
 }
 
-/* { dg-final { scan-tree-dump-times "omp_get_thread_num" 1 "ompexp" } } */
+/* { dg-final { scan-tree-dump-times "GOMP_has_masked_thread_num" 1 "ompexp" } } */
index 1bd61760ced2052e1b98d75fe17f45068990376a..2b2526ac7127287e81bbc3634e0bee80d5cee503 100644 (file)
@@ -1,4 +1,6 @@
-! { dg-additional-options "-ffree-line-length-none" }
+! { dg-do compile }
+! { dg-additional-options "-ffree-line-length-none -fdump-tree-omplower" }
+
 subroutine foo (x, a)
   implicit none
   integer, value :: x
@@ -92,3 +94,5 @@ subroutine foobar (d, f, fi, p, s, g, i1, i2, l, ll, nth, ntm, pp, q, r, r2)
     end do
   !$omp end parallel masked taskloop simd
 end subroutine
+
+! { dg-final { scan-tree-dump "GOMP_has_masked_thread_num" "omplower" } }
index dbde88c4d5f5486703612ba10b4884678fa3f3af..5faeb27b74b053eda56ebf36bff02adea49b926c 100644 (file)
@@ -482,6 +482,11 @@ GOMP_6.0.1 {
        omp_target_memset_async;
 } GOMP_6.0;
 
+GOMP_6.0.2 {
+  global:
+       GOMP_has_masked_thread_num;
+} GOMP_6.0.1;
+
 OACC_2.0 {
   global:
        acc_get_num_devices;
index 761c9bd594b9d1fd34d2cd0bada8b7c1d11ede25..d36d9d36020483fe17046b8578a0fcc9e2edffb5 100644 (file)
@@ -293,6 +293,7 @@ extern unsigned GOMP_parallel_reductions (void (*) (void *), void *, unsigned,
                                          unsigned);
 extern bool GOMP_cancel (int, bool);
 extern bool GOMP_cancellation_point (int);
+extern bool GOMP_has_masked_thread_num (int);
 
 /* task.c */
 
index fe7939ddcce2ff03748317361123f97d033fce44..175554de4c9a5f72ffe2664ef3be31eeb5110c3d 100644 (file)
@@ -270,6 +270,15 @@ GOMP_cancel (int which, bool do_cancel)
   gomp_team_barrier_cancel (team);
   return true;
 }
+
+/* Return true if the current thread number equals TID.
+   Used to implement the masked construct's filter clause.  */
+
+bool
+GOMP_has_masked_thread_num (int tid)
+{
+  return tid == gomp_thread ()->ts.team_id;
+}
 \f
 /* The public OpenMP API for thread and team related inquiries.  */