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

    <tr>
        <th>Summary</th>
        <td>
            [Flang][OpenMP] Incorrect execution result when a threadprivate variable is defined using parallel construct in subroutine
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          ohno-fj
      </td>
    </tr>
</table>

<pre>
    ```
Version of flang-new : 18.0.0(76bbbcb41bcf4a1d7a26bb11b78cf97b60ea7d4b)
```

The value of a variable (declared in `threadprivate` clause) defined in `parallel` construct in subroutine is incorrect in the caller's `parallel` construct.

If subroutine is deployed to the caller, the result is correct. Attach the example program (sngg492a_3.f90) and its results to the end.

The following are the test program, Flang-new, Gfortran and ifort compilation/execution result.

sngg492a_2.f90:
```fortran
subroutine s1
 complex,save:: a1
!$omp threadprivate(a1)
  call set_th
  print *, 300,a1
!$omp parallel
  print *,301,a1
!$omp end parallel
contains
  subroutine set_th
!$omp parallel
    a1%re=1. + 2.
    a1%im=2. + 2.
!$omp end parallel
  end subroutine set_th
end subroutine s1

program main
  call s1
  print *,'pass'
end program main
```

```
$ export OMP_NUM_THREADS=2; flang-new -fopenmp sngg492a_2.f90; ./a.out
 300 (3.,4.)
 301 (3.,4.)
 301 (0.,0.)
 pass
$
```

```
$ export OMP_NUM_THREADS=2; gfortran -fopenmp sngg492a_2.f90; ./a.out
         300 (3.00000000,4.00000000)
         301 (3.00000000,4.00000000)
         301 (3.00000000,4.00000000)
 pass
$
```

```
$ export OMP_NUM_THREADS=2; ifort -qopenmp sngg492a_2.f90; ./a.out
         300 (3.000000,4.000000)
         301 (3.000000,4.000000)
         301 (3.000000,4.000000)
 pass
$
```

sngg492a_3.f90:
```fortran
subroutine s1
 complex,save:: a1
!$omp threadprivate(a1)
!$omp parallel
  a1%re=1. + 2.
  a1%im=2. + 2.
!$omp end parallel
  print *, 300,a1
!$omp parallel
  print *,301,a1
!$omp end parallel
end subroutine s1

program main
  call s1
  print *,'pass'
end program main
```

```
$ export OMP_NUM_THREADS=2; flang-new -fopenmp sngg492a_3.f90; ./a.out
 300 (3.,4.)
 301 (3.,4.)
 301 (3.,4.)
 pass
$
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcVkuP4zYM_jXKRYghUX4khxwyk6bdw3QX7bbXgWzTsRaK5EryzOy_L-TEeXge23Z3W6BCkEQk9ZH8RBGU3qudQVyR7IZkm5nsQ2vdyrbGzptPs9LWn1ckZ8cP2xC2_h2dV9ZQ29BGS7ObG3ykRKwpXyQsYQQWRV6WZVWmvKyaVPK6kJCXJedlsaiaZVHmDGVRpyWB5QFy4uHw_bFF-iB1j9GTpA_SKVlqpAQWNVZaOqypMpTkLLQOZd059SADkpzRSsveI4ElrbFR5mTYSSe1Rj3YWOOD66sQdb4vne2DMkiVp8pU1jk8qEKLtIqnHIHCv4qSXIb-rpkg1thp-xlrGuwV4O2wc-h7HaLd0W9C1yHIqh20-CT3nUbaObtzch_z92a3S5cg70XSLFnMU5qaquCPUH70g6ZOppw2Vmv7qMyOSoeDVUAfRvgY03a81rj5sbEuOGkOLuKGVnbfKS2DsobAFp-w6uP_o_Mrh6dIYYhUrCcXfgQ_Gp858_wgGnxpfCJw6-UDRgCxpvKoJcAJpHbf0esSgIXkp-KiA9vUY7gP7SjqnDKBEljHFAVjBG6fg54u-oVDgvEXz6CpJ-cqa4JUxo8ol1lexPS6WxrzhcwhERueUAI3FJKJUu2J2MC18q2g6CB7JZSpil_e6FiGe6nMNcH8JZ4IFJ30nkBxxn4O8WIDmAohpfjUxQJ8f_fh_uff7u4__vTLD-vNrzF1Im4u2tG8sR2afUen9XdDEwJbmdg-HKMVjMUnJRICt2lyrhrB-FtyFuXsQj5kOQb6LbPajQ_wbyQ1rlNy7LiGZM6b5TN7_l3svxs5h4Y0_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