See the updated patch in attachment and a few comments below.

On 16/07/2026 16:45, Tobias Burnus wrote:
Paul-Antoine Arras wrote:
Add GOMP_has_masked_thread_num_with_end, which is semantically identically but
meant for -fopenmp-ompt.

Which solves the issue I mentioned in my other email
- sorry for the belated review due to vaccation - namely:

For OMPT, libgomp needs to know whether to pass ompt_scope_beginend
or ompt_scope_begin followed by a later ompt_scope_end to the
registered tool.


gcc/ChangeLog:
    * omp-builtins.def (BUILT_IN_GOMP_HAS_MASKED_THREAD_NUM_WITH_END):
    New builtin.
    * omp-low.cc (lower_omp_master): Use it when -fopenmp-ompt.

libgomp/ChangeLog:
    * libgomp.map: Add GOMP_has_masked_thread_num_with_end.
    * libgomp.texi: Document it.
    * libgomp_g.h (GOMP_has_masked_thread_num_with_end): Declare.
    * parallel.c (GOMP_has_masked_thread_num_with_end): New function.

gcc/testsuite/ChangeLog:
    * c-c++-common/gomp/masked-1.c: Check new builtin is not emitted.
    * c-c++-common/gomp/masked-3.c: Check new builtin is emitted with
    -fopenmp-ompt.
...
@@ -9098,7 +9098,11 @@ lower_omp_master (gimple_stmt_iterator *gsi_p, omp_context *ctx)
    gsi_replace (gsi_p, bind, true);
-  bfn_decl = builtin_decl_explicit (BUILT_IN_GOMP_HAS_MASKED_THREAD_NUM);
+  if (flag_openmp_ompt)
+    bfn_decl
+      = builtin_decl_explicit (BUILT_IN_GOMP_HAS_MASKED_THREAD_NUM_WITH_END);
+  else
+    bfn_decl = builtin_decl_explicit (BUILT_IN_GOMP_HAS_MASKED_THREAD_NUM);

Elsewhere, you use such version:

   bfn_decl = builtin_decl_explicit (
                flag_openmp_ompt ? BUILT_IN_GOMP_HAS_MASKED_THREAD_NUM
                                : BUILT_IN_GOMP_HAS_MASKED_THREAD_NUM_WITH_END);

which seems to be tad more readable - however, not that it really
matters.

Agreed! Refactored in the updated patch.

--- a/libgomp/libgomp.texi
+++ b/libgomp/libgomp.texi
-@code{GOMP_has_masked_thread_num} encapsulates the following check, so that OMPT -can distinguish the compiler-generated code from user-level thread- number
-queries:
+@code{GOMP_has_masked_thread_num} (or its variant
+@code{GOMP_has_masked_thread_num_with_end} enabled by @option{- fopenmp-ompt})
+encapsulates the following check, so that OMPT can distinguish the
+compiler-generated code from user-level thread-number queries:

I think it is more readable and clearer (+ more details) to keep the
current wording and add a sentence, i.e.

@code{GOMP_has_masked_thread_num} encapsulates the following check, so that OMPT
can distinguish the compiler-generated code from user-level thread-number
queries; with @option{-fopenmp-ompt}, a paired call to
@code{GOMP_has_masked_thread_num_with_end} and @code{GOMP_masked_end} is
generated instead.

Rephrased as suggested.

Thanks,
--
PA
From 321f2981c4520b91241fa7c0c70e4294c706da98 Mon Sep 17 00:00:00 2001
From: Paul-Antoine Arras <[email protected]>
Date: Thu, 16 Jul 2026 15:06:16 +0200
Subject: [PATCH v2] openmp: Add OMPT variant of GOMP_has_masked_thread_num

Add GOMP_has_masked_thread_num_with_end, which is semantically identically but
meant for -fopenmp-ompt.

gcc/ChangeLog:

	* omp-builtins.def (BUILT_IN_GOMP_HAS_MASKED_THREAD_NUM_WITH_END):
	New builtin.
	* omp-low.cc (lower_omp_master): Use it when -fopenmp-ompt.

libgomp/ChangeLog:

	* libgomp.map: Add GOMP_has_masked_thread_num_with_end.
	* libgomp.texi: Document it.
	* libgomp_g.h (GOMP_has_masked_thread_num_with_end): Declare.
	* parallel.c (GOMP_has_masked_thread_num_with_end): New function.

gcc/testsuite/ChangeLog:

	* c-c++-common/gomp/masked-1.c: Check new builtin is not emitted.
	* c-c++-common/gomp/masked-3.c: Check new builtin is emitted with
	-fopenmp-ompt.
---
 gcc/omp-builtins.def                       | 3 +++
 gcc/omp-low.cc                             | 4 +++-
 gcc/testsuite/c-c++-common/gomp/masked-1.c | 1 +
 gcc/testsuite/c-c++-common/gomp/masked-3.c | 4 +++-
 libgomp/libgomp.map                        | 1 +
 libgomp/libgomp.texi                       | 4 +++-
 libgomp/libgomp_g.h                        | 1 +
 libgomp/parallel.c                         | 8 ++++++++
 8 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/gcc/omp-builtins.def b/gcc/omp-builtins.def
index 121bdcb61a3..29b3b72c2cd 100644
--- a/gcc/omp-builtins.def
+++ b/gcc/omp-builtins.def
@@ -508,6 +508,9 @@ DEF_GOMP_BUILTIN (BUILT_IN_GOMP_ERROR, "GOMP_error",
 DEF_GOMP_BUILTIN (BUILT_IN_GOMP_HAS_MASKED_THREAD_NUM,
 		  "GOMP_has_masked_thread_num", BT_FN_BOOL_INT,
 		  ATTR_CONST_NOTHROW_LEAF_LIST)
+DEF_GOMP_BUILTIN (BUILT_IN_GOMP_HAS_MASKED_THREAD_NUM_WITH_END,
+		  "GOMP_has_masked_thread_num_with_end", BT_FN_BOOL_INT,
+		  ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_GOMP_BUILTIN (BUILT_IN_GOMP_MASKED_END, "GOMP_masked_end", BT_FN_VOID,
 		  ATTR_NOTHROW_LEAF_LIST)
 /* TODO review these function attributes once OMPT implementation done  */
diff --git a/gcc/omp-low.cc b/gcc/omp-low.cc
index 50da8581a8d..f2ab425d965 100644
--- a/gcc/omp-low.cc
+++ b/gcc/omp-low.cc
@@ -9098,7 +9098,9 @@ 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_GOMP_HAS_MASKED_THREAD_NUM);
+  bfn_decl = builtin_decl_explicit (
+    flag_openmp_ompt ? BUILT_IN_GOMP_HAS_MASKED_THREAD_NUM
+		     : BUILT_IN_GOMP_HAS_MASKED_THREAD_NUM_WITH_END);
   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;
diff --git a/gcc/testsuite/c-c++-common/gomp/masked-1.c b/gcc/testsuite/c-c++-common/gomp/masked-1.c
index 28d2735d96e..92d005dbb9b 100644
--- a/gcc/testsuite/c-c++-common/gomp/masked-1.c
+++ b/gcc/testsuite/c-c++-common/gomp/masked-1.c
@@ -26,5 +26,6 @@ foo (int x, int *a)
 }
 
 /* { dg-final { scan-tree-dump "GOMP_has_masked_thread_num" "omplower" } } */
+/* { dg-final { scan-tree-dump-not "GOMP_has_masked_thread_num_with_end" "omplower" } } */
 /* { dg-final { scan-tree-dump-not "GOMP_masked_end" "omplower" } } */
 /* { dg-final { scan-tree-dump-not "omp_get_thread_num" "omplower" } } */
diff --git a/gcc/testsuite/c-c++-common/gomp/masked-3.c b/gcc/testsuite/c-c++-common/gomp/masked-3.c
index adea59fdb69..cb2209284c3 100644
--- a/gcc/testsuite/c-c++-common/gomp/masked-3.c
+++ b/gcc/testsuite/c-c++-common/gomp/masked-3.c
@@ -1,6 +1,8 @@
 /* { dg-do compile } */
 /* { dg-additional-options "-fopenmp-ompt -fdump-tree-omplower" } */
 
+/* Check that OMPT variants of libgomp calls are emitted.  */
+
 void bar (void);
 
 void
@@ -10,6 +12,6 @@ foo (void)
   bar ();
 }
 
-/* { dg-final { scan-tree-dump-times "GOMP_has_masked_thread_num" 1 "omplower" } } */
+/* { dg-final { scan-tree-dump-times "GOMP_has_masked_thread_num_with_end" 1 "omplower" } } */
 /* { dg-final { scan-tree-dump-times "GOMP_masked_end" 1 "omplower" } } */
 /* { dg-final { scan-tree-dump-not "omp_get_thread_num" "omplower" } } */
diff --git a/libgomp/libgomp.map b/libgomp/libgomp.map
index 3f465cc1491..81bad4d2329 100644
--- a/libgomp/libgomp.map
+++ b/libgomp/libgomp.map
@@ -485,6 +485,7 @@ GOMP_6.0.1 {
 GOMP_6.0.2 {
   global:
 	GOMP_has_masked_thread_num;
+	GOMP_has_masked_thread_num_with_end;
 	GOMP_masked_end;
 	GOMP_loop_static_worksharing;
 	GOMP_loop_static_worksharing_start;
diff --git a/libgomp/libgomp.texi b/libgomp/libgomp.texi
index 5d29da0984d..b6314b75aba 100644
--- a/libgomp/libgomp.texi
+++ b/libgomp/libgomp.texi
@@ -8029,7 +8029,9 @@ if (GOMP_has_masked_thread_num(thread_num))
 
 @code{GOMP_has_masked_thread_num} encapsulates the following check, so that OMPT
 can distinguish the compiler-generated code from user-level thread-number
-queries:
+queries; with @option{-fopenmp-ompt}, a paired call to
+@code{GOMP_has_masked_thread_num_with_end} and @code{GOMP_masked_end} is
+generated instead.
 
 @smallexample
 omp_get_thread_num() == @var{thread_num}
diff --git a/libgomp/libgomp_g.h b/libgomp/libgomp_g.h
index 1ea8123056e..be3e349a417 100644
--- a/libgomp/libgomp_g.h
+++ b/libgomp/libgomp_g.h
@@ -307,6 +307,7 @@ extern unsigned GOMP_parallel_reductions (void (*) (void *), void *, unsigned,
 extern bool GOMP_cancel (int, bool);
 extern bool GOMP_cancellation_point (int);
 extern bool GOMP_has_masked_thread_num (int);
+extern bool GOMP_has_masked_thread_num_with_end (int);
 extern void GOMP_masked_end (void);
 
 /* task.c */
diff --git a/libgomp/parallel.c b/libgomp/parallel.c
index bb522b39ecb..286798ff902 100644
--- a/libgomp/parallel.c
+++ b/libgomp/parallel.c
@@ -280,6 +280,14 @@ GOMP_has_masked_thread_num (int tid)
   return tid == gomp_thread ()->ts.team_id;
 }
 
+/* OMPT variant enabled by -fopenmp-ompt.  */
+
+bool
+GOMP_has_masked_thread_num_with_end (int tid)
+{
+  return tid == gomp_thread ()->ts.team_id;
+}
+
 /* Stub for OMPT callback enabled by -fopenmp-ompt.  */
 
 void
-- 
2.53.0

Reply via email to