<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 - Poor optimization of thread local globals on OSX"
   href="https://bugs.llvm.org/show_bug.cgi?id=41722">41722</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Poor optimization of thread local globals on OSX
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>8.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>tyler.m.kopf@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Multiple calls to tlv_get_addr are (often) generated per usage of a thread
local variable on OSX. This issue was discovered by looking at the assembly
generated by rustc, and is discussed in more detail here:

<a href="https://github.com/rust-lang/rust/pull/60341#issuecomment-487982828">https://github.com/rust-lang/rust/pull/60341#issuecomment-487982828</a>

I know very little about llvm - so hopefully this all makes sense. The linked
IR [1] demonstrates the issue. Often, the optimizer spits out IR which
references thread_local globals multiple times when the unoptimized IR only
references them once. Often associated with getelementptr.

In the final assembly the asm does the tlv_get_addr dance twice.

movq    _foo@TLVP(%rip), %rdi
callq   *(%rdi)

For larger structures with multiple members, the problem gets worse, resulting
in many redundant calls to tlv_get_addr. In contrast, when targeting linux,
__tls_get_addr@PLT, is only invoked once.

Maybe there's a good reason the address isn't cached on OSX, but I'm hoping
there isn't :).

[1]: <a href="https://gist.github.com/alexcrichton/a9a90412152d04caa7011042aa89b6bf">https://gist.github.com/alexcrichton/a9a90412152d04caa7011042aa89b6bf</a></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>