[Mlir-commits] [mlir] 4509fb9 - [MLIR] Simplify key construction/hashing in StorageUniquer

Joe Loser llvmlistbot at llvm.org
Sat Dec 3 16:20:57 PST 2022


Author: Joe Loser
Date: 2022-12-03T17:19:46-07:00
New Revision: 4509fb9c007e13b65153e9e797df4fca00ecb241

URL: https://github.com/llvm/llvm-project/commit/4509fb9c007e13b65153e9e797df4fca00ecb241
DIFF: https://github.com/llvm/llvm-project/commit/4509fb9c007e13b65153e9e797df4fca00ecb241.diff

LOG: [MLIR] Simplify key construction/hashing in StorageUniquer

`getKey` and `getHash` use mutually exclusive overloads based on existence of
methods to determine how to compute get the key or hash, respectively.  This is
a bit verbose with `std::enable_if_t`.  Simplify it a bit by using
`if constexpr` directly.  As an added bonus, this is slightly quicker to
compile.

Differential Revision: https://reviews.llvm.org/D139245

Added: 
    

Modified: 
    mlir/include/mlir/Support/StorageUniquer.h

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Support/StorageUniquer.h b/mlir/include/mlir/Support/StorageUniquer.h
index dc4a6921bc183..39fe1e56d07f3 100644
--- a/mlir/include/mlir/Support/StorageUniquer.h
+++ b/mlir/include/mlir/Support/StorageUniquer.h
@@ -294,45 +294,32 @@ class StorageUniquer {
   //===--------------------------------------------------------------------===//
 
   /// Used to construct an instance of 'ImplTy::KeyTy' if there is an
-  /// 'ImplTy::getKey' function for the provided arguments.
+  /// 'ImplTy::getKey' function for the provided arguments.  Otherwise, then we
+  /// try to directly construct the 'ImplTy::KeyTy' with the provided arguments.
   template <typename ImplTy, typename... Args>
-  static std::enable_if_t<
-      llvm::is_detected<detail::has_impltype_getkey_t, ImplTy, Args...>::value,
-      typename ImplTy::KeyTy>
-  getKey(Args &&...args) {
-    return ImplTy::getKey(args...);
-  }
-  /// If there is no 'ImplTy::getKey' method, then we try to directly construct
-  /// the 'ImplTy::KeyTy' with the provided arguments.
-  template <typename ImplTy, typename... Args>
-  static std::enable_if_t<
-      !llvm::is_detected<detail::has_impltype_getkey_t, ImplTy, Args...>::value,
-      typename ImplTy::KeyTy>
-  getKey(Args &&...args) {
-    return typename ImplTy::KeyTy(args...);
+  static typename ImplTy::KeyTy getKey(Args &&...args) {
+    if constexpr (llvm::is_detected<detail::has_impltype_getkey_t, ImplTy,
+                                    Args...>::value)
+      return ImplTy::getKey(args...);
+    else
+      return typename ImplTy::KeyTy(args...);
   }
 
   //===--------------------------------------------------------------------===//
   // Key Hashing
   //===--------------------------------------------------------------------===//
 
-  /// Used to generate a hash for the 'ImplTy::KeyTy' of a storage instance if
-  /// there is an 'ImplTy::hashKey' overload for 'DerivedKey'.
-  template <typename ImplTy, typename DerivedKey>
-  static std::enable_if_t<
-      llvm::is_detected<detail::has_impltype_hash_t, ImplTy, DerivedKey>::value,
-      ::llvm::hash_code>
-  getHash(const DerivedKey &derivedKey) {
-    return ImplTy::hashKey(derivedKey);
-  }
-  /// If there is no 'ImplTy::hashKey' default to using the 'llvm::DenseMapInfo'
-  /// definition for 'DerivedKey' for generating a hash.
+  /// Used to generate a hash for the `ImplTy` of a storage instance if
+  /// there is a `ImplTy::hashKey.  Otherwise, if there is no `ImplTy::hashKey`
+  /// then default to using the 'llvm::DenseMapInfo' definition for
+  /// 'DerivedKey' for generating a hash.
   template <typename ImplTy, typename DerivedKey>
-  static std::enable_if_t<!llvm::is_detected<detail::has_impltype_hash_t,
-                                             ImplTy, DerivedKey>::value,
-                          ::llvm::hash_code>
-  getHash(const DerivedKey &derivedKey) {
-    return DenseMapInfo<DerivedKey>::getHashValue(derivedKey);
+  static ::llvm::hash_code getHash(const DerivedKey &derivedKey) {
+    if constexpr (llvm::is_detected<detail::has_impltype_hash_t, ImplTy,
+                                    DerivedKey>::value)
+      return ImplTy::hashKey(derivedKey);
+    else
+      return DenseMapInfo<DerivedKey>::getHashValue(derivedKey);
   }
 };
 } // namespace mlir


        


More information about the Mlir-commits mailing list