<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Target x86_64 RTL --> Target library loading error: undefined symbol with global constexpr T[]"
   href="https://bugs.llvm.org/show_bug.cgi?id=46530">46530</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Target x86_64 RTL --> Target library loading error: undefined symbol with global constexpr T[]
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>OpenMP
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Runtime Library
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>gcc.j.kelling@hzdr.de
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=23668" name="attach_23668" title="Preprocessed code, gz">attachment 23668</a> <a href="attachment.cgi?id=23668&action=edit" title="Preprocessed code, gz">[details]</a></span>
Preprocessed code, gz

* platform x86_64
* offload target x86_64
* clang/libomptarget version: master (cloned/build 20/06/31)

## Problem description:
When a global constexpr array is accessed through member operator/function
under some condition which are not yet clear to me, a symbol for the global
array is reported missing although no such symbol should generated.

## Example

In the attached code the following key point happen:
* the operator() defined at line 101196 is called in a target region.
* It accesses the global(in namespaces)
  constexpr double DriftParamIons_direction_data[]={0.0, 0.0, -1.0};
  defined at line 101181
* Element [2] of this constexpr array is printed two times:
  1. accessed directly as DriftParamIons_direction_data[2]
  2. through the operator[] provided by a local instance of the
     class ConstArrayStorage which is also defined in line 101181
     where the access to the array is hard-coded
     (line 101181 resulted from macro expansions)
* The printf is done by each thread.

### Expected result
Running the code, the last line on stdout should be:
-1.000000 -1.000000
(there will be one of these per thread and some junk before)

### Actual result
exit(1) is called with the error message:
```
Libomptarget fatal error 1: failure of target construct while offloading is
mandatory
```
using libomptarget build with debug enabled and setting LIBOMPTARGET_DEBUG=1
yields the following error message:
```
Target x86_64 RTL --> Target library loading error: /tmp/tmpfile_SsIv8d:
undefined symbol:
_ZN27pmacc_static_const_storage031pmacc_static_const_vector_host0L29DriftParamIons_direction_dataE
```
The missing symbol reads 
```
pmacc_static_const_storage0::pmacc_static_const_vector_host0::DriftParamIons_direction_data
```
which is the constexpr defined at line 101181 .

## Example Code

Attached is the preprocessed code described above, I could not find a small
reproducer by only implementing the properties outlined above. Running clang++
with -E appears to have duplicated code, which I guess is for offload? The code
in lines 101181ff reappears at 202434ff.

The attached code can be compiled with:
```
clang++  -fopenmp -fopenmp=libomp -fopenmp-targets=x86_64-pc-linux-gnu   
-fopenmp=libomp helloWorld.ii -lomp -o helloWorld
```
and ran using
```
./helloWorld
```
.

## Additional Observations

Applying the following diff:
```
101201c101201
<             , stor[2]
---
<span class="quote">>             , -1.</span >
202454c202454
<             , stor[2]
---
<span class="quote">>             , -1.</span >
```
removes the error and produces the expected result. Note, that this only
removes the indirect access to DriftParamIons_direction_data but keeps the
direct access, so the global constexpr array is not the issue.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>