[MLIR] Add clearAndCopyFrom to IntegerPolyhedron

This patch adds clearAndCopyFrom to IntegerPolyhedron. This requires moving
LLVM-style RTTI from FlatAffineConstraints to IntegerPolyhedron.

This patch is part of a series of patches to move presburger math to Presburger
directory.

Reviewed By: arjunp

Differential Revision: https://reviews.llvm.org/D116533
This commit is contained in:
Groverkss 2022-01-05 23:19:36 +05:30
parent e2165e0968
commit dde7388ad5
4 changed files with 45 additions and 17 deletions

View File

@ -59,9 +59,6 @@ struct MutableAffineMap;
/// ///
class FlatAffineConstraints : public IntegerPolyhedron { class FlatAffineConstraints : public IntegerPolyhedron {
public: public:
/// All derived classes of FlatAffineConstraints.
enum class Kind { FlatAffineConstraints, FlatAffineValueConstraints };
/// Constructs a constraint system reserving memory for the specified number /// Constructs a constraint system reserving memory for the specified number
/// of constraints and identifiers. /// of constraints and identifiers.
FlatAffineConstraints(unsigned numReservedInequalities, FlatAffineConstraints(unsigned numReservedInequalities,
@ -99,9 +96,11 @@ public:
virtual ~FlatAffineConstraints() = default; virtual ~FlatAffineConstraints() = default;
/// Return the kind of this FlatAffineConstraints. /// Return the kind of this FlatAffineConstraints.
virtual Kind getKind() const { return Kind::FlatAffineConstraints; } Kind getKind() const override { return Kind::FlatAffineConstraints; }
static bool classof(const FlatAffineConstraints *cst) { return true; } static bool classof(const IntegerPolyhedron *cst) {
return cst->getKind() == Kind::FlatAffineConstraints;
}
/// Checks for emptiness by performing variable elimination on all /// Checks for emptiness by performing variable elimination on all
/// identifiers, running the GCD test on each equality constraint, and /// identifiers, running the GCD test on each equality constraint, and
@ -250,7 +249,7 @@ public:
LogicalResult unionBoundingBox(const FlatAffineConstraints &other); LogicalResult unionBoundingBox(const FlatAffineConstraints &other);
/// Replaces the contents of this FlatAffineConstraints with `other`. /// Replaces the contents of this FlatAffineConstraints with `other`.
virtual void clearAndCopyFrom(const FlatAffineConstraints &other); void clearAndCopyFrom(const IntegerPolyhedron &other) override;
/// Returns the smallest known constant bound for the extent of the specified /// Returns the smallest known constant bound for the extent of the specified
/// identifier (pos^th), i.e., the smallest known constant that is greater /// identifier (pos^th), i.e., the smallest known constant that is greater
@ -499,7 +498,7 @@ public:
/// Return the kind of this FlatAffineConstraints. /// Return the kind of this FlatAffineConstraints.
Kind getKind() const override { return Kind::FlatAffineValueConstraints; } Kind getKind() const override { return Kind::FlatAffineValueConstraints; }
static bool classof(const FlatAffineConstraints *cst) { static bool classof(const IntegerPolyhedron *cst) {
return cst->getKind() == Kind::FlatAffineValueConstraints; return cst->getKind() == Kind::FlatAffineValueConstraints;
} }
@ -698,7 +697,7 @@ public:
bool areIdsAlignedWithOther(const FlatAffineValueConstraints &other); bool areIdsAlignedWithOther(const FlatAffineValueConstraints &other);
/// Replaces the contents of this FlatAffineValueConstraints with `other`. /// Replaces the contents of this FlatAffineValueConstraints with `other`.
void clearAndCopyFrom(const FlatAffineConstraints &other) override; void clearAndCopyFrom(const IntegerPolyhedron &other) override;
/// Returns the Value associated with the pos^th identifier. Asserts if /// Returns the Value associated with the pos^th identifier. Asserts if
/// no Value identifier was associated. /// no Value identifier was associated.

View File

@ -50,6 +50,13 @@ namespace mlir {
/// ///
class IntegerPolyhedron { class IntegerPolyhedron {
public: public:
/// All derived classes of IntegerPolyhedron.
enum class Kind {
FlatAffineConstraints,
FlatAffineValueConstraints,
IntegerPolyhedron
};
/// Kind of identifier (column). /// Kind of identifier (column).
enum IdKind { Dimension, Symbol, Local }; enum IdKind { Dimension, Symbol, Local };
@ -77,6 +84,11 @@ public:
virtual ~IntegerPolyhedron() = default; virtual ~IntegerPolyhedron() = default;
/// Return the kind of this IntegerPolyhedron.
virtual Kind getKind() const { return Kind::IntegerPolyhedron; }
static bool classof(const IntegerPolyhedron *cst) { return true; }
// Clones this object. // Clones this object.
std::unique_ptr<IntegerPolyhedron> clone() const; std::unique_ptr<IntegerPolyhedron> clone() const;
@ -189,6 +201,9 @@ public:
/// values and removes them. /// values and removes them.
void setAndEliminate(unsigned pos, ArrayRef<int64_t> values); void setAndEliminate(unsigned pos, ArrayRef<int64_t> values);
/// Replaces the contents of this IntegerPolyhedron with `other`.
virtual void clearAndCopyFrom(const IntegerPolyhedron &other);
/// Gather positions of all lower and upper bounds of the identifier at `pos`, /// Gather positions of all lower and upper bounds of the identifier at `pos`,
/// and optionally any equalities on it. In addition, the bounds are to be /// and optionally any equalities on it. In addition, the bounds are to be
/// independent of identifiers in position range [`offset`, `offset` + `num`). /// independent of identifiers in position range [`offset`, `offset` + `num`).

View File

@ -2643,26 +2643,36 @@ void FlatAffineConstraints::removeTrivialRedundancy() {
// the savings. // the savings.
} }
void FlatAffineConstraints::clearAndCopyFrom( void FlatAffineConstraints::clearAndCopyFrom(const IntegerPolyhedron &other) {
const FlatAffineConstraints &other) {
if (auto *otherValueSet = dyn_cast<const FlatAffineValueConstraints>(&other)) if (auto *otherValueSet = dyn_cast<const FlatAffineValueConstraints>(&other))
assert(!otherValueSet->hasValues() && assert(!otherValueSet->hasValues() &&
"cannot copy associated Values into FlatAffineConstraints"); "cannot copy associated Values into FlatAffineConstraints");
// Note: Assigment operator does not vtable pointer, so kind does not change.
*this = other; // Note: Assigment operator does not vtable pointer, so kind does not
// change.
if (auto *otherValueSet = dyn_cast<const FlatAffineConstraints>(&other))
*this = *otherValueSet;
else
*static_cast<IntegerPolyhedron *>(this) = other;
} }
void FlatAffineValueConstraints::clearAndCopyFrom( void FlatAffineValueConstraints::clearAndCopyFrom(
const FlatAffineConstraints &other) { const IntegerPolyhedron &other) {
if (auto *otherValueSet = if (auto *otherValueSet =
dyn_cast<const FlatAffineValueConstraints>(&other)) { dyn_cast<const FlatAffineValueConstraints>(&other)) {
*this = *otherValueSet; *this = *otherValueSet;
} else { return;
*static_cast<FlatAffineConstraints *>(this) = other; }
if (auto *otherValueSet = dyn_cast<const FlatAffineValueConstraints>(&other))
*static_cast<FlatAffineConstraints *>(this) = *otherValueSet;
else
*static_cast<IntegerPolyhedron *>(this) = other;
values.clear(); values.clear();
values.resize(numIds, None); values.resize(numIds, None);
} }
}
static std::pair<unsigned, unsigned> static std::pair<unsigned, unsigned>
getNewNumDimsSymbols(unsigned pos, const FlatAffineConstraints &cst) { getNewNumDimsSymbols(unsigned pos, const FlatAffineConstraints &cst) {

View File

@ -303,6 +303,10 @@ void IntegerPolyhedron::setAndEliminate(unsigned pos,
removeIdRange(pos, pos + values.size()); removeIdRange(pos, pos + values.size());
} }
void IntegerPolyhedron::clearAndCopyFrom(const IntegerPolyhedron &other) {
*this = other;
}
void IntegerPolyhedron::printSpace(raw_ostream &os) const { void IntegerPolyhedron::printSpace(raw_ostream &os) const {
os << "\nConstraints (" << getNumDimIds() << " dims, " << getNumSymbolIds() os << "\nConstraints (" << getNumDimIds() << " dims, " << getNumSymbolIds()
<< " symbols, " << getNumLocalIds() << " locals), (" << getNumConstraints() << " symbols, " << getNumLocalIds() << " locals), (" << getNumConstraints()