LLVM 22.0.0git
Math.h
Go to the documentation of this file.
1//===- Math.h - PBQP Vector and Matrix classes ------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CODEGEN_PBQP_MATH_H
10#define LLVM_CODEGEN_PBQP_MATH_H
11
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/Hashing.h"
14#include "llvm/ADT/STLExtras.h"
16#include <algorithm>
17#include <cassert>
18#include <functional>
19#include <memory>
20
21namespace llvm {
22namespace PBQP {
23
24using PBQPNum = float;
25
26/// PBQP Vector class.
27class Vector {
28public:
29 /// Construct a PBQP vector of the given size.
30 explicit Vector(unsigned Length) : Data(Length) {}
31
32 /// Construct a PBQP vector with initializer.
33 Vector(unsigned Length, PBQPNum InitVal) : Data(Length) {
34 std::fill(begin(), end(), InitVal);
35 }
36
37 /// Copy construct a PBQP vector.
38 Vector(const Vector &V) : Data(ArrayRef<PBQPNum>(V.Data)) {}
39
40 /// Move construct a PBQP vector.
41 Vector(Vector &&V) : Data(std::move(V.Data)) {}
42
43 // Iterator-based access.
44 const PBQPNum *begin() const { return Data.begin(); }
45 const PBQPNum *end() const { return Data.end(); }
46 PBQPNum *begin() { return Data.begin(); }
47 PBQPNum *end() { return Data.end(); }
48
49 /// Comparison operator.
50 bool operator==(const Vector &V) const {
51 assert(!Data.empty() && "Invalid vector");
52 return llvm::equal(*this, V);
53 }
54
55 /// Return the length of the vector
56 unsigned getLength() const {
57 assert(!Data.empty() && "Invalid vector");
58 return Data.size();
59 }
60
61 /// Element access.
63 assert(!Data.empty() && "Invalid vector");
64 assert(Index < Data.size() && "Vector element access out of bounds.");
65 return Data[Index];
66 }
67
68 /// Const element access.
69 const PBQPNum& operator[](unsigned Index) const {
70 assert(!Data.empty() && "Invalid vector");
71 assert(Index < Data.size() && "Vector element access out of bounds.");
72 return Data[Index];
73 }
74
75 /// Add another vector to this one.
77 assert(!Data.empty() && "Invalid vector");
78 assert(Data.size() == V.Data.size() && "Vector length mismatch.");
79 std::transform(begin(), end(), V.begin(), begin(), std::plus<PBQPNum>());
80 return *this;
81 }
82
83 /// Returns the index of the minimum value in this vector
84 unsigned minIndex() const {
85 assert(!Data.empty() && "Invalid vector");
86 return llvm::min_element(*this) - begin();
87 }
88
89private:
91};
92
93/// Return a hash_value for the given vector.
94inline hash_code hash_value(const Vector &V) {
95 const unsigned *VBegin = reinterpret_cast<const unsigned *>(V.begin());
96 const unsigned *VEnd = reinterpret_cast<const unsigned *>(V.end());
97 return hash_combine(V.getLength(), hash_combine_range(VBegin, VEnd));
98}
99
100/// Output a textual representation of the given vector on the given
101/// output stream.
102template <typename OStream>
103OStream& operator<<(OStream &OS, const Vector &V) {
104 assert((V.getLength() != 0) && "Zero-length vector badness.");
105 OS << "[ " << llvm::interleaved(V) << " ]";
106 return OS;
107}
108
109/// PBQP Matrix class
110class Matrix {
111private:
112 friend hash_code hash_value(const Matrix &);
113
114public:
115 /// Construct a PBQP Matrix with the given dimensions.
116 Matrix(unsigned Rows, unsigned Cols) :
117 Rows(Rows), Cols(Cols), Data(std::make_unique<PBQPNum []>(Rows * Cols)) {
118 }
119
120 /// Construct a PBQP Matrix with the given dimensions and initial
121 /// value.
122 Matrix(unsigned Rows, unsigned Cols, PBQPNum InitVal)
123 : Rows(Rows), Cols(Cols),
124 Data(std::make_unique<PBQPNum []>(Rows * Cols)) {
125 std::fill(Data.get(), Data.get() + (Rows * Cols), InitVal);
126 }
127
128 /// Copy construct a PBQP matrix.
129 Matrix(const Matrix &M)
130 : Rows(M.Rows), Cols(M.Cols),
131 Data(std::make_unique<PBQPNum []>(Rows * Cols)) {
132 std::copy(M.Data.get(), M.Data.get() + (Rows * Cols), Data.get());
133 }
134
135 /// Move construct a PBQP matrix.
137 : Rows(M.Rows), Cols(M.Cols), Data(std::move(M.Data)) {
138 M.Rows = M.Cols = 0;
139 }
140
141 /// Comparison operator.
142 bool operator==(const Matrix &M) const {
143 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
144 if (Rows != M.Rows || Cols != M.Cols)
145 return false;
146 return std::equal(Data.get(), Data.get() + (Rows * Cols), M.Data.get());
147 }
148
149 /// Return the number of rows in this matrix.
150 unsigned getRows() const {
151 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
152 return Rows;
153 }
154
155 /// Return the number of cols in this matrix.
156 unsigned getCols() const {
157 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
158 return Cols;
159 }
160
161 /// Matrix element access.
162 PBQPNum* operator[](unsigned R) {
163 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
164 assert(R < Rows && "Row out of bounds.");
165 return Data.get() + (R * Cols);
166 }
167
168 /// Matrix element access.
169 const PBQPNum* operator[](unsigned R) const {
170 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
171 assert(R < Rows && "Row out of bounds.");
172 return Data.get() + (R * Cols);
173 }
174
175 /// Returns the given row as a vector.
176 Vector getRowAsVector(unsigned R) const {
177 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
178 Vector V(Cols);
179 for (unsigned C = 0; C < Cols; ++C)
180 V[C] = (*this)[R][C];
181 return V;
182 }
183
184 /// Returns the given column as a vector.
185 Vector getColAsVector(unsigned C) const {
186 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
187 Vector V(Rows);
188 for (unsigned R = 0; R < Rows; ++R)
189 V[R] = (*this)[R][C];
190 return V;
191 }
192
193 /// Matrix transpose.
195 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
196 Matrix M(Cols, Rows);
197 for (unsigned r = 0; r < Rows; ++r)
198 for (unsigned c = 0; c < Cols; ++c)
199 M[c][r] = (*this)[r][c];
200 return M;
201 }
202
203 /// Add the given matrix to this one.
205 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
206 assert(Rows == M.Rows && Cols == M.Cols &&
207 "Matrix dimensions mismatch.");
208 std::transform(Data.get(), Data.get() + (Rows * Cols), M.Data.get(),
209 Data.get(), std::plus<PBQPNum>());
210 return *this;
211 }
212
214 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
215 Matrix Tmp(*this);
216 Tmp += M;
217 return Tmp;
218 }
219
220private:
221 unsigned Rows, Cols;
222 std::unique_ptr<PBQPNum []> Data;
223};
224
225/// Return a hash_code for the given matrix.
226inline hash_code hash_value(const Matrix &M) {
227 unsigned *MBegin = reinterpret_cast<unsigned*>(M.Data.get());
228 unsigned *MEnd =
229 reinterpret_cast<unsigned*>(M.Data.get() + (M.Rows * M.Cols));
230 return hash_combine(M.Rows, M.Cols, hash_combine_range(MBegin, MEnd));
231}
232
233/// Output a textual representation of the given matrix on the given
234/// output stream.
235template <typename OStream>
236OStream& operator<<(OStream &OS, const Matrix &M) {
237 assert((M.getRows() != 0) && "Zero-row matrix badness.");
238 for (unsigned i = 0; i < M.getRows(); ++i)
239 OS << M.getRowAsVector(i) << "\n";
240 return OS;
241}
242
243template <typename Metadata>
244class MDVector : public Vector {
245public:
246 MDVector(const Vector &v) : Vector(v), md(*this) {}
247 MDVector(Vector &&v) : Vector(std::move(v)), md(*this) { }
248
249 const Metadata& getMetadata() const { return md; }
250
251private:
252 Metadata md;
253};
254
255template <typename Metadata>
257 return hash_value(static_cast<const Vector&>(V));
258}
259
260template <typename Metadata>
261class MDMatrix : public Matrix {
262public:
263 MDMatrix(const Matrix &m) : Matrix(m), md(*this) {}
264 MDMatrix(Matrix &&m) : Matrix(std::move(m)), md(*this) { }
265
266 const Metadata& getMetadata() const { return md; }
267
268private:
269 Metadata md;
270};
271
272template <typename Metadata>
274 return hash_value(static_cast<const Matrix&>(M));
275}
276
277} // end namespace PBQP
278} // end namespace llvm
279
280#endif // LLVM_CODEGEN_PBQP_MATH_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint32_t Index
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:147
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:142
Root of the metadata hierarchy.
Definition: Metadata.h:63
iterator end() const
Definition: ArrayRef.h:348
iterator begin() const
Definition: ArrayRef.h:347
This is a MutableArrayRef that owns its array.
Definition: ArrayRef.h:454
MDMatrix(const Matrix &m)
Definition: Math.h:263
MDMatrix(Matrix &&m)
Definition: Math.h:264
const Metadata & getMetadata() const
Definition: Math.h:266
const Metadata & getMetadata() const
Definition: Math.h:249
MDVector(const Vector &v)
Definition: Math.h:246
MDVector(Vector &&v)
Definition: Math.h:247
PBQP Matrix class.
Definition: Math.h:110
Matrix operator+(const Matrix &M)
Definition: Math.h:213
PBQPNum * operator[](unsigned R)
Matrix element access.
Definition: Math.h:162
Matrix & operator+=(const Matrix &M)
Add the given matrix to this one.
Definition: Math.h:204
unsigned getRows() const
Return the number of rows in this matrix.
Definition: Math.h:150
const PBQPNum * operator[](unsigned R) const
Matrix element access.
Definition: Math.h:169
Vector getColAsVector(unsigned C) const
Returns the given column as a vector.
Definition: Math.h:185
bool operator==(const Matrix &M) const
Comparison operator.
Definition: Math.h:142
unsigned getCols() const
Return the number of cols in this matrix.
Definition: Math.h:156
friend hash_code hash_value(const Matrix &)
Return a hash_code for the given matrix.
Definition: Math.h:226
Matrix(Matrix &&M)
Move construct a PBQP matrix.
Definition: Math.h:136
Vector getRowAsVector(unsigned R) const
Returns the given row as a vector.
Definition: Math.h:176
Matrix(const Matrix &M)
Copy construct a PBQP matrix.
Definition: Math.h:129
Matrix(unsigned Rows, unsigned Cols, PBQPNum InitVal)
Construct a PBQP Matrix with the given dimensions and initial value.
Definition: Math.h:122
Matrix transpose() const
Matrix transpose.
Definition: Math.h:194
Matrix(unsigned Rows, unsigned Cols)
Construct a PBQP Matrix with the given dimensions.
Definition: Math.h:116
PBQP Vector class.
Definition: Math.h:27
unsigned getLength() const
Return the length of the vector.
Definition: Math.h:56
PBQPNum & operator[](unsigned Index)
Element access.
Definition: Math.h:62
PBQPNum * begin()
Definition: Math.h:46
unsigned minIndex() const
Returns the index of the minimum value in this vector.
Definition: Math.h:84
const PBQPNum * begin() const
Definition: Math.h:44
Vector(unsigned Length, PBQPNum InitVal)
Construct a PBQP vector with initializer.
Definition: Math.h:33
Vector & operator+=(const Vector &V)
Add another vector to this one.
Definition: Math.h:76
Vector(Vector &&V)
Move construct a PBQP vector.
Definition: Math.h:41
Vector(const Vector &V)
Copy construct a PBQP vector.
Definition: Math.h:38
PBQPNum * end()
Definition: Math.h:47
const PBQPNum & operator[](unsigned Index) const
Const element access.
Definition: Math.h:69
const PBQPNum * end() const
Definition: Math.h:45
bool operator==(const Vector &V) const
Comparison operator.
Definition: Math.h:50
Vector(unsigned Length)
Construct a PBQP vector of the given size.
Definition: Math.h:30
An opaque object representing a hash code.
Definition: Hashing.h:76
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
OStream & operator<<(OStream &OS, const Vector &V)
Output a textual representation of the given vector on the given output stream.
Definition: Math.h:103
float PBQPNum
Definition: Math.h:24
hash_code hash_value(const Vector &V)
Return a hash_value for the given vector.
Definition: Math.h:94
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Length
Definition: DWP.cpp:477
auto min_element(R &&Range)
Provide wrappers to std::min_element which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:2039
InterleavedRange< Range > interleaved(const Range &R, StringRef Separator=", ", StringRef Prefix="", StringRef Suffix="")
Output range R as a sequence of interleaved elements.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1886
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:595
bool equal(L &&LRange, R &&RRange)
Wrapper function around std::equal to detect if pair-wise elements between two ranges are the same.
Definition: STLExtras.h:2107
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition: Hashing.h:469
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856