This is an archive of the discontinued LLVM Phabricator instance.

[LoopSimplifyCFG] Check predecessors of exits before marking them dead.
ClosedPublic

Authored by fhahn on Mar 17 2022, 10:04 AM.

Details

Summary

LoopSimplifyCFG may process loops that are not in
loop-simplify/canonical form. For loops not in canonical form, exit
blocks may be reachable from non-loop blocks and we cannot consider them
as dead if they only are not reachable from the loop itself.

Unfortunately the smallest test I could come up with requires running
multiple passes:

-passes='loop-mssa(loop-instsimplify,loop-simplifycfg,simple-loop-unswitch)'

The reason is that loops are canonicalized at the beginning of loop
pipelines, so a later transform has to break canonical form in a way
that breaks LoopSimplifyCFG's dead-exit analysis.

Alternatively we could try to require all loop passes to maintain
canonical form. That in turn would also require additional verification.

Fixes #54023.

Diff Detail

Event Timeline

fhahn created this revision.Mar 17 2022, 10:04 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 17 2022, 10:04 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
fhahn requested review of this revision.Mar 17 2022, 10:04 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 17 2022, 10:04 AM
nikic accepted this revision.Mar 17 2022, 1:49 PM

LGTM

Alternatively we could try to require all loop passes to maintain canonical form. That in turn would also require additional verification.

I think that may be worthwhile to do longer term. I think we have some transforms that bail out if we're not in loop simplify form, so this would ensure that things compose as intended. Also, I suspect that preserving loop simplify form might make preservation simpler overall, for example it should be easier to preserve LCSSA form (don't need to deal with an exit also being a sibling loop header etc).

IIRC the legacy pass manager did not automatically convert to loop simplify form for loop pass managers, which is where the current behavior might be coming from.

llvm/test/Transforms/LoopSimplifyCFG/loop-not-in-simplify-form.ll
6–7

Remove fixme.

This revision is now accepted and ready to land.Mar 17 2022, 1:49 PM