This is an archive of the discontinued LLVM Phabricator instance.

[MLIR] Add division normalization by GCD in `getDivRepr` fn.
ClosedPublic

Authored by pashu123 on Dec 12 2021, 1:03 AM.

Details

Summary

This commits adds division normalization in the getDivRepr function which extracts
the gcd from the dividend and divisor and normalizes them.

Signed-off-by: Prashant Kumar <pk5561@gmail.com>

Diff Detail

Event Timeline

pashu123 created this revision.Dec 12 2021, 1:03 AM
pashu123 requested review of this revision.Dec 12 2021, 1:03 AM
pashu123 updated this revision to Diff 393729.Dec 12 2021, 1:04 AM

Clang format.

pashu123 updated this revision to Diff 393730.Dec 12 2021, 1:06 AM

Formatting.

pashu123 updated this revision to Diff 393731.Dec 12 2021, 1:10 AM

Updating for loops.

Groverkss added inline comments.Dec 12 2021, 2:50 AM
mlir/lib/Analysis/AffineStructures.cpp
1969–1971

Please mention that by normalization we mean GCD normalization. Maybe even change the function name to reflect that.

1983–1987

You can ignore the constant term while taking gcd. The reasoning is as follows:

For a division:

floor((a + m f(x))/(m d))

you can replace it by

floor((floor(a/m) + f(x))/d)

Because, the difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d and can therefore not influence the result of the floor.

1991

Unnecessary newline.

2001–2002

I think a better place for normalization is in getDivRepr after you obtain the division.

pashu123 updated this revision to Diff 393763.Dec 12 2021, 10:49 AM

Addressing groverkss comments and clang-format.

pashu123 updated this revision to Diff 393764.Dec 12 2021, 10:54 AM

Removing unneccesarry newline.

pashu123 updated this revision to Diff 393769.Dec 12 2021, 11:07 AM

Comments typo.

pashu123 added inline comments.Dec 13 2021, 2:13 AM
mlir/lib/Analysis/AffineStructures.cpp
2001–2002

I see, thanks.

Groverkss added inline comments.Dec 14 2021, 2:39 AM
mlir/lib/Analysis/AffineStructures.cpp
1355–1356
1355–1357
1358

I think a better name would be normalizeDivisionByGCD.

1358

Could you rename expr to dividend for more clarity?

1366
1366–1367

Please write the reasoning of why it can be replaced here.

1404–1406
mlir/unittests/Analysis/AffineStructuresTest.cpp
817

Just mention here that this tests normalization.

902

Please mention that this division should be normalized. Something like z = [3x + 3y] / 9 -> z = [x+y]/3.

pashu123 updated this revision to Diff 394310.Dec 14 2021, 10:46 AM

Addressing groverkss comments.

pashu123 marked 7 inline comments as done.Dec 14 2021, 10:47 AM
pashu123 updated this revision to Diff 394316.Dec 14 2021, 10:55 AM

Updated comments.

pashu123 marked an inline comment as done.Dec 14 2021, 10:59 AM
Groverkss added inline comments.Dec 15 2021, 6:42 AM
mlir/lib/Analysis/AffineStructures.cpp
1358
1367–1373

The commit summary and title is missing context on where the normalization is being added. Please update.

mlir/lib/Analysis/AffineStructures.cpp
1358

It'd be good to change denominator -> divisor to be consistent with dividend.

1360–1365

This whole thing doesn't look clean to me. Calling something gcd when the dividend is empty is out of line.

We probably need a logic cleanup or better variable names.

1363

Please use !empty() instead of size().

1404–1405

"returned division" isn't meaningful. Rephrase?

pashu123 updated this revision to Diff 394813.Dec 16 2021, 3:51 AM

Addressing Uday's comments.

pashu123 updated this revision to Diff 394815.Dec 16 2021, 3:57 AM
pashu123 marked 4 inline comments as done.

Clang format.

pashu123 marked 2 inline comments as done.Dec 16 2021, 3:57 AM
pashu123 added inline comments.
mlir/lib/Analysis/AffineStructures.cpp
1360–1365

I have placed the dividend.empty() check in the if condition above.

1404–1405

How about final division expression?

pashu123 retitled this revision from [MLIR] Add division normalization. to [MLIR] Add division normalization by GCD in `getDivRepr` fn..Dec 16 2021, 3:59 AM
pashu123 edited the summary of this revision. (Show Details)
pashu123 updated this revision to Diff 394830.Dec 16 2021, 5:02 AM

Clang format warnings.

pashu123 marked an inline comment as done.Dec 17 2021, 3:19 AM
pashu123 marked an inline comment as done.Dec 18 2021, 6:17 AM
pashu123 marked an inline comment as done.

LGTM. But, I would wait for @bondhugula to accept this in case he has any remaining concerns.

@bondhugula I have addressed all your comments. Please take a look.

@bondhugula I have addressed all your comments. Please take a look.

Reminder.

pashu123 updated this revision to Diff 397478.Jan 5 2022, 1:38 AM

Rebasing latest changes.

bondhugula added inline comments.Jan 5 2022, 8:02 AM
mlir/lib/Analysis/Presburger/Utils.cpp
44 ↗(On Diff #397478)

Capture by value is sufficient.

mlir/unittests/Analysis/AffineStructuresTest.cpp
694–697

Why is this test case updated to a trivial one? Please try to preserve the original test's strength here. @Groverkss?

817

Looks like this comment is still unaddressed.

836

1 division <-- One division ...

pashu123 updated this revision to Diff 397599.Jan 5 2022, 8:43 AM

Addressing bondhugula's comment.

pashu123 updated this revision to Diff 397601.Jan 5 2022, 8:45 AM

Comments.

pashu123 updated this revision to Diff 397604.Jan 5 2022, 8:50 AM
pashu123 marked 2 inline comments as done.

Comments.

Groverkss added inline comments.Jan 5 2022, 8:57 AM
mlir/unittests/Analysis/AffineStructuresTest.cpp
694–697

After the changes this patch introduces, constant divisions are simplified to an integer. Here it becomes ([0/1], [0/1]) instead of ([10/30], [99/101]). This test was supposed to check if we can extract constant divisions.

This test could be changed to adding divisions to ([100/30], [206/101]) and the result should be ([3/1], [2/1]). Just to check if constants are still extracted properly (extracting 0 does not seem like the best test to me).

pashu123 updated this revision to Diff 397611.Jan 5 2022, 9:15 AM

updating tests.

pashu123 marked 2 inline comments as done.Jan 5 2022, 9:16 AM
pashu123 added inline comments.
mlir/unittests/Analysis/AffineStructuresTest.cpp
694–697

I have updated the test case.

@Groverkss @bondhugula I have addressed all your comments PTAL.

bondhugula accepted this revision.Jan 6 2022, 2:21 AM
This revision is now accepted and ready to land.Jan 6 2022, 2:21 AM
This revision was automatically updated to reflect the committed changes.