How can I disable vectorization while using GCC?

2020-07-06 04:01发布

问题:

I am compiling my code using following command:

gcc -O3 -ftree-vectorizer-verbose=6 -msse4.1 -ffast-math 

With this all the optimizations are enabled.

But I want to disable vectorization while keeping the other optimizations.

回答1:

Most of the GCC switches can be used with a no prefix to disable their behavior. Try with -fno-tree-vectorize (after -O3 on the command line).



回答2:

you can also selectively enable and disable vectorization with the optimize function attributes or pragmas

http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

http://gcc.gnu.org/onlinedocs/gcc/Function-Specific-Option-Pragmas.html

e.g.

__attribute__((optimize("no-tree-vectorize")))
void f(double * restrict a, double * restrict b)
{
    for (int i = 0; i < 256; i++)
        a[i] += b[i];
}


回答3:

Excellent, now that gcc has become more aggressive at vectorizing e.g.

extern "C" __attribute__((optimize("no-tree-vectorize")))
/* Subroutine */
int s111_ (integer * ntimes, integer * ld, integer * n,
           real * ctime, real * dtime,
           real * __restrict a, real * b, real * c__, real * d__,
           real * e, real * aa, real * bb, real * cc)
{
    ....
    for (i__ = 2; i__ <= i__2; i__ += 2)
        a[i__] = a[i__ - 1] + b[i__];
    ....

In the case posted above, removing restrict used to do the job, but now g++ 6.0 can't be stopped from vectorizing by removing __restrict.