Skip to content

Partially-inline-libcalls should probably issue a missed-optimization message if it inserts a branch #78785

@TiborGY

Description

@TiborGY

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementImproving things as opposed to bug fixing, e.g. new or missing featurellvm:optimizations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions