<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <p><br>
    </p>
    <div class="moz-cite-prefix">On 12/26/2016 01:54 PM, Siu Kwan Lam
      via llvm-dev wrote:<br>
    </div>
    <blockquote
cite="mid:CAHjo7=W9fu4wVHuLaKezf4g16fjW4ZFVATb5AoaZd7_NVgD0iA@mail.gmail.com"
      type="cite">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <div dir="ltr">Hi all,<br>
        <br>
        I am noticing a significant degradation in execution performance
        in loops with just one backedge than loops with two backedges. 
        Unifying the backedges into one will also cause the slowdown.
        <div><br>
        </div>
        <div>To replicate this problem, I used the C code in <a
            moz-do-not-send="true"
            href="https://gist.github.com/sklam/11f11a410258ca191e6f263262a4ea65">https://gist.github.com/sklam/11f11a410258ca191e6f263262a4ea65</a> and
          checked against clang-3.8 and clang-4.0 nightly.  Depending on
          where I put the "increment" code for a for-loop, I can get 2x
          performance difference.</div>
        <div><br>
        </div>
        <div>The slow (but natural) version:</div>
        <div>
          <div><br>
          </div>
          <div>    for (i=0; i<size; ++i) {</div>
          <div>        ai = arr[i];</div>
          <div><br>
          </div>
          <div>        if ( ai <= amin ) {</div>
          <div>            amin = ai;</div>
          <div>            all_missing = 0;</div>
          <div>        }</div>
          <div>    }</div>
          <div><br>
          </div>
          <div>The fast version:</div>
          <div>
            <div><br>
            </div>
            <div>    for (i=0; i<size;) {</div>
            <div>        ai = arr[i];</div>
            <div>        ++i;        // increment moved here</div>
            <div>        if ( ai <= amin ) {</div>
            <div>            amin = ai;</div>
            <div>            all_missing = 0;</div>
            <div>        }</div>
            <div>    }</div>
          </div>
          <div><br>
          </div>
          <div>With the fast version, adding a dummy line after the
            if-block will make the code slow again:</div>
          <div><br>
          </div>
          <div>
            <div>    for (i=0; i<size;) {</div>
            <div>        ai = arr[i];</div>
            <div>        ++i;</div>
            <div>        if ( ai <= amin ) {</div>
            <div>            amin = ai;</div>
            <div>            all_missing = 0;</div>
            <div>        }</div>
            <div>        i;  // no effect</div>
            <div>    }</div>
          </div>
          <div><br>
          </div>
          <div>At first, I noticed the problem with any opt level >=
            O1.  In an attempt to narrow it down, I found that using
            `opt -simplifycfg -sroa -simplifycfg` will trigger the
            slowdown.  Removing the second simplifycfg solves it and
            both versions of the code run fast.  </div>
          <div><br>
          </div>
          <div>Is there a known issue for this? Or, any idea why?</div>
        </div>
      </div>
    </blockquote>
    <br>
    Can you please file a bug report for this (<a class="moz-txt-link-freetext" href="https://llvm.org/bugs/">https://llvm.org/bugs/</a>),
    attaching the IR? I suspect we'll need to look at the generated
    code.<br>
    <br>
     -Hal<br>
    <br>
    <blockquote
cite="mid:CAHjo7=W9fu4wVHuLaKezf4g16fjW4ZFVATb5AoaZd7_NVgD0iA@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div>
          <div><br>
          </div>
          <div>Regards,</div>
          <div>Siu Kwan Lam</div>
        </div>
        <div><br>
        </div>
      </div>
      <div dir="ltr">-- <br>
      </div>
      <div data-smartmail="gmail_signature">
        <div dir="ltr">Siu Kwan Lam
          <div>Software Engineer</div>
          <div>Continuum Analytics</div>
        </div>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
LLVM Developers mailing list
<a class="moz-txt-link-abbreviated" href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>
<a class="moz-txt-link-freetext" href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a>
</pre>
    </blockquote>
    <br>
    <pre class="moz-signature" cols="72">-- 
Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory</pre>
  </body>
</html>