Skip to content

Commit 0dc210e

Browse files
committed
fix out-of-bounds issues
1 parent e2aefd4 commit 0dc210e

2 files changed

Lines changed: 11 additions & 14 deletions

File tree

mlir/lib/Dialect/MQTOpt/Transforms/Helpers.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ template <typename T>
252252
int rowsLhs = lhs.size() / columnsLhs;
253253
int rowsRhs = columnsLhs;
254254
int columnsRhs = rhs.size() / rowsRhs;
255+
assert(rowsLhs * columnsLhs == lhs.size());
256+
assert(rowsRhs * columnsRhs == rhs.size());
255257

256258
std::vector<T> result(rowsLhs * columnsRhs, T{});
257259
for (int i = 0; i < rowsLhs; i++) {
@@ -364,15 +366,15 @@ auto submatrix(Container&& matrix, int rowStart, int columnStart, int numRows,
364366

365367
template <typename Lhs, typename Rhs>
366368
auto assignSubmatrix(Lhs&& lhs, Rhs&& rhs, int rowStart, int columnStart,
367-
int rowSize, int columnSize) {
369+
int numRows, int numColumns) {
368370
const int n = std::sqrt(lhs.size());
369-
assert((rowStart + rowSize) < n);
370-
assert((columnStart + columnSize) < n);
371-
assert(columnSize * rowSize == rhs.size());
371+
assert((rowStart + numRows) <= n);
372+
assert((columnStart + numColumns) <= n);
373+
assert(numColumns * numRows == rhs.size());
372374

373-
for (int i = 0; i < columnSize; ++i) {
374-
for (int j = 0; j < rowSize; ++j) {
375-
lhs[(rowStart + j) * 4 + (columnStart + i)] = rhs[j * rowSize + i];
375+
for (int i = 0; i < numColumns; ++i) {
376+
for (int j = 0; j < numRows; ++j) {
377+
lhs[(rowStart + j) * n + (columnStart + i)] = rhs[j * numColumns + i];
376378
}
377379
}
378380
}

mlir/lib/Dialect/MQTOpt/Transforms/b.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,7 @@ void householderSequenceEval(rmatrix4x4& m_vectors,
335335
int shift) {
336336
auto essentialVector = [&](const rmatrix4x4& vectors, int k) {
337337
int start = k + 1 + shift;
338-
std::vector<fp> result;
339-
result.reserve(4 - start);
340-
for (int j = start; j < 4; ++j) {
341-
result.push_back(vectors[j * 4 + k]);
342-
}
343-
return result;
338+
return helpers::submatrix(vectors, start, k, 4 - start, 1);
344339
};
345340

346341
auto applyThisOnTheLeft = [&](auto&& vectors, auto& dst) {
@@ -375,7 +370,7 @@ void householderSequenceEval(rmatrix4x4& m_vectors,
375370

376371
tmp2 = helpers::add(
377372
bottom, helpers::multiply(-tau, helpers::multiply(essential, tmp,
378-
essential.size())));
373+
1)));
379374
// insert all rows except first row
380375
llvm::copy(tmp2, matrix.begin() + n);
381376
}

0 commit comments

Comments
 (0)