[llvm] [Hashing] Replace CityHash mixers with xxh3 (PR #194567)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sat May 9 16:27:54 PDT 2026
https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/194567
>From 23bca84cab85dbba9efa43dc8ad83632df234576 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Mon, 27 Apr 2026 23:34:13 -0700
Subject: [PATCH 1/2] [Hashing] Replace CityHash mixers with xxh3
Replace the CityHash-style mixer in hash_combine and (transitively)
hash_value(std::basic_string) with a flatten-and-call into xxh3_64bits,
a modern hash superior to CityHash.
hash_value(int) / hash_value(ptr) keep the existing Murmur-style
hash_16_bytes mixer; those are the dominant DenseMap key paths and a
fully-inline 16-byte mix beats inlining xxh3's larger 0..16-byte short
path.
To break dependency cycle: xxHash64, xxh3_64bits, and xxh3_128bits
ArrayRef/StringRef overloads move from llvm/Support/xxhash.h to inline
overloads in llvm/ADT/ArrayRef.h and llvm/ADT/StringRef.h, so xxhash.h
has no ADT dependencies.
A variant that inlined xxh3's 0..16-byte fast path at every
combine_bytes call site (vs. always calling out-of-line xxh3_64bits)
showed no measurable compile-time improvement on the tracker.
llvm-compile-time-tracker.com (CTMark, instructions:u)
```
stage1-O0-g -1.64% (sqlite3 -3.42%)
stage1-aarch64-O0-g -1.41% (sqlite3 -2.71%)
stage1-ReleaseLTO-g -1.14% (tramp3d-v4 -1.64%)
stage1-O3 -0.44%
stage1-ReleaseThinLTO -0.43%
stage2-O3 -0.16%
```
DenseMap-of-pointer paths (dominant at -O3) are untouched, so higher-
optimization configs see smaller wins as expected. opt's .text shrinks
~92 KB. Subsumes the StringRef-only carve-out proposed in #191115.
Aided by Claude opus 4.7
---
llvm/include/llvm/ADT/ArrayRef.h | 14 +
llvm/include/llvm/ADT/FoldingSet.h | 4 +-
llvm/include/llvm/ADT/Hashing.h | 389 ++++----------------------
llvm/include/llvm/ADT/StableHashing.h | 5 +-
llvm/include/llvm/ADT/StringRef.h | 12 +
llvm/include/llvm/Support/xxhash.h | 20 +-
llvm/lib/Support/xxhash.cpp | 19 +-
7 files changed, 103 insertions(+), 360 deletions(-)
diff --git a/llvm/include/llvm/ADT/ArrayRef.h b/llvm/include/llvm/ADT/ArrayRef.h
index eafc4330a1b1b..976eb6ee040de 100644
--- a/llvm/include/llvm/ADT/ArrayRef.h
+++ b/llvm/include/llvm/ADT/ArrayRef.h
@@ -13,6 +13,7 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Compiler.h"
+#include "llvm/Support/xxhash.h"
#include <algorithm>
#include <array>
#include <cassert>
@@ -562,6 +563,19 @@ namespace llvm {
return hash_combine_range(S);
}
+ /// Inline ArrayRef overloads of the xxhash entry points declared
+ /// out-of-line in llvm/Support/xxhash.h. They live here so xxhash.h can stay
+ /// free of ADT dependencies.
+ inline uint64_t xxHash64(ArrayRef<uint8_t> data) {
+ return xxHash64(data.data(), data.size());
+ }
+ inline uint64_t xxh3_64bits(ArrayRef<uint8_t> data) {
+ return xxh3_64bits(data.data(), data.size());
+ }
+ inline XXH128_hash_t xxh3_128bits(ArrayRef<uint8_t> data) {
+ return xxh3_128bits(data.data(), data.size());
+ }
+
// Provide DenseMapInfo for ArrayRefs.
template <typename T> struct DenseMapInfo<ArrayRef<T>, void> {
static inline ArrayRef<T> getEmptyKey() {
diff --git a/llvm/include/llvm/ADT/FoldingSet.h b/llvm/include/llvm/ADT/FoldingSet.h
index 0322dc5bddfe3..590b7ca76e322 100644
--- a/llvm/include/llvm/ADT/FoldingSet.h
+++ b/llvm/include/llvm/ADT/FoldingSet.h
@@ -187,8 +187,8 @@ class FoldingSetNodeIDRef {
// Compute a deterministic hash value across processes that is suitable for
// on-disk serialization.
unsigned computeStableHash() const {
- return static_cast<unsigned>(xxh3_64bits(ArrayRef(
- reinterpret_cast<const uint8_t *>(Data), sizeof(unsigned) * Size)));
+ return static_cast<unsigned>(xxh3_64bits(
+ reinterpret_cast<const uint8_t *>(Data), sizeof(unsigned) * Size));
}
LLVM_ABI bool operator==(FoldingSetNodeIDRef) const;
diff --git a/llvm/include/llvm/ADT/Hashing.h b/llvm/include/llvm/ADT/Hashing.h
index 0f96b857bff16..937822e783b12 100644
--- a/llvm/include/llvm/ADT/Hashing.h
+++ b/llvm/include/llvm/ADT/Hashing.h
@@ -50,9 +50,11 @@
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/SwapByteOrder.h"
#include "llvm/Support/type_traits.h"
-#include <algorithm>
+#include "llvm/Support/xxhash.h"
+#include <array>
#include <cassert>
#include <cstring>
+#include <memory>
#include <optional>
#include <string>
#include <tuple>
@@ -136,14 +138,6 @@ template <typename T> hash_code hash_value(const std::optional<T> &arg);
namespace hashing {
namespace detail {
-inline uint64_t fetch64(const char *p) {
- uint64_t result;
- std::memcpy(&result, p, sizeof(result));
- if (sys::IsBigEndianHost)
- sys::swapByteOrder(result);
- return result;
-}
-
inline uint32_t fetch32(const char *p) {
uint32_t result;
std::memcpy(&result, p, sizeof(result));
@@ -152,22 +146,6 @@ inline uint32_t fetch32(const char *p) {
return result;
}
-/// Some primes between 2^63 and 2^64 for various uses.
-static constexpr uint64_t k0 = 0xc3a5c85c97cb3127ULL;
-static constexpr uint64_t k1 = 0xb492b66fbe98f273ULL;
-static constexpr uint64_t k2 = 0x9ae16a3b2f90404fULL;
-static constexpr uint64_t k3 = 0xc949d7c7509e6557ULL;
-
-/// Bitwise right rotate.
-/// Normally this will compile to a single instruction, especially if the
-/// shift is a manifest constant.
-constexpr uint64_t rotate(uint64_t val, size_t shift) {
- // Avoid shifting by 64: doing so yields an undefined result.
- return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
-}
-
-constexpr uint64_t shift_mix(uint64_t val) { return val ^ (val >> 47); }
-
constexpr uint64_t hash_16_bytes(uint64_t low, uint64_t high) {
// Murmur-inspired hashing.
const uint64_t kMul = 0x9ddfea08eb382d69ULL;
@@ -179,134 +157,6 @@ constexpr uint64_t hash_16_bytes(uint64_t low, uint64_t high) {
return b;
}
-constexpr uint64_t hash_1to3_bytes(const char *s, size_t len, uint64_t seed) {
- uint8_t a = s[0];
- uint8_t b = s[len >> 1];
- uint8_t c = s[len - 1];
- uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
- uint32_t z = static_cast<uint32_t>(len) + (static_cast<uint32_t>(c) << 2);
- return shift_mix(y * k2 ^ z * k3 ^ seed) * k2;
-}
-
-inline uint64_t hash_4to8_bytes(const char *s, size_t len, uint64_t seed) {
- uint64_t a = fetch32(s);
- return hash_16_bytes(len + (a << 3), seed ^ fetch32(s + len - 4));
-}
-
-inline uint64_t hash_9to16_bytes(const char *s, size_t len, uint64_t seed) {
- uint64_t a = fetch64(s);
- uint64_t b = fetch64(s + len - 8);
- return hash_16_bytes(seed ^ a, rotate(b + len, len)) ^ b;
-}
-
-inline uint64_t hash_17to32_bytes(const char *s, size_t len, uint64_t seed) {
- uint64_t a = fetch64(s) * k1;
- uint64_t b = fetch64(s + 8);
- uint64_t c = fetch64(s + len - 8) * k2;
- uint64_t d = fetch64(s + len - 16) * k0;
- return hash_16_bytes(llvm::rotr<uint64_t>(a - b, 43) +
- llvm::rotr<uint64_t>(c ^ seed, 30) + d,
- a + llvm::rotr<uint64_t>(b ^ k3, 20) - c + len + seed);
-}
-
-inline uint64_t hash_33to64_bytes(const char *s, size_t len, uint64_t seed) {
- uint64_t z = fetch64(s + 24);
- uint64_t a = fetch64(s) + (len + fetch64(s + len - 16)) * k0;
- uint64_t b = llvm::rotr<uint64_t>(a + z, 52);
- uint64_t c = llvm::rotr<uint64_t>(a, 37);
- a += fetch64(s + 8);
- c += llvm::rotr<uint64_t>(a, 7);
- a += fetch64(s + 16);
- uint64_t vf = a + z;
- uint64_t vs = b + llvm::rotr<uint64_t>(a, 31) + c;
- a = fetch64(s + 16) + fetch64(s + len - 32);
- z = fetch64(s + len - 8);
- b = llvm::rotr<uint64_t>(a + z, 52);
- c = llvm::rotr<uint64_t>(a, 37);
- a += fetch64(s + len - 24);
- c += llvm::rotr<uint64_t>(a, 7);
- a += fetch64(s + len - 16);
- uint64_t wf = a + z;
- uint64_t ws = b + llvm::rotr<uint64_t>(a, 31) + c;
- uint64_t r = shift_mix((vf + ws) * k2 + (wf + vs) * k0);
- return shift_mix((seed ^ (r * k0)) + vs) * k2;
-}
-
-inline uint64_t hash_short(const char *s, size_t length, uint64_t seed) {
- if (length >= 4 && length <= 8)
- return hash_4to8_bytes(s, length, seed);
- if (length > 8 && length <= 16)
- return hash_9to16_bytes(s, length, seed);
- if (length > 16 && length <= 32)
- return hash_17to32_bytes(s, length, seed);
- if (length > 32)
- return hash_33to64_bytes(s, length, seed);
- if (length != 0)
- return hash_1to3_bytes(s, length, seed);
-
- return k2 ^ seed;
-}
-
-/// The intermediate state used during hashing.
-/// Currently, the algorithm for computing hash codes is based on CityHash and
-/// keeps 56 bytes of arbitrary state.
-struct hash_state {
- uint64_t h0 = 0, h1 = 0, h2 = 0, h3 = 0, h4 = 0, h5 = 0, h6 = 0;
-
- /// Create a new hash_state structure and initialize it based on the
- /// seed and the first 64-byte chunk.
- /// This effectively performs the initial mix.
- static hash_state create(const char *s, uint64_t seed) {
- hash_state state = {0,
- seed,
- hash_16_bytes(seed, k1),
- llvm::rotr<uint64_t>(seed ^ k1, 49),
- seed * k1,
- shift_mix(seed),
- 0};
- state.h6 = hash_16_bytes(state.h4, state.h5);
- state.mix(s);
- return state;
- }
-
- /// Mix 32-bytes from the input sequence into the 16-bytes of 'a'
- /// and 'b', including whatever is already in 'a' and 'b'.
- static void mix_32_bytes(const char *s, uint64_t &a, uint64_t &b) {
- a += fetch64(s);
- uint64_t c = fetch64(s + 24);
- b = llvm::rotr<uint64_t>(b + a + c, 21);
- uint64_t d = a;
- a += fetch64(s + 8) + fetch64(s + 16);
- b += llvm::rotr<uint64_t>(a, 44) + d;
- a += c;
- }
-
- /// Mix in a 64-byte buffer of data.
- /// We mix all 64 bytes even when the chunk length is smaller, but we
- /// record the actual length.
- void mix(const char *s) {
- h0 = llvm::rotr<uint64_t>(h0 + h1 + h3 + fetch64(s + 8), 37) * k1;
- h1 = llvm::rotr<uint64_t>(h1 + h4 + fetch64(s + 48), 42) * k1;
- h0 ^= h6;
- h1 += h3 + fetch64(s + 40);
- h2 = llvm::rotr<uint64_t>(h2 + h5, 33) * k1;
- h3 = h4 * k1;
- h4 = h0 + h5;
- mix_32_bytes(s, h3, h4);
- h5 = h2 + h6;
- h6 = h1 + fetch64(s + 16);
- mix_32_bytes(s + 32, h5, h6);
- std::swap(h2, h0);
- }
-
- /// Compute the final 64-bit hash code value based on the current
- /// state and the length of bytes hashed.
- constexpr uint64_t finalize(size_t length) {
- return hash_16_bytes(hash_16_bytes(h3, h5) + shift_mix(h1) * k1 + h2,
- hash_16_bytes(h4, h6) + shift_mix(length) * k1 + h0);
- }
-};
-
/// In LLVM_ENABLE_ABI_BREAKING_CHECKS builds, the seed is non-deterministic
/// per process (address of a function in LLVMSupport) to prevent having users
/// depend on the particular hash values. On platforms without ASLR, this is
@@ -320,6 +170,13 @@ inline uint64_t get_execution_seed() {
#endif
}
+/// Hash a contiguous byte buffer to a hash_code. The execution seed is XORed
+/// into the result (not propagated through the avalanche), so a given byte
+/// stream produces the same xxh3 output modulo the per-process seed.
+inline hash_code combine_bytes(const char *data, size_t len) {
+ return xxh3_64bits(reinterpret_cast<const uint8_t *>(data), len) ^
+ get_execution_seed();
+}
/// Trait to indicate whether a type's bits can be hashed directly.
///
@@ -362,63 +219,39 @@ template <typename T> auto get_hashable_data(const T &value) {
}
}
-/// Helper to store data from a value into a buffer and advance the
-/// pointer into that buffer.
-///
-/// This routine first checks whether there is enough space in the provided
-/// buffer, and if not immediately returns false. If there is space, it
-/// copies the underlying bytes of value into the buffer, advances the
-/// buffer_ptr past the copied bytes, and returns true.
-template <typename T>
-bool store_and_advance(char *&buffer_ptr, char *buffer_end, const T& value,
- size_t offset = 0) {
- size_t store_size = sizeof(value) - offset;
- if (buffer_ptr + store_size > buffer_end)
- return false;
- const char *value_data = reinterpret_cast<const char *>(&value);
- std::memcpy(buffer_ptr, value_data + offset, store_size);
- buffer_ptr += store_size;
- return true;
-}
-
/// Implement the combining of integral values into a hash_code.
///
/// This overload is selected when the value type of the iterator is
/// integral. Rather than computing a hash_code for each object and then
/// combining them, this (as an optimization) directly combines the integers.
+///
+/// xxh3 has no streaming entry point in libLLVMSupport, so the byte stream is
+/// flattened to a buffer and hashed in one shot. A 64-byte on-stack buffer
+/// covers the common cases; longer non-contiguous ranges (the prior chunked
+/// CityHash impl was streaming and never allocated) fall back to the heap.
template <typename InputIteratorT>
hash_code hash_combine_range_impl(InputIteratorT first, InputIteratorT last) {
- const uint64_t seed = get_execution_seed();
- char buffer[64], *buffer_ptr = buffer;
- char *const buffer_end = std::end(buffer);
- while (first != last && store_and_advance(buffer_ptr, buffer_end,
- get_hashable_data(*first)))
- ++first;
- if (first == last)
- return hash_short(buffer, buffer_ptr - buffer, seed);
- assert(buffer_ptr == buffer_end);
-
- hash_state state = state.create(buffer, seed);
- size_t length = 64;
- while (first != last) {
- // Fill up the buffer. We don't clear it, which re-mixes the last round
- // when only a partial 64-byte chunk is left.
- buffer_ptr = buffer;
- while (first != last && store_and_advance(buffer_ptr, buffer_end,
- get_hashable_data(*first)))
- ++first;
-
- // Rotate the buffer if we did a partial fill in order to simulate doing
- // a mix of the last 64-bytes. That is how the algorithm works when we
- // have a contiguous byte sequence, and we want to emulate that here.
- std::rotate(buffer, buffer_ptr, buffer_end);
-
- // Mix this chunk into the current state.
- state.mix(buffer);
- length += buffer_ptr - buffer;
- };
-
- return state.finalize(length);
+ alignas(uint64_t) char stack_buf[64];
+ std::unique_ptr<char[]> heap_buf;
+ char *buf = stack_buf;
+ size_t cap = sizeof(stack_buf);
+ size_t len = 0;
+ for (; first != last; ++first) {
+ auto data = get_hashable_data(*first);
+ if (len + sizeof(data) > cap) {
+ size_t new_cap = cap * 2;
+ while (new_cap < len + sizeof(data))
+ new_cap *= 2;
+ std::unique_ptr<char[]> new_buf(new char[new_cap]);
+ std::memcpy(new_buf.get(), buf, len);
+ heap_buf = std::move(new_buf);
+ buf = heap_buf.get();
+ cap = new_cap;
+ }
+ std::memcpy(buf + len, &data, sizeof(data));
+ len += sizeof(data);
+ }
+ return combine_bytes(buf, len);
}
/// Implement the combining of integral values into a hash_code.
@@ -432,24 +265,22 @@ hash_code hash_combine_range_impl(InputIteratorT first, InputIteratorT last) {
template <typename ValueT>
std::enable_if_t<is_hashable_data<ValueT>::value, hash_code>
hash_combine_range_impl(ValueT *first, ValueT *last) {
- const uint64_t seed = get_execution_seed();
- const char *s_begin = reinterpret_cast<const char *>(first);
- const char *s_end = reinterpret_cast<const char *>(last);
- const size_t length = std::distance(s_begin, s_end);
- if (length <= 64)
- return hash_short(s_begin, length, seed);
-
- const char *s_aligned_end = s_begin + (length & ~63);
- hash_state state = state.create(s_begin, seed);
- s_begin += 64;
- while (s_begin != s_aligned_end) {
- state.mix(s_begin);
- s_begin += 64;
- }
- if (length & 63)
- state.mix(s_end - 64);
+ return combine_bytes(reinterpret_cast<const char *>(first),
+ size_t(last - first) * sizeof(ValueT));
+}
+
+/// Sum of `sizeof(get_hashable_data(arg))` across a parameter pack.
+template <typename... Ts> constexpr size_t total_hashable_size() {
+ return (size_t(0) + ... +
+ sizeof(decltype(get_hashable_data(std::declval<Ts>()))));
+}
- return state.finalize(length);
+/// Copy `get_hashable_data(arg)` into `buf` at offset `off`, advancing `off`.
+template <typename T>
+inline void store_hashable_data(char *buf, size_t &off, const T &arg) {
+ auto data = get_hashable_data(arg);
+ std::memcpy(buf + off, &data, sizeof(data));
+ off += sizeof(data);
}
} // namespace detail
@@ -472,112 +303,6 @@ template <typename RangeT> hash_code hash_combine_range(RangeT &&R) {
return hash_combine_range(adl_begin(R), adl_end(R));
}
-// Implementation details for hash_combine.
-namespace hashing {
-namespace detail {
-
-/// Helper class to manage the recursive combining of hash_combine
-/// arguments.
-///
-/// This class exists to manage the state and various calls involved in the
-/// recursive combining of arguments used in hash_combine. It is particularly
-/// useful at minimizing the code in the recursive calls to ease the pain
-/// caused by a lack of variadic functions.
-struct hash_combine_recursive_helper {
- char buffer[64] = {};
- hash_state state;
- const uint64_t seed;
-
-public:
- /// Construct a recursive hash combining helper.
- ///
- /// This sets up the state for a recursive hash combine, including getting
- /// the seed and buffer setup.
- hash_combine_recursive_helper()
- : seed(get_execution_seed()) {}
-
- /// Combine one chunk of data into the current in-flight hash.
- ///
- /// This merges one chunk of data into the hash. First it tries to buffer
- /// the data. If the buffer is full, it hashes the buffer into its
- /// hash_state, empties it, and then merges the new chunk in. This also
- /// handles cases where the data straddles the end of the buffer.
- template <typename T>
- char *combine_data(size_t &length, char *buffer_ptr, char *buffer_end, T data) {
- if (!store_and_advance(buffer_ptr, buffer_end, data)) {
- // Check for skew which prevents the buffer from being packed, and do
- // a partial store into the buffer to fill it. This is only a concern
- // with the variadic combine because that formation can have varying
- // argument types.
- size_t partial_store_size = buffer_end - buffer_ptr;
- std::memcpy(buffer_ptr, &data, partial_store_size);
-
- // If the store fails, our buffer is full and ready to hash. We have to
- // either initialize the hash state (on the first full buffer) or mix
- // this buffer into the existing hash state. Length tracks the *hashed*
- // length, not the buffered length.
- if (length == 0) {
- state = state.create(buffer, seed);
- length = 64;
- } else {
- // Mix this chunk into the current state and bump length up by 64.
- state.mix(buffer);
- length += 64;
- }
- // Reset the buffer_ptr to the head of the buffer for the next chunk of
- // data.
- buffer_ptr = buffer;
-
- // Try again to store into the buffer -- this cannot fail as we only
- // store types smaller than the buffer.
- if (!store_and_advance(buffer_ptr, buffer_end, data,
- partial_store_size))
- llvm_unreachable("buffer smaller than stored type");
- }
- return buffer_ptr;
- }
-
- /// Recursive, variadic combining method.
- ///
- /// This function recurses through each argument, combining that argument
- /// into a single hash.
- template <typename T, typename ...Ts>
- hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
- const T &arg, const Ts &...args) {
- buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg));
-
- // Recurse to the next argument.
- return combine(length, buffer_ptr, buffer_end, args...);
- }
-
- /// Base case for recursive, variadic combining.
- ///
- /// The base case when combining arguments recursively is reached when all
- /// arguments have been handled. It flushes the remaining buffer and
- /// constructs a hash_code.
- hash_code combine(size_t length, char *buffer_ptr, char *buffer_end) {
- // Check whether the entire set of values fit in the buffer. If so, we'll
- // use the optimized short hashing routine and skip state entirely.
- if (length == 0)
- return hash_short(buffer, buffer_ptr - buffer, seed);
-
- // Mix the final buffer, rotating it if we did a partial fill in order to
- // simulate doing a mix of the last 64-bytes. That is how the algorithm
- // works when we have a contiguous byte sequence, and we want to emulate
- // that here.
- std::rotate(buffer, buffer_ptr, buffer_end);
-
- // Mix this chunk into the current state.
- state.mix(buffer);
- length += buffer_ptr - buffer;
-
- return state.finalize(length);
- }
-};
-
-} // namespace detail
-} // namespace hashing
-
/// Combine values into a single hash_code.
///
/// This routine accepts a varying number of arguments of any type. It will
@@ -589,10 +314,14 @@ struct hash_combine_recursive_helper {
/// The result is suitable for returning from a user's hash_value
/// *implementation* for their user-defined type. Consumers of a type should
/// *not* call this routine, they should instead call 'hash_value'.
-template <typename ...Ts> hash_code hash_combine(const Ts &...args) {
- // Recursively hash each argument using a helper class.
- ::llvm::hashing::detail::hash_combine_recursive_helper helper;
- return helper.combine(0, helper.buffer, helper.buffer + 64, args...);
+template <typename... Ts> hash_code hash_combine(const Ts &...args) {
+ constexpr size_t Total = hashing::detail::total_hashable_size<Ts...>();
+ // Round up so `data()` is non-null when Total == 0; combine_bytes won't
+ // read the buffer in that case (len=0 short-circuits in xxh3_64bits).
+ std::array<char, Total == 0 ? 1 : Total> buf;
+ size_t off = 0;
+ (hashing::detail::store_hashable_data(buf.data(), off, args), ...);
+ return hashing::detail::combine_bytes(buf.data(), Total);
}
// Implementation details for implementations of hash_value overloads provided
diff --git a/llvm/include/llvm/ADT/StableHashing.h b/llvm/include/llvm/ADT/StableHashing.h
index 0dd83be639424..1beb5b85c9967 100644
--- a/llvm/include/llvm/ADT/StableHashing.h
+++ b/llvm/include/llvm/ADT/StableHashing.h
@@ -30,9 +30,8 @@ namespace llvm {
using stable_hash = uint64_t;
inline stable_hash stable_hash_combine(ArrayRef<stable_hash> Buffer) {
- const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(Buffer.data());
- size_t Size = Buffer.size() * sizeof(stable_hash);
- return xxh3_64bits(ArrayRef<uint8_t>(Ptr, Size));
+ return xxh3_64bits(reinterpret_cast<const uint8_t *>(Buffer.data()),
+ Buffer.size() * sizeof(stable_hash));
}
inline stable_hash stable_hash_combine(stable_hash A, stable_hash B) {
diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index 1a2f2799fe960..c77608d4a2432 100644
--- a/llvm/include/llvm/ADT/StringRef.h
+++ b/llvm/include/llvm/ADT/StringRef.h
@@ -13,6 +13,7 @@
#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Support/Compiler.h"
+#include "llvm/Support/xxhash.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
@@ -941,6 +942,17 @@ inline std::string &operator+=(std::string &buffer, StringRef string) {
/// Compute a hash_code for a StringRef.
[[nodiscard]] LLVM_ABI hash_code hash_value(StringRef S);
+/// Inline StringRef overloads of the xxhash entry points declared out-of-line
+/// in llvm/Support/xxhash.h. They live here so xxhash.h can stay free of ADT
+/// dependencies.
+inline uint64_t xxHash64(StringRef data) {
+ return xxHash64(reinterpret_cast<const uint8_t *>(data.data()), data.size());
+}
+inline uint64_t xxh3_64bits(StringRef data) {
+ return xxh3_64bits(reinterpret_cast<const uint8_t *>(data.data()),
+ data.size());
+}
+
// Provide DenseMapInfo for StringRefs.
template <> struct DenseMapInfo<StringRef, void> {
static inline StringRef getEmptyKey() {
diff --git a/llvm/include/llvm/Support/xxhash.h b/llvm/include/llvm/Support/xxhash.h
index b521adbef3456..15c4f1bfd4563 100644
--- a/llvm/include/llvm/Support/xxhash.h
+++ b/llvm/include/llvm/Support/xxhash.h
@@ -38,19 +38,18 @@
#ifndef LLVM_SUPPORT_XXHASH_H
#define LLVM_SUPPORT_XXHASH_H
-#include "llvm/ADT/ArrayRef.h"
-#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Compiler.h"
+#include <cstddef>
+#include <cstdint>
namespace llvm {
-LLVM_ABI uint64_t xxHash64(llvm::StringRef Data);
-LLVM_ABI uint64_t xxHash64(llvm::ArrayRef<uint8_t> Data);
+// Deprecated pre-xxh3 64-bit hash.
+LLVM_ABI uint64_t xxHash64(const uint8_t *data, size_t len);
-LLVM_ABI uint64_t xxh3_64bits(ArrayRef<uint8_t> data);
-inline uint64_t xxh3_64bits(StringRef data) {
- return xxh3_64bits(ArrayRef(data.bytes_begin(), data.size()));
-}
+/// XXH3's 64-bit variant. Inline ArrayRef and StringRef overloads live in
+/// llvm/ADT/ArrayRef.h and llvm/ADT/StringRef.h.
+LLVM_ABI uint64_t xxh3_64bits(const uint8_t *data, size_t len);
/*-**********************************************************************
* XXH3 128-bit variant
@@ -72,8 +71,9 @@ struct XXH128_hash_t {
}
};
-/// XXH3's 128-bit variant.
-LLVM_ABI XXH128_hash_t xxh3_128bits(ArrayRef<uint8_t> data);
+/// XXH3's 128-bit variant. Inline ArrayRef overload lives in
+/// llvm/ADT/ArrayRef.h.
+LLVM_ABI XXH128_hash_t xxh3_128bits(const uint8_t *data, size_t len);
} // namespace llvm
diff --git a/llvm/lib/Support/xxhash.cpp b/llvm/lib/Support/xxhash.cpp
index cdb76d57e2c1d..6997fed7e8336 100644
--- a/llvm/lib/Support/xxhash.cpp
+++ b/llvm/lib/Support/xxhash.cpp
@@ -100,11 +100,9 @@ static uint64_t XXH64_avalanche(uint64_t hash) {
return hash;
}
-uint64_t llvm::xxHash64(StringRef Data) {
- size_t Len = Data.size();
+uint64_t llvm::xxHash64(const uint8_t *P, size_t Len) {
uint64_t Seed = 0;
- const unsigned char *P = Data.bytes_begin();
- const unsigned char *const BEnd = Data.bytes_end();
+ const uint8_t *const BEnd = P + Len;
uint64_t H64;
if (Len >= 32) {
@@ -160,10 +158,6 @@ uint64_t llvm::xxHash64(StringRef Data) {
return XXH64_avalanche(H64);
}
-uint64_t llvm::xxHash64(ArrayRef<uint8_t> Data) {
- return xxHash64({(const char *)Data.data(), Data.size()});
-}
-
constexpr size_t XXH3_SECRETSIZE_MIN = 136;
constexpr size_t XXH_SECRET_DEFAULT_SIZE = 192;
@@ -550,9 +544,7 @@ static uint64_t XXH3_hashLong_64b(const uint8_t *input, size_t len,
(uint64_t)len * PRIME64_1);
}
-uint64_t llvm::xxh3_64bits(ArrayRef<uint8_t> data) {
- auto *in = data.data();
- size_t len = data.size();
+uint64_t llvm::xxh3_64bits(const uint8_t *in, size_t len) {
if (len <= 16)
return XXH3_len_0to16_64b(in, len, kSecret, 0);
if (len <= 128)
@@ -1020,10 +1012,7 @@ XXH3_hashLong_128b(const uint8_t *input, size_t len, const uint8_t *secret,
return h128;
}
-llvm::XXH128_hash_t llvm::xxh3_128bits(ArrayRef<uint8_t> data) {
- size_t len = data.size();
- const uint8_t *input = data.data();
-
+llvm::XXH128_hash_t llvm::xxh3_128bits(const uint8_t *input, size_t len) {
/*
* If an action is to be taken if `secret` conditions are not respected,
* it should be done here.
>From d6660fe5732405048c7a3810b898b4d8d7c5ccd5 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Sat, 9 May 2026 16:27:44 -0700
Subject: [PATCH 2/2] benchmark; comment
---
llvm/include/llvm/ADT/Hashing.h | 9 ++++-----
llvm/unittests/ADT/HashingTest.cpp | 19 +++++++++++++++++++
2 files changed, 23 insertions(+), 5 deletions(-)
diff --git a/llvm/include/llvm/ADT/Hashing.h b/llvm/include/llvm/ADT/Hashing.h
index 2c49e04a3d148..c3cc37683c79a 100644
--- a/llvm/include/llvm/ADT/Hashing.h
+++ b/llvm/include/llvm/ADT/Hashing.h
@@ -33,11 +33,10 @@
// a single hash_code for their object. They should only logically be used
// within the implementation of a 'hash_value' routine or similar context.
//
-// Note that 'hash_combine_range' contains very special logic for hashing
-// a contiguous array of integers or pointers. This logic is *extremely* fast,
-// on a modern Intel "Gainestown" Xeon (Nehalem uarch) @2.2 GHz, these were
-// benchmarked at over 6.5 GiB/s for large keys, and <20 cycles/hash for keys
-// under 32-bytes.
+// 'hash_combine_range' hashes the byte stream of the range via xxh3. The
+// contiguous-array overload hashes the range in place; the iterator overload
+// materializes the byte stream into a 256-byte on-stack buffer, falling back
+// to the heap for ranges that exceed it.
//
//===----------------------------------------------------------------------===//
diff --git a/llvm/unittests/ADT/HashingTest.cpp b/llvm/unittests/ADT/HashingTest.cpp
index e116ee934a0a4..b6daf8f4695f8 100644
--- a/llvm/unittests/ADT/HashingTest.cpp
+++ b/llvm/unittests/ADT/HashingTest.cpp
@@ -202,6 +202,25 @@ TEST(HashingTest, HashCombineRangeBasicTest) {
EXPECT_EQ(arr5_hash, d_arr5_hash);
}
+TEST(HashingTest, HashCombineRangeIteratorOverInlineBuffer) {
+ // Drive the non-pointer iterator overload past the inline stack buffer
+ // (>256 bytes of hashable data) into the heap-grow path.
+ constexpr size_t N = 100; // 100 * sizeof(size_t) = 800 bytes
+ std::vector<size_t> v(N);
+ for (size_t i = 0; i < N; ++i)
+ v[i] = i * 0x9E3779B97F4A7C15ULL;
+ std::list<size_t> l(v.begin(), v.end());
+
+ // Iterator and pointer paths see the same byte stream and agree past the
+ // inline buffer threshold.
+ EXPECT_EQ(hash_combine_range(v), hash_combine_range(l));
+ EXPECT_EQ(hash_combine_range(l), hash_combine_range(l));
+
+ std::list<size_t> l2 = l;
+ l2.push_back(0xDEADBEEFu);
+ EXPECT_NE(hash_combine_range(l), hash_combine_range(l2));
+}
+
TEST(HashingTest, HashCombineRangeLengthDiff) {
// Test that as only the length varies, we compute different hash codes for
// sequences.
More information about the llvm-commits
mailing list