[llvm] [ADT] Move FoldingSetBase definition lower in the file (PR #172503)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 16 07:43:08 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-adt
Author: Thibault Monnier (Thibault-Monnier)
<details>
<summary>Changes</summary>
This is required for #<!-- -->172371, where `FoldingSetBase::FindNodeOrInsertPos` needs full definition of `FoldingSetNodeID`.
---
Full diff: https://github.com/llvm/llvm-project/pull/172503.diff
1 Files Affected:
- (modified) llvm/include/llvm/ADT/FoldingSet.h (+121-121)
``````````diff
diff --git a/llvm/include/llvm/ADT/FoldingSet.h b/llvm/include/llvm/ADT/FoldingSet.h
index 675b5c6a35bbc..56e6e34dba591 100644
--- a/llvm/include/llvm/ADT/FoldingSet.h
+++ b/llvm/include/llvm/ADT/FoldingSet.h
@@ -108,127 +108,6 @@ namespace llvm {
class FoldingSetNodeID;
class StringRef;
-//===----------------------------------------------------------------------===//
-/// FoldingSetBase - Implements the folding set functionality. The main
-/// structure is an array of buckets. Each bucket is indexed by the hash of
-/// the nodes it contains. The bucket itself points to the nodes contained
-/// in the bucket via a singly linked list. The last node in the list points
-/// back to the bucket to facilitate node removal.
-///
-class FoldingSetBase {
-protected:
- /// Buckets - Array of bucket chains.
- void **Buckets;
-
- /// NumBuckets - Length of the Buckets array. Always a power of 2.
- unsigned NumBuckets;
-
- /// NumNodes - Number of nodes in the folding set. Growth occurs when NumNodes
- /// is greater than twice the number of buckets.
- unsigned NumNodes;
-
- LLVM_ABI explicit FoldingSetBase(unsigned Log2InitSize = 6);
- LLVM_ABI FoldingSetBase(FoldingSetBase &&Arg);
- LLVM_ABI FoldingSetBase &operator=(FoldingSetBase &&RHS);
- LLVM_ABI ~FoldingSetBase();
-
-public:
- //===--------------------------------------------------------------------===//
- /// Node - This class is used to maintain the singly linked bucket list in
- /// a folding set.
- class Node {
- private:
- // NextInFoldingSetBucket - next link in the bucket list.
- void *NextInFoldingSetBucket = nullptr;
-
- public:
- Node() = default;
-
- // Accessors
- void *getNextInBucket() const { return NextInFoldingSetBucket; }
- void SetNextInBucket(void *N) { NextInFoldingSetBucket = N; }
- };
-
- /// clear - Remove all nodes from the folding set.
- LLVM_ABI void clear();
-
- /// size - Returns the number of nodes in the folding set.
- unsigned size() const { return NumNodes; }
-
- /// empty - Returns true if there are no nodes in the folding set.
- bool empty() const { return NumNodes == 0; }
-
- /// capacity - Returns the number of nodes permitted in the folding set
- /// before a rebucket operation is performed.
- unsigned capacity() {
- // We allow a load factor of up to 2.0,
- // so that means our capacity is NumBuckets * 2
- return NumBuckets * 2;
- }
-
-protected:
- /// Functions provided by the derived class to compute folding properties.
- /// This is effectively a vtable for FoldingSetBase, except that we don't
- /// actually store a pointer to it in the object.
- struct FoldingSetInfo {
- /// GetNodeProfile - Instantiations of the FoldingSet template implement
- /// this function to gather data bits for the given node.
- void (*GetNodeProfile)(const FoldingSetBase *Self, Node *N,
- FoldingSetNodeID &ID);
-
- /// NodeEquals - Instantiations of the FoldingSet template implement
- /// this function to compare the given node with the given ID.
- bool (*NodeEquals)(const FoldingSetBase *Self, Node *N,
- const FoldingSetNodeID &ID, unsigned IDHash,
- FoldingSetNodeID &TempID);
-
- /// ComputeNodeHash - Instantiations of the FoldingSet template implement
- /// this function to compute a hash value for the given node.
- unsigned (*ComputeNodeHash)(const FoldingSetBase *Self, Node *N,
- FoldingSetNodeID &TempID);
- };
-
-private:
- /// GrowHashTable - Double the size of the hash table and rehash everything.
- void GrowHashTable(const FoldingSetInfo &Info);
-
- /// GrowBucketCount - resize the hash table and rehash everything.
- /// NewBucketCount must be a power of two, and must be greater than the old
- /// bucket count.
- void GrowBucketCount(unsigned NewBucketCount, const FoldingSetInfo &Info);
-
-protected:
- // The below methods are protected to encourage subclasses to provide a more
- // type-safe API.
-
- /// reserve - Increase the number of buckets such that adding the
- /// EltCount-th node won't cause a rebucket operation. reserve is permitted
- /// to allocate more space than requested by EltCount.
- LLVM_ABI void reserve(unsigned EltCount, const FoldingSetInfo &Info);
-
- /// RemoveNode - Remove a node from the folding set, returning true if one
- /// was removed or false if the node was not in the folding set.
- LLVM_ABI bool RemoveNode(Node *N);
-
- /// GetOrInsertNode - If there is an existing simple Node exactly
- /// equal to the specified node, return it. Otherwise, insert 'N' and return
- /// it instead.
- LLVM_ABI Node *GetOrInsertNode(Node *N, const FoldingSetInfo &Info);
-
- /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
- /// return it. If not, return the insertion token that will make insertion
- /// faster.
- LLVM_ABI Node *FindNodeOrInsertPos(const FoldingSetNodeID &ID,
- void *&InsertPos,
- const FoldingSetInfo &Info);
-
- /// InsertNode - Insert the specified node into the folding set, knowing that
- /// it is not already in the folding set. InsertPos must be obtained from
- /// FindNodeOrInsertPos.
- LLVM_ABI void InsertNode(Node *N, void *InsertPos,
- const FoldingSetInfo &Info);
-};
-
//===----------------------------------------------------------------------===//
/// DefaultFoldingSetTrait - This class provides default implementations
@@ -404,6 +283,127 @@ class FoldingSetNodeID {
LLVM_ABI FoldingSetNodeIDRef Intern(BumpPtrAllocator &Allocator) const;
};
+//===----------------------------------------------------------------------===//
+/// FoldingSetBase - Implements the folding set functionality. The main
+/// structure is an array of buckets. Each bucket is indexed by the hash of
+/// the nodes it contains. The bucket itself points to the nodes contained
+/// in the bucket via a singly linked list. The last node in the list points
+/// back to the bucket to facilitate node removal.
+///
+class FoldingSetBase {
+protected:
+ /// Buckets - Array of bucket chains.
+ void **Buckets;
+
+ /// NumBuckets - Length of the Buckets array. Always a power of 2.
+ unsigned NumBuckets;
+
+ /// NumNodes - Number of nodes in the folding set. Growth occurs when NumNodes
+ /// is greater than twice the number of buckets.
+ unsigned NumNodes;
+
+ LLVM_ABI explicit FoldingSetBase(unsigned Log2InitSize = 6);
+ LLVM_ABI FoldingSetBase(FoldingSetBase &&Arg);
+ LLVM_ABI FoldingSetBase &operator=(FoldingSetBase &&RHS);
+ LLVM_ABI ~FoldingSetBase();
+
+public:
+ //===--------------------------------------------------------------------===//
+ /// Node - This class is used to maintain the singly linked bucket list in
+ /// a folding set.
+ class Node {
+ private:
+ // NextInFoldingSetBucket - next link in the bucket list.
+ void *NextInFoldingSetBucket = nullptr;
+
+ public:
+ Node() = default;
+
+ // Accessors
+ void *getNextInBucket() const { return NextInFoldingSetBucket; }
+ void SetNextInBucket(void *N) { NextInFoldingSetBucket = N; }
+ };
+
+ /// clear - Remove all nodes from the folding set.
+ LLVM_ABI void clear();
+
+ /// size - Returns the number of nodes in the folding set.
+ unsigned size() const { return NumNodes; }
+
+ /// empty - Returns true if there are no nodes in the folding set.
+ bool empty() const { return NumNodes == 0; }
+
+ /// capacity - Returns the number of nodes permitted in the folding set
+ /// before a rebucket operation is performed.
+ unsigned capacity() {
+ // We allow a load factor of up to 2.0,
+ // so that means our capacity is NumBuckets * 2
+ return NumBuckets * 2;
+ }
+
+protected:
+ /// Functions provided by the derived class to compute folding properties.
+ /// This is effectively a vtable for FoldingSetBase, except that we don't
+ /// actually store a pointer to it in the object.
+ struct FoldingSetInfo {
+ /// GetNodeProfile - Instantiations of the FoldingSet template implement
+ /// this function to gather data bits for the given node.
+ void (*GetNodeProfile)(const FoldingSetBase *Self, Node *N,
+ FoldingSetNodeID &ID);
+
+ /// NodeEquals - Instantiations of the FoldingSet template implement
+ /// this function to compare the given node with the given ID.
+ bool (*NodeEquals)(const FoldingSetBase *Self, Node *N,
+ const FoldingSetNodeID &ID, unsigned IDHash,
+ FoldingSetNodeID &TempID);
+
+ /// ComputeNodeHash - Instantiations of the FoldingSet template implement
+ /// this function to compute a hash value for the given node.
+ unsigned (*ComputeNodeHash)(const FoldingSetBase *Self, Node *N,
+ FoldingSetNodeID &TempID);
+ };
+
+private:
+ /// GrowHashTable - Double the size of the hash table and rehash everything.
+ void GrowHashTable(const FoldingSetInfo &Info);
+
+ /// GrowBucketCount - resize the hash table and rehash everything.
+ /// NewBucketCount must be a power of two, and must be greater than the old
+ /// bucket count.
+ void GrowBucketCount(unsigned NewBucketCount, const FoldingSetInfo &Info);
+
+protected:
+ // The below methods are protected to encourage subclasses to provide a more
+ // type-safe API.
+
+ /// reserve - Increase the number of buckets such that adding the
+ /// EltCount-th node won't cause a rebucket operation. reserve is permitted
+ /// to allocate more space than requested by EltCount.
+ LLVM_ABI void reserve(unsigned EltCount, const FoldingSetInfo &Info);
+
+ /// RemoveNode - Remove a node from the folding set, returning true if one
+ /// was removed or false if the node was not in the folding set.
+ LLVM_ABI bool RemoveNode(Node *N);
+
+ /// GetOrInsertNode - If there is an existing simple Node exactly
+ /// equal to the specified node, return it. Otherwise, insert 'N' and return
+ /// it instead.
+ LLVM_ABI Node *GetOrInsertNode(Node *N, const FoldingSetInfo &Info);
+
+ /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
+ /// return it. If not, return the insertion token that will make insertion
+ /// faster.
+ LLVM_ABI Node *FindNodeOrInsertPos(const FoldingSetNodeID &ID,
+ void *&InsertPos,
+ const FoldingSetInfo &Info);
+
+ /// InsertNode - Insert the specified node into the folding set, knowing that
+ /// it is not already in the folding set. InsertPos must be obtained from
+ /// FindNodeOrInsertPos.
+ LLVM_ABI void InsertNode(Node *N, void *InsertPos,
+ const FoldingSetInfo &Info);
+};
+
// Convenience type to hide the implementation of the folding set.
using FoldingSetNode = FoldingSetBase::Node;
template<class T> class FoldingSetIterator;
``````````
</details>
https://github.com/llvm/llvm-project/pull/172503
More information about the llvm-commits
mailing list