diff mbox

RFC: Patch to avoid using _finite calls in some cases (including in libmvec names)

Message ID ea70c1dee76d6c2253bac2940e0c09ab6c90102a.camel@marvell.com
State RFC
Headers show

Commit Message

Steve Ellcey March 13, 2019, 8:25 p.m. UTC
This RFC is related to this glibc patch:

https://sourceware.org/ml/libc-alpha/2019-03/msg00106.html

and to this GCC discussion:

https://gcc.gnu.org/ml/gcc-patches/2019-03/msg00532.html


The basic issue is that if you call a function like 'expf' with
GCC -Ofast, the glibc header file 'math-finite.h' gives it the
assembler name of __expf_finite.  Then, if you have a vectorized
version of this inlibmvec, GCC could wind up calling something like
'_ZGVnN4v___expf_finite' instead of '_ZGVnN4v_expf', even though these
are the same functionality on Aarch64 and X86.

There are a number of ways to handle this:

In my patch I just made _ZGVnN4v___expf_finite an alias for _ZGVnN4v_expf
and exported both from libmvec.so.  Joseph didn't like having the two
different names visible as part of the interface between GCC and glibc.

On X86 they have libmvec_nonshared.a, the *_finite routines are defined
here and they just call the 'normal' functions that are in libmvec.so.
I don't like this method because it means you have two calls to get to
the libmvec routine.

Joseph suggested putting a '__vector_name__' attribute on functions that
have libmvec vector version, then we could use that name (expf) instead
of the assembler name (__expf_finite) to form the libmvec name.  The 
objection here is that both C and Fortran would have to handle this new
attribute and anyone building glibc with a non GCC compiler would also
have to add support for the new attribute.



Here is another idea that I have and I would like to get opinions on
this approach.

On Aarch64, expf and expf_finite (and the other exp routines) are
aliases for each other and the vector versions would also be aliases,
so I would like to change math-finite.h to allow platforms to decide
whether or not to use the _finite names in calls.  This would affect
both the scalar and vector versions and all types (float, double, long
double).

As Joseph has said in the email from the threads referenced there is
really no reason why scalar and vector versions of different types
of a function like 'exp (expf, expl, etc) would agree on whether or
not they need different versions but in practice I am not sure this
will come up.  Here is an untested patch to give you an idea of what
I would like to do, obviously a real patch would be extend to put
the rest of the __MATH_REDIRCALL calls in math-finite.h inside of
ifdefs.  

Comments?

Steve Ellcey
sellcey@marvell.com


2019-13-03  Steve Ellcey  <sellcey@marvell.com>

	* bits/math-finite-funcs.h: New file.
	* bits/math-finite.h (exp): Put in ifdef.
	* math/Makefile: (headers): Add bits/math-finite-funcs.h.
	* math/math.h: Include bits/math-finite-funcs.h.
	* sysdeps/aarch64/fpu/bits/math-finite-funcs.h: New file.

Comments

Joseph Myers March 13, 2019, 9:46 p.m. UTC | #1
On Wed, 13 Mar 2019, Steve Ellcey wrote:

> Joseph suggested putting a '__vector_name__' attribute on functions that
> have libmvec vector version, then we could use that name (expf) instead
> of the assembler name (__expf_finite) to form the libmvec name.  The 
> objection here is that both C and Fortran would have to handle this new
> attribute and anyone building glibc with a non GCC compiler would also
> have to add support for the new attribute.

Fortran would not have to handle that attribute.  Fortran never gets 
_finite names in the first place because it can't parse the C header which 
is how C code gets the _finite names.

In the powerpc64le long double context, Fortran (and other languages 
working with C libraries such as D) not parsing C headers requires some 
special compiler support to handle remapping the names of relevant library 
functions (or else glibc support for generating lists of remappings for 
use from other languages, etc.).  But in the libmvec context, those 
languages not parsing C headers means that _finite isn't an issue at all 
for libmvec use for such languages.

> On Aarch64, expf and expf_finite (and the other exp routines) are
> aliases for each other and the vector versions would also be aliases,

That's not the case for expl, which by itself makes this patch suboptimal 
(losing use of __expl_finite while it still does something more efficient 
than expl).

> so I would like to change math-finite.h to allow platforms to decide
> whether or not to use the _finite names in calls.  This would affect
> both the scalar and vector versions and all types (float, double, long
> double).

We also know that significant changes to handling of _finite functions 
will be needed to avoid double redirections (which don't actually work) 
for non-default long double formats.  See bug 23292 (and my review saying 
this indicates merging bits/math-finite.h into bits/mathcalls.h).  It's 
not clear how a version using __MATHCALL_FINITE or similar to declare 
functions in bits/mathcalls.h could readily handle such per-function, 
per-architecture, per-type conditionals.

I still think independent asm names for scalar and vector functions is the 
right approach here.
diff mbox

Patch

diff --git a/bits/math-finite-funcs.h b/bits/math-finite-funcs.h
index e69de29..1ac64ac 100644
--- a/bits/math-finite-funcs.h
+++ b/bits/math-finite-funcs.h
@@ -0,0 +1,31 @@ 
+/* List of functions where the *_finite versions match the normal versions.
+   Copyright (C) 2011-2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifndef _MATH_H
+# error "Never use <bits/math-finite-funcs.h> directly; include <math.h> instead."
+#endif
+
+/* By default we set the names of all the functions in math-finite.h to have
+   the  _finite suffix so that when __FINITE_MATH_ONLY__ is set we call the
+   _finite version of the routines.  On platforms where the normal version and
+   the _finite version are the same we allow the name to not be changed so that
+   the call is to the original name.  This is done by setting
+   '__GLIBC_SKIP_MATH_FINITE_<NAME>'.
+
+   This has the side-affect of changing the name of the vectorized version of
+   the function if such a function exists.  */
diff --git a/bits/math-finite.h b/bits/math-finite.h
index 6141c12..b9a652a 100644
--- a/bits/math-finite.h
+++ b/bits/math-finite.h
@@ -65,7 +65,9 @@  __MATH_REDIRCALL (atanh, , (_Mdouble_));
 __MATH_REDIRCALL (cosh, , (_Mdouble_));
 
 /* exp.  */
+#ifndef __GLIBC_SKIP_MATH_FINITE_EXP
 __MATH_REDIRCALL (exp, , (_Mdouble_));
+#endif
 
 #if __GLIBC_USE (IEC_60559_FUNCS_EXT)
 /* exp10.  */
diff --git a/math/Makefile b/math/Makefile
index cb4eaec..4306289 100644
--- a/math/Makefile
+++ b/math/Makefile
@@ -25,8 +25,8 @@  include ../Makeconfig
 headers		:= math.h bits/mathcalls.h bits/mathinline.h \
 		   fpu_control.h complex.h bits/cmathcalls.h fenv.h \
 		   bits/fenv.h bits/fenvinline.h bits/mathdef.h tgmath.h \
-		   bits/math-finite.h bits/math-vector.h \
-		   finclude/math-vector-fortran.h \
+		   bits/math-finite.h bits/math-finite-funcs.h \
+		   bits/math-vector.h finclude/math-vector-fortran.h \
 		   bits/libm-simd-decl-stubs.h bits/iscanonical.h \
 		   bits/flt-eval-method.h bits/fp-fast.h bits/fp-logb.h \
 		   bits/long-double.h bits/mathcalls-helper-functions.h \
diff --git a/math/math.h b/math/math.h
index ffbc24a..8ac35e6 100644
--- a/math/math.h
+++ b/math/math.h
@@ -1240,6 +1240,10 @@  iszero (__T __val)
 # include <bits/mathinline.h>
 #endif
 
+/* Get machine-depenedent list of functions that do not need
+   finite versions.  */
+#include <bits/math-finite-funcs.h>
+
 /* Define special entry points to use when the compiler got told to
    only expect finite results.  */
 #if defined __FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ > 0
diff --git a/sysdeps/aarch64/fpu/bits/math-finite-funcs.h b/sysdeps/aarch64/fpu/bits/math-finite-funcs.h
index e69de29..e6e4154 100644
--- a/sysdeps/aarch64/fpu/bits/math-finite-funcs.h
+++ b/sysdeps/aarch64/fpu/bits/math-finite-funcs.h
@@ -0,0 +1,23 @@ 
+/* Platform specific list of functions that do not need *_finite versions.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifndef _MATH_H
+# error "Never use <bits/math-finite-funcs.h> directly; include <math.h> instead."
+#endif
+
+#define __GLIBC_SKIP_MATH_FINITE_EXP 1