[llvm] [AMDGPU] Fix fmed3 constant-fold sign-of-zero miscompile (PR #201896)
Jay Foad via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 8 01:34:20 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);
----------------
jayfoad wrote:
Just use `APFloat::bitwiseIsEqual`?
https://github.com/llvm/llvm-project/pull/201896
More information about the llvm-commits
mailing list