1 //===- Matrix.cpp - MLIR Matrix Class -------------------------------------===//
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 #include "mlir/Analysis/Presburger/Matrix.h"
10 #include "llvm/Support/MathExtras.h"
11 
12 using namespace mlir;
13 using namespace presburger;
14 
15 Matrix::Matrix(unsigned rows, unsigned columns, unsigned reservedRows,
16                unsigned reservedColumns)
17     : nRows(rows), nColumns(columns),
18       nReservedColumns(std::max(nColumns, reservedColumns)),
19       data(nRows * nReservedColumns) {
20   data.reserve(std::max(nRows, reservedRows) * nReservedColumns);
21 }
22 
23 Matrix Matrix::identity(unsigned dimension) {
24   Matrix matrix(dimension, dimension);
25   for (unsigned i = 0; i < dimension; ++i)
26     matrix(i, i) = 1;
27   return matrix;
28 }
29 
30 int64_t &Matrix::at(unsigned row, unsigned column) {
31   assert(row < nRows && "Row outside of range");
32   assert(column < nColumns && "Column outside of range");
33   return data[row * nReservedColumns + column];
34 }
35 
36 int64_t Matrix::at(unsigned row, unsigned column) const {
37   assert(row < nRows && "Row outside of range");
38   assert(column < nColumns && "Column outside of range");
39   return data[row * nReservedColumns + column];
40 }
41 
42 int64_t &Matrix::operator()(unsigned row, unsigned column) {
43   return at(row, column);
44 }
45 
46 int64_t Matrix::operator()(unsigned row, unsigned column) const {
47   return at(row, column);
48 }
49 
50 unsigned Matrix::getNumRows() const { return nRows; }
51 
52 unsigned Matrix::getNumColumns() const { return nColumns; }
53 
54 unsigned Matrix::getNumReservedColumns() const { return nReservedColumns; }
55 
56 unsigned Matrix::getNumReservedRows() const {
57   return data.capacity() / nReservedColumns;
58 }
59 
60 void Matrix::reserveRows(unsigned rows) {
61   data.reserve(rows * nReservedColumns);
62 }
63 
64 unsigned Matrix::appendExtraRow() {
65   resizeVertically(nRows + 1);
66   return nRows - 1;
67 }
68 
69 void Matrix::resizeHorizontally(unsigned newNColumns) {
70   if (newNColumns < nColumns)
71     removeColumns(newNColumns, nColumns - newNColumns);
72   if (newNColumns > nColumns)
73     insertColumns(nColumns, newNColumns - nColumns);
74 }
75 
76 void Matrix::resize(unsigned newNRows, unsigned newNColumns) {
77   resizeHorizontally(newNColumns);
78   resizeVertically(newNRows);
79 }
80 
81 void Matrix::resizeVertically(unsigned newNRows) {
82   nRows = newNRows;
83   data.resize(nRows * nReservedColumns);
84 }
85 
86 void Matrix::swapRows(unsigned row, unsigned otherRow) {
87   assert((row < getNumRows() && otherRow < getNumRows()) &&
88          "Given row out of bounds");
89   if (row == otherRow)
90     return;
91   for (unsigned col = 0; col < nColumns; col++)
92     std::swap(at(row, col), at(otherRow, col));
93 }
94 
95 void Matrix::swapColumns(unsigned column, unsigned otherColumn) {
96   assert((column < getNumColumns() && otherColumn < getNumColumns()) &&
97          "Given column out of bounds");
98   if (column == otherColumn)
99     return;
100   for (unsigned row = 0; row < nRows; row++)
101     std::swap(at(row, column), at(row, otherColumn));
102 }
103 
104 MutableArrayRef<int64_t> Matrix::getRow(unsigned row) {
105   return {&data[row * nReservedColumns], nColumns};
106 }
107 
108 ArrayRef<int64_t> Matrix::getRow(unsigned row) const {
109   return {&data[row * nReservedColumns], nColumns};
110 }
111 
112 void Matrix::insertColumn(unsigned pos) { insertColumns(pos, 1); }
113 void Matrix::insertColumns(unsigned pos, unsigned count) {
114   if (count == 0)
115     return;
116   assert(pos <= nColumns);
117   unsigned oldNReservedColumns = nReservedColumns;
118   if (nColumns + count > nReservedColumns) {
119     nReservedColumns = llvm::NextPowerOf2(nColumns + count);
120     data.resize(nRows * nReservedColumns);
121   }
122   nColumns += count;
123 
124   for (int ri = nRows - 1; ri >= 0; --ri) {
125     for (int ci = nReservedColumns - 1; ci >= 0; --ci) {
126       unsigned r = ri;
127       unsigned c = ci;
128       int64_t &dest = data[r * nReservedColumns + c];
129       if (c >= nColumns) { // NOLINT
130         // Out of bounds columns are zero-initialized. NOLINT because clang-tidy
131         // complains about this branch being the same as the c >= pos one.
132         //
133         // TODO: this case can be skipped if the number of reserved columns
134         // didn't change.
135         dest = 0;
136       } else if (c >= pos + count) {
137         // Shift the data occuring after the inserted columns.
138         dest = data[r * oldNReservedColumns + c - count];
139       } else if (c >= pos) {
140         // The inserted columns are also zero-initialized.
141         dest = 0;
142       } else {
143         // The columns before the inserted columns stay at the same (row, col)
144         // but this corresponds to a different location in the linearized array
145         // if the number of reserved columns changed.
146         if (nReservedColumns == oldNReservedColumns)
147           break;
148         dest = data[r * oldNReservedColumns + c];
149       }
150     }
151   }
152 }
153 
154 void Matrix::removeColumn(unsigned pos) { removeColumns(pos, 1); }
155 void Matrix::removeColumns(unsigned pos, unsigned count) {
156   if (count == 0)
157     return;
158   assert(pos + count - 1 < nColumns);
159   for (unsigned r = 0; r < nRows; ++r) {
160     for (unsigned c = pos; c < nColumns - count; ++c)
161       at(r, c) = at(r, c + count);
162     for (unsigned c = nColumns - count; c < nColumns; ++c)
163       at(r, c) = 0;
164   }
165   nColumns -= count;
166 }
167 
168 void Matrix::insertRow(unsigned pos) { insertRows(pos, 1); }
169 void Matrix::insertRows(unsigned pos, unsigned count) {
170   if (count == 0)
171     return;
172 
173   assert(pos <= nRows);
174   resizeVertically(nRows + count);
175   for (int r = nRows - 1; r >= int(pos + count); --r)
176     copyRow(r - count, r);
177   for (int r = pos + count - 1; r >= int(pos); --r)
178     for (unsigned c = 0; c < nColumns; ++c)
179       at(r, c) = 0;
180 }
181 
182 void Matrix::removeRow(unsigned pos) { removeRows(pos, 1); }
183 void Matrix::removeRows(unsigned pos, unsigned count) {
184   if (count == 0)
185     return;
186   assert(pos + count - 1 <= nRows);
187   for (unsigned r = pos; r + count < nRows; ++r)
188     copyRow(r + count, r);
189   resizeVertically(nRows - count);
190 }
191 
192 void Matrix::copyRow(unsigned sourceRow, unsigned targetRow) {
193   if (sourceRow == targetRow)
194     return;
195   for (unsigned c = 0; c < nColumns; ++c)
196     at(targetRow, c) = at(sourceRow, c);
197 }
198 
199 void Matrix::fillRow(unsigned row, int64_t value) {
200   for (unsigned col = 0; col < nColumns; ++col)
201     at(row, col) = value;
202 }
203 
204 void Matrix::addToRow(unsigned sourceRow, unsigned targetRow, int64_t scale) {
205   if (scale == 0)
206     return;
207   for (unsigned col = 0; col < nColumns; ++col)
208     at(targetRow, col) += scale * at(sourceRow, col);
209 }
210 
211 void Matrix::addToColumn(unsigned sourceColumn, unsigned targetColumn,
212                          int64_t scale) {
213   if (scale == 0)
214     return;
215   for (unsigned row = 0, e = getNumRows(); row < e; ++row)
216     at(row, targetColumn) += scale * at(row, sourceColumn);
217 }
218 
219 void Matrix::negateColumn(unsigned column) {
220   for (unsigned row = 0, e = getNumRows(); row < e; ++row)
221     at(row, column) = -at(row, column);
222 }
223 
224 void Matrix::negateRow(unsigned row) {
225   for (unsigned column = 0, e = getNumColumns(); column < e; ++column)
226     at(row, column) = -at(row, column);
227 }
228 
229 uint64_t Matrix::normalizeRow(unsigned row, unsigned cols) {
230   if (cols == 0)
231     return 0;
232 
233   int64_t gcd = std::abs(at(row, 0));
234   for (unsigned j = 1, e = cols; j < e; ++j)
235     gcd = llvm::GreatestCommonDivisor64(gcd, std::abs(at(row, j)));
236 
237   if (gcd > 1)
238     for (unsigned j = 0, e = cols; j < e; ++j)
239       at(row, j) /= gcd;
240 
241   return gcd;
242 }
243 
244 uint64_t Matrix::normalizeRow(unsigned row) {
245   return normalizeRow(row, getNumColumns());
246 }
247 
248 SmallVector<int64_t, 8>
249 Matrix::preMultiplyWithRow(ArrayRef<int64_t> rowVec) const {
250   assert(rowVec.size() == getNumRows() && "Invalid row vector dimension!");
251 
252   SmallVector<int64_t, 8> result(getNumColumns(), 0);
253   for (unsigned col = 0, e = getNumColumns(); col < e; ++col)
254     for (unsigned i = 0, e = getNumRows(); i < e; ++i)
255       result[col] += rowVec[i] * at(i, col);
256   return result;
257 }
258 
259 SmallVector<int64_t, 8>
260 Matrix::postMultiplyWithColumn(ArrayRef<int64_t> colVec) const {
261   assert(getNumColumns() == colVec.size() &&
262          "Invalid column vector dimension!");
263 
264   SmallVector<int64_t, 8> result(getNumRows(), 0);
265   for (unsigned row = 0, e = getNumRows(); row < e; row++)
266     for (unsigned i = 0, e = getNumColumns(); i < e; i++)
267       result[row] += at(row, i) * colVec[i];
268   return result;
269 }
270 
271 void Matrix::print(raw_ostream &os) const {
272   for (unsigned row = 0; row < nRows; ++row) {
273     for (unsigned column = 0; column < nColumns; ++column)
274       os << at(row, column) << ' ';
275     os << '\n';
276   }
277 }
278 
279 void Matrix::dump() const { print(llvm::errs()); }
280 
281 bool Matrix::hasConsistentState() const {
282   if (data.size() != nRows * nReservedColumns)
283     return false;
284   if (nColumns > nReservedColumns)
285     return false;
286   for (unsigned r = 0; r < nRows; ++r)
287     for (unsigned c = nColumns; c < nReservedColumns; ++c)
288       if (data[r * nReservedColumns + c] != 0)
289         return false;
290   return true;
291 }
292