Skip to content

Commit 78de27a

Browse files
authored
[MLIR] NFC. Improve API signature + clang-tidy warning in IntegerRelation (llvm#128993)
1 parent 9421e17 commit 78de27a

File tree

3 files changed

+41
-47
lines changed

3 files changed

+41
-47
lines changed

mlir/include/mlir/Analysis/Presburger/IntegerRelation.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -738,11 +738,11 @@ class IntegerRelation {
738738
/// Same as findSymbolicIntegerLexMin but produces lexmax instead of lexmin
739739
SymbolicLexOpt findSymbolicIntegerLexMax() const;
740740

741-
/// Searches for a constraint with a non-zero coefficient at `colIdx` in
742-
/// equality (isEq=true) or inequality (isEq=false) constraints.
743-
/// Returns true and sets row found in search in `rowIdx`, false otherwise.
744-
bool findConstraintWithNonZeroAt(unsigned colIdx, bool isEq,
745-
unsigned *rowIdx) const;
741+
/// Finds a constraint with a non-zero coefficient at `colIdx` in equality
742+
/// (isEq=true) or inequality (isEq=false) constraints. Returns the position
743+
/// of the row if it was found or none otherwise.
744+
std::optional<unsigned> findConstraintWithNonZeroAt(unsigned colIdx,
745+
bool isEq) const;
746746

747747
/// Return the set difference of this set and the given set, i.e.,
748748
/// return `this \ set`.

mlir/lib/Analysis/FlatLinearValueConstraints.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,8 @@ static void computeUnknownVars(const FlatLinearConstraints &cst,
635635
}
636636

637637
// Detect a variable as an expression of other variables.
638-
unsigned idx;
639-
if (!cst.findConstraintWithNonZeroAt(pos, /*isEq=*/true, &idx)) {
638+
std::optional<unsigned> idx;
639+
if (!(idx = cst.findConstraintWithNonZeroAt(pos, /*isEq=*/true))) {
640640
continue;
641641
}
642642

@@ -646,7 +646,7 @@ static void computeUnknownVars(const FlatLinearConstraints &cst,
646646
for (j = 0, e = cst.getNumVars(); j < e; ++j) {
647647
if (j == pos)
648648
continue;
649-
int64_t c = cst.atEq64(idx, j);
649+
int64_t c = cst.atEq64(*idx, j);
650650
if (c == 0)
651651
continue;
652652
// If any of the involved IDs hasn't been found yet, we can't proceed.
@@ -660,8 +660,8 @@ static void computeUnknownVars(const FlatLinearConstraints &cst,
660660
continue;
661661

662662
// Add constant term to AffineExpr.
663-
expr = expr + cst.atEq64(idx, cst.getNumVars());
664-
int64_t vPos = cst.atEq64(idx, pos);
663+
expr = expr + cst.atEq64(*idx, cst.getNumVars());
664+
int64_t vPos = cst.atEq64(*idx, pos);
665665
assert(vPos != 0 && "expected non-zero here");
666666
if (vPos > 0)
667667
expr = (-expr).floorDiv(vPos);

mlir/lib/Analysis/Presburger/IntegerRelation.cpp

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -564,22 +564,18 @@ void IntegerRelation::clearAndCopyFrom(const IntegerRelation &other) {
564564
*this = other;
565565
}
566566

567-
// Searches for a constraint with a non-zero coefficient at `colIdx` in
568-
// equality (isEq=true) or inequality (isEq=false) constraints.
569-
// Returns true and sets row found in search in `rowIdx`, false otherwise.
570-
bool IntegerRelation::findConstraintWithNonZeroAt(unsigned colIdx, bool isEq,
571-
unsigned *rowIdx) const {
567+
std::optional<unsigned>
568+
IntegerRelation::findConstraintWithNonZeroAt(unsigned colIdx, bool isEq) const {
572569
assert(colIdx < getNumCols() && "position out of bounds");
573570
auto at = [&](unsigned rowIdx) -> DynamicAPInt {
574571
return isEq ? atEq(rowIdx, colIdx) : atIneq(rowIdx, colIdx);
575572
};
576573
unsigned e = isEq ? getNumEqualities() : getNumInequalities();
577-
for (*rowIdx = 0; *rowIdx < e; ++(*rowIdx)) {
578-
if (at(*rowIdx) != 0) {
579-
return true;
580-
}
574+
for (unsigned rowIdx = 0; rowIdx < e; ++rowIdx) {
575+
if (at(rowIdx) != 0)
576+
return rowIdx;
581577
}
582-
return false;
578+
return std::nullopt;
583579
}
584580

585581
void IntegerRelation::normalizeConstraintsByGCD() {
@@ -1088,31 +1084,30 @@ unsigned IntegerRelation::gaussianEliminateVars(unsigned posStart,
10881084
unsigned pivotCol = 0;
10891085
for (pivotCol = posStart; pivotCol < posLimit; ++pivotCol) {
10901086
// Find a row which has a non-zero coefficient in column 'j'.
1091-
unsigned pivotRow;
1092-
if (!findConstraintWithNonZeroAt(pivotCol, /*isEq=*/true, &pivotRow)) {
1093-
// No pivot row in equalities with non-zero at 'pivotCol'.
1094-
if (!findConstraintWithNonZeroAt(pivotCol, /*isEq=*/false, &pivotRow)) {
1095-
// If inequalities are also non-zero in 'pivotCol', it can be
1096-
// eliminated.
1097-
continue;
1098-
}
1099-
break;
1087+
std::optional<unsigned> pivotRow =
1088+
findConstraintWithNonZeroAt(pivotCol, /*isEq=*/true);
1089+
// No pivot row in equalities with non-zero at 'pivotCol'.
1090+
if (!pivotRow) {
1091+
// If inequalities are also non-zero in 'pivotCol', it can be eliminated.
1092+
if ((pivotRow = findConstraintWithNonZeroAt(pivotCol, /*isEq=*/false)))
1093+
break;
1094+
continue;
11001095
}
11011096

11021097
// Eliminate variable at 'pivotCol' from each equality row.
11031098
for (unsigned i = 0, e = getNumEqualities(); i < e; ++i) {
1104-
eliminateFromConstraint(this, i, pivotRow, pivotCol, posStart,
1099+
eliminateFromConstraint(this, i, *pivotRow, pivotCol, posStart,
11051100
/*isEq=*/true);
11061101
equalities.normalizeRow(i);
11071102
}
11081103

11091104
// Eliminate variable at 'pivotCol' from each inequality row.
11101105
for (unsigned i = 0, e = getNumInequalities(); i < e; ++i) {
1111-
eliminateFromConstraint(this, i, pivotRow, pivotCol, posStart,
1106+
eliminateFromConstraint(this, i, *pivotRow, pivotCol, posStart,
11121107
/*isEq=*/false);
11131108
inequalities.normalizeRow(i);
11141109
}
1115-
removeEquality(pivotRow);
1110+
removeEquality(*pivotRow);
11161111
gcdTightenInequalities();
11171112
}
11181113
// Update position limit based on number eliminated.
@@ -1125,31 +1120,31 @@ unsigned IntegerRelation::gaussianEliminateVars(unsigned posStart,
11251120
bool IntegerRelation::gaussianEliminate() {
11261121
gcdTightenInequalities();
11271122
unsigned firstVar = 0, vars = getNumVars();
1128-
unsigned nowDone, eqs, pivotRow;
1123+
unsigned nowDone, eqs;
1124+
std::optional<unsigned> pivotRow;
11291125
for (nowDone = 0, eqs = getNumEqualities(); nowDone < eqs; ++nowDone) {
11301126
// Finds the first non-empty column.
11311127
for (; firstVar < vars; ++firstVar) {
1132-
if (!findConstraintWithNonZeroAt(firstVar, true, &pivotRow))
1133-
continue;
1134-
break;
1128+
if ((pivotRow = findConstraintWithNonZeroAt(firstVar, /*isEq=*/true)))
1129+
break;
11351130
}
11361131
// The matrix has been normalized to row echelon form.
11371132
if (firstVar >= vars)
11381133
break;
11391134

11401135
// The first pivot row found is below where it should currently be placed.
1141-
if (pivotRow > nowDone) {
1142-
equalities.swapRows(pivotRow, nowDone);
1143-
pivotRow = nowDone;
1136+
if (*pivotRow > nowDone) {
1137+
equalities.swapRows(*pivotRow, nowDone);
1138+
*pivotRow = nowDone;
11441139
}
11451140

11461141
// Normalize all lower equations and all inequalities.
11471142
for (unsigned i = nowDone + 1; i < eqs; ++i) {
1148-
eliminateFromConstraint(this, i, pivotRow, firstVar, 0, true);
1143+
eliminateFromConstraint(this, i, *pivotRow, firstVar, 0, true);
11491144
equalities.normalizeRow(i);
11501145
}
11511146
for (unsigned i = 0, ineqs = getNumInequalities(); i < ineqs; ++i) {
1152-
eliminateFromConstraint(this, i, pivotRow, firstVar, 0, false);
1147+
eliminateFromConstraint(this, i, *pivotRow, firstVar, 0, false);
11531148
inequalities.normalizeRow(i);
11541149
}
11551150
gcdTightenInequalities();
@@ -2290,9 +2285,8 @@ IntegerRelation::unionBoundingBox(const IntegerRelation &otherCst) {
22902285
}
22912286

22922287
bool IntegerRelation::isColZero(unsigned pos) const {
2293-
unsigned rowPos;
2294-
return !findConstraintWithNonZeroAt(pos, /*isEq=*/false, &rowPos) &&
2295-
!findConstraintWithNonZeroAt(pos, /*isEq=*/true, &rowPos);
2288+
return !findConstraintWithNonZeroAt(pos, /*isEq=*/false) &&
2289+
!findConstraintWithNonZeroAt(pos, /*isEq=*/true);
22962290
}
22972291

22982292
/// Find positions of inequalities and equalities that do not have a coefficient
@@ -2600,16 +2594,16 @@ void IntegerRelation::print(raw_ostream &os) const {
26002594
for (unsigned j = 0, f = getNumCols(); j < f; ++j)
26012595
updatePrintMetrics<DynamicAPInt>(atIneq(i, j), ptm);
26022596
// Print using PrintMetrics.
2603-
unsigned MIN_SPACING = 1;
2597+
constexpr unsigned kMinSpacing = 1;
26042598
for (unsigned i = 0, e = getNumEqualities(); i < e; ++i) {
26052599
for (unsigned j = 0, f = getNumCols(); j < f; ++j) {
2606-
printWithPrintMetrics<DynamicAPInt>(os, atEq(i, j), MIN_SPACING, ptm);
2600+
printWithPrintMetrics<DynamicAPInt>(os, atEq(i, j), kMinSpacing, ptm);
26072601
}
26082602
os << " = 0\n";
26092603
}
26102604
for (unsigned i = 0, e = getNumInequalities(); i < e; ++i) {
26112605
for (unsigned j = 0, f = getNumCols(); j < f; ++j) {
2612-
printWithPrintMetrics<DynamicAPInt>(os, atIneq(i, j), MIN_SPACING, ptm);
2606+
printWithPrintMetrics<DynamicAPInt>(os, atIneq(i, j), kMinSpacing, ptm);
26132607
}
26142608
os << " >= 0\n";
26152609
}

0 commit comments

Comments
 (0)