Skip to content

Conversation

@kazutakahirata
Copy link
Contributor

This patch replaces ConstantLog2 with CTLog2. ConstantLog2 only
operates on alignment values, making the two interchangeable in this
context. CTLog2 also has the benefit of a static_assert that ensures
its parameter is a power of two.

This patch replaces ConstantLog2 with CTLog2.  ConstantLog2 only
operates on alignment values, making the two interchangeable in this
context.  CTLog2 also has the benefit of a static_assert that ensures
its parameter is a power of two.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" llvm:support labels Sep 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 10, 2025

@llvm/pr-subscribers-llvm-support

@llvm/pr-subscribers-clang

Author: Kazu Hirata (kazutakahirata)

Changes

This patch replaces ConstantLog2 with CTLog2. ConstantLog2 only
operates on alignment values, making the two interchangeable in this
context. CTLog2 also has the benefit of a static_assert that ensures
its parameter is a power of two.


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

2 Files Affected:

  • (modified) clang/include/clang/AST/Expr.h (+1-1)
  • (modified) llvm/include/llvm/Support/PointerLikeTypeTraits.h (+3-11)
diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index 23a0996f02725..d2b872458ae82 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -1038,7 +1038,7 @@ class Expr : public ValueStmt {
 // PointerLikeTypeTraits is specialized so it can be used with a forward-decl of
 // Expr. Verify that we got it right.
 static_assert(llvm::PointerLikeTypeTraits<Expr *>::NumLowBitsAvailable <=
-                  llvm::detail::ConstantLog2<alignof(Expr)>::value,
+                  llvm::CTLog2<alignof(Expr)>(),
               "PointerLikeTypeTraits<Expr*> assumes too much alignment.");
 
 using ConstantExprKind = Expr::ConstantExprKind;
diff --git a/llvm/include/llvm/Support/PointerLikeTypeTraits.h b/llvm/include/llvm/Support/PointerLikeTypeTraits.h
index 1b15f930bd87d..0ac919fd9c0b9 100644
--- a/llvm/include/llvm/Support/PointerLikeTypeTraits.h
+++ b/llvm/include/llvm/Support/PointerLikeTypeTraits.h
@@ -15,8 +15,8 @@
 #define LLVM_SUPPORT_POINTERLIKETYPETRAITS_H
 
 #include "llvm/Support/DataTypes.h"
+#include "llvm/Support/MathExtras.h"
 #include <cassert>
-#include <type_traits>
 
 namespace llvm {
 
@@ -25,12 +25,6 @@ namespace llvm {
 template <typename T> struct PointerLikeTypeTraits;
 
 namespace detail {
-/// A tiny meta function to compute the log2 of a compile time constant.
-template <size_t N>
-struct ConstantLog2
-    : std::integral_constant<size_t, ConstantLog2<N / 2>::value + 1> {};
-template <> struct ConstantLog2<1> : std::integral_constant<size_t, 0> {};
-
 // Provide a trait to check if T is pointer-like.
 template <typename T, typename U = void> struct HasPointerLikeTypeTraits {
   static const bool value = false;
@@ -57,8 +51,7 @@ template <typename T> struct PointerLikeTypeTraits<T *> {
   static inline void *getAsVoidPointer(T *P) { return P; }
   static inline T *getFromVoidPointer(void *P) { return static_cast<T *>(P); }
 
-  static constexpr int NumLowBitsAvailable =
-      detail::ConstantLog2<alignof(T)>::value;
+  static constexpr int NumLowBitsAvailable = CTLog2<alignof(T)>();
 };
 
 template <> struct PointerLikeTypeTraits<void *> {
@@ -123,8 +116,7 @@ template <> struct PointerLikeTypeTraits<uintptr_t> {
 /// potentially use alignment attributes on functions to satisfy that.
 template <int Alignment, typename FunctionPointerT>
 struct FunctionPointerLikeTypeTraits {
-  static constexpr int NumLowBitsAvailable =
-      detail::ConstantLog2<Alignment>::value;
+  static constexpr int NumLowBitsAvailable = CTLog2<Alignment>();
   static inline void *getAsVoidPointer(FunctionPointerT P) {
     assert((reinterpret_cast<uintptr_t>(P) &
             ~((uintptr_t)-1 << NumLowBitsAvailable)) == 0 &&

Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConstantLog2 name is better though

@kazutakahirata
Copy link
Contributor Author

ConstantLog2 name is better though

@arsenm Yeah, CTLog2 is a bit cryptic. Let me follow up by renaming CTLog2 to ConstantLog2 after this patch. Thanks for reviews as always.

@kazutakahirata kazutakahirata merged commit 9a64fa7 into llvm:main Sep 10, 2025
13 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20250909_Support_PointerLike_ConstantLog2_CTLog2 branch September 10, 2025 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category llvm:support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants