<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/139776>139776</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [flang][openacc] Flang hangs and continuously allocates memory when compiling gfortran test: goacc/loop-3.f95
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          tarunprabhu
      </td>
    </tr>
</table>

<pre>
    Steps to reproduce:

Assuming that the [LLVM test suite](https://github.com/llvm/llvm-test-suite) is checked out in `./llvm-test-suite`, compile the test as follows:

```
$ flang -fopenacc ./llvm-test-suite/Fortran/gfortran/regression/goacc/loop-3.f95
```

Observed behavior: Some warning messages are printed following which flang hangs and continuously allocates memory until either manually killed or an OOM error occurs. This was run with `/usr/bin/time -v` and terminated with `SIGTERM` after approx. 1 minute. The output in this case is provided below. Note the maximum resident set size and the number of page faults. 

```
./llvm-test-suite/Fortran/gfortran/regression/goacc/loop-3.f95:18:9: warning: Misplaced OpenACC end directive
    !$acc end loop ! { dg-error "Unexpected" }
 ^^^^^^^^
./llvm-test-suite/Fortran/gfortran/regression/goacc/loop-3.f95:25:9: warning: Misplaced OpenACC end directive
    !$acc end loop ! { dg-error "Unexpected" }
 ^^^^^^^^
./llvm-test-suite/Fortran/gfortran/regression/goacc/loop-3.f95:32:9: warning: Misplaced OpenACC end directive
    !$acc end loop ! { dg-error "Unexpected" }
          ^^^^^^^^
flang-21: error: unable to execute command: Terminated
flang-21: error: flang frontend command failed due to signal (use -v to see invocation)
flang version 21.0.0git (git@github.com:llvm-project-tlp/llvm-project.git 84c1564d1825880d463d9f85153812f1f6805289)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/bin
Build config: +assertions
flang-21: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
flang-21: note: diagnostic msg: /tmp/loop-3-2b0785
flang-21: note: diagnostic msg: /tmp/loop-3-2b0785.sh
flang-21: note: diagnostic msg: 

********************
Command exited with non-zero status 254
        Command being timed: "/path/to/bin/flang -fopenacc ./llvm-test-suite/Fortran/gfortran/regression/goacc/loop-3.f95"
        User time (seconds): 18.04
        System time (seconds): 53.96
        Percent of CPU this job got: 99%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 1:12.41
        Average shared text size (kbytes): 0
        Average unshared data size (kbytes): 0
        Average stack size (kbytes): 0
        Average total size (kbytes): 0
        Maximum resident set size (kbytes): 93215748
        Average resident set size (kbytes): 0
        Major (requiring I/O) page faults: 3
        Minor (reclaiming a frame) page faults: 23283575
        Voluntary context switches: 178
        Involuntary context switches: 96
        Swaps: 0
        File system inputs: 0
        File system outputs: 0
        Socket messages sent: 0
        Socket messages received: 0
        Signals delivered: 0
        Page size (bytes): 4096
        Exit status: 254

```

What follows is a minimal section of code from the original test that seems to reproduce this behavior.

```fortran
subroutine test1
  implicit none
  integer :: i, j

  outer_loop1: do i = 1, 5
    !$acc loop
    inner_loop1: do j = 1,5
      if (i .eq. j) cycle outer_loop1 ! { dg-error "CYCLE statement" }
    end do inner_loop1
  end do outer_loop1
end subroutine test1
```

Another gfortran test, `goacc/loop-3-2.f95` also exhibits similar behavior.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzcWF9v2zgS_zTMy8CGRFm2_OAH5Y-7AdJN0KS7uKcFTY0kJhSpJSk72U9_GEpOkzTtddE9HHCGAAvkDDl_yN_8RsJ71RjEDctPWX5-IobQWrcJwg2md2LXDic7Wz1tbgP2HoIFh72z1SCRZSVL6Cm9HzplGgitCBBaBJafXl399hEC-gB-UAFZfs540YbQe9LjW8a3jQrtsJtL2zG-1Xp__JuR2mxU42tQHmSL8gErsEMAZYAtk_k7ssuE8TOQtuuVxmhH3F94qK3W9uCfLSbR8UlKxhdQa2EamNW2RyOkhP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