This is an archive of the discontinued LLVM Phabricator instance.

[MLIR][Presburger] Refactor looping strategy in coalesce
ClosedPublic

Authored by webmiche on Feb 23 2022, 12:42 AM.

Details

Summary

This patch refactors the looping strategy of coalesce for future patches. The new strategy works in-place and uses IneqType to organize inequalities into vectors of the same type. Future coalesce cases will pattern match on this organization. E.g. the contained case needs all inequalities and equalities to be redundant, so this case becomes checking whether the respective vectors are empty. For other cases, the patterns consider the types of all inequalities of both sets making it wasteful to only consider whether a can be coalesced with b in one step, as inequalities would need to be typed again for the opposite case. Therefore, the new strategy tries to coalesce a with b and b with a in a single step.

Diff Detail

Event Timeline

webmiche created this revision.Feb 23 2022, 12:42 AM
webmiche requested review of this revision.Feb 23 2022, 12:42 AM
Groverkss requested changes to this revision.Feb 23 2022, 6:34 AM

Instead of creating a new Simplex object each time you do a coalescePair call, can you instead precompute them and only recompute if two polyhedrons get coalesced? This should reduce the number of times you compute Simplex from n^2 to at max 2n. AFAIU creating a Simplex object is the most expensive operation in coalescing so this is useful to do.

mlir/lib/Analysis/Presburger/PresburgerSet.cpp
557

Could you write a comment that since a polyhedron may have been coalesced, polyhedrons.size() can change after an iteration.

558

For this loop, polyhedrons.size() doesn't change since you break if it changes. You can use:

for (unsigned j = i + 1, e = polyhedrons.size(); j < e; ++j)

instead.

560

This will overflow if i = 0 right?

This revision now requires changes to proceed.Feb 23 2022, 6:34 AM

Thanks for this patch.

Please add some tests.

mlir/lib/Analysis/Presburger/PresburgerSet.cpp
503–504

I think "Erases polyhedrons[i] and polyhedrons[j] and appends poly to the vector`" would be more understandable

508–514

Why can't you append poly directly? Also, local ids are missing.

516

Please write a comment for this

516

const &p?

519

nit: can just use i here

519
521

You can return LogicalResult

531–536

You can just erase the empty poly here.

541–549

isRedundant* perform pivots, so are expensive. Early exit the loop as soon as onlyRedundantEqs becomes false.

545–550

If using the simplex array, you can use that here. Also, note in the comment that this only happens when there is exactly one empty poly left, since if there were two empty left one of them will get removed as a subset of the other.

551–554

I can't see a usecase for not coalescing in-place. I would just call it coalesceInPlace and do all the operations directly on integerPolyhedrons. We can write a function coalesce() { newSet = *this; newSet.coalesceInPlace(); return newSet; } if someone really wants a copy returned.

arjunp added inline comments.Feb 24 2022, 1:03 AM
mlir/lib/Analysis/Presburger/PresburgerSet.cpp
551–554

But let's leave that for a different patch. Non-in-place might be useful when this is const I guess. The above coalesce would be able to be const.

webmiche updated this revision to Diff 411066.Feb 24 2022, 3:49 AM

I implemented a slightly different looping strategy that now does not shorten the polyhedron vector, but rather replaces elements and keeps track of which ones are actually part of the polytope. Additionally, I implemented the vector of simplices and addressed comments.

webmiche marked 10 inline comments as done.Feb 24 2022, 3:50 AM
webmiche added inline comments.Feb 24 2022, 3:56 AM
mlir/lib/Analysis/Presburger/PresburgerSet.cpp
541–549

I do early exit here: k < e && onlyRedundantEqsA. If you want, I can make this part of the body rather than part of the for.

arjunp added inline comments.Feb 24 2022, 4:07 AM
mlir/lib/Analysis/Presburger/PresburgerSet.cpp
541–549

Oops, I missed that. Yeah, putting it in the body might make it easier to spot.

Groverkss added inline comments.Feb 24 2022, 4:23 AM
mlir/lib/Analysis/Presburger/PresburgerSet.cpp
518–524

Please write a comment on what exactly this does. Also, a comment that simplices and polyhedrons are guaranteed to be at least of size 2.

590

You can check for rational emptiness here only. After this, you don't need to check that since coalesce of two non empty polyhedrons should not be empty.

594
596
606

For now, can you just iterate from j=0 to n - coalescing? I'm not convinced that this covers all cases and may cause problems later. A TODO comment for this would be good and we can discuss this in a later patch.

609

n - coalescing does not change here so you can do e = n-coalescing, j < e.

webmiche updated this revision to Diff 411085.Feb 24 2022, 5:02 AM

Removed the addCoalescedPolyhedron function as currently only erasing is needed. Furthermore, addressed remaining comments.

webmiche marked 10 inline comments as done.Feb 24 2022, 5:03 AM
grosser added inline comments.Feb 24 2022, 5:05 AM
mlir/lib/Analysis/Presburger/PresburgerSet.cpp
539

newly ???

It seems there is a very missing in the last subclause of this sentence.

webmiche updated this revision to Diff 411099.Feb 24 2022, 5:38 AM

The IntegerPolyhedra at the end of the vector are not needed anymore, so they are erased now.

webmiche added inline comments.Feb 24 2022, 5:40 AM
mlir/lib/Analysis/Presburger/PresburgerSet.cpp
539

Is "the IntegerPolyhedron now at position i is not compared" more understandable?

arjunp added inline comments.Feb 24 2022, 5:50 AM
mlir/lib/Analysis/Presburger/PresburgerSet.cpp
429

assert that polyhedrons and simplices have the same length

430

You can use polyhedrons.back()

532

And similarly for simplices

webmiche updated this revision to Diff 411113.Feb 24 2022, 6:59 AM

addressed remaining comments

webmiche updated this revision to Diff 411115.Feb 24 2022, 7:06 AM
webmiche marked 4 inline comments as done.

include the commit that actually adresses the comments ;)

webmiche updated this revision to Diff 411242.Feb 24 2022, 3:04 PM

added a testcase with multiple coalescings happening

arjunp accepted this revision.Feb 24 2022, 9:22 PM

LGTM, please address minor comments below.

mlir/include/mlir/Analysis/Presburger/Simplex.h
311 ↗(On Diff #411115)

Can you send this as a separate patch? :)

mlir/lib/Analysis/Presburger/PresburgerSet.cpp
413–419

Drop braces for single-line if statements

484–487

drop braces

webmiche updated this revision to Diff 411331.Feb 24 2022, 11:40 PM

Addressed minor comments and moved the change in Simplex.h to another patch (that already landed).

webmiche marked 2 inline comments as done.Feb 24 2022, 11:41 PM
Groverkss accepted this revision.Feb 25 2022, 2:03 AM
This revision is now accepted and ready to land.Feb 25 2022, 2:03 AM
This revision was landed with ongoing or failed builds.Feb 25 2022, 4:41 AM
This revision was automatically updated to reflect the committed changes.