LLVM 23.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"
17#include <algorithm>
18#include <cassert>
19#include <functional>
20#include <memory>
21
22namespace llvm {
23namespace PBQP {
24
25using PBQPNum = float;
26
27/// PBQP Vector class.
28class Vector {
29public:
30 /// Construct a PBQP vector of the given size.
31 explicit Vector(unsigned Length) : Data(Length) {}
32
33 /// Construct a PBQP vector with initializer.
34 Vector(unsigned Length, PBQPNum InitVal) : Data(Length) {
35 std::fill(begin(), end(), InitVal);
36 }
37
38 /// Copy construct a PBQP vector.
39 Vector(const Vector &V) : Data(ArrayRef<PBQPNum>(V.Data)) {}
40
41 /// Move construct a PBQP vector.
42 Vector(Vector &&V) : Data(std::move(V.Data)) {}
43
44 // Iterator-based access.
45 const PBQPNum *begin() const { return Data.data(); }
46 const PBQPNum *end() const { return Data.data() + Data.size(); }
47 PBQPNum *begin() { return Data.data(); }
48 PBQPNum *end() { return Data.data() + Data.size(); }
49
50 /// Comparison operator.
51 bool operator==(const Vector &V) const {
52 assert(!Data.empty() && "Invalid vector");
53 return llvm::equal(*this, V);
54 }
55
56 /// Return the length of the vector
57 unsigned getLength() const {
58 assert(!Data.empty() && "Invalid vector");
59 return Data.size();
60 }
61
62 /// Element access.
63 PBQPNum& operator[](unsigned Index) {
64 assert(!Data.empty() && "Invalid vector");
65 assert(Index < Data.size() && "Vector element access out of bounds.");
66 return Data[Index];
67 }
68
69 /// Const element access.
70 const PBQPNum& operator[](unsigned Index) const {
71 assert(!Data.empty() && "Invalid vector");
72 assert(Index < Data.size() && "Vector element access out of bounds.");
73 return Data[Index];
74 }
75
76 /// Add another vector to this one.
78 assert(!Data.empty() && "Invalid vector");
79 assert(Data.size() == V.Data.size() && "Vector length mismatch.");
80 std::transform(begin(), end(), V.begin(), begin(), std::plus<PBQPNum>());
81 return *this;
82 }
83
84 /// Returns the index of the minimum value in this vector
85 unsigned minIndex() const {
86 assert(!Data.empty() && "Invalid vector");
87 return llvm::min_element(*this) - begin();
88 }
89
90private:
92};
93
94/// Return a hash_value for the given vector.
95inline hash_code hash_value(const Vector &V) {
96 const unsigned *VBegin = reinterpret_cast<const unsigned *>(V.begin());
97 const unsigned *VEnd = reinterpret_cast<const unsigned *>(V.end());
98 return hash_combine(V.getLength(), hash_combine_range(VBegin, VEnd));
99}
100
101/// Output a textual representation of the given vector on the given
102/// output stream.
103template <typename OStream>
104OStream& operator<<(OStream &OS, const Vector &V) {
105 assert((V.getLength() != 0) && "Zero-length vector badness.");
106 OS << "[ " << llvm::interleaved(V) << " ]";
107 return OS;
108}
109
110/// PBQP Matrix class
111class Matrix {
112private:
113 friend hash_code hash_value(const Matrix &);
114
115public:
116 /// Construct a PBQP Matrix with the given dimensions.
117 Matrix(unsigned Rows, unsigned Cols) :
118 Rows(Rows), Cols(Cols), Data(std::make_unique<PBQPNum []>(Rows * Cols)) {
119 }
120
121 /// Construct a PBQP Matrix with the given dimensions and initial
122 /// value.
123 Matrix(unsigned Rows, unsigned Cols, PBQPNum InitVal)
124 : Rows(Rows), Cols(Cols),
125 Data(std::make_unique<PBQPNum []>(Rows * Cols)) {
126 std::fill(Data.get(), Data.get() + (Rows * Cols), InitVal);
127 }
128
129 /// Copy construct a PBQP matrix.
130 Matrix(const Matrix &M)
131 : Rows(M.Rows), Cols(M.Cols),
132 Data(std::make_unique<PBQPNum []>(Rows * Cols)) {
133 std::copy(M.Data.get(), M.Data.get() + (Rows * Cols), Data.get());
134 }
135
136 /// Move construct a PBQP matrix.
138 : Rows(M.Rows), Cols(M.Cols), Data(std::move(M.Data)) {
139 M.Rows = M.Cols = 0;
140 }
141
142 /// Comparison operator.
143 bool operator==(const Matrix &M) const {
144 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
145 if (Rows != M.Rows || Cols != M.Cols)
146 return false;
147 return std::equal(Data.get(), Data.get() + (Rows * Cols), M.Data.get());
148 }
149
150 /// Return the number of rows in this matrix.
151 unsigned getRows() const {
152 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
153 return Rows;
154 }
155
156 /// Return the number of cols in this matrix.
157 unsigned getCols() const {
158 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
159 return Cols;
160 }
161
162 /// Matrix element access.
163 PBQPNum* operator[](unsigned R) {
164 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
165 assert(R < Rows && "Row out of bounds.");
166 return Data.get() + (R * Cols);
167 }
168
169 /// Matrix element access.
170 const PBQPNum* operator[](unsigned R) const {
171 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
172 assert(R < Rows && "Row out of bounds.");
173 return Data.get() + (R * Cols);
174 }
175
176 /// Returns the given row as a vector.
177 Vector getRowAsVector(unsigned R) const {
178 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
179 Vector V(Cols);
180 for (unsigned C = 0; C < Cols; ++C)
181 V[C] = (*this)[R][C];
182 return V;
183 }
184
185 /// Returns the given column as a vector.
186 Vector getColAsVector(unsigned C) const {
187 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
188 Vector V(Rows);
189 for (unsigned R = 0; R < Rows; ++R)
190 V[R] = (*this)[R][C];
191 return V;
192 }
193
194 /// Matrix transpose.
196 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
197 Matrix M(Cols, Rows);
198 for (unsigned r = 0; r < Rows; ++r)
199 for (unsigned c = 0; c < Cols; ++c)
200 M[c][r] = (*this)[r][c];
201 return M;
202 }
203
204 /// Add the given matrix to this one.
206 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
207 assert(Rows == M.Rows && Cols == M.Cols &&
208 "Matrix dimensions mismatch.");
209 std::transform(Data.get(), Data.get() + (Rows * Cols), M.Data.get(),
210 Data.get(), std::plus<PBQPNum>());
211 return *this;
212 }
213
215 assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
216 Matrix Tmp(*this);
217 Tmp += M;
218 return Tmp;
219 }
220
221private:
222 unsigned Rows, Cols;
223 std::unique_ptr<PBQPNum []> Data;
224};
225
226/// Return a hash_code for the given matrix.
227inline hash_code hash_value(const Matrix &M) {
228 unsigned *MBegin = reinterpret_cast<unsigned*>(M.Data.get());
229 unsigned *MEnd =
230 reinterpret_cast<unsigned*>(M.Data.get() + (M.Rows * M.Cols));
231 return hash_combine(M.Rows, M.Cols, hash_combine_range(MBegin, MEnd));
232}
233
234/// Output a textual representation of the given matrix on the given
235/// output stream.
236template <typename OStream>
237OStream& operator<<(OStream &OS, const Matrix &M) {
238 assert((M.getRows() != 0) && "Zero-row matrix badness.");
239 for (unsigned i = 0; i < M.getRows(); ++i)
240 OS << M.getRowAsVector(i) << "\n";
241 return OS;
242}
243
244template <typename Metadata>
245class MDVector : public Vector {
246public:
247 MDVector(const Vector &v) : Vector(v), md(*this) {}
248 MDVector(Vector &&v) : Vector(std::move(v)), md(*this) { }
249
250 const Metadata& getMetadata() const { return md; }
251
252private:
253 Metadata md;
254};
255
256template <typename Metadata>
258 return hash_value(static_cast<const Vector&>(V));
259}
260
261template <typename Metadata>
262class MDMatrix : public Matrix {
263public:
264 MDMatrix(const Matrix &m) : Matrix(m), md(*this) {}
265 MDMatrix(Matrix &&m) : Matrix(std::move(m)), md(*this) { }
266
267 const Metadata& getMetadata() const { return md; }
268
269private:
270 Metadata md;
271};
272
273template <typename Metadata>
275 return hash_value(static_cast<const Matrix&>(M));
276}
277
278} // end namespace PBQP
279} // end namespace llvm
280
281#endif // LLVM_CODEGEN_PBQP_MATH_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Root of the metadata hierarchy.
Definition Metadata.h:64
MDMatrix(const Matrix &m)
Definition Math.h:264
MDMatrix(Matrix &&m)
Definition Math.h:265
const Metadata & getMetadata() const
Definition Math.h:267
const Metadata & getMetadata() const
Definition Math.h:250
MDVector(const Vector &v)
Definition Math.h:247
MDVector(Vector &&v)
Definition Math.h:248
PBQP Matrix class.
Definition Math.h:111
Matrix operator+(const Matrix &M)
Definition Math.h:214
PBQPNum * operator[](unsigned R)
Matrix element access.
Definition Math.h:163
Matrix & operator+=(const Matrix &M)
Add the given matrix to this one.
Definition Math.h:205
friend hash_code hash_value(const Matrix &)
Return a hash_code for the given matrix.
Definition Math.h:227
unsigned getRows() const
Return the number of rows in this matrix.
Definition Math.h:151
const PBQPNum * operator[](unsigned R) const
Matrix element access.
Definition Math.h:170
Vector getColAsVector(unsigned C) const
Returns the given column as a vector.
Definition Math.h:186
bool operator==(const Matrix &M) const
Comparison operator.
Definition Math.h:143
unsigned getCols() const
Return the number of cols in this matrix.
Definition Math.h:157
Matrix(Matrix &&M)
Move construct a PBQP matrix.
Definition Math.h:137
Vector getRowAsVector(unsigned R) const
Returns the given row as a vector.
Definition Math.h:177
Matrix(const Matrix &M)
Copy construct a PBQP matrix.
Definition Math.h:130
Matrix(unsigned Rows, unsigned Cols, PBQPNum InitVal)
Construct a PBQP Matrix with the given dimensions and initial value.
Definition Math.h:123
Matrix transpose() const
Matrix transpose.
Definition Math.h:195
Matrix(unsigned Rows, unsigned Cols)
Construct a PBQP Matrix with the given dimensions.
Definition Math.h:117
PBQP Vector class.
Definition Math.h:28
unsigned getLength() const
Return the length of the vector.
Definition Math.h:57
PBQPNum & operator[](unsigned Index)
Element access.
Definition Math.h:63
PBQPNum * begin()
Definition Math.h:47
unsigned minIndex() const
Returns the index of the minimum value in this vector.
Definition Math.h:85
const PBQPNum * begin() const
Definition Math.h:45
Vector(unsigned Length, PBQPNum InitVal)
Construct a PBQP vector with initializer.
Definition Math.h:34
Vector & operator+=(const Vector &V)
Add another vector to this one.
Definition Math.h:77
Vector(Vector &&V)
Move construct a PBQP vector.
Definition Math.h:42
Vector(const Vector &V)
Copy construct a PBQP vector.
Definition Math.h:39
PBQPNum * end()
Definition Math.h:48
const PBQPNum & operator[](unsigned Index) const
Const element access.
Definition Math.h:70
const PBQPNum * end() const
Definition Math.h:46
bool operator==(const Vector &V) const
Comparison operator.
Definition Math.h:51
Vector(unsigned Length)
Construct a PBQP vector of the given size.
Definition Math.h:31
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
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:104
hash_code hash_value(const Vector &V)
Return a hash_value for the given vector.
Definition Math.h:95
float PBQPNum
Definition Math.h:25
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ Length
Definition DWP.cpp:532
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:2068
InterleavedRange< Range > interleaved(const Range &R, StringRef Separator=", ", StringRef Prefix="", StringRef Suffix="")
Output range R as a sequence of interleaved elements.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
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:1915
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition Hashing.h:592
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:2136
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition Hashing.h:466
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870