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

    <tr>
        <th>Summary</th>
        <td>
            [OpenMP] Offloading returns incorrect value when compiled from a different file
        </td>
    </tr>

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

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

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

<pre>
    The following offloading program returns an incorrect value when run on my V100.
```
/// impl.c
#pragma omp begin declare target
int x = 1;

int function() {
#pragma omp parallel for
  for (int i = 0; i < 128; ++i) {
    x = 0;
  }
  return 0;
}
#pragma omp end declare target
```
```
// kernel.c
int function();

int main() {
  int x = 0;
#pragma omp target map(from : x)
  x = function();
  
  return x;
}
```
When compiled together we get incorrect results using regular compilation.
```
3n4:iguazu ~/llvm/llvm-project/build> clang impl.c kernel.c -fopenmp -fopenmp-targets=nvptx64 -O3
3n4:iguazu ~/llvm/llvm-project/build> ./a.out; echo $?
33
```
However, using LTO or manually linking the static library this seems fine.
```
3n4:iguazu ~/llvm/llvm-project/build> clang impl.c kernel.c -fopenmp -fopenmp-targets=nvptx64 -O3 -foffload-lto
3n4:iguazu ~/llvm/llvm-project/build> ./a.out; echo $?
0
```
This is most likely caused by a variable that is not shred correctly in the bitcode version of the runtime. We can try to identify this variable or move forward making LTO default / always using the static library.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNVclu2zAQ_Rr5MrAgS14POsRZ0EOL9BC0Z0oaSkwoUuDipV_foSXbcaNcChSoQcDDGc72ZlGhq2P-0iBwLaXeC1WD5lxqVgWyM7o2rAWDzhtlgSkQqtTGYOlgx6RH2DeowHgFWkF7hB-zJImj5CFK7qJlMpz-mj71B0Tbybg8c7POsLploNsOCqyFggpLyQyCY6ZG178TysEBouwBZlG2HXQvEu5V6YRWUbqO0g1Eq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