[llvm] r265337 - Enable unroll for constant bound loops when TripCount is not modulo of unroll factor, reducing it to maximum power-of-2 that satisfies threshold limit.
Evgeny Stupachenko via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 4 13:35:51 PDT 2016
Before the patch the loop
for (i = 0; i < 15; i++)
loop_body;
was not unrolled,
the loop
for (i = 0; i < 16; i++)
loop_body;
was unrolled
the loop
for (i = 0; i < n; i++)
loop_body;
was unrolled
Why we should avoid unrolling if threshold let us unroll a loop?
The sense of unrolling (right now) is to reduce induction variable and
compare/branch costs.
One of possible solutions is to add " && Unrolling == Runtime":
> if (Count <= 1 && Unrolling == Runtime) {
However I still do not understand why we should avoid unrolling if
threshold let us unroll a loop?
For the cases where unroll is unprofitable there should be
corresponding heuristics.
What is your case?
Thanks,
Evgeny
On Mon, Apr 4, 2016 at 1:10 PM, <escha at apple.com> wrote:
> What if we don’t want this behavior?
>
> if (Count <= 1) {
> // If there is no Count that is modulo of TripCount, set Count to
> // largest power-of-two factor that satisfies the threshold limit.
> Count = (std::max(UP.PartialThreshold, 3u) - 2) / (LoopSize - 2);
> UnrolledSize = (LoopSize - 2) * Count + 2;
> while (Count != 0 && UnrolledSize > UP.PartialThreshold) {
> Count >>= 1;
> UnrolledSize = (LoopSize - 2) * Count + 2;
> }
> }
>
> Previously, partial unrolling was always modulo the trip count; this allows
> partial unrolling to duplicate the loop, which we don’t want. What should we
> do if we don’t want this behavior?
>
> —escha
>
> On Apr 4, 2016, at 1:02 PM, Evgeny Stupachenko via llvm-commits
> <llvm-commits at lists.llvm.org> wrote:
>
> When the a TripCount is not modulo of Count (unroll factor) we jump
> into the middle of a loop:
>
> i = 0;
> while(1) {
> a[i] = i;
> i++;
> if (i >= 15) break;
> }
>
> Unrolled by 2 into:
>
> i = 0;
> jump L1;
> while(1) {
> a[i] = i;
> i++;
> L1:
> a[i] = i;
> i++;
> if (i >= 15) break;
> }
>
> Here we check this type of unroll. It could be optimized by further
> passes, however unroll should do exactly this.
>
> Thanks,
> Evgeny
>
>
>
> On Mon, Apr 4, 2016 at 12:37 PM, Sean Silva via llvm-commits
> <llvm-commits at lists.llvm.org> wrote:
>
>
>
> On Mon, Apr 4, 2016 at 12:24 PM, Zia Ansari via llvm-commits
> <llvm-commits at lists.llvm.org> wrote:
>
>
> Author: zansari
> Date: Mon Apr 4 14:24:46 2016
> New Revision: 265337
>
> URL: http://llvm.org/viewvc/llvm-project?rev=265337&view=rev
> Log:
> Enable unroll for constant bound loops when TripCount is not modulo of
> unroll factor, reducing it to maximum power-of-2 that satisfies threshold
> limit.
>
> Commit for Evgeny Stupachenko (evstupac at gmail.com)
>
> Differential Revision: http://reviews.llvm.org/D18290
>
>
> Added:
> llvm/trunk/test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll
> Modified:
> llvm/trunk/lib/Transforms/Scalar/LoopUnrollPass.cpp
>
> Modified: llvm/trunk/lib/Transforms/Scalar/LoopUnrollPass.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopUnrollPass.cpp?rev=265337&r1=265336&r2=265337&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/Transforms/Scalar/LoopUnrollPass.cpp (original)
> +++ llvm/trunk/lib/Transforms/Scalar/LoopUnrollPass.cpp Mon Apr 4
> 14:24:46 2016
> @@ -639,6 +639,16 @@ static bool tryToUnrollLoop(Loop *L, Dom
> Count = (std::max(UP.PartialThreshold, 3u) - 2) / (LoopSize - 2);
> while (Count != 0 && TripCount % Count != 0)
> Count--;
> + if (Count <= 1) {
> + // If there is no Count that is modulo of TripCount, set Count to
> + // largest power-of-two factor that satisfies the threshold
> limit.
> + Count = (std::max(UP.PartialThreshold, 3u) - 2) / (LoopSize - 2);
> + UnrolledSize = (LoopSize - 2) * Count + 2;
> + while (Count != 0 && UnrolledSize > UP.PartialThreshold) {
> + Count >>= 1;
> + UnrolledSize = (LoopSize - 2) * Count + 2;
> + }
> + }
> }
> } else if (Unrolling == Runtime) {
> if (!AllowRuntime && !CountSetExplicitly) {
>
> Added:
> llvm/trunk/test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll?rev=265337&view=auto
>
> ==============================================================================
> --- llvm/trunk/test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll
> (added)
> +++ llvm/trunk/test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll
> Mon Apr 4 14:24:46 2016
> @@ -0,0 +1,29 @@
> +; RUN: opt < %s -S -unroll-threshold=20 -loop-unroll
> -unroll-allow-partial | FileCheck %s
> +
> +; The Loop TripCount is 9. However unroll factors 3 or 9 exceed given
> threshold.
> +; The test checks that we choose a smaller, power-of-two, unroll count
> and do not give up on unrolling.
> +
> +; CHECK: for.body:
> +; CHECK: store
> +; CHECK: for.body.1:
> +; CHECK: store
>
>
>
> Could these checks be tightened up? E.g. to verify that the bodies are
> actually unrolled?
>
> -- Sean Silva
>
>
> +
> +define void @foo(i32* nocapture %a, i32* nocapture readonly %b) nounwind
> uwtable {
> +entry:
> + br label %for.body
> +
> +for.body: ; preds = %for.body,
> %entry
> + %indvars.iv = phi i64 [ 1, %entry ], [ %indvars.iv.next, %for.body ]
> + %arrayidx = getelementptr inbounds i32, i32* %b, i64 %indvars.iv
> + %ld = load i32, i32* %arrayidx, align 4
> + %idxprom1 = sext i32 %ld to i64
> + %arrayidx2 = getelementptr inbounds i32, i32* %a, i64 %idxprom1
> + %st = trunc i64 %indvars.iv to i32
> + store i32 %st, i32* %arrayidx2, align 4
> + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
> + %exitcond = icmp eq i64 %indvars.iv.next, 10
> + br i1 %exitcond, label %for.end, label %for.body
> +
> +for.end: ; preds = %for.body
> + ret void
> +}
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
>
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
>
More information about the llvm-commits
mailing list