Skip to content

Commit 8506c8c

Browse files
committed
[MLIR] Move LinearTransform to Presburger/
This patch moves LinearTransform to Presburger/ and makes it use IntegerPolyhedron instead of FlatAffineConstraints. Also modifies its usage in `FlatAffineConstraints::findIntegerSample` to support the changes. This patch is part of a series of patches for moving presburger math functionality into Presburger directory. Reviewed By: arjunp Differential Revision: https://reviews.llvm.org/D116311
1 parent 1bb9f4e commit 8506c8c

File tree

11 files changed

+48
-46
lines changed

11 files changed

+48
-46
lines changed

mlir/include/mlir/Analysis/AffineStructures.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ class FlatAffineConstraints : public IntegerPolyhedron {
8181
1,
8282
numDims, numSymbols, numLocals) {}
8383

84+
explicit FlatAffineConstraints(const IntegerPolyhedron &poly)
85+
: IntegerPolyhedron(poly) {}
86+
8487
/// Return a system with no constraints, i.e., one which is satisfied by all
8588
/// points.
8689
static FlatAffineConstraints getUniverse(unsigned numDims = 0,
@@ -212,10 +215,6 @@ class FlatAffineConstraints : public IntegerPolyhedron {
212215
void projectOut(unsigned pos, unsigned num);
213216
inline void projectOut(unsigned pos) { return projectOut(pos, 1); }
214217

215-
/// Sets the `values.size()` identifiers starting at `po`s to the specified
216-
/// values and removes them.
217-
void setAndEliminate(unsigned pos, ArrayRef<int64_t> values);
218-
219218
/// Changes the partition between dimensions and symbols. Depending on the new
220219
/// symbol count, either a chunk of trailing dimensional identifiers becomes
221220
/// symbols, or some of the leading symbols become dimensions.

mlir/include/mlir/Analysis/Presburger/IntegerPolyhedron.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ class IntegerPolyhedron {
185185
/// Removes all equalities and inequalities.
186186
void clearConstraints();
187187

188+
/// Sets the `values.size()` identifiers starting at `po`s to the specified
189+
/// values and removes them.
190+
void setAndEliminate(unsigned pos, ArrayRef<int64_t> values);
191+
188192
/// Gather positions of all lower and upper bounds of the identifier at `pos`,
189193
/// and optionally any equalities on it. In addition, the bounds are to be
190194
/// independent of identifiers in position range [`offset`, `offset` + `num`).

mlir/include/mlir/Analysis/LinearTransform.h renamed to 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 FlatAffineConstraints.
9+
// Support for linear transforms and applying them to an IntegerPolyhedron.
1010
//
1111
//===----------------------------------------------------------------------===//
1212

1313
#ifndef MLIR_ANALYSIS_LINEARTRANSFORM_H
1414
#define MLIR_ANALYSIS_LINEARTRANSFORM_H
1515

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

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

36-
// Returns a FlatAffineConstraints having a constraint vector vT for every
37-
// constraint vector v in fac, where T is this transform.
38-
FlatAffineConstraints applyTo(const FlatAffineConstraints &fac) const;
36+
// Returns an IntegerPolyhedron having a constraint vector vT for every
37+
// constraint vector v in poly, where T is this transform.
38+
IntegerPolyhedron applyTo(const IntegerPolyhedron &poly) const;
3939

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

mlir/lib/Analysis/AffineStructures.cpp

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "mlir/Analysis/AffineStructures.h"
14-
#include "mlir/Analysis/LinearTransform.h"
14+
#include "mlir/Analysis/Presburger/LinearTransform.h"
1515
#include "mlir/Analysis/Presburger/Simplex.h"
1616
#include "mlir/Analysis/Presburger/Utils.h"
1717
#include "mlir/Dialect/Affine/IR/AffineOps.h"
@@ -1090,11 +1090,11 @@ FlatAffineConstraints::findIntegerSample() const {
10901090
LinearTransform::makeTransformToColumnEchelon(std::move(m));
10911091
const LinearTransform &transform = result.second;
10921092
// 1) Apply T to S to obtain S*T.
1093-
FlatAffineConstraints transformedSet = transform.applyTo(*this);
1093+
IntegerPolyhedron transformedSet = transform.applyTo(*this);
10941094

10951095
// 2) Remove the unbounded dimensions and constraints involving them to
10961096
// obtain a bounded set.
1097-
FlatAffineConstraints boundedSet = transformedSet;
1097+
FlatAffineConstraints boundedSet(transformedSet);
10981098
unsigned numBoundedDims = result.first;
10991099
unsigned numUnboundedDims = getNumIds() - numBoundedDims;
11001100
removeConstraintsInvolvingSuffixDims(boundedSet, numUnboundedDims);
@@ -1111,7 +1111,7 @@ FlatAffineConstraints::findIntegerSample() const {
11111111
// 4) Substitute the values of the bounded dimensions into S*T to obtain a
11121112
// full-dimensional cone, which necessarily contains an integer sample.
11131113
transformedSet.setAndEliminate(0, *boundedSample);
1114-
FlatAffineConstraints &cone = transformedSet;
1114+
IntegerPolyhedron &cone = transformedSet;
11151115

11161116
// 5) Obtain an integer sample from the cone.
11171117
//
@@ -1139,10 +1139,10 @@ FlatAffineConstraints::findIntegerSample() const {
11391139
// negative a_i, so we accomodate this by shifting the inequality by this
11401140
// amount for the shrunken cone.
11411141
for (unsigned i = 0, e = cone.getNumInequalities(); i < e; ++i) {
1142-
for (unsigned j = 0; j < cone.numIds; ++j) {
1142+
for (unsigned j = 0; j < cone.getNumIds(); ++j) {
11431143
int64_t coeff = cone.atIneq(i, j);
11441144
if (coeff < 0)
1145-
cone.atIneq(i, cone.numIds) += coeff;
1145+
cone.atIneq(i, cone.getNumIds()) += coeff;
11461146
}
11471147
}
11481148

@@ -2303,24 +2303,6 @@ static int findEqualityToConstant(const FlatAffineConstraints &cst,
23032303
return -1;
23042304
}
23052305

2306-
void FlatAffineConstraints::setAndEliminate(unsigned pos,
2307-
ArrayRef<int64_t> values) {
2308-
if (values.empty())
2309-
return;
2310-
assert(pos + values.size() <= getNumIds() &&
2311-
"invalid position or too many values");
2312-
// Setting x_j = p in sum_i a_i x_i + c is equivalent to adding p*a_j to the
2313-
// constant term and removing the id x_j. We do this for all the ids
2314-
// pos, pos + 1, ... pos + values.size() - 1.
2315-
for (unsigned r = 0, e = getNumInequalities(); r < e; r++)
2316-
for (unsigned i = 0, numVals = values.size(); i < numVals; ++i)
2317-
atIneq(r, getNumCols() - 1) += atIneq(r, pos + i) * values[i];
2318-
for (unsigned r = 0, e = getNumEqualities(); r < e; r++)
2319-
for (unsigned i = 0, numVals = values.size(); i < numVals; ++i)
2320-
atEq(r, getNumCols() - 1) += atEq(r, pos + i) * values[i];
2321-
removeIdRange(pos, pos + values.size());
2322-
}
2323-
23242306
LogicalResult FlatAffineConstraints::constantFoldId(unsigned pos) {
23252307
assert(pos < getNumIds() && "invalid position");
23262308
int rowIdx;

mlir/lib/Analysis/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ set(LLVM_OPTIONAL_SOURCES
66
CallGraph.cpp
77
DataFlowAnalysis.cpp
88
DataLayoutAnalysis.cpp
9-
LinearTransform.cpp
109
Liveness.cpp
1110
LoopAnalysis.cpp
1211
NestedMatcher.cpp
@@ -48,7 +47,6 @@ add_mlir_library(MLIRAnalysis
4847
add_mlir_library(MLIRLoopAnalysis
4948
AffineAnalysis.cpp
5049
AffineStructures.cpp
51-
LinearTransform.cpp
5250
LoopAnalysis.cpp
5351
NestedMatcher.cpp
5452
PresburgerSet.cpp

mlir/lib/Analysis/Presburger/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_mlir_library(MLIRPresburger
22
IntegerPolyhedron.cpp
3+
LinearTransform.cpp
34
Matrix.cpp
45
Simplex.cpp
56
Utils.cpp

mlir/lib/Analysis/Presburger/IntegerPolyhedron.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,24 @@ bool IntegerPolyhedron::hasConsistentState() const {
285285
return true;
286286
}
287287

288+
void IntegerPolyhedron::setAndEliminate(unsigned pos,
289+
ArrayRef<int64_t> values) {
290+
if (values.empty())
291+
return;
292+
assert(pos + values.size() <= getNumIds() &&
293+
"invalid position or too many values");
294+
// Setting x_j = p in sum_i a_i x_i + c is equivalent to adding p*a_j to the
295+
// constant term and removing the id x_j. We do this for all the ids
296+
// pos, pos + 1, ... pos + values.size() - 1.
297+
for (unsigned r = 0, e = getNumInequalities(); r < e; r++)
298+
for (unsigned i = 0, numVals = values.size(); i < numVals; ++i)
299+
atIneq(r, getNumCols() - 1) += atIneq(r, pos + i) * values[i];
300+
for (unsigned r = 0, e = getNumEqualities(); r < e; r++)
301+
for (unsigned i = 0, numVals = values.size(); i < numVals; ++i)
302+
atEq(r, getNumCols() - 1) += atEq(r, pos + i) * values[i];
303+
removeIdRange(pos, pos + values.size());
304+
}
305+
288306
void IntegerPolyhedron::printSpace(raw_ostream &os) const {
289307
os << "\nConstraints (" << getNumDimIds() << " dims, " << getNumSymbolIds()
290308
<< " symbols, " << getNumLocalIds() << " locals), (" << getNumConstraints()

mlir/lib/Analysis/LinearTransform.cpp renamed to mlir/lib/Analysis/Presburger/LinearTransform.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "mlir/Analysis/LinearTransform.h"
10-
#include "mlir/Analysis/AffineStructures.h"
9+
#include "mlir/Analysis/Presburger/LinearTransform.h"
10+
#include "mlir/Analysis/Presburger/IntegerPolyhedron.h"
1111

1212
namespace mlir {
1313

@@ -135,12 +135,12 @@ LinearTransform::preMultiplyColumn(ArrayRef<int64_t> colVec) const {
135135
return result;
136136
}
137137

138-
FlatAffineConstraints
139-
LinearTransform::applyTo(const FlatAffineConstraints &fac) const {
140-
FlatAffineConstraints result(fac.getNumIds());
138+
IntegerPolyhedron
139+
LinearTransform::applyTo(const IntegerPolyhedron &poly) const {
140+
IntegerPolyhedron result(poly.getNumIds());
141141

142-
for (unsigned i = 0, e = fac.getNumEqualities(); i < e; ++i) {
143-
ArrayRef<int64_t> eq = fac.getEquality(i);
142+
for (unsigned i = 0, e = poly.getNumEqualities(); i < e; ++i) {
143+
ArrayRef<int64_t> eq = poly.getEquality(i);
144144

145145
int64_t c = eq.back();
146146

@@ -149,8 +149,8 @@ LinearTransform::applyTo(const FlatAffineConstraints &fac) const {
149149
result.addEquality(newEq);
150150
}
151151

152-
for (unsigned i = 0, e = fac.getNumInequalities(); i < e; ++i) {
153-
ArrayRef<int64_t> ineq = fac.getInequality(i);
152+
for (unsigned i = 0, e = poly.getNumInequalities(); i < e; ++i) {
153+
ArrayRef<int64_t> ineq = poly.getInequality(i);
154154

155155
int64_t c = ineq.back();
156156

mlir/unittests/Analysis/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ add_mlir_unittest(MLIRAnalysisTests
22
AffineStructuresParser.cpp
33
AffineStructuresParserTest.cpp
44
AffineStructuresTest.cpp
5-
LinearTransformTest.cpp
65
PresburgerSetTest.cpp
76
)
87

mlir/unittests/Analysis/Presburger/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_mlir_unittest(MLIRPresburgerTests
22
IntegerPolyhedronTest.cpp
3+
LinearTransformTest.cpp
34
MatrixTest.cpp
45
SimplexTest.cpp
56
../AffineStructuresParser.cpp

0 commit comments

Comments
 (0)