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

[AMDGPU] HIP miscompilation bug (LowerDIVREM24)

Open ElliotBLabs opened this issue 7 months ago • 10 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 -Wno-c++11-narrowing --offload-arch=native -o O0.out
  2. clang++ -O1 -x hip HIPProg.hip -Wno-c++11-narrowing --offload-arch=native -o O1.out
  3. ./O0.out
  4. ./O1.out

Compare output values

ElliotBLabs avatar Apr 29 '26 10:04 ElliotBLabs

tagging @mgcarrasco who is working on it

ElliotBLabs avatar Apr 29 '26 10:04 ElliotBLabs

I can confirm the bug for commit 085f2407fec2 and gfx90a. I'm currently debugging it.

mgcarrasco avatar Apr 29 '26 10:04 mgcarrasco

@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. hipcc -O0 -x hip HIPProg.hip -Wno-c++11-narrowing --offload-arch=native -o O0.out
  2. hipcc -O1 -x hip HIPProg.hip -Wno-c++11-narrowing --offload-arch=native -o O1.out
  3. ./O0.out
  4. ./O1.out

Compare output values

hipcc is not part of upstream LLVM. Does it also reproduce with clang?

jayfoad avatar Apr 29 '26 11:04 jayfoad

hipcc is not part of upstream LLVM. Does it also reproduce with clang?

Yes, it reproduces with clang++.

mgcarrasco avatar Apr 29 '26 11:04 mgcarrasco

There is no reason to use hipcc, all users should directly invoke clang

arsenm avatar Apr 29 '26 11:04 arsenm

There is no reason to use hipcc, all users should directly invoke clang

I have edited my issue, which was initially found using hipcc, then reproduced in clang++

ElliotBLabs avatar Apr 29 '26 11:04 ElliotBLabs

I've managed to reduce the test further and identify where the miscompilation is triggered.

When divisor is 1, the O0 execution computes 4294967295 (expected) and O1 computes 127 (wrong).

__global__ void hipsmith_kernel(int8_t divisor, uint64_t *results) {
  uint64_t si1 = 4294967295u;
  uint64_t safe_div = si1 / divisor;
  *results = safe_div;
}

The division is eventually encoded as %5 = udiv i32 -1, %4 which is correct too. This is then lowered here: https://github.com/llvm/llvm-project/blob/62310b0af446adee8ffdfda17d540cc2733708df/llvm/lib/Target/AMDGPU/AMDGPUISelLowering.cpp#L2340

If we comment that optimization path, the miscompilation is fixed. LowerDIVREM24 seems to only be sound if the result can fit in a 24-bit integer (see its current comment), which is not satisfied by the test (it computes 4294967295). This explains the miscompilation.

I've checked one previous refactoring and fix in that function: https://github.com/llvm/llvm-project/commit/81a709503d378c188462465918df161d664c4205

That commit refactored the callsites and the function, and seem to have tried to move the precondition checks that were at the callsites inside the function. However, it looks like it has missed moving the precondition checks for the unsigned case. The function currently only has the checks that were for the signed callsite.

This patch fixed the problem for this test:

diff --git a/llvm/lib/Target/AMDGPU/AMDGPUISelLowering.cpp b/llvm/lib/Target/AMDGPU/AMDGPUISelLowering.cpp
index e05213b2aaf9..73c8c4d45695 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUISelLowering.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUISelLowering.cpp
@@ -2000,18 +2000,27 @@ SDValue AMDGPUTargetLowering::LowerDIVREM24(SDValue Op, SelectionDAG &DAG,
                                             bool Sign) const {
   SDLoc DL(Op);
   EVT VT = Op.getValueType();

   SDValue LHS = Op.getOperand(0);
   SDValue RHS = Op.getOperand(1);
   MVT IntVT = MVT::i32;
   MVT FltVT = MVT::f32;
 
   unsigned LHSSignBits = DAG.ComputeNumSignBits(LHS);
-  if (LHSSignBits < 9)
-    return SDValue();
-
   unsigned RHSSignBits = DAG.ComputeNumSignBits(RHS);
-  if (RHSSignBits < 9)
-    return SDValue();
+
+  if (Sign) {
+    if (LHSSignBits < 9 || RHSSignBits < 9)
+      return SDValue();
+  } else {
+    APInt MustBeZero = APInt::getHighBitsSet(32, 8);
+    if (!DAG.MaskedValueIsZero(LHS, MustBeZero) ||
+        !DAG.MaskedValueIsZero(RHS, MustBeZero))
+      return SDValue();
+  }
 
   unsigned BitSize = VT.getSizeInBits();
   unsigned SignBits = std::min(LHSSignBits, RHSSignBits);

I still have to test this more and open a PR when ready.

mgcarrasco avatar Apr 30 '26 17:04 mgcarrasco

cc @shiltian

kzhuravl avatar May 01 '26 13:05 kzhuravl

Fixed by https://github.com/llvm/llvm-project/pull/196418

mgcarrasco avatar Jun 03 '26 10:06 mgcarrasco