<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Apr 4, 2016 at 12:37 PM, Sean Silva <span dir="ltr"><<a href="mailto:chisophugis@gmail.com" target="_blank">chisophugis@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Mon, Apr 4, 2016 at 12:24 PM, Zia Ansari via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: zansari<br>
Date: Mon Apr  4 14:24:46 2016<br>
New Revision: 265337<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=265337&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=265337&view=rev</a><br>
Log:<br>
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.<br>
<br>
Commit for Evgeny Stupachenko (<a href="mailto:evstupac@gmail.com" target="_blank">evstupac@gmail.com</a>)<br>
<br>
Differential Revision: <a href="http://reviews.llvm.org/D18290" rel="noreferrer" target="_blank">http://reviews.llvm.org/D18290</a><br>
<br>
<br>
Added:<br>
    llvm/trunk/test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll<br>
Modified:<br>
    llvm/trunk/lib/Transforms/Scalar/LoopUnrollPass.cpp<br>
<br>
Modified: llvm/trunk/lib/Transforms/Scalar/LoopUnrollPass.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopUnrollPass.cpp?rev=265337&r1=265336&r2=265337&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopUnrollPass.cpp?rev=265337&r1=265336&r2=265337&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Scalar/LoopUnrollPass.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/Scalar/LoopUnrollPass.cpp Mon Apr  4 14:24:46 2016<br>
@@ -639,6 +639,16 @@ static bool tryToUnrollLoop(Loop *L, Dom<br>
       Count = (std::max(UP.PartialThreshold, 3u) - 2) / (LoopSize - 2);<br>
       while (Count != 0 && TripCount % Count != 0)<br>
         Count--;<br>
+      if (Count <= 1) {<br>
+        // If there is no Count that is modulo of TripCount, set Count to<br>
+        // largest power-of-two factor that satisfies the threshold limit.<br>
+        Count = (std::max(UP.PartialThreshold, 3u) - 2) / (LoopSize - 2);<br>
+        UnrolledSize = (LoopSize - 2) * Count + 2;<br>
+        while (Count != 0 && UnrolledSize > UP.PartialThreshold) {<br>
+          Count >>= 1;<br>
+          UnrolledSize = (LoopSize - 2) * Count + 2;<br>
+        }<br>
+      }<br>
     }<br>
   } else if (Unrolling == Runtime) {<br>
     if (!AllowRuntime && !CountSetExplicitly) {<br>
<br>
Added: llvm/trunk/test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll?rev=265337&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll?rev=265337&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll (added)<br>
+++ llvm/trunk/test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll Mon Apr  4 14:24:46 2016<br>
@@ -0,0 +1,29 @@<br>
+; RUN: opt < %s -S -unroll-threshold=20 -loop-unroll -unroll-allow-partial | FileCheck %s<br>
+<br>
+; The Loop TripCount is 9. However unroll factors 3 or 9 exceed given threshold.<br>
+; The test checks that we choose a smaller, power-of-two, unroll count and do not give up on unrolling.<br>
+<br>
+; CHECK: for.body:<br>
+; CHECK: store<br>
+; CHECK: for.body.1:<br>
+; CHECK: store<br></blockquote><div><br></div></div></div><div>Could these checks be tightened up? E.g. to verify that the bodies are actually unrolled?</div></div></div></div></blockquote><div><br></div><div>(e.g. check for the presence of the expected number of stores in the body)</div><div><br></div><div>-- Sean Silva</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="HOEnZb"><font color="#888888"><div><br></div><div>-- Sean Silva</div></font></span><span class=""><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+define void @foo(i32* nocapture %a, i32* nocapture readonly %b) nounwind uwtable {<br>
+entry:<br>
+  br label %for.body<br>
+<br>
+for.body:                                         ; preds = %for.body, %entry<br>
+  %indvars.iv = phi i64 [ 1, %entry ], [ %indvars.iv.next, %for.body ]<br>
+  %arrayidx = getelementptr inbounds i32, i32* %b, i64 %indvars.iv<br>
+  %ld = load i32, i32* %arrayidx, align 4<br>
+  %idxprom1 = sext i32 %ld to i64<br>
+  %arrayidx2 = getelementptr inbounds i32, i32* %a, i64 %idxprom1<br>
+  %st = trunc i64 %indvars.iv to i32<br>
+  store i32 %st, i32* %arrayidx2, align 4<br>
+  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1<br>
+  %exitcond = icmp eq i64 %indvars.iv.next, 10<br>
+  br i1 %exitcond, label %for.end, label %for.body<br>
+<br>
+for.end:                                          ; preds = %for.body<br>
+  ret void<br>
+}<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></span></div><br></div></div>
</blockquote></div><br></div></div>