Skip to content

Commit a5a598b

Browse files
committed
[MLIR][Presburger] Use PresburgerSpace in constructors
This patch modifies IntegerPolyhedron, IntegerRelation, PresburgerRelation, PresburgerSet, PWMAFunction, constructors to take PresburgerSpace instead of dimensions. This allows information present in PresburgerSpace to be carried better and allows for a general interface. Reviewed By: arjunp Differential Revision: https://reviews.llvm.org/D122842
1 parent a1901f5 commit a5a598b

File tree

15 files changed

+194
-167
lines changed

15 files changed

+194
-167
lines changed

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

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,24 @@ class IntegerRelation : public PresburgerSpace {
5757
/// of constraints and identifiers.
5858
IntegerRelation(unsigned numReservedInequalities,
5959
unsigned numReservedEqualities, unsigned numReservedCols,
60-
unsigned numDomain, unsigned numRange, unsigned numSymbols,
61-
unsigned numLocals)
62-
: PresburgerSpace(numDomain, numRange, numSymbols, numLocals),
60+
const PresburgerSpace &space)
61+
: PresburgerSpace(space),
6362
equalities(0, getNumIds() + 1, numReservedEqualities, numReservedCols),
6463
inequalities(0, getNumIds() + 1, numReservedInequalities,
6564
numReservedCols) {
6665
assert(numReservedCols >= getNumIds() + 1);
6766
}
6867

6968
/// Constructs a relation with the specified number of dimensions and symbols.
70-
IntegerRelation(unsigned numDomain = 0, unsigned numRange = 0,
71-
unsigned numSymbols = 0, unsigned numLocals = 0)
69+
IntegerRelation(const PresburgerSpace &space)
7270
: IntegerRelation(/*numReservedInequalities=*/0,
7371
/*numReservedEqualities=*/0,
74-
/*numReservedCols=*/numDomain + numRange + numSymbols +
75-
numLocals + 1,
76-
numDomain, numRange, numSymbols, numLocals) {}
72+
/*numReservedCols=*/space.getNumIds() + 1, space) {}
7773

7874
/// Return a system with no constraints, i.e., one which is satisfied by all
7975
/// points.
80-
static IntegerRelation getUniverse(unsigned numDomain = 0,
81-
unsigned numRange = 0,
82-
unsigned numSymbols = 0) {
83-
return IntegerRelation(numDomain, numRange, numSymbols);
76+
static IntegerRelation getUniverse(const PresburgerSpace &space) {
77+
return IntegerRelation(space);
8478
}
8579

8680
/// Return the kind of this IntegerRelation.
@@ -562,25 +556,24 @@ class IntegerPolyhedron : public IntegerRelation {
562556
/// of constraints and identifiers.
563557
IntegerPolyhedron(unsigned numReservedInequalities,
564558
unsigned numReservedEqualities, unsigned numReservedCols,
565-
unsigned numDims, unsigned numSymbols, unsigned numLocals)
559+
const PresburgerSpace &space)
566560
: IntegerRelation(numReservedInequalities, numReservedEqualities,
567-
numReservedCols, /*numDomain=*/0, /*numRange=*/numDims,
568-
numSymbols, numLocals) {}
561+
numReservedCols, space) {
562+
assert(space.getNumDomainIds() == 0 &&
563+
"Number of domain id's should be zero in Set kind space.");
564+
}
569565

570-
/// Constructs a relation with the specified number of dimensions and symbols.
571-
IntegerPolyhedron(unsigned numDims = 0, unsigned numSymbols = 0,
572-
unsigned numLocals = 0)
566+
/// Constructs a relation with the specified number of dimensions and
567+
/// symbols.
568+
IntegerPolyhedron(const PresburgerSpace &space)
573569
: IntegerPolyhedron(/*numReservedInequalities=*/0,
574570
/*numReservedEqualities=*/0,
575-
/*numReservedCols=*/numDims + numSymbols + numLocals +
576-
1,
577-
numDims, numSymbols, numLocals) {}
571+
/*numReservedCols=*/space.getNumIds() + 1, space) {}
578572

579573
/// Return a system with no constraints, i.e., one which is satisfied by all
580574
/// points.
581-
static IntegerPolyhedron getUniverse(unsigned numDims = 0,
582-
unsigned numSymbols = 0) {
583-
return IntegerPolyhedron(numDims, numSymbols);
575+
static IntegerPolyhedron getUniverse(const PresburgerSpace &space) {
576+
return IntegerPolyhedron(space);
584577
}
585578

586579
/// Return the kind of this IntegerRelation.

mlir/include/mlir/Analysis/Presburger/PWMAFunction.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ class MultiAffineFunction : protected IntegerPolyhedron {
5757

5858
MultiAffineFunction(const IntegerPolyhedron &domain, const Matrix &output)
5959
: IntegerPolyhedron(domain), output(output) {}
60-
MultiAffineFunction(const Matrix &output, unsigned numDims,
61-
unsigned numSymbols = 0, unsigned numLocals = 0)
62-
: IntegerPolyhedron(numDims, numSymbols, numLocals), output(output) {}
60+
MultiAffineFunction(const Matrix &output, const PresburgerSpace &space)
61+
: IntegerPolyhedron(space), output(output) {}
6362

6463
~MultiAffineFunction() override = default;
6564
Kind getKind() const override { return Kind::MultiAffineFunction; }
@@ -137,10 +136,10 @@ class MultiAffineFunction : protected IntegerPolyhedron {
137136
/// finding the value of the function at a point.
138137
class PWMAFunction : public PresburgerSpace {
139138
public:
140-
PWMAFunction(unsigned numDims, unsigned numSymbols, unsigned numOutputs)
141-
: PresburgerSpace(/*numDomain=*/0, /*numRange=*/numDims, numSymbols,
142-
/*numLocals=*/0),
143-
numOutputs(numOutputs) {
139+
PWMAFunction(const PresburgerSpace &space, unsigned numOutputs)
140+
: PresburgerSpace(space), numOutputs(numOutputs) {
141+
assert(getNumDomainIds() == 0 && "Set type space should zero domain ids.");
142+
assert(getNumLocalIds() == 0 && "PWMAFunction cannot have local ids.");
144143
assert(numOutputs >= 1 && "The function must output something!");
145144
}
146145

mlir/include/mlir/Analysis/Presburger/PresburgerRelation.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,10 @@ class SetCoalescer;
3737
class PresburgerRelation : public PresburgerSpace {
3838
public:
3939
/// Return a universe set of the specified type that contains all points.
40-
static PresburgerRelation getUniverse(unsigned numDomain, unsigned numRange,
41-
unsigned numSymbols);
40+
static PresburgerRelation getUniverse(const PresburgerSpace &space);
4241

4342
/// Return an empty set of the specified type that contains no points.
44-
static PresburgerRelation getEmpty(unsigned numDomain = 0,
45-
unsigned numRange = 0,
46-
unsigned numSymbols = 0);
43+
static PresburgerRelation getEmpty(const PresburgerSpace &space);
4744

4845
explicit PresburgerRelation(const IntegerRelation &disjunct);
4946

@@ -119,9 +116,10 @@ class PresburgerRelation : public PresburgerSpace {
119116
protected:
120117
/// Construct an empty PresburgerRelation with the specified number of
121118
/// dimension and symbols.
122-
PresburgerRelation(unsigned numDomain = 0, unsigned numRange = 0,
123-
unsigned numSymbols = 0)
124-
: PresburgerSpace(numDomain, numRange, numSymbols, /*numLocals=*/0) {}
119+
PresburgerRelation(const PresburgerSpace &space) : PresburgerSpace(space) {
120+
assert(space.getNumLocalIds() == 0 &&
121+
"PresburgerRelation cannot have local ids.");
122+
}
125123

126124
/// The list of disjuncts that this set is the union of.
127125
SmallVector<IntegerRelation, 2> integerRelations;
@@ -132,11 +130,10 @@ class PresburgerRelation : public PresburgerSpace {
132130
class PresburgerSet : public PresburgerRelation {
133131
public:
134132
/// Return a universe set of the specified type that contains all points.
135-
static PresburgerSet getUniverse(unsigned numDims = 0,
136-
unsigned numSymbols = 0);
133+
static PresburgerSet getUniverse(const PresburgerSpace &space);
137134

138135
/// Return an empty set of the specified type that contains no points.
139-
static PresburgerSet getEmpty(unsigned numDims = 0, unsigned numSymbols = 0);
136+
static PresburgerSet getEmpty(const PresburgerSpace &space);
140137

141138
/// Create a set from a relation.
142139
explicit PresburgerSet(const IntegerPolyhedron &disjunct);
@@ -154,8 +151,11 @@ class PresburgerSet : public PresburgerRelation {
154151
protected:
155152
/// Construct an empty PresburgerRelation with the specified number of
156153
/// dimension and symbols.
157-
PresburgerSet(unsigned numDims = 0, unsigned numSymbols = 0)
158-
: PresburgerRelation(/*numDomain=*/0, numDims, numSymbols) {}
154+
PresburgerSet(const PresburgerSpace &space) : PresburgerRelation(space) {
155+
assert(space.getNumDomainIds() == 0 && "Set type cannot have domain ids.");
156+
assert(space.getNumLocalIds() == 0 &&
157+
"PresburgerRelation cannot have local ids.");
158+
}
159159
};
160160

161161
} // namespace presburger

mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,24 @@ enum class IdKind { Symbol, Local, Domain, Range, SetDim = Range };
6464
/// identifiers of each kind are equal.
6565
class PresburgerSpace {
6666
public:
67-
PresburgerSpace(unsigned numDomain = 0, unsigned numRange = 0,
68-
unsigned numSymbols = 0, unsigned numLocals = 0)
69-
: numDomain(numDomain), numRange(numRange), numSymbols(numSymbols),
70-
numLocals(numLocals) {}
67+
static PresburgerSpace getRelationSpace(unsigned numDomain = 0,
68+
unsigned numRange = 0,
69+
unsigned numSymbols = 0,
70+
unsigned numLocals = 0) {
71+
return PresburgerSpace(numDomain, numRange, numSymbols, numLocals);
72+
}
73+
74+
static PresburgerSpace getSetSpace(unsigned numDims = 0,
75+
unsigned numSymbols = 0,
76+
unsigned numLocals = 0) {
77+
return PresburgerSpace(/*numDomain=*/0, /*numRange=*/numDims, numSymbols,
78+
numLocals);
79+
}
80+
81+
PresburgerSpace getSpace() const { return *this; }
82+
PresburgerSpace getCompatibleSpace() const {
83+
return PresburgerSpace(numDomain, numRange, numSymbols);
84+
}
7185

7286
virtual ~PresburgerSpace() = default;
7387

@@ -99,6 +113,9 @@ class PresburgerSpace {
99113
unsigned getIdKindOverlap(IdKind kind, unsigned idStart,
100114
unsigned idLimit) const;
101115

116+
/// Return the IdKind of the id at the specified position.
117+
IdKind getIdKindAt(unsigned pos) const;
118+
102119
/// Insert `num` identifiers of the specified kind at position `pos`.
103120
/// Positions are relative to the kind of identifier. Return the absolute
104121
/// column position (i.e., not relative to the kind of identifier) of the
@@ -131,6 +148,12 @@ class PresburgerSpace {
131148
void print(llvm::raw_ostream &os) const;
132149
void dump() const;
133150

151+
protected:
152+
PresburgerSpace(unsigned numDomain = 0, unsigned numRange = 0,
153+
unsigned numSymbols = 0, unsigned numLocals = 0)
154+
: numDomain(numDomain), numRange(numRange), numSymbols(numSymbols),
155+
numLocals(numLocals) {}
156+
134157
private:
135158
// Number of identifiers corresponding to domain identifiers.
136159
unsigned numDomain;

mlir/include/mlir/Dialect/Affine/Analysis/AffineStructures.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,19 @@ class FlatAffineConstraints : public presburger::IntegerPolyhedron {
6565
unsigned numReservedEqualities,
6666
unsigned numReservedCols, unsigned numDims,
6767
unsigned numSymbols, unsigned numLocals)
68-
: IntegerPolyhedron(numReservedInequalities, numReservedEqualities,
69-
numReservedCols, numDims, numSymbols, numLocals) {}
68+
: IntegerPolyhedron(
69+
numReservedInequalities, numReservedEqualities, numReservedCols,
70+
PresburgerSpace::getSetSpace(numDims, numSymbols, numLocals)) {}
7071

7172
/// Constructs a constraint system with the specified number of
7273
/// dimensions and symbols.
7374
FlatAffineConstraints(unsigned numDims = 0, unsigned numSymbols = 0,
7475
unsigned numLocals = 0)
75-
: IntegerPolyhedron(/*numReservedInequalities=*/0,
76-
/*numReservedEqualities=*/0,
77-
/*numReservedCols=*/numDims + numSymbols + numLocals +
78-
1,
79-
numDims, numSymbols, numLocals) {}
76+
: FlatAffineConstraints(/*numReservedInequalities=*/0,
77+
/*numReservedEqualities=*/0,
78+
/*numReservedCols=*/numDims + numSymbols +
79+
numLocals + 1,
80+
numDims, numSymbols, numLocals) {}
8081

8182
explicit FlatAffineConstraints(const IntegerPolyhedron &poly)
8283
: IntegerPolyhedron(poly) {}

mlir/lib/Analysis/Presburger/IntegerRelation.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,20 +1702,14 @@ void IntegerRelation::fourierMotzkinEliminate(unsigned pos, bool darkShadow,
17021702
}
17031703
}
17041704

1705-
// Set the number of dimensions, symbols, locals in the resulting system.
1706-
unsigned newNumDomain =
1707-
getNumDomainIds() - getIdKindOverlap(IdKind::Domain, pos, pos + 1);
1708-
unsigned newNumRange =
1709-
getNumRangeIds() - getIdKindOverlap(IdKind::Range, pos, pos + 1);
1710-
unsigned newNumSymbols =
1711-
getNumSymbolIds() - getIdKindOverlap(IdKind::Symbol, pos, pos + 1);
1712-
unsigned newNumLocals =
1713-
getNumLocalIds() - getIdKindOverlap(IdKind::Local, pos, pos + 1);
1705+
PresburgerSpace newSpace = getSpace();
1706+
IdKind idKindRemove = newSpace.getIdKindAt(pos);
1707+
unsigned relativePos = pos - newSpace.getIdKindOffset(idKindRemove);
1708+
newSpace.removeIdRange(idKindRemove, relativePos, relativePos + 1);
17141709

17151710
/// Create the new system which has one identifier less.
17161711
IntegerRelation newRel(lbIndices.size() * ubIndices.size() + nbIndices.size(),
1717-
getNumEqualities(), getNumCols() - 1, newNumDomain,
1718-
newNumRange, newNumSymbols, newNumLocals);
1712+
getNumEqualities(), getNumCols() - 1, newSpace);
17191713

17201714
// This will be used to check if the elimination was integer exact.
17211715
unsigned lcmProducts = 1;
@@ -1866,8 +1860,7 @@ static BoundCmpResult compareBounds(ArrayRef<int64_t> a, ArrayRef<int64_t> b) {
18661860
// Returns constraints that are common to both A & B.
18671861
static void getCommonConstraints(const IntegerRelation &a,
18681862
const IntegerRelation &b, IntegerRelation &c) {
1869-
c = IntegerRelation(a.getNumDomainIds(), a.getNumRangeIds(),
1870-
a.getNumSymbolIds(), a.getNumLocalIds());
1863+
c = IntegerRelation(a.getSpace());
18711864
// a naive O(n^2) check should be enough here given the input sizes.
18721865
for (unsigned r = 0, e = a.getNumInequalities(); r < e; ++r) {
18731866
for (unsigned s = 0, f = b.getNumInequalities(); s < f; ++s) {
@@ -1896,7 +1889,7 @@ IntegerRelation::unionBoundingBox(const IntegerRelation &otherCst) {
18961889

18971890
// Get the constraints common to both systems; these will be added as is to
18981891
// the union.
1899-
IntegerRelation commonCst;
1892+
IntegerRelation commonCst(PresburgerSpace::getRelationSpace());
19001893
getCommonConstraints(*this, otherCst, commonCst);
19011894

19021895
std::vector<SmallVector<int64_t, 8>> boundingLbs;

mlir/lib/Analysis/Presburger/LinearTransform.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ LinearTransform::makeTransformToColumnEchelon(Matrix m) {
113113
}
114114

115115
IntegerRelation LinearTransform::applyTo(const IntegerRelation &rel) const {
116-
IntegerRelation result(rel.getNumDomainIds(), rel.getNumRangeIds(),
117-
rel.getNumSymbolIds(), rel.getNumLocalIds());
116+
IntegerRelation result(rel.getSpace());
118117

119118
for (unsigned i = 0, e = rel.getNumEqualities(); i < e; ++i) {
120119
ArrayRef<int64_t> eq = rel.getEquality(i);

mlir/lib/Analysis/Presburger/PWMAFunction.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ static SmallVector<int64_t, 8> subtract(ArrayRef<int64_t> vecA,
2727
}
2828

2929
PresburgerSet PWMAFunction::getDomain() const {
30-
PresburgerSet domain =
31-
PresburgerSet::getEmpty(getNumDimIds(), getNumSymbolIds());
30+
PresburgerSet domain = PresburgerSet::getEmpty(getSpace());
3231
for (const MultiAffineFunction &piece : pieces)
3332
domain.unionInPlace(piece.getDomain());
3433
return domain;

0 commit comments

Comments
 (0)