<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/126200>126200</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Miscompilation: Missing prefixes for emulatedTLS variables on macOS/ARM64 with -O0
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          sschaller
      </td>
    </tr>
</table>

<pre>
    Reproducer:
https://alive2.llvm.org/ce/z/6Xr9_c

```cpp
struct T {
    T() {} // required
    int a = 0;
};

thread_local T aa;

int main() {
    return 0;
}
```
Trying to compile this code on macOS / ARM64 with `clang -stdlib=libc++ -std=c++17 -femulated-tls main.cpp`
leads to a linker error for undefined symbols `___tls_guard` and `_aa` (4-5, 60-61 in the reproducer).

Inspecting the generated assembly code shows that both symbols are used as operands instead of the prefixed variables `___emutls_v.__tls_guard` and `___emutls_v.aa`.

Compiling with `-O1` correctly prefixes the operands and fixes the issue.

I'm not quite sure what is missing and would appreciate any pointers.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJx0VE-vozYQ_zTmMiIyJkBy4JC3EVKlrp60m0Nv0WBPgltjs7ZJmn76yuTPSw-VkLAHz-_PjAcMQZ8tUcuqD1btM5zj4HwbghzQGPJZ79St_UGTd2qW5Fm5Y3w3xDiFtBQdEx0afSGxMuYyrpw_M9FJYqL7h4mu_sNvj5LxlMRqfn_kNDG-C9HPMsIBWPPB-A4A4MDEhontEmn2cEcHT79m7Uk9DmkbAYGVe-CsTJms2T8WfBcHT6iOxkk0cADE15eUNqK2bxR3PE9x9vYd7E0q47uDv2l7huhAunHShiAOOoB0isBZGFF-_kxSYffje72Gq44DJJMG7RnyEJXRPSv3RveSiQ8mPpYgK_ePbdFAfqJxNhhJ5dGEReYqFWnhN4QqJHoEo-1f5IG8dx5OzsNsFZ20JQXhNvbOhMR8PB6jCcfzjF6xmgNatYQR046JzTqvmPgGNc_rArSFOBD4rw6L7epest9smEjGxf1AcCZLPokEDIHG3tzuRQiDuwaIA0boXRxeUtATzGE5Dm4ij1YF0DZEQgXutGBOnk76b1JwQa-xN_R0QOOcTFxW_2Pm7cTi6yH529KipPjZh_yzSHnSeU8ymtuTMiz8L10J9yusQ5jpWQUmmhGsi_Br1pEgzJ7gmtzqAKMOIbGl9KubjQKcJk9SYyRAe4PJaRvJh1Wm2lJtyy1m1BZNudkW9bqqsqEVZdE0qq963EiFp22huESFJTbFmrDmmW4FFxUXvOEbwatqJftNvSnWp7pco0BRsTWnEbV5TWC26G8LUQvOM4M9mbAMuBCWrnd3TIg0775NSXk_nwNbc6NDDF8wUUdD7Xcd7jcfo3aWlTv4_nD9KmW6i88rfPj951s3nxPCRPc2H_knz2Zv2v_-R846DnO_km5koksiHq988u5PkpGJbpEemOge3i6t-DcAAP__3S-E1w">