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

    <tr>
        <th>Summary</th>
        <td>
            [AMDGPU] backend emits wrong code causes memory access fault if an AS is specified
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:AMDGPU
      </td>
    </tr>

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

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

<pre>
    To reproduce this issue, both LLVM and OpenMP (with target offloading at least for AMDGPU) need to be built.

The reproducer is in LLVM repo.
```
$ clang -fopenmp --offload-arch=gfx90a llvm-project/openmp/libomptarget/test/offloading/bug51982.c -O1 -fopenmp-target-jit -o bug51982
$ ./bug51982
```
At this moment the test should not fail. Extract the attachment. We can see there are four files: `good.ll`, `good.s`, `bad.ll`, and ``bad.s``. As their name suggests, "good" means the IR can pass the test, while "bad" means the IR causes the test failure. The only difference between the two IRs is, the bad version has an extra AS cast, and then the corresponding store to the original generic pointer is replaced with a store with the right AS. The two `.s` files correspond to the IRs.

In order to reproduce the runtime memory access fault, we don't need to rebuilt the reproducer.
```
$ llc -O1 -march=amdgcn -mcpu=gfx90a --filetype=obj good.ll -o good.o
$ export LIBOMPTARGET_JIT_REPLACEMENT_OBJECT="good.o"
$ ./bug51982
```
The test should pass as well. Now we compile the bad version.
```
$ llc -O1 -march=amdgcn -mcpu=gfx90a --filetype=obj bad.ll -o bad.o
$ export LIBOMPTARGET_JIT_REPLACEMENT_OBJECT="bad.o"
$ ./bug51982
```
Now you are supposed to see the memory access fault error that crashes the program.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVd9vozgQ_muclxGImIYmD3mgTbLKqt1Wbe7usTJmAPeMB9mmaf_7k4EkvVXvYXUrVQ3g-fn5-2aEc6o2iGu2uGGLzUz0viG7do3SXgkzK6j8WB8ILHaWyl4i-EY5UM71yPg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