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 "mlir/Analysis/Presburger/Utils.h" 11 #include "llvm/Support/MathExtras.h" 12 13 using namespace mlir; 14 using namespace presburger; 15 16 Matrix::Matrix(unsigned rows, unsigned columns, unsigned reservedRows, 17 unsigned reservedColumns) 18 : nRows(rows), nColumns(columns), 19 nReservedColumns(std::max(nColumns, reservedColumns)), 20 data(nRows * nReservedColumns) { 21 data.reserve(std::max(nRows, reservedRows) * nReservedColumns); 22 } 23 24 Matrix Matrix::identity(unsigned dimension) { 25 Matrix matrix(dimension, dimension); 26 for (unsigned i = 0; i < dimension; ++i) 27 matrix(i, i) = 1; 28 return matrix; 29 } 30 31 unsigned Matrix::getNumReservedRows() const { 32 return data.capacity() / nReservedColumns; 33 } 34 35 void Matrix::reserveRows(unsigned rows) { 36 data.reserve(rows * nReservedColumns); 37 } 38 39 unsigned Matrix::appendExtraRow() { 40 resizeVertically(nRows + 1); 41 return nRows - 1; 42 } 43 44 unsigned Matrix::appendExtraRow(ArrayRef<int64_t> elems) { 45 assert(elems.size() == nColumns && "elems must match row length!"); 46 unsigned row = appendExtraRow(); 47 for (unsigned col = 0; col < nColumns; ++col) 48 at(row, col) = elems[col]; 49 return row; 50 } 51 52 void Matrix::resizeHorizontally(unsigned newNColumns) { 53 if (newNColumns < nColumns) 54 removeColumns(newNColumns, nColumns - newNColumns); 55 if (newNColumns > nColumns) 56 insertColumns(nColumns, newNColumns - nColumns); 57 } 58 59 void Matrix::resize(unsigned newNRows, unsigned newNColumns) { 60 resizeHorizontally(newNColumns); 61 resizeVertically(newNRows); 62 } 63 64 void Matrix::resizeVertically(unsigned newNRows) { 65 nRows = newNRows; 66 data.resize(nRows * nReservedColumns); 67 } 68 69 void Matrix::swapRows(unsigned row, unsigned otherRow) { 70 assert((row < getNumRows() && otherRow < getNumRows()) && 71 "Given row out of bounds"); 72 if (row == otherRow) 73 return; 74 for (unsigned col = 0; col < nColumns; col++) 75 std::swap(at(row, col), at(otherRow, col)); 76 } 77 78 void Matrix::swapColumns(unsigned column, unsigned otherColumn) { 79 assert((column < getNumColumns() && otherColumn < getNumColumns()) && 80 "Given column out of bounds"); 81 if (column == otherColumn) 82 return; 83 for (unsigned row = 0; row < nRows; row++) 84 std::swap(at(row, column), at(row, otherColumn)); 85 } 86 87 MutableArrayRef<int64_t> Matrix::getRow(unsigned row) { 88 return {&data[row * nReservedColumns], nColumns}; 89 } 90 91 ArrayRef<int64_t> Matrix::getRow(unsigned row) const { 92 return {&data[row * nReservedColumns], nColumns}; 93 } 94 95 void Matrix::insertColumn(unsigned pos) { insertColumns(pos, 1); } 96 void Matrix::insertColumns(unsigned pos, unsigned count) { 97 if (count == 0) 98 return; 99 assert(pos <= nColumns); 100 unsigned oldNReservedColumns = nReservedColumns; 101 if (nColumns + count > nReservedColumns) { 102 nReservedColumns = llvm::NextPowerOf2(nColumns + count); 103 data.resize(nRows * nReservedColumns); 104 } 105 nColumns += count; 106 107 for (int ri = nRows - 1; ri >= 0; --ri) { 108 for (int ci = nReservedColumns - 1; ci >= 0; --ci) { 109 unsigned r = ri; 110 unsigned c = ci; 111 int64_t &dest = data[r * nReservedColumns + c]; 112 if (c >= nColumns) { // NOLINT 113 // Out of bounds columns are zero-initialized. NOLINT because clang-tidy 114 // complains about this branch being the same as the c >= pos one. 115 // 116 // TODO: this case can be skipped if the number of reserved columns 117 // didn't change. 118 dest = 0; 119 } else if (c >= pos + count) { 120 // Shift the data occuring after the inserted columns. 121 dest = data[r * oldNReservedColumns + c - count]; 122 } else if (c >= pos) { 123 // The inserted columns are also zero-initialized. 124 dest = 0; 125 } else { 126 // The columns before the inserted columns stay at the same (row, col) 127 // but this corresponds to a different location in the linearized array 128 // if the number of reserved columns changed. 129 if (nReservedColumns == oldNReservedColumns) 130 break; 131 dest = data[r * oldNReservedColumns + c]; 132 } 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 int64_t Matrix::normalizeRow(unsigned row, unsigned cols) { 213 return normalizeRange(getRow(row).slice(0, cols)); 214 } 215 216 int64_t Matrix::normalizeRow(unsigned row) { 217 return normalizeRow(row, getNumColumns()); 218 } 219 220 SmallVector<int64_t, 8> 221 Matrix::preMultiplyWithRow(ArrayRef<int64_t> rowVec) const { 222 assert(rowVec.size() == getNumRows() && "Invalid row vector dimension!"); 223 224 SmallVector<int64_t, 8> result(getNumColumns(), 0); 225 for (unsigned col = 0, e = getNumColumns(); col < e; ++col) 226 for (unsigned i = 0, e = getNumRows(); i < e; ++i) 227 result[col] += rowVec[i] * at(i, col); 228 return result; 229 } 230 231 SmallVector<int64_t, 8> 232 Matrix::postMultiplyWithColumn(ArrayRef<int64_t> colVec) const { 233 assert(getNumColumns() == colVec.size() && 234 "Invalid column vector dimension!"); 235 236 SmallVector<int64_t, 8> result(getNumRows(), 0); 237 for (unsigned row = 0, e = getNumRows(); row < e; row++) 238 for (unsigned i = 0, e = getNumColumns(); i < e; i++) 239 result[row] += at(row, i) * colVec[i]; 240 return result; 241 } 242 243 void Matrix::print(raw_ostream &os) const { 244 for (unsigned row = 0; row < nRows; ++row) { 245 for (unsigned column = 0; column < nColumns; ++column) 246 os << at(row, column) << ' '; 247 os << '\n'; 248 } 249 } 250 251 void Matrix::dump() const { print(llvm::errs()); } 252 253 bool Matrix::hasConsistentState() const { 254 if (data.size() != nRows * nReservedColumns) 255 return false; 256 if (nColumns > nReservedColumns) 257 return false; 258 for (unsigned r = 0; r < nRows; ++r) 259 for (unsigned c = nColumns; c < nReservedColumns; ++c) 260 if (data[r * nReservedColumns + c] != 0) 261 return false; 262 return true; 263 } 264