[Mlir-commits] [mlir] 3001d46 - [MLIR][Presburger] Move IdKind specific insert/append

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sat Feb 26 05:55:13 PST 2022


Author: Groverkss
Date: 2022-02-26T18:54:30+05:30
New Revision: 3001d4674d2869b8d61356b40af8dee9a018e5de

URL: https://github.com/llvm/llvm-project/commit/3001d4674d2869b8d61356b40af8dee9a018e5de
DIFF: https://github.com/llvm/llvm-project/commit/3001d4674d2869b8d61356b40af8dee9a018e5de.diff

LOG: [MLIR][Presburger] Move IdKind specific insert/append

This patch moves identifier kind specific insert/append functions like
`insertDimId`, `appendSymbolId`, etc. from IntegerPolyhedron to
FlatAffineConstraints.

This change allows for a smoother transition to IntegerRelation.

This change is part of a series of patches to introduce Relations in Presburger
library.

Reviewed By: arjunp

Differential Revision: https://reviews.llvm.org/D120576

Added: 
    

Modified: 
    mlir/include/mlir/Analysis/Presburger/IntegerPolyhedron.h
    mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h
    mlir/include/mlir/Dialect/Affine/Analysis/AffineStructures.h
    mlir/lib/Analysis/Presburger/IntegerPolyhedron.cpp
    mlir/lib/Analysis/Presburger/PresburgerSet.cpp
    mlir/lib/Dialect/Affine/Analysis/AffineStructures.cpp
    mlir/unittests/Analysis/Presburger/IntegerPolyhedronTest.cpp
    mlir/unittests/Analysis/Presburger/PresburgerSpaceTest.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Analysis/Presburger/IntegerPolyhedron.h b/mlir/include/mlir/Analysis/Presburger/IntegerPolyhedron.h
index d2554d3f1b51a..5f0ae76855049 100644
--- a/mlir/include/mlir/Analysis/Presburger/IntegerPolyhedron.h
+++ b/mlir/include/mlir/Analysis/Presburger/IntegerPolyhedron.h
@@ -212,18 +212,13 @@ class IntegerPolyhedron : public IntegerRelation {
   /// 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<int64_t> inEq);

diff  --git a/mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h b/mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h
index 6453e47451279..deb90e699aa85 100644
--- a/mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h
+++ b/mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h
@@ -22,6 +22,11 @@ namespace presburger {
 
 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 @@ class PresburgerSpace {
   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
index 374f0ce73570f..67e565b73d8c8 100644
--- a/mlir/include/mlir/Dialect/Affine/Analysis/AffineStructures.h
+++ b/mlir/include/mlir/Dialect/Affine/Analysis/AffineStructures.h
@@ -105,6 +105,35 @@ class FlatAffineConstraints : public presburger::IntegerPolyhedron {
   // Clones this object.
   std::unique_ptr<FlatAffineConstraints> 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 @@ class FlatAffineConstraints : public presburger::IntegerPolyhedron {
                         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
index d4eb66bfc8e6b..dba7b04719f4d 100644
--- a/mlir/lib/Analysis/Presburger/IntegerPolyhedron.cpp
+++ b/mlir/lib/Analysis/Presburger/IntegerPolyhedron.cpp
@@ -111,18 +111,6 @@ IntegerPolyhedron::findIntegerLexMin() const {
   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 @@ unsigned IntegerPolyhedron::insertId(IdKind kind, unsigned pos, unsigned num) {
   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<int64_t> eq) {
@@ -1044,8 +1019,8 @@ void IntegerPolyhedron::mergeLocalIds(IntegerPolyhedron &other) {
   // 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<SmallVector<int64_t, 8>> divsA, divsB;
@@ -1133,7 +1108,7 @@ void IntegerPolyhedron::convertDimToLocal(unsigned dimStart,
   // 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 @@ void IntegerPolyhedron::addLocalFloorDiv(ArrayRef<int64_t> dividend,
   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<int64_t, 8> bound(dividend.size() + 1);

diff  --git a/mlir/lib/Analysis/Presburger/PresburgerSet.cpp b/mlir/lib/Analysis/Presburger/PresburgerSet.cpp
index c213bf5eb5e6b..355f0d4eb1c72 100644
--- a/mlir/lib/Analysis/Presburger/PresburgerSet.cpp
+++ b/mlir/lib/Analysis/Presburger/PresburgerSet.cpp
@@ -179,8 +179,7 @@ static void subtractRecursively(IntegerPolyhedron &b, Simplex &simplex,
   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
index 5ac69e97232bd..c3cbbd832cab1 100644
--- a/mlir/lib/Dialect/Affine/Analysis/AffineStructures.cpp
+++ b/mlir/lib/Dialect/Affine/Analysis/AffineStructures.cpp
@@ -362,14 +362,14 @@ areIdsUnique(const FlatAffineConstraints &cst) {
 
 /// 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
index cdd1fdfa37a57..2dd168664ab94 100644
--- a/mlir/unittests/Analysis/Presburger/IntegerPolyhedronTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/IntegerPolyhedronTest.cpp
@@ -180,17 +180,17 @@ TEST(IntegerPolyhedronTest, removeIdRange) {
   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 @@ TEST(IntegerPolyhedronTest, mergeDivisionsSimple) {
     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 @@ TEST(IntegerPolyhedronTest, mergeDivisionsSimple) {
     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
index 01be97a5d9768..628ec070d9b59 100644
--- 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 @@ TEST(PresburgerSpaceTest, insertIdSet) {
   // 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);
 }
 


        


More information about the Mlir-commits mailing list