[llvm] Inline operator== and operator!= (PR #67958)

Giulio Eulisse via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 3 02:11:35 PDT 2023


https://github.com/ktf updated https://github.com/llvm/llvm-project/pull/67958

>From 77e9d0c050ecc998ab4c2485e6a284f835689c6e Mon Sep 17 00:00:00 2001
From: Giulio Eulisse <10544+ktf at users.noreply.github.com>
Date: Mon, 2 Oct 2023 10:27:50 +0200
Subject: [PATCH 1/5] Inline operator== and operator!=

Avoid triggering -Wnon-template-friend.
---
 llvm/include/llvm/ADT/PagedVector.h | 43 +++++++++++++----------------
 1 file changed, 19 insertions(+), 24 deletions(-)

diff --git a/llvm/include/llvm/ADT/PagedVector.h b/llvm/include/llvm/ADT/PagedVector.h
index 667bece6d718385..c96e0b20e2d33d7 100644
--- a/llvm/include/llvm/ADT/PagedVector.h
+++ b/llvm/include/llvm/ADT/PagedVector.h
@@ -209,36 +209,31 @@ template <typename T, size_t PageSize = 1024 / sizeof(T)> class PagedVector {
       return PagePtr[ElementIdx % PageSize];
     }
 
+    /// Equality operator.
     friend bool operator==(MaterializedIterator const &LHS,
-                           MaterializedIterator const &RHS);
+                           MaterializedIterator const &RHS) {
+      assert(LHS.PV == RHS.PV);
+      // Make sure we are comparing either end iterators or iterators pointing
+      // to materialized elements.
+      // It should not be possible to build two iterators pointing to non
+      // materialized elements.
+      assert(LHS.ElementIdx == LHS.PV->Size ||
+             (LHS.ElementIdx < LHS.PV->Size &&
+              LHS.PV->PageToDataPtrs[LHS.ElementIdx / PageSize]));
+      assert(RHS.ElementIdx == RHS.PV->Size ||
+             (RHS.ElementIdx < RHS.PV->Size &&
+              RHS.PV->PageToDataPtrs[RHS.ElementIdx / PageSize]));
+      return LHS.ElementIdx == RHS.ElementIdx;
+    }
+
     friend bool operator!=(MaterializedIterator const &LHS,
-                           MaterializedIterator const &RHS);
+                           MaterializedIterator const &RHS) {
+      return !(LHS == RHS);
+    }
 
     [[nodiscard]] size_t getIndex() const { return ElementIdx; }
   };
 
-  /// Equality operator.
-  friend bool operator==(MaterializedIterator const &LHS,
-                         MaterializedIterator const &RHS) {
-    assert(LHS.PV == RHS.PV);
-    // Make sure we are comparing either end iterators or iterators pointing
-    // to materialized elements.
-    // It should not be possible to build two iterators pointing to non
-    // materialized elements.
-    assert(LHS.ElementIdx == LHS.PV->Size ||
-           (LHS.ElementIdx < LHS.PV->Size &&
-            LHS.PV->PageToDataPtrs[LHS.ElementIdx / PageSize]));
-    assert(RHS.ElementIdx == RHS.PV->Size ||
-           (RHS.ElementIdx < RHS.PV->Size &&
-            RHS.PV->PageToDataPtrs[RHS.ElementIdx / PageSize]));
-    return LHS.ElementIdx == RHS.ElementIdx;
-  }
-
-  friend bool operator!=(MaterializedIterator const &LHS,
-                         MaterializedIterator const &RHS) {
-    return !(LHS == RHS);
-  }
-
   /// Iterators over the materialized elements of the vector.
   ///
   /// This includes all the elements belonging to allocated pages,

>From 8494fea5ad62645bcdc93e4e886795d9de65379c Mon Sep 17 00:00:00 2001
From: Giulio Eulisse <10544+ktf at users.noreply.github.com>
Date: Tue, 3 Oct 2023 09:33:10 +0200
Subject: [PATCH 2/5] Update llvm/include/llvm/ADT/PagedVector.h

Co-authored-by: Richard Smith <richard at metafoo.co.uk>
---
 llvm/include/llvm/ADT/PagedVector.h | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/llvm/include/llvm/ADT/PagedVector.h b/llvm/include/llvm/ADT/PagedVector.h
index c96e0b20e2d33d7..39d569b8df8b49f 100644
--- a/llvm/include/llvm/ADT/PagedVector.h
+++ b/llvm/include/llvm/ADT/PagedVector.h
@@ -212,18 +212,21 @@ template <typename T, size_t PageSize = 1024 / sizeof(T)> class PagedVector {
     /// Equality operator.
     friend bool operator==(MaterializedIterator const &LHS,
                            MaterializedIterator const &RHS) {
-      assert(LHS.PV == RHS.PV);
-      // Make sure we are comparing either end iterators or iterators pointing
-      // to materialized elements.
-      // It should not be possible to build two iterators pointing to non
-      // materialized elements.
-      assert(LHS.ElementIdx == LHS.PV->Size ||
-             (LHS.ElementIdx < LHS.PV->Size &&
-              LHS.PV->PageToDataPtrs[LHS.ElementIdx / PageSize]));
-      assert(RHS.ElementIdx == RHS.PV->Size ||
-             (RHS.ElementIdx < RHS.PV->Size &&
-              RHS.PV->PageToDataPtrs[RHS.ElementIdx / PageSize]));
-      return LHS.ElementIdx == RHS.ElementIdx;
+      return LHS.equals(RHS);
+    }
+
+  private:
+    void verify() {
+      assert(ElementIdx == PV->Size ||
+             (ElementIdx < PV->Size &&
+              PV->PageToDataPtrs[ElementIdx / PageSize]));
+    }
+
+    bool equals(const MaterializedIterator &Other) const {
+      assert(PV == Other.PV);
+      verify();
+      Other.verify();
+      return ElementIdx == Other.ElementIdx;
     }
 
     friend bool operator!=(MaterializedIterator const &LHS,

>From d853c21fce5a1a67d2ecf2f8734c6b2635a32c57 Mon Sep 17 00:00:00 2001
From: Giulio Eulisse <10544+ktf at users.noreply.github.com>
Date: Tue, 3 Oct 2023 09:35:30 +0200
Subject: [PATCH 3/5] Fix friendship

---
 llvm/include/llvm/ADT/PagedVector.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/ADT/PagedVector.h b/llvm/include/llvm/ADT/PagedVector.h
index 39d569b8df8b49f..0d561c0303d55fa 100644
--- a/llvm/include/llvm/ADT/PagedVector.h
+++ b/llvm/include/llvm/ADT/PagedVector.h
@@ -210,8 +210,8 @@ template <typename T, size_t PageSize = 1024 / sizeof(T)> class PagedVector {
     }
 
     /// Equality operator.
-    friend bool operator==(MaterializedIterator const &LHS,
-                           MaterializedIterator const &RHS) {
+    friend bool operator==(const MaterializedIterator &LHS,
+                           const MaterializedIterator &RHS) {
       return LHS.equals(RHS);
     }
 
@@ -229,8 +229,8 @@ template <typename T, size_t PageSize = 1024 / sizeof(T)> class PagedVector {
       return ElementIdx == Other.ElementIdx;
     }
 
-    friend bool operator!=(MaterializedIterator const &LHS,
-                           MaterializedIterator const &RHS) {
+    friend bool operator!=(const MaterializedIterator &LHS,
+                           const MaterializedIterator &RHS) {
       return !(LHS == RHS);
     }
 

>From 4849265603ebe050ec210964658ad33d20ccd95a Mon Sep 17 00:00:00 2001
From: Giulio Eulisse <10544+ktf at users.noreply.github.com>
Date: Tue, 3 Oct 2023 09:41:47 +0200
Subject: [PATCH 4/5] Adapt const, fix wrongly private member

---
 llvm/include/llvm/ADT/PagedVector.h | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/llvm/include/llvm/ADT/PagedVector.h b/llvm/include/llvm/ADT/PagedVector.h
index 0d561c0303d55fa..4238e4b3b6786c9 100644
--- a/llvm/include/llvm/ADT/PagedVector.h
+++ b/llvm/include/llvm/ADT/PagedVector.h
@@ -215,8 +215,15 @@ template <typename T, size_t PageSize = 1024 / sizeof(T)> class PagedVector {
       return LHS.equals(RHS);
     }
 
+    [[nodiscard]] size_t getIndex() const { return ElementIdx; }
+
+    friend bool operator!=(const MaterializedIterator &LHS,
+                           const MaterializedIterator &RHS) {
+      return !(LHS == RHS);
+    }
+
   private:
-    void verify() {
+    void verify() const {
       assert(ElementIdx == PV->Size ||
              (ElementIdx < PV->Size &&
               PV->PageToDataPtrs[ElementIdx / PageSize]));
@@ -228,13 +235,6 @@ template <typename T, size_t PageSize = 1024 / sizeof(T)> class PagedVector {
       Other.verify();
       return ElementIdx == Other.ElementIdx;
     }
-
-    friend bool operator!=(const MaterializedIterator &LHS,
-                           const MaterializedIterator &RHS) {
-      return !(LHS == RHS);
-    }
-
-    [[nodiscard]] size_t getIndex() const { return ElementIdx; }
   };
 
   /// Iterators over the materialized elements of the vector.

>From 33a23b0832740b6c6a68625105af75d9410a9279 Mon Sep 17 00:00:00 2001
From: Giulio Eulisse <10544+ktf at users.noreply.github.com>
Date: Tue, 3 Oct 2023 10:13:04 +0200
Subject: [PATCH 5/5] Keep formatting happy.

---
 llvm/include/llvm/ADT/PagedVector.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/ADT/PagedVector.h b/llvm/include/llvm/ADT/PagedVector.h
index 4238e4b3b6786c9..3fcca6d82cb33a0 100644
--- a/llvm/include/llvm/ADT/PagedVector.h
+++ b/llvm/include/llvm/ADT/PagedVector.h
@@ -224,9 +224,9 @@ template <typename T, size_t PageSize = 1024 / sizeof(T)> class PagedVector {
 
   private:
     void verify() const {
-      assert(ElementIdx == PV->Size ||
-             (ElementIdx < PV->Size &&
-              PV->PageToDataPtrs[ElementIdx / PageSize]));
+      assert(
+          ElementIdx == PV->Size ||
+          (ElementIdx < PV->Size && PV->PageToDataPtrs[ElementIdx / PageSize]));
     }
 
     bool equals(const MaterializedIterator &Other) const {



More information about the llvm-commits mailing list