diff --git a/mlir/include/mlir/Analysis/AffineStructures.h b/mlir/include/mlir/Analysis/AffineStructures.h index bb76b4ff13d2..3991628a6100 100644 --- a/mlir/include/mlir/Analysis/AffineStructures.h +++ b/mlir/include/mlir/Analysis/AffineStructures.h @@ -59,9 +59,6 @@ struct MutableAffineMap; /// class FlatAffineConstraints : public IntegerPolyhedron { public: - /// All derived classes of FlatAffineConstraints. - enum class Kind { FlatAffineConstraints, FlatAffineValueConstraints }; - /// Constructs a constraint system reserving memory for the specified number /// of constraints and identifiers. FlatAffineConstraints(unsigned numReservedInequalities, @@ -99,9 +96,11 @@ public: virtual ~FlatAffineConstraints() = default; /// 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 /// identifiers, running the GCD test on each equality constraint, and @@ -250,7 +249,7 @@ public: LogicalResult unionBoundingBox(const FlatAffineConstraints &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 /// identifier (pos^th), i.e., the smallest known constant that is greater @@ -499,7 +498,7 @@ public: /// Return the kind of this FlatAffineConstraints. Kind getKind() const override { return Kind::FlatAffineValueConstraints; } - static bool classof(const FlatAffineConstraints *cst) { + static bool classof(const IntegerPolyhedron *cst) { return cst->getKind() == Kind::FlatAffineValueConstraints; } @@ -698,7 +697,7 @@ public: bool areIdsAlignedWithOther(const FlatAffineValueConstraints &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 /// no Value identifier was associated. diff --git a/mlir/include/mlir/Analysis/Presburger/IntegerPolyhedron.h b/mlir/include/mlir/Analysis/Presburger/IntegerPolyhedron.h index 9185ea14cd53..eae56e3acd0a 100644 --- a/mlir/include/mlir/Analysis/Presburger/IntegerPolyhedron.h +++ b/mlir/include/mlir/Analysis/Presburger/IntegerPolyhedron.h @@ -50,6 +50,13 @@ namespace mlir { /// class IntegerPolyhedron { public: + /// All derived classes of IntegerPolyhedron. + enum class Kind { + FlatAffineConstraints, + FlatAffineValueConstraints, + IntegerPolyhedron + }; + /// Kind of identifier (column). enum IdKind { Dimension, Symbol, Local }; @@ -77,6 +84,11 @@ public: 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. std::unique_ptr clone() const; @@ -189,6 +201,9 @@ public: /// values and removes them. void setAndEliminate(unsigned pos, ArrayRef 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`, /// and optionally any equalities on it. In addition, the bounds are to be /// independent of identifiers in position range [`offset`, `offset` + `num`). diff --git a/mlir/lib/Analysis/AffineStructures.cpp b/mlir/lib/Analysis/AffineStructures.cpp index b6f592a71023..6c11bf74a414 100644 --- a/mlir/lib/Analysis/AffineStructures.cpp +++ b/mlir/lib/Analysis/AffineStructures.cpp @@ -2643,25 +2643,35 @@ void FlatAffineConstraints::removeTrivialRedundancy() { // the savings. } -void FlatAffineConstraints::clearAndCopyFrom( - const FlatAffineConstraints &other) { +void FlatAffineConstraints::clearAndCopyFrom(const IntegerPolyhedron &other) { if (auto *otherValueSet = dyn_cast(&other)) assert(!otherValueSet->hasValues() && "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(&other)) + *this = *otherValueSet; + else + *static_cast(this) = other; } void FlatAffineValueConstraints::clearAndCopyFrom( - const FlatAffineConstraints &other) { + const IntegerPolyhedron &other) { + if (auto *otherValueSet = dyn_cast(&other)) { *this = *otherValueSet; - } else { - *static_cast(this) = other; - values.clear(); - values.resize(numIds, None); + return; } + + if (auto *otherValueSet = dyn_cast(&other)) + *static_cast(this) = *otherValueSet; + else + *static_cast(this) = other; + + values.clear(); + values.resize(numIds, None); } static std::pair diff --git a/mlir/lib/Analysis/Presburger/IntegerPolyhedron.cpp b/mlir/lib/Analysis/Presburger/IntegerPolyhedron.cpp index 03de0e97aaee..06b39cbc0f8b 100644 --- a/mlir/lib/Analysis/Presburger/IntegerPolyhedron.cpp +++ b/mlir/lib/Analysis/Presburger/IntegerPolyhedron.cpp @@ -303,6 +303,10 @@ void IntegerPolyhedron::setAndEliminate(unsigned pos, removeIdRange(pos, pos + values.size()); } +void IntegerPolyhedron::clearAndCopyFrom(const IntegerPolyhedron &other) { + *this = other; +} + void IntegerPolyhedron::printSpace(raw_ostream &os) const { os << "\nConstraints (" << getNumDimIds() << " dims, " << getNumSymbolIds() << " symbols, " << getNumLocalIds() << " locals), (" << getNumConstraints()