[llvm] b91de4c - [IR] Simplify dispatchRecalculateHash and dispatchResetHash (NFC) (#159903)

via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 20 08:15:23 PDT 2025


Author: Kazu Hirata
Date: 2025-09-20T08:15:20-07:00
New Revision: b91de4cbee54cf50f9f92c140a9259bdea81e1d2

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

LOG: [IR] Simplify dispatchRecalculateHash and dispatchResetHash (NFC) (#159903)

This patch simplifies dispatchRecalculateHash and dispatchResetHash
with "constexpr if".

This patch does not inline dispatchRecalculateHash and
dispatchResetHash into their respective call sites.  Using "constexpr
if" in a non-template context like MDNode::uniquify would still
require the discarded branch to be syntactically valid, causing a
compilation error for node types that do not have
recalculateHash/setHash.  Using template functions ensures that the
"constexpr if" is evaluated in a proper template context, allowing the
compiler to fully discard the inactive branch.

Added: 
    

Modified: 
    llvm/include/llvm/IR/Metadata.h
    llvm/lib/IR/Metadata.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/IR/Metadata.h b/llvm/include/llvm/IR/Metadata.h
index 4ba31b5545cb2..990bdc618f240 100644
--- a/llvm/include/llvm/IR/Metadata.h
+++ b/llvm/include/llvm/IR/Metadata.h
@@ -1410,18 +1410,14 @@ class MDNode : public Metadata {
   void eraseFromStore();
 
   template <class NodeTy> struct HasCachedHash;
-  template <class NodeTy>
-  static void dispatchRecalculateHash(NodeTy *N, std::true_type) {
-    N->recalculateHash();
-  }
-  template <class NodeTy>
-  static void dispatchRecalculateHash(NodeTy *, std::false_type) {}
-  template <class NodeTy>
-  static void dispatchResetHash(NodeTy *N, std::true_type) {
-    N->setHash(0);
-  }
-  template <class NodeTy>
-  static void dispatchResetHash(NodeTy *, std::false_type) {}
+  template <class NodeTy> static void dispatchRecalculateHash(NodeTy *N) {
+    if constexpr (HasCachedHash<NodeTy>::value)
+      N->recalculateHash();
+  }
+  template <class NodeTy> static void dispatchResetHash(NodeTy *N) {
+    if constexpr (HasCachedHash<NodeTy>::value)
+      N->setHash(0);
+  }
 
   /// Merge branch weights from two direct callsites.
   static MDNode *mergeDirectCallProfMetadata(MDNode *A, MDNode *B,

diff  --git a/llvm/lib/IR/Metadata.cpp b/llvm/lib/IR/Metadata.cpp
index dc1651462aef4..9cfb0ff4d689a 100644
--- a/llvm/lib/IR/Metadata.cpp
+++ b/llvm/lib/IR/Metadata.cpp
@@ -1003,8 +1003,7 @@ MDNode *MDNode::uniquify() {
 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
   case CLASS##Kind: {                                                          \
     CLASS *SubclassThis = cast<CLASS>(this);                                   \
-    std::bool_constant<HasCachedHash<CLASS>::value> ShouldRecalculateHash;     \
-    dispatchRecalculateHash(SubclassThis, ShouldRecalculateHash);              \
+    dispatchRecalculateHash(SubclassThis);                                     \
     return uniquifyImpl(SubclassThis, getContext().pImpl->CLASS##s);           \
   }
 #include "llvm/IR/Metadata.def"
@@ -1060,8 +1059,7 @@ void MDNode::storeDistinctInContext() {
     llvm_unreachable("Invalid subclass of MDNode");
 #define HANDLE_MDNODE_LEAF(CLASS)                                              \
   case CLASS##Kind: {                                                          \
-    std::bool_constant<HasCachedHash<CLASS>::value> ShouldResetHash;           \
-    dispatchResetHash(cast<CLASS>(this), ShouldResetHash);                     \
+    dispatchResetHash(cast<CLASS>(this));                                      \
     break;                                                                     \
   }
 #include "llvm/IR/Metadata.def"


        


More information about the llvm-commits mailing list