[llvm-commits] [llvm] r151883 - in /llvm/trunk: include/llvm/ADT/Hashing.h include/llvm/Support/type_traits.h unittests/ADT/HashingTest.cpp

Chandler Carruth chandlerc at gmail.com
Fri Mar 2 01:26:36 PST 2012


Author: chandlerc
Date: Fri Mar  2 03:26:36 2012
New Revision: 151883

URL: http://llvm.org/viewvc/llvm-project?rev=151883&view=rev
Log:
We really want to hash pairs of directly-hashable data as directly
hashable data. This matters when we have pair<T*, U*> as a key, which is
quite common in DenseMap, etc. To that end, we need to detect when this
is safe. The requirements on a generic std::pair<T, U> are:

1) Both T and U must satisfy the existing is_hashable_data trait. Note
   that this includes the requirement that T and U have no internal
   padding bits or other bits not contributing directly to equality.
2) The alignment constraints of std::pair<T, U> do not require padding
   between consecutive objects.
3) The alignment constraints of U and the size of T do not conspire to
   require padding between the first and second elements.

Grow two somewhat magical traits to detect this by forming a pod
structure and inspecting offset artifacts on it. Hopefully this won't
cause any compilers to panic.

Added and adjusted tests now that pairs, even nested pairs, are treated
as just sequences of data.

Thanks to Jeffrey Yasskin for helping me sort through this and reviewing
the somewhat subtle traits.

Modified:
    llvm/trunk/include/llvm/ADT/Hashing.h
    llvm/trunk/include/llvm/Support/type_traits.h
    llvm/trunk/unittests/ADT/HashingTest.cpp

Modified: llvm/trunk/include/llvm/ADT/Hashing.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Hashing.h?rev=151883&r1=151882&r2=151883&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/Hashing.h (original)
+++ llvm/trunk/include/llvm/ADT/Hashing.h Fri Mar  2 03:26:36 2012
@@ -350,6 +350,16 @@
   : integral_constant<bool, ((is_integral<T>::value || is_pointer<T>::value) &&
                              64 % sizeof(T) == 0)> {};
 
+// Special case std::pair to detect when both types are viable and when there
+// is no alignment-derived padding in the pair. This is a bit of a lie because
+// std::pair isn't truly POD, but it's close enough in all reasonable
+// implementations for our use case of hashing the underlying data.
+template <typename T, typename U> struct is_hashable_data<std::pair<T, U> >
+  : integral_constant<bool, (is_hashable_data<T>::value &&
+                             is_hashable_data<U>::value &&
+                             !is_alignment_padded<std::pair<T, U> >::value &&
+                             !is_pod_pair_padded<T, U>::value)> {};
+
 /// \brief Helper to get the hashable data representation for a type.
 /// This variant is enabled when the type itself can be used.
 template <typename T>

Modified: llvm/trunk/include/llvm/Support/type_traits.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/type_traits.h?rev=151883&r1=151882&r2=151883&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/type_traits.h (original)
+++ llvm/trunk/include/llvm/Support/type_traits.h Fri Mar  2 03:26:36 2012
@@ -125,6 +125,24 @@
 template <typename T> struct is_pointer : false_type {};
 template <typename T> struct is_pointer<T*> : true_type {};
 
+/// \brief Metafunction to compute whether a type requires alignment padding.
+/// When true, an object of this type will have padding bytes inside its
+/// 'sizeof' bytes.
+template <typename T> class is_alignment_padded {
+  struct pod_size_tester { T t; char c; };
+public:
+  enum { value = offsetof(pod_size_tester, c) != sizeof(T) };
+};
+
+/// \brief Metafunction to determine whether an adjacent pair of two types will
+/// require padding between them due to alignment.
+template <typename T, typename U> class is_pod_pair_padded {
+  struct pod_pair { T t; U u; };
+  struct pod_char_pair { T t; char c; };
+public:
+  enum { value = offsetof(pod_pair, u) != offsetof(pod_char_pair, c) };
+};
+
   
 // enable_if_c - Enable/disable a template based on a metafunction
 template<bool Cond, typename T = void>

Modified: llvm/trunk/unittests/ADT/HashingTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/HashingTest.cpp?rev=151883&r1=151882&r2=151883&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/HashingTest.cpp (original)
+++ llvm/trunk/unittests/ADT/HashingTest.cpp Fri Mar  2 03:26:36 2012
@@ -66,11 +66,13 @@
   EXPECT_NE(hash_combine(42, 43), hash_value(std::make_pair(42ull, 43ull)));
   EXPECT_NE(hash_combine(42, 43), hash_value(std::make_pair(42, 43ull)));
   EXPECT_NE(hash_combine(42, 43), hash_value(std::make_pair(42ull, 43)));
-  EXPECT_EQ(hash_combine(42, hash_combine(43, hash_combine(44, 45))),
-            hash_value(
-              std::make_pair(42, std::make_pair(43, std::make_pair(44, 45)))));
-  EXPECT_EQ(hash_combine(42, 43), hash_value(std::make_pair(42, 43)));
-  EXPECT_EQ(hash_combine(42, 43), hash_value(std::make_pair(42, 43)));
+
+  // Note that pairs are implicitly flattened to a direct sequence of data and
+  // hashed efficiently as a consequence.
+  EXPECT_EQ(hash_combine(42, 43, 44),
+            hash_value(std::make_pair(42, std::make_pair(43, 44))));
+  EXPECT_EQ(hash_value(std::make_pair(42, std::make_pair(43, 44))),
+            hash_value(std::make_pair(std::make_pair(42, 43), 44)));
 }
 
 template <typename T, size_t N> T *begin(T (&arr)[N]) { return arr; }





More information about the llvm-commits mailing list