This is an archive of the discontinued LLVM Phabricator instance.

[CodeMoverUtils][WIP] Move code motion related checks from LICM to CodeMoverUtils
AcceptedPublic

Authored by RithikSharma on Jun 22 2020, 6:13 AM.

Details

Summary

This patch is a part of attempt to standardize code motion checks into one place. LoopFuse already uses CodeMoverUtils. This is a work in progress until we move all generic code motion checks into CodeMoverUtils. We started with moving less aggressive checks from LICM to CodeMoverUtils which can also be used by other code motion clients.

Diff Detail

Event Timeline

RithikSharma created this revision.Jun 22 2020, 6:13 AM
RithikSharma added inline comments.Jun 22 2020, 6:23 AM
llvm/lib/Transforms/Scalar/LICM.cpp
255

@Whitney I'm not sure is it okay to do all these deep changes but they all exist because this LICM invocation requires analysis from AnalysisResult. I request your comment on this.

fhahn added a subscriber: fhahn.
fhahn added inline comments.
llvm/include/llvm/Analysis/LoopAnalysisManager.h
63 ↗(On Diff #272400)

Requiring those extra analysis here requires a strong motivation I think, as I think each loop pass has to make sure they are preserved or recomputed.

llvm/lib/Transforms/Scalar/LICM.cpp
1385

In order to make the transition to more general utils easier I think it might be worth considering making PDT/DI optional in the various utils and separate it from making DI & PDT required for all users, which potentially has quite a big impact.

Also, I think LICM already uses MemorySSA to figure out if it is legal to move memory access out of the loop.

nikic added a subscriber: nikic.Jun 22 2020, 6:47 AM
fhahn added a comment.Jun 22 2020, 7:07 AM

Thanks for working on consolidating various utilities! Some initial comments inline.

bmahjour added inline comments.Jun 22 2020, 11:29 AM
llvm/include/llvm/Transforms/Utils/LoopUtils.h
200 ↗(On Diff #272400)

fix formatting

llvm/lib/Transforms/Utils/CodeMoverUtils.cpp
333 ↗(On Diff #272400)

query functions should not change the dominator trees. why const is being removed here?

413 ↗(On Diff #272400)

why const is dropped?

asbirlea requested changes to this revision.Jun 22 2020, 1:06 PM

Thank you for the patch!

This needs to be split up into multiple changes, there's too much going on in this patch to review as is.

Cleaning up the utilities, adding const, clang-format are all awesome and straight-forward to review. They should be separate NFCs.

Adding those dependencies (PDT and DepAnalysis) are not correct in this format, in particular in the loop pass manager. The implication there is that *all* loop passes in *any* loop pipeline are guaranteed to preserve those analyses. At least for PDT, I know that's not the case. I'm not familiar with DepAnalysis, but it looks like its scope overlaps with MemorySSA.
The constraints are different for Loop vs Function passes (LICM is a loop pass, LoopVectorize is a function pass), so again, the changes need to be reviewed separately.

This revision now requires changes to proceed.Jun 22 2020, 1:06 PM
Whitney added inline comments.Jun 22 2020, 2:21 PM
llvm/include/llvm/Analysis/LoopAnalysisManager.h
48 ↗(On Diff #272400)

nit: follow alphabetic order.

163 ↗(On Diff #272400)

recommend not to do formatting changes on lines not changed by you.
This is a general comment to all your changes, this can make your patch smaller.

llvm/lib/Transforms/Scalar/LICM.cpp
255

Can you please elaborate what you mean by deep changes? Do you mean adding adding PDT and DI in AR?
We should consider extending CodeMover utilities to take in more analyses, where some of them can be optional, and depending on the available analysis do the best judgement.
Or if MSSA can be used to cover all cases currently covered on CodeMover, then we should change CodeMover to use MSSA.

RithikSharma marked an inline comment as done.Jun 23 2020, 9:06 AM
RithikSharma added inline comments.
llvm/lib/Transforms/Scalar/LICM.cpp
255

Thanks, yes, I was referring to all the side effect changes that are required because of adding PDT and DI in AR. MSSA checks look really promising I was planning to get them in.

RithikSharma edited the summary of this revision. (Show Details)

Many thanks @asbirlea and @fhahn for the review, it surely makes more sense to keep CodeMoverUtils flexible with the required analysis. I've made all the analysis in CodeMotionUtils optional so now we need not add them in LICM.

asbirlea requested changes to this revision.Jun 26 2020, 4:18 PM

The issue with this change is that there is a lot of unnecessary work done now. Before, we could exit early to decide an instruction could never be hoisted/sunk. Now, we're removing all those early checks and only when actually trying to move get the "actually, you can't". I expect this to get a big compile-time penalty.
Could you do some testing and include results in the patch summary?

This revision now requires changes to proceed.Jun 26 2020, 4:18 PM

The issue with this change is that there is a lot of unnecessary work done now. Before, we could exit early to decide an instruction could never be hoisted/sunk. Now, we're removing all those early checks and only when actually trying to move get the "actually, you can't". I expect this to get a big compile-time penalty.
Could you do some testing and include results in the patch summary?

Totally agreed with the delayed check issue and I've moved it to a place that is more analogous to the original checks. LICM has very extensive code motion checks, moving the generalized checks to a unified place will greatly help other passes to take advantage of them. Does the updated diff look satisfactory to you?

Yes, the diff look good. Could you clarify what isSafeToMove does? I don't see it in the included file, only isSafeToMoveBefore. Is there a dependent patch not linked?

Many thanks, updated the parent revision.

asbirlea accepted this revision.Jun 29 2020, 12:06 PM

This looks only dependent on D82290, not D82566.

This revision is now accepted and ready to land.Jun 29 2020, 12:06 PM
Whitney accepted this revision.Jun 29 2020, 2:22 PM