This is an archive of the discontinued LLVM Phabricator instance.

[MLIR] FlatAffineConstraints: Fixed bug where some divisions were not being detected
ClosedPublic

Authored by Groverkss on Jul 31 2021, 12:54 AM.

Details

Summary

This patch fixes a bug in the existing implementation of detectAsFloorDiv,
where floordivs with numerator with non-zero constant term and floordivs with
numerator only consisting of a constant term were not being detected.

Diff Detail

Event Timeline

Groverkss created this revision.Jul 31 2021, 12:54 AM
Groverkss requested review of this revision.Jul 31 2021, 12:54 AM
Groverkss updated this revision to Diff 363279.Jul 31 2021, 1:52 AM
  • Added test for bugfix

Thanks for the patch! Please fix the clang-format errors. Other than that, this seems good to me. Let's wait for one of the others to take a look as well.

mlir/lib/Analysis/AffineStructures.cpp
1556

This now also allows numerators which are just zero, right? I think it makes sense to support that (and it will be needed to support subtraction on sets with divisions that happen to have numerator zero), but maybe it's worth mentioning in the commit message.

Groverkss updated this revision to Diff 363338.Aug 1 2021, 3:17 AM
Groverkss marked an inline comment as done.
  • Fixed clang-tidy errors and add punctuation in some comments
Groverkss edited the summary of this revision. (Show Details)Aug 1 2021, 3:24 AM

Addressed comments.

vinayaka-polymage requested changes to this revision.Aug 1 2021, 10:45 PM

Thanks for this enhancement!

  1. I tried an example, but didn't get the expected result. Can you please clarify ?
  2. Format the commit msg to wrap around.
  3. If my understanding of (1) is correct, can you also extend the doc comment of the function to use an example that has non-zero constant upper bound ?
mlir/lib/Analysis/AffineStructures.cpp
1561

This would be incorrect, I think. You would need to still initialize the expr with zero. I tried the example:
4q - 1 <= i + j <= 4q + 2.

Finally, the dividend should be i + j + 1, but I think it would detect i + j + 2 now.

On a side note, it means, testing is insufficient as it is only checking if all ids are detected, but not verifying the results.

This revision now requires changes to proceed.Aug 1 2021, 10:45 PM
Groverkss added inline comments.Aug 2 2021, 1:40 AM
mlir/lib/Analysis/AffineStructures.cpp
1561

The detected dividend will be i + j + 1.

The upper-bound inequality is:

4q <= i + j + 1

rearranging it we get:

i + j - 4q + 1 >= 0

From which, the constant term extracted is 1. Is there something I'm missing?

Groverkss edited the summary of this revision. (Show Details)Aug 2 2021, 1:42 AM

Thanks for the clarification, will take another look in some time.

mlir/lib/Analysis/AffineStructures.cpp
1561

I just noticed that the loop below runs 0 to cst.getNumCols() - 1; this means the constant term is not added once again. So, you are correct.

I was under the impression that the loop is 0 to cst.getNumCols(), and the constant 1 in the above case would get added once again. So, this works too.

Groverkss updated this revision to Diff 363420.Aug 2 2021, 3:08 AM
  • Updated exaxmples in detectAsFloorDiv docs
Groverkss marked an inline comment as done.Aug 2 2021, 3:14 AM

Addressed comments.

mlir/lib/Analysis/AffineStructures.cpp
1561

Regarding the verification in testing: due to how the current implementation is exposed, it is hard to test if the result matches. There is another patch (https://reviews.llvm.org/D106662) that refactors this implementation and provides better a better way to test.

vinayaka-polymage requested changes to this revision.Aug 2 2021, 4:07 AM
This comment was removed by vinayaka-polymage.
This revision now requires changes to proceed.Aug 2 2021, 4:07 AM
vinayaka-polymage accepted this revision.Aug 2 2021, 4:11 AM

Looks good now, thanks for addressing the comments.

This revision is now accepted and ready to land.Aug 2 2021, 4:11 AM