diff --git a/mlir/include/mlir/Analysis/Presburger/IntegerPolyhedron.h b/mlir/include/mlir/Analysis/Presburger/IntegerPolyhedron.h --- a/mlir/include/mlir/Analysis/Presburger/IntegerPolyhedron.h +++ b/mlir/include/mlir/Analysis/Presburger/IntegerPolyhedron.h @@ -212,18 +212,13 @@ /// corresponding to the added identifiers are initialized to zero. Return the /// absolute column position (i.e., not relative to the kind of identifier) /// of the first added identifier. - unsigned insertDimId(unsigned pos, unsigned num = 1); - unsigned insertSymbolId(unsigned pos, unsigned num = 1); - unsigned insertLocalId(unsigned pos, unsigned num = 1); unsigned insertId(IdKind kind, unsigned pos, unsigned num = 1) override; /// Append `num` identifiers of the specified kind after the last identifier. - /// of that kind. Return the position of the first appended column. The - /// coefficient columns corresponding to the added identifiers are initialized - /// to zero. - unsigned appendDimId(unsigned num = 1); - unsigned appendSymbolId(unsigned num = 1); - unsigned appendLocalId(unsigned num = 1); + /// of that kind. Return the position of the first appended column relative to + /// the kind of identifier. The coefficient columns corresponding to the added + /// identifiers are initialized to zero. + unsigned appendId(IdKind kind, unsigned num = 1); /// Adds an inequality (>= 0) from the coefficients specified in `inEq`. void addInequality(ArrayRef inEq); diff --git a/mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h b/mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h --- a/mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h +++ b/mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h @@ -22,6 +22,11 @@ class PresburgerLocalSpace; +/// Kind of identifier. Implementation wise SetDims are treated as Range +/// ids, and spaces with no distinction between dimension ids are treated +/// as relations with zero domain ids. +enum class IdKind { Symbol, Local, Domain, Range, SetDim = Range }; + /// PresburgerSpace is the space of all possible values of a tuple of integer /// valued variables/identifiers. Each identifier has one of the three types: /// @@ -65,11 +70,6 @@ friend PresburgerLocalSpace; public: - /// Kind of identifier. Implementation wise SetDims are treated as Range - /// ids, and spaces with no distinction between dimension ids are treated - /// as relations with zero domain ids. - enum IdKind { Symbol, Local, Domain, Range, SetDim = Range }; - static PresburgerSpace getRelationSpace(unsigned numDomain, unsigned numRange, unsigned numSymbols); diff --git a/mlir/include/mlir/Dialect/Affine/Analysis/AffineStructures.h b/mlir/include/mlir/Dialect/Affine/Analysis/AffineStructures.h --- a/mlir/include/mlir/Dialect/Affine/Analysis/AffineStructures.h +++ b/mlir/include/mlir/Dialect/Affine/Analysis/AffineStructures.h @@ -105,6 +105,35 @@ // Clones this object. std::unique_ptr clone() const; + /// Insert `num` identifiers of the specified kind at position `pos`. + /// Positions are relative to the kind of identifier. The coefficient columns + /// corresponding to the added identifiers are initialized to zero. Return the + /// absolute column position (i.e., not relative to the kind of identifier) + /// of the first added identifier. + unsigned insertDimId(unsigned pos, unsigned num = 1) { + return insertId(IdKind::SetDim, pos, num); + } + unsigned insertSymbolId(unsigned pos, unsigned num = 1) { + return insertId(IdKind::Symbol, pos, num); + } + unsigned insertLocalId(unsigned pos, unsigned num = 1) { + return insertId(IdKind::Local, pos, num); + } + + /// Append `num` identifiers of the specified kind after the last identifier. + /// of that kind. Return the position of the first appended column relative to + /// the kind of identifier. The coefficient columns corresponding to the added + /// identifiers are initialized to zero. + unsigned appendDimId(unsigned num = 1) { + return appendId(IdKind::SetDim, num); + } + unsigned appendSymbolId(unsigned num = 1) { + return appendId(IdKind::Symbol, num); + } + unsigned appendLocalId(unsigned num = 1) { + return appendId(IdKind::Local, num); + } + /// Adds a bound for the identifier at the specified position with constraints /// being drawn from the specified bound map. In case of an EQ bound, the /// bound map is expected to have exactly one result. In case of a LB/UB, the @@ -156,6 +185,8 @@ MLIRContext *context) const; protected: + using IdKind = presburger::IdKind; + /// Given an affine map that is aligned with this constraint system: /// * Flatten the map. /// * Add newly introduced local columns at the beginning of this constraint diff --git a/mlir/lib/Analysis/Presburger/IntegerPolyhedron.cpp b/mlir/lib/Analysis/Presburger/IntegerPolyhedron.cpp --- a/mlir/lib/Analysis/Presburger/IntegerPolyhedron.cpp +++ b/mlir/lib/Analysis/Presburger/IntegerPolyhedron.cpp @@ -111,18 +111,6 @@ return maybeLexMin; } -unsigned IntegerPolyhedron::insertDimId(unsigned pos, unsigned num) { - return insertId(IdKind::SetDim, pos, num); -} - -unsigned IntegerPolyhedron::insertSymbolId(unsigned pos, unsigned num) { - return insertId(IdKind::Symbol, pos, num); -} - -unsigned IntegerPolyhedron::insertLocalId(unsigned pos, unsigned num) { - return insertId(IdKind::Local, pos, num); -} - unsigned IntegerPolyhedron::insertId(IdKind kind, unsigned pos, unsigned num) { assert(pos <= getNumIdKind(kind)); @@ -132,22 +120,9 @@ return insertPos; } -unsigned IntegerPolyhedron::appendDimId(unsigned num) { - unsigned pos = getNumDimIds(); - insertId(IdKind::SetDim, pos, num); - return pos; -} - -unsigned IntegerPolyhedron::appendSymbolId(unsigned num) { - unsigned pos = getNumSymbolIds(); - insertId(IdKind::Symbol, pos, num); - return pos; -} - -unsigned IntegerPolyhedron::appendLocalId(unsigned num) { - unsigned pos = getNumLocalIds(); - insertId(IdKind::Local, pos, num); - return pos; +unsigned IntegerPolyhedron::appendId(IdKind kind, unsigned num) { + unsigned pos = getNumIdKind(kind); + return insertId(kind, pos, num); } void IntegerPolyhedron::addEquality(ArrayRef eq) { @@ -1044,8 +1019,8 @@ // i.e. append local ids of `polyB` to `polyA` and insert local ids of `polyA` // to `polyB` at start of its local ids. unsigned initLocals = polyA.getNumLocalIds(); - insertLocalId(polyA.getNumLocalIds(), polyB.getNumLocalIds()); - polyB.insertLocalId(0, initLocals); + insertId(IdKind::Local, polyA.getNumLocalIds(), polyB.getNumLocalIds()); + polyB.insertId(IdKind::Local, 0, initLocals); // Get division representations from each poly. std::vector> divsA, divsB; @@ -1133,7 +1108,7 @@ // Append new local variables corresponding to the dimensions to be converted. unsigned convertCount = dimLimit - dimStart; unsigned newLocalIdStart = getNumIds(); - appendLocalId(convertCount); + appendId(IdKind::Local, convertCount); // Swap the new local variables with dimensions. for (unsigned i = 0; i < convertCount; ++i) @@ -1178,7 +1153,7 @@ assert(dividend.size() == getNumCols() && "incorrect dividend size"); assert(divisor > 0 && "positive divisor expected"); - appendLocalId(); + appendId(IdKind::Local); // Add two constraints for this new identifier 'q'. SmallVector bound(dividend.size() + 1); diff --git a/mlir/lib/Analysis/Presburger/PresburgerSet.cpp b/mlir/lib/Analysis/Presburger/PresburgerSet.cpp --- a/mlir/lib/Analysis/Presburger/PresburgerSet.cpp +++ b/mlir/lib/Analysis/Presburger/PresburgerSet.cpp @@ -179,8 +179,7 @@ const unsigned initialSnapshot = simplex.getSnapshot(); auto restoreState = [&]() { - b.removeIdRange(IntegerPolyhedron::IdKind::Local, bInitNumLocals, - b.getNumLocalIds()); + b.removeIdRange(IdKind::Local, bInitNumLocals, b.getNumLocalIds()); b.removeInequalityRange(bInitNumIneqs, b.getNumInequalities()); b.removeEqualityRange(bInitNumEqs, b.getNumEqualities()); simplex.rollback(initialSnapshot); diff --git a/mlir/lib/Dialect/Affine/Analysis/AffineStructures.cpp b/mlir/lib/Dialect/Affine/Analysis/AffineStructures.cpp --- a/mlir/lib/Dialect/Affine/Analysis/AffineStructures.cpp +++ b/mlir/lib/Dialect/Affine/Analysis/AffineStructures.cpp @@ -362,14 +362,14 @@ /// Checks if the SSA values associated with `cst`'s identifiers of kind `kind` /// are unique. -static bool LLVM_ATTRIBUTE_UNUSED areIdsUnique( - const FlatAffineValueConstraints &cst, FlatAffineConstraints::IdKind kind) { +static bool LLVM_ATTRIBUTE_UNUSED +areIdsUnique(const FlatAffineValueConstraints &cst, IdKind kind) { - if (kind == FlatAffineConstraints::IdKind::SetDim) + if (kind == IdKind::SetDim) return areIdsUnique(cst, 0, cst.getNumDimIds()); - if (kind == FlatAffineConstraints::IdKind::Symbol) + if (kind == IdKind::Symbol) return areIdsUnique(cst, cst.getNumDimIds(), cst.getNumDimAndSymbolIds()); - if (kind == FlatAffineConstraints::IdKind::Local) + if (kind == IdKind::Local) return areIdsUnique(cst, cst.getNumDimAndSymbolIds(), cst.getNumIds()); llvm_unreachable("Unexpected IdKind"); } diff --git a/mlir/unittests/Analysis/Presburger/IntegerPolyhedronTest.cpp b/mlir/unittests/Analysis/Presburger/IntegerPolyhedronTest.cpp --- a/mlir/unittests/Analysis/Presburger/IntegerPolyhedronTest.cpp +++ b/mlir/unittests/Analysis/Presburger/IntegerPolyhedronTest.cpp @@ -180,17 +180,17 @@ IntegerPolyhedron set(3, 2, 1); set.addInequality({10, 11, 12, 20, 21, 30, 40}); - set.removeId(IntegerPolyhedron::IdKind::Symbol, 1); + set.removeId(IdKind::Symbol, 1); EXPECT_THAT(set.getInequality(0), testing::ElementsAre(10, 11, 12, 20, 30, 40)); - set.removeIdRange(IntegerPolyhedron::IdKind::SetDim, 0, 2); + set.removeIdRange(IdKind::SetDim, 0, 2); EXPECT_THAT(set.getInequality(0), testing::ElementsAre(12, 20, 30, 40)); - set.removeIdRange(IntegerPolyhedron::IdKind::Local, 1, 1); + set.removeIdRange(IdKind::Local, 1, 1); EXPECT_THAT(set.getInequality(0), testing::ElementsAre(12, 20, 30, 40)); - set.removeIdRange(IntegerPolyhedron::IdKind::Local, 0, 1); + set.removeIdRange(IdKind::Local, 0, 1); EXPECT_THAT(set.getInequality(0), testing::ElementsAre(12, 20, 40)); } @@ -830,7 +830,7 @@ IntegerPolyhedron poly2(1); poly2.addLocalFloorDiv({1, 0}, 2); // y = [x / 2]. poly2.addEquality({1, -5, 0}); // x = 5y. - poly2.appendLocalId(); // Add local id z. + poly2.appendId(IdKind::Local); // Add local id z. poly1.mergeLocalIds(poly2); @@ -878,7 +878,7 @@ IntegerPolyhedron poly2(1); poly2.addLocalFloorDiv({1, 0}, 2); // y = [x / 2]. poly2.addEquality({1, -5, 0}); // x = 5y. - poly2.appendLocalId(); // Add local id z. + poly2.appendId(IdKind::Local); // Add local id z. poly1.mergeLocalIds(poly2); diff --git a/mlir/unittests/Analysis/Presburger/PresburgerSpaceTest.cpp b/mlir/unittests/Analysis/Presburger/PresburgerSpaceTest.cpp --- a/mlir/unittests/Analysis/Presburger/PresburgerSpaceTest.cpp +++ b/mlir/unittests/Analysis/Presburger/PresburgerSpaceTest.cpp @@ -13,17 +13,15 @@ using namespace mlir; using namespace presburger; -using IdKind = PresburgerSpace::IdKind; - TEST(PresburgerSpaceTest, insertId) { PresburgerSpace space = PresburgerSpace::getRelationSpace(2, 2, 1); // Try inserting 2 domain ids. - space.insertId(PresburgerSpace::IdKind::Domain, 0, 2); + space.insertId(IdKind::Domain, 0, 2); EXPECT_EQ(space.getNumDomainIds(), 4u); // Try inserting 1 range ids. - space.insertId(PresburgerSpace::IdKind::Range, 0, 1); + space.insertId(IdKind::Range, 0, 1); EXPECT_EQ(space.getNumRangeIds(), 3u); } @@ -33,7 +31,7 @@ // Try inserting 2 dimension ids. The space should have 4 range ids since // spaces which do not distinguish between domain, range are implemented like // this. - space.insertId(PresburgerSpace::IdKind::SetDim, 0, 2); + space.insertId(IdKind::SetDim, 0, 2); EXPECT_EQ(space.getNumRangeIds(), 4u); }