[llvm] ConstantFold logl calls (PR #94944)

Matthew Devereau via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 14 00:35:50 PDT 2024


https://github.com/MDevereau updated https://github.com/llvm/llvm-project/pull/94944

>From 8f80f150122c1bfec0ea59fd0de46b23cfc37b24 Mon Sep 17 00:00:00 2001
From: Matt Devereau <matthew.devereau at arm.com>
Date: Fri, 7 Jun 2024 09:55:09 +0000
Subject: [PATCH 1/3] ConstantFold logl calls

This is a follow up patch from #90611 which folds logl calls in the same manner
as log.f128 calls. Logl suffers from the same problem as logf128 of having slow
calls to fp128 log functions which can be constant folded. However, Logl is
emitted at the O3 level instead whereas log.f128 is emitted by Ofast by various
intrinsics.
---
 llvm/lib/Analysis/ConstantFolding.cpp         | 24 ++++++++++++-------
 .../InstSimplify/ConstProp/logf128.ll         | 10 ++++++++
 2 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 3ca3ae951fcd7..c0b9f7a4d68da 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1669,9 +1669,9 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
            Name == "floor" || Name == "floorf" ||
            Name == "fmod" || Name == "fmodf";
   case 'l':
-    return Name == "log" || Name == "logf" ||
-           Name == "log2" || Name == "log2f" ||
-           Name == "log10" || Name == "log10f";
+    return Name == "log" || Name == "logf" || Name == "log2" ||
+           Name == "log2f" || Name == "log10" || Name == "log10f" ||
+           Name == "logl";
   case 'n':
     return Name == "nearbyint" || Name == "nearbyintf";
   case 'p':
@@ -2085,15 +2085,19 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
     if (IntrinsicID == Intrinsic::canonicalize)
       return constantFoldCanonicalize(Ty, Call, U);
 
+      // Try to handle special fp128 cases before bailing
 #if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
     if (Ty->isFP128Ty()) {
-      switch (IntrinsicID) {
-      default:
-        return nullptr;
-      case Intrinsic::log:
-        return ConstantFP::get(Ty, logf128(Op->getValueAPF().convertToQuad()));
-      }
+      const APFloat &Fp128APF = Op->getValueAPF();
+      if (IntrinsicID == Intrinsic::log)
+        return ConstantFP::get(Ty, logf128(Fp128APF.convertToQuad()));
     }
+
+    LibFunc Fp128Func = NotLibFunc;
+    if (Ty->isFP128Ty() && TLI->getLibFunc(Name, Fp128Func) &&
+        TLI->has(Fp128Func) && Fp128Func == LibFunc_logl &&
+        !Op->getValueAPF().isNegative() && !Op->getValueAPF().isZero())
+      return ConstantFP::get(Ty, logf128(Op->getValueAPF().convertToQuad()));
 #endif
 
     if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
@@ -2356,6 +2360,8 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
         // TODO: What about hosts that lack a C99 library?
         return ConstantFoldFP(log10, APF, Ty);
       break;
+    case LibFunc_logl:
+      return nullptr;
     case LibFunc_nearbyint:
     case LibFunc_nearbyintf:
     case LibFunc_rint:
diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/logf128.ll b/llvm/test/Transforms/InstSimplify/ConstProp/logf128.ll
index da56997f69382..051d514058ccc 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/logf128.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/logf128.ll
@@ -3,6 +3,7 @@
 
 ; REQUIRES: has_logf128
 declare fp128 @llvm.log.f128(fp128)
+declare fp128 @logl(fp128)
 
 define fp128 @log_e_64(){
 ; CHECK-LABEL: define fp128 @log_e_64() {
@@ -124,3 +125,12 @@ define <2 x fp128> @log_e_negative_2_vector(){
   %A = call <2 x fp128> @llvm.log.v2f128(<2 x fp128> <fp128 0xL0000000000000000C000000000000000, fp128 0xL0000000000000000C000000000000001>)
   ret <2 x fp128> %A
 }
+
+define fp128 @logl_e_64(){
+; CHECK-LABEL: define fp128 @logl_e_64() {
+; CHECK-NEXT:    %A = call fp128 @logl(fp128 noundef 0xL00000000000000004005000000000000)
+; CHECK-NEXT:    ret fp128 0xL300000000000000040010A2B23F3BAB7
+;
+  %A = call fp128 @logl(fp128 noundef 0xL00000000000000004005000000000000)
+  ret fp128 %A
+}

>From a0683c486dd957c6d294bf896887473b550d5240 Mon Sep 17 00:00:00 2001
From: Matt Devereau <matthew.devereau at arm.com>
Date: Wed, 12 Jun 2024 08:06:59 +0000
Subject: [PATCH 2/3] Remove negative and zero checks, add more tests

---
 llvm/lib/Analysis/ConstantFolding.cpp         |  3 +-
 .../InstSimplify/ConstProp/logf128.ll         | 39 ++++++++++++++++++-
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index c0b9f7a4d68da..69c8323478005 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -2095,8 +2095,7 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
 
     LibFunc Fp128Func = NotLibFunc;
     if (Ty->isFP128Ty() && TLI->getLibFunc(Name, Fp128Func) &&
-        TLI->has(Fp128Func) && Fp128Func == LibFunc_logl &&
-        !Op->getValueAPF().isNegative() && !Op->getValueAPF().isZero())
+        TLI->has(Fp128Func) && Fp128Func == LibFunc_logl)
       return ConstantFP::get(Ty, logf128(Op->getValueAPF().convertToQuad()));
 #endif
 
diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/logf128.ll b/llvm/test/Transforms/InstSimplify/ConstProp/logf128.ll
index 051d514058ccc..d48713cf0a7bd 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/logf128.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/logf128.ll
@@ -128,9 +128,46 @@ define <2 x fp128> @log_e_negative_2_vector(){
 
 define fp128 @logl_e_64(){
 ; CHECK-LABEL: define fp128 @logl_e_64() {
-; CHECK-NEXT:    %A = call fp128 @logl(fp128 noundef 0xL00000000000000004005000000000000)
+; CHECK-NEXT:    [[A:%.*]] = call fp128 @logl(fp128 noundef 0xL00000000000000004005000000000000)
 ; CHECK-NEXT:    ret fp128 0xL300000000000000040010A2B23F3BAB7
 ;
   %A = call fp128 @logl(fp128 noundef 0xL00000000000000004005000000000000)
   ret fp128 %A
 }
+
+define fp128 @logl_e_0(){
+; CHECK-LABEL: define fp128 @logl_e_0() {
+; CHECK-NEXT:    [[A:%.*]] = call fp128 @logl(fp128 noundef 0xL00000000000000000000000000000000)
+; CHECK-NEXT:    ret fp128 0xL0000000000000000FFFF000000000000
+;
+  %A = call fp128 @logl(fp128 noundef 0xL00000000000000000000000000000000)
+  ret fp128 %A
+}
+
+define fp128 @logl_e_infinity(){
+; CHECK-LABEL: define fp128 @logl_e_infinity() {
+; CHECK-NEXT:    [[A:%.*]] = call fp128 @logl(fp128 noundef 0xL00000000000000007FFF000000000000)
+; CHECK-NEXT:    ret fp128 0xL00000000000000007FFF000000000000
+;
+  %A = call fp128 @logl(fp128 noundef 0xL00000000000000007FFF000000000000)
+  ret fp128 %A
+}
+
+define fp128 @logl_e_nan(){
+; CHECK-LABEL: define fp128 @logl_e_nan() {
+; CHECK-NEXT:    [[A:%.*]] = call fp128 @logl(fp128 noundef 0xL00000000000000007FFF000000000001)
+; CHECK-NEXT:    ret fp128 0xL00000000000000007FFF800000000001
+;
+  %A = call fp128 @logl(fp128 noundef 0xL00000000000000007FFF000000000001)
+  ret fp128 %A
+}
+
+
+define fp128 @logl_e_negative_2(){
+; CHECK-LABEL: define fp128 @logl_e_negative_2() {
+; CHECK-NEXT:    [[A:%.*]] = call fp128 @logl(fp128 noundef 0xL0000000000000000C000000000000000)
+; CHECK-NEXT:    ret fp128 0xL00000000000000007FFF800000000000
+;
+  %A = call fp128 @logl(fp128 noundef 0xL0000000000000000C000000000000000)
+  ret fp128 %A
+}

>From 172f77f0445d0e85c9b14112aaa84a7d2093698b Mon Sep 17 00:00:00 2001
From: Matt Devereau <matthew.devereau at arm.com>
Date: Fri, 14 Jun 2024 07:31:13 +0000
Subject: [PATCH 3/3] Run constant folded values through exception check

---
 llvm/lib/Analysis/ConstantFolding.cpp         | 36 ++++++++++++++-----
 .../InstSimplify/ConstProp/logf128.ll         | 24 ++++++++-----
 2 files changed, 43 insertions(+), 17 deletions(-)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 69c8323478005..028c3ed72fcd8 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1734,6 +1734,14 @@ Constant *GetConstantFoldFPValue(double V, Type *Ty) {
   llvm_unreachable("Can only constant fold half/float/double");
 }
 
+#if defined(HAS_IEE754_FLOAT128)
+Constant *GetConstantFoldFPValue128(__float128 V, Type *Ty) {
+  if (Ty->isFP128Ty())
+    return ConstantFP::get(Ty, V);
+  llvm_unreachable("Can only constant fold fp128");
+}
+#endif
+
 /// Clear the floating-point exception state.
 inline void llvm_fenv_clearexcept() {
 #if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT
@@ -1766,6 +1774,20 @@ Constant *ConstantFoldFP(double (*NativeFP)(double), const APFloat &V,
   return GetConstantFoldFPValue(Result, Ty);
 }
 
+#if defined(HAS_IEE754_FLOAT128)
+Constant *ConstantFoldFP128(long double (*NativeFP)(long double),
+                            const APFloat &V, Type *Ty) {
+  llvm_fenv_clearexcept();
+  __float128 Result = NativeFP(V.convertToQuad());
+  if (llvm_fenv_testexcept()) {
+    llvm_fenv_clearexcept();
+    return nullptr;
+  }
+
+  return GetConstantFoldFPValue128(Result, Ty);
+}
+#endif
+
 Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
                                const APFloat &V, const APFloat &W, Type *Ty) {
   llvm_fenv_clearexcept();
@@ -2085,18 +2107,16 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
     if (IntrinsicID == Intrinsic::canonicalize)
       return constantFoldCanonicalize(Ty, Call, U);
 
-      // Try to handle special fp128 cases before bailing
 #if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
     if (Ty->isFP128Ty()) {
-      const APFloat &Fp128APF = Op->getValueAPF();
       if (IntrinsicID == Intrinsic::log)
-        return ConstantFP::get(Ty, logf128(Fp128APF.convertToQuad()));
-    }
+        return ConstantFoldFP128(logf128, Op->getValueAPF(), Ty);
 
-    LibFunc Fp128Func = NotLibFunc;
-    if (Ty->isFP128Ty() && TLI->getLibFunc(Name, Fp128Func) &&
-        TLI->has(Fp128Func) && Fp128Func == LibFunc_logl)
-      return ConstantFP::get(Ty, logf128(Op->getValueAPF().convertToQuad()));
+      LibFunc Fp128Func = NotLibFunc;
+      if (TLI->getLibFunc(Name, Fp128Func) && TLI->has(Fp128Func) &&
+          Fp128Func == LibFunc_logl)
+        return ConstantFoldFP128(logf128, Op->getValueAPF(), Ty);
+    }
 #endif
 
     if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/logf128.ll b/llvm/test/Transforms/InstSimplify/ConstProp/logf128.ll
index d48713cf0a7bd..284f8942ba6b7 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/logf128.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/logf128.ll
@@ -72,7 +72,8 @@ define fp128 @log_e_smallest_number_larger_than_one(){
 
 define fp128 @log_e_negative_2(){
 ; CHECK-LABEL: define fp128 @log_e_negative_2() {
-; CHECK-NEXT:    ret fp128 0xL00000000000000007FFF800000000000
+; CHECK-NEXT:    [[A:%.*]] = call fp128 @llvm.log.f128(fp128 noundef 0xL0000000000000000C000000000000000)
+; CHECK-NEXT:    ret fp128 [[A]]
 ;
   %A = call fp128 @llvm.log.f128(fp128 noundef 0xL0000000000000000C000000000000000)
   ret fp128 %A
@@ -80,7 +81,8 @@ define fp128 @log_e_negative_2(){
 
 define fp128 @log_e_0(){
 ; CHECK-LABEL: define fp128 @log_e_0() {
-; CHECK-NEXT:    ret fp128 0xL0000000000000000FFFF000000000000
+; CHECK-NEXT:    [[A:%.*]] = call fp128 @llvm.log.f128(fp128 noundef 0xL00000000000000000000000000000000)
+; CHECK-NEXT:    ret fp128 [[A]]
 ;
   %A = call fp128 @llvm.log.f128(fp128 noundef 0xL00000000000000000000000000000000)
   ret fp128 %A
@@ -88,7 +90,8 @@ define fp128 @log_e_0(){
 
 define fp128 @log_e_negative_0(){
 ; CHECK-LABEL: define fp128 @log_e_negative_0() {
-; CHECK-NEXT:    ret fp128 0xL0000000000000000FFFF000000000000
+; CHECK-NEXT:    [[A:%.*]] = call fp128 @llvm.log.f128(fp128 noundef 0xL00000000000000008000000000000000)
+; CHECK-NEXT:    ret fp128 [[A]]
 ;
   %A = call fp128 @llvm.log.f128(fp128 noundef 0xL00000000000000008000000000000000)
   ret fp128 %A
@@ -104,7 +107,8 @@ define fp128 @log_e_infinity(){
 
 define fp128 @log_e_negative_infinity(){
 ; CHECK-LABEL: define fp128 @log_e_negative_infinity() {
-; CHECK-NEXT:    ret fp128 0xL00000000000000007FFF800000000000
+; CHECK-NEXT:    [[A:%.*]] = call fp128 @llvm.log.f128(fp128 noundef 0xL0000000000000000FFFF000000000000)
+; CHECK-NEXT:    ret fp128 [[A]]
 ;
   %A = call fp128 @llvm.log.f128(fp128 noundef 0xL0000000000000000FFFF000000000000)
   ret fp128 %A
@@ -112,7 +116,8 @@ define fp128 @log_e_negative_infinity(){
 
 define fp128 @log_e_nan(){
 ; CHECK-LABEL: define fp128 @log_e_nan() {
-; CHECK-NEXT:    ret fp128 0xL00000000000000007FFF800000000001
+; CHECK-NEXT:    [[A:%.*]] = call fp128 @llvm.log.f128(fp128 noundef 0xL00000000000000007FFF000000000001)
+; CHECK-NEXT:    ret fp128 [[A]]
 ;
   %A = call fp128 @llvm.log.f128(fp128 noundef 0xL00000000000000007FFF000000000001)
   ret fp128 %A
@@ -120,7 +125,8 @@ define fp128 @log_e_nan(){
 
 define <2 x fp128> @log_e_negative_2_vector(){
 ; CHECK-LABEL: define <2 x fp128> @log_e_negative_2_vector() {
-; CHECK-NEXT:    ret <2 x fp128> <fp128 0xL00000000000000007FFF800000000000, fp128 0xL00000000000000007FFF800000000000>
+; CHECK-NEXT:    [[A:%.*]] = call <2 x fp128> @llvm.log.v2f128(<2 x fp128> <fp128 0xL0000000000000000C000000000000000, fp128 0xL0000000000000000C000000000000001>)
+; CHECK-NEXT:    ret <2 x fp128> [[A]]
 ;
   %A = call <2 x fp128> @llvm.log.v2f128(<2 x fp128> <fp128 0xL0000000000000000C000000000000000, fp128 0xL0000000000000000C000000000000001>)
   ret <2 x fp128> %A
@@ -138,7 +144,7 @@ define fp128 @logl_e_64(){
 define fp128 @logl_e_0(){
 ; CHECK-LABEL: define fp128 @logl_e_0() {
 ; CHECK-NEXT:    [[A:%.*]] = call fp128 @logl(fp128 noundef 0xL00000000000000000000000000000000)
-; CHECK-NEXT:    ret fp128 0xL0000000000000000FFFF000000000000
+; CHECK-NEXT:    ret fp128 [[A]]
 ;
   %A = call fp128 @logl(fp128 noundef 0xL00000000000000000000000000000000)
   ret fp128 %A
@@ -156,7 +162,7 @@ define fp128 @logl_e_infinity(){
 define fp128 @logl_e_nan(){
 ; CHECK-LABEL: define fp128 @logl_e_nan() {
 ; CHECK-NEXT:    [[A:%.*]] = call fp128 @logl(fp128 noundef 0xL00000000000000007FFF000000000001)
-; CHECK-NEXT:    ret fp128 0xL00000000000000007FFF800000000001
+; CHECK-NEXT:    ret fp128 [[A]]
 ;
   %A = call fp128 @logl(fp128 noundef 0xL00000000000000007FFF000000000001)
   ret fp128 %A
@@ -166,7 +172,7 @@ define fp128 @logl_e_nan(){
 define fp128 @logl_e_negative_2(){
 ; CHECK-LABEL: define fp128 @logl_e_negative_2() {
 ; CHECK-NEXT:    [[A:%.*]] = call fp128 @logl(fp128 noundef 0xL0000000000000000C000000000000000)
-; CHECK-NEXT:    ret fp128 0xL00000000000000007FFF800000000000
+; CHECK-NEXT:    ret fp128 [[A]]
 ;
   %A = call fp128 @logl(fp128 noundef 0xL0000000000000000C000000000000000)
   ret fp128 %A



More information about the llvm-commits mailing list