[cfe-dev] [llvm-dev] generate vectorized code

Mehdi Amini via cfe-dev cfe-dev at lists.llvm.org
Fri Mar 18 14:52:14 PDT 2016


> On Mar 18, 2016, at 2:43 PM, Rail Shafigulin <rail at esenciatech.com> wrote:
> 
> I see vectorization happening on this example (see below).
> 
> 
>> 
>> Any help is appreciated.
>>  
>> 
>> -- 
>> Mehdi
>> 
>> 
>> 
>> -- 
>> Rail Shafigulin
>> Software Engineer 
>> Esencia Technologies
>> 
>> Forgot to attach a C file. Here it is:
>> 
>> #define N 32
>> 
>> int main () {
>> 
>>   int  a[N], b[N];
>>   int c[N];
>> 
>>   for (int i = 0; i < N; ++i)
>>        c[i] = a[i] + b[i];
>> 
>>   int sum=0;
>>   for (int i = 0; i < N; ++i)
>>        sum += c[i];
>> 
>>   return sum;
>> }
>> 
> 
> This will be vectorized without any insertelement, here is a few lines extracted from the output of clang on this code:
> 
>   %wide.load8.6 = load <4 x i32>* %48, align 16, !tbaa !2
>   %49 = add nsw <4 x i32> %wide.load8.6, %wide.load.6
>   %50 = getelementptr inbounds [32 x i32]* %c, i64 0, i64 24
>   %51 = bitcast i32* %50 to <4 x i32>*
>   store <4 x i32> %49, <4 x i32>* %51, align 16, !tbaa !2
> 
> Because you didn't write the example as I described it, i.e. taking integer, doing a few arithmetic and writing result to contiguous memory, the vectorizer will be able to load directly vectors from memory, operates on them, and store the results. For example try with the following C code:
> 
> void foo (int a1, int a2, int a3, int a4, int b1, int b2, int b3, int b4, int *res) {
>   res[0] = a1 + b1 * 2;
>   res[1] = a2 + b2 * 2;
>   res[2] = a3 + b3 * 2;
>   res[3] = a4 + b4 * 2;
> }
> 
> 
> That's for the clang part, you can look at the vectorizer lit test to have examples of IR before/after vectorization.
> 
> -- 
> Mehdi
> 
> Just out of curiosity how did you know that your foo code will produce vectorized code?

I read the source code for the SLP Vectorizer ;)
(other than looking at unit tests, this is another good way of learning of LLVM works)

> I tried code similar to yours without any multiplication and no vectors were generated.

It is a matter of cost model: there need to be a few arithmetic instruction to balance the cost of building a vector.

-- 
Mehdi


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160318/0198d22f/attachment.html>


More information about the cfe-dev mailing list