Skip to content

Commit 8a67c6e

Browse files
committed
[MLIR][Presburger] simplify removeConstraintsInvolvingRange
1 parent e6c58e6 commit 8a67c6e

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

mlir/lib/Analysis/Presburger/IntegerRelation.cpp

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ IntegerRelation::findIntegerLexMin() const {
102102
return maybeLexMin;
103103
}
104104

105+
static bool rangeIsZero(ArrayRef<int64_t> range) {
106+
return llvm::all_of(range, [](int64_t x) { return x == 0; });
107+
}
108+
109+
void removeConstraintsInvolvingIdRange(IntegerRelation &poly, unsigned begin,
110+
unsigned count) {
111+
// We loop until i > 0 and index into i - 1 to avoid sign issues.
112+
//
113+
// We iterate backwards so that whether we remove constraint i - 1 or not, the
114+
// next constraint to be tested is always i - 2.
115+
for (unsigned i = poly.getNumEqualities(); i > 0; i--)
116+
if (!rangeIsZero(poly.getEquality(i - 1).slice(begin, count)))
117+
poly.removeEquality(i - 1);
118+
for (unsigned i = poly.getNumInequalities(); i > 0; i--)
119+
if (!rangeIsZero(poly.getInequality(i - 1).slice(begin, count)))
120+
poly.removeInequality(i - 1);
121+
}
105122
unsigned IntegerRelation::insertId(IdKind kind, unsigned pos, unsigned num) {
106123
assert(pos <= getNumIdKind(kind));
107124

@@ -561,33 +578,6 @@ Matrix IntegerRelation::getBoundedDirections() const {
561578
return dirs;
562579
}
563580

564-
bool eqInvolvesSuffixDims(const IntegerRelation &rel, unsigned eqIndex,
565-
unsigned numDims) {
566-
for (unsigned e = rel.getNumIds(), j = e - numDims; j < e; ++j)
567-
if (rel.atEq(eqIndex, j) != 0)
568-
return true;
569-
return false;
570-
}
571-
bool ineqInvolvesSuffixDims(const IntegerRelation &rel, unsigned ineqIndex,
572-
unsigned numDims) {
573-
for (unsigned e = rel.getNumIds(), j = e - numDims; j < e; ++j)
574-
if (rel.atIneq(ineqIndex, j) != 0)
575-
return true;
576-
return false;
577-
}
578-
579-
void removeConstraintsInvolvingSuffixDims(IntegerRelation &rel,
580-
unsigned unboundedDims) {
581-
// We iterate backwards so that whether we remove constraint i - 1 or not, the
582-
// next constraint to be tested is always i - 2.
583-
for (unsigned i = rel.getNumEqualities(); i > 0; i--)
584-
if (eqInvolvesSuffixDims(rel, i - 1, unboundedDims))
585-
rel.removeEquality(i - 1);
586-
for (unsigned i = rel.getNumInequalities(); i > 0; i--)
587-
if (ineqInvolvesSuffixDims(rel, i - 1, unboundedDims))
588-
rel.removeInequality(i - 1);
589-
}
590-
591581
bool IntegerRelation::isIntegerEmpty() const {
592582
return !findIntegerSample().hasValue();
593583
}
@@ -671,7 +661,8 @@ Optional<SmallVector<int64_t, 8>> IntegerRelation::findIntegerSample() const {
671661
IntegerRelation boundedSet(transformedSet);
672662
unsigned numBoundedDims = result.first;
673663
unsigned numUnboundedDims = getNumIds() - numBoundedDims;
674-
removeConstraintsInvolvingSuffixDims(boundedSet, numUnboundedDims);
664+
removeConstraintsInvolvingIdRange(boundedSet, numBoundedDims,
665+
numUnboundedDims);
675666
boundedSet.removeIdRange(numBoundedDims, boundedSet.getNumIds());
676667

677668
// 3) Try to obtain a sample from the bounded set.

0 commit comments

Comments
 (0)