On 03/11/2025 21:20, Tobias Burnus wrote:
Paul-Antoine Arras wrote:
Here is a revamped patch as well as some inline replies.
Thanks,
On 31/10/2025 17:41, Tobias Burnus wrote:
Error: Label 4567 referenced at (1) is never defined

Added testcase. Fixed in the attached patch.

Can you also add a test case like
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122508#c1

Namely, testing that a label of an outer scope
that get redefined in an inner scope will print
an error? Like the one in comment 1 - or a bit
refined:

Like:

1234 ...
!$omp metadirective ...
    5678 ...
    !$omp metadirective ...
       1234 ... ! re-used outerside one
       5678 ... ! re-used outer one

What about the attached testcase?
I get the following diagnostics:

```
   17 |     1345 print *, 'nested'
      |                          1
Error: Label 1345 at (1) already referenced as a format label
/home/parras/dev/openacc-mainline/src/gcc-mainline/gcc/testsuite/gfortran.dg/gomp/pr122508-2.f90:17:26:

   17 |     1345 print *, 'nested'
      |                          1
Error: Label 1345 at (1) already referenced as a format label
/home/parras/dev/openacc-mainline/src/gcc-mainline/gcc/testsuite/gfortran.dg/gomp/pr122508-2.f90:21:15:

   21 |   write(*,6789) cnt
      |               1
Error: Label 6789 at (1) previously used as branch target
/home/parras/dev/openacc-mainline/src/gcc-mainline/gcc/testsuite/gfortran.dg/gomp/pr122508-2.f90:20:15:

   20 |   write(*,1345) cnt
      |               1
Error: Label 1345 at (1) previously used as branch target
/home/parras/dev/openacc-mainline/src/gcc-mainline/gcc/testsuite/gfortran.dg/gomp/pr122508-2.f90:21:15:

   21 |   write(*,6789) cnt
      |               1
Error: Label 6789 at (1) previously used as branch target
/home/parras/dev/openacc-mainline/src/gcc-mainline/gcc/testsuite/gfortran.dg/gomp/pr122508-2.f90:20:15:

   20 |   write(*,1345) cnt
      |               1
Error: Label 1345 at (1) previously used as branch target
/home/parras/dev/openacc-mainline/src/gcc-mainline/gcc/testsuite/gfortran.dg/gomp/pr122508-2.f90:21:15:

   21 |   write(*,6789) cnt
      |               1
Error: Label 6789 at (1) previously used as branch target
```

Are those the expected diagnostics?
If yes, we still have a duplication problem due to several copies of the same label coexisting in different metadirective regions.

* * *

The attached patch should now incorporate your draft and handle PR122508 as well.

Thanks :-)


Subject: [PATCH] OpenMP/Fortran: Revamp handling of labels in metadirectives
  [PR122369,PR122508]

When a label is matched in the first statement after the end of a metadirective body, it is bound to the associated region. However this prevents it from being
referenced elsewhere.
This patch fixes it by rebinding such labels to the outer region. It also
ensures that labels defined in an outer region can be referenced in a
metadirective body.

    PR fortran/122369
    PR fortran/122508

gcc/fortran/ChangeLog:
    * gfortran.h (gfc_rebind_label): Declare new function.
    * parse.cc (parse_omp_metadirective_body): Rebind labels to the outer
    region. Maintain a vector of metadirective regions.
    (gfc_parse_file): Initialise it.
    * parse.h (GFC_PARSE_H): Declare it.
    * symbol.cc (gfc_get_st_label): Look for existing labels in outer
    metadirective regions.
    (gfc_rebind_label): Define new function.
    (gfc_define_st_label): Accept duplicate labels in metadirective body.
    (gfc_reference_st_label): Accept shared DO termination labels in
    metadirective body.

gcc/testsuite/ChangeLog:

    * gfortran.dg/gomp/pr122369-1.f90: New test.
    * gfortran.dg/gomp/pr122369-2.f90: New test.
    * gfortran.dg/gomp/pr122369-3.f90: New test.
    * gfortran.dg/gomp/pr122369-4.f90: New test.
    * gfortran.dg/gomp/pr122508-1.f90: New test.


The patch LGTM – but please add an dg-error test case on top, as suggested above. (Could also be a separate follow-up commit.)

Thanks,

Tobias



--
PA
! { dg-do compile }
! { dg-additional-options "-Wunused-label" }

! Check that redefining labels across metadirective regions triggers a
! diagnostic.

implicit none
integer :: cnt
1345 format("The count is ", g0)

cnt = 0
write(*,1345) cnt

!$omp begin metadirective when(user={condition(cnt > 0)} : parallel)
  6789 format("The count is ", g0)
  !$omp begin metadirective when(user={condition(cnt > 0)} : parallel)
    1345 print *, 'nested'
    6789 print *, 'world'
  !$omp end metadirective
  write(*,1345) cnt
  write(*,6789) cnt
!$omp end metadirective
end

Reply via email to