auto vectorizer

Marc Glisse marc.glisse@normalesup.org
Wed Aug 26 01:15:00 GMT 2009


On Tue, 25 Aug 2009, Ira Rosen wrote:

>> My loop doesn't vectorize. The message says:
>>
>> ./TestAutoVect.cpp:31: note: not vectorized: unhandled data-ref
>>
>> 22
>> //-----------------------------------------------------------------------------
>
>> 23 void MaskRGBA(
>> 24       const int *in,
>> 25       const int * const inEnd,
>> 26       int *out,
>> 27       const int * const m)
>> 28 {
>> 29   for (;in<inEnd; ++in, ++out)
>> 30     {
>> 31     *out=*in&*m;
>> 32     }
>> 33 }
>
> The problem is that '*m' cannot be taken out of the loop because the alias
> analysis cannot prove that it doesn't alias other memory accesses in the
> loop. This causes data references analysis to fail since the above access
> is loop invariant. Therefore, this loop is not vectorized.
>
>>
>> I found that when I change it to this
>>
>> 23
>> //-----------------------------------------------------------------------------
>
>> 24 void MaskRGBA(
>> 25       const int *in,
>> 26       const int * const inEnd,
>> 27       int *out,
>> 28       const int * const mask)
>> 29 {
>> 30   const int m=*mask;
>> 31   for (;in<inEnd; ++in, ++out)
>> 32     {
>> 33     *out=*in&m;
>> 34     }
>> 35 }
>
> Here there is no such problem.

Actually there still is possible aliasing here, if (out-in) is positive 
and small enough. However, gcc is clever enough to test if this is the 
case and use the vectorized code only when it is safe. Supposedly, it 
could as well check if m is somewhere after out, although it increases the 
overhead again, and the appropriate logic is possibly missing in that 
version of the compiler.

> You can also use __restrict__ to help vectorization:

Indeed. Too bad restrict is not standard in C++ :-(

In the example shown here, the mask looks like it should be passed by 
value instead of by pointer, but I guess the real code modifies the mask 
afterwards.

-- 
Marc Glisse



More information about the Gcc-help mailing list