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

    <tr>
        <th>Summary</th>
        <td>
            Static inline variable and global are initialized in incorrect order with optimization on
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          semushin-s
      </td>
    </tr>
</table>

<pre>
    Please take a look at the following example:
```cpp
struct A {
        A() {
        a = 1;
 }

    int a;
};

struct B {
        B() : _a(_ca) {}

        A _a;
        static inline A _ca;
};

B b;

int main() {
        return b._a.a;
}
```

Currently it returns 0 with `-O2` using clang:
https://gcc.godbolt.org/z/66hGqoaj8
and returns 1 with `-O0`. Compiled by all the other compilers, it results in 1 as well.

It seems initialization of `static inline A _ca` should be done earlier than initialization of `B`.
According to `[basic.start.dynamic]`:
`if V has partially-ordered initialization, W does not have unordered initialization, and for every definition E of W there exists a definition D of V such that D is appearance-ordered before E ... V is sequenced before the initialization of W;`

Let's say `static inline A _ca` is definition of `V`, `B b` is definition of `W`, seems by appearance order `_ca` should have been initialized before `b`, however with optimization `a=1` initialization fails to happen before initializing `B b` apparently. Please correct me if my interpretation of the standard is wrong in this case.


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8VU2PozgQ_TXOpdTIGELCgUPSmaxWWmlXWqn72CpMETxjbMY2ncn8-pUhnz3TG0VKsJ-rXr16ZdB7dTBEFVtu2XK3wDF01lWe-tF3yjz5RW2bU_WPJvQEAb8RIGhrvwEGCB1Ba7W2R2UOQD-wHzSxbMP4jvENK_j8lcMwr_jgRhlgA2y1PWN4uWFizUR5W4PzB4FlO0hZdllnq93l1AWnTAC8IiLg-v8-4_Yh4_aSMdvAGzKxfpN4YfCYItKLkFvQ0gcMSoIyWhmCDcSzn-ffQv1hJTLuUZmPVTNeOgqjM1Anb5g8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