This is an archive of the discontinued LLVM Phabricator instance.

[LoopFlatten] Simplify loops so that the pass can operate on unsimplified loops.
ClosedPublic

Authored by stelios-arm on May 11 2021, 9:04 AM.

Details

Summary

The loop flattening pass requires loops to be in simplified form. If the
loops are not in simplified form, the pass cannot operate. This patch
simplifies all loops before flattening. As a result, all loops will be
simplified regardless of whether anything ends up being flattened.

This change was inspired by observing a certain loop that was not flatten
because the loops were not in simplified form. This loop is added as a
test to verify that it is now flattened.

Diff Detail

Event Timeline

stelios-arm created this revision.May 11 2021, 9:04 AM
stelios-arm requested review of this revision.May 11 2021, 9:04 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 11 2021, 9:04 AM

It looks like Loop Flattening required LCSSA too. Can we make sure it runs formLCSSARecursively as well?

As a result, all loops will be simplified regardless of whether anything ends up being flattened.

Not sure if we should simplify loops where there's nothing to flatten, e.g. loops that are not nested?

llvm/test/Transforms/LoopFlatten/loop-flatten-simplify-cfg.ll
6

Personally I do not think this line here is helpful. Options change, the bisect limit can change and so on, making it unlikely that the IR can be reproduced in a few years....

The IR test itself could be a bit simplified to make it slightly easier to read (e.g. use the same sized type for all values, so no extra zext/sext are needed.

That way, there should be no need to add a C reference, because the IR is already quite simple.

@dmgreen Will do.

@fhahn Makes sense. I will change it to simplify loops that contain inner loops.

Thanks for the comments!

llvm/test/Transforms/LoopFlatten/loop-flatten-simplify-cfg.ll
6

I see, I will update the test as suggested. Thanks!

Addressed the comments.

dmgreen added inline comments.May 12 2021, 9:50 AM
llvm/lib/Transforms/Scalar/LoopFlatten.cpp
689

Maybe use if (L->isInnermost()) continue;?
That would keep the indentation down too, which llvm usually prefers.

stelios-arm marked an inline comment as done.

Addressed the comment of @dmgreen.

dmgreen accepted this revision.May 12 2021, 11:13 AM

Thanks. LGTM

This revision is now accepted and ready to land.May 12 2021, 11:13 AM
fhahn added inline comments.May 12 2021, 11:24 AM
llvm/lib/Transforms/Scalar/LoopFlatten.cpp
692

Would it not be more natural to do that directly in the main loop in Flatten() rather than duplicating the loop?

stelios-arm added inline comments.May 12 2021, 11:36 AM
llvm/lib/Transforms/Scalar/LoopFlatten.cpp
692

Oh yes, I missed that.

dmgreen added inline comments.May 12 2021, 12:01 PM
llvm/lib/Transforms/Scalar/LoopFlatten.cpp
692

I'm not sure that's better. It would mean that the old pass manager duplicates the simplification/lcssa forming that it has already done through the pass dependencies. Put here it is only called once for the entire loop nest.