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

    <tr>
        <th>Summary</th>
        <td>
            Clang HIP inconsistent order for template functions
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          Ritanya-B-Bharadwaj
      </td>
    </tr>
</table>

<pre>
    The following test case demonstrates an issue with the Clang compiler where the order of functions in the assembly output is non-deterministic. Although this is not a functional problem, as each function is identical, it poses a challenge for debugging and testing. Normally, one would expect bitwise identical output when compiling the same source code with the same compiler and flags. The issue seems to occur while compiling HIP with -fgpu-rdc. Here is the reproducer: 

```
#include "hip/hip_runtime.h"
 
#define CHECK(cmd) \
   { \
       hipError_t error = cmd; \
       if (error != hipSuccess) { \
           fprintf(stderr, "error: '%s'(%d) at %s:%d\n", hipGetErrorString(error), error,        \
 __FILE__, __LINE__); \
           exit(EXIT_FAILURE); \
       } \
   }
 
template<int i>
__global__ void kernel() {
 printf("Hello from kernel %d\n", i);
}
 
template __global__ void kernel<1>();
template __global__ void kernel<2>();
template __global__ void kernel<3>();
 
int main(int argc, char* argv[]) {
 hipLaunchKernelGGL(kernel<1>, dim3(1), dim3(1), 0, 0);
 CHECK(hipDeviceSynchronize());
   hipLaunchKernelGGL(kernel<2>, dim3(1), dim3(1), 0, 0);
   CHECK(hipDeviceSynchronize());
 hipLaunchKernelGGL(kernel<3>, dim3(1), dim3(1), 0, 0);
 CHECK(hipDeviceSynchronize());
}
```

```
for i in $(seq 5); do
 clang -x hip --offload-arch=gfx908 -save-temps -fgpu-rdc -Ofast test_hip.cpp
 md5sum test_hip-hip-amdgcn-amd-amdhsa-gfx908.bc
 llvm-dis test_hip-hip-amdgcn-amd-amdhsa-gfx908.bc
 cp test_hip-hip-amdgcn-amd-amdhsa-gfx908.ll test_hip-hip-amdgcn-amd-amdhsa-gfx908.$i.ll
done
75be8654e3a6c39e1e83f5c8b7dda364 test_hip-hip-amdgcn-amd-amdhsa-gfx908.bc
bde823a75c56e9af933be309d8e433f3 test_hip-hip-amdgcn-amd-amdhsa-gfx908.bc
e18bbc2e4768556c52864c716cba9c02 test_hip-hip-amdgcn-amd-amdhsa-gfx908.bc
e18bbc2e4768556c52864c716cba9c02 test_hip-hip-amdgcn-amd-amdhsa-gfx908.bc
75be8654e3a6c39e1e83f5c8b7dda364 test_hip-hip-amdgcn-amd-amdhsa-gfx908.bc
```


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVltv27gS_jX0y0CGRF0sPfghseMmaNBz0PYA502gyJHELkVqSSpO9tcvKCtOmqbbpljsCrZIam7ffBxemHOy04hbkl-SfL9ik--N3X6UnukHFl1Glz2zTBzZl1VjxMP2c4_QGqXMUeoOPDoPnDkEgYPRzlvm0QHTIJ2bEI7S9-B7hJ1iugNuhlEqtHDs0eIsMFagBdNCO2nupdEOpJ4lzDkcGvUAZvLj5EE60EZHAj3aQWrpvORruFC-N1MXokh30vHAzt6YgtGaRuFA6A6YA2S8P0uDvhSoveRMBQXpYTQuZAC8Z0qh7kK6FgQ2U9eFlJkWc9pSd2v4YOzAlHoItkYjHM2kBOD9iNxDI_1ROnyK8JjJsUe9cDGT2CM4NiA4M1mOwI14xtwsORMXoreKdW4NYSZOLDvEwYE3YDifArlS4TP_1zf_PbmL2m6cIiv4Gq4D_9LNESyO1oiJoyXpBZB4T-KL5V3Ey-80pKnUXE0CgVDay5HQQy_H2k7aywHXPaH0pAlnA4Gt1Ai766vde0JLPghCKyD5blEEIJvLr8bh6eV4Za2xtQcMLZB0D8E2_VZXtkBouajRJGj2cvw0cY7OzcFeCRCedrRS-5bQ0nmB1oZJJJTOnmYm6IbQ3M1NSWg-I2ce5o_pxfwl3-mQNN2FmO_Qz6g_eSt19wiK0CrIl_7uMfoToro-3Nxe1XUQ1vXtzYe5X72W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