<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 - [amdgpu] const globals incorrectly marked addrspace(4)"
   href="https://bugs.llvm.org/show_bug.cgi?id=52511">52511</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[amdgpu] const globals incorrectly marked addrspace(4)
          </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>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Clang Compiler Support
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>jonathanchesterfield@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Given

#include <complex>
#include <cmath>
#include <omp.h>

#pragma omp declare target

static const double smallx = 1.0e-5;

static const double log_smallx = log(smallx);

#pragma omp end declare target


clang++ -O2 -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa   math_exp.cpp -o
math_exp  -lm -save-temps

unhandled address space
UNREACHABLE executed at
/home/amd/llvm-project/llvm/lib/Target/AMDGPU/SIISelLowering.cpp:8869!
PLEASE submit a bug report to <a href="https://bugs.llvm.org/">https://bugs.llvm.org/</a> and include the crash
backtrace.

IR emitted from clang with -disable-llvm-passes contains a global constructor
to do the log call,

define internal void @__omp_offloading__10303_4068b_log_smallx_l9_ctor() #4 {
entry:
  %call = call double @"_ZL28log$ompvariant$S2$s7$Pamdgcnd"(double
1.000000e-05) #17
  store double %call, double addrspace(4)* @_ZL10log_smallx, align 8, !tbaa !9
  ret void
}

The backend (probably rightly) chokes on that, addrspace(4) is constant in the
sense that the value has to be set before kernel execution begins. 

The optimal codegen, I think, would look like a global ctor that writes to that
value as an addrspace(1) value, then the other kernels load it as an
addrspace(4) value. Thus we can use scalar loads on it, after the global ctor
ran to initialise it.

A less clever way to fix this ICE is to find the point in clang that tags the
const variable as addrspace(4) and back it off to only do so when the
initialiser is already available as a constant.

I think this is an artefact of how constexpr is handled by target regions - I
suspect log is marked constexpr in a glibc header, but it isn't actually
constexpr in the hip math headers - but have not confirmed that theory.</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>