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

    <tr>
        <th>Summary</th>
        <td>
            Flang OpenMP default(private) clause fails to implicitly set the data-sharing of variables to private.
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          scamp-nvidia
      </td>
    </tr>
</table>

<pre>
    We have found that the "default(private)" clause gets ignored by the compiler when it is used on a "parallel" construct that contains a "single" construct with a "copyprivate" clause. This clause should not be ignored. Here are the relevant rules from the OpenMP 6.0 specification:

> 210:23 7.1.1 Variables Referenced in a Construct
> 213:15 * In a parallel, teams, or task-generating construct, the data-sharing attributes of these
> 213:16 variables are determined by the default clause, if present (see Section 7.5.1).

> 223:3  7.5.1 default Clause
> 223:12 The default clause determines the implicitly determined data-sharing attributes of certain
> 223:13 variables that are referenced in the construct, in accordance with the rules given in Section 7.1.1.
> 224:1  If data-sharing-attribute is not none, the data-sharing attributes of the selected variables will be
> 224:2 data-sharing-attribute.

We attach a simple reproducer that shows the error for scalar variables, but we have also seen it for pointers. 

test.F90:
```
Program main
  implicit none
  integer :: x
#if defined(USE_PRIVATE)
  !Explicitly set the data-sharing of x to be private
 !$omp parallel private(x) default(none)
#else
  !Implicitly obtain the data-sharing of x - see rule 213:15-16
  !$omp parallel default(private)
#endif
  !$omp single
  x = 1
  !$omp end single copyprivate(x)
  if (x == 1) then
     print *, "SUCCESS"
  else
 print *, "FAILURE"
  end if
  !$omp end parallel
End Program main
```

So compiling the code normally, without declaring "USE_PRIVATE", we get: 
```
scamp@dev-sky5:~$ flang --version
flang version 21.0.0git (https://github.com/llvm/llvm-project db5e4016c0332e7e5c0950414bb0b252975663ed)
Target: x86_64-unknown-linux-gnu
Thread model: posix
...
scamp@dev-sky5:~$ flang test.F90 -o test -fopenmp
flang-21: warning: OpenMP support in flang is still experimental [-Wexperimental-option]
error: Semantic errors in test.F90
./test.F90:13:32: error: COPYPRIVATE variable 'x' is not PRIVATE or THREADPRIVATE in outer context
    !$omp end single copyprivate(x)
 ^
```
Whereas, if we compile with "-DUSE_PRIVATE", we get successful compilations. We believe this code should work without the "-DUSE_PRIVATE", however. We also identified this as a bug in gfortran 14.1.0. 
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVk1v4zjS_jX0pSBBoix_HHzQJDYmwPtiGp30BHsaUFJJ4jZNCiRlO5f97YuiPuKku3d3AANRyGJ9PvVUCedkqxEPLP-N5Y8rMfjO2IOrxLmP9EXWUqxKU78dXhE6cUFozKBr8J3w4DsExnmNjRiUZ3zXW3kRHhnfM86hUmJwCC16B7LVxmIN5Vt4VZlzLxVauHaoQXqQDgaHNRgNgnT2wgqlUAU9Rjtvh8qPViujvZDajYJO6lbhR7Gr9N14W5n-bXFq9iiGl0662T3XmUHVoI2HEmc_Y_gdLYKwGNy1qPAitAc7KHTQWHMO53_0qP__C2ziBFyPlWxkJbw0mmUFS8IvOwJPE5YVPINtnMYp_CmsFCWp-YoNWtQV1iAp7Ic5gOVhxrIizYHxAp5I4j0rD-BRnB19GAteuO9Rixqt8FK376kIgh1CLbyIXCcs3QrvrSwHjw5MQ9cOP1rcwGVxklJQo0d7lvq9flPJpxySFdlAb9Gh9sD4ziHCM1aUC9jGeZwyvo8nI7MtTrYyGO8XjQ-jxnuZlMPLDzbfnXLBI3nulaykV2_37v6HuCu0BKOPlrK7yAPYKHz7oU4jfO_yS7WrKmNroSscsRcwE6DSygsBXN9lI43TeLG6JqsAT80HV6PFVWoMgqY2Gv-3YoJDhZXH-i6Uq1QKSvxglf_CYjwi9xVJtaiokRwll_LQW1MPFdoxN64z1zH5aK2x0BgLrhJK2HfT5HM5eLhO7CGUM-BwbHp60BupPVoXw2jXo_PxaZ9MLbRJpl9SfLGmteIM57FqsJR8TE440R5btECPswJupIFnsiHsEB4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