Skip to content

Conversation

@kazutakahirata
Copy link
Contributor

This patch consolidates the two implementations of Recycler::clear
with "if constexpr" for simplicity.

This patch consolidates the two implementations of Recycler::clear
with "if constexpr" for simplicity.
@llvmbot
Copy link
Member

llvmbot commented Oct 25, 2025

@llvm/pr-subscribers-llvm-support

Author: Kazu Hirata (kazutakahirata)

Changes

This patch consolidates the two implementations of Recycler::clear
with "if constexpr" for simplicity.


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

1 Files Affected:

  • (modified) llvm/include/llvm/Support/Recycler.h (+11-10)
diff --git a/llvm/include/llvm/Support/Recycler.h b/llvm/include/llvm/Support/Recycler.h
index b51c58678e653..6502a70dbf89c 100644
--- a/llvm/include/llvm/Support/Recycler.h
+++ b/llvm/include/llvm/Support/Recycler.h
@@ -19,6 +19,7 @@
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/ErrorHandling.h"
 #include <cassert>
+#include <type_traits>
 
 namespace llvm {
 
@@ -72,19 +73,19 @@ class Recycler {
   /// deleted; calling clear is one way to ensure this.
   template<class AllocatorType>
   void clear(AllocatorType &Allocator) {
-    while (FreeList) {
-      T *t = reinterpret_cast<T *>(pop_val());
-      Allocator.Deallocate(t, Size, Align);
+    if constexpr (std::is_same_v<std::decay_t<AllocatorType>,
+                                 BumpPtrAllocator>) {
+      // For BumpPtrAllocator, Deallocate is a no-op, so just drop the free
+      // list.
+      FreeList = nullptr;
+    } else {
+      while (FreeList) {
+        T *t = reinterpret_cast<T *>(pop_val());
+        Allocator.Deallocate(t, Size, Align);
+      }
     }
   }
 
-  /// Special case for BumpPtrAllocator which has an empty Deallocate()
-  /// function.
-  ///
-  /// There is no need to traverse the free list, pulling all the objects into
-  /// cache.
-  void clear(BumpPtrAllocator &) { FreeList = nullptr; }
-
   template<class SubClass, class AllocatorType>
   SubClass *Allocate(AllocatorType &Allocator) {
     static_assert(alignof(SubClass) <= Align,

@kazutakahirata kazutakahirata merged commit 7379100 into llvm:main Oct 25, 2025
12 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20251024_Support_Recycler branch October 25, 2025 13:25
dvbuka pushed a commit to dvbuka/llvm-project that referenced this pull request Oct 27, 2025
llvm#165081)

This patch consolidates the two implementations of Recycler::clear
with "if constexpr" for simplicity.
Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Oct 29, 2025
llvm#165081)

This patch consolidates the two implementations of Recycler::clear
with "if constexpr" for simplicity.
aokblast pushed a commit to aokblast/llvm-project that referenced this pull request Oct 30, 2025
llvm#165081)

This patch consolidates the two implementations of Recycler::clear
with "if constexpr" for simplicity.
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