This is an archive of the discontinued LLVM Phabricator instance.

[LoopUnroll] Enable option to unroll remainder loop
ClosedPublic

Authored by samparker on Aug 4 2017, 7:15 AM.

Details

Summary

On some targets, the penalty of executing runtime unrolling checks and then not the unrolled loop can be significantly detrimental to performance. This results in the need to be more conservative with the unroll count, keeping a trip count of 2 reduces the overhead as well as increasing the chance of the unrolled body being executed. But being conservative leaves performance gains on the table.

This patch enables the unrolling of the remainder loop introduced by runtime unrolling. This can help reduce the overhead of mis-unrolled loops because the cost of non-taken branches is much less than the cost of the backedge that would normally be executed in the remainder loop. This allows larger unroll factors to be used without suffering performance loses with smaller iteration counts.

Diff Detail

Event Timeline

samparker created this revision.Aug 4 2017, 7:15 AM

This makes sense.

lib/Transforms/Utils/LoopUnrollRuntime.cpp
929

I think I would write this using UnrollLoop: full unrolling will result in a slightly simpler CFG because the backedge goes away. (Granted, later optimizations will probably clean it up either way.)

samparker added inline comments.Aug 7 2017, 8:39 AM
lib/Transforms/Utils/LoopUnrollRuntime.cpp
929

That's not possible here though. As we only know the maximum trip count, we'll need to maintain comparisons for each iteration and convert the backedges to early exits.

samparker updated this revision to Diff 110012.Aug 7 2017, 8:55 AM

Since we know that the maximum number of iterations have been peeled off the loop, I've added a function that removes the remainder loop body. Also now added a test.

LoopUnroll has been able to unroll using an upper bound since https://reviews.llvm.org/rL284053 .

Ah ok, great. I'll have a play with that then. Thanks

samparker updated this revision to Diff 110180.Aug 8 2017, 5:45 AM
samparker retitled this revision from [LoopUnroll] Enable option to peel remainder loop to [LoopUnroll] Enable option to unroll remainder loop.
samparker edited the summary of this revision. (Show Details)

Now unrolling the loop instead of peeling.

efriedma added inline comments.
lib/Transforms/Utils/LoopUnrollRuntime.cpp
930

It's difficult to figure out what you're actually passing here; maybe include the parameter names in comments, like UnrollLoop(remainderLoop, /*Count*/Count - 1, /*TripCount*/Count - 1, /*Force*/ false, etc.

test/Transforms/LoopUnroll/runtime-unroll-remainder.ll
1 ↗(On Diff #110180)

I'd prefer to see the result without the simplifycfg run, to see what the generated IR actually looks like.

samparker updated this revision to Diff 110330.Aug 9 2017, 1:17 AM

Added useful comments for call arguments and modified the test not to use simplifycfg.

efriedma accepted this revision.Aug 11 2017, 2:28 PM

LGTM.

include/llvm/Transforms/Utils/UnrollLoop.h
46

Weird line wrapping.

lib/Transforms/Scalar/LoopUnrollPass.cpp
948

Please commit whitespace change separately.

This revision is now accepted and ready to land.Aug 11 2017, 2:28 PM
This revision was automatically updated to reflect the committed changes.