[llvm] 161d9dc - [SimplifyLibCalls] Fix profile propagation in replacePowWithSqrt (#229645)

via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 6 21:32:28 PDT 2026


Author: Aiden Grossman
Date: 2026-10-07T04:32:17Z
New Revision: 161d9dca93239b3d4bb1d1478d088df22fd2e52a

URL: https://github.com/llvm/llvm-project/commit/161d9dca93239b3d4bb1d1478d088df22fd2e52a
DIFF: https://github.com/llvm/llvm-project/commit/161d9dca93239b3d4bb1d1478d088df22fd2e52a.diff

LOG: [SimplifyLibCalls] Fix profile propagation in replacePowWithSqrt (#229645)

In some cases we create a select conditioned on whether the input value
is equal to -infinity. We assume that this case is rare and thus mark
the true arm of the select unlikely.

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
    llvm/test/Transforms/InstCombine/pow-1.ll
    llvm/utils/profcheck-xfail.txt

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 62972e8dc49dc..2829146817e30 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -28,6 +28,7 @@
 #include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/Intrinsics.h"
+#include "llvm/IR/MDBuilder.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/PatternMatch.h"
 #include "llvm/IR/ProfDataUtils.h"
@@ -2490,6 +2491,15 @@ Value *LibCallSimplifier::replacePowWithSqrt(CallInst *Pow, IRBuilderBase &B) {
           *NegInf = ConstantFP::getInfinity(Ty, true);
     Value *FCmp = B.CreateFCmpOEQ(Base, NegInf, "isinf");
     Sqrt = B.CreateSelect(FCmp, PosInf, Sqrt);
+    // We assume that the case where x == -infinity is unlikely, so we assign
+    // unlikely branch weights to that arm of the select.
+    if (!ProfcheckDisableMetadataFixes) {
+      if (auto *SqrtSI = dyn_cast<SelectInst>(Sqrt))
+        setBranchWeights(
+            *SqrtSI,
+            {MDBuilder::kUnlikelyBranchWeight, MDBuilder::kLikelyBranchWeight},
+            /*IsExpected=*/false);
+    }
   }
 
   // If the exponent is negative, then get the reciprocal.

diff  --git a/llvm/test/Transforms/InstCombine/pow-1.ll b/llvm/test/Transforms/InstCombine/pow-1.ll
index e79b85f2f4a2f..51dab7c5b7690 100644
--- a/llvm/test/Transforms/InstCombine/pow-1.ll
+++ b/llvm/test/Transforms/InstCombine/pow-1.ll
@@ -904,7 +904,7 @@ define double @pow_libcall_half_no_FMF_noerrno(double %x) {
 ; LIB-NEXT:    [[SQRT:%.*]] = call double @llvm.sqrt.f64(double [[X]])
 ; LIB-NEXT:    [[ABS:%.*]] = call double @llvm.fabs.f64(double [[SQRT]])
 ; LIB-NEXT:    [[ISINF:%.*]] = fcmp oeq double [[X]], -inf
-; LIB-NEXT:    [[RETVAL:%.*]] = select i1 [[ISINF]], double +inf, double [[ABS]]
+; LIB-NEXT:    [[RETVAL:%.*]] = select i1 [[ISINF]], double +inf, double [[ABS]], !prof [[PROF0:![0-9]+]]
 ; LIB-NEXT:    ret double [[RETVAL]]
 ;
 ; NOLIB-LABEL: define double @pow_libcall_half_no_FMF_noerrno(
@@ -1340,7 +1340,7 @@ define double @pow_intrinsic_half_no_FMF(double %x) {
 ; CHECK-NEXT:    [[SQRT:%.*]] = call double @llvm.sqrt.f64(double [[X]])
 ; CHECK-NEXT:    [[ABS:%.*]] = call double @llvm.fabs.f64(double [[SQRT]])
 ; CHECK-NEXT:    [[ISINF:%.*]] = fcmp oeq double [[X]], -inf
-; CHECK-NEXT:    [[RETVAL:%.*]] = select i1 [[ISINF]], double +inf, double [[ABS]]
+; CHECK-NEXT:    [[RETVAL:%.*]] = select i1 [[ISINF]], double +inf, double [[ABS]], !prof [[PROF0:![0-9]+]]
 ; CHECK-NEXT:    ret double [[RETVAL]]
 ;
   %retval = call double @llvm.pow.f64(double %x, double 0.5)
@@ -1541,3 +1541,16 @@ define <2 x bfloat> @test_pow_10_v2bf16(<2 x bfloat> %x) {
 }
 
 attributes #0 = { nounwind memory(none) }
+;.
+; CHECK-EXP10: [[PROF0]] = !{!"branch_weights", i32 1, i32 1048575}
+;.
+; VC32: [[PROF0]] = !{!"branch_weights", i32 1, i32 1048575}
+;.
+; VC51: [[PROF0]] = !{!"branch_weights", i32 1, i32 1048575}
+;.
+; VC64: [[PROF0]] = !{!"branch_weights", i32 1, i32 1048575}
+;.
+; VC83: [[PROF0]] = !{!"branch_weights", i32 1, i32 1048575}
+;.
+; NOLIB: [[PROF0]] = !{!"branch_weights", i32 1, i32 1048575}
+;.

diff  --git a/llvm/utils/profcheck-xfail.txt b/llvm/utils/profcheck-xfail.txt
index fd3dc85bf9eaa..ad0bf8bc8a725 100644
--- a/llvm/utils/profcheck-xfail.txt
+++ b/llvm/utils/profcheck-xfail.txt
@@ -39,9 +39,6 @@ Transforms/InstCombine/minmax-fold.ll
 Transforms/InstCombine/minmax-intrinsics.ll
 Transforms/InstCombine/mul-inseltpoison.ll
 Transforms/InstCombine/mul.ll
-Transforms/InstCombine/pow-1.ll
-Transforms/InstCombine/pow-3.ll
-Transforms/InstCombine/pow-sqrt.ll
 Transforms/InstCombine/select-factorize.ll
 Transforms/InstCombine/select-min-max.ll
 Transforms/InstCombine/select-of-symmetric-selects.ll


        


More information about the llvm-commits mailing list