[llvm] [IR] Simplify dispatchRecalculateHash and dispatchResetHash (NFC) (PR #159903)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 19 22:06:11 PDT 2025
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/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.
>From 8c5992bcfe514aca7fce7568069a34c5625e2cbe Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 18 Sep 2025 09:34:16 -0700
Subject: [PATCH] [IR] Simplify dispatchRecalculateHash and dispatchResetHash
(NFC)
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.
---
llvm/include/llvm/IR/Metadata.h | 20 ++++++++------------
llvm/lib/IR/Metadata.cpp | 6 ++----
2 files changed, 10 insertions(+), 16 deletions(-)
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 fc78a5b299f49..1e2e3e39994d3 100644
--- a/llvm/lib/IR/Metadata.cpp
+++ b/llvm/lib/IR/Metadata.cpp
@@ -1007,8 +1007,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"
@@ -1064,8 +1063,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