openmp: Add OMPT variant of GOMP_has_masked_thread_num

Message ID 20260716131227.4182453-1-parras@baylibre.com
State New
Headers
Series openmp: Add OMPT variant of GOMP_has_masked_thread_num |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed

Commit Message

Paul-Antoine Arras July 16, 2026, 1:12 p.m. UTC
  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                             | 6 +++++-
 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                       | 7 ++++---
 libgomp/libgomp_g.h                        | 1 +
 libgomp/parallel.c                         | 8 ++++++++
 8 files changed, 26 insertions(+), 5 deletions(-)
  

Comments

Tobias Burnus July 16, 2026, 2:45 p.m. UTC | #1
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.

> --- 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.

Otherwise, LGTM.

Thanks,

Tobias
  
Paul-Antoine Arras July 16, 2026, 5:34 p.m. UTC | #2
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,
  
Thomas Schwinge July 16, 2026, 9:28 p.m. UTC | #3
Hi!

On 2026-07-16T19:34:51+0200, Paul-Antoine Arras <parras@baylibre.com> wrote:
> 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: [...]

We've managed to keep things under control, and moving forward.

>>> @@ -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/gcc/omp-low.cc
> +++ b/gcc/omp-low.cc
> @@ -9098,7 +9098,9 @@ lower_omp_master (gimple_stmt_iterator *gsi_p, omp_context *ctx)

> -  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);

Please test your patches; v1 vs. v2:

    PASS: c-c++-common/gomp/masked-1.c (test for excess errors)
    PASS: c-c++-common/gomp/masked-1.c scan-tree-dump omplower "GOMP_has_masked_thread_num"
    [-PASS:-]{+FAIL:+} c-c++-common/gomp/masked-1.c scan-tree-dump-not omplower "GOMP_has_masked_thread_num_with_end"
    PASS: c-c++-common/gomp/masked-1.c scan-tree-dump-not omplower "GOMP_masked_end"
    PASS: c-c++-common/gomp/masked-1.c scan-tree-dump-not omplower "omp_get_thread_num"

    PASS: c-c++-common/gomp/masked-3.c (test for excess errors)
    [-PASS:-]{+FAIL:+} c-c++-common/gomp/masked-3.c scan-tree-dump-times omplower "GOMP_has_masked_thread_num_with_end" 1
    PASS: c-c++-common/gomp/masked-3.c scan-tree-dump-times omplower "GOMP_masked_end" 1
    PASS: c-c++-common/gomp/masked-3.c scan-tree-dump-not omplower "omp_get_thread_num"

Similar for C++.


Sorry if I sound grumpy, but I am; such things are a waste of time.


Grüße
 Thomas
  
Paul-Antoine Arras July 17, 2026, 9:07 a.m. UTC | #4
On 16/07/2026 23:28, Thomas Schwinge wrote:
>> --- 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)
>> -  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);
> Please test your patches; v1 vs. v2:
> 
>      PASS: c-c++-common/gomp/masked-1.c (test for excess errors)
>      PASS: c-c++-common/gomp/masked-1.c scan-tree-dump omplower "GOMP_has_masked_thread_num"
>      [-PASS:-]{+FAIL:+} c-c++-common/gomp/masked-1.c scan-tree-dump-not omplower "GOMP_has_masked_thread_num_with_end"
>      PASS: c-c++-common/gomp/masked-1.c scan-tree-dump-not omplower "GOMP_masked_end"
>      PASS: c-c++-common/gomp/masked-1.c scan-tree-dump-not omplower "omp_get_thread_num"
> 
>      PASS: c-c++-common/gomp/masked-3.c (test for excess errors)
>      [-PASS:-]{+FAIL:+} c-c++-common/gomp/masked-3.c scan-tree-dump-times omplower "GOMP_has_masked_thread_num_with_end" 1
>      PASS: c-c++-common/gomp/masked-3.c scan-tree-dump-times omplower "GOMP_masked_end" 1
>      PASS: c-c++-common/gomp/masked-3.c scan-tree-dump-not omplower "omp_get_thread_num"
> 
> Similar for C++.
> 
> 
> Sorry if I sound grumpy, but I am; such things are a waste of time.

Fixed in v3.
  
Thomas Schwinge July 17, 2026, 9:59 a.m. UTC | #5
Hi!

On 2026-07-17T11:07:55+0200, Paul-Antoine Arras <parras@baylibre.com> wrote:
> Subject: [PATCH v3] openmp: Add OMPT variant of GOMP_has_masked_thread_num

Thanks, looks good to me!  Tobias, any further comments?


Grüße
 Thomas


> 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..ea140224fd9 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_WITH_END
> +		     : 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;
> 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
  
Tobias Burnus July 17, 2026, 10:24 a.m. UTC | #6
Thomas Schwinge wrote:
> On 2026-07-17T11:07:55+0200, Paul-Antoine Arras <parras@baylibre.com> wrote:
>> Subject: [PATCH v3] openmp: Add OMPT variant of GOMP_has_masked_thread_num
> Thanks, looks good to me!  Tobias, any further comments?

No LGTM – and sorry for mixing up the order in '? :' in my previous 
review comment.

Tobias
  

Patch

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..b99d072f753 100644
--- a/gcc/omp-low.cc
+++ b/gcc/omp-low.cc
@@ -9098,7 +9098,11 @@  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);
+  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);
   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..9f80e94a48a 100644
--- a/libgomp/libgomp.texi
+++ b/libgomp/libgomp.texi
@@ -8027,9 +8027,10 @@  if (GOMP_has_masked_thread_num(thread_num))
   block
 @end smallexample
 
-@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:
 
 @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