Skip to content

Conversation

kazutakahirata
Copy link
Contributor

This patch consolidates two implementations of getHashValueImpl into
one with "constexpr if", which should be more readable than the
SFINAE-based approach.

The same applies to isEqualImpl.

This patch consolidates two implementations of getHashValueImpl into
one with "constexpr if", which should be more readable than the
SFINAE-based approach.

The same applies to isEqualImpl.
@llvmbot
Copy link
Member

llvmbot commented Sep 4, 2025

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

This patch consolidates two implementations of getHashValueImpl into
one with "constexpr if", which should be more readable than the
SFINAE-based approach.

The same applies to isEqualImpl.


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

1 Files Affected:

  • (modified) llvm/include/llvm/ADT/DenseMapInfo.h (+20-26)
diff --git a/llvm/include/llvm/ADT/DenseMapInfo.h b/llvm/include/llvm/ADT/DenseMapInfo.h
index ec7a116856bb4..717156c4bd196 100644
--- a/llvm/include/llvm/ADT/DenseMapInfo.h
+++ b/llvm/include/llvm/ADT/DenseMapInfo.h
@@ -179,41 +179,35 @@ template <typename... Ts> struct DenseMapInfo<std::tuple<Ts...>> {
     return Tuple(DenseMapInfo<Ts>::getTombstoneKey()...);
   }
 
-  template <unsigned I>
-  static unsigned getHashValueImpl(const Tuple &values, std::false_type) {
-    using EltType = std::tuple_element_t<I, Tuple>;
-    std::integral_constant<bool, I + 1 == sizeof...(Ts)> atEnd;
-    return detail::combineHashValue(
-        DenseMapInfo<EltType>::getHashValue(std::get<I>(values)),
-        getHashValueImpl<I + 1>(values, atEnd));
-  }
-
-  template <unsigned I>
-  static unsigned getHashValueImpl(const Tuple &, std::true_type) {
-    return 0;
+  template <unsigned I> static unsigned getHashValueImpl(const Tuple &values) {
+    if constexpr (I == sizeof...(Ts))
+      return 0;
+    else {
+      using EltType = std::tuple_element_t<I, Tuple>;
+      return detail::combineHashValue(
+          DenseMapInfo<EltType>::getHashValue(std::get<I>(values)),
+          getHashValueImpl<I + 1>(values));
+    }
   }
 
   static unsigned getHashValue(const std::tuple<Ts...> &values) {
-    std::integral_constant<bool, 0 == sizeof...(Ts)> atEnd;
-    return getHashValueImpl<0>(values, atEnd);
+    return getHashValueImpl<0>(values);
   }
 
   template <unsigned I>
-  static bool isEqualImpl(const Tuple &lhs, const Tuple &rhs, std::false_type) {
-    using EltType = std::tuple_element_t<I, Tuple>;
-    std::integral_constant<bool, I + 1 == sizeof...(Ts)> atEnd;
-    return DenseMapInfo<EltType>::isEqual(std::get<I>(lhs), std::get<I>(rhs)) &&
-           isEqualImpl<I + 1>(lhs, rhs, atEnd);
-  }
-
-  template <unsigned I>
-  static bool isEqualImpl(const Tuple &, const Tuple &, std::true_type) {
-    return true;
+  static bool isEqualImpl(const Tuple &lhs, const Tuple &rhs) {
+    if constexpr (I == sizeof...(Ts))
+      return true;
+    else {
+      using EltType = std::tuple_element_t<I, Tuple>;
+      return DenseMapInfo<EltType>::isEqual(std::get<I>(lhs),
+                                            std::get<I>(rhs)) &&
+             isEqualImpl<I + 1>(lhs, rhs);
+    }
   }
 
   static bool isEqual(const Tuple &lhs, const Tuple &rhs) {
-    std::integral_constant<bool, 0 == sizeof...(Ts)> atEnd;
-    return isEqualImpl<0>(lhs, rhs, atEnd);
+    return isEqualImpl<0>(lhs, rhs);
   }
 };
 

@kazutakahirata kazutakahirata merged commit ec061cd into llvm:main Sep 4, 2025
11 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20250903_DenseMapInfo_constexpr branch September 4, 2025 16:18
kazutakahirata added a commit to kazutakahirata/llvm-project that referenced this pull request Sep 4, 2025
kazutakahirata added a commit that referenced this pull request Sep 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants