<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 - Clang doesn't mix static and dynamic initialization of objects"
   href="https://bugs.llvm.org/show_bug.cgi?id=39680">39680</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang doesn't mix static and dynamic initialization of objects
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Windows NT
          </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>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>dmajor@mozilla.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When a global is initialized with compile-time values but does non-constexpr
work in its constructor, MSVC will bake the compile-time values into the object
to minimize the runtime work.

Clang sets all the fields in the constructor. This can lead to lengthy static
initializers when the objects are large or numerous:
<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1506763">https://bugzilla.mozilla.org/show_bug.cgi?id=1506763</a>

// ------------------------------------------------------------------

void (*Log)(void*);

struct Entry
{
    Entry(int _a, int _b, int _c, int _d)
     : a(_a), b(_b), c(_c), d(_d)
    {
       Log(this);
    }

    int a, b, c, d;
};

static const Entry myentry { 0, 1, 2, 3 };

int main(int argc, char** argv)
{
  return myentry.a;
}

// ------------------------------------------------------------------

<span class="quote">> cl -Z7 -O2 -c x.cpp 
> link -debug x.obj</span >

The members are already populated at load time:

0:000> dx -r1 (*((x!Entry *)0x14007f000))
(*((x!Entry *)0x14007f000))                 [Type: Entry]
    [+0x000] a                : 0 [Type: int]
    [+0x004] b                : 1 [Type: int]
    [+0x008] c                : 2 [Type: int]
    [+0x00c] d                : 3 [Type: int]

So the initializer only needs to do:

00000001`40006bb0 488d0d49840700  lea     rcx,[x!myentry (00000001`4007f000)]
00000001`40006bb7 48ff2562930700  jmp     qword ptr [x!Log (00000001`4007ff20)]

But on clang, the members are zero at process load:

<span class="quote">> clang-cl -Z7 -O2 -c x.cpp 
> lld-link -debug x.obj </span >

0:000> dx -r1 (*((x!Entry *)0x14005eac0))
(*((x!Entry *)0x14005eac0))                 [Type: Entry]
    [+0x000] a                : 0 [Type: int]
    [+0x004] b                : 0 [Type: int]
    [+0x008] c                : 0 [Type: int]
    [+0x00c] d                : 0 [Type: int]

And the initializer writes the data manually:

00000001`40001014 0f2805e5bf0400  movaps  xmm0,xmmword ptr [x!_xmm
(00000001`4004d000)]
00000001`4000101b 0f29059eda0500  movaps  xmmword ptr [x!myentry
(00000001`4005eac0)],xmm0
00000001`40001022 488d0d97da0500  lea     rcx,[x!myentry (00000001`4005eac0)]
00000001`40001029 ff1581da0500    call    qword ptr [x!Log (00000001`4005eab0)]</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>