Skip to content

Commit bb90135

Browse files
committed
[MLIR][Presburger] Move functionality from IntegerPolyhedron to IntegerRelation
This patch moves all functionality from IntegerPolyhedron to IntegerRelation. IntegerPolyhedron is now implemented as a relation with no domain. All existing functionality is extended to work on relations. This patch does not affect external users like FlatAffineConstraints as they can still continue to use IntegerPolyhedron abstraction. This patch is part of a series of patches to support relations in Presburger library. Reviewed By: arjunp Differential Revision: https://reviews.llvm.org/D120652
1 parent 31efecf commit bb90135

File tree

17 files changed

+347
-313
lines changed

17 files changed

+347
-313
lines changed

mlir/include/mlir/Analysis/Presburger/IntegerPolyhedron.h renamed to mlir/include/mlir/Analysis/Presburger/IntegerRelation.h

Lines changed: 113 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
//===- IntegerPolyhedron.h - MLIR IntegerPolyhedron Class -----*- C++ -*---===//
1+
//===- IntegerRelation.h - MLIR IntegerRelation Class ---------*- C++ -*---===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// A class to represent an integer polyhedron.
9+
// A class to represent a relation over integer tuples. A relation is
10+
// represented as a constraint system over a space of tuples of integer valued
11+
// variables supporting symbolic identifiers and existential quantification.
1012
//
1113
//===----------------------------------------------------------------------===//
1214

@@ -42,6 +44,15 @@ namespace presburger {
4244
/// and IdKind::Domain should be used to refer to dimension identifiers.
4345
class IntegerRelation : public PresburgerLocalSpace {
4446
public:
47+
/// All derived classes of IntegerRelation.
48+
enum class Kind {
49+
FlatAffineConstraints,
50+
FlatAffineValueConstraints,
51+
MultiAffineFunction,
52+
IntegerRelation,
53+
IntegerPolyhedron,
54+
};
55+
4556
/// Constructs a relation reserving memory for the specified number
4657
/// of constraints and identifiers.
4758
IntegerRelation(unsigned numReservedInequalities,
@@ -64,99 +75,35 @@ class IntegerRelation : public PresburgerLocalSpace {
6475
numLocals + 1,
6576
numDomain, numRange, numSymbols, numLocals) {}
6677

67-
protected:
68-
/// Constructs a set reserving memory for the specified number
69-
/// of constraints and identifiers. This constructor should not be used
70-
/// directly to create a relation and should only be used to create Sets.
71-
IntegerRelation(unsigned numReservedInequalities,
72-
unsigned numReservedEqualities, unsigned numReservedCols,
73-
unsigned numDims, unsigned numSymbols, unsigned numLocals)
74-
: PresburgerLocalSpace(numDims, numSymbols, numLocals),
75-
equalities(0, getNumIds() + 1, numReservedEqualities, numReservedCols),
76-
inequalities(0, getNumIds() + 1, numReservedInequalities,
77-
numReservedCols) {
78-
assert(numReservedCols >= getNumIds() + 1);
79-
}
80-
81-
/// Coefficients of affine equalities (in == 0 form).
82-
Matrix equalities;
83-
84-
/// Coefficients of affine inequalities (in >= 0 form).
85-
Matrix inequalities;
86-
};
87-
88-
/// An IntegerPolyhedron is a PresburgerLocalSpace subject to affine
89-
/// constraints. Affine constraints can be inequalities or equalities in the
90-
/// form:
91-
///
92-
/// Inequality: c_0*x_0 + c_1*x_1 + .... + c_{n-1}*x_{n-1} + c_n >= 0
93-
/// Equality : c_0*x_0 + c_1*x_1 + .... + c_{n-1}*x_{n-1} + c_n == 0
94-
///
95-
/// where c_0, c_1, ..., c_n are integers and n is the total number of
96-
/// identifiers in the space.
97-
///
98-
/// An IntegerPolyhedron is similar to a IntegerRelation but it does not make a
99-
/// distinction between Domain and Range identifiers. Internally,
100-
/// IntegerPolyhedron is implemented as a IntegerRelation with zero domain ids.
101-
///
102-
/// Since IntegerPolyhedron does not make a distinction between dimensions,
103-
/// IdKind::SetDim should be used to refer to dimension identifiers.
104-
class IntegerPolyhedron : public IntegerRelation {
105-
public:
106-
/// All derived classes of IntegerPolyhedron.
107-
enum class Kind {
108-
FlatAffineConstraints,
109-
FlatAffineValueConstraints,
110-
MultiAffineFunction,
111-
IntegerPolyhedron
112-
};
113-
114-
/// Constructs a constraint system reserving memory for the specified number
115-
/// of constraints and identifiers.
116-
IntegerPolyhedron(unsigned numReservedInequalities,
117-
unsigned numReservedEqualities, unsigned numReservedCols,
118-
unsigned numDims, unsigned numSymbols, unsigned numLocals)
119-
: IntegerRelation(numReservedInequalities, numReservedEqualities,
120-
numReservedCols, numDims, numSymbols, numLocals) {}
121-
122-
/// Constructs a constraint system with the specified number of
123-
/// dimensions and symbols.
124-
IntegerPolyhedron(unsigned numDims = 0, unsigned numSymbols = 0,
125-
unsigned numLocals = 0)
126-
: IntegerPolyhedron(/*numReservedInequalities=*/0,
127-
/*numReservedEqualities=*/0,
128-
/*numReservedCols=*/numDims + numSymbols + numLocals +
129-
1,
130-
numDims, numSymbols, numLocals) {}
131-
13278
/// Return a system with no constraints, i.e., one which is satisfied by all
13379
/// points.
134-
static IntegerPolyhedron getUniverse(unsigned numDims = 0,
135-
unsigned numSymbols = 0) {
136-
return IntegerPolyhedron(numDims, numSymbols);
80+
static IntegerRelation getUniverse(unsigned numDomain = 0,
81+
unsigned numRange = 0,
82+
unsigned numSymbols = 0) {
83+
return IntegerRelation(numDomain, numRange, numSymbols);
13784
}
13885

139-
/// Return the kind of this IntegerPolyhedron.
140-
virtual Kind getKind() const { return Kind::IntegerPolyhedron; }
86+
/// Return the kind of this IntegerRelation.
87+
virtual Kind getKind() const { return Kind::IntegerRelation; }
14188

142-
static bool classof(const IntegerPolyhedron *cst) { return true; }
89+
static bool classof(const IntegerRelation *cst) { return true; }
14390

14491
// Clones this object.
145-
std::unique_ptr<IntegerPolyhedron> clone() const;
92+
std::unique_ptr<IntegerRelation> clone() const;
14693

14794
/// Appends constraints from `other` into `this`. This is equivalent to an
14895
/// intersection with no simplification of any sort attempted.
149-
void append(const IntegerPolyhedron &other);
96+
void append(const IntegerRelation &other);
15097

15198
/// Return whether `this` and `other` are equal. This is integer-exact
15299
/// and somewhat expensive, since it uses the integer emptiness check
153-
/// (see IntegerPolyhedron::findIntegerSample()).
154-
bool isEqual(const IntegerPolyhedron &other) const;
100+
/// (see IntegerRelation::findIntegerSample()).
101+
bool isEqual(const IntegerRelation &other) const;
155102

156-
/// Return whether this is a subset of the given IntegerPolyhedron. This is
103+
/// Return whether this is a subset of the given IntegerRelation. This is
157104
/// integer-exact and somewhat expensive, since it uses the integer emptiness
158-
/// check (see IntegerPolyhedron::findIntegerSample()).
159-
bool isSubsetOf(const IntegerPolyhedron &other) const;
105+
/// check (see IntegerRelation::findIntegerSample()).
106+
bool isSubsetOf(const IntegerRelation &other) const;
160107

161108
/// Returns the value at the specified equality row and column.
162109
inline int64_t atEq(unsigned i, unsigned j) const { return equalities(i, j); }
@@ -237,15 +184,16 @@ class IntegerPolyhedron : public IntegerRelation {
237184
void removeInequalityRange(unsigned start, unsigned end);
238185

239186
/// Get the lexicographically minimum rational point satisfying the
240-
/// constraints. Returns an empty optional if the polyhedron is empty or if
187+
/// constraints. Returns an empty optional if the relation is empty or if
241188
/// the lexmin is unbounded. Symbols are not supported and will result in
242-
/// assert-failure.
189+
/// assert-failure. Note that Domain is minimized first, then range.
243190
MaybeOptimum<SmallVector<Fraction, 8>> findRationalLexMin() const;
244191

245192
/// Same as above, but returns lexicographically minimal integer point.
246193
/// Note: this should be used only when the lexmin is really required.
247194
/// For a generic integer sampling operation, findIntegerSample is more
248-
/// robust and should be preferred.
195+
/// robust and should be preferred. Note that Domain is minimized first, then
196+
/// range.
249197
MaybeOptimum<SmallVector<int64_t, 8>> findIntegerLexMin() const;
250198

251199
/// Swap the posA^th identifier with the posB^th identifier.
@@ -258,8 +206,8 @@ class IntegerPolyhedron : public IntegerRelation {
258206
/// values and removes them.
259207
void setAndEliminate(unsigned pos, ArrayRef<int64_t> values);
260208

261-
/// Replaces the contents of this IntegerPolyhedron with `other`.
262-
virtual void clearAndCopyFrom(const IntegerPolyhedron &other);
209+
/// Replaces the contents of this IntegerRelation with `other`.
210+
virtual void clearAndCopyFrom(const IntegerRelation &other);
263211

264212
/// Gather positions of all lower and upper bounds of the identifier at `pos`,
265213
/// and optionally any equalities on it. In addition, the bounds are to be
@@ -304,14 +252,14 @@ class IntegerPolyhedron : public IntegerRelation {
304252
Optional<SmallVector<int64_t, 8>> findIntegerSample() const;
305253

306254
/// Compute an overapproximation of the number of integer points in the
307-
/// polyhedron. Symbol ids are currently not supported. If the computed
255+
/// relation. Symbol ids are currently not supported. If the computed
308256
/// overapproximation is infinite, an empty optional is returned.
309257
Optional<uint64_t> computeVolume() const;
310258

311259
/// Returns true if the given point satisfies the constraints, or false
312260
/// otherwise.
313261
///
314-
/// Note: currently, if the polyhedron contains local ids, the values of
262+
/// Note: currently, if the relation contains local ids, the values of
315263
/// the local ids must also be provided.
316264
bool containsPoint(ArrayRef<int64_t> point) const;
317265

@@ -383,7 +331,7 @@ class IntegerPolyhedron : public IntegerRelation {
383331
/// 3) this = {0 <= d0 <= 5, 1 <= d1 <= 9}
384332
/// other = {2 <= d0 <= 6, 5 <= d1 <= 15},
385333
/// output = {0 <= d0 <= 6, 1 <= d1 <= 15}
386-
LogicalResult unionBoundingBox(const IntegerPolyhedron &other);
334+
LogicalResult unionBoundingBox(const IntegerRelation &other);
387335

388336
/// Returns the smallest known constant bound for the extent of the specified
389337
/// identifier (pos^th), i.e., the smallest known constant that is greater
@@ -452,12 +400,27 @@ class IntegerPolyhedron : public IntegerRelation {
452400
///
453401
/// The number of dimensions and symbol ids in `this` and `other` should
454402
/// match.
455-
void mergeLocalIds(IntegerPolyhedron &other);
403+
void mergeLocalIds(IntegerRelation &other);
456404

457405
void print(raw_ostream &os) const;
458406
void dump() const;
459407

460408
protected:
409+
/// Constructs a set reserving memory for the specified number
410+
/// of constraints and identifiers. This constructor should not be used
411+
/// directly to create a relation and should only be used to create Sets.
412+
/// Internally this constructs a relation with with no domain and a
413+
/// space with no distinction between domain and range identifiers.
414+
IntegerRelation(unsigned numReservedInequalities,
415+
unsigned numReservedEqualities, unsigned numReservedCols,
416+
unsigned numDims, unsigned numSymbols, unsigned numLocals)
417+
: PresburgerLocalSpace(numDims, numSymbols, numLocals),
418+
equalities(0, getNumIds() + 1, numReservedEqualities, numReservedCols),
419+
inequalities(0, getNumIds() + 1, numReservedInequalities,
420+
numReservedCols) {
421+
assert(numReservedCols >= getNumIds() + 1);
422+
}
423+
461424
/// Checks all rows of equality/inequality constraints for trivial
462425
/// contradictions (for example: 1 == 0, 0 >= 1), which may have surfaced
463426
/// after elimination. Returns true if an invalid constraint is found;
@@ -526,7 +489,7 @@ class IntegerPolyhedron : public IntegerRelation {
526489
virtual bool hasConsistentState() const;
527490

528491
/// Prints the number of constraints, dimensions, symbols and locals in the
529-
/// IntegerPolyhedron.
492+
/// IntegerRelation.
530493
virtual void printSpace(raw_ostream &os) const;
531494

532495
/// Removes identifiers in the column range [idStart, idLimit), and copies any
@@ -537,14 +500,73 @@ class IntegerPolyhedron : public IntegerRelation {
537500
/// A parameter that controls detection of an unrealistic number of
538501
/// constraints. If the number of constraints is this many times the number of
539502
/// variables, we consider such a system out of line with the intended use
540-
/// case of IntegerPolyhedron.
503+
/// case of IntegerRelation.
541504
// The rationale for 32 is that in the typical simplest of cases, an
542505
// identifier is expected to have one lower bound and one upper bound
543506
// constraint. With a level of tiling or a connection to another identifier
544507
// through a div or mod, an extra pair of bounds gets added. As a limit, we
545508
// don't expect an identifier to have more than 32 lower/upper/equality
546509
// constraints. This is conservatively set low and can be raised if needed.
547510
constexpr static unsigned kExplosionFactor = 32;
511+
512+
/// Coefficients of affine equalities (in == 0 form).
513+
Matrix equalities;
514+
515+
/// Coefficients of affine inequalities (in >= 0 form).
516+
Matrix inequalities;
517+
};
518+
519+
/// An IntegerPolyhedron is a PresburgerLocalSpace subject to affine
520+
/// constraints. Affine constraints can be inequalities or equalities in the
521+
/// form:
522+
///
523+
/// Inequality: c_0*x_0 + c_1*x_1 + .... + c_{n-1}*x_{n-1} + c_n >= 0
524+
/// Equality : c_0*x_0 + c_1*x_1 + .... + c_{n-1}*x_{n-1} + c_n == 0
525+
///
526+
/// where c_0, c_1, ..., c_n are integers and n is the total number of
527+
/// identifiers in the space.
528+
///
529+
/// An IntegerPolyhedron is similar to an IntegerRelation but it does not make a
530+
/// distinction between Domain and Range identifiers. Internally,
531+
/// IntegerPolyhedron is implemented as a IntegerRelation with zero domain ids.
532+
///
533+
/// Since IntegerPolyhedron does not make a distinction between kinds of
534+
/// dimensions, IdKind::SetDim should be used to refer to dimension identifiers.
535+
class IntegerPolyhedron : public IntegerRelation {
536+
public:
537+
/// Constructs a set reserving memory for the specified number
538+
/// of constraints and identifiers.
539+
IntegerPolyhedron(unsigned numReservedInequalities,
540+
unsigned numReservedEqualities, unsigned numReservedCols,
541+
unsigned numDims, unsigned numSymbols, unsigned numLocals)
542+
: IntegerRelation(numReservedInequalities, numReservedEqualities,
543+
numReservedCols, numDims, numSymbols, numLocals) {}
544+
545+
/// Constructs a relation with the specified number of dimensions and symbols.
546+
IntegerPolyhedron(unsigned numDims = 0, unsigned numSymbols = 0,
547+
unsigned numLocals = 0)
548+
: IntegerPolyhedron(/*numReservedInequalities=*/0,
549+
/*numReservedEqualities=*/0,
550+
/*numReservedCols=*/numDims + numSymbols + numLocals +
551+
1,
552+
numDims, numSymbols, numLocals) {}
553+
554+
/// Return a system with no constraints, i.e., one which is satisfied by all
555+
/// points.
556+
static IntegerPolyhedron getUniverse(unsigned numDims = 0,
557+
unsigned numSymbols = 0) {
558+
return IntegerPolyhedron(numDims, numSymbols);
559+
}
560+
561+
/// Return the kind of this IntegerRelation.
562+
Kind getKind() const override { return Kind::IntegerPolyhedron; }
563+
564+
static bool classof(const IntegerRelation *cst) {
565+
return cst->getKind() == Kind::IntegerPolyhedron;
566+
}
567+
568+
// Clones this object.
569+
std::unique_ptr<IntegerPolyhedron> clone() const;
548570
};
549571

550572
} // namespace presburger

mlir/include/mlir/Analysis/Presburger/LinearTransform.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// Support for linear transforms and applying them to an IntegerPolyhedron.
9+
// Support for linear transforms and applying them to an IntegerRelation.
1010
//
1111
//===----------------------------------------------------------------------===//
1212

1313
#ifndef MLIR_ANALYSIS_PRESBURGER_LINEARTRANSFORM_H
1414
#define MLIR_ANALYSIS_PRESBURGER_LINEARTRANSFORM_H
1515

16-
#include "mlir/Analysis/Presburger/IntegerPolyhedron.h"
16+
#include "mlir/Analysis/Presburger/IntegerRelation.h"
1717
#include "mlir/Analysis/Presburger/Matrix.h"
1818
#include "llvm/ADT/SmallVector.h"
1919

@@ -34,9 +34,9 @@ class LinearTransform {
3434
static std::pair<unsigned, LinearTransform>
3535
makeTransformToColumnEchelon(Matrix m);
3636

37-
// Returns an IntegerPolyhedron having a constraint vector vT for every
38-
// constraint vector v in poly, where T is this transform.
39-
IntegerPolyhedron applyTo(const IntegerPolyhedron &poly) const;
37+
// Returns an IntegerRelation having a constraint vector vT for every
38+
// constraint vector v in rel, where T is this transform.
39+
IntegerRelation applyTo(const IntegerRelation &rel) const;
4040

4141
// The given vector is interpreted as a row vector v. Post-multiply v with
4242
// this transform, say T, and return vT.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#ifndef MLIR_ANALYSIS_PRESBURGER_PWMAFUNCTION_H
1717
#define MLIR_ANALYSIS_PRESBURGER_PWMAFUNCTION_H
1818

19-
#include "mlir/Analysis/Presburger/IntegerPolyhedron.h"
19+
#include "mlir/Analysis/Presburger/IntegerRelation.h"
2020
#include "mlir/Analysis/Presburger/PresburgerSet.h"
2121

2222
namespace mlir {
@@ -62,8 +62,8 @@ class MultiAffineFunction : protected IntegerPolyhedron {
6262

6363
~MultiAffineFunction() override = default;
6464
Kind getKind() const override { return Kind::MultiAffineFunction; }
65-
bool classof(const IntegerPolyhedron *poly) const {
66-
return poly->getKind() == Kind::MultiAffineFunction;
65+
bool classof(const IntegerRelation *rel) const {
66+
return rel->getKind() == Kind::MultiAffineFunction;
6767
}
6868

6969
unsigned getNumInputs() const { return getNumDimAndSymbolIds(); }

mlir/include/mlir/Analysis/Presburger/PresburgerSet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#ifndef MLIR_ANALYSIS_PRESBURGER_PRESBURGERSET_H
1414
#define MLIR_ANALYSIS_PRESBURGER_PRESBURGERSET_H
1515

16-
#include "mlir/Analysis/Presburger/IntegerPolyhedron.h"
16+
#include "mlir/Analysis/Presburger/IntegerRelation.h"
1717

1818
namespace mlir {
1919
namespace presburger {

0 commit comments

Comments
 (0)