Skip to content

Commit a1aa8e7

Browse files
committed
[MLIR][Presburger] Use Identifiers outside Presburger library
1 parent 5a61c36 commit a1aa8e7

File tree

10 files changed

+231
-193
lines changed

10 files changed

+231
-193
lines changed

mlir/include/mlir/Analysis/FlatLinearValueConstraints.h

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ class FlatLinearConstraints : public presburger::IntegerPolyhedron {
205205
/// where each non-local variable can have an SSA Value attached to it.
206206
class FlatLinearValueConstraints : public FlatLinearConstraints {
207207
public:
208+
using Identifier = presburger::Identifier;
209+
208210
/// Constructs a constraint system reserving memory for the specified number
209211
/// of constraints and variables. `valArgs` are the optional SSA values
210212
/// associated with each dimension/symbol. These must either be empty or match
@@ -217,11 +219,11 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
217219
: FlatLinearConstraints(numReservedInequalities, numReservedEqualities,
218220
numReservedCols, numDims, numSymbols, numLocals) {
219221
assert(valArgs.empty() || valArgs.size() == getNumDimAndSymbolVars());
220-
values.reserve(numReservedCols);
221-
if (valArgs.empty())
222-
values.resize(getNumDimAndSymbolVars(), std::nullopt);
223-
else
224-
values.append(valArgs.begin(), valArgs.end());
222+
// Store Values in space's identifiers.
223+
space.resetIds();
224+
for (unsigned i = 0, e = valArgs.size(); i < e; ++i)
225+
if (valArgs[i])
226+
setValue(i, *valArgs[i]);
225227
}
226228

227229
/// Constructs a constraint system reserving memory for the specified number
@@ -236,11 +238,11 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
236238
: FlatLinearConstraints(numReservedInequalities, numReservedEqualities,
237239
numReservedCols, numDims, numSymbols, numLocals) {
238240
assert(valArgs.empty() || valArgs.size() == getNumDimAndSymbolVars());
239-
values.reserve(numReservedCols);
240-
if (valArgs.empty())
241-
values.resize(getNumDimAndSymbolVars(), std::nullopt);
242-
else
243-
values.append(valArgs.begin(), valArgs.end());
241+
// Store Values in space's identifiers.
242+
space.resetIds();
243+
for (unsigned i = 0, e = valArgs.size(); i < e; ++i)
244+
if (valArgs[i])
245+
setValue(i, valArgs[i]);
244246
}
245247

246248
/// Constructs a constraint system with the specified number of dimensions
@@ -272,11 +274,15 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
272274
FlatLinearValueConstraints(const IntegerPolyhedron &fac,
273275
ArrayRef<std::optional<Value>> valArgs = {})
274276
: FlatLinearConstraints(fac) {
275-
assert(valArgs.empty() || valArgs.size() == getNumDimAndSymbolVars());
277+
// Do not reset values assigned by FlatLinearConstraints' constructor.
276278
if (valArgs.empty())
277-
values.resize(getNumDimAndSymbolVars(), std::nullopt);
278-
else
279-
values.append(valArgs.begin(), valArgs.end());
279+
return;
280+
assert(valArgs.size() == getNumDimAndSymbolVars());
281+
// Store Values in space's identifiers.
282+
space.resetIds();
283+
for (unsigned i = 0, e = valArgs.size(); i < e; ++i)
284+
if (valArgs[i])
285+
setValue(i, *valArgs[i]);
280286
}
281287

282288
/// Creates an affine constraint system from an IntegerSet.
@@ -290,9 +296,6 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
290296
cst->getKind() <= Kind::FlatAffineRelation;
291297
}
292298

293-
/// Replaces the contents of this FlatLinearValueConstraints with `other`.
294-
void clearAndCopyFrom(const IntegerRelation &other) override;
295-
296299
/// Adds a constant bound for the variable associated with the given Value.
297300
void addBound(presburger::BoundType type, Value val, int64_t value);
298301
using FlatLinearConstraints::addBound;
@@ -302,7 +305,9 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
302305
inline Value getValue(unsigned pos) const {
303306
assert(pos < getNumDimAndSymbolVars() && "Invalid position");
304307
assert(hasValue(pos) && "variable's Value not set");
305-
return *values[pos];
308+
VarKind kind = getVarKindAt(pos);
309+
unsigned relativePos = pos - getVarKindOffset(kind);
310+
return space.getId(kind, relativePos).getValue<Value>();
306311
}
307312

308313
/// Returns the Values associated with variables in range [start, end).
@@ -313,27 +318,47 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
313318
assert(start <= end && "invalid start position");
314319
values->clear();
315320
values->reserve(end - start);
316-
for (unsigned i = start; i < end; i++)
321+
for (unsigned i = start; i < end; ++i)
317322
values->push_back(getValue(i));
318323
}
319324

320-
inline ArrayRef<std::optional<Value>> getMaybeValues() const {
321-
return {values.data(), values.size()};
325+
inline SmallVector<std::optional<Value>> getMaybeValues() const {
326+
SmallVector<std::optional<Value>> maybeValues;
327+
maybeValues.reserve(getNumDimAndSymbolVars());
328+
for (unsigned i = 0, e = getNumDimAndSymbolVars(); i < e; ++i)
329+
if (hasValue(i))
330+
maybeValues.push_back(getValue(i));
331+
else
332+
maybeValues.push_back(std::nullopt);
333+
return maybeValues;
322334
}
323335

324-
inline ArrayRef<std::optional<Value>>
336+
inline SmallVector<std::optional<Value>>
325337
getMaybeValues(presburger::VarKind kind) const {
326338
assert(kind != VarKind::Local &&
327339
"Local variables do not have any value attached to them.");
328-
return {values.data() + getVarKindOffset(kind), getNumVarKind(kind)};
340+
SmallVector<std::optional<Value>> maybeValues;
341+
maybeValues.reserve(getNumVarKind(kind));
342+
for (unsigned i = 0, e = getNumVarKind(kind); i < e; ++i) {
343+
const Identifier id = space.getId(kind, i);
344+
if (id.hasValue())
345+
maybeValues.push_back(id.getValue<Value>());
346+
else
347+
maybeValues.push_back(std::nullopt);
348+
}
349+
return maybeValues;
329350
}
330351

331352
/// Returns true if the pos^th variable has an associated Value.
332353
inline bool hasValue(unsigned pos) const {
333354
assert(pos < getNumDimAndSymbolVars() && "Invalid position");
334-
return values[pos].has_value();
355+
VarKind kind = getVarKindAt(pos);
356+
unsigned relativePos = pos - getVarKindOffset(kind);
357+
return space.getId(kind, relativePos).hasValue();
335358
}
336359

360+
void resetValues() { space.resetIds(); }
361+
337362
unsigned appendDimVar(ValueRange vals);
338363
using FlatLinearConstraints::appendDimVar;
339364

@@ -360,7 +385,9 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
360385
/// Sets the Value associated with the pos^th variable.
361386
inline void setValue(unsigned pos, Value val) {
362387
assert(pos < getNumDimAndSymbolVars() && "invalid var position");
363-
values[pos] = val;
388+
VarKind kind = getVarKindAt(pos);
389+
unsigned relativePos = pos - getVarKindOffset(kind);
390+
space.getId(kind, relativePos) = presburger::Identifier(val);
364391
}
365392

366393
/// Sets the Values associated with the variables in the range [start, end).
@@ -387,9 +414,6 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
387414
void projectOut(Value val);
388415
using IntegerPolyhedron::projectOut;
389416

390-
/// Swap the posA^th variable with the posB^th variable.
391-
void swapVar(unsigned posA, unsigned posB) override;
392-
393417
/// Prints the number of constraints, dimensions, symbols and locals in the
394418
/// FlatAffineValueConstraints. Also, prints for each variable whether there
395419
/// is an SSA Value attached to it.
@@ -444,28 +468,6 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
444468
/// output = {0 <= d0 <= 6, 1 <= d1 <= 15}
445469
LogicalResult unionBoundingBox(const FlatLinearValueConstraints &other);
446470
using IntegerPolyhedron::unionBoundingBox;
447-
448-
protected:
449-
/// Eliminates the variable at the specified position using Fourier-Motzkin
450-
/// variable elimination, but uses Gaussian elimination if there is an
451-
/// equality involving that variable. If the result of the elimination is
452-
/// integer exact, `*isResultIntegerExact` is set to true. If `darkShadow` is
453-
/// set to true, a potential under approximation (subset) of the rational
454-
/// shadow / exact integer shadow is computed.
455-
// See implementation comments for more details.
456-
void fourierMotzkinEliminate(unsigned pos, bool darkShadow = false,
457-
bool *isResultIntegerExact = nullptr) override;
458-
459-
/// Returns false if the fields corresponding to various variable counts, or
460-
/// equality/inequality buffer sizes aren't consistent; true otherwise. This
461-
/// is meant to be used within an assert internally.
462-
bool hasConsistentState() const override;
463-
464-
/// Values corresponding to the (column) non-local variables of this
465-
/// constraint system appearing in the order the variables correspond to
466-
/// columns. Variables that aren't associated with any Value are set to
467-
/// std::nullopt.
468-
SmallVector<std::optional<Value>, 8> values;
469471
};
470472

471473
/// Flattens 'expr' into 'flattenedExpr', which contains the coefficients of the

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,11 @@ class IntegerRelation {
674674
/// this for uniformity with `applyDomain`.
675675
void applyRange(const IntegerRelation &rel);
676676

677+
/// Given a relation `other: (A -> B)`, this operation merges the symbol and
678+
/// local variables and then takes the composition of `other` on `this: (B ->
679+
/// C)`. The resulting relation represents tuples of the form: `A -> C`.
680+
void mergeAndCompose(const IntegerRelation &other);
681+
677682
/// Compute an equivalent representation of the same set, such that all local
678683
/// vars in all disjuncts have division representations. This representation
679684
/// may involve local vars that correspond to divisions, and may also be a

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ class PresburgerSpace {
265265
return {identifiers.data() + getVarKindOffset(kind), getNumVarKind(kind)};
266266
}
267267

268+
ArrayRef<Identifier> getIds() const { return identifiers; }
269+
268270
/// Returns if identifiers are being used.
269271
bool isUsingIds() const { return usingIds; }
270272

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef MLIR_DIALECT_AFFINE_ANALYSIS_AFFINEANALYSIS_H
1616
#define MLIR_DIALECT_AFFINE_ANALYSIS_AFFINEANALYSIS_H
1717

18+
#include "mlir/Analysis/Presburger/IntegerRelation.h"
1819
#include "mlir/Dialect/Arith/IR/Arith.h"
1920
#include "mlir/IR/Value.h"
2021
#include "llvm/ADT/SmallVector.h"
@@ -115,7 +116,7 @@ struct MemRefAccess {
115116
///
116117
/// Returns failure for yet unimplemented/unsupported cases (see docs of
117118
/// mlir::getIndexSet and mlir::getRelationFromMap for these cases).
118-
LogicalResult getAccessRelation(FlatAffineRelation &accessRel) const;
119+
LogicalResult getAccessRelation(presburger::IntegerRelation &accessRel) const;
119120

120121
/// Populates 'accessMap' with composition of AffineApplyOps reachable from
121122
/// 'indices'.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,10 @@ class FlatAffineRelation : public FlatAffineValueConstraints {
251251
/// For AffineValueMap, the domain and symbols have Value set corresponding to
252252
/// the Value in `map`. Returns failure if the AffineMap could not be flattened
253253
/// (i.e., semi-affine is not yet handled).
254-
LogicalResult getRelationFromMap(AffineMap &map, FlatAffineRelation &rel);
254+
LogicalResult getRelationFromMap(AffineMap &map,
255+
presburger::IntegerRelation &rel);
255256
LogicalResult getRelationFromMap(const AffineValueMap &map,
256-
FlatAffineRelation &rel);
257+
presburger::IntegerRelation &rel);
257258

258259
} // namespace affine
259260
} // namespace mlir

0 commit comments

Comments
 (0)