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

via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 27 13:26:11 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

<details>
<summary>Changes</summary>

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.


---
Full diff: https://github.com/llvm/llvm-project/pull/79709.diff


1 Files Affected:

- (modified) llvm/include/llvm/ADT/SmallPtrSet.h (+14-25) 


``````````diff
diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index 4c064397057d37..acc4d0ffc10f3e 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];
 

``````````

</details>


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


More information about the llvm-commits mailing list