-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[MLIR][Presburger] Add simplify function #69107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
670abc7
6333325
5e8ffab
cf459cd
92d39d5
900dae0
2167004
333591a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,19 +83,19 @@ void PresburgerRelation::unionInPlace(const IntegerRelation &disjunct) { | |
void PresburgerRelation::unionInPlace(const PresburgerRelation &set) { | ||
assert(space.isCompatible(set.getSpace()) && "Spaces should match"); | ||
|
||
if (isPlainEqual(set)) | ||
if (isObviouslyEqual(set)) | ||
return; | ||
|
||
if (isPlainEmpty()) { | ||
if (isObviouslyEmpty()) { | ||
disjuncts = set.disjuncts; | ||
return; | ||
} | ||
if (set.isPlainEmpty()) | ||
if (set.isObviouslyEmpty()) | ||
return; | ||
|
||
if (isPlainUniverse()) | ||
if (isObviouslyUniverse()) | ||
return; | ||
if (set.isPlainUniverse()) { | ||
if (set.isObviouslyUniverse()) { | ||
disjuncts = set.disjuncts; | ||
return; | ||
} | ||
|
@@ -144,10 +144,10 @@ PresburgerRelation::intersect(const PresburgerRelation &set) const { | |
|
||
// If the set is empty or the other set is universe, | ||
// directly return the set | ||
if (isPlainEmpty() || set.isPlainUniverse()) | ||
if (isObviouslyEmpty() || set.isObviouslyUniverse()) | ||
return *this; | ||
|
||
if (set.isPlainEmpty() || isPlainUniverse()) | ||
if (set.isObviouslyEmpty() || isObviouslyUniverse()) | ||
return set; | ||
|
||
PresburgerRelation result(getSpace()); | ||
|
@@ -576,6 +576,9 @@ static PresburgerRelation getSetDifference(IntegerRelation b, | |
} | ||
} | ||
|
||
// Try to simplify the results. | ||
result = result.simplify(); | ||
|
||
return result; | ||
} | ||
|
||
|
@@ -593,7 +596,7 @@ PresburgerRelation::subtract(const PresburgerRelation &set) const { | |
|
||
// If we know that the two sets are clearly equal, we can simply return the | ||
// empty set. | ||
if (isPlainEqual(set)) | ||
if (isObviouslyEqual(set)) | ||
return result; | ||
|
||
// We compute (U_i t_i) \ (U_i set_i) as U_i (t_i \ V_i set_i). | ||
|
@@ -615,7 +618,7 @@ bool PresburgerRelation::isEqual(const PresburgerRelation &set) const { | |
return this->isSubsetOf(set) && set.isSubsetOf(*this); | ||
} | ||
|
||
bool PresburgerRelation::isPlainEqual(const PresburgerRelation &set) const { | ||
bool PresburgerRelation::isObviouslyEqual(const PresburgerRelation &set) const { | ||
if (!space.isCompatible(set.getSpace())) | ||
return false; | ||
|
||
|
@@ -625,7 +628,7 @@ bool PresburgerRelation::isPlainEqual(const PresburgerRelation &set) const { | |
// Compare each disjunct in this PresburgerRelation with the corresponding | ||
// disjunct in the other PresburgerRelation. | ||
for (unsigned int i = 0, n = getNumDisjuncts(); i < n; ++i) { | ||
if (!getDisjunct(i).isPlainEqual(set.getDisjunct(i))) | ||
if (!getDisjunct(i).isObviouslyEqual(set.getDisjunct(i))) | ||
return false; | ||
} | ||
return true; | ||
|
@@ -635,18 +638,22 @@ bool PresburgerRelation::isPlainEqual(const PresburgerRelation &set) const { | |
/// otherwise. It is a simple check that only check if the relation has at least | ||
/// one unconstrained disjunct, indicating the absence of constraints or | ||
/// conditions. | ||
bool PresburgerRelation::isPlainUniverse() const { | ||
return llvm::any_of(getAllDisjuncts(), [](const IntegerRelation &disjunct) { | ||
return disjunct.getNumConstraints() == 0; | ||
}); | ||
bool PresburgerRelation::isObviouslyUniverse() const { | ||
for (const IntegerRelation &disjunct : getAllDisjuncts()) { | ||
if (disjunct.getNumConstraints() == 0) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool PresburgerRelation::isConvexNoLocals() const { | ||
return getNumDisjuncts() == 1 && getSpace().getNumLocalVars() == 0; | ||
} | ||
|
||
/// Return true if there is no disjunct, false otherwise. | ||
bool PresburgerRelation::isPlainEmpty() const { return getNumDisjuncts() == 0; } | ||
bool PresburgerRelation::isObviouslyEmpty() const { | ||
return getNumDisjuncts() == 0; | ||
} | ||
|
||
/// Return true if all the sets in the union are known to be integer empty, | ||
/// false otherwise. | ||
|
@@ -1015,6 +1022,17 @@ bool PresburgerRelation::hasOnlyDivLocals() const { | |
}); | ||
} | ||
|
||
PresburgerRelation PresburgerRelation::simplify() const { | ||
PresburgerRelation origin = *this; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we make a copy here? Can we use &origin = *this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The simplify function is currently a const member function, and now the simplify function of IntegerRelation is a non-const member function, in order to be able to call its simplify so there is a copy, here how to change will be more appropriate? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense, its fine for now |
||
PresburgerRelation result = PresburgerRelation(getSpace()); | ||
for (IntegerRelation &disjunct : origin.disjuncts) { | ||
disjunct.simplify(); | ||
if (!disjunct.isObviouslyEmpty()) | ||
result.unionInPlace(disjunct); | ||
} | ||
return result; | ||
} | ||
|
||
void PresburgerRelation::print(raw_ostream &os) const { | ||
os << "Number of Disjuncts: " << getNumDisjuncts() << "\n"; | ||
for (const IntegerRelation &disjunct : disjuncts) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use our own hash function for the DenseMap where -hash means the negative of the inequality.