1
- // ===- IntegerPolyhedron .h - MLIR IntegerPolyhedron Class -----*- C++ -*---===//
1
+ // ===- IntegerRelation .h - MLIR IntegerRelation Class ---- -----*- C++ -*---===//
2
2
//
3
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
4
// See https://llvm.org/LICENSE.txt for license information.
5
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
6
//
7
7
// ===----------------------------------------------------------------------===//
8
8
//
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.
10
12
//
11
13
// ===----------------------------------------------------------------------===//
12
14
@@ -42,6 +44,15 @@ namespace presburger {
42
44
// / and IdKind::Domain should be used to refer to dimension identifiers.
43
45
class IntegerRelation : public PresburgerLocalSpace {
44
46
public:
47
+ // / All derived classes of IntegerRelation.
48
+ enum class Kind {
49
+ FlatAffineConstraints,
50
+ FlatAffineValueConstraints,
51
+ MultiAffineFunction,
52
+ IntegerRelation,
53
+ IntegerPolyhedron,
54
+ };
55
+
45
56
// / Constructs a relation reserving memory for the specified number
46
57
// / of constraints and identifiers.
47
58
IntegerRelation (unsigned numReservedInequalities,
@@ -64,99 +75,35 @@ class IntegerRelation : public PresburgerLocalSpace {
64
75
numLocals + 1 ,
65
76
numDomain, numRange, numSymbols, numLocals) {}
66
77
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
-
132
78
// / Return a system with no constraints, i.e., one which is satisfied by all
133
79
// / 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);
137
84
}
138
85
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 ; }
141
88
142
- static bool classof (const IntegerPolyhedron *cst) { return true ; }
89
+ static bool classof (const IntegerRelation *cst) { return true ; }
143
90
144
91
// Clones this object.
145
- std::unique_ptr<IntegerPolyhedron > clone () const ;
92
+ std::unique_ptr<IntegerRelation > clone () const ;
146
93
147
94
// / Appends constraints from `other` into `this`. This is equivalent to an
148
95
// / intersection with no simplification of any sort attempted.
149
- void append (const IntegerPolyhedron &other);
96
+ void append (const IntegerRelation &other);
150
97
151
98
// / Return whether `this` and `other` are equal. This is integer-exact
152
99
// / 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 ;
155
102
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
157
104
// / 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 ;
160
107
161
108
// / Returns the value at the specified equality row and column.
162
109
inline int64_t atEq (unsigned i, unsigned j) const { return equalities (i, j); }
@@ -237,15 +184,16 @@ class IntegerPolyhedron : public IntegerRelation {
237
184
void removeInequalityRange (unsigned start, unsigned end);
238
185
239
186
// / 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
241
188
// / 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.
243
190
MaybeOptimum<SmallVector<Fraction, 8 >> findRationalLexMin () const ;
244
191
245
192
// / Same as above, but returns lexicographically minimal integer point.
246
193
// / Note: this should be used only when the lexmin is really required.
247
194
// / 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.
249
197
MaybeOptimum<SmallVector<int64_t , 8 >> findIntegerLexMin () const ;
250
198
251
199
// / Swap the posA^th identifier with the posB^th identifier.
@@ -258,8 +206,8 @@ class IntegerPolyhedron : public IntegerRelation {
258
206
// / values and removes them.
259
207
void setAndEliminate (unsigned pos, ArrayRef<int64_t > values);
260
208
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);
263
211
264
212
// / Gather positions of all lower and upper bounds of the identifier at `pos`,
265
213
// / and optionally any equalities on it. In addition, the bounds are to be
@@ -304,14 +252,14 @@ class IntegerPolyhedron : public IntegerRelation {
304
252
Optional<SmallVector<int64_t , 8 >> findIntegerSample () const ;
305
253
306
254
// / 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
308
256
// / overapproximation is infinite, an empty optional is returned.
309
257
Optional<uint64_t > computeVolume () const ;
310
258
311
259
// / Returns true if the given point satisfies the constraints, or false
312
260
// / otherwise.
313
261
// /
314
- // / Note: currently, if the polyhedron contains local ids, the values of
262
+ // / Note: currently, if the relation contains local ids, the values of
315
263
// / the local ids must also be provided.
316
264
bool containsPoint (ArrayRef<int64_t > point) const ;
317
265
@@ -383,7 +331,7 @@ class IntegerPolyhedron : public IntegerRelation {
383
331
// / 3) this = {0 <= d0 <= 5, 1 <= d1 <= 9}
384
332
// / other = {2 <= d0 <= 6, 5 <= d1 <= 15},
385
333
// / output = {0 <= d0 <= 6, 1 <= d1 <= 15}
386
- LogicalResult unionBoundingBox (const IntegerPolyhedron &other);
334
+ LogicalResult unionBoundingBox (const IntegerRelation &other);
387
335
388
336
// / Returns the smallest known constant bound for the extent of the specified
389
337
// / identifier (pos^th), i.e., the smallest known constant that is greater
@@ -452,12 +400,27 @@ class IntegerPolyhedron : public IntegerRelation {
452
400
// /
453
401
// / The number of dimensions and symbol ids in `this` and `other` should
454
402
// / match.
455
- void mergeLocalIds (IntegerPolyhedron &other);
403
+ void mergeLocalIds (IntegerRelation &other);
456
404
457
405
void print (raw_ostream &os) const ;
458
406
void dump () const ;
459
407
460
408
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
+
461
424
// / Checks all rows of equality/inequality constraints for trivial
462
425
// / contradictions (for example: 1 == 0, 0 >= 1), which may have surfaced
463
426
// / after elimination. Returns true if an invalid constraint is found;
@@ -526,7 +489,7 @@ class IntegerPolyhedron : public IntegerRelation {
526
489
virtual bool hasConsistentState () const ;
527
490
528
491
// / Prints the number of constraints, dimensions, symbols and locals in the
529
- // / IntegerPolyhedron .
492
+ // / IntegerRelation .
530
493
virtual void printSpace (raw_ostream &os) const ;
531
494
532
495
// / Removes identifiers in the column range [idStart, idLimit), and copies any
@@ -537,14 +500,73 @@ class IntegerPolyhedron : public IntegerRelation {
537
500
// / A parameter that controls detection of an unrealistic number of
538
501
// / constraints. If the number of constraints is this many times the number of
539
502
// / variables, we consider such a system out of line with the intended use
540
- // / case of IntegerPolyhedron .
503
+ // / case of IntegerRelation .
541
504
// The rationale for 32 is that in the typical simplest of cases, an
542
505
// identifier is expected to have one lower bound and one upper bound
543
506
// constraint. With a level of tiling or a connection to another identifier
544
507
// through a div or mod, an extra pair of bounds gets added. As a limit, we
545
508
// don't expect an identifier to have more than 32 lower/upper/equality
546
509
// constraints. This is conservatively set low and can be raised if needed.
547
510
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 ;
548
570
};
549
571
550
572
} // namespace presburger
0 commit comments