[llvm] [ADT] Use a constexpr version of llvm::bit_ceil (NFC) (PR #79709)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 29 16:55:23 PST 2024


https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/79709

>From e57ebdbbbc4075217aed0df337da57822dcc75c1 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Wed, 24 Jan 2024 13:03:27 -0800
Subject: [PATCH 1/4] [ADT] Use a constexpr version of llvm::bit_ceil (NFC)

This patch replaces the template trick with a constexpr function that
is more readable.  Once C++20 is available in our code base, we can
remove the constexpr function in favor of std::bit_ceil.
---
 llvm/include/llvm/ADT/SmallPtrSet.h | 39 +++++++++++------------------
 1 file changed, 14 insertions(+), 25 deletions(-)

diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index 4c064397057d3..acc4d0ffc10f3 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -25,6 +25,7 @@
 #include <cstring>
 #include <initializer_list>
 #include <iterator>
+#include <limits>
 #include <utility>
 
 namespace llvm {
@@ -311,30 +312,17 @@ class LLVM_DEBUGEPOCHBASE_HANDLEBASE_EMPTYBASE SmallPtrSetIterator
   }
 };
 
-/// RoundUpToPowerOfTwo - This is a helper template that rounds N up to the next
-/// power of two (which means N itself if N is already a power of two).
-template<unsigned N>
-struct RoundUpToPowerOfTwo;
-
-/// RoundUpToPowerOfTwoH - If N is not a power of two, increase it.  This is a
-/// helper template used to implement RoundUpToPowerOfTwo.
-template<unsigned N, bool isPowerTwo>
-struct RoundUpToPowerOfTwoH {
-  enum { Val = N };
-};
-template<unsigned N>
-struct RoundUpToPowerOfTwoH<N, false> {
-  enum {
-    // We could just use NextVal = N+1, but this converges faster.  N|(N-1) sets
-    // the right-most zero bits to one all at once, e.g. 0b0011000 -> 0b0011111.
-    Val = RoundUpToPowerOfTwo<(N|(N-1)) + 1>::Val
-  };
-};
-
-template<unsigned N>
-struct RoundUpToPowerOfTwo {
-  enum { Val = RoundUpToPowerOfTwoH<N, (N&(N-1)) == 0>::Val };
-};
+namespace detail {
+// A constexpr version of llvm::bit_ceil.
+// TODO: Replace this with std::bit_ceil once C++20 is available.
+static constexpr size_t RoundUpToPowerOfTwo(size_t X) {
+  size_t C = 1;
+  size_t CMax = C << (std::numeric_limits<size_t>::digits - 1);
+  while (C < X && C < CMax)
+    C <<= 1;
+  return C;
+}
+} // namespace detail
 
 /// A templated base class for \c SmallPtrSet which provides the
 /// typesafe interface that is common across all small sizes.
@@ -457,7 +445,8 @@ class SmallPtrSet : public SmallPtrSetImpl<PtrType> {
   using BaseT = SmallPtrSetImpl<PtrType>;
 
   // Make sure that SmallSize is a power of two, round up if not.
-  enum { SmallSizePowTwo = RoundUpToPowerOfTwo<SmallSize>::Val };
+  static constexpr size_t SmallSizePowTwo =
+      detail::RoundUpToPowerOfTwo(SmallSize);
   /// SmallStorage - Fixed size storage used in 'small mode'.
   const void *SmallStorage[SmallSizePowTwo];
 

>From 0a5ec1e840026664917d7b948aac9eb1d339930e Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 29 Jan 2024 16:06:09 -0800
Subject: [PATCH 2/4] Make RoundUpToPowerOfTwo a private member static
 constexpr function

---
 llvm/include/llvm/ADT/SmallPtrSet.h | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index acc4d0ffc10f3..a5aace26301ae 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -312,18 +312,6 @@ class LLVM_DEBUGEPOCHBASE_HANDLEBASE_EMPTYBASE SmallPtrSetIterator
   }
 };
 
-namespace detail {
-// A constexpr version of llvm::bit_ceil.
-// TODO: Replace this with std::bit_ceil once C++20 is available.
-static constexpr size_t RoundUpToPowerOfTwo(size_t X) {
-  size_t C = 1;
-  size_t CMax = C << (std::numeric_limits<size_t>::digits - 1);
-  while (C < X && C < CMax)
-    C <<= 1;
-  return C;
-}
-} // namespace detail
-
 /// A templated base class for \c SmallPtrSet which provides the
 /// typesafe interface that is common across all small sizes.
 ///
@@ -444,6 +432,16 @@ class SmallPtrSet : public SmallPtrSetImpl<PtrType> {
 
   using BaseT = SmallPtrSetImpl<PtrType>;
 
+  // A constexpr version of llvm::bit_ceil.
+  // TODO: Replace this with std::bit_ceil once C++20 is available.
+  static constexpr size_t RoundUpToPowerOfTwo(size_t X) {
+    size_t C = 1;
+    size_t CMax = C << (std::numeric_limits<size_t>::digits - 1);
+    while (C < X && C < CMax)
+      C <<= 1;
+    return C;
+  }
+
   // Make sure that SmallSize is a power of two, round up if not.
   static constexpr size_t SmallSizePowTwo =
       detail::RoundUpToPowerOfTwo(SmallSize);

>From 3ec889f12dbf6b76fa3509ec38a01961255d54c8 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 29 Jan 2024 16:50:42 -0800
Subject: [PATCH 3/4] clang-format

---
 llvm/include/llvm/ADT/SmallPtrSet.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index a5aace26301ae..69076ed326927 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -443,8 +443,7 @@ class SmallPtrSet : public SmallPtrSetImpl<PtrType> {
   }
 
   // Make sure that SmallSize is a power of two, round up if not.
-  static constexpr size_t SmallSizePowTwo =
-      detail::RoundUpToPowerOfTwo(SmallSize);
+  static constexpr size_t SmallSizePowTwo = RoundUpToPowerOfTwo(SmallSize);
   /// SmallStorage - Fixed size storage used in 'small mode'.
   const void *SmallStorage[SmallSizePowTwo];
 



More information about the llvm-commits mailing list