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

    <tr>
        <th>Summary</th>
        <td>
            flang complains about incorrect OpenMP reduction if variable is used before parallel block
        </td>
    </tr>

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

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

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

<pre>
    Originally reported at [R-package-devel](https://stat.ethz.ch/pipermail/r-package-devel/2023q4/009787.html) for 092b6c5ee3707ea10b9f10d0a674e8d12395369b, here's [the compilation log](http://web.archive.org/web/20231031154710/https://www.stats.ox.ac.uk/pub/bdr/clang17/frailtypack.log) and [the full source code](https://cran.r-project.org/src/contrib/Archive/frailtypack/frailtypack_3.5.0.tar.gz). Can also be reproduced for e107c9468b9c734fba016166fccc82a7e2b6527b (`main` at the time of writing).

I was able to reduce this to the following example:

```fortran
program main
    implicit none

 real :: sum = 0
    integer :: i, n = 10

    sum = sum + 1. ! <-- error here if followed by OpenMP reduction

    !$OMP PARALLEL DO default(none) PRIVATE(i) SHARED(n) REDUCTION(+:sum) SCHEDULE(Dynamic,1)
 do i=1,n
        sum = sum + real(i)
    end do
    !$OMP END PARALLEL DO

    print *, sum
end
```

```
% flang-new -c -o /dev/null src.f90 -fopenmp
error: loc("src.f90":7:5): 'omp.reduction' op must be used within an operation supporting reduction clause interface
error: verification of lowering to FIR failed
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJxsVVtv2zgT_TX0y8ACNbpZD35Q7AgNkDZBvvZ7XVDUyOaWIrUkFTf99QvKbi7eAIYFUnM558xFwnt1MERbVtywYr8SczhatxUqiGnV2f5l--DUQRmh9Qs4mqwL1IMIwIqbp_Uk5E9xoHVPz6RZsWe4OYYweZY1DFuGrQ8iJBSOvxN5ZNhOaiI3CqUZtu7KG1vkmP2TM2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