<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64317>64317</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
`__builtin_assume` in destructors leads to worse codegen
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
cor3ntin
</td>
</tr>
</table>
<pre>
Consider the following C++ code
```cpp
struct S {
S();
~S();
int x = 0;
};
S::~S() {
__builtin_assume(x); // #1
}
void f(int n) {
S s;
if (n == 0)
throw;
else
f(n - 1);
}
```
https://godbolt.org/z/ddchhPssK
If line \#1 is commented, llvm is able to see that `S::~S` does nothing and as such it figures out that `f` requires no cleanup and
not call to `_Unwind_Resume` is performed.
If on the other hand `__builtin_assume(x)` is present - and x is not a constant, then:
* Some IR is created to load `x`
* Llvm does not deduce that S::~S() does nothing
* A landing pad / call to `_Unwind_Resume` is generated.
The outcome of that is that `__builtin_assume` cause significant performance regression compared to identical code without it,
when it appears in cleaning code, because it forces an onerous unwinding in places that may otherwise not need one.
I think I have convinced myself that this is a bug in llvm rather than clang but I'm not completely certain.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8Vc9vMjcQ_WvMZRS0eGFhD3sgiZCi9lB9ac-R1x5Yt97x1j9C0kP_9mq85IOk_YqQQWNm5r03bwcVoz0RYic292LzuFA5DT502oeakqVF78179-ApWoMB0oBw9M75s6UTPAh5L-Q9aG9QVI-i2l_OpprfeprmSEwh6wTPILb3cwQA4FnInZCtqG9if_9X0FKCNxD1I1Tf42L7eP1ezmdR70W9_6jAveBa4-Wlz9YlSy8qxjyikLu3uQ8IeRDyAELWq2vtm8Kv3ho4CrljHHQpfUMD4me0RxByR4x3hizb6yW_0hD8-VMKunhRsPQhuIPVrQhXQB_S3uIbUpoiky88Tt703qWlDychD38JeTBGD8MvMf50m_R0BGcJQWwemDjYCNqPI1JC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