[AMDGPU] HIP miscompilation bug (struct assignments nested inside comma expressions)
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): 65536 Ref (-O0): 0
tagging @mgcarrasco who is working on it confirmed in commit a4cd17e Potential Duplicate to #198603
@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): 65536 Ref (-O0): 0
I think this is what is happening, which is a bug in the compiler:
func_33correctly initializes the active int32_t of returned union value with a 4-bytestore 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.
@ahueck , can you investigate this one when you get a chance?