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

    <tr>
        <th>Summary</th>
        <td>
            Optimization report text for std::sqrt is arguably misleading on x86/Linux
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          TiborGY
      </td>
    </tr>
</table>

<pre>
    ### Background
https://godbolt.org/z/ob6bT4x7P
Consider the following simple function:
```
const size_t natoms = 12;
const size_t distsPerGeom = (natoms*(natoms-1))/2;
double CompareDistmats(double* distmat1, double* distmat2){
    double RMSD = 0.0;
    for (size_t i=0; i<distsPerGeom; i++){
            RMSD += std::pow(distmat1[i]-distmat2[i],2);
    }
        return std::sqrt(RMSD / distsPerGeom);
}
```
Compiling this for x86-64/Linux, then looking at the optimization report, one of the missed optimizations that is reported for this function is an inability to inline `sqrt`:

> Missed - sqrt will not be inlined into CompareDistmats (5:0) because its definition is unavailable

I feel like _on its own_ this text is confusing and uninformative.

1. Clang _does_ inline `sqrt` in the form of the `vsqrtsd` instruction. It is guarded behind a branch, yes, but that is not what "will not be inlined" means.
2. The reason why the above happens is of course because on Linux, math functions from C (such as `sqrt`) are supposed to set `ERRNO` if the argument is negative. If Clang is instructed to neglect that convention with `-fno-math-errno`, then the compare & branch to the library function is gone, leaving behind only the `vsqrtsd` instruction. Simultaneously, the message talking about the missed inlining opportunity is also gone.
I feel like this can lead to confusion for new users. The definition of sqrt has not suddenly became available, the change was that inserting a call to it has become unnecessary.

### Proposal
I think the best way to improve the situation is not to touch the missed inlining mesasge, but to add a new missed-optimization report, which gets issued when a call to a library math function had to be inserted purely because of `ERRNO`-related reasons.
Something like:
> Missed - branch to library sqrt has been inserted due to errno convention on Linux
or
> Missed - inlined sqrt version required a guard due to errno convention on Linux
</pre>
<img width="1px" height="1px" alt="" src="http://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