-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
[LLVM][ADT] Convert llvm::hash_code to unsigned explicitly in DenseMapInfo #77743
[LLVM][ADT] Convert llvm::hash_code to unsigned explicitly in DenseMapInfo #77743
Conversation
…pInfo The getHashValue() signature returns a value of type 'unsigned' while the hash_code could only be implicitly converted to 'size_t'. Depending on the C++ implementation, this may or may not be a narrowing conversion. On some platform/compiler combination, this becomes a warning. To avoid the warning (and better highlight the narrowing), do an explicit conversion instead. Co-authored-by: Orest Chura <orest.chura@intel.com>
@llvm/pr-subscribers-llvm-adt Author: Andrei Golubev (andrey-golubev) ChangesThe getHashValue() signature returns a value of type 'unsigned' while the hash_code could only be implicitly converted to 'size_t'. Depending on the C++ implementation, this may or may not be a narrowing conversion. On some platform/compiler combination, this becomes a warning. To avoid the warning (and better highlight the narrowing), do an explicit conversion instead. Full diff: https://github.com/llvm/llvm-project/pull/77743.diff 1 Files Affected:
diff --git a/llvm/include/llvm/ADT/Hashing.h b/llvm/include/llvm/ADT/Hashing.h
index 4e82ba9a6fba2a..a5477362a50793 100644
--- a/llvm/include/llvm/ADT/Hashing.h
+++ b/llvm/include/llvm/ADT/Hashing.h
@@ -677,7 +677,9 @@ template <typename T> hash_code hash_value(const std::optional<T> &arg) {
template <> struct DenseMapInfo<hash_code, void> {
static inline hash_code getEmptyKey() { return hash_code(-1); }
static inline hash_code getTombstoneKey() { return hash_code(-2); }
- static unsigned getHashValue(hash_code val) { return val; }
+ static unsigned getHashValue(hash_code val) {
+ return static_cast<unsigned>(size_t(val));
+ }
static bool isEqual(hash_code LHS, hash_code RHS) { return LHS == RHS; }
};
|
@lattner @dwblaikie @joker-eph @River707 @silvasean judging from your "recent" activity in the area, you might be good reviewers here. Please feel free to add any other person who might be more suitable. |
Shouldn't this return |
I agree but I think the between changing the type (~signature) everywhere and doing the explicit thing for the case I picked the latter. Guessing overall this is a bigger topic for discussion on whether the hashing should really return (potentially) 32-bit values when e.g. 64-bit values are used as input (and current API also contradicts P.S.: this problem only appeared in our project due to this specialization participating in some MLIR code, would've gone completely unnoticed otherwise. |
Yeah, if But I think the right solution would be to do the same thing as is done for |
So from what I understand, there's: Now,
this would essentially mean smth like:
my gut feeling is that we don't really need constant multiplication: given that the hash_code was computed using some proper hashing mechanism, multiplying by a constant does produce equivalent value distribution? (it's the original but just "shifted" by that multiplication?). BUT: this is valid if we don't "trim" the bytes afterwards, which we do by taking only So the bulk of the discussion is how |
Fair enough. |
@dwblaikie do you mind clicking the merge as well? (I don't have the rights at the moment!) |
The getHashValue() signature returns a value of type 'unsigned' while the hash_code could only be implicitly converted to 'size_t'. Depending on the C++ implementation, this may or may not be a narrowing conversion.
On some platform/compiler combination, this becomes a warning. To avoid the warning (and better highlight the narrowing), do an explicit conversion instead.