Skip to content

Commit bb3aefd

Browse files
committed
[MLIR] NFC. Improve API signature + clang-tidy warning in IntegerRelation
1 parent ecc7e6c commit bb3aefd

File tree

3 files changed

+39
-46
lines changed

3 files changed

+39
-46
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: 29 additions & 36 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,29 @@ 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)) {
1087+
std::optional<unsigned> pivotRow =
1088+
findConstraintWithNonZeroAt(pivotCol, /*isEq=*/true);
1089+
if (!pivotRow) {
10931090
// 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;
1091+
// If inequalities are also non-zero in 'pivotCol', it can be eliminated.
1092+
if ((pivotRow = findConstraintWithNonZeroAt(pivotCol, /*isEq=*/false)))
1093+
break;
11001094
}
11011095

11021096
// Eliminate variable at 'pivotCol' from each equality row.
11031097
for (unsigned i = 0, e = getNumEqualities(); i < e; ++i) {
1104-
eliminateFromConstraint(this, i, pivotRow, pivotCol, posStart,
1098+
eliminateFromConstraint(this, i, *pivotRow, pivotCol, posStart,
11051099
/*isEq=*/true);
11061100
equalities.normalizeRow(i);
11071101
}
11081102

11091103
// Eliminate variable at 'pivotCol' from each inequality row.
11101104
for (unsigned i = 0, e = getNumInequalities(); i < e; ++i) {
1111-
eliminateFromConstraint(this, i, pivotRow, pivotCol, posStart,
1105+
eliminateFromConstraint(this, i, *pivotRow, pivotCol, posStart,
11121106
/*isEq=*/false);
11131107
inequalities.normalizeRow(i);
11141108
}
1115-
removeEquality(pivotRow);
1109+
removeEquality(*pivotRow);
11161110
gcdTightenInequalities();
11171111
}
11181112
// Update position limit based on number eliminated.
@@ -1125,31 +1119,31 @@ unsigned IntegerRelation::gaussianEliminateVars(unsigned posStart,
11251119
bool IntegerRelation::gaussianEliminate() {
11261120
gcdTightenInequalities();
11271121
unsigned firstVar = 0, vars = getNumVars();
1128-
unsigned nowDone, eqs, pivotRow;
1122+
unsigned nowDone, eqs;
1123+
std::optional<unsigned> pivotRow;
11291124
for (nowDone = 0, eqs = getNumEqualities(); nowDone < eqs; ++nowDone) {
11301125
// Finds the first non-empty column.
11311126
for (; firstVar < vars; ++firstVar) {
1132-
if (!findConstraintWithNonZeroAt(firstVar, true, &pivotRow))
1133-
continue;
1134-
break;
1127+
if ((pivotRow = findConstraintWithNonZeroAt(firstVar, /*isEq=*/true)))
1128+
break;
11351129
}
11361130
// The matrix has been normalized to row echelon form.
11371131
if (firstVar >= vars)
11381132
break;
11391133

11401134
// 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;
1135+
if (*pivotRow > nowDone) {
1136+
equalities.swapRows(*pivotRow, nowDone);
1137+
*pivotRow = nowDone;
11441138
}
11451139

11461140
// Normalize all lower equations and all inequalities.
11471141
for (unsigned i = nowDone + 1; i < eqs; ++i) {
1148-
eliminateFromConstraint(this, i, pivotRow, firstVar, 0, true);
1142+
eliminateFromConstraint(this, i, *pivotRow, firstVar, 0, true);
11491143
equalities.normalizeRow(i);
11501144
}
11511145
for (unsigned i = 0, ineqs = getNumInequalities(); i < ineqs; ++i) {
1152-
eliminateFromConstraint(this, i, pivotRow, firstVar, 0, false);
1146+
eliminateFromConstraint(this, i, *pivotRow, firstVar, 0, false);
11531147
inequalities.normalizeRow(i);
11541148
}
11551149
gcdTightenInequalities();
@@ -2290,9 +2284,8 @@ IntegerRelation::unionBoundingBox(const IntegerRelation &otherCst) {
22902284
}
22912285

22922286
bool IntegerRelation::isColZero(unsigned pos) const {
2293-
unsigned rowPos;
2294-
return !findConstraintWithNonZeroAt(pos, /*isEq=*/false, &rowPos) &&
2295-
!findConstraintWithNonZeroAt(pos, /*isEq=*/true, &rowPos);
2287+
return !findConstraintWithNonZeroAt(pos, /*isEq=*/false) &&
2288+
!findConstraintWithNonZeroAt(pos, /*isEq=*/true);
22962289
}
22972290

22982291
/// Find positions of inequalities and equalities that do not have a coefficient
@@ -2600,16 +2593,16 @@ void IntegerRelation::print(raw_ostream &os) const {
26002593
for (unsigned j = 0, f = getNumCols(); j < f; ++j)
26012594
updatePrintMetrics<DynamicAPInt>(atIneq(i, j), ptm);
26022595
// Print using PrintMetrics.
2603-
unsigned MIN_SPACING = 1;
2596+
constexpr unsigned kMinSpacing = 1;
26042597
for (unsigned i = 0, e = getNumEqualities(); i < e; ++i) {
26052598
for (unsigned j = 0, f = getNumCols(); j < f; ++j) {
2606-
printWithPrintMetrics<DynamicAPInt>(os, atEq(i, j), MIN_SPACING, ptm);
2599+
printWithPrintMetrics<DynamicAPInt>(os, atEq(i, j), kMinSpacing, ptm);
26072600
}
26082601
os << " = 0\n";
26092602
}
26102603
for (unsigned i = 0, e = getNumInequalities(); i < e; ++i) {
26112604
for (unsigned j = 0, f = getNumCols(); j < f; ++j) {
2612-
printWithPrintMetrics<DynamicAPInt>(os, atIneq(i, j), MIN_SPACING, ptm);
2605+
printWithPrintMetrics<DynamicAPInt>(os, atIneq(i, j), kMinSpacing, ptm);
26132606
}
26142607
os << " >= 0\n";
26152608
}

0 commit comments

Comments
 (0)