<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Changing place of "goto" makes code slower"
   href="https://bugs.llvm.org/show_bug.cgi?id=44380">44380</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Changing place of "goto" makes code slower
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>safinaskar@mail.ru
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider this two sources: <a href="https://godbolt.org/z/j6uSL6">https://godbolt.org/z/j6uSL6</a> (STILLFAST) and
<a href="https://godbolt.org/z/gcvDJV">https://godbolt.org/z/gcvDJV</a> (BECAMESLOW) (all tests performed on clang 10,
even if godbolt pages say something else). The only difference is placement of
"goto" statement. This should not change meaning of the code. But one code
gives vectorized code, and other is not. Change in speed is dramatic in my
tests. So, changing placement of "goto" prevents optimizations. This is a bug,
and so I reported it.

Now let me say what that code means.

Consider this source (I will codename it as STDGETC)

---
int c;
int result = 0;
while ((c = getc_unlocked(stdin)) != EOF)
  if (c == '\n')
    ++result;
---

This is, of course, simple implementation of "wc -l". And it is slow in my
tests.

Let's rewrite it so: <a href="https://godbolt.org/z/HtY3Ym">https://godbolt.org/z/HtY3Ym</a> (I name this RAWREAD)

Now the code is very fast, but it is too big.

So I've got an idea: to write fast stdio implementation. Such implementation
should combine beautiful "getc" API with speed of raw implementation.

Here is first attempt to write such implementation:
<a href="https://godbolt.org/z/BJJPYv">https://godbolt.org/z/BJJPYv</a> (I name this MYIO). Of course, it is not full
implementation yet. And it performs slower than RAWREAD.

So, I started to investigate why it performs slower. So I started to make
incremental changes in RAWREAD until I get to MYIO. To catch that moment when
code starts to be slow.

So here is chain of my changes to RAWREAD:

<a href="https://godbolt.org/z/QVC7Cz">https://godbolt.org/z/QVC7Cz</a>
<a href="https://godbolt.org/z/HSP3hV">https://godbolt.org/z/HSP3hV</a>
<a href="https://godbolt.org/z/3BzQTi">https://godbolt.org/z/3BzQTi</a>
<a href="https://godbolt.org/z/s8HRr_">https://godbolt.org/z/s8HRr_</a>
<a href="https://godbolt.org/z/yUptWS">https://godbolt.org/z/yUptWS</a>
<a href="https://godbolt.org/z/P35x3N">https://godbolt.org/z/P35x3N</a>
<a href="https://godbolt.org/z/g5JD-D">https://godbolt.org/z/g5JD-D</a>
<a href="https://godbolt.org/z/YPVh3j">https://godbolt.org/z/YPVh3j</a>
<a href="https://godbolt.org/z/j6uSL6">https://godbolt.org/z/j6uSL6</a> (at this point assembly changes, but code is still
vectorized and fast, I will call this code STILLFAST)
<a href="https://godbolt.org/z/gcvDJV">https://godbolt.org/z/gcvDJV</a> (I will call this BECAMESLOW)

So, the last two sources are essentially same, but the second is slower. And
this is a bug.

Debian stretch (with some packages from Debian buster), x86_64, Linux 4.19

"clang-10 -v" output:

----
clang version 10.0.0-+20191211115110+02168549172-1~exp1~20191211105657.1646 
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/6
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/6.3.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/6
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/6.3.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.3.0
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0
Candidate multilib: .;@m64
Selected multilib: .;@m64
----

This is clang installed from <a href="https://apt.llvm.org">https://apt.llvm.org</a>

Intel Core i7, hyper-threading is disabled at BIOS level, /proc/cpuinfo reports
4 cores</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>