[llvm] [ADT] Hash pointers with densemap::detail::mix (PR #197390)
via llvm-commits
llvm-commits at lists.llvm.org
Wed May 13 01:48:58 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-adt
Author: Fangrui Song (MaskRay)
<details>
<summary>Changes</summary>
The current DenseMapInfo<T*>::getHashValue is very weak.
Distinct bump-allocated pointers — the dominant key shape in clang and
the LLVM IR layer — share their high bits and differ only in low bits
that this hash collapses to a short range of buckets.
Quadratic probing has been masking the weakness because its probe
sequence spreads collisions out, but the latent quality issue still
hurts at high load factor and shows up directly as primary clustering
once probing becomes linear.
DeclContext::lookupImpl over a StoredDeclsMap keyed on DeclarationName
is the hottest example: the underlying IdentifierInfo* comes from
clang's bump allocator, so consecutive identifiers land on consecutive
low-bit hash values.
Switch to densemap::detail::mix, the splitmix64 mixer already defined
above and already used by the >32-bit unsigned integral specialization.
Root cause identified by Claude Opus 4.7
instructions:u has mixed results
https://llvm-compile-time-tracker.com/compare.php?from=c9560139d3053fe5eb49131c0354ed6a67177fbc&to=23d37b18147179d8c908716cd2a8427e5f491d6c&stat=instructions:u
---
Full diff: https://github.com/llvm/llvm-project/pull/197390.diff
1 Files Affected:
- (modified) llvm/include/llvm/ADT/DenseMapInfo.h (+1-2)
``````````diff
diff --git a/llvm/include/llvm/ADT/DenseMapInfo.h b/llvm/include/llvm/ADT/DenseMapInfo.h
index f24aeb4371e7f..832c12fc6482c 100644
--- a/llvm/include/llvm/ADT/DenseMapInfo.h
+++ b/llvm/include/llvm/ADT/DenseMapInfo.h
@@ -84,8 +84,7 @@ struct DenseMapInfo<T*> {
}
static unsigned getHashValue(const T *PtrVal) {
- return (unsigned((uintptr_t)PtrVal) >> 4) ^
- (unsigned((uintptr_t)PtrVal) >> 9);
+ return densemap::detail::mix(reinterpret_cast<uintptr_t>(PtrVal));
}
static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
``````````
</details>
https://github.com/llvm/llvm-project/pull/197390
More information about the llvm-commits
mailing list