[PATCH] D101727: Fix PR47960 - Incorrect transformation of fabs with nnan flag

Krishna Kariya via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 14 06:23:29 PDT 2021


Krishnakariya updated this revision to Diff 345414.
Krishnakariya added a comment.

Added description for the test case.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D101727/new/

https://reviews.llvm.org/D101727

Files:
  llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
  llvm/test/Transforms/InstCombine/fabs.ll


Index: llvm/test/Transforms/InstCombine/fabs.ll
===================================================================
--- llvm/test/Transforms/InstCombine/fabs.ll
+++ llvm/test/Transforms/InstCombine/fabs.ll
@@ -266,10 +266,11 @@
 }
 
 ; X <= 0.0 ? (0.0 - X) : X --> fabs(X)
+; This transformation drops the fast math flags, here, nnan flag. 
 
 define double @select_fcmp_nnan_ole_zero(double %x) {
 ; CHECK-LABEL: @select_fcmp_nnan_ole_zero(
-; CHECK-NEXT:    [[TMP1:%.*]] = call nnan double @llvm.fabs.f64(double [[X:%.*]])
+; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.fabs.f64(double [[X:%.*]])
 ; CHECK-NEXT:    ret double [[TMP1]]
 ;
   %lezero = fcmp ole double %x, 0.0
@@ -278,11 +279,24 @@
   ret double %fabs
 }
 
+; An Example with fast math flag in select instruction.  
+
+define double @select_nnan_fcmp_nnan_ole_zero(double %x) {
+; CHECK-LABEL: @select_nnan_fcmp_nnan_ole_zero(
+; CHECK-NEXT:    [[TMP1:%.*]] = call nnan double @llvm.fabs.f64(double [[X:%.*]])
+; CHECK-NEXT:    ret double [[TMP1]]
+;
+  %lezero = fcmp ole double %x, 0.0
+  %negx = fsub nnan double 0.0, %x
+  %fabs = select nnan i1 %lezero, double %negx, double %x
+  ret double %fabs
+}
+
 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
 
 define double @select_fcmp_nnan_ule_zero(double %x) {
 ; CHECK-LABEL: @select_fcmp_nnan_ule_zero(
-; CHECK-NEXT:    [[TMP1:%.*]] = call nnan double @llvm.fabs.f64(double [[X:%.*]])
+; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.fabs.f64(double [[X:%.*]])
 ; CHECK-NEXT:    ret double [[TMP1]]
 ;
   %lezero = fcmp ule double %x, 0.0
@@ -310,7 +324,7 @@
 
 define <2 x float> @select_fcmp_nnan_ole_negzero(<2 x float> %x) {
 ; CHECK-LABEL: @select_fcmp_nnan_ole_negzero(
-; CHECK-NEXT:    [[TMP1:%.*]] = call nnan <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
+; CHECK-NEXT:    [[TMP1:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
 ; CHECK-NEXT:    ret <2 x float> [[TMP1]]
 ;
   %lezero = fcmp ole <2 x float> %x, <float -0.0, float -0.0>
Index: llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -2846,15 +2846,13 @@
   // fast-math-flags (nsz) or fsub with +0.0 (not fneg) for this to work. We
   // also require nnan because we do not want to unintentionally change the
   // sign of a NaN value.
-  // FIXME: These folds should test/propagate FMF from the select, not the
-  //        fsub or fneg.
   // (X <= +/-0.0) ? (0.0 - X) : X --> fabs(X)
   Instruction *FSub;
   if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) &&
       match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(FalseVal))) &&
       match(TrueVal, m_Instruction(FSub)) && FSub->hasNoNaNs() &&
       (Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)) {
-    Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, FSub);
+    Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, &SI);
     return replaceInstUsesWith(SI, Fabs);
   }
   // (X >  +/-0.0) ? X : (0.0 - X) --> fabs(X)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101727.345414.patch
Type: text/x-patch
Size: 3207 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210514/be40bc90/attachment.bin>


More information about the llvm-commits mailing list