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

    <tr>
        <th>Summary</th>
        <td>
            [flang][OpenMP][OpenACC] The lowering for stop statement in a region is not supported yet
        </td>
    </tr>

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

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

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

<pre>
    ```
program m
  use omp_lib
  !$omp parallel num_threads(4)
    stop omp_get_thread_num()
  !$omp end parallel
end
```
```
$ bbc -emit-fir -fopenmp test.f90 && cat test.mlir
func.func @_QQmain() {
  %c4_i32 = arith.constant 4 : i32
  omp.parallel   num_threads(%c4_i32 : i32) {
    %0 = fir.call @omp_get_thread_num() : () -> i32
    %false = arith.constant false
    %false_0 = arith.constant false
    %1 = fir.call @_FortranAStopStatement(%0, %false, %false_0) : (i32, i1, i1) -> none
    fir.unreachable
  }
  return
}
```
The problem is the fir.unreachable in the parallel region. Change it into "omp.terminator" by hand:
```
func.func @_QQmain() {
  %c4_i32 = arith.constant 4 : i32
  omp.parallel   num_threads(%c4_i32 : i32) {
    %0 = fir.call @omp_get_thread_num() : () -> i32
    %false = arith.constant false
    %false_0 = arith.constant false
    %1 = fir.call @_FortranAStopStatement(%0, %false, %false_0) : (i32, i1, i1) -> none
    omp.terminator
  }
  return
}
```
```
tco test.mlir -o test.ll
$ clang++ -L/path-to-lib -lFortran_main -lFortranRuntime -lFortranDecimal -lomp test.ll 
$ ./a.out 
Fortran STOP: code 1

Fortran STOP: code 3

IEEE arithmetic exceptions signaled: INEXACT
IEEE arithmetic exceptions signaled: INEXACT
Fortran STOP: code 2

IEEE arithmetic exceptions signaled: INEXACT
Fortran STOP

IEEE arithmetic exceptions signaled:
```
This is expected.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llv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