Skip to content

Commit 20aedb1

Browse files
committed
[MLIR][Presburger] Remove inheritance from PresburgerSpace in IntegerRelation, PresburgerRelation and PWMAFunction
This patch removes inheritence from PresburgerSpace in IntegerRelation and instead makes it a member of these classes. This is required for three reasons: - It prevents implicit casting to PresburgerSpace. - Not all functions of PresburgerSpace need to be exposed by the deriving classes. - IntegerRelation and IntegerPolyhedron are defined in a PresburgerSpace. It makes more sense for the space to be a member instead of them inheriting from a space. Reviewed By: arjunp, ftynse Differential Revision: https://reviews.llvm.org/D123585
1 parent e08c435 commit 20aedb1

File tree

9 files changed

+137
-77
lines changed

9 files changed

+137
-77
lines changed

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

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
namespace mlir {
2525
namespace presburger {
2626

27-
/// An IntegerRelation is a PresburgerSpace subject to affine constraints.
28-
/// Affine constraints can be inequalities or equalities in the form:
27+
/// An IntegerRelation represents the set of points from a PresburgerSpace that
28+
/// satisfy a list of affine constraints. Affine constraints can be inequalities
29+
/// or equalities in the form:
2930
///
3031
/// Inequality: c_0*x_0 + c_1*x_1 + .... + c_{n-1}*x_{n-1} + c_n >= 0
3132
/// Equality : c_0*x_0 + c_1*x_1 + .... + c_{n-1}*x_{n-1} + c_n == 0
@@ -42,7 +43,7 @@ namespace presburger {
4243
///
4344
/// Since IntegerRelation makes a distinction between dimensions, IdKind::Range
4445
/// and IdKind::Domain should be used to refer to dimension identifiers.
45-
class IntegerRelation : public PresburgerSpace {
46+
class IntegerRelation {
4647
public:
4748
/// All derived classes of IntegerRelation.
4849
enum class Kind {
@@ -58,11 +59,11 @@ class IntegerRelation : public PresburgerSpace {
5859
IntegerRelation(unsigned numReservedInequalities,
5960
unsigned numReservedEqualities, unsigned numReservedCols,
6061
const PresburgerSpace &space)
61-
: PresburgerSpace(space),
62-
equalities(0, getNumIds() + 1, numReservedEqualities, numReservedCols),
63-
inequalities(0, getNumIds() + 1, numReservedInequalities,
62+
: space(space), equalities(0, space.getNumIds() + 1,
63+
numReservedEqualities, numReservedCols),
64+
inequalities(0, space.getNumIds() + 1, numReservedInequalities,
6465
numReservedCols) {
65-
assert(numReservedCols >= getNumIds() + 1);
66+
assert(numReservedCols >= space.getNumIds() + 1);
6667
}
6768

6869
/// Constructs a relation with the specified number of dimensions and symbols.
@@ -71,6 +72,8 @@ class IntegerRelation : public PresburgerSpace {
7172
/*numReservedEqualities=*/0,
7273
/*numReservedCols=*/space.getNumIds() + 1, space) {}
7374

75+
virtual ~IntegerRelation() = default;
76+
7477
/// Return a system with no constraints, i.e., one which is satisfied by all
7578
/// points.
7679
static IntegerRelation getUniverse(const PresburgerSpace &space) {
@@ -85,6 +88,16 @@ class IntegerRelation : public PresburgerSpace {
8588
// Clones this object.
8689
std::unique_ptr<IntegerRelation> clone() const;
8790

91+
/// Returns a reference to the underlying space.
92+
const PresburgerSpace &getSpace() const { return space; }
93+
94+
/// Returns a copy of the space without locals.
95+
PresburgerSpace getSpaceWithoutLocals() const {
96+
return PresburgerSpace::getRelationSpace(space.getNumDomainIds(),
97+
space.getNumRangeIds(),
98+
space.getNumSymbolIds());
99+
}
100+
88101
/// Appends constraints from `other` into `this`. This is equivalent to an
89102
/// intersection with no simplification of any sort attempted.
90103
void append(const IntegerRelation &other);
@@ -117,8 +130,19 @@ class IntegerRelation : public PresburgerSpace {
117130
return getNumInequalities() + getNumEqualities();
118131
}
119132

133+
unsigned getNumDomainIds() const { return space.getNumDomainIds(); }
134+
unsigned getNumRangeIds() const { return space.getNumRangeIds(); }
135+
unsigned getNumSymbolIds() const { return space.getNumSymbolIds(); }
136+
unsigned getNumLocalIds() const { return space.getNumLocalIds(); }
137+
138+
unsigned getNumDimIds() const { return space.getNumDimIds(); }
139+
unsigned getNumDimAndSymbolIds() const {
140+
return space.getNumDimAndSymbolIds();
141+
}
142+
unsigned getNumIds() const { return space.getNumIds(); }
143+
120144
/// Returns the number of columns in the constraint system.
121-
inline unsigned getNumCols() const { return getNumIds() + 1; }
145+
inline unsigned getNumCols() const { return space.getNumIds() + 1; }
122146

123147
inline unsigned getNumEqualities() const { return equalities.getNumRows(); }
124148

@@ -142,6 +166,27 @@ class IntegerRelation : public PresburgerSpace {
142166
return inequalities.getRow(idx);
143167
}
144168

169+
/// Get the number of ids of the specified kind.
170+
unsigned getNumIdKind(IdKind kind) const { return space.getNumIdKind(kind); };
171+
172+
/// Return the index at which the specified kind of id starts.
173+
unsigned getIdKindOffset(IdKind kind) const {
174+
return space.getIdKindOffset(kind);
175+
};
176+
177+
/// Return the index at Which the specified kind of id ends.
178+
unsigned getIdKindEnd(IdKind kind) const { return space.getIdKindEnd(kind); };
179+
180+
/// Get the number of elements of the specified kind in the range
181+
/// [idStart, idLimit).
182+
unsigned getIdKindOverlap(IdKind kind, unsigned idStart,
183+
unsigned idLimit) const {
184+
return space.getIdKindOverlap(kind, idStart, idLimit);
185+
};
186+
187+
/// Return the IdKind of the id at the specified position.
188+
IdKind getIdKindAt(unsigned pos) const { return space.getIdKindAt(pos); };
189+
145190
/// The struct CountsSnapshot stores the count of each IdKind, and also of
146191
/// each constraint type. getCounts() returns a CountsSnapshot object
147192
/// describing the current state of the IntegerRelation. truncate() truncates
@@ -171,7 +216,7 @@ class IntegerRelation : public PresburgerSpace {
171216
/// corresponding to the added identifiers are initialized to zero. Return the
172217
/// absolute column position (i.e., not relative to the kind of identifier)
173218
/// of the first added identifier.
174-
unsigned insertId(IdKind kind, unsigned pos, unsigned num = 1) override;
219+
virtual unsigned insertId(IdKind kind, unsigned pos, unsigned num = 1);
175220

176221
/// Append `num` identifiers of the specified kind after the last identifier.
177222
/// of that kind. Return the position of the first appended column relative to
@@ -193,7 +238,7 @@ class IntegerRelation : public PresburgerSpace {
193238
/// within the specified range) from the system. The specified location is
194239
/// relative to the first identifier of the specified kind.
195240
void removeId(IdKind kind, unsigned pos);
196-
void removeIdRange(IdKind kind, unsigned idStart, unsigned idLimit) override;
241+
virtual void removeIdRange(IdKind kind, unsigned idStart, unsigned idLimit);
197242

198243
/// Removes the specified identifier from the system.
199244
void removeId(unsigned pos);
@@ -432,6 +477,14 @@ class IntegerRelation : public PresburgerSpace {
432477
/// match.
433478
void mergeLocalIds(IntegerRelation &other);
434479

480+
/// Changes the partition between dimensions and symbols. Depending on the new
481+
/// symbol count, either a chunk of dimensional identifiers immediately before
482+
/// the split become symbols, or some of the symbols immediately after the
483+
/// split become dimensions.
484+
void setDimSymbolSeparation(unsigned newSymbolCount) {
485+
space.setDimSymbolSeparation(newSymbolCount);
486+
}
487+
435488
void print(raw_ostream &os) const;
436489
void dump() const;
437490

@@ -512,7 +565,10 @@ class IntegerRelation : public PresburgerSpace {
512565
/// arrays as needed.
513566
void removeIdRange(unsigned idStart, unsigned idLimit);
514567

515-
using PresburgerSpace::truncateIdKind;
568+
/// Truncate the ids of the specified kind to the specified number by dropping
569+
/// some ids at the end. `num` must be less than the current number.
570+
void truncateIdKind(IdKind kind, unsigned num);
571+
516572
/// Truncate the ids to the number in the space of the specified
517573
/// CountsSnapshot.
518574
void truncateIdKind(IdKind kind, const CountsSnapshot &counts);
@@ -529,6 +585,8 @@ class IntegerRelation : public PresburgerSpace {
529585
// constraints. This is conservatively set low and can be raised if needed.
530586
constexpr static unsigned kExplosionFactor = 32;
531587

588+
PresburgerSpace space;
589+
532590
/// Coefficients of affine equalities (in == 0 form).
533591
Matrix equalities;
534592

@@ -537,9 +595,10 @@ class IntegerRelation : public PresburgerSpace {
537595
};
538596

539597
struct SymbolicLexMin;
540-
/// An IntegerPolyhedron is a PresburgerSpace subject to affine
541-
/// constraints. Affine constraints can be inequalities or equalities in the
542-
/// form:
598+
599+
/// An IntegerPolyhedron represents the set of points from a PresburgerSpace
600+
/// that satisfy a list of affine constraints. Affine constraints can be
601+
/// inequalities or equalities in the form:
543602
///
544603
/// Inequality: c_0*x_0 + c_1*x_1 + .... + c_{n-1}*x_{n-1} + c_n >= 0
545604
/// Equality : c_0*x_0 + c_1*x_1 + .... + c_{n-1}*x_{n-1} + c_n == 0

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ class MultiAffineFunction : protected IntegerPolyhedron {
5252
using IntegerPolyhedron::getNumIds;
5353
using IntegerPolyhedron::getNumLocalIds;
5454
using IntegerPolyhedron::getNumSymbolIds;
55-
using PresburgerSpace::isSpaceCompatible;
56-
using PresburgerSpace::isSpaceEqual;
55+
using IntegerPolyhedron::getSpace;
5756

5857
MultiAffineFunction(const IntegerPolyhedron &domain, const Matrix &output)
5958
: IntegerPolyhedron(domain), output(output) {}
@@ -139,22 +138,26 @@ class MultiAffineFunction : protected IntegerPolyhedron {
139138
///
140139
/// Support is provided to compare equality of two such functions as well as
141140
/// finding the value of the function at a point.
142-
class PWMAFunction : private PresburgerSpace {
141+
class PWMAFunction {
143142
public:
144143
PWMAFunction(const PresburgerSpace &space, unsigned numOutputs)
145-
: PresburgerSpace(space), numOutputs(numOutputs) {
146-
assert(getNumDomainIds() == 0 && "Set type space should zero domain ids.");
147-
assert(getNumLocalIds() == 0 && "PWMAFunction cannot have local ids.");
144+
: space(space), numOutputs(numOutputs) {
145+
assert(space.getNumDomainIds() == 0 &&
146+
"Set type space should have zero domain ids.");
147+
assert(space.getNumLocalIds() == 0 &&
148+
"PWMAFunction cannot have local ids.");
148149
assert(numOutputs >= 1 && "The function must output something!");
149150
}
150151

152+
const PresburgerSpace &getSpace() const { return space; }
153+
151154
void addPiece(const MultiAffineFunction &piece);
152155
void addPiece(const IntegerPolyhedron &domain, const Matrix &output);
153156

154157
const MultiAffineFunction &getPiece(unsigned i) const { return pieces[i]; }
155158
unsigned getNumPieces() const { return pieces.size(); }
156159
unsigned getNumOutputs() const { return numOutputs; }
157-
unsigned getNumInputs() const { return getNumIds(); }
160+
unsigned getNumInputs() const { return space.getNumIds(); }
158161
MultiAffineFunction &getPiece(unsigned i) { return pieces[i]; }
159162

160163
/// Return the domain of this piece-wise MultiAffineFunction. This is the
@@ -179,6 +182,8 @@ class PWMAFunction : private PresburgerSpace {
179182
void dump() const;
180183

181184
private:
185+
PresburgerSpace space;
186+
182187
/// The list of pieces in this piece-wise MultiAffineFunction.
183188
SmallVector<MultiAffineFunction, 4> pieces;
184189

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class SetCoalescer;
3434
/// Note that there are no invariants guaranteed on the list of disjuncts
3535
/// other than that they are all in the same PresburgerSpace. For example, the
3636
/// relations may overlap with each other.
37-
class PresburgerRelation : public PresburgerSpace {
37+
class PresburgerRelation {
3838
public:
3939
/// Return a universe set of the specified type that contains all points.
4040
static PresburgerRelation getUniverse(const PresburgerSpace &space);
@@ -44,9 +44,17 @@ class PresburgerRelation : public PresburgerSpace {
4444

4545
explicit PresburgerRelation(const IntegerRelation &disjunct);
4646

47+
unsigned getNumDomainIds() const { return space.getNumDomainIds(); }
48+
unsigned getNumRangeIds() const { return space.getNumRangeIds(); }
49+
unsigned getNumSymbolIds() const { return space.getNumSymbolIds(); }
50+
unsigned getNumLocalIds() const { return space.getNumLocalIds(); }
51+
unsigned getNumIds() const { return space.getNumIds(); }
52+
4753
/// Return the number of disjuncts in the union.
4854
unsigned getNumDisjuncts() const;
4955

56+
const PresburgerSpace &getSpace() const { return space; }
57+
5058
/// Return a reference to the list of disjuncts.
5159
ArrayRef<IntegerRelation> getAllDisjuncts() const;
5260

@@ -116,12 +124,13 @@ class PresburgerRelation : public PresburgerSpace {
116124
protected:
117125
/// Construct an empty PresburgerRelation with the specified number of
118126
/// dimension and symbols.
119-
explicit PresburgerRelation(const PresburgerSpace &space)
120-
: PresburgerSpace(space) {
127+
explicit PresburgerRelation(const PresburgerSpace &space) : space(space) {
121128
assert(space.getNumLocalIds() == 0 &&
122129
"PresburgerRelation cannot have local ids.");
123130
}
124131

132+
PresburgerSpace space;
133+
125134
/// The list of disjuncts that this set is the union of.
126135
SmallVector<IntegerRelation, 2> disjuncts;
127136

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

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,6 @@ class PresburgerSpace {
7878
numLocals);
7979
}
8080

81-
/// Returns the space. This function is primarily intended to be used from
82-
/// derived classes.
83-
PresburgerSpace getSpace() const { return *this; }
84-
85-
/// Returns the space without locals. This function is primarily intended to
86-
/// be used from derived classes.
87-
PresburgerSpace getSpaceWithoutLocals() const {
88-
return PresburgerSpace(numDomain, numRange, numSymbols);
89-
}
90-
91-
virtual ~PresburgerSpace() = default;
92-
9381
unsigned getNumDomainIds() const { return numDomain; }
9482
unsigned getNumRangeIds() const { return numRange; }
9583
unsigned getNumSetDimIds() const { return numRange; }
@@ -125,24 +113,20 @@ class PresburgerSpace {
125113
/// Positions are relative to the kind of identifier. Return the absolute
126114
/// column position (i.e., not relative to the kind of identifier) of the
127115
/// first added identifier.
128-
virtual unsigned insertId(IdKind kind, unsigned pos, unsigned num = 1);
116+
unsigned insertId(IdKind kind, unsigned pos, unsigned num = 1);
129117

130118
/// Removes identifiers of the specified kind in the column range [idStart,
131119
/// idLimit). The range is relative to the kind of identifier.
132-
virtual void removeIdRange(IdKind kind, unsigned idStart, unsigned idLimit);
133-
134-
/// Truncate the ids of the specified kind to the specified number by dropping
135-
/// some ids at the end. `num` must be less than the current number.
136-
void truncateIdKind(IdKind kind, unsigned num);
120+
void removeIdRange(IdKind kind, unsigned idStart, unsigned idLimit);
137121

138122
/// Returns true if both the spaces are compatible i.e. if both spaces have
139123
/// the same number of identifiers of each kind (excluding locals).
140-
bool isSpaceCompatible(const PresburgerSpace &other) const;
124+
bool isCompatible(const PresburgerSpace &other) const;
141125

142126
/// Returns true if both the spaces are equal including local identifiers i.e.
143127
/// if both spaces have the same number of identifiers of each kind (including
144128
/// locals).
145-
bool isSpaceEqual(const PresburgerSpace &other) const;
129+
bool isEqual(const PresburgerSpace &other) const;
146130

147131
/// Changes the partition between dimensions and symbols. Depending on the new
148132
/// symbol count, either a chunk of dimensional identifiers immediately before

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ class FlatAffineValueConstraints : public presburger::IntegerPolyhedron {
4343
unsigned numReservedCols, unsigned numDims,
4444
unsigned numSymbols, unsigned numLocals,
4545
ArrayRef<Optional<Value>> valArgs = {})
46-
: IntegerPolyhedron(
47-
numReservedInequalities, numReservedEqualities, numReservedCols,
48-
PresburgerSpace::getSetSpace(numDims, numSymbols, numLocals)) {
46+
: IntegerPolyhedron(numReservedInequalities, numReservedEqualities,
47+
numReservedCols,
48+
presburger::PresburgerSpace::getSetSpace(
49+
numDims, numSymbols, numLocals)) {
4950
assert(numReservedCols >= getNumIds() + 1);
5051
assert(valArgs.empty() || valArgs.size() == getNumIds());
5152
values.reserve(numReservedCols);

0 commit comments

Comments
 (0)