Skip to content

Commit fcbe64d

Browse files
committed
[MLIR][Presburger] Merge PresburgerLocalSpace and PresburgerSpace
This patch is a cleanup patch that merges PresburgerLocalSpace and PresburgerSpace. Asserting that there are no locals is shifted to the users of PresburgerSpace themselves. The reasoning for this patch is that PresburgerLocalSpace did not contribute much and only introduced additional complexity as locals could still be present in PresburgerSpace, just not writable. This could introduce problems if a PresburgerSpace with locals was copied to PresburgerLocalSpace which expected no locals in a PresburgerSpace. Reviewed By: arjunp Differential Revision: https://reviews.llvm.org/D122353
1 parent 3c6bd17 commit fcbe64d

File tree

8 files changed

+56
-118
lines changed

8 files changed

+56
-118
lines changed

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
namespace mlir {
2525
namespace presburger {
2626

27-
/// An IntegerRelation is a PresburgerLocalSpace subject to affine constraints.
27+
/// An IntegerRelation is a PresburgerSpace subject to affine constraints.
2828
/// Affine constraints can be inequalities or equalities in the form:
2929
///
3030
/// Inequality: c_0*x_0 + c_1*x_1 + .... + c_{n-1}*x_{n-1} + c_n >= 0
@@ -42,7 +42,7 @@ namespace presburger {
4242
///
4343
/// Since IntegerRelation makes a distinction between dimensions, IdKind::Range
4444
/// and IdKind::Domain should be used to refer to dimension identifiers.
45-
class IntegerRelation : public PresburgerLocalSpace {
45+
class IntegerRelation : public PresburgerSpace {
4646
public:
4747
/// All derived classes of IntegerRelation.
4848
enum class Kind {
@@ -59,7 +59,7 @@ class IntegerRelation : public PresburgerLocalSpace {
5959
unsigned numReservedEqualities, unsigned numReservedCols,
6060
unsigned numDomain, unsigned numRange, unsigned numSymbols,
6161
unsigned numLocals)
62-
: PresburgerLocalSpace(numDomain, numRange, numSymbols, numLocals),
62+
: PresburgerSpace(numDomain, numRange, numSymbols, numLocals),
6363
equalities(0, getNumIds() + 1, numReservedEqualities, numReservedCols),
6464
inequalities(0, getNumIds() + 1, numReservedInequalities,
6565
numReservedCols) {
@@ -158,15 +158,15 @@ class IntegerRelation : public PresburgerLocalSpace {
158158
/// this addition can be rolled back using truncate.
159159
struct CountsSnapshot {
160160
public:
161-
CountsSnapshot(const PresburgerLocalSpace &space, unsigned numIneqs,
161+
CountsSnapshot(const PresburgerSpace &space, unsigned numIneqs,
162162
unsigned numEqs)
163163
: space(space), numIneqs(numIneqs), numEqs(numEqs) {}
164-
const PresburgerLocalSpace &getSpace() const { return space; };
164+
const PresburgerSpace &getSpace() const { return space; };
165165
unsigned getNumIneqs() const { return numIneqs; }
166166
unsigned getNumEqs() const { return numEqs; }
167167

168168
private:
169-
PresburgerLocalSpace space;
169+
PresburgerSpace space;
170170
unsigned numIneqs, numEqs;
171171
};
172172
CountsSnapshot getCounts() const;
@@ -540,7 +540,7 @@ class IntegerRelation : public PresburgerLocalSpace {
540540
Matrix inequalities;
541541
};
542542

543-
/// An IntegerPolyhedron is a PresburgerLocalSpace subject to affine
543+
/// An IntegerPolyhedron is a PresburgerSpace subject to affine
544544
/// constraints. Affine constraints can be inequalities or equalities in the
545545
/// form:
546546
///

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

+4-11
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class MultiAffineFunction : protected IntegerPolyhedron {
5252
using IntegerPolyhedron::getNumIds;
5353
using IntegerPolyhedron::getNumLocalIds;
5454
using IntegerPolyhedron::getNumSymbolIds;
55+
using PresburgerSpace::isSpaceCompatible;
56+
using PresburgerSpace::isSpaceEqual;
5557

5658
MultiAffineFunction(const IntegerPolyhedron &domain, const Matrix &output)
5759
: IntegerPolyhedron(domain), output(output) {}
@@ -96,16 +98,6 @@ class MultiAffineFunction : protected IntegerPolyhedron {
9698
/// the intersection of the domains.
9799
bool isEqualWhereDomainsOverlap(MultiAffineFunction other) const;
98100

99-
/// Returns whether the underlying PresburgerSpace is equal to `other`.
100-
bool isSpaceEqual(const PresburgerSpace &other) const {
101-
return PresburgerSpace::isEqual(other);
102-
};
103-
104-
/// Returns whether the underlying PresburgerLocalSpace is equal to `other`.
105-
bool isSpaceEqual(const PresburgerLocalSpace &other) const {
106-
return PresburgerLocalSpace::isEqual(other);
107-
};
108-
109101
/// Return whether the `this` and `other` are equal. This is the case if
110102
/// they lie in the same space, i.e. have the same dimensions, and their
111103
/// domains are identical and their outputs are equal on their domain.
@@ -146,7 +138,8 @@ class MultiAffineFunction : protected IntegerPolyhedron {
146138
class PWMAFunction : public PresburgerSpace {
147139
public:
148140
PWMAFunction(unsigned numDims, unsigned numSymbols, unsigned numOutputs)
149-
: PresburgerSpace(/*numDomain=*/0, /*numRange=*/numDims, numSymbols),
141+
: PresburgerSpace(/*numDomain=*/0, /*numRange=*/numDims, numSymbols,
142+
/*numLocals=*/0),
150143
numOutputs(numOutputs) {
151144
assert(numOutputs >= 1 && "The function must output something!");
152145
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class PresburgerRelation : public PresburgerSpace {
121121
/// dimension and symbols.
122122
PresburgerRelation(unsigned numDomain = 0, unsigned numRange = 0,
123123
unsigned numSymbols = 0)
124-
: PresburgerSpace(numDomain, numRange, numSymbols) {}
124+
: PresburgerSpace(numDomain, numRange, numSymbols, /*numLocals=*/0) {}
125125

126126
/// The list of disjuncts that this set is the union of.
127127
SmallVector<IntegerRelation, 2> integerRelations;

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

+17-41
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,23 @@ enum class IdKind { Symbol, Local, Domain, Range, SetDim = Range };
6161
/// be implemented as a space with zero domain. IdKind::SetDim should be used
6262
/// to refer to dimensions in such spaces.
6363
///
64-
/// PresburgerSpace does not allow identifiers of kind Local. See
65-
/// PresburgerLocalSpace for an extension that does allow local identifiers.
64+
/// Compatibility of two spaces implies that number of identifiers of each kind
65+
/// other than Locals are equal. Equality of two spaces implies that number of
66+
/// identifiers of each kind are equal.
6667
class PresburgerSpace {
67-
friend PresburgerLocalSpace;
68-
6968
public:
70-
PresburgerSpace(unsigned numDomain, unsigned numRange, unsigned numSymbols)
71-
: PresburgerSpace(numDomain, numRange, numSymbols, 0) {}
69+
PresburgerSpace(unsigned numDomain = 0, unsigned numRange = 0,
70+
unsigned numSymbols = 0, unsigned numLocals = 0)
71+
: numDomain(numDomain), numRange(numRange), numSymbols(numSymbols),
72+
numLocals(numLocals) {}
7273

7374
virtual ~PresburgerSpace() = default;
7475

7576
unsigned getNumDomainIds() const { return numDomain; }
7677
unsigned getNumRangeIds() const { return numRange; }
77-
unsigned getNumSymbolIds() const { return numSymbols; }
7878
unsigned getNumSetDimIds() const { return numRange; }
79+
unsigned getNumSymbolIds() const { return numSymbols; }
80+
unsigned getNumLocalIds() const { return numLocals; }
7981

8082
unsigned getNumDimIds() const { return numDomain + numRange; }
8183
unsigned getNumDimAndSymbolIds() const {
@@ -113,9 +115,14 @@ class PresburgerSpace {
113115
/// some ids at the end. `num` must be less than the current number.
114116
void truncateIdKind(IdKind kind, unsigned num);
115117

116-
/// Returns true if both the spaces are equal i.e. if both spaces have the
117-
/// same number of identifiers of each kind (excluding Local Identifiers).
118-
bool isEqual(const PresburgerSpace &other) const;
118+
/// Returns true if both the spaces are compatible i.e. if both spaces have
119+
/// the same number of identifiers of each kind (excluding locals).
120+
bool isSpaceCompatible(const PresburgerSpace &other) const;
121+
122+
/// Returns true if both the spaces are equal including local identifiers i.e.
123+
/// if both spaces have the same number of identifiers of each kind (including
124+
/// locals).
125+
bool isSpaceEqual(const PresburgerSpace &other) const;
119126

120127
/// Changes the partition between dimensions and symbols. Depending on the new
121128
/// symbol count, either a chunk of dimensional identifiers immediately before
@@ -127,11 +134,6 @@ class PresburgerSpace {
127134
void dump() const;
128135

129136
private:
130-
PresburgerSpace(unsigned numDomain, unsigned numRange, unsigned numSymbols,
131-
unsigned numLocals)
132-
: numDomain(numDomain), numRange(numRange), numSymbols(numSymbols),
133-
numLocals(numLocals) {}
134-
135137
// Number of identifiers corresponding to domain identifiers.
136138
unsigned numDomain;
137139

@@ -147,32 +149,6 @@ class PresburgerSpace {
147149
unsigned numLocals;
148150
};
149151

150-
/// Extension of PresburgerSpace supporting Local identifiers.
151-
class PresburgerLocalSpace : public PresburgerSpace {
152-
public:
153-
PresburgerLocalSpace(unsigned numDomain, unsigned numRange,
154-
unsigned numSymbols, unsigned numLocals)
155-
: PresburgerSpace(numDomain, numRange, numSymbols, numLocals) {}
156-
157-
unsigned getNumLocalIds() const { return numLocals; }
158-
159-
/// Insert `num` identifiers of the specified kind at position `pos`.
160-
/// Positions are relative to the kind of identifier. Return the absolute
161-
/// column position (i.e., not relative to the kind of identifier) of the
162-
/// first added identifier.
163-
unsigned insertId(IdKind kind, unsigned pos, unsigned num = 1) override;
164-
165-
/// Removes identifiers in the column range [idStart, idLimit).
166-
void removeIdRange(IdKind kind, unsigned idStart, unsigned idLimit) override;
167-
168-
/// Returns true if both the spaces are equal i.e. if both spaces have the
169-
/// same number of identifiers of each kind.
170-
bool isEqual(const PresburgerLocalSpace &other) const;
171-
172-
void print(llvm::raw_ostream &os) const;
173-
void dump() const;
174-
};
175-
176152
} // namespace presburger
177153
} // namespace mlir
178154

mlir/lib/Analysis/Presburger/IntegerRelation.cpp

+9-10
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ std::unique_ptr<IntegerPolyhedron> IntegerPolyhedron::clone() const {
3838
}
3939

4040
void IntegerRelation::append(const IntegerRelation &other) {
41-
assert(PresburgerLocalSpace::isEqual(other) && "Spaces must be equal.");
41+
assert(isSpaceEqual(other) && "Spaces must be equal.");
4242

4343
inequalities.reserveRows(inequalities.getNumRows() +
4444
other.getNumInequalities());
@@ -60,12 +60,12 @@ IntegerRelation IntegerRelation::intersect(IntegerRelation other) const {
6060
}
6161

6262
bool IntegerRelation::isEqual(const IntegerRelation &other) const {
63-
assert(PresburgerLocalSpace::isEqual(other) && "Spaces must be equal.");
63+
assert(isSpaceEqual(other) && "Spaces must be equal.");
6464
return PresburgerRelation(*this).isEqual(PresburgerRelation(other));
6565
}
6666

6767
bool IntegerRelation::isSubsetOf(const IntegerRelation &other) const {
68-
assert(PresburgerLocalSpace::isEqual(other) && "Spaces must be equal.");
68+
assert(isSpaceEqual(other) && "Spaces must be equal.");
6969
return PresburgerRelation(*this).isSubsetOf(PresburgerRelation(other));
7070
}
7171

@@ -128,8 +128,7 @@ void removeConstraintsInvolvingIdRange(IntegerRelation &poly, unsigned begin,
128128
}
129129

130130
IntegerRelation::CountsSnapshot IntegerRelation::getCounts() const {
131-
return {PresburgerLocalSpace(*this), getNumInequalities(),
132-
getNumEqualities()};
131+
return {PresburgerSpace(*this), getNumInequalities(), getNumEqualities()};
133132
}
134133

135134
void IntegerRelation::truncateIdKind(IdKind kind,
@@ -149,7 +148,7 @@ void IntegerRelation::truncate(const CountsSnapshot &counts) {
149148
unsigned IntegerRelation::insertId(IdKind kind, unsigned pos, unsigned num) {
150149
assert(pos <= getNumIdKind(kind));
151150

152-
unsigned insertPos = PresburgerLocalSpace::insertId(kind, pos, num);
151+
unsigned insertPos = PresburgerSpace::insertId(kind, pos, num);
153152
inequalities.insertColumns(insertPos, num);
154153
equalities.insertColumns(insertPos, num);
155154
return insertPos;
@@ -193,7 +192,7 @@ void IntegerRelation::removeIdRange(IdKind kind, unsigned idStart,
193192
inequalities.removeColumns(offset + idStart, idLimit - idStart);
194193

195194
// Remove eliminated identifiers from the space.
196-
PresburgerLocalSpace::removeIdRange(kind, idStart, idLimit);
195+
PresburgerSpace::removeIdRange(kind, idStart, idLimit);
197196
}
198197

199198
void IntegerRelation::removeIdRange(unsigned idStart, unsigned idLimit) {
@@ -1068,7 +1067,7 @@ void IntegerRelation::eliminateRedundantLocalId(unsigned posA, unsigned posB) {
10681067
/// division representation for some local id cannot be obtained, and thus these
10691068
/// local ids are not considered for detecting duplicates.
10701069
void IntegerRelation::mergeLocalIds(IntegerRelation &other) {
1071-
assert(PresburgerSpace::isEqual(other) && "Spaces should match.");
1070+
assert(isSpaceCompatible(other) && "Spaces should be compatible.");
10721071

10731072
IntegerRelation &relA = *this;
10741073
IntegerRelation &relB = other;
@@ -1892,7 +1891,7 @@ static void getCommonConstraints(const IntegerRelation &a,
18921891
// lower bounds and the max of the upper bounds along each of the dimensions.
18931892
LogicalResult
18941893
IntegerRelation::unionBoundingBox(const IntegerRelation &otherCst) {
1895-
assert(PresburgerLocalSpace::isEqual(otherCst) && "Spaces should match.");
1894+
assert(isSpaceEqual(otherCst) && "Spaces should match.");
18961895
assert(getNumLocalIds() == 0 && "local ids not supported yet here");
18971896

18981897
// Get the constraints common to both systems; these will be added as is to
@@ -2056,7 +2055,7 @@ void IntegerRelation::removeIndependentConstraints(unsigned pos, unsigned num) {
20562055
}
20572056

20582057
void IntegerRelation::printSpace(raw_ostream &os) const {
2059-
PresburgerLocalSpace::print(os);
2058+
PresburgerSpace::print(os);
20602059
os << getNumConstraints() << " constraints\n";
20612060
}
20622061

mlir/lib/Analysis/Presburger/PWMAFunction.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ void MultiAffineFunction::print(raw_ostream &os) const {
8484
void MultiAffineFunction::dump() const { print(llvm::errs()); }
8585

8686
bool MultiAffineFunction::isEqual(const MultiAffineFunction &other) const {
87-
return PresburgerSpace::isEqual(other) &&
88-
getDomain().isEqual(other.getDomain()) &&
87+
return isSpaceCompatible(other) && getDomain().isEqual(other.getDomain()) &&
8988
isEqualWhereDomainsOverlap(other);
9089
}
9190

@@ -117,7 +116,7 @@ void MultiAffineFunction::eliminateRedundantLocalId(unsigned posA,
117116

118117
bool MultiAffineFunction::isEqualWhereDomainsOverlap(
119118
MultiAffineFunction other) const {
120-
if (!PresburgerSpace::isEqual(other))
119+
if (!isSpaceCompatible(other))
121120
return false;
122121

123122
// `commonFunc` has the same output as `this`.
@@ -150,7 +149,7 @@ bool MultiAffineFunction::isEqualWhereDomainsOverlap(
150149
/// Two PWMAFunctions are equal if they have the same dimensionalities,
151150
/// the same domain, and take the same value at every point in the domain.
152151
bool PWMAFunction::isEqual(const PWMAFunction &other) const {
153-
if (!PresburgerSpace::isEqual(other))
152+
if (!isSpaceCompatible(other))
154153
return false;
155154

156155
if (!this->getDomain().isEqual(other.getDomain()))
@@ -168,7 +167,7 @@ bool PWMAFunction::isEqual(const PWMAFunction &other) const {
168167
}
169168

170169
void PWMAFunction::addPiece(const MultiAffineFunction &piece) {
171-
assert(piece.isSpaceEqual(*this) &&
170+
assert(piece.isSpaceCompatible(*this) &&
172171
"Piece to be added is not compatible with this PWMAFunction!");
173172
assert(piece.isConsistent() && "Piece is internally inconsistent!");
174173
assert(this->getDomain()

mlir/lib/Analysis/Presburger/PresburgerRelation.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const IntegerRelation &PresburgerRelation::getDisjunct(unsigned index) const {
3737
/// Mutate this set, turning it into the union of this set and the given
3838
/// IntegerRelation.
3939
void PresburgerRelation::unionInPlace(const IntegerRelation &disjunct) {
40-
assert(PresburgerSpace::isEqual(disjunct) && "Spaces should match");
40+
assert(isSpaceCompatible(disjunct) && "Spaces should match");
4141
integerRelations.push_back(disjunct);
4242
}
4343

@@ -46,15 +46,15 @@ void PresburgerRelation::unionInPlace(const IntegerRelation &disjunct) {
4646
/// This is accomplished by simply adding all the disjuncts of the given set
4747
/// to this set.
4848
void PresburgerRelation::unionInPlace(const PresburgerRelation &set) {
49-
assert(PresburgerSpace::isEqual(set) && "Spaces should match");
49+
assert(isSpaceCompatible(set) && "Spaces should match");
5050
for (const IntegerRelation &disjunct : set.integerRelations)
5151
unionInPlace(disjunct);
5252
}
5353

5454
/// Return the union of this set and the given set.
5555
PresburgerRelation
5656
PresburgerRelation::unionSet(const PresburgerRelation &set) const {
57-
assert(PresburgerSpace::isEqual(set) && "Spaces should match");
57+
assert(isSpaceCompatible(set) && "Spaces should match");
5858
PresburgerRelation result = *this;
5959
result.unionInPlace(set);
6060
return result;
@@ -91,7 +91,7 @@ PresburgerRelation PresburgerRelation::getEmpty(unsigned numDomain,
9191
// variables of both.
9292
PresburgerRelation
9393
PresburgerRelation::intersect(const PresburgerRelation &set) const {
94-
assert(PresburgerSpace::isEqual(set) && "Spaces should match");
94+
assert(isSpaceCompatible(set) && "Spaces should match");
9595

9696
PresburgerRelation result(getNumDomainIds(), getNumRangeIds(),
9797
getNumSymbolIds());
@@ -281,7 +281,7 @@ static void subtractRecursively(IntegerRelation &b, Simplex &simplex,
281281
/// returning from that function.
282282
static PresburgerRelation getSetDifference(IntegerRelation disjunct,
283283
const PresburgerRelation &set) {
284-
assert(disjunct.PresburgerSpace::isEqual(set) && "Spaces should match");
284+
assert(disjunct.isSpaceCompatible(set) && "Spaces should match");
285285
if (disjunct.isEmptyByGCDTest())
286286
return PresburgerRelation::getEmpty(disjunct.getNumDomainIds(),
287287
disjunct.getNumRangeIds(),
@@ -307,7 +307,7 @@ PresburgerRelation PresburgerRelation::complement() const {
307307
/// return `this \ set`.
308308
PresburgerRelation
309309
PresburgerRelation::subtract(const PresburgerRelation &set) const {
310-
assert(PresburgerSpace::isEqual(set) && "Spaces should match");
310+
assert(isSpaceCompatible(set) && "Spaces should match");
311311
PresburgerRelation result(getNumDomainIds(), getNumRangeIds(),
312312
getNumSymbolIds());
313313
// We compute (U_i t_i) \ (U_i set_i) as U_i (t_i \ V_i set_i).
@@ -325,7 +325,7 @@ bool PresburgerRelation::isSubsetOf(const PresburgerRelation &set) const {
325325

326326
/// Two sets are equal iff they are subsets of each other.
327327
bool PresburgerRelation::isEqual(const PresburgerRelation &set) const {
328-
assert(PresburgerSpace::isEqual(set) && "Spaces should match");
328+
assert(isSpaceCompatible(set) && "Spaces should match");
329329
return this->isSubsetOf(set) && set.isSubsetOf(*this);
330330
}
331331

0 commit comments

Comments
 (0)