[llvm] 4bdd4eb - [ADT] Don't use getEmptyKey() when hashing std::optional (#202002)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 6 11:00:29 PDT 2026


Author: Fangrui Song
Date: 2026-06-06T11:00:24-07:00
New Revision: 4bdd4eb56704cdbd5d74c6576c06219628cf2255

URL: https://github.com/llvm/llvm-project/commit/4bdd4eb56704cdbd5d74c6576c06219628cf2255
DIFF: https://github.com/llvm/llvm-project/commit/4bdd4eb56704cdbd5d74c6576c06219628cf2255.diff

LOG: [ADT] Don't use getEmptyKey() when hashing std::optional (#202002)

DenseMapInfo<std::optional<T>>::getHashValue() calls T's getEmptyKey()
for the nullopt case. This blocks removing the now-unused getEmptyKey()
from DenseMapInfo specializations: a leaf type that drops getEmptyKey()
fails to compile wherever std::optional<T> is used as a DenseMap key
(e.g. DenseMapInfo<mlir::spirv::StorageClass>).

Compute the hash directly instead. Prerequisite for the getEmptyKey()
removal series; #201281 made getEmptyKey() unused by DenseMap.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/DenseMapInfo.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/DenseMapInfo.h b/llvm/include/llvm/ADT/DenseMapInfo.h
index e8e21e4a15e66..374c952584f28 100644
--- a/llvm/include/llvm/ADT/DenseMapInfo.h
+++ b/llvm/include/llvm/ADT/DenseMapInfo.h
@@ -214,9 +214,9 @@ template <typename T> struct DenseMapInfo<std::optional<T>> {
   static constexpr Optional getEmptyKey() { return {Info::getEmptyKey()}; }
 
   static unsigned getHashValue(const Optional &OptionalVal) {
-    return detail::combineHashValue(
-        OptionalVal.has_value(),
-        Info::getHashValue(OptionalVal.value_or(Info::getEmptyKey())));
+    if (OptionalVal)
+      return detail::combineHashValue(1, Info::getHashValue(*OptionalVal));
+    return 0;
   }
 
   static bool isEqual(const Optional &LHS, const Optional &RHS) {


        


More information about the llvm-commits mailing list