Skip to content

Commit 0dc618e

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

File tree

11 files changed

+190
-186
lines changed

11 files changed

+190
-186
lines changed

mlir/include/mlir/Analysis/FlatLinearValueConstraints.h

Lines changed: 59 additions & 54 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,12 @@ 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+
// Use values in space for FlatLinearValueConstraints.
223+
space.resetIds();
224+
// Set the values for the non-local variables.
225+
for (unsigned i = 0, e = valArgs.size(); i < e; ++i)
226+
if (valArgs[i])
227+
setValue(i, *valArgs[i]);
225228
}
226229

227230
/// Constructs a constraint system reserving memory for the specified number
@@ -236,11 +239,12 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
236239
: FlatLinearConstraints(numReservedInequalities, numReservedEqualities,
237240
numReservedCols, numDims, numSymbols, numLocals) {
238241
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());
242+
// Use values in space for FlatLinearValueConstraints.
243+
space.resetIds();
244+
// Set the values for the non-local variables.
245+
for (unsigned i = 0, e = valArgs.size(); i < e; ++i)
246+
if (valArgs[i])
247+
setValue(i, valArgs[i]);
244248
}
245249

246250
/// Constructs a constraint system with the specified number of dimensions
@@ -273,10 +277,12 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
273277
ArrayRef<std::optional<Value>> valArgs = {})
274278
: FlatLinearConstraints(fac) {
275279
assert(valArgs.empty() || valArgs.size() == getNumDimAndSymbolVars());
276-
if (valArgs.empty())
277-
values.resize(getNumDimAndSymbolVars(), std::nullopt);
278-
else
279-
values.append(valArgs.begin(), valArgs.end());
280+
// Use values in space for FlatLinearValueConstraints.
281+
space.resetIds();
282+
// Set the values for the non-local variables.
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,25 +318,48 @@ 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+
}
334+
return maybeValues;
322335
}
323336

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

331353
/// Returns true if the pos^th variable has an associated Value.
332354
inline bool hasValue(unsigned pos) const {
333355
assert(pos < getNumDimAndSymbolVars() && "Invalid position");
334-
return values[pos].has_value();
356+
VarKind kind = getVarKindAt(pos);
357+
unsigned relativePos = pos - getVarKindOffset(kind);
358+
return space.getId(kind, relativePos).hasValue();
359+
}
360+
361+
void resetValues() {
362+
space.resetIds();
335363
}
336364

337365
unsigned appendDimVar(ValueRange vals);
@@ -360,7 +388,9 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
360388
/// Sets the Value associated with the pos^th variable.
361389
inline void setValue(unsigned pos, Value val) {
362390
assert(pos < getNumDimAndSymbolVars() && "invalid var position");
363-
values[pos] = val;
391+
VarKind kind = getVarKindAt(pos);
392+
unsigned relativePos = pos - getVarKindOffset(kind);
393+
space.getId(kind, relativePos) = presburger::Identifier(val);
364394
}
365395

366396
/// Sets the Values associated with the variables in the range [start, end).
@@ -387,9 +417,6 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
387417
void projectOut(Value val);
388418
using IntegerPolyhedron::projectOut;
389419

390-
/// Swap the posA^th variable with the posB^th variable.
391-
void swapVar(unsigned posA, unsigned posB) override;
392-
393420
/// Prints the number of constraints, dimensions, symbols and locals in the
394421
/// FlatAffineValueConstraints. Also, prints for each variable whether there
395422
/// is an SSA Value attached to it.
@@ -444,28 +471,6 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
444471
/// output = {0 <= d0 <= 6, 1 <= d1 <= 15}
445472
LogicalResult unionBoundingBox(const FlatLinearValueConstraints &other);
446473
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;
469474
};
470475

471476
/// 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
@@ -127,6 +127,11 @@ class IntegerRelation {
127127
/// the variable.
128128
void setId(VarKind kind, unsigned i, Identifier id);
129129

130+
/// Search for an identifier by skipping offset identifiers. Returns true and
131+
/// sets idx to the index of the indentifier if the identifier exists, returns
132+
/// false and leaves idx unchanged otherwise.
133+
bool findVar(Identifier id, unsigned &idx, unsigned offset) const;
134+
130135
/// Returns a copy of the space without locals.
131136
PresburgerSpace getSpaceWithoutLocals() const {
132137
return PresburgerSpace::getRelationSpace(space.getNumDomainVars(),

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

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

268+
ArrayRef<Identifier> getIds() const {
269+
return identifiers;
270+
}
271+
268272
/// Returns if identifiers are being used.
269273
bool isUsingIds() const { return usingIds; }
270274

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ 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, presburger::IntegerRelation &rel);
255255
LogicalResult getRelationFromMap(const AffineValueMap &map,
256-
FlatAffineRelation &rel);
256+
presburger::IntegerRelation &rel);
257257

258258
} // namespace affine
259259
} // namespace mlir

0 commit comments

Comments
 (0)