Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Serialization] Use specialized decl hash function for GlobalDeclID #95730

Merged
merged 2 commits into from
Jun 18, 2024

Conversation

ChuanqiXu9
Copy link
Member

@ChuanqiXu9 ChuanqiXu9 commented Jun 17, 2024

See the comment:
#92083 (comment)

After the patch, #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.

Feel free to update this if you prefer other hash functions.

See the comment:
llvm#92083 (comment)

After the patch, llvm#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.
@ChuanqiXu9 ChuanqiXu9 requested a review from ilya-biryukov June 17, 2024 03:35
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 17, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 17, 2024

@llvm/pr-subscribers-clang

Author: Chuanqi Xu (ChuanqiXu9)

Changes

See the comment:
#92083 (comment)

After the patch, #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.


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

1 Files Affected:

  • (modified) clang/include/clang/AST/DeclID.h (+5-1)
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) {

// 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()) ^
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use hash_combine from llvm/ADT/Hash.h?

Copy link
Contributor

@ilya-biryukov ilya-biryukov Jun 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to using hash_combine, it should provide better results than xor.
Update: after asking around, I think hash_value from ADT/Hashing.h on the underlying int64 value would give good results here and make the code even simpler.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

@ilya-biryukov ilya-biryukov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@ChuanqiXu9 ChuanqiXu9 merged commit 47f5707 into llvm:main Jun 18, 2024
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants