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

    <tr>
        <th>Summary</th>
        <td>
             coroutine optimizer too aggressive for thread local variables
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    Using cpp reference sample: [co_await](https://en.cppreference.com/w/cpp/language/coroutines#co_await)
On godbolt clang trunk with "-std=c++20 -pthread" get correct, expected output... 
_Coroutine started on thread: 140410345137984
New thread ID: 140410345133824
Coroutine resumed on thread: 140410345133824_
Adding any of "-O2" or "-O3" or "-Ofast" gives incorrect output... (thread id in co-routine incorrectly preserved across co_await)
_Coroutine started on thread: 140442470807360
New thread ID: 140442470803200
Coroutine resumed on thread: **140442470807360** _
This seems to be a recent (last month or two) regression.
MSVC had this problem about a year ago ... dis-able 'fiber' optimization was their temporary fix, their trunk now works without /GT.
[fiber-safe thread-local storage](https://docs.microsoft.com/en-us/cpp/build/reference/gt-support-fiber-safe-thread-local-storage?view=msvc-170)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyNVE2PmzAQ_TVwGYEcG0Jy4LDdVaoe-iH167gyMBC3xka2CU1_fQdCstttVVUiATwzb968maGyzbn87JXpoB4GcNiiQ1MjeNkPGiNxB1H-qraPcpIqRPlDxHfHEAZPlogf6EKTUuQtMK1tT6cT_eiY_rU03Sg7nA-ss2NQBn3ExQ2T7yP2ELG79wY621RWB6jnIAhuNN9hUuEIEeeJD00kHuqIv6KLM0iGcHQoG7JBhxRkncOa8O4Bfwz0hA1QumEMaZrCJcfj_ZUC-CDd4mJgxaFaNxnLNkxk-UYU-112CXqH0-oCbx5eeIkdX72ekB36sf8H8hzzeAm6a5pZemnOYNulyvd8rse6y4t4_tJKH5Zi1Qk9KLMW_LxIvluJKroMSZJcSd3c9RmoWx7diSjK2lnv4Y9e_I9OGc8KtmOF2LJ_6LR6Cc7Yf-kUcZqru5f4yyGson06Kg8esfcQLFQIkqBqNGGuX5NI0FtDQ0O6hclSTWTuKJlX1qQXiLcfv9zDkYiGGWtwttLYg6yIGaGdUTqQnYVZ00b5RJKZwItWVejoDnYIqlc_ZSBImCQROaKidNgP1kl3hlb9mAdxPV4G2dgJJuu--2Wk50y0Eq8_rYxoyxb0xMsWVz0SbWupqQOESQv0l-VrbO3TXs1dtG1Ydw9NMvrb_lWj0rQjh9uG0nMXEj8ORDUkT0mT50mTa1JxOCmcaPF6f6qTTUG92MdYbrZbti_2m3wbN6Vo9mIv46CCxhJuW35VCUkBa0nQSxNOCO3cmsukXEo8SadmkX08Ol3-XmNHao3VWpvWp-stobZ9Wxb-oLwf54_KIS_4lsXHksmsLnZ1ntV1nstWCLZtm31eVYIXxHYXa1mh9iWpTrLGquSMc7ajIS1YnudpI0TbchRSbJqmyniUMeyl0umcOLWui125cKjGzpNRKx_8k1FSmZ1BvOLLkRruyg8Y0H1VWivZvyPxj_FCvFxY_wLpSchn">