[llvm-branch-commits] [mlir] [mlir][Presburger] Add PresburgerRelation::gist (PR #235958)

Jeremy Kun via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Nov 2 20:57:46 PST 2026


https://github.com/j2kun updated https://github.com/llvm/llvm-project/pull/235958

>From 1c7f494350f3d0c79e6ce6c1efea409eea5a3c2a Mon Sep 17 00:00:00 2001
From: Jeremy Kun <jkun at google.com>
Date: Mon, 26 Oct 2026 12:20:21 -0700
Subject: [PATCH] [mlir][Presburger] Add PresburgerRelation::gist

Add PresburgerRelation::gist (and PresburgerSet forwarding overloads)
for a convex context and for a context that is itself a union of
disjuncts, following ISL's isl_map_gist / isl_map_gist_basic_map.

For a convex context, each disjunct is simplified separately with
IntegerRelation::gist; disjuncts that become empty are dropped and if any
disjunct becomes the universe, the universe is returned.

For a non-convex context, the trivial cases (universe set or context,
empty context, equal set and context, context being a subset of the
set) are handled first. Otherwise, the context is approximated by a
convex set. Instead of ISL's unshifted simple hull of the context and the
set, we use the universe together with all constraints of the disjuncts
of the context and of the set that are valid for every disjunct of the
context (checked with Simplex); including the constraints of the set
allows removing constraints that are implied by every disjunct of the
context even if they are not implied by the simple hull.
---
 .../Analysis/Presburger/PresburgerRelation.h  |  23 +++
 .../Presburger/PresburgerRelation.cpp         | 122 +++++++++++++++
 .../Presburger/PresburgerRelationTest.cpp     |  39 +++++
 .../Analysis/Presburger/PresburgerSetTest.cpp | 142 ++++++++++++++++++
 4 files changed, 326 insertions(+)

diff --git a/mlir/include/mlir/Analysis/Presburger/PresburgerRelation.h b/mlir/include/mlir/Analysis/Presburger/PresburgerRelation.h
index 8c014f0007eca4..a6e33a35a20138 100644
--- a/mlir/include/mlir/Analysis/Presburger/PresburgerRelation.h
+++ b/mlir/include/mlir/Analysis/Presburger/PresburgerRelation.h
@@ -217,6 +217,27 @@ class PresburgerRelation {
   /// redundencies.
   PresburgerRelation simplify() const;
 
+  /// Return a relation that has the same intersection with `context` as `this`
+  /// but is as simple as possible (ISL's "gist", see `isl_map_gist_basic_map`);
+  /// see `IntegerRelation::gist` for the simplification of a single disjunct.
+  /// Each disjunct is simplified separately in the context; disjuncts that
+  /// become empty are dropped, and if any disjunct becomes the universe, the
+  /// universe is returned (a deliberate deviation from
+  /// `isl_map_gist_basic_map`, which keeps the other disjuncts). An obviously
+  /// empty context yields the universe. The result is only meaningful together
+  /// with `context`.
+  PresburgerRelation gist(const IntegerRelation &context) const;
+
+  /// Same as above, but for a context that is a union of disjuncts (ISL's
+  /// `isl_map_gist`). A context with a single disjunct is used directly.
+  /// Otherwise, `this` is simplified in the context of a convex
+  /// over-approximation of `context` that consists of all the constraints of
+  /// the disjuncts of `context` and of `this` that are valid for every
+  /// disjunct of `context`. This replaces ISL's
+  /// `isl_map_unshifted_simple_hull_from_map_list`: the constraints are not
+  /// shifted, but checked for validity with a Simplex per disjunct.
+  PresburgerRelation gist(const PresburgerRelation &context) const;
+
   /// Return whether the given PresburgerRelation is full-dimensional. By full-
   /// dimensional we mean that it is not flat along any dimension.
   bool isFullDim() const;
@@ -263,6 +284,8 @@ class PresburgerSet : public PresburgerRelation {
   PresburgerSet complement() const;
   PresburgerSet subtract(const PresburgerRelation &set) const;
   PresburgerSet coalesce() const;
+  PresburgerSet gist(const IntegerRelation &context) const;
+  PresburgerSet gist(const PresburgerRelation &context) const;
 
 protected:
   /// Construct an empty PresburgerRelation with the specified number of
diff --git a/mlir/lib/Analysis/Presburger/PresburgerRelation.cpp b/mlir/lib/Analysis/Presburger/PresburgerRelation.cpp
index 5dceeb35e8345a..e9ef7c34e2f788 100644
--- a/mlir/lib/Analysis/Presburger/PresburgerRelation.cpp
+++ b/mlir/lib/Analysis/Presburger/PresburgerRelation.cpp
@@ -1042,6 +1042,120 @@ PresburgerRelation PresburgerRelation::simplify() const {
   return result;
 }
 
+PresburgerRelation
+PresburgerRelation::gist(const IntegerRelation &context) const {
+  assert(space.isCompatible(context.getSpace()) && "Spaces should match");
+  // Anything is the universe in an obviously empty context.
+  if (context.isObviouslyEmpty())
+    return getUniverse(getSpace());
+
+  PresburgerRelation result(getSpace());
+  for (const IntegerRelation &disjunct : disjuncts) {
+    IntegerRelation simplified = disjunct;
+    simplified.gist(context);
+    if (simplified.isObviouslyEmpty())
+      continue;
+    // If a disjunct becomes the universe, so does the whole union.
+    if (simplified.getNumConstraints() == 0)
+      return getUniverse(getSpace());
+    result.unionInPlace(simplified);
+  }
+  return result;
+}
+
+/// Returns a convex over-approximation of `context`, which must not be
+/// obviously empty, to be used as the context for simplifying `rel`: the
+/// universe together with those constraints of the disjuncts of `context` and
+/// of `rel` that do not involve local variables and that hold in every disjunct
+/// of `context`. Including the constraints of `rel` allows constraints of `rel`
+/// to be simplified away that are implied by every disjunct of the context but
+/// not by its simple hull. This follows ISL's
+/// isl_map_unshifted_simple_hull_from_map_list, which is used by isl_map_gist.
+static IntegerRelation
+getValidConstraintsHull(const PresburgerRelation &context,
+                        const PresburgerRelation &rel) {
+  const PresburgerSpace &space = context.getSpace();
+  assert(space.getNumLocalVars() == 0 && "space should not have locals");
+  unsigned numVars = space.getNumVars();
+
+  // A Simplex for each non-empty disjunct of the context.
+  SmallVector<Simplex, 2> simplices;
+  for (const IntegerRelation &disjunct : context.getAllDisjuncts()) {
+    simplices.emplace_back(disjunct);
+    if (simplices.back().isEmpty())
+      simplices.pop_back();
+  }
+  if (simplices.empty())
+    return IntegerRelation::getEmpty(space);
+
+  // Check whether the constraint with coefficients `coeffs` over the non-local
+  // variables holds in every disjunct of the context.
+  SmallVector<DynamicAPInt, 8> padded;
+  auto isValid = [&](ArrayRef<DynamicAPInt> coeffs, bool isEq) {
+    for (Simplex &simplex : simplices) {
+      padded.assign(coeffs.begin(), coeffs.end() - 1);
+      padded.append(simplex.getNumVariables() - numVars, DynamicAPInt(0));
+      padded.emplace_back(coeffs.back());
+      if (isEq ? !simplex.isRedundantEquality(padded)
+               : !simplex.isRedundantInequality(padded))
+        return false;
+    }
+    return true;
+  };
+
+  IntegerRelation hull = IntegerRelation::getUniverse(space);
+  SmallVector<DynamicAPInt, 8> candidate;
+  auto addCandidates = [&](const IntegerRelation &disjunct) {
+    unsigned numLocals = disjunct.getNumLocalVars();
+    // Returns false if the constraint involves local variables; otherwise,
+    // stores the constraint without the local columns in `candidate`.
+    auto extract = [&](ArrayRef<DynamicAPInt> coeffs) {
+      if (llvm::any_of(coeffs.slice(numVars, numLocals),
+                       [](const DynamicAPInt &c) { return c != 0; }))
+        return false;
+      candidate.assign(coeffs.begin(), coeffs.begin() + numVars);
+      candidate.emplace_back(coeffs.back());
+      return true;
+    };
+    for (unsigned i = 0, e = disjunct.getNumEqualities(); i < e; ++i)
+      if (extract(disjunct.getEquality(i)) && isValid(candidate, /*isEq=*/true))
+        hull.addEquality(candidate);
+    for (unsigned i = 0, e = disjunct.getNumInequalities(); i < e; ++i)
+      if (extract(disjunct.getInequality(i)) &&
+          isValid(candidate, /*isEq=*/false))
+        hull.addInequality(candidate);
+  };
+  for (const IntegerRelation &disjunct : context.getAllDisjuncts())
+    addCandidates(disjunct);
+  for (const IntegerRelation &disjunct : rel.getAllDisjuncts())
+    addCandidates(disjunct);
+
+  hull.simplify();
+  return hull;
+}
+
+PresburgerRelation
+PresburgerRelation::gist(const PresburgerRelation &context) const {
+  assert(space.isCompatible(context.getSpace()) && "Spaces should match");
+
+  // Nothing can be simplified in the universe or with a universe context.
+  if (isObviouslyUniverse() || context.isObviouslyUniverse())
+    return *this;
+  // Anything is the universe in an empty context.
+  if (context.isObviouslyEmpty() || isObviouslyEqual(context))
+    return getUniverse(getSpace());
+  // With multiple disjuncts, the per-disjunct simplification may not be able
+  // to detect that the context is a subset of `this`, in which case all
+  // constraints can be removed.
+  if ((getNumDisjuncts() != 1 || context.getNumDisjuncts() != 1) &&
+      hasOnlyDivLocals() && context.isSubsetOf(*this))
+    return getUniverse(getSpace());
+
+  if (context.getNumDisjuncts() == 1)
+    return gist(context.getDisjunct(0));
+  return gist(getValidConstraintsHull(context, *this));
+}
+
 bool PresburgerRelation::isFullDim() const {
   return llvm::any_of(getAllDisjuncts(), [](IntegerRelation disjunct) {
     return disjunct.isFullDim();
@@ -1095,3 +1209,11 @@ PresburgerSet PresburgerSet::subtract(const PresburgerRelation &set) const {
 PresburgerSet PresburgerSet::coalesce() const {
   return PresburgerSet(PresburgerRelation::coalesce());
 }
+
+PresburgerSet PresburgerSet::gist(const IntegerRelation &context) const {
+  return PresburgerSet(PresburgerRelation::gist(context));
+}
+
+PresburgerSet PresburgerSet::gist(const PresburgerRelation &context) const {
+  return PresburgerSet(PresburgerRelation::gist(context));
+}
diff --git a/mlir/unittests/Analysis/Presburger/PresburgerRelationTest.cpp b/mlir/unittests/Analysis/Presburger/PresburgerRelationTest.cpp
index ad71bb32a06880..aa0730fa41154b 100644
--- a/mlir/unittests/Analysis/Presburger/PresburgerRelationTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/PresburgerRelationTest.cpp
@@ -316,3 +316,42 @@ TEST(PresburgerRelationTest, convertVarKind) {
   // Check if identifiers are transferred correctly.
   EXPECT_TRUE(expectedRel.isEqual(rel));
 }
+
+/// The relation and context used by the gist tests below.
+static PresburgerRelation getGistTestRelation() {
+  return parsePresburgerRelationFromPresburgerSet(
+      {"(x, y) : (y - x - 1 >= 0, x >= 0, -x + 10 >= 0)",
+       "(x, y) : (x - 20 >= 0, -x + 30 >= 0, y >= 0)"},
+      1);
+}
+
+static PresburgerRelation getGistTestContext() {
+  return parsePresburgerRelationFromPresburgerSet({"(x, y) : (x >= 0)"}, 1);
+}
+
+TEST(PresburgerRelationTest, gistPreservesDomainAndRange) {
+  PresburgerRelation rel = getGistTestRelation();
+  PresburgerRelation context = getGistTestContext();
+  PresburgerRelation gist = rel.gist(context);
+
+  EXPECT_TRUE(gist.intersect(context).isEqual(rel.intersect(context)));
+  EXPECT_TRUE(gist.gist(context).isEqual(gist));
+  EXPECT_TRUE(gist.isEqual(parsePresburgerRelationFromPresburgerSet(
+      {"(x, y) : (y - x - 1 >= 0, -x + 10 >= 0)",
+       "(x, y) : (x - 20 >= 0, -x + 30 >= 0, y >= 0)"},
+      1)));
+  EXPECT_EQ(gist.getNumDomainVars(), 1u);
+  EXPECT_EQ(gist.getNumRangeVars(), 1u);
+  ASSERT_EQ(gist.getNumDisjuncts(), 2u);
+  EXPECT_EQ(gist.getDisjunct(0).getNumConstraints(), 2u);
+  EXPECT_EQ(gist.getDisjunct(1).getNumConstraints(), 3u);
+}
+
+TEST(PresburgerRelationTest, gistConvexContextMatchesUnionContext) {
+  PresburgerRelation rel = getGistTestRelation();
+  PresburgerRelation context = getGistTestContext();
+  PresburgerRelation gist = rel.gist(context.getDisjunct(0));
+  EXPECT_TRUE(gist.intersect(context).isEqual(rel.intersect(context)));
+  EXPECT_TRUE(gist.gist(context.getDisjunct(0)).isEqual(gist));
+  EXPECT_TRUE(gist.isEqual(rel.gist(context)));
+}
diff --git a/mlir/unittests/Analysis/Presburger/PresburgerSetTest.cpp b/mlir/unittests/Analysis/Presburger/PresburgerSetTest.cpp
index c89b528bc7f7b8..72e71388be5058 100644
--- a/mlir/unittests/Analysis/Presburger/PresburgerSetTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/PresburgerSetTest.cpp
@@ -882,3 +882,145 @@ TEST(SetTest, subtractOutputSizeRegression) {
   // sets, which is correct, but bad for output size.
   EXPECT_EQ(subtractSelf.getNumDisjuncts(), 0u);
 }
+
+/// Computes the gist of `set` in the context of `context`, checks that the
+/// gist has the same intersection with the context as `set`, that gist is
+/// idempotent (the gist of the result is equal to the result and has the same
+/// number of disjuncts), and, if given, that the result is equal to
+/// `expected`. Returns the gist.
+static PresburgerSet
+expectGist(const PresburgerSet &set, const PresburgerSet &context,
+           std::optional<PresburgerSet> expected = std::nullopt) {
+  PresburgerSet gist = set.gist(context);
+  EXPECT_TRUE(gist.intersect(context).isEqual(set.intersect(context)));
+  PresburgerSet gistAgain = gist.gist(context);
+  EXPECT_TRUE(gistAgain.isEqual(gist));
+  EXPECT_EQ(gistAgain.getNumDisjuncts(), gist.getNumDisjuncts());
+  if (expected)
+    EXPECT_TRUE(gist.isEqual(*expected));
+  return gist;
+}
+
+static PresburgerSet
+expectGist(ArrayRef<StringRef> set, ArrayRef<StringRef> context,
+           std::optional<PresburgerSet> expected = std::nullopt) {
+  return expectGist(parsePresburgerSet(set), parsePresburgerSet(context),
+                    expected);
+}
+
+static PresburgerSet universeSet(unsigned numDims) {
+  return PresburgerSet::getUniverse(PresburgerSpace::getSetSpace(numDims));
+}
+
+static PresburgerSet emptySet(unsigned numDims) {
+  return PresburgerSet::getEmpty(PresburgerSpace::getSetSpace(numDims));
+}
+
+TEST(SetTest, gistUnionSubsetContextYieldsUniverse) {
+  // From the ISL test suite. The context is a subset of the set.
+  expectGist(
+      {"(x) : (x >= 0, -x + 4 >= 0)", "(x) : (x - 6 >= 0, -x + 9 >= 0)"},
+      {"(x) : (x - 1 >= 0, -x + 3 >= 0)", "(x) : (x - 7 >= 0, -x + 8 >= 0)"},
+      universeSet(1));
+}
+
+TEST(SetTest, gistUnionConvexSubsetContextYieldsUniverse) {
+  // From the ISL test suite. The convex context is a subset of the union.
+  expectGist({"(x, y) : (-x - 1 >= 0, y >= 0, -y + 4 >= 0)",
+              "(x, y) : (x + 2 >= 0, x + y >= 0, x - y + 10 >= 0)"},
+             {"(x, y) : (y - 1 >= 0, -y + 3 >= 0)"}, universeSet(2));
+}
+
+TEST(SetTest, gistUnionConstraintValidInEveryContextDisjunct) {
+  // From the ISL test suite. The constraint of the set is valid in each
+  // disjunct of the context, but not in the simple hull of the context.
+  expectGist({"(m, n, a, b) : (-a + n + 2147 >= 0)"},
+             {"(m, n, a, b) : (m - 1 >= 0, n - 1 >= 0, -a - m + 2148 >= 0, "
+              "-b - n + 2148 >= 0, b >= 0, b + n + a - 2149 >= 0)",
+              "(m, n, a, b) : (n - 1 >= 0, a >= 0, -b - n - a + 2148 >= 0, "
+              "b >= 0)"},
+             universeSet(4));
+}
+
+TEST(SetTest, gistUnionPerDisjunct) {
+  // Each disjunct of the set is simplified separately.
+  PresburgerSet gist = expectGist(
+      {"(x) : (x >= 0, -x + 10 >= 0)", "(x) : (x - 20 >= 0, -x + 30 >= 0)"},
+      {"(x) : (x >= 0)"},
+      parsePresburgerSet(
+          {"(x) : (-x + 10 >= 0)", "(x) : (x - 20 >= 0, -x + 30 >= 0)"}));
+  ASSERT_EQ(gist.getNumDisjuncts(), 2u);
+  EXPECT_EQ(gist.getDisjunct(0).getNumConstraints(), 1u);
+  EXPECT_EQ(gist.getDisjunct(1).getNumConstraints(), 2u);
+}
+
+TEST(SetTest, gistUnionDropsEmptyDisjunct) {
+  // Disjuncts that do not intersect the context are dropped.
+  PresburgerSet gist = expectGist(
+      {"(x) : (x >= 0, -x + 10 >= 0)", "(x) : (x - 20 >= 0, -x + 30 >= 0)"},
+      {"(x) : (-x + 5 >= 0)"}, parsePresburgerSet({"(x) : (x >= 0)"}));
+  EXPECT_EQ(gist.getNumDisjuncts(), 1u);
+}
+
+TEST(SetTest, gistUnionHullFromMultiDisjunctContext) {
+  // Context with multiple disjuncts: the hull of the context implies
+  // 1 <= x <= 8.
+  PresburgerSet gist =
+      expectGist({"(x, y) : (x >= 0, -x + 10 >= 0, -y + 5 >= 0)"},
+                 {"(x, y) : (x - 1 >= 0, -x + 3 >= 0, y >= 0)",
+                  "(x, y) : (x - 6 >= 0, -x + 8 >= 0, y - 1 >= 0)"},
+                 parsePresburgerSet({"(x, y) : (-y + 5 >= 0)"}));
+  ASSERT_EQ(gist.getNumDisjuncts(), 1u);
+  EXPECT_EQ(gist.getDisjunct(0).getNumConstraints(), 1u);
+}
+
+TEST(SetTest, gistUnionSimplifiesOnlyAffectedDisjunct) {
+  expectGist({"(i, j) : (i - 1 >= 0, j - 1 >= 0)",
+              "(i, j) : (i - 2 >= 0, -j + 10 >= 0)"},
+             {"(i, j) : (i - 1 >= 0)"},
+             parsePresburgerSet({"(i, j) : (j - 1 >= 0)",
+                                 "(i, j) : (i - 2 >= 0, -j + 10 >= 0)"}));
+}
+
+TEST(SetTest, gistEmptySetStaysEmpty) {
+  PresburgerSet gist = expectGist(
+      emptySet(1), parsePresburgerSet({"(x) : (x >= 0, -x + 10 >= 0)"}));
+  EXPECT_TRUE(gist.isObviouslyEmpty());
+}
+
+TEST(SetTest, gistEmptyContextYieldsUniverse) {
+  expectGist(parsePresburgerSet({"(x) : (x >= 0, -x + 10 >= 0)"}), emptySet(1),
+             universeSet(1));
+}
+
+TEST(SetTest, gistUniverseStaysUniverse) {
+  expectGist(universeSet(1),
+             parsePresburgerSet({"(x) : (x >= 0, -x + 10 >= 0)"}),
+             universeSet(1));
+}
+
+TEST(SetTest, gistUniverseContextKeepsSet) {
+  PresburgerSet set = parsePresburgerSet({"(x) : (x >= 0, -x + 10 >= 0)"});
+  expectGist(set, universeSet(1), set);
+}
+
+TEST(SetTest, gistSelfYieldsUniverse) {
+  PresburgerSet set = parsePresburgerSet({"(x) : (x >= 0, -x + 10 >= 0)"});
+  expectGist(set, set, universeSet(1));
+}
+
+TEST(SetTest, gistUnionAllEmptyContextDisjunctsYieldsUniverse) {
+  // A context whose disjuncts are all (obviously) empty is empty.
+  PresburgerSet emptyDisjuncts = parsePresburgerSet(
+      {"(x) : (x - 1 >= 0, -x >= 0)", "(x) : (x - 1 == 0, x - 2 == 0)"});
+  expectGist(parsePresburgerSet({"(x) : (x >= 0, -x + 10 >= 0)"}),
+             emptyDisjuncts, universeSet(1));
+}
+
+TEST(SetTest, gistEmptySetInEmptyConvexContextYieldsUniverse) {
+  // The empty set in an empty convex context is the universe.
+  EXPECT_TRUE(
+      emptySet(1)
+          .gist(IntegerRelation::getEmpty(PresburgerSpace::getSetSpace(1)))
+          .isEqual(universeSet(1)));
+}



More information about the llvm-branch-commits mailing list