[llvm] [PBQP] Add begin and end to Vector (NFC) (PR #136454)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 19 15:03:11 PDT 2025


https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/136454

>From 1a3254f4b50cea837f30ad6a37d25ea05c9cd697 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 19 Apr 2025 12:08:09 -0700
Subject: [PATCH 1/3] [PBQP] Add begin and end to Vector (NFC)

This patch adds begin and end to Vector.

The new functions will allow us to use llvm::interleaved in operator<<
for example.
---
 llvm/include/llvm/CodeGen/PBQP/Math.h | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/PBQP/Math.h b/llvm/include/llvm/CodeGen/PBQP/Math.h
index 099ba788e9a2d..f0efe07c4fb0e 100644
--- a/llvm/include/llvm/CodeGen/PBQP/Math.h
+++ b/llvm/include/llvm/CodeGen/PBQP/Math.h
@@ -48,12 +48,18 @@ class Vector {
     V.Length = 0;
   }
 
+  // Iterator-based access.
+  const PBQPNum *begin() const { return Data.get(); }
+  const PBQPNum *end() const { return Data.get() + Length; }
+  PBQPNum *begin() { return Data.get(); }
+  PBQPNum *end() { return Data.get() + Length; }
+
   /// Comparison operator.
   bool operator==(const Vector &V) const {
     assert(Length != 0 && Data && "Invalid vector");
     if (Length != V.Length)
       return false;
-    return std::equal(Data.get(), Data.get() + Length, V.Data.get());
+    return std::equal(begin(), end(), V.begin());
   }
 
   /// Return the length of the vector
@@ -80,15 +86,14 @@ class Vector {
   Vector& operator+=(const Vector &V) {
     assert(Length != 0 && Data && "Invalid vector");
     assert(Length == V.Length && "Vector length mismatch.");
-    std::transform(Data.get(), Data.get() + Length, V.Data.get(), Data.get(),
-                   std::plus<PBQPNum>());
+    std::transform(begin(), end(), V.begin(), begin(), std::plus<PBQPNum>());
     return *this;
   }
 
   /// Returns the index of the minimum value in this vector
   unsigned minIndex() const {
     assert(Length != 0 && Data && "Invalid vector");
-    return std::min_element(Data.get(), Data.get() + Length) - Data.get();
+    return llvm::min_element(*this) - begin();
   }
 
 private:
@@ -98,8 +103,8 @@ class Vector {
 
 /// Return a hash_value for the given vector.
 inline hash_code hash_value(const Vector &V) {
-  unsigned *VBegin = reinterpret_cast<unsigned*>(V.Data.get());
-  unsigned *VEnd = reinterpret_cast<unsigned*>(V.Data.get() + V.Length);
+  const unsigned *VBegin = reinterpret_cast<const unsigned *>(V.begin());
+  const unsigned *VEnd = reinterpret_cast<const unsigned *>(V.end());
   return hash_combine(V.Length, hash_combine_range(VBegin, VEnd));
 }
 

>From 5ba1a1c756321f09db982390191bc67e40ea07e7 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 19 Apr 2025 13:31:19 -0700
Subject: [PATCH 2/3] Address a comment.

---
 llvm/include/llvm/CodeGen/PBQP/Math.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/PBQP/Math.h b/llvm/include/llvm/CodeGen/PBQP/Math.h
index f0efe07c4fb0e..fc8b22f43ebfb 100644
--- a/llvm/include/llvm/CodeGen/PBQP/Math.h
+++ b/llvm/include/llvm/CodeGen/PBQP/Math.h
@@ -57,9 +57,7 @@ class Vector {
   /// Comparison operator.
   bool operator==(const Vector &V) const {
     assert(Length != 0 && Data && "Invalid vector");
-    if (Length != V.Length)
-      return false;
-    return std::equal(begin(), end(), V.begin());
+    return llvm::equal(*this, V);
   }
 
   /// Return the length of the vector

>From 6ee62846906c3cc36d73e417fd54663f011778ac Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 19 Apr 2025 14:52:45 -0700
Subject: [PATCH 3/3] Address a comment.

---
 llvm/include/llvm/CodeGen/PBQP/Math.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/PBQP/Math.h b/llvm/include/llvm/CodeGen/PBQP/Math.h
index fc8b22f43ebfb..6a8942e08b101 100644
--- a/llvm/include/llvm/CodeGen/PBQP/Math.h
+++ b/llvm/include/llvm/CodeGen/PBQP/Math.h
@@ -33,13 +33,13 @@ class Vector {
   /// Construct a PBQP vector with initializer.
   Vector(unsigned Length, PBQPNum InitVal)
     : Length(Length), Data(std::make_unique<PBQPNum []>(Length)) {
-    std::fill(Data.get(), Data.get() + Length, InitVal);
+    std::fill(begin(), end(), InitVal);
   }
 
   /// Copy construct a PBQP vector.
   Vector(const Vector &V)
     : Length(V.Length), Data(std::make_unique<PBQPNum []>(Length)) {
-    std::copy(V.Data.get(), V.Data.get() + Length, Data.get());
+    llvm::copy(V, begin());
   }
 
   /// Move construct a PBQP vector.



More information about the llvm-commits mailing list