Skip to content

Conversation

kazutakahirata
Copy link
Contributor

This patch uses "constexpr if" to merge two implementations of
addRangeElementsImpl. While the line count does not change much, the
"if" condition should be a lot more readable than in std::enable_if.

This patch uses "constexpr if" to merge two implementations of
addRangeElementsImpl.  While the line count does not change much, the
"if" condition should be a lot more readable than in std::enable_if.
@llvmbot
Copy link
Member

llvmbot commented Sep 12, 2025

@llvm/pr-subscribers-llvm-support

Author: Kazu Hirata (kazutakahirata)

Changes

This patch uses "constexpr if" to merge two implementations of
addRangeElementsImpl. While the line count does not change much, the
"if" condition should be a lot more readable than in std::enable_if.


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

1 Files Affected:

  • (modified) llvm/include/llvm/Support/HashBuilder.h (+10-12)
diff --git a/llvm/include/llvm/Support/HashBuilder.h b/llvm/include/llvm/Support/HashBuilder.h
index 097110874400d..17fbc3f96ed04 100644
--- a/llvm/include/llvm/Support/HashBuilder.h
+++ b/llvm/include/llvm/Support/HashBuilder.h
@@ -366,18 +366,16 @@ class HashBuilder : public HashBuilderBase<HasherT> {
   HashBuilder &addRangeElementsImpl(ForwardIteratorT First,
                                     ForwardIteratorT Last,
                                     std::forward_iterator_tag) {
-    for (auto It = First; It != Last; ++It)
-      add(*It);
-    return *this;
-  }
-
-  template <typename T>
-  std::enable_if_t<hashbuilder_detail::IsHashableData<T>::value &&
-                       Endianness == llvm::endianness::native,
-                   HashBuilder &>
-  addRangeElementsImpl(T *First, T *Last, std::forward_iterator_tag) {
-    this->update(ArrayRef(reinterpret_cast<const uint8_t *>(First),
-                          (Last - First) * sizeof(T)));
+    using T = typename std::iterator_traits<ForwardIteratorT>::value_type;
+    if constexpr (std::is_pointer_v<ForwardIteratorT> &&
+                  hashbuilder_detail::IsHashableData<T>::value &&
+                  Endianness == llvm::endianness::native) {
+      this->update(ArrayRef(reinterpret_cast<const uint8_t *>(First),
+                            (Last - First) * sizeof(T)));
+    } else {
+      for (auto It = First; It != Last; ++It)
+        add(*It);
+    }
     return *this;
   }
 };

@kazutakahirata kazutakahirata merged commit b22a97d into llvm:main Sep 12, 2025
11 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20250911_Support_HashBuilder branch September 12, 2025 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants