Skip to content

[libc++] Replace __libcpp_popcount by __builtin_popcountg #133937

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Apr 13, 2025

Conversation

winner245
Copy link
Contributor

__libcpp_popcount was previously used as a fallback for __builtin_popcountg to ensure compatibility with older compilers (Clang 18 and earlier), as __builtin_popcountg became available in Clang 19. Now that support for Clang 18 has been officially dropped in #130142, we can now safely replace all instances of __libcpp_popcount with __builtin_popcountg and eliminate the fallback logic.

@winner245 winner245 changed the title [libc++] Replace __libcpp_popcount by __popcount [libc++] Replace __libcpp_popcount by __builtin_popcountg Apr 1, 2025
@winner245 winner245 force-pushed the remove-__libcpp_popcount branch from ee6edcd to cd2ed7c Compare April 3, 2025 02:42
@winner245 winner245 force-pushed the remove-__libcpp_popcount branch from cd2ed7c to eb75b91 Compare April 11, 2025 15:23
@winner245 winner245 force-pushed the remove-__libcpp_popcount branch from eb75b91 to 590452a Compare April 12, 2025 12:02
@winner245 winner245 marked this pull request as ready for review April 12, 2025 13:40
@winner245 winner245 requested a review from a team as a code owner April 12, 2025 13:40
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Apr 12, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 12, 2025

@llvm/pr-subscribers-libcxx

Author: Peng Liu (winner245)

Changes

__libcpp_popcount was previously used as a fallback for __builtin_popcountg to ensure compatibility with older compilers (Clang 18 and earlier), as __builtin_popcountg became available in Clang 19. Now that support for Clang 18 has been officially dropped in #130142, we can now safely replace all instances of __libcpp_popcount with __builtin_popcountg and eliminate the fallback logic.


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

2 Files Affected:

  • (modified) libcxx/include/__bit/popcount.h (-43)
  • (modified) libcxx/include/__stop_token/atomic_unique_lock.h (+1-1)
diff --git a/libcxx/include/__bit/popcount.h b/libcxx/include/__bit/popcount.h
index b1d93caab0081..622c8efd7938a 100644
--- a/libcxx/include/__bit/popcount.h
+++ b/libcxx/include/__bit/popcount.h
@@ -6,9 +6,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-// TODO: __builtin_popcountg is available since Clang 19 and GCC 14. When support for older versions is dropped, we can
-//  refactor this code to exclusively use __builtin_popcountg.
-
 #ifndef _LIBCPP___BIT_POPCOUNT_H
 #define _LIBCPP___BIT_POPCOUNT_H
 
@@ -27,50 +24,10 @@ _LIBCPP_PUSH_MACROS
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned __x) _NOEXCEPT {
-  return __builtin_popcount(__x);
-}
-
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned long __x) _NOEXCEPT {
-  return __builtin_popcountl(__x);
-}
-
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned long long __x) _NOEXCEPT {
-  return __builtin_popcountll(__x);
-}
-
-template <class _Tp>
-[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __popcount_impl(_Tp __t) _NOEXCEPT {
-  if _LIBCPP_CONSTEXPR (sizeof(_Tp) <= sizeof(unsigned int)) {
-    return std::__libcpp_popcount(static_cast<unsigned int>(__t));
-  } else if _LIBCPP_CONSTEXPR (sizeof(_Tp) <= sizeof(unsigned long)) {
-    return std::__libcpp_popcount(static_cast<unsigned long>(__t));
-  } else if _LIBCPP_CONSTEXPR (sizeof(_Tp) <= sizeof(unsigned long long)) {
-    return std::__libcpp_popcount(static_cast<unsigned long long>(__t));
-  } else {
-#if _LIBCPP_STD_VER == 11
-    return __t != 0 ? std::__libcpp_popcount(static_cast<unsigned long long>(__t)) +
-                          std::__popcount_impl<_Tp>(__t >> numeric_limits<unsigned long long>::digits)
-                    : 0;
-#else
-    int __ret = 0;
-    while (__t != 0) {
-      __ret += std::__libcpp_popcount(static_cast<unsigned long long>(__t));
-      __t >>= std::numeric_limits<unsigned long long>::digits;
-    }
-    return __ret;
-#endif
-  }
-}
-
 template <class _Tp>
 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __popcount(_Tp __t) _NOEXCEPT {
   static_assert(is_unsigned<_Tp>::value, "__popcount only works with unsigned types");
-#if __has_builtin(__builtin_popcountg) // TODO (LLVM 21): This can be dropped once we only support Clang >= 19.
   return __builtin_popcountg(__t);
-#else
-  return std::__popcount_impl(__t);
-#endif
 }
 
 #if _LIBCPP_STD_VER >= 20
diff --git a/libcxx/include/__stop_token/atomic_unique_lock.h b/libcxx/include/__stop_token/atomic_unique_lock.h
index a698260ac7bbd..05e8f223167f1 100644
--- a/libcxx/include/__stop_token/atomic_unique_lock.h
+++ b/libcxx/include/__stop_token/atomic_unique_lock.h
@@ -28,7 +28,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 // and LockedBit is the value of State when the lock bit is set, e.g  1 << 2
 template <class _State, _State _LockedBit>
 class _LIBCPP_AVAILABILITY_SYNC __atomic_unique_lock {
-  static_assert(std::__libcpp_popcount(static_cast<unsigned long long>(_LockedBit)) == 1,
+  static_assert(std::__popcount(static_cast<unsigned long long>(_LockedBit)) == 1,
                 "LockedBit must be an integer where only one bit is set");
 
   std::atomic<_State>& __state_;

Copy link
Member

@mordante mordante left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM!

@winner245 winner245 merged commit 703cfe7 into llvm:main Apr 13, 2025
83 checks passed
@winner245 winner245 deleted the remove-__libcpp_popcount branch April 13, 2025 12:42
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
`__libcpp_popcount` was previously used as a fallback for `__builtin_popcountg` to ensure compatibility with older compilers (Clang 18 and earlier), as `__builtin_popcountg` became available in Clang 19. Now that support for Clang 18 has been officially dropped in llvm#130142, we can now safely  replace all instances of `__libcpp_popcount` with `__builtin_popcountg` and eliminate the fallback logic.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants