SIMD intrinsics / gcc vectors

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

SIMD intrinsics / gcc vectors

Jeremy Sanders
Dear all

In C/C++ it's nice to be able to use the Intel intrinsics for SSE/AVX to
achieve large speedups, beyond what the automatic vectoriser in gcc can
achieve. gcc also has some vector extensions which are instruction-set
generic. As far as I can see, the header files translate the standard
intrinsic names to builtin gcc functions.

Is there some way to access the vector extensions, intrinsics or gcc
builtins from fortran code? Has someone looked into this?

Thanks

Jeremy
Reply | Threaded
Open this post in threaded view
|

Re: SIMD intrinsics / gcc vectors

Tobias Burnus-3
Dear Jeremy,

I think there is only
* the OpenMP 'omp simd' directives, see OpenMP spec.
(they can be enabled independently from the rest of OpenMP)
* a limited set of directives, cf.
https://gcc.gnu.org/onlinedocs/gfortran/GNU-Fortran-Compiler-Directives.html

Regarding vectorization: Newer glibc* ship with the file
/usr/include/finclude/math-vector-fortran.h
which contains lines such as:
   !GCC$ builtin (cos) attributes simd (notinbranch) if('x86_64')
which use a special vectorized version of the trigonometric
functions  + exp/log/pow.
This file is automatically included by gfortran. (I think this
happens at build time of GCC if glibc is new enough, but I have
not checked.)

For C/C++, it works likewise for
   /usr/include/bits/math-vector.h
except that file is only used with -ffast-math (which sets
__FAST_MATH__)

This "simd" attribute is described in
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
and I saw draft patches for extend this syntax and to support ARM
as well.


I think that's all, what's supported.

Does this cover *most* what you were thinking of? Or do you have
something specific additionally in mind? (I know that the C/C++
compiler supports several more means.)

Tobias

*Newer glibc = GNU C Library 2.22 of August 2015; see
https://sourceware.org/legacy-ml/libc-alpha/2015-08/msg00609.html

-----------------
Mentor Graphics (Deutschland) GmbH, Arnulfstraße 201, 80634 München / Germany
Registergericht München HRB 106955, Geschäftsführer: Thomas Heurung, Alexander Walter
Reply | Threaded
Open this post in threaded view
|

Re: SIMD intrinsics / gcc vectors

gcc - fortran mailing list
On Wed, Mar 11, 2020 at 9:39 AM Tobias Burnus <[hidden email]> wrote:

>
> Dear Jeremy,
>
> I think there is only
> * the OpenMP 'omp simd' directives, see OpenMP spec.
> (they can be enabled independently from the rest of OpenMP)
> * a limited set of directives, cf.
> https://gcc.gnu.org/onlinedocs/gfortran/GNU-Fortran-Compiler-Directives.html
>
> Regarding vectorization: Newer glibc* ship with the file
> /usr/include/finclude/math-vector-fortran.h
> which contains lines such as:
>    !GCC$ builtin (cos) attributes simd (notinbranch) if('x86_64')
> which use a special vectorized version of the trigonometric
> functions  + exp/log/pow.
> This file is automatically included by gfortran. (I think this
> happens at build time of GCC if glibc is new enough, but I have
> not checked.)
>
> For C/C++, it works likewise for
>    /usr/include/bits/math-vector.h
> except that file is only used with -ffast-math (which sets
> __FAST_MATH__)
>
> This "simd" attribute is described in
> https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
> and I saw draft patches for extend this syntax and to support ARM
> as well.
>
>
> I think that's all, what's supported.
>
> Does this cover *most* what you were thinking of? Or do you have
> something specific additionally in mind? (I know that the C/C++
> compiler supports several more means.)

I think he is talking about using intrinsics to manually write vector
code like those from xmmintrin.h, the C intrinsic headers provide
vector types and functions to operate on them mapping to CPU
instructions, in this case for the x86 architecture.  There's similar
intrinsics for PPC altivec and VSX and for ARM neon.

Richard.

> Tobias
>
> *Newer glibc = GNU C Library 2.22 of August 2015; see
> https://sourceware.org/legacy-ml/libc-alpha/2015-08/msg00609.html
>
> -----------------
> Mentor Graphics (Deutschland) GmbH, Arnulfstraße 201, 80634 München / Germany
> Registergericht München HRB 106955, Geschäftsführer: Thomas Heurung, Alexander Walter
Reply | Threaded
Open this post in threaded view
|

Re: SIMD intrinsics / gcc vectors

Tobias Burnus-5
On 3/11/20 11:01 AM, Richard Biener wrote:
> I think he is talking about using intrinsics to manually write vector

I assumed so – but I thought it might be still useful to list
what is actually supported instead of just saying "no".

> code like those from xmmintrin.h, the C intrinsic headers provide
> vector types and functions to operate on them mapping to CPU

The starting problem is that there is currently no way to create
vector types in gfortran. I thought the Intel Fortran compiler might
support something as vendor extension, but a quick search also only
found annotations like for OpenMP's simd.  (ifort's vector attribute is
more powerful, but I think it also does not permit defining vectors.)

Thus, the first step is to come up with a syntax to specify a vector type.

I am also not sure whether it would get used as most Fortran code is
written such that it is completely hardware/compiler independent. And
there is some opposition among the gfortran developers adding and especially
inventing vendor extensions.

Tobias

-----------------
Mentor Graphics (Deutschland) GmbH, Arnulfstraße 201, 80634 München / Germany
Registergericht München HRB 106955, Geschäftsführer: Thomas Heurung, Alexander Walter
Reply | Threaded
Open this post in threaded view
|

Re: SIMD intrinsics / gcc vectors

gcc - fortran mailing list
On Wed, Mar 11, 2020 at 11:27 AM Tobias Burnus <[hidden email]> wrote:

>
> On 3/11/20 11:01 AM, Richard Biener wrote:
> > I think he is talking about using intrinsics to manually write vector
>
> I assumed so – but I thought it might be still useful to list
> what is actually supported instead of just saying "no".
>
> > code like those from xmmintrin.h, the C intrinsic headers provide
> > vector types and functions to operate on them mapping to CPU
>
> The starting problem is that there is currently no way to create
> vector types in gfortran. I thought the Intel Fortran compiler might
> support something as vendor extension, but a quick search also only
> found annotations like for OpenMP's simd.  (ifort's vector attribute is
> more powerful, but I think it also does not permit defining vectors.)
>
> Thus, the first step is to come up with a syntax to specify a vector type.

real(8) v(4) [[gnu_vector]]

and replace [[gnu_vector]] with a standards compliant attribute syntax.
That is, I'd first try to expose GCCs generic vectors somehow and
piggy-backing on the fortran array syntax seems most natural to me
since fortran also allows array arithmetic and even vector permutes
via array reshaping!  I hope there's some nicer syntax than the
"pragma" one.

The various intrinsic headers could then be mapped to built-in modules
for example (or real modules installed together with gfortran).

Richard.

> I am also not sure whether it would get used as most Fortran code is
> written such that it is completely hardware/compiler independent. And
> there is some opposition among the gfortran developers adding and especially
> inventing vendor extensions.
>
> Tobias
>
> -----------------
> Mentor Graphics (Deutschland) GmbH, Arnulfstraße 201, 80634 München / Germany
> Registergericht München HRB 106955, Geschäftsführer: Thomas Heurung, Alexander Walter
Reply | Threaded
Open this post in threaded view
|

Re: SIMD intrinsics / gcc vectors

gcc - fortran mailing list
Am 11.03.20 um 12:35 schrieb Richard Biener via Fortran:
>> Thus, the first step is to come up with a syntax to specify a vector type.
> real(8) v(4) [[gnu_vector]]
>
> and replace [[gnu_vector]] with a standards compliant attribute syntax.

That means as a comment, otherwise it would be a violation of
the standard.

It would probably make sense to have something like

   real(kind=8), dimension(4) :: v
!GCC$ declare gnu_vector(4) :: v

where we have a standard fortran syntax after GCC$ declare.
This would make the code still compatible with other compilers.

This would also give us the possibility of something like


   real(kind=8), dimension(16) :: v
!GCC$ declare gnu_vector(4) :: v

to declare (from the Fortran end) an array of 16
(kind=8) reals, which the gcc middle end would then
treat as an array of four four-item vectors.
Reply | Threaded
Open this post in threaded view
|

Re: SIMD intrinsics / gcc vectors

gcc - fortran mailing list
On Wed, Mar 11, 2020 at 8:34 PM Thomas Koenig <[hidden email]> wrote:

>
> Am 11.03.20 um 12:35 schrieb Richard Biener via Fortran:
> >> Thus, the first step is to come up with a syntax to specify a vector type.
> > real(8) v(4) [[gnu_vector]]
> >
> > and replace [[gnu_vector]] with a standards compliant attribute syntax.
>
> That means as a comment, otherwise it would be a violation of
> the standard.
>
> It would probably make sense to have something like
>
>    real(kind=8), dimension(4) :: v
> !GCC$ declare gnu_vector(4) :: v
>
> where we have a standard fortran syntax after GCC$ declare.
> This would make the code still compatible with other compilers.
>
> This would also give us the possibility of something like
>
>
>    real(kind=8), dimension(16) :: v
> !GCC$ declare gnu_vector(4) :: v
>
> to declare (from the Fortran end) an array of 16
> (kind=8) reals, which the gcc middle end would then
> treat as an array of four four-item vectors.

Yeah.  It needs some thinking if we can come up with something
that is expressive enough to interface with a module API that
models the intrinsics and at the same time allow correct
operation when using a compiler w/o gnu_vector support
(not using the intrinsics of course).  That is, come up with something
where array and vector semantics match.

Note some of the intrinsics map to target specific builtin functions
which IIRC the gfortran frontend doesn't see or can interface to
(there are the builtin_function and register_builtin_type langhooks
which gfortran defines but not sure about their effect).

Richard.
Reply | Threaded
Open this post in threaded view
|

Re: SIMD intrinsics / gcc vectors

Jeremy Sanders


On 12/03/2020 10:31, Richard Biener via Fortran wrote:

> Yeah.  It needs some thinking if we can come up with something
> that is expressive enough to interface with a module API that
> models the intrinsics and at the same time allow correct
> operation when using a compiler w/o gnu_vector support
> (not using the intrinsics of course).  That is, come up with something
> where array and vector semantics match.
>
> Note some of the intrinsics map to target specific builtin functions
> which IIRC the gfortran frontend doesn't see or can interface to
> (there are the builtin_function and register_builtin_type langhooks
> which gfortran defines but not sure about their effect).

This seems interesting. I agree that a vanilla-compatible interface
would be better than inventing new syntax. I would use it, but I'm more
at home using C++, though I have to do some work in Fortran. I'd
understand that there's not much call for this from traditional Fortran
coders.

Cheers

Jeremy