Bug 105242 - [OpenMP] ICE with EXIT in collapsed loop: in gfc_trans_exit, at fortran/trans-stmt.cc:6147
Summary: [OpenMP] ICE with EXIT in collapsed loop: in gfc_trans_exit, at fortran/trans...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: ---
Assignee: Tobias Burnus
URL:
Keywords: accepts-invalid, diagnostic, openmp
Depends on:
Blocks:
 
Reported: 2022-04-12 16:43 UTC by Tobias Burnus
Modified: 2022-04-13 16:41 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-04-13 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2022-04-12 16:43:14 UTC
The following code (reduced code from a real-world project) gives an ICE but compiles with other vendors' compilers. It fails with GCC 10, 11 and trunk/12 with:

    8 |      if (kk == 7) exit
      |                      1
internal compiler error: in gfc_trans_exit, at fortran/trans-stmt.cc:6147
0x69734b gfc_trans_exit(gfc_code*)
        ../../repos/gcc/gcc/fortran/trans-stmt.cc:6147
0xa17360 trans_code
        ../../repos/gcc/gcc/fortran/trans.cc:1947
0xa9e4b5 gfc_trans_if_1
        ../../repos/gcc/gcc/fortran/trans-stmt.cc:1484
0xaa84cf gfc_trans_if(gfc_code*)
        ../../repos/gcc/gcc/fortran/trans-stmt.cc:1516
0xa17563 trans_code
        ../../repos/gcc/gcc/fortran/trans.cc:2004
0xa8ad58 gfc_trans_omp_code
        ../../repos/gcc/gcc/fortran/trans-openmp.cc:4399
0xa9a6aa gfc_trans_omp_do
        ../../repos/gcc/gcc/fortran/trans-openmp.cc:5381




!$omp target parallel do simd collapse(3)
do ii = i1, i2
 do jj = j1, j2
   do kk = k1, k2
     if (kk > 5) then
       k = 0
     end if
     if (kk == 7) exit
  end do
  end do
end do
!$omp end target parallel do simd
end
Comment 1 Tobias Burnus 2022-04-13 06:46:01 UTC
Actually, the EXIT does not make sense. There is:

Fortran
"final-loop-body must not contain any EXIT statement that would cause the termination of the5
innermost loop."

→ Accepts invalid for C/C++/Fortran

I thought it did work in C/C++, but I messed up.
Comment 2 Jakub Jelinek 2022-04-13 11:25:33 UTC
Why do you say accepts invalid for C/C++?
void
foo (int i1, int i2, int j1, int j2, int k1, int k2)
{
  int k = 1;
  #pragma omp target parallel for simd collapse(3) firstprivate (k)
  for (int ii = i1; ii <= i2; ++ii)
    for (int jj = j1; jj <= j2; ++jj)
      for (int kk = k1; kk <= k2; ++kk)
        {
          if (kk > 5)
            k = 0;
	  if (kk == 7)
	    break;
	}
}
is certainly rejected:
pr105242.c: In function ‘foo’:
pr105242.c:13:13: error: break statement used with OpenMP for loop
   13 |             break;
      |             ^~~~~
by both C and C++ (all the way back to GCC 6).
Comment 3 Tobias Burnus 2022-04-13 14:58:31 UTC
(In reply to Jakub Jelinek from comment #2)
> Why do you say accepts invalid for C/C++?

Because I messed up initially due to doing do much in parallel, found the issue, but forget to delete that line before hitting 'save'.

I have patch – the problem is that the check does exist, but only takes care of a small subset of directives.
Comment 4 Tobias Burnus 2022-04-13 15:30:44 UTC
Submitted patch:
  https://gcc.gnu.org/pipermail/gcc-patches/2022-April/593194.html
Comment 5 GCC Commits 2022-04-13 16:41:10 UTC
The master branch has been updated by Tobias Burnus <burnus@gcc.gnu.org>:

https://gcc.gnu.org/g:469fad0161afeb9369010ad498198297993ca592

commit r12-8145-g469fad0161afeb9369010ad498198297993ca592
Author: Tobias Burnus <tobias@codesourcery.com>
Date:   Wed Apr 13 18:40:52 2022 +0200

    OpenMP/Fortran: Fix EXIT in loop diagnostic [PR105242]
    
    gcc/fortran/ChangeLog:
    
            PR fortran/105242
            * match.cc (match_exit_cycle): Handle missing OMP LOOP, DO and SIMD
            directives in the EXIT/CYCLE diagnostic.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/105242
            * gfortran.dg/gomp/loop-exit.f90: New test.
Comment 6 Tobias Burnus 2022-04-13 16:41:46 UTC
FIXED on MAINLINE (= GCC 12)