Skip to content

Conversation

@kazutakahirata
Copy link
Contributor

This patch uses llvm::copy in combination with buckets() and
small_buckets().

This patch uses llvm::copy in combination with buckets() and
small_buckets().
@llvmbot
Copy link
Member

llvmbot commented Aug 16, 2025

@llvm/pr-subscribers-llvm-support

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

This patch uses llvm::copy in combination with buckets() and
small_buckets().


Full diff: https://github.com/llvm/llvm-project/pull/153930.diff

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/SmallPtrSet.h (+4)
  • (modified) llvm/lib/Support/SmallPtrSet.cpp (+5-5)
diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index 2829cbf41b66c..0d7fe308a32fb 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -160,6 +160,10 @@ class SmallPtrSetImplBase : public DebugEpochBase {
     return make_range(CurArray, EndPointer());
   }
 
+  iterator_range<const void *const *> buckets() const {
+    return make_range(CurArray, EndPointer());
+  }
+
   /// insert_imp - This returns true if the pointer was new to the set, false if
   /// it was already in the set.  This is hidden from the client so that the
   /// derived class can check that the right type of pointer is passed in.
diff --git a/llvm/lib/Support/SmallPtrSet.cpp b/llvm/lib/Support/SmallPtrSet.cpp
index 46cf53f1ecf83..39fe1715d19bf 100644
--- a/llvm/lib/Support/SmallPtrSet.cpp
+++ b/llvm/lib/Support/SmallPtrSet.cpp
@@ -13,6 +13,7 @@
 
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/DenseMapInfo.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/MemAlloc.h"
 #include <algorithm>
@@ -190,7 +191,7 @@ void SmallPtrSetImplBase::copyHelper(const SmallPtrSetImplBase &RHS) {
   CurArraySize = RHS.CurArraySize;
 
   // Copy over the contents from the other set
-  std::copy(RHS.CurArray, RHS.EndPointer(), CurArray);
+  llvm::copy(RHS.buckets(), CurArray);
 
   NumEntries = RHS.NumEntries;
   NumTombstones = RHS.NumTombstones;
@@ -214,7 +215,7 @@ void SmallPtrSetImplBase::moveHelper(const void **SmallStorage,
   if (RHS.isSmall()) {
     // Copy a small RHS rather than moving.
     CurArray = SmallStorage;
-    std::copy(RHS.CurArray, RHS.CurArray + RHS.NumEntries, CurArray);
+    llvm::copy(RHS.small_buckets(), CurArray);
   } else {
     CurArray = RHS.CurArray;
     RHS.CurArray = RHSSmallStorage;
@@ -252,7 +253,7 @@ void SmallPtrSetImplBase::swap(const void **SmallStorage,
   // If only RHS is small, copy the small elements into LHS and move the pointer
   // from LHS to RHS.
   if (!this->isSmall() && RHS.isSmall()) {
-    std::copy(RHS.CurArray, RHS.CurArray + RHS.NumEntries, SmallStorage);
+    llvm::copy(RHS.small_buckets(), SmallStorage);
     std::swap(RHS.CurArraySize, this->CurArraySize);
     std::swap(this->NumEntries, RHS.NumEntries);
     std::swap(this->NumTombstones, RHS.NumTombstones);
@@ -266,8 +267,7 @@ void SmallPtrSetImplBase::swap(const void **SmallStorage,
   // If only LHS is small, copy the small elements into RHS and move the pointer
   // from RHS to LHS.
   if (this->isSmall() && !RHS.isSmall()) {
-    std::copy(this->CurArray, this->CurArray + this->NumEntries,
-              RHSSmallStorage);
+    llvm::copy(this->small_buckets(), RHSSmallStorage);
     std::swap(RHS.CurArraySize, this->CurArraySize);
     std::swap(RHS.NumEntries, this->NumEntries);
     std::swap(RHS.NumTombstones, this->NumTombstones);

@kazutakahirata kazutakahirata merged commit 0ede7ac into llvm:main Aug 16, 2025
12 of 13 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20250815_SmallPtrSet_iterator_range branch August 16, 2025 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants