Skip to content

Commit b91de4c

Browse files
[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.
1 parent a04c0f5 commit b91de4c

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

llvm/include/llvm/IR/Metadata.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,18 +1410,14 @@ class MDNode : public Metadata {
14101410
void eraseFromStore();
14111411

14121412
template <class NodeTy> struct HasCachedHash;
1413-
template <class NodeTy>
1414-
static void dispatchRecalculateHash(NodeTy *N, std::true_type) {
1415-
N->recalculateHash();
1416-
}
1417-
template <class NodeTy>
1418-
static void dispatchRecalculateHash(NodeTy *, std::false_type) {}
1419-
template <class NodeTy>
1420-
static void dispatchResetHash(NodeTy *N, std::true_type) {
1421-
N->setHash(0);
1422-
}
1423-
template <class NodeTy>
1424-
static void dispatchResetHash(NodeTy *, std::false_type) {}
1413+
template <class NodeTy> static void dispatchRecalculateHash(NodeTy *N) {
1414+
if constexpr (HasCachedHash<NodeTy>::value)
1415+
N->recalculateHash();
1416+
}
1417+
template <class NodeTy> static void dispatchResetHash(NodeTy *N) {
1418+
if constexpr (HasCachedHash<NodeTy>::value)
1419+
N->setHash(0);
1420+
}
14251421

14261422
/// Merge branch weights from two direct callsites.
14271423
static MDNode *mergeDirectCallProfMetadata(MDNode *A, MDNode *B,

llvm/lib/IR/Metadata.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,8 +1003,7 @@ MDNode *MDNode::uniquify() {
10031003
#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
10041004
case CLASS##Kind: { \
10051005
CLASS *SubclassThis = cast<CLASS>(this); \
1006-
std::bool_constant<HasCachedHash<CLASS>::value> ShouldRecalculateHash; \
1007-
dispatchRecalculateHash(SubclassThis, ShouldRecalculateHash); \
1006+
dispatchRecalculateHash(SubclassThis); \
10081007
return uniquifyImpl(SubclassThis, getContext().pImpl->CLASS##s); \
10091008
}
10101009
#include "llvm/IR/Metadata.def"
@@ -1060,8 +1059,7 @@ void MDNode::storeDistinctInContext() {
10601059
llvm_unreachable("Invalid subclass of MDNode");
10611060
#define HANDLE_MDNODE_LEAF(CLASS) \
10621061
case CLASS##Kind: { \
1063-
std::bool_constant<HasCachedHash<CLASS>::value> ShouldResetHash; \
1064-
dispatchResetHash(cast<CLASS>(this), ShouldResetHash); \
1062+
dispatchResetHash(cast<CLASS>(this)); \
10651063
break; \
10661064
}
10671065
#include "llvm/IR/Metadata.def"

0 commit comments

Comments
 (0)