[llvm] [ADT] "Inline" TestAndEraseFromMap into PriorityWorklist.h (NFC) (PR #156596)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 2 23:53:15 PDT 2025


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/156596

TestAndEraseFromMap is used only from PriorityWorklist::erase_if.
This patch "inlines" the struct into its sole user in the form of a
lambda function, eliminating a lot of boilerplate code.


>From 41ddc7629f1de4a97e7f91960f246ba0da3bb0b0 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 1 Sep 2025 23:49:44 -0700
Subject: [PATCH] [ADT] "Inline" TestAndEraseFromMap into PriorityWorklist.h
 (NFC)

TestAndEraseFromMap is used only from PriorityWorklist::erase_if.
This patch "inlines" the struct into its sole user in the form of a
lambda function, eliminating a lot of boilerplate code.
---
 llvm/include/llvm/ADT/PriorityWorklist.h | 41 +++++++-----------------
 1 file changed, 11 insertions(+), 30 deletions(-)

diff --git a/llvm/include/llvm/ADT/PriorityWorklist.h b/llvm/include/llvm/ADT/PriorityWorklist.h
index 2b6510f42d569..144993a761ac3 100644
--- a/llvm/include/llvm/ADT/PriorityWorklist.h
+++ b/llvm/include/llvm/ADT/PriorityWorklist.h
@@ -191,8 +191,17 @@ class PriorityWorklist {
   /// \returns true if any element is removed.
   template <typename UnaryPredicate>
   bool erase_if(UnaryPredicate P) {
-    typename VectorT::iterator E =
-        remove_if(V, TestAndEraseFromMap<UnaryPredicate>(P, M));
+    typename VectorT::iterator E = remove_if(V, [&](const T &Arg) {
+      if (Arg == T())
+        // Skip null values in the PriorityWorklist.
+        return false;
+
+      if (P(Arg)) {
+        M.erase(Arg);
+        return true;
+      }
+      return false;
+    });
     if (E == V.end())
       return false;
     for (auto I = V.begin(); I != E; ++I)
@@ -214,34 +223,6 @@ class PriorityWorklist {
   }
 
 private:
-  /// A wrapper predicate designed for use with std::remove_if.
-  ///
-  /// This predicate wraps a predicate suitable for use with std::remove_if to
-  /// call M.erase(x) on each element which is slated for removal. This just
-  /// allows the predicate to be move only which we can't do with lambdas
-  /// today.
-  template <typename UnaryPredicateT>
-  class TestAndEraseFromMap {
-    UnaryPredicateT P;
-    MapT &M;
-
-  public:
-    TestAndEraseFromMap(UnaryPredicateT P, MapT &M)
-        : P(std::move(P)), M(M) {}
-
-    bool operator()(const T &Arg) {
-      if (Arg == T())
-        // Skip null values in the PriorityWorklist.
-        return false;
-
-      if (P(Arg)) {
-        M.erase(Arg);
-        return true;
-      }
-      return false;
-    }
-  };
-
   /// The map from value to index in the vector.
   MapT M;
 



More information about the llvm-commits mailing list