[AMDGPU] HIP miscompilation bug (LowerDIVREM24)
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 -Wno-c++11-narrowing --offload-arch=native -o O0.outclang++ -O1 -x hip HIPProg.hip -Wno-c++11-narrowing --offload-arch=native -o O1.out./O0.out./O1.out
Compare output values
tagging @mgcarrasco who is working on it
I can confirm the bug for commit 085f2407fec2 and gfx90a. I'm currently debugging it.
@llvm/issue-subscribers-backend-amdgpu
Author: ElliotBLabs
Test File: HIPProg.hip
How to reproduce:
hipcc -O0 -x hip HIPProg.hip -Wno-c++11-narrowing --offload-arch=native -o O0.outhipcc -O1 -x hip HIPProg.hip -Wno-c++11-narrowing --offload-arch=native -o O1.out./O0.out./O1.out
Compare output values
hipcc is not part of upstream LLVM. Does it also reproduce with clang?
hipcc is not part of upstream LLVM. Does it also reproduce with clang?
Yes, it reproduces with clang++.
There is no reason to use hipcc, all users should directly invoke clang
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++
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.
cc @shiltian
Fixed by https://github.com/llvm/llvm-project/pull/196418