[llvm] 74a2dd1 - [InstSimplify] fix/improve folding with an SNaN vector element operand

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 14 16:11:24 PST 2023


Author: Sanjay Patel
Date: 2023-02-14T19:10:56-05:00
New Revision: 74a2dd1356bfd750689a82dc3951688437196117

URL: https://github.com/llvm/llvm-project/commit/74a2dd1356bfd750689a82dc3951688437196117
DIFF: https://github.com/llvm/llvm-project/commit/74a2dd1356bfd750689a82dc3951688437196117.diff

LOG: [InstSimplify] fix/improve folding with an SNaN vector element operand

Follow-up to the equivalent change for scalars:
D143505 / 83ba349ae0a8

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/test/Transforms/InstSimplify/fp-nan.ll
    llvm/test/Transforms/InstSimplify/strictfp-fadd.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index c71ff9474bd9..d6664723af01 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5315,13 +5315,15 @@ static Constant *propagateNaN(Constant *In) {
     SmallVector<Constant *, 32> NewC(NumElts);
     for (unsigned i = 0; i != NumElts; ++i) {
       Constant *EltC = In->getAggregateElement(i);
-      // Poison and existing NaN elements propagate.
+      // Poison elements propagate. NaN propagates except signaling is quieted.
       // Replace unknown or undef elements with canonical NaN.
-      // TODO: Quiet a signaling NaN element.
-      if (EltC && (isa<PoisonValue>(EltC) || EltC->isNaN()))
+      if (EltC && isa<PoisonValue>(EltC))
         NewC[i] = EltC;
+      else if (EltC && EltC->isNaN())
+        NewC[i] = ConstantFP::get(
+            EltC->getType(), cast<ConstantFP>(EltC)->getValue().makeQuiet());
       else
-        NewC[i] = (ConstantFP::getNaN(VecTy->getElementType()));
+        NewC[i] = ConstantFP::getNaN(VecTy->getElementType());
     }
     return ConstantVector::get(NewC);
   }

diff  --git a/llvm/test/Transforms/InstSimplify/fp-nan.ll b/llvm/test/Transforms/InstSimplify/fp-nan.ll
index 37760a6ecf20..620bda146177 100644
--- a/llvm/test/Transforms/InstSimplify/fp-nan.ll
+++ b/llvm/test/Transforms/InstSimplify/fp-nan.ll
@@ -43,7 +43,7 @@ define float @fsub_nan_op1(float %x) {
 
 define <2 x float> @fsub_nan_op1_vec(<2 x float> %x) {
 ; CHECK-LABEL: @fsub_nan_op1_vec(
-; CHECK-NEXT:    ret <2 x float> <float 0x7FF1000000000000, float 0xFFF1000000000000>
+; CHECK-NEXT:    ret <2 x float> <float 0x7FF9000000000000, float 0xFFF9000000000000>
 ;
   %r = fsub <2 x float> %x, <float 0x7FF1000000000000, float 0xFFF1000000000000>
   ret <2 x float> %r
@@ -61,7 +61,7 @@ define double @fmul_nan_op0(double %x) {
 
 define <2 x double> @fmul_nan_op0_vec(<2 x double> %x) {
 ; CHECK-LABEL: @fmul_nan_op0_vec(
-; CHECK-NEXT:    ret <2 x double> <double 0xFFF0000000000001, double 0xFFF0DEADDEADDEAD>
+; CHECK-NEXT:    ret <2 x double> <double 0xFFF8000000000001, double 0xFFF8DEADDEADDEAD>
 ;
   %r = fmul <2 x double> <double 0xFFF0000000000001, double 0xFFF0DEADDEADDEAD>, %x
   ret <2 x double> %r

diff  --git a/llvm/test/Transforms/InstSimplify/strictfp-fadd.ll b/llvm/test/Transforms/InstSimplify/strictfp-fadd.ll
index 24889b1cdd21..98422706f653 100644
--- a/llvm/test/Transforms/InstSimplify/strictfp-fadd.ll
+++ b/llvm/test/Transforms/InstSimplify/strictfp-fadd.ll
@@ -390,41 +390,41 @@ define float @fold_fadd_snan_variable_ebmaytrap(float %x) #0 {
   ret float %add
 }
 
-; FIXME: Exceptions are ignored, so this can be folded, but constrained math requires that SNaN is quieted per IEEE-754 spec.
+; Exceptions are ignored, so this can be folded, but constrained math requires that SNaN is quieted per IEEE-754 spec.
 
 define <2 x float> @fold_fadd_vec_snan_variable_ebignore(<2 x float> %x) #0 {
 ; CHECK-LABEL: @fold_fadd_vec_snan_variable_ebignore(
-; CHECK-NEXT:    ret <2 x float> <float 0x7FF4000000000000, float 0xFFF4000000000000>
+; CHECK-NEXT:    ret <2 x float> <float 0x7FFC000000000000, float 0xFFFC000000000000>
 ;
   %add = call <2 x float> @llvm.experimental.constrained.fadd.v2f32(<2 x float><float 0x7ff4000000000000, float 0xfff4000000000000>, <2 x float> %x, metadata !"round.tonearest", metadata !"fpexcept.ignore") #0
   ret <2 x float> %add
 }
 
-; FIXME: Exceptions may (not) trap, so this can be folded, but constrained math requires that SNaN is quieted per IEEE-754 spec.
+; Exceptions may (not) trap, so this can be folded, but constrained math requires that SNaN is quieted per IEEE-754 spec.
 
 define <2 x float> @fold_fadd_vec_snan_variable_ebmaytrap(<2 x float> %x) #0 {
 ; CHECK-LABEL: @fold_fadd_vec_snan_variable_ebmaytrap(
-; CHECK-NEXT:    ret <2 x float> <float 0xFFF4000000000000, float 0x7FF4000000000000>
+; CHECK-NEXT:    ret <2 x float> <float 0xFFFC000000000000, float 0x7FFC000000000000>
 ;
   %add = call <2 x float> @llvm.experimental.constrained.fadd.v2f32(<2 x float><float 0xfff4000000000000, float 0x7ff4000000000000>, <2 x float> %x, metadata !"round.tonearest", metadata !"fpexcept.maytrap") #0
   ret <2 x float> %add
 }
 
-; FIXME: Exceptions are ignored, so this can be folded, but constrained math requires that SNaN is quieted per IEEE-754 spec.
+; Exceptions are ignored, so this can be folded, but constrained math requires that SNaN is quieted per IEEE-754 spec.
 
 define <2 x float> @fold_fadd_vec_partial_snan_variable_ebignore(<2 x float> %x) #0 {
 ; CHECK-LABEL: @fold_fadd_vec_partial_snan_variable_ebignore(
-; CHECK-NEXT:    ret <2 x float> <float 0x7FF4000000000000, float 0xFFFF000000000000>
+; CHECK-NEXT:    ret <2 x float> <float 0x7FFC000000000000, float 0xFFFF000000000000>
 ;
   %add = call <2 x float> @llvm.experimental.constrained.fadd.v2f32(<2 x float><float 0x7ff4000000000000, float 0xffff000000000000>, <2 x float> %x, metadata !"round.tonearest", metadata !"fpexcept.ignore") #0
   ret <2 x float> %add
 }
 
-; FIXME: Exceptions may (not) trap, so this can be folded, but constrained math requires that SNaN is quieted per IEEE-754 spec.
+; Exceptions may (not) trap, so this can be folded, but constrained math requires that SNaN is quieted per IEEE-754 spec.
 
 define <2 x float> @fold_fadd_vec_partial_snan_variable_ebmaytrap(<2 x float> %x) #0 {
 ; CHECK-LABEL: @fold_fadd_vec_partial_snan_variable_ebmaytrap(
-; CHECK-NEXT:    ret <2 x float> <float 0xFFF8000000000000, float 0x7FF4000000000000>
+; CHECK-NEXT:    ret <2 x float> <float 0xFFF8000000000000, float 0x7FFC000000000000>
 ;
   %add = call <2 x float> @llvm.experimental.constrained.fadd.v2f32(<2 x float><float 0xfff8000000000000, float 0x7ff4000000000000>, <2 x float> %x, metadata !"round.tonearest", metadata !"fpexcept.maytrap") #0
   ret <2 x float> %add


        


More information about the llvm-commits mailing list