Skip to content

Commit 770353c

Browse files
committed
[MLIR] The return type in the computeSingleVarRepr function is modified to include equality expressions.
Earlier `computeSingleVarRepr` was returning a pair of upper bound and lower bound indices of the inequality contraints that can be expressed as a floordiv of an affine function. The equality expression can also be expressed as a floordiv but contains only one index and hence the `LocalRepr` class is introduced to facilitate this. Reviewed By: Groverkss Differential Revision: https://reviews.llvm.org/D117430
1 parent 787f91b commit 770353c

File tree

6 files changed

+59
-36
lines changed

6 files changed

+59
-36
lines changed

mlir/include/mlir/Analysis/Presburger/IntegerPolyhedron.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define MLIR_ANALYSIS_PRESBURGER_INTEGERPOLYHEDRON_H
1515

1616
#include "mlir/Analysis/Presburger/Matrix.h"
17+
#include "mlir/Analysis/Presburger/Utils.h"
1718
#include "mlir/Support/LogicalResult.h"
1819

1920
namespace mlir {
@@ -267,12 +268,10 @@ class IntegerPolyhedron {
267268
/// and the denominators in `denominators`. If no explicit representation
268269
/// could be found for the `i^th` local identifier, `denominators[i]` is set
269270
/// to 0.
270-
void getLocalReprs(
271-
std::vector<SmallVector<int64_t, 8>> &dividends,
272-
SmallVector<unsigned, 4> &denominators,
273-
std::vector<llvm::Optional<std::pair<unsigned, unsigned>>> &repr) const;
274-
void getLocalReprs(
275-
std::vector<llvm::Optional<std::pair<unsigned, unsigned>>> &repr) const;
271+
void getLocalReprs(std::vector<SmallVector<int64_t, 8>> &dividends,
272+
SmallVector<unsigned, 4> &denominators,
273+
std::vector<presburger_utils::MaybeLocalRepr> &repr) const;
274+
void getLocalReprs(std::vector<presburger_utils::MaybeLocalRepr> &repr) const;
276275
void getLocalReprs(std::vector<SmallVector<int64_t, 8>> &dividends,
277276
SmallVector<unsigned, 4> &denominators) const;
278277

mlir/include/mlir/Analysis/Presburger/Utils.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ class IntegerPolyhedron;
2121

2222
namespace presburger_utils {
2323

24+
/// `ReprKind` enum is used to set the constraint type in `MaybeLocalRepr`.
25+
enum class ReprKind { Inequality, Equality, None };
26+
27+
/// `MaybeLocalRepr` contains the indices of the contraints that can be
28+
/// expressed as a floordiv of an affine function. If it's an `equality`
29+
/// contraint `equalityIdx` is set, in case of `inequality` the `lowerBoundIdx`
30+
/// and `upperBoundIdx` is set. By default the kind attribute is set to None.
31+
struct MaybeLocalRepr {
32+
ReprKind kind = ReprKind::None;
33+
union {
34+
unsigned equalityIdx;
35+
struct {
36+
unsigned lowerBoundIdx, upperBoundIdx;
37+
} inEqualityPair;
38+
} repr;
39+
};
40+
2441
/// Check if the pos^th identifier can be expressed as a floordiv of an affine
2542
/// function of other identifiers (where the divisor is a positive constant).
2643
/// `foundRepr` contains a boolean for each identifier indicating if the
@@ -29,10 +46,10 @@ namespace presburger_utils {
2946
/// can be computed. If the representation could be computed, `dividend` and
3047
/// `denominator` are set. If the representation could not be computed,
3148
/// `llvm::None` is returned.
32-
Optional<std::pair<unsigned, unsigned>>
33-
computeSingleVarRepr(const IntegerPolyhedron &cst, ArrayRef<bool> foundRepr,
34-
unsigned pos, SmallVector<int64_t, 8> &dividend,
35-
unsigned &divisor);
49+
MaybeLocalRepr computeSingleVarRepr(const IntegerPolyhedron &cst,
50+
ArrayRef<bool> foundRepr, unsigned pos,
51+
SmallVector<int64_t, 8> &dividend,
52+
unsigned &divisor);
3653

3754
} // namespace presburger_utils
3855
} // namespace mlir

mlir/lib/Analysis/Presburger/IntegerPolyhedron.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define DEBUG_TYPE "presburger"
2222

2323
using namespace mlir;
24+
using namespace presburger_utils;
2425
using llvm::SmallDenseMap;
2526
using llvm::SmallDenseSet;
2627

@@ -799,8 +800,7 @@ bool IntegerPolyhedron::containsPoint(ArrayRef<int64_t> point) const {
799800
return true;
800801
}
801802

802-
void IntegerPolyhedron::getLocalReprs(
803-
std::vector<llvm::Optional<std::pair<unsigned, unsigned>>> &repr) const {
803+
void IntegerPolyhedron::getLocalReprs(std::vector<MaybeLocalRepr> &repr) const {
804804
std::vector<SmallVector<int64_t, 8>> dividends(getNumLocalIds());
805805
SmallVector<unsigned, 4> denominators(getNumLocalIds());
806806
getLocalReprs(dividends, denominators, repr);
@@ -809,15 +809,14 @@ void IntegerPolyhedron::getLocalReprs(
809809
void IntegerPolyhedron::getLocalReprs(
810810
std::vector<SmallVector<int64_t, 8>> &dividends,
811811
SmallVector<unsigned, 4> &denominators) const {
812-
std::vector<llvm::Optional<std::pair<unsigned, unsigned>>> repr(
813-
getNumLocalIds());
812+
std::vector<MaybeLocalRepr> repr(getNumLocalIds());
814813
getLocalReprs(dividends, denominators, repr);
815814
}
816815

817816
void IntegerPolyhedron::getLocalReprs(
818817
std::vector<SmallVector<int64_t, 8>> &dividends,
819818
SmallVector<unsigned, 4> &denominators,
820-
std::vector<llvm::Optional<std::pair<unsigned, unsigned>>> &repr) const {
819+
std::vector<MaybeLocalRepr> &repr) const {
821820

822821
repr.resize(getNumLocalIds());
823822
dividends.resize(getNumLocalIds());
@@ -835,11 +834,13 @@ void IntegerPolyhedron::getLocalReprs(
835834
changed = false;
836835
for (unsigned i = 0, e = getNumLocalIds(); i < e; ++i) {
837836
if (!foundRepr[i + divOffset]) {
838-
if (auto res = presburger_utils::computeSingleVarRepr(
839-
*this, foundRepr, divOffset + i, dividends[i],
840-
denominators[i])) {
837+
auto res = computeSingleVarRepr(*this, foundRepr, divOffset + i,
838+
dividends[i], denominators[i]);
839+
if (res.kind == ReprKind::Inequality) {
841840
foundRepr[i + divOffset] = true;
842-
repr[i] = res;
841+
repr[i].kind = ReprKind::Inequality;
842+
repr[i].repr.inEqualityPair = {res.repr.inEqualityPair.lowerBoundIdx,
843+
res.repr.inEqualityPair.upperBoundIdx};
843844
changed = true;
844845
}
845846
}
@@ -849,7 +850,7 @@ void IntegerPolyhedron::getLocalReprs(
849850
// Set 0 denominator for identifiers for which no division representation
850851
// could be found.
851852
for (unsigned i = 0, e = repr.size(); i < e; ++i)
852-
if (!repr[i].hasValue())
853+
if (repr[i].kind == ReprKind::None)
853854
denominators[i] = 0;
854855
}
855856

mlir/lib/Analysis/Presburger/PresburgerSet.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
#include "mlir/Analysis/Presburger/PresburgerSet.h"
1010
#include "mlir/Analysis/Presburger/Simplex.h"
11+
#include "mlir/Analysis/Presburger/Utils.h"
1112
#include "llvm/ADT/STLExtras.h"
1213
#include "llvm/ADT/SmallBitVector.h"
1314

1415
using namespace mlir;
16+
using namespace presburger_utils;
1517

1618
PresburgerSet::PresburgerSet(const IntegerPolyhedron &poly)
1719
: nDim(poly.getNumDimIds()), nSym(poly.getNumSymbolIds()) {
@@ -209,8 +211,7 @@ static void subtractRecursively(IntegerPolyhedron &b, Simplex &simplex,
209211

210212
// Find out which inequalities of sI correspond to division inequalities for
211213
// the local variables of sI.
212-
std::vector<llvm::Optional<std::pair<unsigned, unsigned>>> repr(
213-
sI.getNumLocalIds());
214+
std::vector<MaybeLocalRepr> repr(sI.getNumLocalIds());
214215
sI.getLocalReprs(repr);
215216

216217
// Add sI's locals to b, after b's locals. Also add b's locals to sI, before
@@ -220,18 +221,20 @@ static void subtractRecursively(IntegerPolyhedron &b, Simplex &simplex,
220221
// Mark which inequalities of sI are division inequalities and add all such
221222
// inequalities to b.
222223
llvm::SmallBitVector isDivInequality(sI.getNumInequalities());
223-
for (Optional<std::pair<unsigned, unsigned>> &maybePair : repr) {
224-
assert(maybePair &&
224+
for (MaybeLocalRepr &maybeInequality : repr) {
225+
assert(maybeInequality.kind == ReprKind::Inequality &&
225226
"Subtraction is not supported when a representation of the local "
226227
"variables of the subtrahend cannot be found!");
228+
auto lb = maybeInequality.repr.inEqualityPair.lowerBoundIdx;
229+
auto ub = maybeInequality.repr.inEqualityPair.upperBoundIdx;
227230

228-
b.addInequality(sI.getInequality(maybePair->first));
229-
b.addInequality(sI.getInequality(maybePair->second));
231+
b.addInequality(sI.getInequality(lb));
232+
b.addInequality(sI.getInequality(ub));
230233

231-
assert(maybePair->first != maybePair->second &&
234+
assert(lb != ub &&
232235
"Upper and lower bounds must be different inequalities!");
233-
isDivInequality[maybePair->first] = true;
234-
isDivInequality[maybePair->second] = true;
236+
isDivInequality[lb] = true;
237+
isDivInequality[ub] = true;
235238
}
236239

237240
unsigned offset = simplex.getNumConstraints();

mlir/lib/Analysis/Presburger/Utils.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "mlir/Support/MathExtras.h"
1717

1818
using namespace mlir;
19+
using namespace presburger_utils;
1920

2021
/// Normalize a division's `dividend` and the `divisor` by their GCD. For
2122
/// example: if the dividend and divisor are [2,0,4] and 4 respectively,
@@ -144,7 +145,7 @@ static LogicalResult getDivRepr(const IntegerPolyhedron &cst, unsigned pos,
144145
/// be computed. If the representation could be computed, `dividend` and
145146
/// `denominator` are set. If the representation could not be computed,
146147
/// `llvm::None` is returned.
147-
Optional<std::pair<unsigned, unsigned>> presburger_utils::computeSingleVarRepr(
148+
MaybeLocalRepr presburger_utils::computeSingleVarRepr(
148149
const IntegerPolyhedron &cst, ArrayRef<bool> foundRepr, unsigned pos,
149150
SmallVector<int64_t, 8> &dividend, unsigned &divisor) {
150151
assert(pos < cst.getNumIds() && "invalid position");
@@ -153,6 +154,7 @@ Optional<std::pair<unsigned, unsigned>> presburger_utils::computeSingleVarRepr(
153154

154155
SmallVector<unsigned, 4> lbIndices, ubIndices;
155156
cst.getLowerAndUpperBoundIndices(pos, &lbIndices, &ubIndices);
157+
MaybeLocalRepr repr;
156158

157159
for (unsigned ubPos : ubIndices) {
158160
for (unsigned lbPos : lbIndices) {
@@ -178,9 +180,10 @@ Optional<std::pair<unsigned, unsigned>> presburger_utils::computeSingleVarRepr(
178180
if (c < f)
179181
continue;
180182

181-
return std::make_pair(ubPos, lbPos);
183+
repr.kind = ReprKind::Inequality;
184+
repr.repr.inEqualityPair = {ubPos, lbPos};
185+
return repr;
182186
}
183187
}
184-
185-
return llvm::None;
188+
return repr;
186189
}

mlir/lib/Dialect/Affine/Analysis/AffineStructures.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#define DEBUG_TYPE "affine-structures"
3232

3333
using namespace mlir;
34+
using namespace presburger_utils;
3435

3536
namespace {
3637

@@ -851,11 +852,10 @@ static bool detectAsFloorDiv(const FlatAffineConstraints &cst, unsigned pos,
851852

852853
SmallVector<int64_t, 8> dividend;
853854
unsigned divisor;
854-
auto ulPair = presburger_utils::computeSingleVarRepr(cst, foundRepr, pos,
855-
dividend, divisor);
855+
auto ulPair = computeSingleVarRepr(cst, foundRepr, pos, dividend, divisor);
856856

857857
// No upper-lower bound pair found for this var.
858-
if (!ulPair)
858+
if (ulPair.kind == ReprKind::None || ulPair.kind == ReprKind::Equality)
859859
return false;
860860

861861
// Construct the dividend expression.

0 commit comments

Comments
 (0)