-
Notifications
You must be signed in to change notification settings - Fork 15.1k
Description
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.
- Clang does inline
sqrt
in the form of thevsqrtsd
instruction. It is guarded behind a branch, yes, but that is not what "will not be inlined" means. - The reason why the above happens is of course because on Linux, math functions from C (such as
sqrt
) are supposed to setERRNO
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 thevsqrtsd
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