This is an archive of the discontinued LLVM Phabricator instance.

[LoopUnroll] Update branch weight for remainder loop
ClosedPublic

Authored by hoyFB on Jul 5 2020, 10:36 PM.

Details

Summary

Unrolling a loop with compile-time unknown trip count results in a remainder loop. The remainder loop executes the remaining iterations of the original loop when the original trip count is not a multiple of the unroll factor. For better profile counts maintenance throughout the optimization pipeline, I'm assigning an artificial weight to the latch branch of the remainder loop.

A remainder loop runs up to as many times as the unroll factor subtracted by 1. Therefore I'm assigning the maximum possible trip count as the back edge weight. This should be more accurate than the default non-profile weight, which assumes the back edge runs much more frequently than the exit edge.

Diff Detail

Event Timeline

hoyFB created this revision.Jul 5 2020, 10:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 5 2020, 10:36 PM
hoyFB edited the summary of this revision. (Show Details)Jul 5 2020, 10:40 PM
hoyFB added reviewers: fhahn, wenlei.
hoyFB added a comment.Jul 5 2020, 10:42 PM

@fhahn I'm not sure who is an appropriate code reviewer. Can you please help find one? Thanks!

hoyFB updated this revision to Diff 278076.Jul 14 2020, 11:11 PM
hoyFB edited the summary of this revision. (Show Details)

Updating D83187: [LoopUnroll] Update branch weight for remainder loop

davidxl added inline comments.Jul 15 2020, 8:53 AM
llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp
820

It is better to fold this part into the update method too with one additional parameter for unroll factor.

hoyFB updated this revision to Diff 278231.Jul 15 2020, 9:59 AM
hoyFB marked 2 inline comments as done.

Updating D83187: [LoopUnroll] Update branch weight for remainder loop

hoyFB marked an inline comment as done.Jul 15 2020, 9:59 AM
hoyFB added inline comments.
llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp
820

I ended up passing the original loop into the helper as well.

asbirlea accepted this revision.Jul 15 2020, 10:55 AM

lgtm with 2 nits to clarify/fix.

llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp
521

Nit: not sure if possible: is it correct if UnrollFactor = 1 to set BackEdgeWeight = 0?

820

Nit: remove if braces.

This revision is now accepted and ready to land.Jul 15 2020, 10:55 AM
hoyFB updated this revision to Diff 278261.Jul 15 2020, 11:34 AM
hoyFB marked an inline comment as done.

Updating D83187: [LoopUnroll] Update branch weight for remainder loop

hoyFB marked 3 inline comments as done.Jul 15 2020, 11:35 AM
hoyFB added inline comments.
llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp
521

Good point. UnrollFactor should never be one since it means the loop is not being unrolled. I put an assert for that.

This revision was automatically updated to reflect the committed changes.
hoyFB marked an inline comment as done.