<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/55083>55083</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
init_priority loses static initializer optimization
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
davidben
</td>
</tr>
</table>
<pre>
Clang is able to optimize out some static initializers. However, when some variable in the TU uses the `init_priority` attribute, Clang no longer elides the static initializer, for both it and other variables.
```
// Set this to 1 and *both* foo and bar get their initializers back.
#define USE_INIT_PRIORITY 0
struct NotConstExprButOptimizable {
explicit NotConstExprButOptimizable(int x) : x_(x) {}
int x_;
};
NotConstExprButOptimizable foo(5);
#if USE_INIT_PRIORITY
__attribute__((init_priority(101)))
#endif
NotConstExprButOptimizable bar(5);
```
https://godbolt.org/z/hP9rPoqjb
I'm not sure exactly what the semantics are here, but if clang has managed to provide an initializer is elidable, seems to me that should apply independent of init_priority. protobuf seems to use init_priority to control cross-TU ordering, but in cases where the constructor is elidable, that be even more preferable.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyFVE1T4zAM_TXpRUMnTRpoDznwtbO9AAPsYU8dx1ESs66dtZXy8etXdlqgdIbtuIltWc9P0lMqW7-Wl1qYFpQHUWkEsmB7Uhv1hmAHAm83CJ4EKQnKKFJCs8n5Kfy0z7hFl2SX8NyhGU9uhVMRRxmgDuHxFwwefZwnp2lAWPdOWafoldcgiJyqBsIAMzIxFrQ1LTpAreqd7zGD4NBYB5WlDhSBMDXwlN32HPw0Sa-S9Hz3PE13Y1xmP3jAAxLjc_Ac9yxiJNl5gOQXw9u4VQkHbTyIyh1kgU3yz_6aLK-xUQbh18P1enWzelzf3a9u71ePvyH9zMSTGyTBjaVLazxdv_TuYqDbMesxecnZxXgU-IcvvVZSfeeQZAtlCF6SbAlJfg4va94ZV4x0dvUBFo-tk3yHH2zv8_j8hhWng2ELhv3iw5Gr5jjq0bhev9d4HWhFrp9VkC1m6SyAjmOPiKZWzX85cWmOOB0WuiPqPSdlLHhr68pqmlrX8uqN_93d0t3Zv0_V54hWSXa2YSWy_geHXAIhSb-yzgWNcsSNMCxIbho2s-iifjlG4ETIKONOeOBDosU6iKt3dstqZj19FlBou6DysYiXjIubqEVuJQqX-c4OugbR93y9MjX2nBfkItoGDtI4DTeQrYbmA4Q77_BQ2JTWkLMapLPen3B_WlejU6Z9j8CAFKFnn0NcMVwZsh9Ea48YR5YVp2jLn4CNZYfeYYMu2HeNManLvF7mSzEhRRrLQ0rahruO-3v_GeJ9ayaD0-WXSirqhmoq7YYXWm_3rxPOwxNK4qXyfkDPk6JIF_mkK4uiyRe4zGfLtMjrqpCzos7nUsxls8yLeT7RokLty6S4SLLM4DNECJ4nxdVElVmaZek8K7LZbJ7Np3IheVIvFg37Ls8WyTxlXSg9DTyCxCaujJSqofVs1MqT_zAK71VrEON1jC8G6qwra8FCqdBM4t1l5P4PWcHcaA">