[lld] [llvm] [FoldingSet] Add typed lookup/insert/erase/getOrInsert (PR #219644)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 29 00:14:13 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-support
Author: Fangrui Song (MaskRay)
<details>
<summary>Changes</summary>
FindNodeOrInsertPos hands the insertion state to `InsertNode` as a `void *`:
any pointer converts to it, since the switch to linear probing it's the
hash, not a position at all.
Carry it in `FoldingSetInsertToken` instead: a 64-bit value (even on
32-bit platforms) holding the hash plus one, so every hash including
NotAHash yields a non-zero token.
Add lookup/insert as the typed replacements for FindNodeOrInsertPos/
InsertNode, plus erase/getOrInsert, on FoldingSetImpl, FoldingSetVector
and ContextualFoldingSet. The old `void *`-based names will be phased
out.
Migrate RecordKeeper's dozen Init/RecTy FoldingSet pools, the first
caller, to the new API.
LLM-aided
---
Full diff: https://github.com/llvm/llvm-project/pull/219644.diff
4 Files Affected:
- (modified) lld/ELF/SyntheticSections.cpp (+3-3)
- (modified) llvm/include/llvm/ADT/FoldingSet.h (+40-3)
- (modified) llvm/lib/Support/FoldingSet.cpp (+24-10)
- (modified) llvm/lib/TableGen/Record.cpp (+42-45)
``````````diff
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index e2b9bb7590e26..aa7a859a9da9a 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -2875,14 +2875,14 @@ void DebugNamesBaseSection::computeHdrAndAbbrevTable(
FoldingSetNodeID id;
abbrev.Profile(id);
uint32_t newCode;
- void *insertPos;
- if (Abbrev *existing = abbrevSet.FindNodeOrInsertPos(id, insertPos)) {
+ FoldingSetInsertToken token;
+ if (Abbrev *existing = abbrevSet.lookup(id, token)) {
// Found it; we've already seen an identical abbreviation.
newCode = existing->code;
} else {
Abbrev *abbrev2 =
new (abbrevAlloc.Allocate()) Abbrev(std::move(abbrev));
- abbrevSet.InsertNode(abbrev2, insertPos);
+ abbrevSet.insert(abbrev2, token);
abbrevTable.push_back(abbrev2);
newCode = abbrevTable.size();
abbrev2->code = newCode;
diff --git a/llvm/include/llvm/ADT/FoldingSet.h b/llvm/include/llvm/ADT/FoldingSet.h
index c984c00b86fd1..070b23440c7fa 100644
--- a/llvm/include/llvm/ADT/FoldingSet.h
+++ b/llvm/include/llvm/ADT/FoldingSet.h
@@ -288,6 +288,21 @@ class FoldingSetNodeID {
LLVM_ABI FoldingSetNodeIDRef Intern(BumpPtrAllocator &Allocator) const;
};
+/// Insertion token: a failed lookup fills it in, the matching insert consumes
+/// it.
+class FoldingSetInsertToken {
+ uint64_t Value = 0; // hash + 1 while holding a token, 0 otherwise.
+
+ explicit FoldingSetInsertToken(uint32_t Hash) : Value(uint64_t(Hash) + 1) {}
+ uint32_t hash() const { return uint32_t(Value - 1); }
+
+ friend class FoldingSetBase;
+
+public:
+ FoldingSetInsertToken() = default;
+ explicit operator bool() const { return Value != 0; }
+};
+
//===----------------------------------------------------------------------===//
/// Non-templated base class for FoldingSet and ContextualFoldingSet, holding
/// the memory management and probing that does not depend on the node type.
@@ -384,11 +399,16 @@ class FoldingSetBase : public DebugEpochBase {
LLVM_ABI Node *FindNodeOrInsertPos(const FoldingSetNodeID &ID,
void *&InsertPos,
const FoldingSetInfo &Info);
+ LLVM_ABI Node *lookup(const FoldingSetNodeID &ID,
+ FoldingSetInsertToken &Token,
+ const FoldingSetInfo &Info);
/// Insert the specified node into the folding set, knowing that
/// it is not already in the folding set. InsertPos must be obtained from
- /// FindNodeOrInsertPos for an ID that \p N profiles identically to.
+ /// FindNodeOrInsertPos (or lookup) for an ID that \p N profiles identically
+ /// to.
LLVM_ABI void InsertNode(Node *N, void *InsertPos);
+ LLVM_ABI void insert(Node *N, FoldingSetInsertToken Token);
};
// Convenience type to hide the implementation of the folding set.
@@ -477,6 +497,7 @@ class FoldingSetImpl : public FoldingSetBase, public Trait::ContextStorage {
/// Remove a node from the folding set, returning true if one
/// was removed or false if the node was not in the folding set.
bool RemoveNode(T *N) { return FoldingSetBase::RemoveNode(N); }
+ bool erase(T *N) { return RemoveNode(N); }
/// If there is an existing node exactly equal to the specified node,
/// return it. Otherwise, insert 'N' and return it instead.
@@ -484,6 +505,7 @@ class FoldingSetImpl : public FoldingSetBase, public Trait::ContextStorage {
return static_cast<T *>(
FoldingSetBase::GetOrInsertNode(N, getFoldingSetInfo()));
}
+ T *getOrInsert(T *N) { return GetOrInsertNode(N); }
/// Look up the node specified by ID. If it exists, return it. If not,
/// return the insertion token that will make insertion faster.
@@ -491,13 +513,20 @@ class FoldingSetImpl : public FoldingSetBase, public Trait::ContextStorage {
return static_cast<T *>(FoldingSetBase::FindNodeOrInsertPos(
ID, InsertPos, getFoldingSetInfo()));
}
+ T *lookup(const FoldingSetNodeID &ID, FoldingSetInsertToken &Token) {
+ return static_cast<T *>(
+ FoldingSetBase::lookup(ID, Token, getFoldingSetInfo()));
+ }
/// Insert the specified node into the folding set, knowing that
/// it is not already in the folding set. InsertPos must be obtained from
- /// FindNodeOrInsertPos.
+ /// FindNodeOrInsertPos (or lookup).
void InsertNode(T *N, void *InsertPos) {
FoldingSetBase::InsertNode(N, InsertPos);
}
+ void insert(T *N, FoldingSetInsertToken Token) {
+ FoldingSetBase::insert(N, Token);
+ }
/// Insert the specified node into the folding set, knowing that it is not
/// already in the folding set.
@@ -565,6 +594,9 @@ template <class T, class VectorT = SmallVector<T *, 8>> class FoldingSetVector {
T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
return Set.FindNodeOrInsertPos(ID, InsertPos);
}
+ T *lookup(const FoldingSetNodeID &ID, FoldingSetInsertToken &Token) {
+ return Set.lookup(ID, Token);
+ }
/// If there is an existing node exactly equal to the specified node,
/// return it. Otherwise, insert 'N' and return it instead.
@@ -574,14 +606,19 @@ template <class T, class VectorT = SmallVector<T *, 8>> class FoldingSetVector {
Vector.push_back(N);
return Result;
}
+ T *getOrInsert(T *N) { return GetOrInsertNode(N); }
/// Insert the specified node into the folding set, knowing that
/// it is not already in the folding set. InsertPos must be obtained from
- /// FindNodeOrInsertPos.
+ /// FindNodeOrInsertPos (or lookup).
void InsertNode(T *N, void *InsertPos) {
Set.InsertNode(N, InsertPos);
Vector.push_back(N);
}
+ void insert(T *N, FoldingSetInsertToken Token) {
+ Set.insert(N, Token);
+ Vector.push_back(N);
+ }
/// Insert the specified node into the folding set, knowing that
/// it is not already in the folding set.
diff --git a/llvm/lib/Support/FoldingSet.cpp b/llvm/lib/Support/FoldingSet.cpp
index 0083e77ad95e4..784b33ae81ef2 100644
--- a/llvm/lib/Support/FoldingSet.cpp
+++ b/llvm/lib/Support/FoldingSet.cpp
@@ -219,33 +219,47 @@ FoldingSetBase::nodeEquals(const FoldingSetInfo &Info,
return Info.NodeEquals(Self, N, ID, TempID);
}
-FoldingSetBase::Node *FoldingSetBase::FindNodeOrInsertPos(
- const FoldingSetNodeID &ID, void *&InsertPos, const FoldingSetInfo &Info) {
+FoldingSetBase::Node *FoldingSetBase::lookup(const FoldingSetNodeID &ID,
+ FoldingSetInsertToken &Token,
+ const FoldingSetInfo &Info) {
unsigned IDHash = ID.ComputeHash();
unsigned Mask = NumBuckets - 1;
for (unsigned I = IDHash & Mask; Buckets[I]; I = (I + 1) & Mask) {
Node *N = static_cast<Node *>(Buckets[I]);
if (N->getFoldingSetHash() == IDHash && nodeEquals(Info, this, N, ID)) {
- InsertPos = nullptr;
+ Token = FoldingSetInsertToken();
return N;
}
}
- InsertPos = encodeHash(IDHash);
+ Token = FoldingSetInsertToken(IDHash);
return nullptr;
}
-void FoldingSetBase::InsertNode(Node *N, void *InsertPos) {
+FoldingSetBase::Node *FoldingSetBase::FindNodeOrInsertPos(
+ const FoldingSetNodeID &ID, void *&InsertPos, const FoldingSetInfo &Info) {
+ FoldingSetInsertToken Token;
+ Node *N = lookup(ID, Token, Info);
+ InsertPos = Token ? encodeHash(Token.hash()) : nullptr;
+ return N;
+}
+
+void FoldingSetBase::insert(Node *N, FoldingSetInsertToken Token) {
assert(N && "Cannot insert a null node");
- assert(InsertPos && "Invalid InsertPos!");
+ assert(Token && "Invalid token!");
incrementEpoch();
if (LLVM_UNLIKELY((NumNodes + 1) * 4 > NumBuckets * 3))
grow(NumBuckets * 2);
- uint32_t Hash = decodeHash(InsertPos);
+ uint32_t Hash = Token.hash();
placeNode(N, Hash);
N->setFoldingSetHash(Hash);
}
+void FoldingSetBase::InsertNode(Node *N, void *InsertPos) {
+ assert(InsertPos && "Invalid InsertPos!");
+ insert(N, FoldingSetInsertToken(decodeHash(InsertPos)));
+}
+
bool FoldingSetBase::RemoveNode(Node *N) {
uint32_t Hash = N->getFoldingSetHash();
if (Hash == FoldingSetNodeIDRef::NotAHash)
@@ -280,9 +294,9 @@ FoldingSetBase::Node *
FoldingSetBase::GetOrInsertNode(Node *N, const FoldingSetInfo &Info) {
FoldingSetNodeID ID;
Info.GetNodeProfile(this, N, ID);
- void *IP;
- if (Node *E = FindNodeOrInsertPos(ID, IP, Info))
+ FoldingSetInsertToken Token;
+ if (Node *E = lookup(ID, Token, Info))
return E;
- InsertNode(N, IP);
+ insert(N, Token);
return N;
}
diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp
index ac62e2f5785ae..3aa2e5ce2b94a 100644
--- a/llvm/lib/TableGen/Record.cpp
+++ b/llvm/lib/TableGen/Record.cpp
@@ -254,8 +254,8 @@ const RecordRecTy *RecordRecTy::get(RecordKeeper &RK,
FoldingSetNodeID ID;
ProfileRecordRecTy(ID, Classes);
- void *IP = nullptr;
- if (RecordRecTy *Ty = ThePool.FindNodeOrInsertPos(ID, IP))
+ FoldingSetInsertToken Token;
+ if (RecordRecTy *Ty = ThePool.lookup(ID, Token))
return Ty;
#ifndef NDEBUG
@@ -271,7 +271,7 @@ const RecordRecTy *RecordRecTy::get(RecordKeeper &RK,
void *Mem = RKImpl.Allocator.Allocate(
totalSizeToAlloc<const Record *>(Classes.size()), alignof(RecordRecTy));
RecordRecTy *Ty = new (Mem) RecordRecTy(RK, Classes);
- ThePool.InsertNode(Ty, IP);
+ ThePool.insert(Ty, Token);
return Ty;
}
@@ -416,13 +416,12 @@ const ArgumentInit *ArgumentInit::get(const Init *Value, ArgAuxType Aux) {
RecordKeeper &RK = Value->getRecordKeeper();
detail::RecordKeeperImpl &RKImpl = RK.getImpl();
- void *IP = nullptr;
- if (const ArgumentInit *I =
- RKImpl.TheArgumentInitPool.FindNodeOrInsertPos(ID, IP))
+ FoldingSetInsertToken Token;
+ if (const ArgumentInit *I = RKImpl.TheArgumentInitPool.lookup(ID, Token))
return I;
ArgumentInit *I = new (RKImpl.Allocator) ArgumentInit(Value, Aux);
- RKImpl.TheArgumentInitPool.InsertNode(I, IP);
+ RKImpl.TheArgumentInitPool.insert(I, Token);
return I;
}
@@ -473,14 +472,14 @@ BitsInit *BitsInit::get(RecordKeeper &RK, ArrayRef<const Init *> Bits) {
ProfileBitsInit(ID, Bits);
detail::RecordKeeperImpl &RKImpl = RK.getImpl();
- void *IP = nullptr;
- if (BitsInit *I = RKImpl.TheBitsInitPool.FindNodeOrInsertPos(ID, IP))
+ FoldingSetInsertToken Token;
+ if (BitsInit *I = RKImpl.TheBitsInitPool.lookup(ID, Token))
return I;
void *Mem = RKImpl.Allocator.Allocate(
totalSizeToAlloc<const Init *>(Bits.size()), alignof(BitsInit));
BitsInit *I = new (Mem) BitsInit(RK, Bits);
- RKImpl.TheBitsInitPool.InsertNode(I, IP);
+ RKImpl.TheBitsInitPool.insert(I, Token);
return I;
}
@@ -716,8 +715,8 @@ const ListInit *ListInit::get(ArrayRef<const Init *> Elements,
ProfileListInit(ID, Elements, EltTy);
detail::RecordKeeperImpl &RK = EltTy->getRecordKeeper().getImpl();
- void *IP = nullptr;
- if (const ListInit *I = RK.TheListInitPool.FindNodeOrInsertPos(ID, IP))
+ FoldingSetInsertToken Token;
+ if (const ListInit *I = RK.TheListInitPool.lookup(ID, Token))
return I;
assert(Elements.empty() || !isa<TypedInit>(Elements[0]) ||
@@ -726,7 +725,7 @@ const ListInit *ListInit::get(ArrayRef<const Init *> Elements,
void *Mem = RK.Allocator.Allocate(
totalSizeToAlloc<const Init *>(Elements.size()), alignof(ListInit));
ListInit *I = new (Mem) ListInit(Elements, EltTy);
- RK.TheListInitPool.InsertNode(I, IP);
+ RK.TheListInitPool.insert(I, Token);
return I;
}
@@ -826,12 +825,12 @@ const UnOpInit *UnOpInit::get(UnaryOp Opc, const Init *LHS, const RecTy *Type) {
ProfileUnOpInit(ID, Opc, LHS, Type);
detail::RecordKeeperImpl &RK = Type->getRecordKeeper().getImpl();
- void *IP = nullptr;
- if (const UnOpInit *I = RK.TheUnOpInitPool.FindNodeOrInsertPos(ID, IP))
+ FoldingSetInsertToken Token;
+ if (const UnOpInit *I = RK.TheUnOpInitPool.lookup(ID, Token))
return I;
UnOpInit *I = new (RK.Allocator) UnOpInit(Opc, LHS, Type);
- RK.TheUnOpInitPool.InsertNode(I, IP);
+ RK.TheUnOpInitPool.insert(I, Token);
return I;
}
@@ -1098,12 +1097,12 @@ const BinOpInit *BinOpInit::get(BinaryOp Opc, const Init *LHS, const Init *RHS,
ProfileBinOpInit(ID, Opc, LHS, RHS, Type);
detail::RecordKeeperImpl &RK = LHS->getRecordKeeper().getImpl();
- void *IP = nullptr;
- if (const BinOpInit *I = RK.TheBinOpInitPool.FindNodeOrInsertPos(ID, IP))
+ FoldingSetInsertToken Token;
+ if (const BinOpInit *I = RK.TheBinOpInitPool.lookup(ID, Token))
return I;
BinOpInit *I = new (RK.Allocator) BinOpInit(Opc, LHS, RHS, Type);
- RK.TheBinOpInitPool.InsertNode(I, IP);
+ RK.TheBinOpInitPool.insert(I, Token);
return I;
}
@@ -1693,12 +1692,12 @@ const TernOpInit *TernOpInit::get(TernaryOp Opc, const Init *LHS,
ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type);
detail::RecordKeeperImpl &RK = LHS->getRecordKeeper().getImpl();
- void *IP = nullptr;
- if (TernOpInit *I = RK.TheTernOpInitPool.FindNodeOrInsertPos(ID, IP))
+ FoldingSetInsertToken Token;
+ if (TernOpInit *I = RK.TheTernOpInitPool.lookup(ID, Token))
return I;
TernOpInit *I = new (RK.Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type);
- RK.TheTernOpInitPool.InsertNode(I, IP);
+ RK.TheTernOpInitPool.insert(I, Token);
return I;
}
@@ -2122,12 +2121,12 @@ const FoldOpInit *FoldOpInit::get(const Init *Start, const Init *List,
ProfileFoldOpInit(ID, Start, List, A, B, Expr, Type);
detail::RecordKeeperImpl &RK = Start->getRecordKeeper().getImpl();
- void *IP = nullptr;
- if (const FoldOpInit *I = RK.TheFoldOpInitPool.FindNodeOrInsertPos(ID, IP))
+ FoldingSetInsertToken Token;
+ if (const FoldOpInit *I = RK.TheFoldOpInitPool.lookup(ID, Token))
return I;
FoldOpInit *I = new (RK.Allocator) FoldOpInit(Start, List, A, B, Expr, Type);
- RK.TheFoldOpInitPool.InsertNode(I, IP);
+ RK.TheFoldOpInitPool.insert(I, Token);
return I;
}
@@ -2189,12 +2188,12 @@ const IsAOpInit *IsAOpInit::get(const RecTy *CheckType, const Init *Expr) {
ProfileIsAOpInit(ID, CheckType, Expr);
detail::RecordKeeperImpl &RK = Expr->getRecordKeeper().getImpl();
- void *IP = nullptr;
- if (const IsAOpInit *I = RK.TheIsAOpInitPool.FindNodeOrInsertPos(ID, IP))
+ FoldingSetInsertToken Token;
+ if (const IsAOpInit *I = RK.TheIsAOpInitPool.lookup(ID, Token))
return I;
IsAOpInit *I = new (RK.Allocator) IsAOpInit(CheckType, Expr);
- RK.TheIsAOpInitPool.InsertNode(I, IP);
+ RK.TheIsAOpInitPool.insert(I, Token);
return I;
}
@@ -2253,13 +2252,12 @@ const ExistsOpInit *ExistsOpInit::get(const RecTy *CheckType,
ProfileExistsOpInit(ID, CheckType, Expr);
detail::RecordKeeperImpl &RK = Expr->getRecordKeeper().getImpl();
- void *IP = nullptr;
- if (const ExistsOpInit *I =
- RK.TheExistsOpInitPool.FindNodeOrInsertPos(ID, IP))
+ FoldingSetInsertToken Token;
+ if (const ExistsOpInit *I = RK.TheExistsOpInitPool.lookup(ID, Token))
return I;
ExistsOpInit *I = new (RK.Allocator) ExistsOpInit(CheckType, Expr);
- RK.TheExistsOpInitPool.InsertNode(I, IP);
+ RK.TheExistsOpInitPool.insert(I, Token);
return I;
}
@@ -2328,13 +2326,12 @@ const InstancesOpInit *InstancesOpInit::get(const RecTy *Type,
ProfileInstancesOpInit(ID, Type, Regex);
detail::RecordKeeperImpl &RK = Regex->getRecordKeeper().getImpl();
- void *IP = nullptr;
- if (const InstancesOpInit *I =
- RK.TheInstancesOpInitPool.FindNodeOrInsertPos(ID, IP))
+ FoldingSetInsertToken Token;
+ if (const InstancesOpInit *I = RK.TheInstancesOpInitPool.lookup(ID, Token))
return I;
InstancesOpInit *I = new (RK.Allocator) InstancesOpInit(Type, Regex);
- RK.TheInstancesOpInitPool.InsertNode(I, IP);
+ RK.TheInstancesOpInitPool.insert(I, Token);
return I;
}
@@ -2521,14 +2518,14 @@ const VarDefInit *VarDefInit::get(SMLoc Loc, const Record *Class,
ProfileVarDefInit(ID, Class, Args);
detail::RecordKeeperImpl &RK = Class->getRecords().getImpl();
- void *IP = nullptr;
- if (const VarDefInit *I = RK.TheVarDefInitPool.FindNodeOrInsertPos(ID, IP))
+ FoldingSetInsertToken Token;
+ if (const VarDefInit *I = RK.TheVarDefInitPool.lookup(ID, Token))
return I;
void *Mem = RK.Allocator.Allocate(
totalSizeToAlloc<const ArgumentInit *>(Args.size()), alignof(VarDefInit));
VarDefInit *I = new (Mem) VarDefInit(Loc, Class, Args);
- RK.TheVarDefInitPool.InsertNode(I, IP);
+ RK.TheVarDefInitPool.insert(I, Token);
return I;
}
@@ -2714,14 +2711,14 @@ const CondOpInit *CondOpInit::get(ArrayRef<const Init *> Conds,
ProfileCondOpInit(ID, Conds, Values, Ty);
detail::RecordKeeperImpl &RK = Ty->getRecordKeeper().getImpl();
- void *IP = nullptr;
- if (const CondOpInit *I = RK.TheCondOpInitPool.FindNodeOrInsertPos(ID, IP))
+ FoldingSetInsertToken Token;
+ if (const CondOpInit *I = RK.TheCondOpInitPool.lookup(ID, Token))
return I;
void *Mem = RK.Allocator.Allocate(
totalSizeToAlloc<const Init *>(2 * Conds.size()), alignof(CondOpInit));
CondOpInit *I = new (Mem) CondOpInit(Conds, Values, Ty);
- RK.TheCondOpInitPool.InsertNode(I, IP);
+ RK.TheCondOpInitPool.insert(I, Token);
return I;
}
@@ -2836,8 +2833,8 @@ const DagInit *DagInit::get(const Init *V, const StringInit *VN,
ProfileDagInit(ID, V, VN, Args, ArgNames);
detail::RecordKeeperImpl &RK = V->getRecordKeeper().getImpl();
- void *IP = nullptr;
- if (const DagInit *I = RK.TheDagInitPool.FindNodeOrInsertPos(ID, IP))
+ FoldingSetInsertToken Token;
+ if (const DagInit *I = RK.TheDagInitPool.lookup(ID, Token))
return I;
void *Mem =
@@ -2845,7 +2842,7 @@ const DagInit *DagInit::get(const Init *V, const StringInit *VN,
Args.size(), ArgNames.size()),
alignof(DagInit));
DagInit *I = new (Mem) DagInit(V, VN, Args, ArgNames);
- RK.TheDagInitPool.InsertNode(I, IP);
+ RK.TheDagInitPool.insert(I, Token);
return I;
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/219644
More information about the llvm-commits
mailing list