[llvm] [DenseMap] Update combineHashValue (PR #95970)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 18 11:49:58 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-adt

Author: Fangrui Song (MaskRay)

<details>
<summary>Changes</summary>

`combineHashValue` is a custom bit mixer from 2008
(5fc8ab6d187aefbf1d2cbd36e191e675b14db8f6) used for std::pair and
std::tuple. It has a long dependency chain and slow. Replace it with
Moremur from Pelle Evensen[1]

Measured time to compute [0,1000000000) values on an i7-11850H:

* old: 1.152s
* new: 0.769s

[1]: https://mostlymangling.blogspot.com/2019/12/stronger-better-morer-moremur-better.html


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


1 Files Affected:

- (modified) llvm/include/llvm/ADT/DenseMapInfo.h (+8-10) 


``````````diff
diff --git a/llvm/include/llvm/ADT/DenseMapInfo.h b/llvm/include/llvm/ADT/DenseMapInfo.h
index 5b7dce7b53c62..ba05309e4e413 100644
--- a/llvm/include/llvm/ADT/DenseMapInfo.h
+++ b/llvm/include/llvm/ADT/DenseMapInfo.h
@@ -26,17 +26,15 @@ namespace llvm {
 namespace detail {
 
 /// Simplistic combination of 32-bit hash values into 32-bit hash values.
+/// This uses a Murmur3-type mixer "Moremur" from Pelle Evensen.
 static inline unsigned combineHashValue(unsigned a, unsigned b) {
-  uint64_t key = (uint64_t)a << 32 | (uint64_t)b;
-  key += ~(key << 32);
-  key ^= (key >> 22);
-  key += ~(key << 13);
-  key ^= (key >> 8);
-  key += (key << 3);
-  key ^= (key >> 15);
-  key += ~(key << 27);
-  key ^= (key >> 31);
-  return (unsigned)key;
+  uint64_t x = (uint64_t)a << 32 | (uint64_t)b;
+  x ^= x >> 27;
+  x *= 0x3C79AC492BA7B653UL;
+  x ^= x >> 33;
+  x *= 0x1C69B3F74AC4AE35UL;
+  x ^= x >> 27;
+  return (unsigned)x;
 }
 
 } // end namespace detail

``````````

</details>


https://github.com/llvm/llvm-project/pull/95970


More information about the llvm-commits mailing list