[llvm] [Transforms] Resolve FIXME: No Lowering of FNeg to FMul unless it is safe (PR #85252)

via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 14 11:18:38 PDT 2024


https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/85252

>From fde2dbf1372fd8aee36eb1252459c3396d53441a Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Thu, 14 Mar 2024 14:17:00 -0400
Subject: [PATCH 1/2] [Transforms] Pre-commit tests (NFC)

---
 .../Transforms/Reassociate/fast-basictest.ll  | 45 +++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/llvm/test/Transforms/Reassociate/fast-basictest.ll b/llvm/test/Transforms/Reassociate/fast-basictest.ll
index c1842d0cde456c..1771d4799747a0 100644
--- a/llvm/test/Transforms/Reassociate/fast-basictest.ll
+++ b/llvm/test/Transforms/Reassociate/fast-basictest.ll
@@ -578,3 +578,48 @@ define float @test18(float %A, float %B) {
   %Z = fadd fast float %Y, 1.200000e+01
   ret float %Z
 }
+
+; Do not fneg fast float unless it has nnan
+define float @scalar(float %a) {
+; CHECK-LABEL: @scalar(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[NEG:%.*]] = fneg fast float [[A:%.*]]
+; CHECK-NEXT:    ret float [[NEG]]
+;
+entry:
+  %neg = fneg fast float %a
+  ret float %neg
+}
+
+define float @scalar_nnan(float %a) {
+; CHECK-LABEL: @scalar_nnan(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[NEG:%.*]] = fneg fast float [[A:%.*]]
+; CHECK-NEXT:    ret float [[NEG]]
+;
+entry:
+  %neg = fneg fast nnan float %a
+  ret float %neg
+}
+
+define <4 x float> @vector(<4 x float> %a) {
+; CHECK-LABEL: @vector(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[NEG:%.*]] = fneg fast <4 x float> [[A:%.*]]
+; CHECK-NEXT:    ret <4 x float> [[NEG]]
+;
+entry:
+  %neg = fneg fast <4 x float> %a
+  ret <4 x float> %neg
+}
+
+define <4 x float> @vector_nnan(<4 x float> %a) {
+; CHECK-LABEL: @vector_nnan(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[NEG:%.*]] = fneg fast <4 x float> [[A:%.*]]
+; CHECK-NEXT:    ret <4 x float> [[NEG]]
+;
+entry:
+  %neg = fneg fast nnan <4 x float> %a
+  ret <4 x float> %neg
+}

>From cf284e9ab84a98aabea7fe7de6ada4fc16364b39 Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Thu, 14 Mar 2024 11:43:14 -0400
Subject: [PATCH 2/2] [Transforms] Resolve FIXME: No Lowering of FNeg to FMul
 unless it is safe or flags are specified

---
 llvm/lib/Transforms/Scalar/Reassociate.cpp | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/Reassociate.cpp b/llvm/lib/Transforms/Scalar/Reassociate.cpp
index d91320863e241d..971cf88c3394d7 100644
--- a/llvm/lib/Transforms/Scalar/Reassociate.cpp
+++ b/llvm/lib/Transforms/Scalar/Reassociate.cpp
@@ -287,11 +287,15 @@ static Instruction *CreateNeg(Value *S1, const Twine &Name,
 static BinaryOperator *LowerNegateToMultiply(Instruction *Neg) {
   assert((isa<UnaryOperator>(Neg) || isa<BinaryOperator>(Neg)) &&
          "Expected a Negate!");
-  // FIXME: It's not safe to lower a unary FNeg into a FMul by -1.0.
   unsigned OpNo = isa<BinaryOperator>(Neg) ? 1 : 0;
   Type *Ty = Neg->getType();
-  Constant *NegOne = Ty->isIntOrIntVectorTy() ?
-    ConstantInt::getAllOnesValue(Ty) : ConstantFP::get(Ty, -1.0);
+  // Only lower to FMul if the operation is not a unary FNeg or Neg has the
+  // correct flags.
+  if (Ty->isFPOrFPVectorTy() && isa<UnaryOperator>(Neg) && !Neg->hasNoNaNs())
+    return nullptr;
+
+  Constant *NegOne = Ty->isIntOrIntVectorTy() ? ConstantInt::getAllOnesValue(Ty)
+                                              : ConstantFP::get(Ty, -1.0);
 
   BinaryOperator *Res =
       CreateMul(Neg->getOperand(OpNo), NegOne, "", Neg->getIterator(), Neg);



More information about the llvm-commits mailing list