<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">What if we don’t want this behavior?</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">      <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> (Count <= <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">        </span>// If there is no Count that is modulo of TripCount, set Count to</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">        </span>// largest power-of-two factor that satisfies the threshold limit.</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">        Count = (std::max(UP.PartialThreshold, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span>u) - <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span>) / (LoopSize - <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span>);</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">        UnrolledSize = (LoopSize - <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span>) * Count + <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span>;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">        <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">while</span> (Count != <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span> && UnrolledSize > UP.PartialThreshold) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">          Count >>= <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">          UnrolledSize = (LoopSize - <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span>) * Count + <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span>;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">        }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">      }</div></div><div class=""><br class=""></div><div class="">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?</div><div class=""><br class=""></div><div class="">—escha</div><br class=""><div><blockquote type="cite" class=""><div class="">On Apr 4, 2016, at 1:02 PM, Evgeny Stupachenko via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org" class="">llvm-commits@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">When the a TripCount is not modulo of Count (unroll factor) we jump<br class="">into the middle of a loop:<br class=""><br class="">i = 0;<br class="">while(1) {<br class="">  a[i] = i;<br class="">  i++;<br class="">  if (i >= 15) break;<br class="">}<br class=""><br class="">Unrolled by 2 into:<br class=""><br class="">i = 0;<br class="">jump L1;<br class="">while(1) {<br class="">  a[i] = i;<br class="">  i++;<br class="">L1:<br class="">  a[i] = i;<br class="">  i++;<br class="">  if (i >= 15) break;<br class="">}<br class=""><br class="">Here we check this type of unroll. It could be optimized by further<br class="">passes, however unroll should do exactly this.<br class=""><br class="">Thanks,<br class="">Evgeny<br class=""><br class=""><br class=""><br class="">On Mon, Apr 4, 2016 at 12:37 PM, Sean Silva via llvm-commits<br class=""><<a href="mailto:llvm-commits@lists.llvm.org" class="">llvm-commits@lists.llvm.org</a>> wrote:<br class=""><blockquote type="cite" class=""><br class=""><br class="">On Mon, Apr 4, 2016 at 12:24 PM, Zia Ansari via llvm-commits<br class=""><<a href="mailto:llvm-commits@lists.llvm.org" class="">llvm-commits@lists.llvm.org</a>> wrote:<br class=""><blockquote type="cite" class=""><br class="">Author: zansari<br class="">Date: Mon Apr  4 14:24:46 2016<br class="">New Revision: 265337<br class=""><br class="">URL: <a href="http://llvm.org/viewvc/llvm-project?rev=265337&view=rev" class="">http://llvm.org/viewvc/llvm-project?rev=265337&view=rev</a><br class="">Log:<br class="">Enable unroll for constant bound loops when TripCount is not modulo of<br class="">unroll factor, reducing it to maximum power-of-2 that satisfies threshold<br class="">limit.<br class=""><br class="">Commit for Evgeny Stupachenko (<a href="mailto:evstupac@gmail.com" class="">evstupac@gmail.com</a>)<br class=""><br class="">Differential Revision: <a href="http://reviews.llvm.org/D18290" class="">http://reviews.llvm.org/D18290</a><br class=""><br class=""><br class="">Added:<br class="">    llvm/trunk/test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll<br class="">Modified:<br class="">    llvm/trunk/lib/Transforms/Scalar/LoopUnrollPass.cpp<br class=""><br class="">Modified: llvm/trunk/lib/Transforms/Scalar/LoopUnrollPass.cpp<br class="">URL:<br class=""><a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopUnrollPass.cpp?rev=265337&r1=265336&r2=265337&view=diff" class="">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopUnrollPass.cpp?rev=265337&r1=265336&r2=265337&view=diff</a><br class=""><br class="">==============================================================================<br class="">--- llvm/trunk/lib/Transforms/Scalar/LoopUnrollPass.cpp (original)<br class="">+++ llvm/trunk/lib/Transforms/Scalar/LoopUnrollPass.cpp Mon Apr  4<br class="">14:24:46 2016<br class="">@@ -639,6 +639,16 @@ static bool tryToUnrollLoop(Loop *L, Dom<br class="">       Count = (std::max(UP.PartialThreshold, 3u) - 2) / (LoopSize - 2);<br class="">       while (Count != 0 && TripCount % Count != 0)<br class="">         Count--;<br class="">+      if (Count <= 1) {<br class="">+        // If there is no Count that is modulo of TripCount, set Count to<br class="">+        // largest power-of-two factor that satisfies the threshold<br class="">limit.<br class="">+        Count = (std::max(UP.PartialThreshold, 3u) - 2) / (LoopSize - 2);<br class="">+        UnrolledSize = (LoopSize - 2) * Count + 2;<br class="">+        while (Count != 0 && UnrolledSize > UP.PartialThreshold) {<br class="">+          Count >>= 1;<br class="">+          UnrolledSize = (LoopSize - 2) * Count + 2;<br class="">+        }<br class="">+      }<br class="">     }<br class="">   } else if (Unrolling == Runtime) {<br class="">     if (!AllowRuntime && !CountSetExplicitly) {<br class=""><br class="">Added:<br class="">llvm/trunk/test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll<br class="">URL:<br class="">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll?rev=265337&view=auto<br class=""><br class="">==============================================================================<br class="">--- llvm/trunk/test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll<br class="">(added)<br class="">+++ llvm/trunk/test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll<br class="">Mon Apr  4 14:24:46 2016<br class="">@@ -0,0 +1,29 @@<br class="">+; RUN: opt < %s -S -unroll-threshold=20 -loop-unroll<br class="">-unroll-allow-partial | FileCheck %s<br class="">+<br class="">+; The Loop TripCount is 9. However unroll factors 3 or 9 exceed given<br class="">threshold.<br class="">+; The test checks that we choose a smaller, power-of-two, unroll count<br class="">and do not give up on unrolling.<br class="">+<br class="">+; CHECK: for.body:<br class="">+; CHECK: store<br class="">+; CHECK: for.body.1:<br class="">+; CHECK: store<br class=""></blockquote><br class=""><br class="">Could these checks be tightened up? E.g. to verify that the bodies are<br class="">actually unrolled?<br class=""><br class="">-- Sean Silva<br class=""><br class=""><blockquote type="cite" class=""><br class="">+<br class="">+define void @foo(i32* nocapture %a, i32* nocapture readonly %b) nounwind<br class="">uwtable {<br class="">+entry:<br class="">+  br label %for.body<br class="">+<br class="">+for.body:                                         ; preds = %for.body,<br class="">%entry<br class="">+  %indvars.iv = phi i64 [ 1, %entry ], [ %indvars.iv.next, %for.body ]<br class="">+  %arrayidx = getelementptr inbounds i32, i32* %b, i64 %indvars.iv<br class="">+  %ld = load i32, i32* %arrayidx, align 4<br class="">+  %idxprom1 = sext i32 %ld to i64<br class="">+  %arrayidx2 = getelementptr inbounds i32, i32* %a, i64 %idxprom1<br class="">+  %st = trunc i64 %indvars.iv to i32<br class="">+  store i32 %st, i32* %arrayidx2, align 4<br class="">+  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1<br class="">+  %exitcond = icmp eq i64 %indvars.iv.next, 10<br class="">+  br i1 %exitcond, label %for.end, label %for.body<br class="">+<br class="">+for.end:                                          ; preds = %for.body<br class="">+  ret void<br class="">+}<br class=""><br class=""><br class="">_______________________________________________<br class="">llvm-commits mailing list<br class=""><a href="mailto:llvm-commits@lists.llvm.org" class="">llvm-commits@lists.llvm.org</a><br class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits<br class=""></blockquote><br class=""><br class=""><br class="">_______________________________________________<br class="">llvm-commits mailing list<br class=""><a href="mailto:llvm-commits@lists.llvm.org" class="">llvm-commits@lists.llvm.org</a><br class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits<br class=""><br class=""></blockquote>_______________________________________________<br class="">llvm-commits mailing list<br class=""><a href="mailto:llvm-commits@lists.llvm.org" class="">llvm-commits@lists.llvm.org</a><br class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits<br class=""></div></div></blockquote></div><br class=""></body></html>