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 ArrayRef<int64_t> Matrix::getRow(unsigned row) const {
105   return {&data[row * nReservedColumns], nColumns};
106 }
107 
108 void Matrix::insertColumn(unsigned pos) { insertColumns(pos, 1); }
109 void Matrix::insertColumns(unsigned pos, unsigned count) {
110   if (count == 0)
111     return;
112   assert(pos <= nColumns);
113   unsigned oldNReservedColumns = nReservedColumns;
114   if (nColumns + count > nReservedColumns) {
115     nReservedColumns = llvm::NextPowerOf2(nColumns + count);
116     data.resize(nRows * nReservedColumns);
117   }
118   nColumns += count;
119 
120   for (int ri = nRows - 1; ri >= 0; --ri) {
121     for (int ci = nReservedColumns - 1; ci >= 0; --ci) {
122       unsigned r = ri;
123       unsigned c = ci;
124       int64_t &dest = data[r * nReservedColumns + c];
125       if (c >= nColumns)
126         dest = 0;
127       else if (c >= pos + count)
128         dest = data[r * oldNReservedColumns + c - count];
129       else if (c >= pos)
130         dest = 0;
131       else
132         dest = data[r * oldNReservedColumns + c];
133     }
134   }
135 }
136 
137 void Matrix::removeColumn(unsigned pos) { removeColumns(pos, 1); }
138 void Matrix::removeColumns(unsigned pos, unsigned count) {
139   if (count == 0)
140     return;
141   assert(pos + count - 1 < nColumns);
142   for (unsigned r = 0; r < nRows; ++r) {
143     for (unsigned c = pos; c < nColumns - count; ++c)
144       at(r, c) = at(r, c + count);
145     for (unsigned c = nColumns - count; c < nColumns; ++c)
146       at(r, c) = 0;
147   }
148   nColumns -= count;
149 }
150 
151 void Matrix::insertRow(unsigned pos) { insertRows(pos, 1); }
152 void Matrix::insertRows(unsigned pos, unsigned count) {
153   if (count == 0)
154     return;
155 
156   assert(pos <= nRows);
157   resizeVertically(nRows + count);
158   for (int r = nRows - 1; r >= int(pos + count); --r)
159     copyRow(r - count, r);
160   for (int r = pos + count - 1; r >= int(pos); --r)
161     for (unsigned c = 0; c < nColumns; ++c)
162       at(r, c) = 0;
163 }
164 
165 void Matrix::removeRow(unsigned pos) { removeRows(pos, 1); }
166 void Matrix::removeRows(unsigned pos, unsigned count) {
167   if (count == 0)
168     return;
169   assert(pos + count - 1 <= nRows);
170   for (unsigned r = pos; r + count < nRows; ++r)
171     copyRow(r + count, r);
172   resizeVertically(nRows - count);
173 }
174 
175 void Matrix::copyRow(unsigned sourceRow, unsigned targetRow) {
176   if (sourceRow == targetRow)
177     return;
178   for (unsigned c = 0; c < nColumns; ++c)
179     at(targetRow, c) = at(sourceRow, c);
180 }
181 
182 void Matrix::fillRow(unsigned row, int64_t value) {
183   for (unsigned col = 0; col < nColumns; ++col)
184     at(row, col) = value;
185 }
186 
187 void Matrix::addToRow(unsigned sourceRow, unsigned targetRow, int64_t scale) {
188   if (scale == 0)
189     return;
190   for (unsigned col = 0; col < nColumns; ++col)
191     at(targetRow, col) += scale * at(sourceRow, col);
192 }
193 
194 void Matrix::addToColumn(unsigned sourceColumn, unsigned targetColumn,
195                          int64_t scale) {
196   if (scale == 0)
197     return;
198   for (unsigned row = 0, e = getNumRows(); row < e; ++row)
199     at(row, targetColumn) += scale * at(row, sourceColumn);
200 }
201 
202 void Matrix::negateColumn(unsigned column) {
203   for (unsigned row = 0, e = getNumRows(); row < e; ++row)
204     at(row, column) = -at(row, column);
205 }
206 
207 void Matrix::negateRow(unsigned row) {
208   for (unsigned column = 0, e = getNumColumns(); column < e; ++column)
209     at(row, column) = -at(row, column);
210 }
211 
212 uint64_t Matrix::normalizeRow(unsigned row, unsigned cols) {
213   if (cols == 0)
214     return 0;
215 
216   int64_t gcd = std::abs(at(row, 0));
217   for (unsigned j = 1, e = cols; j < e; ++j)
218     gcd = llvm::GreatestCommonDivisor64(gcd, std::abs(at(row, j)));
219 
220   if (gcd > 1)
221     for (unsigned j = 0, e = cols; j < e; ++j)
222       at(row, j) /= gcd;
223 
224   return gcd;
225 }
226 
227 uint64_t Matrix::normalizeRow(unsigned row) {
228   return normalizeRow(row, getNumColumns());
229 }
230 
231 SmallVector<int64_t, 8>
232 Matrix::preMultiplyWithRow(ArrayRef<int64_t> rowVec) const {
233   assert(rowVec.size() == getNumRows() && "Invalid row vector dimension!");
234 
235   SmallVector<int64_t, 8> result(getNumColumns(), 0);
236   for (unsigned col = 0, e = getNumColumns(); col < e; ++col)
237     for (unsigned i = 0, e = getNumRows(); i < e; ++i)
238       result[col] += rowVec[i] * at(i, col);
239   return result;
240 }
241 
242 SmallVector<int64_t, 8>
243 Matrix::postMultiplyWithColumn(ArrayRef<int64_t> colVec) const {
244   assert(getNumColumns() == colVec.size() &&
245          "Invalid column vector dimension!");
246 
247   SmallVector<int64_t, 8> result(getNumRows(), 0);
248   for (unsigned row = 0, e = getNumRows(); row < e; row++)
249     for (unsigned i = 0, e = getNumColumns(); i < e; i++)
250       result[row] += at(row, i) * colVec[i];
251   return result;
252 }
253 
254 void Matrix::print(raw_ostream &os) const {
255   for (unsigned row = 0; row < nRows; ++row) {
256     for (unsigned column = 0; column < nColumns; ++column)
257       os << at(row, column) << ' ';
258     os << '\n';
259   }
260 }
261 
262 void Matrix::dump() const { print(llvm::errs()); }
263 
264 bool Matrix::hasConsistentState() const {
265   if (data.size() != nRows * nReservedColumns)
266     return false;
267   if (nColumns > nReservedColumns)
268     return false;
269   for (unsigned r = 0; r < nRows; ++r)
270     for (unsigned c = nColumns; c < nReservedColumns; ++c)
271       if (data[r * nReservedColumns + c] != 0)
272         return false;
273   return true;
274 }
275