<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><span class="vcard"><a class="email" href="mailto:rnk@google.com" title="Reid Kleckner <rnk@google.com>"> <span class="fn">Reid Kleckner</span></a>
</span> changed
          <a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED DUPLICATE - ManagedStatic init-order fiasco bug since Visual Studio 14.27 because of std::atomic value-init constructor"
   href="https://bugs.llvm.org/show_bug.cgi?id=49027">bug 49027</a>
          <br>
             <table border="1" cellspacing="0" cellpadding="8">
          <tr>
            <th>What</th>
            <th>Removed</th>
            <th>Added</th>
          </tr>

         <tr>
           <td style="text-align:right;">Resolution</td>
           <td>---
           </td>
           <td>DUPLICATE
           </td>
         </tr>

         <tr>
           <td style="text-align:right;">Status</td>
           <td>NEW
           </td>
           <td>RESOLVED
           </td>
         </tr></table>
      <p>
        <div>
            <b><a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED DUPLICATE - ManagedStatic init-order fiasco bug since Visual Studio 14.27 because of std::atomic value-init constructor"
   href="https://bugs.llvm.org/show_bug.cgi?id=49027#c3">Comment # 3</a>
              on <a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED DUPLICATE - ManagedStatic init-order fiasco bug since Visual Studio 14.27 because of std::atomic value-init constructor"
   href="https://bugs.llvm.org/show_bug.cgi?id=49027">bug 49027</a>
              from <span class="vcard"><a class="email" href="mailto:rnk@google.com" title="Reid Kleckner <rnk@google.com>"> <span class="fn">Reid Kleckner</span></a>
</span></b>
        <pre>(In reply to Antonio Maiorano from <a href="show_bug.cgi?id=49027#c2">comment #2</a>)
<span class="quote">> Is it a coincidence that for MSC_VER < 1925, the non-constexpr ctor path
> works because std::atomic is trivially_constructible?</span >

It's intentional, not a coincidence. :) We had to back and forth on this
condition a few times to make it work for the various release of MSVC that we
supported at the time. We're trying to use the old codepath if std::atomic is
trivially constructible, and the new codepath if it has a constexpr default
constructor.

<span class="quote">> Perhaps more importantly, this constexpr ctor path is only used  by Clang
> and MSVC >= 1925, but not by GCC. This would imply that once libc(std)++
> adopts the C++20 std::atomic ctor change, GCC builds will result in the same
> problem I had with MSVC (dynamic initializer for std::atomic). To avoid
> this, I think it would be worth adding a static_assert to the non-constexpr
> ctor path like:</span >

GCC should be using a constexpr constructor here, it should be using the new
code path. The condition is if !defined(_MSC_VER) || defined(__clang__), so as
long as GCC does not define _MSC_VER, it should use the constexpr constructor.

Basically, constexpr construction is the future, and we should be able to scrap
these ifdefs in a few years and forget that this unfortunate mess ever
happened. =D

There is an attribute that can help us here:
<a href="https://clang.llvm.org/docs/AttributeReference.html#require-constant-initialization-constinit-c-20">https://clang.llvm.org/docs/AttributeReference.html#require-constant-initialization-constinit-c-20</a>

LLVM already uses it on one ManagedStatic instance here:
<a href="https://github.com/llvm/llvm-project/blob/master/llvm/lib/Support/CommandLine.cpp#L489">https://github.com/llvm/llvm-project/blob/master/llvm/lib/Support/CommandLine.cpp#L489</a>

Right now the attribute doesn't help us find bugs because it only works for
clang. However, we could try to use the C++20 spelling if it is available here:
<a href="https://github.com/llvm/llvm-project/blob/master/llvm/include/llvm/Support/Compiler.h#L283">https://github.com/llvm/llvm-project/blob/master/llvm/include/llvm/Support/Compiler.h#L283</a>

I guess this was prototyped with a descriptive name,
[[clang::require_constant_initialization]], and standardized as a contextual
keyword constinit. That's new to me.

---

Anyway, I think we can close as a duplicate. Re-reading the old issue, I see it
got reopened and fixed again. HTH

*** This bug has been marked as a duplicate of <a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED FIXED - LLVM's ManagedStatic.h fails with VS 2019 <atomic> and clang-cl due to init-order-fiasco and constexpr confusion"
   href="show_bug.cgi?id=41367">bug 41367</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>