This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Extend SimplifyTrivialLoops
ClosedPublic

Authored by ayzhuang on Mar 7 2022, 12:26 PM.

Details

Summary

Fold away empty loops that iterate at least once and only return
values defined outside of the loop.

Diff Detail

Event Timeline

ayzhuang created this revision.Mar 7 2022, 12:26 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 7 2022, 12:26 PM
ayzhuang requested review of this revision.Mar 7 2022, 12:26 PM
mehdi_amini added inline comments.Mar 7 2022, 12:57 PM
mlir/lib/Dialect/SCF/SCF.cpp
763

Where is the "iterates at least once" guaranteed?

766–767

I'll let @bondhugula clarify but my impression about his comment in D120776 is that we should be able to have a single implementation that works for both ScfForOp and AffineForOp by using/extending interfaces like LoopLikeInterface? Maybe I got it wrong. In any case, adding a scf specific implementation is also great. Thanks!

mlir/test/Dialect/SCF/canonicalize.mlir
363

Please, add // ----- before the new test.

ayzhuang added inline comments.Mar 7 2022, 2:26 PM
mlir/lib/Dialect/SCF/SCF.cpp
763

If the loop is known to have 0 iteration, we would have removed the loop and returned success: line 711-714.

mlir/test/Dialect/SCF/canonicalize.mlir
363

for_yields_2, for_yields_3, and for_yields_4 all use make_i32() declared before for_yields_2, thus no // ----- between them.

ayzhuang updated this revision to Diff 413672.Mar 7 2022, 5:59 PM

Address review comment to modify test.

ayzhuang marked an inline comment as done.Mar 7 2022, 5:59 PM
bondhugula accepted this revision.Mar 8 2022, 1:41 AM

LGTM - modulo the remaining minor comments.

mlir/lib/Dialect/SCF/SCF.cpp
764

.. remove it and replace it with yield values.

767–769

In addition to Mehdi's suggestions, you could use llvm::any_of here.

if (llvm::any_of(...))
  return failure();
This revision is now accepted and ready to land.Mar 8 2022, 1:41 AM
ayzhuang updated this revision to Diff 413837.Mar 8 2022, 9:37 AM
ayzhuang marked 3 inline comments as done.

Address review comments.

ayzhuang updated this revision to Diff 413848.Mar 8 2022, 9:43 AM

Upload the correct patch.

Thank you for the review. I'll merge it tomorrow if there are no more comments

dcaballe accepted this revision.Mar 9 2022, 2:38 PM
bondhugula added inline comments.Mar 10 2022, 8:41 AM
mlir/lib/Dialect/SCF/SCF.cpp
761

This check isn't tight enough:
lbValue + step - 1 <= ubValue?

ayzhuang added inline comments.Mar 10 2022, 11:07 AM
mlir/lib/Dialect/SCF/SCF.cpp
761

When lbValue >= ubValue, the loop does not iterate, so we do not fold. When lbValue + step - 1 <= ubValue, the loop can still iterate. For example, if lbValue = 0, step = 1, and ubValue = 1, we have 0 + 1 - 1 <= 1, but the loop iterates once.

ayzhuang updated this revision to Diff 414502.Mar 10 2022, 2:37 PM

Add a comment and remove unneeded check.

bondhugula accepted this revision.Mar 10 2022, 7:57 PM
bondhugula added inline comments.
mlir/lib/Dialect/SCF/SCF.cpp
761

Oops... looks like I was thinking about something else. Sounds fine.

mlir/test/Dialect/SCF/canonicalize.mlir
366

Please either add a short comment hereon what's being tested or make the function name descriptive enough.

ayzhuang updated this revision to Diff 414692.Mar 11 2022, 10:19 AM

Address review comment to add a comment to the unit test.

ayzhuang marked an inline comment as done.Mar 11 2022, 10:19 AM
This revision was automatically updated to reflect the committed changes.