This is an archive of the discontinued LLVM Phabricator instance.

[HardwareLoops] remove loops which has undef count
AbandonedPublic

Authored by shchenz on Nov 9 2022, 6:25 PM.

Details

Reviewers
samparker
dmgreen
nikic
Group Reviewers
Restricted Project
Summary

Remove the loop which has the loop count undef. This aligns with opt behavior.

For the PowerPC code gen changes, to keep the original test point, I add a valid count for the loop in the input IR instead of changing the assembly based on the current IR.

Diff Detail

Event Timeline

shchenz created this revision.Nov 9 2022, 6:25 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 9 2022, 6:25 PM
shchenz requested review of this revision.Nov 9 2022, 6:25 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 9 2022, 6:25 PM
samparker added inline comments.Nov 10 2022, 1:35 AM
llvm/lib/CodeGen/HardwareLoops.cpp
158

nit: I'd prefer that these were references.

247

I see the same logic is used in LoopDeletion, so should this be factored out into deleteDeadLoop?

nikic added a comment.Nov 10 2022, 1:50 AM

Can you please explain what the motivation for this change is?

fhahn added a subscriber: fhahn.Nov 10 2022, 3:55 AM
fhahn added inline comments.
llvm/lib/CodeGen/HardwareLoops.cpp
447

I doubt this is enough. I think you would at least have to ensure that the branch on undef is executed unconditionally in the loop. Also, shouldn't those dead loops be cleaned up earlier?

Can you please explain what the motivation for this change is?

This is from https://reviews.llvm.org/D135847#inline-1327746, in that patch, we found the loop being changed "invalidly" and then we found this is caused by undef loop count, so the "invalid" change is also OK.
And furthermore, we should be able to eliminate the undef count loop. This is what the LLVM opt component does, so we want to make llc align with opt.

llvm/lib/CodeGen/HardwareLoops.cpp
447

I think you would at least have to ensure that the branch on undef is executed unconditionally in the loop.

For hardware loops, the exiting block which contains the compare with loop count will be executed in each loop iteration.

Also, shouldn't those dead loops be cleaned up earlier?

OPT has the pass that can clean up this kind of loop. But in LLC pipeline, there is no such pass.

shchenz marked 3 inline comments as done.Nov 10 2022, 10:47 PM
shchenz added inline comments.
llvm/lib/CodeGen/HardwareLoops.cpp
158

Thanks for review.
But I am a little confused by this comment, do you mean SmallVector<Loop &, 1> DeadLoops;? Seems this can not pass the build

247

There are two callers for deleteDeadLoop in LoopDeletion. And for my understanding, these two callers are handling two different scenarios. The first one is like what we do here, the loop is never be executed, so we can just remove the loop and set the phi incoming value from exiting block to poison. And the other one is for loops which act like executing only once, i.e. all instructions in the loop are invariant. So we can make the incoming value from exiting block be invariant and then set it as the new value from preheader after deleting the phi.

I agree we can handle these two scenarios in deleteDeadLoop, but that would require a non-trivial work and not sure it is desirable. For example, we may also need to move some of the logic in isLoopDead() to deleteDeadLoop as the new incoming value for scenario 2 is calculated there. And for the new incoming value poison for scenario 1, we currently calculate just before we call deleteDeadLoop.

Thoughts?

samparker added inline comments.Nov 11 2022, 1:07 AM
llvm/lib/CodeGen/HardwareLoops.cpp
158

Whooops, sorry, shows I haven't written much C++ recently!

247

Okay, sounds like more effort than it's worth then.

447

Is there a blocker on adding the pass to the llc pipeline?

shchenz marked 2 inline comments as done.Nov 11 2022, 2:19 AM
shchenz added inline comments.
llvm/lib/CodeGen/HardwareLoops.cpp
447

hmm, to be honest, I didn't think about adding the pass to the llc pipeline. I am not sure if it is OK.

In OPT, for the case remove-loop-count-undef-loop.ll, the loops are optimized out by two passes, instcombine and loop deletion (Seems like loop deletion can only delete a simple loop optimized after instcombine)

  • opt -loop-deletion -S remove-loop-count-undef-loop.ll, the loop is kept
  • opt -instcombine -loop-deletion -S remove-loop-count-undef-loop.ll, the loop will be deleted.

I am thinking adding instcombine pass to llc pipeline is not a good idea and doing nested instruction combining to reduce loop complexity first in loop deletion pass also seems strange?

nikic added inline comments.Nov 11 2022, 2:22 AM
llvm/lib/CodeGen/HardwareLoops.cpp
447

Generally speaking, if opt optimizes something, then llc should not optimize it, unless the optimization is only possible as a result of legalization. It doesn't sound like that's the case here?

The input to llc is assumed to be optimized IR -- if it isn't, it's sufficient to not crash or miscompile it, but there is no need to produce good code.

samparker added inline comments.Nov 14 2022, 2:05 AM
llvm/lib/CodeGen/HardwareLoops.cpp
447

Is adding the necessary pass(es) into the backend pipeline an option?

shchenz added inline comments.Nov 14 2022, 8:07 PM
llvm/lib/CodeGen/HardwareLoops.cpp
447

I think @nikic comment suggest llc should not be changed if the opt is able to do the expected optimization, unless the suboptimal IR is result of opt/llc.

I created this patch because I thought llc might be used standalone as llc itself is a functionality complete binary that consumes LLVM IR. But if that is not the case, I am ok to abandon this.

Thanks for all your comments.

shchenz abandoned this revision.Nov 22 2022, 5:41 PM

Thanks again for your review, I'll abandon this patch.