[llvm] [AMDGPU] Fix fmed3 constant-fold sign-of-zero miscompile (PR #201896)

Wooseok Lee via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 8 06:08:35 PDT 2026


================
@@ -54,12 +54,23 @@ static APFloat fmed3AMDGCN(const APFloat &Src0, const APFloat &Src1,
                            const APFloat &Src2) {
   APFloat Max3 = maxnum(maxnum(Src0, Src1), Src2);
 
-  APFloat::cmpResult Cmp0 = Max3.compare(Src0);
+  // Like APFloat::compare but treats -0 < +0. Without this, Max3=+0 would
+  // compare equal to a -0 operand and misidentify it as the maximum,
+  // causing the wrong sign to be returned (hardware v_med3 treats -0 < +0).
+  auto CmpSignedZero = [](const APFloat &A, const APFloat &B) {
+    APFloat::cmpResult Res = A.compare(B);
+    if (Res == APFloat::cmpEqual && A.isZero() && B.isZero() &&
+        A.isNegative() != B.isNegative())
+      return A.isNegative() ? APFloat::cmpLessThan : APFloat::cmpGreaterThan;
+    return Res;
+  };
+
+  APFloat::cmpResult Cmp0 = CmpSignedZero(Max3, Src0);
----------------
wooseoklee wrote:

Thanks for the suggestion. I updated code with the call which makes the code cleaner than before. 

https://github.com/llvm/llvm-project/pull/201896


More information about the llvm-commits mailing list