[llvm] WIP/POC: Constant Fold Logf128 calls (PR #84501)

via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 8 07:35:36 PST 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff 8cfb71613c452dd45a84a74affe8464bfd33de02 2e49b54123fed92e1a2188989a696310c9c4a725 -- llvm/unittests/Analysis/ConstantLogf128.cpp llvm/include/llvm/ADT/APFloat.h llvm/include/llvm/ADT/APInt.h llvm/include/llvm/IR/Constants.h llvm/lib/Analysis/ConstantFolding.cpp llvm/lib/IR/Constants.cpp llvm/lib/Support/APFloat.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 8b74d90655..b85f051b4c 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -1693,12 +1693,12 @@ public:
     return APInt(sizeof(float) * CHAR_BIT, llvm::bit_cast<uint32_t>(V));
   }
 
-  static APInt longDoubleToBits(long double V){
+  static APInt longDoubleToBits(long double V) {
     assert(sizeof(long double) == 16 && "Expected 16 byte long double");
 
     const uint64_t Words[2] = {
-      static_cast<uint64_t>(V),
-      static_cast<uint64_t>(llvm::bit_cast<__uint128_t>(V) >> 64),
+        static_cast<uint64_t>(V),
+        static_cast<uint64_t>(llvm::bit_cast<__uint128_t>(V) >> 64),
     };
     return APInt(sizeof(long double) * CHAR_BIT, 2, Words);
   }
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 12199b7461..11a6307d92 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1678,9 +1678,8 @@ 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";
@@ -1764,13 +1763,13 @@ inline bool llvm_fenv_testexcept() {
   return false;
 }
 
-Constant *ConstantFoldLogf128(const APFloat &V, Type *Ty){
-  #ifdef HAS_LOGF128
-    long double l = logf128(V.convertToQuad());
-    return ConstantFP::get128(Ty, l);
-  #else
-    return nullptr;
-  #endif
+Constant *ConstantFoldLogf128(const APFloat &V, Type *Ty) {
+#ifdef HAS_LOGF128
+  long double l = logf128(V.convertToQuad());
+  return ConstantFP::get128(Ty, l);
+#else
+  return nullptr;
+#endif
 }
 
 Constant *ConstantFoldFP(double (*NativeFP)(double), const APFloat &V,
@@ -2104,7 +2103,8 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
     if (IntrinsicID == Intrinsic::canonicalize)
       return constantFoldCanonicalize(Ty, Call, U);
 
-    if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy() && !Ty->isFP128Ty())
+    if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy() &&
+        !Ty->isFP128Ty())
       return nullptr;
 
     // Use internal versions of these intrinsics.
@@ -2351,7 +2351,8 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
         return ConstantFoldFP(log, APF, Ty);
       break;
     case LibFunc_logl:
-      if (!APF.isNegative() && !APF.isZero() && TLI->has(Func) && Ty->isFP128Ty())
+      if (!APF.isNegative() && !APF.isZero() && TLI->has(Func) &&
+          Ty->isFP128Ty())
         return ConstantFoldLogf128(APF, Ty);
       break;
     case LibFunc_log2:
diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index b231906fee..d822613b2c 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/lib/Support/APFloat.cpp
@@ -3671,7 +3671,7 @@ double IEEEFloat::convertToDouble() const {
 }
 
 long double IEEEFloat::convertToQuad() const {
-  assert(semantics == (const llvm::fltSemantics*)&semIEEEquad &&
+  assert(semantics == (const llvm::fltSemantics *)&semIEEEquad &&
          "Float semantics are not IEEEquads");
   APInt api = bitcastToAPInt();
   return api.bitsToQuad();
diff --git a/llvm/unittests/Analysis/ConstantLogf128.cpp b/llvm/unittests/Analysis/ConstantLogf128.cpp
index bf0f447275..fc1bcaa3d9 100644
--- a/llvm/unittests/Analysis/ConstantLogf128.cpp
+++ b/llvm/unittests/Analysis/ConstantLogf128.cpp
@@ -6,18 +6,19 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "gtest/gtest.h"
 #include "llvm/Analysis/ConstantFolding.h"
-#include "llvm/IR/InstrTypes.h"
-#include "llvm/IR/IRBuilder.h"
-#include "llvm/CodeGen/GlobalISel/CallLowering.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/CodeGen/GlobalISel/CallLowering.h"
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/InstrTypes.h"
+#include "gtest/gtest.h"
 
 using namespace llvm;
 
 namespace {
 
-class ConstantFoldLogf128Fixture :public ::testing ::TestWithParam<std::string>{
+class ConstantFoldLogf128Fixture
+    : public ::testing ::TestWithParam<std::string> {
 protected:
   std::string FuncName;
 };
@@ -28,22 +29,26 @@ TEST_P(ConstantFoldLogf128Fixture, ConstantFoldLogf128) {
   Module MainModule("Logf128TestModule", Context);
   MainModule.setTargetTriple("aarch64-unknown-linux");
 
-  Type* FP128Ty = Type::getFP128Ty(Context);
-  FunctionType* FP128Prototype = FunctionType::get(FP128Ty, false);
-  Function* Logf128TestFunction = Function::Create(FP128Prototype, Function::ExternalLinkage, "logf128test", MainModule);
-  BasicBlock *EntryBlock = BasicBlock::Create(Context, "entry", Logf128TestFunction);
+  Type *FP128Ty = Type::getFP128Ty(Context);
+  FunctionType *FP128Prototype = FunctionType::get(FP128Ty, false);
+  Function *Logf128TestFunction = Function::Create(
+      FP128Prototype, Function::ExternalLinkage, "logf128test", MainModule);
+  BasicBlock *EntryBlock =
+      BasicBlock::Create(Context, "entry", Logf128TestFunction);
   Builder.SetInsertPoint(EntryBlock);
 
-  FunctionType* FP128FP128Prototype = FunctionType::get(FP128Ty, {FP128Ty}, false);
-  Constant* Constant2L = ConstantFP::get128(FP128Ty, 2.0L);
+  FunctionType *FP128FP128Prototype =
+      FunctionType::get(FP128Ty, {FP128Ty}, false);
+  Constant *Constant2L = ConstantFP::get128(FP128Ty, 2.0L);
 
   std::string FunctionName = GetParam();
-  Function* Logl = Function::Create(FP128FP128Prototype, Function::ExternalLinkage, FunctionName, MainModule);
-  CallInst* LoglCall = Builder.CreateCall(Logl, Constant2L);
+  Function *Logl = Function::Create(
+      FP128FP128Prototype, Function::ExternalLinkage, FunctionName, MainModule);
+  CallInst *LoglCall = Builder.CreateCall(Logl, Constant2L);
 
   TargetLibraryInfoImpl TLII(Triple(MainModule.getTargetTriple()));
   TargetLibraryInfo TLI(TLII, Logf128TestFunction);
-  Constant* FoldResult = ConstantFoldCall(LoglCall, Logl, Constant2L, &TLI);
+  Constant *FoldResult = ConstantFoldCall(LoglCall, Logl, Constant2L, &TLI);
 
 #ifndef HAS_LOGF128
   ASSERT_TRUE(FoldResult == nullptr);
@@ -53,18 +58,17 @@ TEST_P(ConstantFoldLogf128Fixture, ConstantFoldLogf128) {
 
   APFloat APF = ConstantLog->getValueAPF();
   char LongDoubleHexString[0xFF];
-  unsigned Size = APF.convertToHexString(LongDoubleHexString, 32, true, APFloatBase::roundingMode::NearestTiesToAway);
+  unsigned Size =
+      APF.convertToHexString(LongDoubleHexString, 32, true,
+                             APFloatBase::roundingMode::NearestTiesToAway);
   EXPECT_GT(Size, 0U);
 
-  ASSERT_STREQ(LongDoubleHexString, std::string("0X1.62E42FEFA39E0000000000000000000P-1").c_str());
+  ASSERT_STREQ(LongDoubleHexString,
+               std::string("0X1.62E42FEFA39E0000000000000000000P-1").c_str());
 #endif
 }
 
-INSTANTIATE_TEST_SUITE_P(
-  ConstantFoldLogf128,
-  ConstantFoldLogf128Fixture,
-  ::testing::Values("logl", "llvm.log.f128")
-);
-
+INSTANTIATE_TEST_SUITE_P(ConstantFoldLogf128, ConstantFoldLogf128Fixture,
+                         ::testing::Values("logl", "llvm.log.f128"));
 
 } // end anonymous namespace

``````````

</details>


https://github.com/llvm/llvm-project/pull/84501


More information about the llvm-commits mailing list