[AMDGPU] HIP miscompilation bug (rshift of unsigned vector types)
The following HIP program triggers a miscompilation bug. The output should be a deterministic value as it's UB free. The bug manifests by enabling optimisations which cause a divergence in the printed value.
Test File: HIPProg.hip
How to reproduce:
clang++ -O0 -x hip HIPProg.hip --offload-arch=native -o O0.outclang++ -O1 -x hip HIPProg.hip --offload-arch=native -o O1.out./O0.out./O1.out
Compare output values: Bug (-O1): ['4', '4', '4', '4'] Ref (-O0): ['0', '0', '0', '0']
tagging @mgcarrasco who is working on it confirmed in commit a4cd17e
@llvm/issue-subscribers-backend-amdgpu
Author: ElliotBLabs
Test File: HIPProg.hip
How to reproduce:
clang++ -O0 -x hip HIPProg.hip --offload-arch=native -o O0.outclang++ -O1 -x hip HIPProg.hip --offload-arch=native -o O1.out./O0.out./O1.out
Compare output values: Bug (-O1): ['4', '4', '4', '4'] Ref (-O0): ['0', '0', '0', '0']
After looking at this further, would this shift be safe in HIP?
The ushort type is 16 bits wide and shifting a bit width >= 16.
make_ushort1(p_3308->g_1535.z) >> 16
I cannot find any documentation to verify this.
After looking at this further, would this shift be safe in HIP? The ushort type is 16 bits wide and shifting a bit width >= 16.
make_ushort1(p_3308->g_1535.z) >> 16I cannot find any documentation to verify this.
This isn't HIP specific, it's C. By the usual promotion rules, this should perform the shift as uint. unsigned short shifted by int should be evaluated as unsigned int
Thanks @arsenm.
So if I follow, each component (.x, .y, .z, .w) should be shifted as a normal unsigned short, so the usual C promotion applies regardless of the rank. A shift by 16 is then defined for ushort1, ushort2, ushort3, and ushort4, and the result is 0 for each of its components. Do you think this is correct?
The HIP headers do not do that for every size. operator>>= shifts the whole native vector:
https://github.com/ROCm/rocm-systems/blob/c6117b142e52654383a96a6eafdc91e6e662cf7c/projects/clr/hipamd/include/hip/amd_detail/amd_hip_vector_types.h#L481-L488
Then, the native vector operation becomes undefined in IR as the test case showed.
There is an exception which is ushort3. It is the only case where things are specialized per scalar.
Opened issue https://github.com/ROCm/rocm-systems/issues/12374