llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[AMDGPU] HIP miscompilation bug (rshift of unsigned vector types)

Open ElliotBLabs opened this issue 6 months ago • 6 comments

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:

  1. clang++ -O0 -x hip HIPProg.hip --offload-arch=native -o O0.out
  2. clang++ -O1 -x hip HIPProg.hip --offload-arch=native -o O1.out
  3. ./O0.out
  4. ./O1.out

Compare output values: Bug (-O1): ['4', '4', '4', '4'] Ref (-O0): ['0', '0', '0', '0']

ElliotBLabs avatar May 19 '26 18:05 ElliotBLabs

tagging @mgcarrasco who is working on it confirmed in commit a4cd17e

ElliotBLabs avatar May 19 '26 18:05 ElliotBLabs

@llvm/issue-subscribers-backend-amdgpu

Author: ElliotBLabs

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:

  1. clang++ -O0 -x hip HIPProg.hip --offload-arch=native -o O0.out
  2. clang++ -O1 -x hip HIPProg.hip --offload-arch=native -o O1.out
  3. ./O0.out
  4. ./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.

ElliotBLabs avatar May 20 '26 09:05 ElliotBLabs

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.

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

arsenm avatar Jun 03 '26 16:06 arsenm

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.

mgcarrasco avatar Sep 25 '26 18:09 mgcarrasco

Opened issue https://github.com/ROCm/rocm-systems/issues/12374

mgcarrasco avatar Sep 28 '26 14:09 mgcarrasco