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

    <tr>
        <th>Summary</th>
        <td>
            [OpenMP] Device declarations are not used when applied to `extern` variables
        </td>
    </tr>

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

    <tr>
      <th>Assignees</th>
      <td>
            alexey-bataev
      </td>
    </tr>

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

<pre>
    OpenMP uses the `#pragma omp declare target` directive to control which variables are present on the device. Currently this cannot be used to mark any variable on the device unless it is applied to the first time that variable is seen. This can be seen in this godbolt example https://godbolt.org/z/aT9171xn6. In the first case, because the `#pragma omp declare target to(x)` clause is applied to an extern declaration of the variable `x`, OpenMP does not place it on the device. This can be seen due to the visibility begin `extern` and `x` being passed in as an argument to the GPU. However, if we instead apply it to the extern variable before the actual declaration, it will work fine.

This is currently preventing us from providing adequately wrapped headers when implementing system utilities on the GPU. I.e. we cannot do the following so there is no way to indicate that  `stderr` is on the GPU without including everything from the host's `stdio.h` on the GPU as well.
```c++
#include_next<stdio.h>

extern FILE *stderr;
#pragma omp declare target to(stderr)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVEFv4zYT_TX0ZRBBoWLZPviQTdbfF6BF97A9FyNqLM0uRarkyLL66wtKSuKkhxYQbIPWvHnvzfBhjNw4oqPaflFao6UrTXcVCtJFaa22zxscpPXh-KMdKgrlpvL1dPytJ_frNxgiRZCWQJW50kUfsOkQfNdDTcZiIBAMDYkqc6g5kBG-EIgH450Eb2Fs2bRwwcBYWYqQSvpAkZyAdzN0TRc2lMHTEAI5sRNIyxEMOucFKkok6oTZYfgJ6KY3uI8IMDhLMQILcATse8tLXXrlzCEKCHcE0qK8Q3CESOQy-L42TR3TCbBbiDS-rrwVoCt2vSVoRfqoikelT0qf1n8zHxqlT38pfcLvh_vd_dWV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