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

[AMDGPU] HIP miscompilation bug (struct assignments nested inside comma expressions)

Open ElliotBLabs opened this issue 6 months ago • 4 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): 65536 Ref (-O0): 0

ElliotBLabs avatar May 19 '26 18:05 ElliotBLabs

tagging @mgcarrasco who is working on it confirmed in commit a4cd17e Potential Duplicate to #198603

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): 65536 Ref (-O0): 0

I think this is what is happening, which is a bug in the compiler:

  • func_33 correctly initializes the active int32_t of returned union value with a 4-byte store i32 0, so bytes 0–3 in memory are zero.
  • The AMDGPU return path then loads that memory as an S0 value, not as a raw bytes copy. S0 is { i16, i32, i8 } with a padding gap at bytes 2–3.
  • The load keeps bytes 0–1 initialized, because they match the i16. The load as an S0 does not preserve the other two bytes of the initialization because they are in the padding gap.

This seems to be related to https://github.com/llvm/llvm-project/issues/107239.

These other two issues might be triggering the same or similar bug: https://github.com/llvm/llvm-project/issues/198603 and https://github.com/llvm/llvm-project/issues/198613.

mgcarrasco avatar Sep 28 '26 13:09 mgcarrasco

@ahueck , can you investigate this one when you get a chance?

kzhuravl avatar Sep 28 '26 15:09 kzhuravl