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

    <tr>
        <th>Summary</th>
        <td>
            Bad SPIR-V code generated for SAXPY kernel in LLVM 17
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    I have a simple SAXPY kernel written in OpenCL that I am compiling with Clang 17.0.5. Using the new experimental SPIR-V target for LLVM, I find that this code generates the incorrect answer. However, I can use the same target to generate LLVM bitcode, which I then pass through the [SPIRV-LLVM translator](https://github.com/KhronosGroup/SPIRV-LLVM-Translator), and this works (producing the correct answer). Therefore, I believe that there is a bug in the LLVM SPIR-V target, and it is producing the wrong SPIR-V code.

For convenience I have produced a [reproducer](https://github.com/ExaFEL/gpu-portability-samples/tree/spirv-repro/opencl):

```
git clone -b spirv-repro https://github.com/ExaFEL/gpu-portability-samples.git
```

The reproducer includes an environment intended for [Sunspot](https://www.alcf.anl.gov/support-center/aurora-sunspot) at ALCF, which is an Intel PVC machine. For anyone who does not have access, you will need to build Clang 17.0.5 (with the experimental SPIR-V target enabled) and the SPIRV-LLVM translator, and obtain an Intel OpenCL platform.

On sunspot I build as follows:

```
cd gpu-portability-samples/opencl
source sunspot_env.sh
make -j4
```

This produces two programs, `saxpy` (built with the LLVM SPIR-V backend) and `saxpy_t` (built with the translator). You can run each of them to see the output:

```console
$ ./saxpy
Running on platform: Intel(R) OpenCL Graphics
Running on device: Intel(R) Data Center GPU Max 1550
CL_DEVICE_ADDRESS_BITS is 64 for this device.
Using SPIR-V code path
Loading SPIR-V from file: saxpy_kernel64.spv
Program build log for device Intel(R) Data Center GPU Max 1550:

bad value at z[0] = 4.253530e+37 (repeats 262143 times)
bad value at z[262144] = 0.000000e+00 (repeats 786431 times)
status: FAIL

$ ./saxpy_t
Running on platform: Intel(R) OpenCL Graphics
Running on device: Intel(R) Data Center GPU Max 1550
CL_DEVICE_ADDRESS_BITS is 64 for this device.
Using SPIR-V code path
Loading SPIR-V from file: saxpy_kernel64_t.spv
Program build log for device Intel(R) Data Center GPU Max 1550:

status: PASS
```

The only difference between the two executions is which SPIR-V code gets loaded.

For posterity, I am uploading the generated SPIR-V code (and LLVM bitcode) below.

[saxpy_kernels.zip](https://github.com/llvm/llvm-project/files/13492635/saxpy_kernels.zip)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkVkFv2zgT_TX0ZWBBpizbOvjg2HE_43PRIEmD7SmgqJHEhiYFkrKS_voFKTmx27TdxWJPGwQyJHLeDN-8GQ6zVlQKcUnSK5JuRqx1tTZLlFJo56xkbVU7NKNcFy_LHdTsiMDAikMjEe5Wf9x8gSc0CiV0RjiHCoSCTw2q9R5czRzsgB2A60MjpFAVdMLVsJZMVTCZR3GURvDZ-gVXIyjsAJ8bNOKAyjEJdze72_EDOGYqdFBqA_v9w0dC17CDUqiid-FqYYHrAqFChYY5tAFOKK6NQe6AKduhieB_usMjmh6AMwWtxbDVsgOe3Dj9ihPcQS6cR_dWXS14DTtvo6Bh1jsyuq3qgELSKx_xwziYOcOUlcxpQ9INoYvaucaSZEXoltBtJVzd5hHXB0K3_6-NVtp-MLptCN2-gYzv30Bo5iNg4dTCQqfNkwVCF43RRctPHF4emdAsgvsaDZbaYH_uHKXAI564Q4MgLDDI28onz4OE-C_IP7kWzm--dNkZrarTds9UROINiVf9c6sNcK2OqAQqjjCIqIfAApinzeDw_nuurp_Z9nrvvzbtuNHGsVxI4V7GlnlRWkK3ziASurWNMMdxgCZ0qxtUXHoWk9V5fGQWD__htRIOuNQKYZzDGQL8g5CiSrh3nfXP-xrhjQAvW9kWaIEpQHUURitfDiCUQ1VgEerAS61VttHuPcK6rouY5GXElIwqffRktI2PbMxROa-LLWuNNmxsBxSaAXOw2q-3bzoXIYad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