[llvm] [ADT] Derive FoldingSetNodeIDRef from ArrayRef<unsigned> (NFC) (PR #220824)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 3 08:56:29 PDT 2026
https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/220824
>From 7c87a939892b641f1d4acf3634d4f4385f307a59 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Wed, 2 Sep 2026 22:13:57 -0700
Subject: [PATCH 1/2] [ADT] Derive FoldingSetNodeIDRef from ArrayRef<unsigned>
(NFC)
This patch derives FoldingSetNodeIDRef from ArrayRef<unsigned>,
benefiting from:
- Standard ArrayRef accessors like data() and size() instead of custom
getData() and getSize().
- Range-based for loops directly over FoldingSetNodeIDRef.
- Implicit conversions to and from SmallVector<unsigned>, simplifying
FoldingSetNodeID.
Assisted-by: Antigravity
---
llvm/include/llvm/ADT/FoldingSet.h | 27 ++++++++----------------
llvm/include/llvm/CodeGen/SelectionDAG.h | 3 +--
llvm/lib/Analysis/ScalarEvolution.cpp | 2 +-
llvm/lib/Support/FoldingSet.cpp | 7 +++---
4 files changed, 14 insertions(+), 25 deletions(-)
diff --git a/llvm/include/llvm/ADT/FoldingSet.h b/llvm/include/llvm/ADT/FoldingSet.h
index bf5f39d055c3c..61c91b1b902be 100644
--- a/llvm/include/llvm/ADT/FoldingSet.h
+++ b/llvm/include/llvm/ADT/FoldingSet.h
@@ -16,6 +16,7 @@
#ifndef LLVM_ADT_FOLDINGSET_H
#define LLVM_ADT_FOLDINGSET_H
+#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ADT/EpochTracker.h"
#include "llvm/ADT/Hashing.h"
@@ -119,13 +120,10 @@ template <typename T, typename Enable = void> struct FoldingSetTrait;
/// since the 32-element SmallVector is often much larger than necessary, and
/// the possibility of heap allocation means it requires a non-trivial
/// destructor call.
-class FoldingSetNodeIDRef {
- const unsigned *Data = nullptr;
- size_t Size = 0;
-
+class FoldingSetNodeIDRef : public ArrayRef<unsigned> {
public:
+ using ArrayRef<unsigned>::ArrayRef;
FoldingSetNodeIDRef() = default;
- FoldingSetNodeIDRef(const unsigned *D, size_t S) : Data(D), Size(S) {}
static constexpr unsigned NotAHash = 0;
@@ -134,8 +132,7 @@ class FoldingSetNodeIDRef {
// Never returns NotAHash: FoldingSetBase reserves it for the empty insert
// token and for a node belonging to no set.
unsigned computeHash() const {
- unsigned Hash =
- static_cast<unsigned>(hash_combine_range(Data, Data + Size));
+ unsigned Hash = static_cast<unsigned>(hash_value(*this));
return Hash == NotAHash ? 1 : Hash;
}
@@ -143,17 +140,12 @@ class FoldingSetNodeIDRef {
// on-disk serialization.
unsigned computeStableHash() const {
return static_cast<unsigned>(xxh3_64bits(
- reinterpret_cast<const uint8_t *>(Data), sizeof(unsigned) * Size));
+ reinterpret_cast<const uint8_t *>(data()), sizeof(unsigned) * size()));
}
-
- const unsigned *getData() const { return Data; }
- size_t getSize() const { return Size; }
};
inline bool operator==(FoldingSetNodeIDRef LHS, FoldingSetNodeIDRef RHS) {
- return LHS.getSize() == RHS.getSize() &&
- memcmp(LHS.getData(), RHS.getData(),
- LHS.getSize() * sizeof(*LHS.getData())) == 0;
+ return LHS.equals(RHS);
}
inline bool operator!=(FoldingSetNodeIDRef LHS, FoldingSetNodeIDRef RHS) {
@@ -184,8 +176,7 @@ class FoldingSetNodeID {
public:
FoldingSetNodeID() = default;
- FoldingSetNodeID(FoldingSetNodeIDRef Ref)
- : Bits(Ref.getData(), Ref.getData() + Ref.getSize()) {}
+ FoldingSetNodeID(FoldingSetNodeIDRef Ref) : Bits(Ref) {}
/// Add* - Add various data types to Bit data.
void AddPointer(const void *Ptr) {
@@ -216,7 +207,7 @@ class FoldingSetNodeID {
inline void clear() { Bits.clear(); }
/// The accumulated profile, valid until this object is next modified.
- FoldingSetNodeIDRef getRef() const { return {Bits.data(), Bits.size()}; }
+ FoldingSetNodeIDRef getRef() const { return Bits; }
// Compute a strong hash value for this FoldingSetNodeID, used to lookup the
// node in the FoldingSetBase. The hash value is not guaranteed to be
@@ -227,7 +218,7 @@ class FoldingSetNodeID {
// on-disk serialization.
unsigned computeStableHash() const { return getRef().computeStableHash(); }
- operator FoldingSetNodeIDRef() const { return {Bits.data(), Bits.size()}; }
+ operator FoldingSetNodeIDRef() const { return Bits; }
/// Copy this node's data to a memory region allocated from the
/// given allocator and return a FoldingSetNodeIDRef describing the
diff --git a/llvm/include/llvm/CodeGen/SelectionDAG.h b/llvm/include/llvm/CodeGen/SelectionDAG.h
index fc72f2a2ba0c6..ccf25e50c9578 100644
--- a/llvm/include/llvm/CodeGen/SelectionDAG.h
+++ b/llvm/include/llvm/CodeGen/SelectionDAG.h
@@ -135,8 +135,7 @@ struct SDNodeKeyInfo {
Key.Opcode, DenseMapInfo<const EVT *>::getHashValue(Key.VTs));
for (const SDValue &Op : Key.Ops)
H = detail::combineHashValue(H, DenseMapInfo<SDValue>::getHashValue(Op));
- FoldingSetNodeIDRef Tail = Key.Tail.getRef();
- for (unsigned Word : ArrayRef(Tail.getData(), Tail.getSize()))
+ for (unsigned Word : Key.Tail.getRef())
H = detail::combineHashValue(H, Word);
return H;
}
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index a93ce95e791a7..6233e838a4c54 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -15509,7 +15509,7 @@ SCEVWrapPredicate::getImpliedFlags(const SCEVAddRecExpr *AR,
/// Union predicates don't get cached so create a dummy set ID for it.
SCEVUnionPredicate::SCEVUnionPredicate(ArrayRef<const SCEVPredicate *> Preds,
ScalarEvolution &SE)
- : SCEVPredicate(FoldingSetNodeIDRef(nullptr, 0), P_Union) {
+ : SCEVPredicate(FoldingSetNodeIDRef(), P_Union) {
for (const auto *P : Preds)
add(P, SE);
}
diff --git a/llvm/lib/Support/FoldingSet.cpp b/llvm/lib/Support/FoldingSet.cpp
index 75ef8834a63d4..c7a9c111fd1a4 100644
--- a/llvm/lib/Support/FoldingSet.cpp
+++ b/llvm/lib/Support/FoldingSet.cpp
@@ -25,10 +25,9 @@ using namespace llvm;
// FoldingSetNodeIDRef Implementation
bool llvm::operator<(FoldingSetNodeIDRef LHS, FoldingSetNodeIDRef RHS) {
- if (LHS.getSize() != RHS.getSize())
- return LHS.getSize() < RHS.getSize();
- return memcmp(LHS.getData(), RHS.getData(),
- LHS.getSize() * sizeof(*LHS.getData())) < 0;
+ if (LHS.size() != RHS.size())
+ return LHS.size() < RHS.size();
+ return memcmp(LHS.data(), RHS.data(), LHS.size() * sizeof(unsigned)) < 0;
}
//===----------------------------------------------------------------------===//
>From 189b7504e64019b170878abc9ac2f0ff0233b81b Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 3 Sep 2026 08:43:48 -0700
Subject: [PATCH 2/2] Address comments.
---
llvm/include/llvm/ADT/FoldingSet.h | 1 -
1 file changed, 1 deletion(-)
diff --git a/llvm/include/llvm/ADT/FoldingSet.h b/llvm/include/llvm/ADT/FoldingSet.h
index 61c91b1b902be..4d081f1544e0b 100644
--- a/llvm/include/llvm/ADT/FoldingSet.h
+++ b/llvm/include/llvm/ADT/FoldingSet.h
@@ -123,7 +123,6 @@ template <typename T, typename Enable = void> struct FoldingSetTrait;
class FoldingSetNodeIDRef : public ArrayRef<unsigned> {
public:
using ArrayRef<unsigned>::ArrayRef;
- FoldingSetNodeIDRef() = default;
static constexpr unsigned NotAHash = 0;
More information about the llvm-commits
mailing list