[llvm] cleanup 001 PBQP Owning (PR #137546)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 27 13:25:08 PDT 2025


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

- **blah**
- **[CodeGen] Use OwningArrayRef in PBQP::Vector (NFC)**


>From 760f7c47d538d7471b30f90fb67a01d2395a99c0 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sun, 27 Apr 2025 12:59:01 -0700
Subject: [PATCH 1/2] blah

---
 llvm/include/llvm/CodeGen/PBQP/Math.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/CodeGen/PBQP/Math.h b/llvm/include/llvm/CodeGen/PBQP/Math.h
index 2b2b498e54fce..3ba3b9c2bf5e3 100644
--- a/llvm/include/llvm/CodeGen/PBQP/Math.h
+++ b/llvm/include/llvm/CodeGen/PBQP/Math.h
@@ -97,7 +97,7 @@ class Vector {
 
 private:
   unsigned Length;
-  std::unique_ptr<PBQPNum []> Data;
+  std::unique_ptr<PBQPNum []> Data;// Use OwningArrayRef
 };
 
 /// Return a hash_value for the given vector.

>From ef3b30d4fcc6aa37ea484c915179363976f7fd69 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sun, 27 Apr 2025 13:15:37 -0700
Subject: [PATCH 2/2] [CodeGen] Use OwningArrayRef in PBQP::Vector (NFC)

---
 llvm/include/llvm/CodeGen/PBQP/Math.h | 50 +++++++++++----------------
 1 file changed, 21 insertions(+), 29 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/PBQP/Math.h b/llvm/include/llvm/CodeGen/PBQP/Math.h
index 3ba3b9c2bf5e3..3dc158d9ac8b9 100644
--- a/llvm/include/llvm/CodeGen/PBQP/Math.h
+++ b/llvm/include/llvm/CodeGen/PBQP/Math.h
@@ -9,6 +9,7 @@
 #ifndef LLVM_CODEGEN_PBQP_MATH_H
 #define LLVM_CODEGEN_PBQP_MATH_H
 
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/Hashing.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/InterleavedRange.h"
@@ -28,83 +29,74 @@ class Vector {
 
 public:
   /// Construct a PBQP vector of the given size.
-  explicit Vector(unsigned Length)
-    : Length(Length), Data(std::make_unique<PBQPNum []>(Length)) {}
+  explicit Vector(unsigned Length) : Data(Length) {}
 
   /// Construct a PBQP vector with initializer.
-  Vector(unsigned Length, PBQPNum InitVal)
-    : Length(Length), Data(std::make_unique<PBQPNum []>(Length)) {
+  Vector(unsigned Length, PBQPNum InitVal) : Data(Length) {
     std::fill(begin(), end(), InitVal);
   }
 
   /// Copy construct a PBQP vector.
-  Vector(const Vector &V)
-    : Length(V.Length), Data(std::make_unique<PBQPNum []>(Length)) {
-    llvm::copy(V, begin());
-  }
+  Vector(const Vector &V) : Data(ArrayRef<PBQPNum>(V.Data)) {}
 
   /// Move construct a PBQP vector.
-  Vector(Vector &&V)
-    : Length(V.Length), Data(std::move(V.Data)) {
-    V.Length = 0;
-  }
+  Vector(Vector &&V) : Data(std::move(V.Data)) {}
 
   // 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; }
+  const PBQPNum *begin() const { return Data.begin(); }
+  const PBQPNum *end() const { return Data.end(); }
+  PBQPNum *begin() { return Data.begin(); }
+  PBQPNum *end() { return Data.end(); }
 
   /// Comparison operator.
   bool operator==(const Vector &V) const {
-    assert(Length != 0 && Data && "Invalid vector");
+    assert(!Data.empty() && "Invalid vector");
     return llvm::equal(*this, V);
   }
 
   /// Return the length of the vector
   unsigned getLength() const {
-    assert(Length != 0 && Data && "Invalid vector");
-    return Length;
+    assert(!Data.empty() && "Invalid vector");
+    return Data.size();
   }
 
   /// Element access.
   PBQPNum& operator[](unsigned Index) {
-    assert(Length != 0 && Data && "Invalid vector");
-    assert(Index < Length && "Vector element access out of bounds.");
+    assert(!Data.empty() && "Invalid vector");
+    assert(Index < Data.size() && "Vector element access out of bounds.");
     return Data[Index];
   }
 
   /// Const element access.
   const PBQPNum& operator[](unsigned Index) const {
-    assert(Length != 0 && Data && "Invalid vector");
-    assert(Index < Length && "Vector element access out of bounds.");
+    assert(!Data.empty() && "Invalid vector");
+    assert(Index < Data.size() && "Vector element access out of bounds.");
     return Data[Index];
   }
 
   /// Add another vector to this one.
   Vector& operator+=(const Vector &V) {
-    assert(Length != 0 && Data && "Invalid vector");
-    assert(Length == V.Length && "Vector length mismatch.");
+    assert(!Data.empty() && "Invalid vector");
+    assert(Data.size() == V.Data.size() && "Vector length mismatch.");
     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");
+    assert(!Data.empty() && "Invalid vector");
     return llvm::min_element(*this) - begin();
   }
 
 private:
-  unsigned Length;
-  std::unique_ptr<PBQPNum []> Data;// Use OwningArrayRef
+  OwningArrayRef<PBQPNum> Data;
 };
 
 /// Return a hash_value for the given vector.
 inline hash_code hash_value(const Vector &V) {
   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));
+  return hash_combine(V.Data.size(), hash_combine_range(VBegin, VEnd));
 }
 
 /// Output a textual representation of the given vector on the given



More information about the llvm-commits mailing list