[clang] [Serialization] Use specialized decl hash function for GlobalDeclID (PR #95730)

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 17 19:04:43 PDT 2024


https://github.com/ChuanqiXu9 updated https://github.com/llvm/llvm-project/pull/95730

>From 50923aa33f09b2530cfe492a53f70296f9ce9107 Mon Sep 17 00:00:00 2001
From: Chuanqi Xu <yedeng.yd at linux.alibaba.com>
Date: Mon, 17 Jun 2024 11:32:35 +0800
Subject: [PATCH 1/2] [Serialization] Use specialized decl hash function for
 GlobalDeclID

See the comment:
https://github.com/llvm/llvm-project/pull/92083#issuecomment-2168121729

After the patch, https://github.com/llvm/llvm-project/pull/92083, the
lower 32 bits of DeclID can be the same commonly. This may produce many
collisions. It will be best to change the default hash implementation
for uint64_t. But sent this one as a quick workaround.
---
 clang/include/clang/AST/DeclID.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/AST/DeclID.h b/clang/include/clang/AST/DeclID.h
index 32d2ed41a374a..4ad7afb463b18 100644
--- a/clang/include/clang/AST/DeclID.h
+++ b/clang/include/clang/AST/DeclID.h
@@ -230,7 +230,11 @@ template <> struct DenseMapInfo<clang::GlobalDeclID> {
   }
 
   static unsigned getHashValue(const GlobalDeclID &Key) {
-    return DenseMapInfo<DeclID>::getHashValue(Key.get());
+    // Our default hash algorithm for 64 bits integer may not be very good.
+    // In GlobalDeclID's case, it is pretty common that the lower 32 bits can
+    // be same.
+    return DenseMapInfo<uint32_t>::getHashValue(Key.getModuleFileIndex()) ^
+           DenseMapInfo<uint32_t>::getHashValue(Key.getLocalDeclIndex());
   }
 
   static bool isEqual(const GlobalDeclID &L, const GlobalDeclID &R) {

>From 17e0c8d4ac2cd89ac1f9fe11c01cccbd2331d56d Mon Sep 17 00:00:00 2001
From: Chuanqi Xu <yedeng.yd at linux.alibaba.com>
Date: Tue, 18 Jun 2024 10:03:53 +0800
Subject: [PATCH 2/2] Update

---
 clang/include/clang/AST/DeclID.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/clang/include/clang/AST/DeclID.h b/clang/include/clang/AST/DeclID.h
index 4ad7afb463b18..8ee645ec0ecdd 100644
--- a/clang/include/clang/AST/DeclID.h
+++ b/clang/include/clang/AST/DeclID.h
@@ -17,6 +17,7 @@
 #define LLVM_CLANG_AST_DECLID_H
 
 #include "llvm/ADT/DenseMapInfo.h"
+#include "llvm/ADT/Hashing.h"
 #include "llvm/ADT/iterator.h"
 
 #include <climits>
@@ -233,8 +234,8 @@ template <> struct DenseMapInfo<clang::GlobalDeclID> {
     // Our default hash algorithm for 64 bits integer may not be very good.
     // In GlobalDeclID's case, it is pretty common that the lower 32 bits can
     // be same.
-    return DenseMapInfo<uint32_t>::getHashValue(Key.getModuleFileIndex()) ^
-           DenseMapInfo<uint32_t>::getHashValue(Key.getLocalDeclIndex());
+    // FIXME: Remove this when we fix the underlying issue.
+    return llvm::hash_value(Key.get());
   }
 
   static bool isEqual(const GlobalDeclID &L, const GlobalDeclID &R) {



More information about the cfe-commits mailing list